xintongsong commented on code in PR #546: URL: https://github.com/apache/flink-agents/pull/546#discussion_r3108158254
########## runtime/src/main/java/org/apache/flink/agents/runtime/operator/DurableExecutionManager.java: ########## @@ -0,0 +1,285 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.flink.agents.runtime.operator; + +import org.apache.flink.agents.api.Event; +import org.apache.flink.agents.api.context.MemoryUpdate; +import org.apache.flink.agents.plan.AgentConfiguration; +import org.apache.flink.agents.plan.actions.Action; +import org.apache.flink.agents.runtime.actionstate.ActionState; +import org.apache.flink.agents.runtime.actionstate.ActionStateStore; +import org.apache.flink.agents.runtime.actionstate.KafkaActionStateStore; +import org.apache.flink.agents.runtime.async.ContinuationContext; +import org.apache.flink.agents.runtime.context.ActionStatePersister; +import org.apache.flink.agents.runtime.context.RunnerContextImpl; +import org.apache.flink.annotation.VisibleForTesting; +import org.apache.flink.api.common.state.ListState; +import org.apache.flink.api.common.state.ListStateDescriptor; +import org.apache.flink.api.common.typeinfo.TypeInformation; +import org.apache.flink.runtime.state.OperatorStateBackend; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nullable; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.apache.flink.agents.api.configuration.AgentConfigOptions.ACTION_STATE_STORE_BACKEND; +import static org.apache.flink.agents.runtime.actionstate.ActionStateStore.BackendType.KAFKA; + +class DurableExecutionManager implements ActionStatePersister, AutoCloseable { + + private static final Logger LOG = LoggerFactory.getLogger(DurableExecutionManager.class); + + private static final String RECOVERY_MARKER_STATE_NAME = "recoveryMarker"; + + private ActionStateStore actionStateStore; + private transient ListState<Object> recoveryMarkerOpState; + private final Map<Long, Map<Object, Long>> checkpointIdToSeqNums; + + private final Map<ActionTask, RunnerContextImpl.DurableExecutionContext> + actionTaskDurableContexts; + private final Map<ActionTask, ContinuationContext> continuationContexts; + private final Map<ActionTask, String> pythonAwaitableRefs; + + DurableExecutionManager(@Nullable ActionStateStore actionStateStore) { + this.actionStateStore = actionStateStore; + this.checkpointIdToSeqNums = new HashMap<>(); + this.actionTaskDurableContexts = new HashMap<>(); + this.continuationContexts = new HashMap<>(); + this.pythonAwaitableRefs = new HashMap<>(); + } + + void maybeInitActionStateStore(AgentConfiguration config) { + if (actionStateStore == null + && KAFKA.getType().equalsIgnoreCase(config.get(ACTION_STATE_STORE_BACKEND))) { + LOG.info("Using Kafka as backend of action state store."); + actionStateStore = new KafkaActionStateStore(config); + } + } + + boolean hasDurableStore() { + return actionStateStore != null; + } + + void initRecoveryMarkerState(OperatorStateBackend operatorStateBackend) throws Exception { + if (actionStateStore != null) { + recoveryMarkerOpState = + operatorStateBackend.getUnionListState( + new ListStateDescriptor<>( + RECOVERY_MARKER_STATE_NAME, TypeInformation.of(Object.class))); + } + } + + // Note: Re-creates the union list state descriptor here because handleRecovery() is called + // from initializeState() which runs BEFORE open(), so recoveryMarkerOpState is not yet + // initialized. The descriptor name matches exactly, so Flink returns the same state. + void handleRecovery(OperatorStateBackend operatorStateBackend) throws Exception { + if (actionStateStore != null) { + List<Object> markers = new ArrayList<>(); + ListState<Object> markerState = + operatorStateBackend.getUnionListState( + new ListStateDescriptor<>( + RECOVERY_MARKER_STATE_NAME, TypeInformation.of(Object.class))); + Iterable<Object> recoveryMarkers = markerState.get(); + if (recoveryMarkers != null) { + recoveryMarkers.forEach(markers::add); + } + LOG.info("Rebuilding action state from {} recovery markers", markers.size()); + actionStateStore.rebuildState(markers); + } + } + + ActionState maybeGetActionState(Object key, long sequenceNum, Action action, Event event) Review Comment: I'd suggest to return an `Optional` or mark `@Nullable` for methods that may return `null`. ########## runtime/src/main/java/org/apache/flink/agents/runtime/operator/ActionExecutionOperator.java: ########## @@ -253,82 +132,42 @@ public void setup( @Override public void open() throws Exception { super.open(); - reusedStreamRecord = new StreamRecord<>(null); - // init sensoryMemState - MapStateDescriptor<String, MemoryObjectImpl.MemoryItem> sensoryMemStateDescriptor = - new MapStateDescriptor<>( - "sensoryMemory", - TypeInformation.of(String.class), - TypeInformation.of(MemoryObjectImpl.MemoryItem.class)); - sensoryMemState = getRuntimeContext().getMapState(sensoryMemStateDescriptor); - - // init shortTermMemState - MapStateDescriptor<String, MemoryObjectImpl.MemoryItem> shortTermMemStateDescriptor = - new MapStateDescriptor<>( - "shortTermMemory", - TypeInformation.of(String.class), - TypeInformation.of(MemoryObjectImpl.MemoryItem.class)); - shortTermMemState = getRuntimeContext().getMapState(shortTermMemStateDescriptor); + + stateManager.initializeKeyedStates(getRuntimeContext()); + stateManager.initializeOperatorStates(getOperatorStateBackend()); resourceCache = new ResourceCache(agentPlan.getResourceProviders()); metricGroup = new FlinkAgentsMetricGroupImpl(getMetricGroup()); builtInMetrics = new BuiltInMetrics(metricGroup, agentPlan); - keySegmentQueue = new SegmentedQueue(); + eventRouter.open(builtInMetrics); - maybeInitActionStateStore(); - - if (actionStateStore != null) { - // init recovery marker state for recovery marker persistence - recoveryMarkerOpState = - getOperatorStateBackend() - .getUnionListState( - new ListStateDescriptor<>( - RECOVERY_MARKER_STATE_NAME, - TypeInformation.of(Object.class))); - } - // init sequence number state for per key message ordering - sequenceNumberKState = - getRuntimeContext() - .getState( - new ValueStateDescriptor<>( - MESSAGE_SEQUENCE_NUMBER_STATE_NAME, Long.class)); - - // init agent processing related state - actionTasksKState = - getRuntimeContext() - .getListState( - new ListStateDescriptor<>( - "actionTasks", TypeInformation.of(ActionTask.class))); - pendingInputEventsKState = - getRuntimeContext() - .getListState( - new ListStateDescriptor<>( - PENDING_INPUT_EVENT_STATE_NAME, - TypeInformation.of(Event.class))); - // We use UnionList here to ensure that the task can access all keys after parallelism - // modifications. - // Subsequent steps {@link #tryResumeProcessActionTasks} will then filter out keys that do - // not belong to the key range of current task. - currentProcessingKeysOpState = - getOperatorStateBackend() - .getUnionListState( - new ListStateDescriptor<>( - "currentProcessingKeys", TypeInformation.of(Object.class))); + durableExecManager.maybeInitActionStateStore(agentPlan.getConfig()); + durableExecManager.initRecoveryMarkerState(getOperatorStateBackend()); // init PythonActionExecutor and PythonResourceAdapter - initPythonEnvironment(); - - // init executor for Java async execution - continuationActionExecutor = - new ContinuationActionExecutor( + pythonBridge = new PythonBridgeManager(); + pythonBridge.open( + agentPlan, + resourceCache, + getExecutionConfig(), + getRuntimeContext().getDistributedCache(), + getContainingTask().getEnvironment().getTaskManagerInfo().getTmpDirectories(), + getRuntimeContext().getJobInfo().getJobId(), + metricGroup, + this::checkMailboxThread, + stateManager.getJobIdentifier()); Review Comment: It's a bit against intuitive that job id is fetched from the state manager. I think it should be maintained in the operator. ########## runtime/src/main/java/org/apache/flink/agents/runtime/operator/DurableExecutionManager.java: ########## @@ -0,0 +1,285 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.flink.agents.runtime.operator; + +import org.apache.flink.agents.api.Event; +import org.apache.flink.agents.api.context.MemoryUpdate; +import org.apache.flink.agents.plan.AgentConfiguration; +import org.apache.flink.agents.plan.actions.Action; +import org.apache.flink.agents.runtime.actionstate.ActionState; +import org.apache.flink.agents.runtime.actionstate.ActionStateStore; +import org.apache.flink.agents.runtime.actionstate.KafkaActionStateStore; +import org.apache.flink.agents.runtime.async.ContinuationContext; +import org.apache.flink.agents.runtime.context.ActionStatePersister; +import org.apache.flink.agents.runtime.context.RunnerContextImpl; +import org.apache.flink.annotation.VisibleForTesting; +import org.apache.flink.api.common.state.ListState; +import org.apache.flink.api.common.state.ListStateDescriptor; +import org.apache.flink.api.common.typeinfo.TypeInformation; +import org.apache.flink.runtime.state.OperatorStateBackend; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nullable; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.apache.flink.agents.api.configuration.AgentConfigOptions.ACTION_STATE_STORE_BACKEND; +import static org.apache.flink.agents.runtime.actionstate.ActionStateStore.BackendType.KAFKA; + +class DurableExecutionManager implements ActionStatePersister, AutoCloseable { + + private static final Logger LOG = LoggerFactory.getLogger(DurableExecutionManager.class); + + private static final String RECOVERY_MARKER_STATE_NAME = "recoveryMarker"; + + private ActionStateStore actionStateStore; + private transient ListState<Object> recoveryMarkerOpState; + private final Map<Long, Map<Object, Long>> checkpointIdToSeqNums; + + private final Map<ActionTask, RunnerContextImpl.DurableExecutionContext> + actionTaskDurableContexts; + private final Map<ActionTask, ContinuationContext> continuationContexts; + private final Map<ActionTask, String> pythonAwaitableRefs; Review Comment: This also leads to the violation of "no manager-to-manager references" design goal. ########## runtime/src/main/java/org/apache/flink/agents/runtime/operator/DurableExecutionManager.java: ########## @@ -0,0 +1,285 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.flink.agents.runtime.operator; + +import org.apache.flink.agents.api.Event; +import org.apache.flink.agents.api.context.MemoryUpdate; +import org.apache.flink.agents.plan.AgentConfiguration; +import org.apache.flink.agents.plan.actions.Action; +import org.apache.flink.agents.runtime.actionstate.ActionState; +import org.apache.flink.agents.runtime.actionstate.ActionStateStore; +import org.apache.flink.agents.runtime.actionstate.KafkaActionStateStore; +import org.apache.flink.agents.runtime.async.ContinuationContext; +import org.apache.flink.agents.runtime.context.ActionStatePersister; +import org.apache.flink.agents.runtime.context.RunnerContextImpl; +import org.apache.flink.annotation.VisibleForTesting; +import org.apache.flink.api.common.state.ListState; +import org.apache.flink.api.common.state.ListStateDescriptor; +import org.apache.flink.api.common.typeinfo.TypeInformation; +import org.apache.flink.runtime.state.OperatorStateBackend; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nullable; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.apache.flink.agents.api.configuration.AgentConfigOptions.ACTION_STATE_STORE_BACKEND; +import static org.apache.flink.agents.runtime.actionstate.ActionStateStore.BackendType.KAFKA; + +class DurableExecutionManager implements ActionStatePersister, AutoCloseable { + + private static final Logger LOG = LoggerFactory.getLogger(DurableExecutionManager.class); + + private static final String RECOVERY_MARKER_STATE_NAME = "recoveryMarker"; + + private ActionStateStore actionStateStore; + private transient ListState<Object> recoveryMarkerOpState; + private final Map<Long, Map<Object, Long>> checkpointIdToSeqNums; + + private final Map<ActionTask, RunnerContextImpl.DurableExecutionContext> + actionTaskDurableContexts; + private final Map<ActionTask, ContinuationContext> continuationContexts; + private final Map<ActionTask, String> pythonAwaitableRefs; Review Comment: These 2 maps might belong to `ActionTaskContextManager`. They are not related to durable execution. In this class, they are only initialized, and used in some one-line getter/setters which are all used by `ActionTaskContextManager`. ########## runtime/src/main/java/org/apache/flink/agents/runtime/operator/ActionExecutionOperator.java: ########## @@ -121,54 +71,27 @@ * and the resulting output event is collected for further processing. */ public class ActionExecutionOperator<IN, OUT> extends AbstractStreamOperator<OUT> - implements OneInputStreamOperator<IN, OUT>, BoundedOneInput, ActionStatePersister { + implements OneInputStreamOperator<IN, OUT>, BoundedOneInput { private static final long serialVersionUID = 1L; private static final Logger LOG = LoggerFactory.getLogger(ActionExecutionOperator.class); - private static final String RECOVERY_MARKER_STATE_NAME = "recoveryMarker"; - private static final String MESSAGE_SEQUENCE_NUMBER_STATE_NAME = "messageSequenceNumber"; - private static final String PENDING_INPUT_EVENT_STATE_NAME = "pendingInputEvents"; - private final AgentPlan agentPlan; private transient ResourceCache resourceCache; private final Boolean inputIsJava; Review Comment: Never used. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
