StefanRRichter commented on a change in pull request #7674: [FLINK-10043] [State Backends] Refactor RocksDBKeyedStateBackend object construction/initialization/restore code URL: https://github.com/apache/flink/pull/7674#discussion_r258057038
########## File path: flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/RocksDBPriorityQueueSetFactory.java ########## @@ -0,0 +1,184 @@ +/* + * 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.contrib.streaming.state; + +import org.apache.flink.api.common.typeutils.TypeSerializer; +import org.apache.flink.api.common.typeutils.TypeSerializerSchemaCompatibility; +import org.apache.flink.core.memory.DataInputDeserializer; +import org.apache.flink.core.memory.DataOutputSerializer; +import org.apache.flink.runtime.state.KeyExtractorFunction; +import org.apache.flink.runtime.state.KeyGroupRange; +import org.apache.flink.runtime.state.KeyGroupedInternalPriorityQueue; +import org.apache.flink.runtime.state.Keyed; +import org.apache.flink.runtime.state.PriorityComparable; +import org.apache.flink.runtime.state.PriorityComparator; +import org.apache.flink.runtime.state.PriorityQueueSetFactory; +import org.apache.flink.runtime.state.RegisteredPriorityQueueStateBackendMetaInfo; +import org.apache.flink.runtime.state.heap.HeapPriorityQueueElement; +import org.apache.flink.runtime.state.heap.KeyGroupPartitionedPriorityQueue; +import org.apache.flink.util.FlinkRuntimeException; +import org.apache.flink.util.StateMigrationException; + +import org.rocksdb.ColumnFamilyHandle; +import org.rocksdb.ColumnFamilyOptions; +import org.rocksdb.RocksDB; + +import javax.annotation.Nonnull; + +import java.util.Map; + +/** + * Encapsulates the logic and resources in connection with creating priority + * queue state structures, for RocksDB backend. + */ +public class RocksDBPriorityQueueSetFactory implements PriorityQueueSetFactory { + + /** + * Default cache size per key-group. + */ + private static final int DEFAULT_CACHES_SIZE = 128; //TODO make this configurable + + /** + * A shared buffer to serialize elements for the priority queue. + */ + @Nonnull + private final DataOutputSerializer sharedElementOutView; + + /** + * A shared buffer to de-serialize elements for the priority queue. + */ + @Nonnull + private final DataInputDeserializer sharedElementInView; + + private final KeyGroupRange keyGroupRange; + private final int keyGroupPrefixBytes; + private final int numberOfKeyGroups; + private final Map<String, StateColumnFamilyHandle> kvStateInformation; + private final RocksDB db; + private final RocksDBWriteBatchWrapper writeBatchWrapper; + private final RocksDBNativeMetricMonitor nativeMetricMonitor; + private final ColumnFamilyOptions columnOptions; + + RocksDBPriorityQueueSetFactory(KeyGroupRange keyGroupRange, + int keyGroupPrefixBytes, + int numberOfKeyGroups, + Map<String, StateColumnFamilyHandle> kvStateInformation, + RocksDB db, + RocksDBWriteBatchWrapper writeBatchWrapper, + RocksDBNativeMetricMonitor nativeMetricMonitor, + ColumnFamilyOptions columnOptions) { + this.keyGroupRange = keyGroupRange; + this.keyGroupPrefixBytes = keyGroupPrefixBytes; + this.numberOfKeyGroups = numberOfKeyGroups; + this.kvStateInformation = kvStateInformation; + this.db = db; + this.writeBatchWrapper = writeBatchWrapper; + this.nativeMetricMonitor = nativeMetricMonitor; + this.columnOptions = columnOptions; + this.sharedElementOutView = new DataOutputSerializer(128); + this.sharedElementInView = new DataInputDeserializer(); + } + + @Nonnull + @Override + public <T extends HeapPriorityQueueElement & PriorityComparable & Keyed> KeyGroupedInternalPriorityQueue<T> + create(@Nonnull String stateName, @Nonnull TypeSerializer<T> byteOrderedElementSerializer) { + + final StateColumnFamilyHandle stateCFHandle = + tryRegisterPriorityQueueMetaInfo(stateName, byteOrderedElementSerializer); + + final ColumnFamilyHandle columnFamilyHandle = stateCFHandle.getColumnFamilyHandle(); + + return new KeyGroupPartitionedPriorityQueue<>( + KeyExtractorFunction.forKeyedObjects(), + PriorityComparator.forPriorityComparableObjects(), + new KeyGroupPartitionedPriorityQueue.PartitionQueueSetFactory<T, RocksDBCachingPriorityQueueSet<T>>() { + @Nonnull + @Override + public RocksDBCachingPriorityQueueSet<T> create( + int keyGroupId, + int numKeyGroups, + @Nonnull KeyExtractorFunction<T> keyExtractor, + @Nonnull PriorityComparator<T> elementPriorityComparator) { + TreeOrderedSetCache orderedSetCache = new TreeOrderedSetCache(DEFAULT_CACHES_SIZE); + return new RocksDBCachingPriorityQueueSet<>( + keyGroupId, + keyGroupPrefixBytes, + db, + columnFamilyHandle, + byteOrderedElementSerializer, + sharedElementOutView, + sharedElementInView, + writeBatchWrapper, + orderedSetCache + ); + } + }, + keyGroupRange, + numberOfKeyGroups); + } + + @Nonnull + private <T> StateColumnFamilyHandle tryRegisterPriorityQueueMetaInfo( + @Nonnull String stateName, + @Nonnull TypeSerializer<T> byteOrderedElementSerializer) { + + StateColumnFamilyHandle stateCFHandle = kvStateInformation.get(stateName); + + if (stateCFHandle == null) { + final ColumnFamilyHandle columnFamilyHandle = RocksDBKeyedStateBackend.createColumnFamily( + stateName, this.columnOptions, this.db Review comment: Unusual formatting style. Can we at least bring `);` back on the same line? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on 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
