echauchot commented on code in PR #3: URL: https://github.com/apache/flink-connector-cassandra/pull/3#discussion_r1138822645
########## flink-connector-cassandra/src/main/java/org/apache/flink/connector/cassandra/source/split/SplitsGenerator.java: ########## @@ -0,0 +1,237 @@ +/* + * 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 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.ArrayList; +import java.util.List; + +/** + * This class generates {@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. But there is no way to estimate the size of the data with the optional SQL + * filters without reading the data. So the splits can be smaller than {@param maxSplitMemorySize} + * when the query is executed. + */ +public final class SplitsGenerator { + + private static final Logger LOG = LoggerFactory.getLogger(SplitsGenerator.class); + private static final int ACCEPTABLE_NB_SPLIT_PARALLELISM_RATIO = 10; + + 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; + } + + /** + * Split Cassandra tokens ring into {@link CassandraSplit}s containing each a range of the + * Cassandra ring of {@param maxSplitMemorySize}. If {@param maxSplitMemorySize} is not defined, + * or is too high or too low compared to the task parallelism, then it generates as many {@link + * CassandraSplit}s as the task parallelism. + * + * @return list containing {@code numSplits} CassandraSplits. + */ + public List<CassandraSplit> generateSplits() { + long numSplits; + if (maxSplitMemorySize != null) { + final long estimateTableSize = estimateTableSize(); + LOG.debug("Estimated table size for table {} is {} bytes", table, estimateTableSize); + numSplits = estimateTableSize / maxSplitMemorySize; + if (numSplits == 0 // estimateTableSize can be null in some cases (see javadoc) + || numSplits < parallelism / ACCEPTABLE_NB_SPLIT_PARALLELISM_RATIO // too low + || numSplits + > (long) parallelism + * ACCEPTABLE_NB_SPLIT_PARALLELISM_RATIO) { // too high Review Comment: That is what I proposed first falling back to numSplits = parallelism when too few splits. So I'll keep doing that. -- 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