echauchot commented on code in PR #3: URL: https://github.com/apache/flink-connector-cassandra/pull/3#discussion_r1142227595
########## flink-connector-cassandra/src/main/java/org/apache/flink/connector/cassandra/source/split/SplitsGenerator.java: ########## @@ -0,0 +1,259 @@ +/* + * 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.connector.cassandra.source.split; + +import org.apache.flink.annotation.VisibleForTesting; +import org.apache.flink.connector.cassandra.source.enumerator.CassandraEnumeratorState; + +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Row; +import com.datastax.driver.core.Session; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nullable; + +import java.math.BigInteger; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.List; + +import static org.apache.flink.util.Preconditions.checkState; + +/** + * This class prepares the generation of {@link CassandraSplit}s based on Cassandra cluster + * partitioner and cluster statistics. It estimates the total size of the table using Cassandra + * system table system.size_estimates. + */ +public final class SplitsGenerator { + + private static final Logger LOG = LoggerFactory.getLogger(SplitsGenerator.class); + @VisibleForTesting public Long minSplitMemorySize = 10_000_000L; // 10 MB + + private final CassandraPartitioner partitioner; + private final Session session; + private final String keyspace; + private final String table; + private final int parallelism; + @Nullable private final Long maxSplitMemorySize; + + public SplitsGenerator( + CassandraPartitioner partitioner, + Session session, + String keyspace, + String table, + int parallelism, + Long maxSplitMemorySize) { + this.partitioner = partitioner; + this.session = session; + this.keyspace = keyspace; + this.table = table; + this.parallelism = parallelism; + this.maxSplitMemorySize = maxSplitMemorySize; + } + + /** + * Prepare the {@param CassandraEnumeratorState} for lazy generation of {@link CassandraSplit}s: + * calculate {@code numSplitsToGenerate} based on estimated target table size and provided + * {@code maxSplitMemorySize} and calculate {@code increment} which is the size of a split in + * tokens. + */ + public CassandraEnumeratorState prepareSplits() { + final long numSplitsToGenerate = decideOnNumSplits(); + final BigInteger increment = + (partitioner.ringSize).divide(new BigInteger(String.valueOf(numSplitsToGenerate))); + final BigInteger startToken = partitioner.minToken; + return new CassandraEnumeratorState( + numSplitsToGenerate, + increment, + startToken, + partitioner.maxToken, + new ArrayDeque<>()); + } + + /** + * Determine {@code numSplits} based on the estimation of the target table size and user defined + * {@code maxSplitMemorySize}. Provide fallbacks when table size is unavailable, too few or too + * many splits are calculated. + */ + private long decideOnNumSplits() { + long numSplits; + if (maxSplitMemorySize != null) { + checkState( + maxSplitMemorySize >= minSplitMemorySize, + "Defined maxSplitMemorySize (%s) is below minimum (%s)", + maxSplitMemorySize, + minSplitMemorySize); + final long estimateTableSize = estimateTableSize(); + if (estimateTableSize == 0) { // size estimates unavailable + LOG.info( + "Cassandra size estimates are not available for {}.{} table. Creating as many splits as parallelism ({})", + keyspace, + table, + parallelism); + numSplits = parallelism; + } else { + LOG.debug( + "Estimated size for {}.{} table is {} bytes", + keyspace, + table, + estimateTableSize); + numSplits = + estimateTableSize / maxSplitMemorySize == 0 + ? parallelism // estimateTableSize under sizes maxSplitMemorySize + // creating as many splits as parallelism + : estimateTableSize / maxSplitMemorySize; + LOG.info( + "maxSplitMemorySize set value ({}) leads to the creation of {} splits", + maxSplitMemorySize, + numSplits); + } + } else { // maxSplitMemorySize not defined Review Comment: fair enough -- 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: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org