Jiabao-Sun commented on code in PR #1: URL: https://github.com/apache/flink-connector-mongodb/pull/1#discussion_r1032994738
########## flink-connector-mongodb/src/main/java/org/apache/flink/connector/mongodb/source/enumerator/splitter/MongoSampleSplitter.java: ########## @@ -0,0 +1,131 @@ +/* + * 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.mongodb.source.enumerator.splitter; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.connector.mongodb.source.config.MongoReadOptions; +import org.apache.flink.connector.mongodb.source.split.MongoScanSourceSplit; + +import com.mongodb.MongoNamespace; +import com.mongodb.client.model.Aggregates; +import com.mongodb.client.model.Projections; +import com.mongodb.client.model.Sorts; +import org.bson.BsonDocument; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +import static org.apache.flink.connector.mongodb.common.utils.MongoConstants.BSON_MAX_KEY; +import static org.apache.flink.connector.mongodb.common.utils.MongoConstants.BSON_MIN_KEY; +import static org.apache.flink.connector.mongodb.common.utils.MongoConstants.ID_FIELD; +import static org.apache.flink.connector.mongodb.common.utils.MongoConstants.ID_HINT; + +/** + * Sample Partitioner + * + * <p>Samples the collection to generate partitions. + * + * <p>Uses the average document size to split the collection into average sized chunks + * + * <p>The partitioner samples the collection, projects and sorts by the partition fields. Then uses + * every {@code samplesPerPartition} as the value to use to calculate the partition boundaries. + * + * <ul> + * <li>scan.partition.size: The average size (MB) for each partition. Note: Uses the average + * document size to determine the number of documents per partition so may not be even. + * Defaults to: 64mb. + * <li>scan.partition.samples: The number of samples to take per partition. Defaults to: 10. The + * total number of samples taken is calculated as: {@code samples per partition * (count of + * documents / number of documents per partition)}. Review Comment: > If multiple samples are taken per partition then somewhere in here we'd have to merge sample to arrive at a single partition again, but afaict that doesn't happen. We merge samples in the following code. ```java List<MongoScanSourceSplit> sourceSplits = new ArrayList<>(); BsonDocument partitionStart = new BsonDocument(ID_FIELD, BSON_MIN_KEY); int splitNum = 0; for (int i = 0; i < samples.size(); i++) { if (i % samplesPerPartition == 0 || i == samples.size() - 1) { sourceSplits.add( createSplit(namespace, splitNum++, partitionStart, samples.get(i))); partitionStart = samples.get(i); } } ``` ____________ > Instead we have some strange formula that determines the number of samples (read: partitions), and I have no idea how the resulting partitions could correlate with the desired partition size. > > Why isnt the number of sample (again: partitions) not count / numDocumentsPerPartition? 1. numDocumentsPerPartition = partitionSizeInBytes / avgObjSizeInBytes 2. samplingRate = samplesPerPartition / numDocumentsPerPartition 3. samplesCount = samplingRate * count 4. merge samples by samplesPerPartition We calculate the sampling rate through samples per partition and partition size. We can also be accomplished directly by setting the sampling rate. @zentol `scan.partition.samples` or `scan.partition.sampling-rate` which do you think is better? -- 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