in computer-k8s-operator/src/main/java/org/apache/hugegraph/computer/k8s/operator/controller/ComputerJobController.java [223:297]
private ComputerJobStatus derivedCRStatus(ComputerJobComponent observed) {
HugeGraphComputerJob computerJob = observed.computerJob();
ComputerJobSpec spec = computerJob.getSpec();
MutableInt failedComponents = new MutableInt(0);
MutableInt succeededComponents = new MutableInt(0);
MutableInt runningComponents = new MutableInt(0);
ComputerJobStatus status = Serialization.clone(computerJob.getStatus());
// ConfigMap
ConfigMap configMap = observed.configMap();
if (configMap != null) {
ComponentState configMapState = new ComponentStateBuilder()
.withName(configMap.getMetadata().getName())
.withState(CommonComponentState.READY.value())
.build();
status.getComponentStates().setConfigMap(configMapState);
} else if (status.getComponentStates().getConfigMap() != null) {
status.getComponentStates().getConfigMap()
.setState(CommonComponentState.DELETED.value());
}
// MasterJob
Job masterJob = observed.masterJob();
ComponentState masterJobState = this.deriveJobStatus(
masterJob,
observed.masterPods(),
status.getComponentStates().getMasterJob(),
Constants.MASTER_INSTANCES,
failedComponents,
succeededComponents,
runningComponents
);
status.getComponentStates().setMasterJob(masterJobState);
// WorkerJob
Job workerJob = observed.workerJob();
ComponentState workerJobState = this.deriveJobStatus(
workerJob,
observed.workerPods(),
status.getComponentStates().getWorkerJob(),
spec.getWorkerInstances(),
failedComponents,
succeededComponents,
runningComponents
);
status.getComponentStates().setWorkerJob(workerJobState);
if (failedComponents.intValue() > ALLOW_FAILED_COMPONENTS) {
status.setJobStatus(JobStatus.FAILED.name());
this.recordFailedEvent(computerJob, masterJobState, workerJobState);
return status;
} else if (succeededComponents.intValue() == TOTAL_COMPONENTS) {
status.setJobStatus(JobStatus.SUCCEEDED.name());
String crName = computerJob.getMetadata().getName();
long cost = this.calculateJobCost(computerJob);
this.recordEvent(computerJob, EventType.NORMAL,
KubeUtil.succeedEventName(crName),
"ComputerJobSucceed",
String.format("Job %s run successfully, took %ss",
crName, cost));
return status;
}
int activeComponents = runningComponents.intValue() +
succeededComponents.intValue();
if (activeComponents == TOTAL_COMPONENTS) {
status.setJobStatus(JobStatus.RUNNING.name());
} else {
status.setJobStatus(JobStatus.INITIALIZING.name());
}
return status;
}