|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.gobblin.service.modules.orchestration; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.LinkedList; |
| 22 | +import java.util.concurrent.ExecutionException; |
| 23 | +import java.util.concurrent.TimeUnit; |
| 24 | + |
| 25 | +import com.google.common.base.Optional; |
| 26 | + |
| 27 | +import lombok.extern.slf4j.Slf4j; |
| 28 | + |
| 29 | +import org.apache.gobblin.annotation.Alpha; |
| 30 | +import org.apache.gobblin.metrics.MetricContext; |
| 31 | +import org.apache.gobblin.metrics.event.EventSubmitter; |
| 32 | +import org.apache.gobblin.metrics.event.TimingEvent; |
| 33 | +import org.apache.gobblin.runtime.api.MultiActiveLeaseArbiter; |
| 34 | +import org.apache.gobblin.service.FlowId; |
| 35 | +import org.apache.gobblin.service.modules.flowgraph.Dag; |
| 36 | +import org.apache.gobblin.service.modules.orchestration.exception.MaybeRetryableException; |
| 37 | +import org.apache.gobblin.service.modules.spec.JobExecutionPlan; |
| 38 | +import org.apache.gobblin.service.monitoring.FlowStatusGenerator; |
| 39 | +import org.apache.gobblin.service.monitoring.JobStatus; |
| 40 | + |
| 41 | + |
| 42 | +/** |
| 43 | + * An implementation of {@link DagProc} that is responsible for clean up {@link Dag} that has been completed |
| 44 | + * or has reached an end state likewise: FAILED, COMPLETE or CANCELED |
| 45 | + * |
| 46 | + */ |
| 47 | +@Slf4j |
| 48 | +@Alpha |
| 49 | +public class CleanUpDagProc extends DagProc { |
| 50 | + |
| 51 | + private MultiActiveLeaseArbiter multiActiveLeaseArbiter; |
| 52 | + |
| 53 | + private DagManagementStateStore dagManagementStateStore; |
| 54 | + |
| 55 | + private MetricContext metricContext; |
| 56 | + |
| 57 | + private Optional<EventSubmitter> eventSubmitter; |
| 58 | + |
| 59 | + private DagManagerMetrics dagManagerMetrics; |
| 60 | + |
| 61 | + private DagStateStore dagStateStore; |
| 62 | + |
| 63 | + private DagStateStore failedDagStateStore; |
| 64 | + |
| 65 | + private DagTaskStream dagTaskStream; |
| 66 | + |
| 67 | + private static final long DAG_FLOW_STATUS_TOLERANCE_TIME_MILLIS = TimeUnit.MINUTES.toMillis(5); |
| 68 | + |
| 69 | + //TODO: instantiate an object of this class |
| 70 | + |
| 71 | + @Override |
| 72 | + protected Object initialize() throws MaybeRetryableException, IOException { |
| 73 | + String dagIdToClean = ""; //TODO: implement this dagID |
| 74 | + if(!this.dagManagementStateStore.hasRunningJobs(dagIdToClean)) { |
| 75 | + Dag<JobExecutionPlan> dag = this.dagManagementStateStore.getDagIdToDags().get(dagIdToClean); |
| 76 | + return dag; |
| 77 | + } |
| 78 | + return null; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + protected Object act(Object state) throws ExecutionException, InterruptedException, IOException { |
| 83 | + Dag<JobExecutionPlan> dag = (Dag<JobExecutionPlan>) state; |
| 84 | + DagManager.DagId dagId = DagManagerUtils.generateDagId(dag); |
| 85 | + LinkedList<Dag.DagNode<JobExecutionPlan>> dagNodeList = this.dagManagementStateStore.getDagToJobs().get(dagId); |
| 86 | + while (!dagNodeList.isEmpty()) { |
| 87 | + Dag.DagNode<JobExecutionPlan> dagNode = dagNodeList.poll(); |
| 88 | + this.dagManagementStateStore.deleteJobState(dagId.toString(), dagNode); |
| 89 | + } |
| 90 | + if (dag.getFlowEvent() == null) { |
| 91 | + // If the dag flow event is not set, then it is successful |
| 92 | + dag.setFlowEvent(TimingEvent.FlowTimings.FLOW_SUCCEEDED); |
| 93 | + } else { |
| 94 | + addFailedDag(dagId.toString(), dag); |
| 95 | + } |
| 96 | + JobStatus flowStatus = dagTaskStream.pollFlowStatus(dag); |
| 97 | + if (flowStatus != null && FlowStatusGenerator.FINISHED_STATUSES.contains(flowStatus.getEventName())) { |
| 98 | + FlowId flowId = DagManagerUtils.getFlowId(dag); |
| 99 | + |
| 100 | + switch (dag.getFlowEvent()) { |
| 101 | + case TimingEvent.FlowTimings.FLOW_SUCCEEDED: |
| 102 | + this.dagManagerMetrics.emitFlowSuccessMetrics(flowId); |
| 103 | + this.dagManagerMetrics.conditionallyMarkFlowAsState(flowId, DagManager.FlowState.SUCCESSFUL); |
| 104 | + break; |
| 105 | + case TimingEvent.FlowTimings.FLOW_FAILED: |
| 106 | + this.dagManagerMetrics.emitFlowFailedMetrics(flowId); |
| 107 | + this.dagManagerMetrics.conditionallyMarkFlowAsState(flowId, DagManager.FlowState.FAILED); |
| 108 | + break; |
| 109 | + case TimingEvent.FlowTimings.FLOW_CANCELLED: |
| 110 | + this.dagManagerMetrics.emitFlowSlaExceededMetrics(flowId); |
| 111 | + this.dagManagerMetrics.conditionallyMarkFlowAsState(flowId, DagManager.FlowState.FAILED); |
| 112 | + break; |
| 113 | + default: |
| 114 | + log.warn("Unexpected flow event {} for dag {}", dag.getFlowEvent(), dagId); |
| 115 | + } |
| 116 | + log.info("Dag {} has finished with status {}; Cleaning up dag from the state store.", dagId, dag.getFlowEvent()); |
| 117 | + cleanUpDag(dagId.toString()); |
| 118 | + } |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + protected void sendNotification(Object result) throws MaybeRetryableException { |
| 124 | + long cleanUpProcessingTime = System.currentTimeMillis(); |
| 125 | + Dag<JobExecutionPlan> dag = (Dag<JobExecutionPlan>) result; |
| 126 | + String dagId = DagManagerUtils.generateDagId(dag).toString(); |
| 127 | + DagManagerUtils.emitFlowEvent(this.eventSubmitter, this.dagManagementStateStore.getDagIdToDags().get(dagId), dag.getFlowEvent()); |
| 128 | + dag.setEventEmittedTimeMillis(cleanUpProcessingTime); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Add a dag to failed dag state store |
| 133 | + */ |
| 134 | + private synchronized void addFailedDag(String dagId, Dag<JobExecutionPlan> dag) { |
| 135 | + try { |
| 136 | + log.info("Adding dag " + dagId + " to failed dag state store"); |
| 137 | + this.failedDagStateStore.writeCheckpoint(this.dagManagementStateStore.getDagIdToDags().get(dagId)); |
| 138 | + } catch (IOException e) { |
| 139 | + log.error("Failed to add dag " + dagId + " to failed dag state store", e); |
| 140 | + } |
| 141 | + this.dagManagementStateStore.getFailedDagIds().add(dagId); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Note that removal of a {@link Dag} entry in {@link #dags} needs to be happen after {@link #cleanUp()} |
| 146 | + * since the real {@link Dag} object is required for {@link #cleanUp()}, |
| 147 | + * and cleaning of all relevant states need to be atomic |
| 148 | + * @param dagId |
| 149 | + */ |
| 150 | + private synchronized void cleanUpDag(String dagId) { |
| 151 | + log.info("Cleaning up dagId {}", dagId); |
| 152 | + // clears flow event after cancelled job to allow resume event status to be set |
| 153 | + this.dagManagementStateStore.getDagIdToDags().get(dagId).setFlowEvent(null); |
| 154 | + try { |
| 155 | + this.dagStateStore.cleanUp(this.dagManagementStateStore.getDagIdToDags().get(dagId)); |
| 156 | + } catch (IOException ioe) { |
| 157 | + log.error(String.format("Failed to clean %s from backStore due to:", dagId), ioe); |
| 158 | + } |
| 159 | + this.dagManagementStateStore.getDagIdToDags().remove(dagId); |
| 160 | + this.dagManagementStateStore.getDagToJobs().remove(dagId); |
| 161 | + } |
| 162 | +} |
0 commit comments