StefanRRichter commented on a change in pull request #7866: [FLINK-11730] 
[State Backends] Make HeapKeyedStateBackend follow the builder pattern
URL: https://github.com/apache/flink/pull/7866#discussion_r262016736
 
 

 ##########
 File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/state/heap/HeapKeyedStateBackendBuilder.java
 ##########
 @@ -0,0 +1,150 @@
+/*
+ * 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.runtime.state.heap;
+
+import org.apache.flink.api.common.ExecutionConfig;
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.core.fs.CloseableRegistry;
+import org.apache.flink.runtime.query.TaskKvStateRegistry;
+import org.apache.flink.runtime.state.AbstractKeyedStateBackendBuilder;
+import org.apache.flink.runtime.state.BackendBuildingException;
+import org.apache.flink.runtime.state.KeyGroupRange;
+import org.apache.flink.runtime.state.KeyedStateHandle;
+import org.apache.flink.runtime.state.LocalRecoveryConfig;
+import org.apache.flink.runtime.state.StreamCompressionDecorator;
+import org.apache.flink.runtime.state.ttl.TtlTimeProvider;
+
+import javax.annotation.Nonnull;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Builder class for {@link HeapKeyedStateBackend} which handles all necessary 
initializations and clean ups.
+ *
+ * @param <K> The data type that the key serializer serializes.
+ */
+public class HeapKeyedStateBackendBuilder<K> extends 
AbstractKeyedStateBackendBuilder<K> {
+       /**
+        * The configuration of local recovery.
+        */
+       private final LocalRecoveryConfig localRecoveryConfig;
+       /**
+        * Factory for state that is organized as priority queue.
+        */
+       private final HeapPriorityQueueSetFactory priorityQueueSetFactory;
+       /**
+        * Whether asynchronous snapshot is enabled.
+        */
+       private final boolean asynchronousSnapshots;
+
+
+       public HeapKeyedStateBackendBuilder(
+               TaskKvStateRegistry kvStateRegistry,
+               TypeSerializer<K> keySerializer,
+               ClassLoader userCodeClassLoader,
+               int numberOfKeyGroups,
+               KeyGroupRange keyGroupRange,
+               ExecutionConfig executionConfig,
+               TtlTimeProvider ttlTimeProvider,
+               @Nonnull Collection<KeyedStateHandle> stateHandles,
+               StreamCompressionDecorator keyGroupCompressionDecorator,
+               LocalRecoveryConfig localRecoveryConfig,
+               HeapPriorityQueueSetFactory priorityQueueSetFactory,
+               boolean asynchronousSnapshots,
+               CloseableRegistry cancelStreamRegistry) {
+               super(
+                       kvStateRegistry,
+                       keySerializer,
+                       userCodeClassLoader,
+                       numberOfKeyGroups,
+                       keyGroupRange,
+                       executionConfig,
+                       ttlTimeProvider,
+                       stateHandles,
+                       keyGroupCompressionDecorator,
+                       cancelStreamRegistry);
+               this.localRecoveryConfig = localRecoveryConfig;
+               this.priorityQueueSetFactory = priorityQueueSetFactory;
+               this.asynchronousSnapshots = asynchronousSnapshots;
+       }
+
+       @Override
+       public HeapKeyedStateBackend<K> build() throws BackendBuildingException 
{
+               // Map of registered Key/Value states
+               Map<String, StateTable<K, ?, ?>> registeredKVStates = new 
HashMap<>();
+               // Map of registered priority queue set states
+               Map<String, HeapPriorityQueueSnapshotRestoreWrapper> 
registeredPQStates = new HashMap<>();
+               HeapSnapshotStrategy<K> snapshotStrategy = initSnapshotStrategy(
+                       asynchronousSnapshots, registeredKVStates, 
registeredPQStates, cancelStreamRegistry);
+               HeapKeyedStateBackend<K> backend = new HeapKeyedStateBackend<>(
+                       kvStateRegistry,
+                       keySerializerProvider,
+                       userCodeClassLoader,
+                       numberOfKeyGroups,
+                       keyGroupRange,
+                       executionConfig,
+                       ttlTimeProvider,
+                       cancelStreamRegistry,
+                       keyGroupCompressionDecorator,
+                       registeredKVStates,
+                       registeredPQStates,
+                       localRecoveryConfig,
+                       priorityQueueSetFactory,
+                       snapshotStrategy
+               );
+               HeapRestoreOperation<K> restoreOperation = new 
HeapRestoreOperation<>(
+                       restoreStateHandles,
+                       keySerializerProvider,
+                       userCodeClassLoader,
+                       registeredKVStates,
+                       registeredPQStates,
+                       cancelStreamRegistry,
+                       priorityQueueSetFactory,
+                       keyGroupRange,
+                       numberOfKeyGroups,
+                       snapshotStrategy,
+                       backend);
+               try {
+                       restoreOperation.restore();
+               } catch (Throwable e) {
+                       throw new BackendBuildingException("Failed when trying 
to restore heap backend", e);
+               }
+               return backend;
+       }
+
+       private HeapSnapshotStrategy<K> initSnapshotStrategy(
+               boolean asynchronousSnapshots,
+               Map<String, StateTable<K, ?, ?>> registeredKVStates,
+               Map<String, HeapPriorityQueueSnapshotRestoreWrapper> 
registeredPQStates,
+               CloseableRegistry cancelStreamRegistry) {
+               SnapshotStrategySynchronicityBehavior<K> synchronicityTrait = 
asynchronousSnapshots ?
+                       new AsyncSnapshotStrategySynchronicityBehavior() :
 
 Review comment:
   Raw-types `new AsyncSnapshotStrategySynchronicityBehavior<>()`

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to