Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the noise issue of updating build at a fixed interval of 5 sec #24

Merged
merged 1 commit into from
May 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/main/java/io/fabric8/jenkins/openshiftsync/BuildCause.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public class BuildCause extends Cause {

private String buildConfigUid;

private int numStages = -1;

private int numFlowNodes = -1;

private long lastUpdateToOpenshift = -1;

public BuildCause(String uid, String namespace, String name, String gitUri, String commit, String buildConfigUid) {
this.uid = uid;
this.namespace = namespace;
Expand All @@ -44,6 +50,13 @@ public BuildCause(String uid, String namespace, String name, String gitUri, Stri
this.buildConfigUid = buildConfigUid;
}

public BuildCause(String uid, String namespace, String name, String gitUri, String commit, String buildConfigUid, int numStages, int numFlowNodes, long lastUpdateToOpenshift) {
this( uid, namespace, name, gitUri, commit, buildConfigUid);
this.numStages = numStages;
this.numFlowNodes = numFlowNodes;
this.lastUpdateToOpenshift = lastUpdateToOpenshift;
}

public BuildCause(Build build, String buildConfigUid) {
this.buildConfigUid = buildConfigUid;
if (build == null || build.getMetadata() == null) {
Expand Down Expand Up @@ -104,4 +117,29 @@ public String getCommit() {
public String getBuildConfigUid() {
return buildConfigUid;
}

public int getNumStages() {
return numStages;
}

public void setNumStages(int numStages) {
this.numStages = numStages;
}

public int getNumFlowNodes() {
return numFlowNodes;
}

public void setNumFlowNodes(int numFlowNodes) {
this.numFlowNodes = numFlowNodes;
}

public long getLastUpdateToOpenshift() {
return lastUpdateToOpenshift;
}

public void setLastUpdateToOpenshift(long lastUpdateToOpenshift) {
this.lastUpdateToOpenshift = lastUpdateToOpenshift;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public class BuildSyncRunListener extends RunListener<Run> {
private long pollPeriodMs = 1000 * 5; // 5 seconds
private long delayPollPeriodMs = 1000; // 1 seconds
private String namespace;
//Not used as we have disable this
//private static final long maxDelay = 30000;

private transient Set<Run> runsToPoll = new CopyOnWriteArraySet<>();

Expand Down Expand Up @@ -208,6 +210,49 @@ protected synchronized void pollRun(Run run) {
}
}

private boolean shouldUpdateOpenShiftBuild(BuildCause cause, int latestStageNum, int latestNumFlowNodes, StatusExt status) {
long currTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
logger.fine(String.format("shouldUpdateOpenShiftBuild current time %s last update %s current stage num &s last stage num %s" +
"current flow num %s last flow num %s status %s",
String.valueOf(currTime),
String.valueOf(cause.getLastUpdateToOpenshift()),
String.valueOf(latestStageNum),
String.valueOf(cause.getNumStages()),
String.valueOf(latestNumFlowNodes),
String.valueOf(cause.getNumFlowNodes()),
status.toString()));

// ** ToDO **
// Right now our use case is to not update if there is no change
// So Disabling the 30 sec time limit for our fork.
// Later we can make this configurable and do the changes in both
// fork and upstream

/*
// if we have not updated in maxDelay time, update
if (currTime > (cause.getLastUpdateToOpenshift() + maxDelay)) {
return true;
}*/

// if the num of stages has changed, update
if (cause.getNumStages() != latestStageNum) {
return true;
}

// if the num of flow nodes has changed, update
if (cause.getNumFlowNodes() != latestNumFlowNodes) {
return true;
}

// if the run is in some sort of terminal state, update
// Do not update if this is in input stage
if (status != StatusExt.IN_PROGRESS && status != StatusExt.PAUSED_PENDING_INPUT) {
return true;
}

return false;
}

private void upsertBuild(Run run, RunExt wfRunExt) {
if (run == null) {
return;
Expand Down Expand Up @@ -251,6 +296,10 @@ private void upsertBuild(Run run, RunExt wfRunExt) {
if (!wfRunExt.get_links().self.href.matches("^https?://.*$")) {
wfRunExt.get_links().self.setHref(joinPaths(rootUrl, wfRunExt.get_links().self.href));
}

int newNumStages = wfRunExt.getStages().size();
int newNumFlowNodes = 0;

for (StageNodeExt stage : wfRunExt.getStages()) {
FlowNodeExt.FlowNodeLinks links = stage.get_links();
if (!links.self.href.matches("^https?://.*$")) {
Expand All @@ -259,6 +308,9 @@ private void upsertBuild(Run run, RunExt wfRunExt) {
if (links.getLog() != null && !links.getLog().href.matches("^https?://.*$")) {
links.getLog().setHref(joinPaths(rootUrl, links.getLog().href));
}

newNumFlowNodes = newNumFlowNodes + stage.getStageFlowNodes().size();

for (AtomFlowNodeExt node : stage.getStageFlowNodes()) {
FlowNodeExt.FlowNodeLinks nodeLinks = node.get_links();
if (!nodeLinks.self.href.matches("^https?://.*$")) {
Expand All @@ -274,6 +326,11 @@ private void upsertBuild(Run run, RunExt wfRunExt) {
}
}

boolean needToUpdate = this.shouldUpdateOpenShiftBuild(cause, newNumStages, newNumFlowNodes, wfRunExt.getStatus());
if (!needToUpdate) {
return;
}

String json;
try {
json = new ObjectMapper().writeValueAsString(wfRunExt);
Expand Down Expand Up @@ -379,6 +436,10 @@ private void upsertBuild(Run run, RunExt wfRunExt) {
throw e;
}
}

cause.setNumFlowNodes(newNumFlowNodes);
cause.setNumStages(newNumStages);
cause.setLastUpdateToOpenshift(TimeUnit.NANOSECONDS.toMillis(System.nanoTime()));
}
}

Expand All @@ -404,6 +465,7 @@ private String getPendingActionsJson(WorkflowRun run) {
}
}
}

try {
return new ObjectMapper().writeValueAsString(pendingInputActions);
} catch (JsonProcessingException e) {
Expand Down