-
Notifications
You must be signed in to change notification settings - Fork 332
[BitSail-143][Connector] Improve kudu split strategy #451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
beyond-up
wants to merge
12
commits into
bytedance:master
Choose a base branch
from
beyond-up:improve_kudu_split_strategy
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4128063
improve kudu split strategy
beyond-up fe83665
add partition split strategy test
beyond-up 266377a
Merge remote-tracking branch 'origin/master' into improve_kudu_split_…
beyond-up 7ab928e
add partition split strategy test
beyond-up 339fe0c
add predicate split strategy test code
beyond-up bbed67f
fix predication split strategy bug
beyond-up 70037be
fix partition split stragtegy
beyond-up e1605b8
fix partition split strategy: support multiple columns
beyond-up d63d007
fix partition split strategy bug
beyond-up 49375f3
add constants
beyond-up 7035b7f
Merge branch 'bytedance:master' into improve_kudu_split_strategy
beyond-up 1b54526
Merge branch 'bytedance:master' into improve_kudu_split_strategy
beyond-up File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
197 changes: 197 additions & 0 deletions
197
...tedance/bitsail/connector/kudu/source/split/strategy/PartitionDivideSplitConstructor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| /* | ||
| * Copyright 2022-2023 Bytedance Ltd. and/or its affiliates. | ||
| * | ||
| * Licensed 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 com.bytedance.bitsail.connector.kudu.source.split.strategy; | ||
|
|
||
| import com.bytedance.bitsail.common.configuration.BitSailConfiguration; | ||
| import com.bytedance.bitsail.connector.kudu.option.KuduReaderOptions; | ||
| import com.bytedance.bitsail.connector.kudu.source.split.AbstractKuduSplitConstructor; | ||
| import com.bytedance.bitsail.connector.kudu.source.split.KuduSourceSplit; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.ToString; | ||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.apache.kudu.ColumnSchema; | ||
| import org.apache.kudu.Schema; | ||
| import org.apache.kudu.client.KuduClient; | ||
| import org.apache.kudu.client.KuduPredicate; | ||
| import org.apache.kudu.client.KuduTable; | ||
| import org.apache.kudu.client.Partition; | ||
| import org.apache.kudu.client.PartitionSchema; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class PartitionDivideSplitConstructor extends AbstractKuduSplitConstructor { | ||
| private static final Logger LOG = LoggerFactory.getLogger(PartitionDivideSplitConstructor.class); | ||
|
|
||
| private SplitConfiguration splitConf = null; | ||
| private boolean available = false; | ||
| private transient List<Partition> partitions = null; | ||
|
|
||
| public PartitionDivideSplitConstructor(BitSailConfiguration jobConf, KuduClient client) throws Exception { | ||
| super(jobConf, client); | ||
|
|
||
| if (!jobConf.fieldExists(KuduReaderOptions.SPLIT_CONFIGURATION)) { | ||
| LOG.warn("{} cannot work due to lack of split configuration.", this.getClass().getSimpleName()); | ||
| return; | ||
| } | ||
| String splitConfStr = jobConf.get(KuduReaderOptions.SPLIT_CONFIGURATION); | ||
| this.splitConf = new ObjectMapper().readValue(splitConfStr, SplitConfiguration.class); | ||
| KuduTable kuduTable = client.openTable(this.tableName); | ||
| this.available = splitConf.isValid(kuduTable); | ||
| if (!available) { | ||
| LOG.warn("{} cannot work because split configuration is invalid.", this.getClass().getSimpleName()); | ||
| return; | ||
| } | ||
| this.available = fillSplitConf(jobConf, client); | ||
| if (!available) { | ||
| return; | ||
| } | ||
| LOG.info("Successfully created ,final split configuration is: {}", splitConf); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isAvailable() { | ||
| return available; | ||
| } | ||
|
|
||
| @Override | ||
| @SuppressWarnings("checkstyle:MagicNumber") | ||
| protected boolean fillSplitConf(BitSailConfiguration jobConf, KuduClient client) throws Exception { | ||
| if (splitConf.getSplitNum() == null || splitConf.getSplitNum() <= 0) { | ||
| splitConf.setSplitNum(jobConf.getUnNecessaryOption(KuduReaderOptions.READER_PARALLELISM_NUM, 1)); | ||
| } | ||
| KuduTable kuduTable = client.openTable(this.tableName); | ||
| this.partitions = kuduTable.getRangePartitions(splitConf.getTimeOut()); | ||
| if (this.partitions.size() < splitConf.getSplitNum()) { | ||
| this.splitConf.setSplitNum(this.partitions.size()); | ||
| LOG.info("Resize split num to {}.", splitConf.getSplitNum()); | ||
| } | ||
| if (splitConf.timeOut == null || splitConf.timeOut <= 0) { | ||
| splitConf.setTimeOut(3000L); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public List <KuduSourceSplit> construct(KuduClient kuduClient) throws Exception { | ||
| List<KuduSourceSplit> splits = new ArrayList <>(); | ||
| ColumnSchema partitionColumn = schema.getColumn(splitConf.getPartitionKey()); | ||
|
|
||
| for (int i = 0; i < partitions.size(); i++) { | ||
| KuduSourceSplit split = new KuduSourceSplit(i); | ||
| split.addPredicate(KuduPredicate.newComparisonPredicate( | ||
| partitionColumn, | ||
| KuduPredicate.ComparisonOp.GREATER_EQUAL, | ||
| partitions.get(i).getPartitionKeyStart()[splitConf.partitionKeyIndex])); | ||
| split.addPredicate(KuduPredicate.newComparisonPredicate( | ||
| partitionColumn, | ||
| KuduPredicate.ComparisonOp.LESS, | ||
| partitions.get(i).getPartitionKeyEnd()[splitConf.partitionKeyIndex])); | ||
| splits.add(split); | ||
| LOG.info(">>> the {}-th split is: {}", i, splits.get(i).toFormatString(schema)); | ||
| } | ||
| LOG.info("Finally get {} splits.", partitions.size()); | ||
|
|
||
| return splits; | ||
| } | ||
|
|
||
| @Override | ||
| public int estimateSplitNum() { | ||
| int estimatedSplitNum = 1; | ||
| if (splitConf.getSplitNum() != null && splitConf.getSplitNum() > 0) { | ||
| estimatedSplitNum = splitConf.getSplitNum(); | ||
| } | ||
| LOG.info("Estimated split num is: {}", estimatedSplitNum); | ||
| return estimatedSplitNum; | ||
| } | ||
|
|
||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| @Data | ||
| @ToString(of = {"partitionKey", "splitNum", "timeOut", "partitionKeyIndex"}) | ||
| public static class SplitConfiguration { | ||
| @JsonProperty("partitionKey") | ||
| private String partitionKey; | ||
|
|
||
| @JsonProperty("split_num") | ||
| private Integer splitNum; | ||
|
|
||
| @JsonProperty("time_out_mills") | ||
| private Long timeOut; | ||
|
|
||
| @JsonProperty("partitionKeyIndex") | ||
| private int partitionKeyIndex; | ||
|
|
||
| public boolean isValid(KuduTable table) { | ||
| if (StringUtils.isEmpty(partitionKey)) { | ||
| LOG.warn("Split partition key cannot be empty."); | ||
| return false; | ||
| } | ||
| PartitionSchema partitionSchema = table.getPartitionSchema(); | ||
| Schema schema = table.getSchema(); | ||
| if (schema.getColumn(partitionKey) == null) { | ||
| LOG.warn("Split column {} cannot be found in schema.", partitionKey); | ||
| return false; | ||
| } | ||
| if (partitionSchema == null || partitionSchema.getRangeSchema() == null) { | ||
| LOG.warn("Partition schema cannot be empty."); | ||
| return false; | ||
| } | ||
| // check is simple range partition mode | ||
| boolean isSimple = isSimpleMode(partitionSchema, schema); | ||
| if (!isSimple) { | ||
| LOG.warn("Partition divide split strategy just support simple range partition mode."); | ||
| return false; | ||
| } | ||
| PartitionSchema.RangeSchema rangeSchema = partitionSchema.getRangeSchema(); | ||
| int idx = -1; | ||
| for (int i = 0; i < rangeSchema.getColumnIds().size(); i++) { | ||
| if (partitionKey.equals(schema.getColumnByIndex(i).getName())) { | ||
| idx = i; | ||
| } | ||
| } | ||
| if (idx == -1) { | ||
| LOG.warn("Split column {} cannot be found in partition range schema.", partitionKey); | ||
| return false; | ||
| } | ||
| partitionKeyIndex = idx; | ||
| return true; | ||
| } | ||
|
|
||
| private boolean isSimpleMode(PartitionSchema partitionSchema, Schema schema) { | ||
| boolean isSimple = partitionSchema.getHashBucketSchemas().isEmpty() && | ||
| partitionSchema.getRangeSchema().getColumnIds().size() == schema.getPrimaryKeyColumnCount(); | ||
| if (isSimple) { | ||
| int i = 0; | ||
| for (Integer id : partitionSchema.getRangeSchema().getColumnIds()) { | ||
| if (schema.getColumnIndex(id) != i++) { | ||
| isSimple = false; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| return isSimple; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.