rkhachatryan commented on a change in pull request #11403: 
[FLINK-16316][operators] Implement new StreamOperatorBase as a replacement for 
AbstractStreamOperator
URL: https://github.com/apache/flink/pull/11403#discussion_r397081550
 
 

 ##########
 File path: 
flink-streaming-java/src/main/java/org/apache/flink/streaming/api/operators/StreamOperatorStateHandler.java
 ##########
 @@ -0,0 +1,297 @@
+/*
+ * 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.streaming.api.operators;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.api.common.ExecutionConfig;
+import org.apache.flink.api.common.state.KeyedStateStore;
+import org.apache.flink.api.common.state.State;
+import org.apache.flink.api.common.state.StateDescriptor;
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.core.fs.CloseableRegistry;
+import org.apache.flink.runtime.checkpoint.CheckpointException;
+import org.apache.flink.runtime.checkpoint.CheckpointFailureReason;
+import org.apache.flink.runtime.checkpoint.CheckpointOptions;
+import org.apache.flink.runtime.state.AbstractKeyedStateBackend;
+import org.apache.flink.runtime.state.CheckpointStreamFactory;
+import org.apache.flink.runtime.state.DefaultKeyedStateStore;
+import org.apache.flink.runtime.state.KeyGroupRange;
+import org.apache.flink.runtime.state.KeyGroupStatePartitionStreamProvider;
+import org.apache.flink.runtime.state.KeyedStateBackend;
+import org.apache.flink.runtime.state.OperatorStateBackend;
+import org.apache.flink.runtime.state.StateInitializationContext;
+import org.apache.flink.runtime.state.StateInitializationContextImpl;
+import org.apache.flink.runtime.state.StatePartitionStreamProvider;
+import org.apache.flink.runtime.state.StateSnapshotContext;
+import org.apache.flink.runtime.state.StateSnapshotContextSynchronousImpl;
+import org.apache.flink.util.CloseableIterable;
+import org.apache.flink.util.IOUtils;
+import org.apache.flink.util.function.ThrowingConsumer;
+
+import org.apache.flink.shaded.guava18.com.google.common.io.Closer;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.Optional;
+
+/**
+ * Class encapsulating various state backend handling logic for {@link 
StreamOperator} implementations.
+ */
+@Internal
+public class StreamOperatorStateHandler {
+
+       protected static final Logger LOG = 
LoggerFactory.getLogger(StreamOperatorStateHandler.class);
+
+       /** Backend for keyed state. This might be empty if we're not on a 
keyed stream. */
+       @Nullable
+       private final AbstractKeyedStateBackend<?> keyedStateBackend;
+       private final CloseableRegistry closeableRegistry;
+       @Nullable
+       private final DefaultKeyedStateStore keyedStateStore;
+       private final OperatorStateBackend operatorStateBackend;
+       private final StreamOperatorStateContext context;
+
+       public StreamOperatorStateHandler(
+                       StreamOperatorStateContext context,
+                       ExecutionConfig executionConfig,
+                       CloseableRegistry closeableRegistry) {
+               this.context = context;
+               operatorStateBackend = context.operatorStateBackend();
+               keyedStateBackend = context.keyedStateBackend();
+               this.closeableRegistry = closeableRegistry;
+
+               if (keyedStateBackend != null) {
+                       keyedStateStore = new 
DefaultKeyedStateStore(keyedStateBackend, executionConfig);
+               }
+               else {
+                       keyedStateStore = null;
+               }
+       }
+
+       public void 
initializeOperatorState(ThrowingConsumer<StateInitializationContext, Exception> 
initializeOperatorAction) throws Exception {
+               CloseableIterable<KeyGroupStatePartitionStreamProvider> 
keyedStateInputs = context.rawKeyedStateInputs();
+               CloseableIterable<StatePartitionStreamProvider> 
operatorStateInputs = context.rawOperatorStateInputs();
+
+               try {
+                       StateInitializationContext initializationContext = new 
StateInitializationContextImpl(
+                               context.isRestored(), // information whether we 
restore or start for the first time
+                               operatorStateBackend, // access to operator 
state backend
+                               keyedStateStore, // access to keyed state 
backend
+                               keyedStateInputs, // access to keyed state 
stream
+                               operatorStateInputs); // access to operator 
state stream
+
+                       initializeOperatorAction.accept(initializationContext);
+               } finally {
+                       closeFromRegistry(operatorStateInputs, 
closeableRegistry);
+                       closeFromRegistry(keyedStateInputs, closeableRegistry);
+               }
+       }
+
+       private static void closeFromRegistry(Closeable closeable, 
CloseableRegistry registry) {
+               if (registry.unregisterCloseable(closeable)) {
+                       IOUtils.closeQuietly(closeable);
+               }
+       }
+
+       public void dispose() throws Exception {
+               try (Closer closer = Closer.create()) {
+                       if 
(closeableRegistry.unregisterCloseable(operatorStateBackend)) {
+                               closer.register(operatorStateBackend);
+                       }
+                       if 
(closeableRegistry.unregisterCloseable(keyedStateBackend)) {
+                               closer.register(keyedStateBackend);
+                       }
+                       if (operatorStateBackend != null) {
+                               closer.register(() -> 
operatorStateBackend.dispose());
+                       }
+                       if (keyedStateBackend != null) {
+                               closer.register(() -> 
keyedStateBackend.dispose());
+                       }
+               }
+       }
+
+       public OperatorSnapshotFutures snapshotState(
+                       ThrowingConsumer<StateSnapshotContext, Exception> 
snapshotStateAction,
+                       InternalTimeServiceManager<?> timeServiceManager,
+                       String operatorName,
+                       long checkpointId,
+                       long timestamp,
+                       CheckpointOptions checkpointOptions,
+                       CheckpointStreamFactory factory) throws 
CheckpointException {
+               KeyGroupRange keyGroupRange = null != keyedStateBackend ?
+                       keyedStateBackend.getKeyGroupRange() : 
KeyGroupRange.EMPTY_KEY_GROUP_RANGE;
+
+               OperatorSnapshotFutures snapshotInProgress = new 
OperatorSnapshotFutures();
+
+               StateSnapshotContextSynchronousImpl snapshotContext = new 
StateSnapshotContextSynchronousImpl(
+                       checkpointId,
+                       timestamp,
+                       factory,
+                       keyGroupRange,
+                       closeableRegistry);
+
+               snapshotState(
+                       snapshotStateAction,
+                       timeServiceManager,
+                       operatorName,
+                       checkpointId,
+                       timestamp,
+                       checkpointOptions,
+                       factory,
+                       snapshotInProgress,
+                       snapshotContext);
+
+               return snapshotInProgress;
+       }
+
+       @VisibleForTesting
+       void snapshotState(
+                       ThrowingConsumer<StateSnapshotContext, Exception> 
snapshotStateAction,
+                       InternalTimeServiceManager<?> timeServiceManager,
+                       String operatorName,
+                       long checkpointId,
+                       long timestamp,
+                       CheckpointOptions checkpointOptions,
+                       CheckpointStreamFactory factory,
+                       OperatorSnapshotFutures snapshotInProgress,
+                       StateSnapshotContextSynchronousImpl snapshotContext) 
throws CheckpointException {
+               try {
+                       timeServiceManager.snapshotState(keyedStateBackend, 
snapshotContext, operatorName);
+                       snapshotStateAction.accept(snapshotContext);
 
 Review comment:
   Operator state snapshot is taken using `Consumer snapshotStateAction`, 
   but for `timeServiceManager` we pass it and call directly.
   
   Should we make it consistent? 
   
   We could remove `opName` parameter and then use `BiConsumerWithException`.
   `opName` can be turned into a field or exception can be wrapped on a higher 
level.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to