RyanSkraba commented on code in PR #3: URL: https://github.com/apache/flink-connector-cassandra/pull/3#discussion_r1054648476
########## flink-connector-cassandra/src/main/java/org/apache/flink/connector/cassandra/source/split/SplitsGenerator.java: ########## @@ -0,0 +1,211 @@ +/* + * 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.shaded.guava30.com.google.common.collect.Sets; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; + +/** + * This class generates {@link CassandraSplit}s by generating {@link RingRange}s based on Cassandra + * cluster partitioner and Flink source parallelism. + */ +public final class SplitsGenerator { + private static final Logger LOG = LoggerFactory.getLogger(SplitsGenerator.class); + + private final String partitioner; + private final BigInteger rangeMin; + private final BigInteger rangeMax; + private final BigInteger rangeSize; + + public SplitsGenerator(String partitioner) { + this.partitioner = partitioner; + rangeMin = getRangeMin(); + rangeMax = getRangeMax(); + rangeSize = getRangeSize(); + } + + private BigInteger getRangeMin() { + if (partitioner.endsWith("RandomPartitioner")) { + return BigInteger.ZERO; + } else if (partitioner.endsWith("Murmur3Partitioner")) { + return BigInteger.valueOf(2).pow(63).negate(); + } else { + throw new UnsupportedOperationException( + "Unsupported partitioner. " + "Only Random and Murmur3 are supported"); + } + } + + private BigInteger getRangeMax() { + if (partitioner.endsWith("RandomPartitioner")) { + return BigInteger.valueOf(2).pow(127).subtract(BigInteger.ONE); + } else if (partitioner.endsWith("Murmur3Partitioner")) { + return BigInteger.valueOf(2).pow(63).subtract(BigInteger.ONE); + } else { + throw new UnsupportedOperationException( + "Unsupported partitioner. " + "Only Random and Murmur3 are supported"); + } + } + + private BigInteger getRangeSize() { + return rangeMax.subtract(rangeMin).add(BigInteger.ONE); + } + + /** + * Given properly ordered list of Cassandra tokens, compute at least {@code totalSplitCount} + * splits. Each split can contain several token ranges in order to reduce the overhead of + * Cassandra vnodes. Currently, token range grouping is not smart and doesn't check if they + * share the same replicas. + * + * @param totalSplitCount requested total amount of splits. This function may generate more + * splits. + * @param ringTokens list of all start tokens in Cassandra cluster. They have to be in ring + * order. + * @return list containing at least {@code totalSplitCount} CassandraSplits. + */ + public List<CassandraSplit> generateSplits(long totalSplitCount, List<BigInteger> ringTokens) { + if (totalSplitCount == 1) { + RingRange totalRingRange = RingRange.of(rangeMin, rangeMax); + // needs to be mutable Review Comment: ```suggestion // The set containing the single, inclusive ring range needs to be mutable ``` Just a suggestion for clarity. This can be bit tricky to follow. ########## flink-connector-cassandra/src/test/resources/log4j2-test.properties: ########## @@ -18,7 +18,7 @@ # Set root logger level to OFF to not flood build logs # set manually to INFO for debugging purposes -rootLogger.level = OFF Review Comment: I found this useful while reviewing, but it might be OK to turn off for CI. ########## flink-connector-cassandra/src/test/java/org/apache/flink/connector/cassandra/source/CassandraSourceITCase.java: ########## @@ -0,0 +1,111 @@ +/* + * 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; + +import org.apache.flink.connector.testframe.environment.ClusterControllable; +import org.apache.flink.connector.testframe.environment.MiniClusterTestEnvironment; +import org.apache.flink.connector.testframe.environment.TestEnvironment; +import org.apache.flink.connector.testframe.external.source.DataStreamSourceExternalContext; +import org.apache.flink.connector.testframe.junit.annotations.TestContext; +import org.apache.flink.connector.testframe.junit.annotations.TestEnv; +import org.apache.flink.connector.testframe.junit.annotations.TestExternalSystem; +import org.apache.flink.connector.testframe.junit.annotations.TestSemantics; +import org.apache.flink.connector.testframe.testsuites.SourceTestSuiteBase; +import org.apache.flink.connector.testframe.utils.CollectIteratorAssertions; +import org.apache.flink.connectors.cassandra.utils.Pojo; +import org.apache.flink.streaming.api.CheckpointingMode; +import org.apache.flink.util.CloseableIterator; + +import org.junit.jupiter.api.Disabled; + +import java.util.List; + +import static java.util.concurrent.CompletableFuture.runAsync; +import static org.apache.flink.connector.cassandra.source.CassandraTestContext.CassandraTestContextFactory; +import static org.apache.flink.connector.testframe.utils.ConnectorTestConstants.DEFAULT_COLLECT_DATA_TIMEOUT; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; Review Comment: ```suggestion import static org.assertj.core.api.Assertions.assertThat; ``` This is probably the one you want. ########## flink-connector-cassandra/src/main/java/org/apache/flink/connector/cassandra/source/split/CassandraSplit.java: ########## @@ -0,0 +1,66 @@ +/* + * 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.api.connector.source.SourceSplit; + +import java.io.Serializable; +import java.util.Objects; +import java.util.Set; + +/** + * {@link SourceSplit} for Cassandra source. A Cassandra split is just a set of {@link RingRange}s + * (a range between 2 tokens). Tokens are spread across the Cassandra cluster with each node + * managing a share of the token ring. Each split can contain several token ranges in order to + * reduce the overhead on Cassandra vnodes. + */ +public class CassandraSplit implements SourceSplit, Serializable { + + private final Set<RingRange> ringRanges; + + public CassandraSplit(Set<RingRange> ringRanges) { + this.ringRanges = ringRanges; + } + + public Set<RingRange> getRingRanges() { + return ringRanges; + } + + @Override + public String splitId() { + return ringRanges.toString(); Review Comment: I noticed this is mutable since ringRanges are mutable while the split is being read -- do you think this will pose a problem if the splitId changes? ########## flink-connector-cassandra/src/main/java/org/apache/flink/connector/cassandra/source/enumerator/CassandraEnumeratorState.java: ########## @@ -0,0 +1,75 @@ +/* + * 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.enumerator; + +import org.apache.flink.connector.cassandra.source.split.CassandraSplit; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.Serializable; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Objects; +import java.util.Set; + +/** Sate for {@link CassandraSplitEnumerator} to track the splits yet to assign. */ +public class CassandraEnumeratorState implements Serializable { + private static final Logger LOG = LoggerFactory.getLogger(CassandraEnumeratorState.class); + + // map readerId to splits + private final Map<Integer, Set<CassandraSplit>> unassignedSplits = new HashMap<>(); + + public void addNewSplits(Collection<CassandraSplit> newSplits, int numReaders) { + for (CassandraSplit split : newSplits) { + int ownerReader = getOwnerReader(numReaders, split); + unassignedSplits.computeIfAbsent(ownerReader, r -> new HashSet<>()).add(split); + } + } + + private int getOwnerReader(int numReaders, CassandraSplit split) { + // readerId == subTaksId == 0 or 1 if numReaders == 2 so modulo is fine for ownerReader Review Comment: ```suggestion // readerId == subTaskId == 0 or 1 if numReaders == 2 so modulo is fine for ownerReader ``` -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
