-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tasks assignment strategy - commons integration - [KCON-63] (#384)
[KCON-63] - Integrate Task assignment strategies of common module into s3 release feature branch - Delete hard coding of file pattern from s3 iterator class - Update existing tests - Added new integration tests to verify other strategy use cases
- Loading branch information
1 parent
c4604f4
commit 6b967d3
Showing
27 changed files
with
951 additions
and
901 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
88 changes: 88 additions & 0 deletions
88
commons/src/main/java/io/aiven/kafka/connect/common/source/input/utils/FilePatternUtils.java
This file contains 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,88 @@ | ||
/* | ||
* Copyright 2025 Aiven Oy | ||
* | ||
* 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 io.aiven.kafka.connect.common.source.input.utils; | ||
|
||
import java.util.Optional; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.apache.kafka.common.config.ConfigException; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
public final class FilePatternUtils { | ||
|
||
public static final String PATTERN_PARTITION_KEY = "partition"; | ||
public static final String PATTERN_TOPIC_KEY = "topic"; | ||
public static final String START_OFFSET_PATTERN = "{{start_offset}}"; | ||
public static final String TIMESTAMP_PATTERN = "{{timestamp}}"; | ||
public static final String PARTITION_PATTERN = "{{" + PATTERN_PARTITION_KEY + "}}"; | ||
public static final String TOPIC_PATTERN = "{{" + PATTERN_TOPIC_KEY + "}}"; | ||
|
||
// Use a named group to return the partition in a complex string to always get the correct information for the | ||
// partition number. | ||
public static final String PARTITION_NAMED_GROUP_REGEX_PATTERN = "(?<" + PATTERN_PARTITION_KEY + ">\\d+)"; | ||
public static final String NUMBER_REGEX_PATTERN = "(?:\\d+)"; | ||
public static final String TOPIC_NAMED_GROUP_REGEX_PATTERN = "(?<" + PATTERN_TOPIC_KEY + ">[a-zA-Z0-9\\-_.]+)"; | ||
|
||
private FilePatternUtils() { | ||
// hidden | ||
} | ||
public static Pattern configurePattern(final String expectedSourceNameFormat) { | ||
if (expectedSourceNameFormat == null || !expectedSourceNameFormat.contains(PARTITION_PATTERN)) { | ||
throw new ConfigException(String.format( | ||
"Source name format %s missing partition pattern {{partition}} please configure the expected source to include the partition pattern.", | ||
expectedSourceNameFormat)); | ||
} | ||
// Build REGEX Matcher | ||
String regexString = StringUtils.replace(expectedSourceNameFormat, START_OFFSET_PATTERN, NUMBER_REGEX_PATTERN); | ||
regexString = StringUtils.replace(regexString, TIMESTAMP_PATTERN, NUMBER_REGEX_PATTERN); | ||
regexString = StringUtils.replace(regexString, TOPIC_PATTERN, TOPIC_NAMED_GROUP_REGEX_PATTERN); | ||
regexString = StringUtils.replace(regexString, PARTITION_PATTERN, PARTITION_NAMED_GROUP_REGEX_PATTERN); | ||
try { | ||
return Pattern.compile(regexString); | ||
} catch (IllegalArgumentException iae) { | ||
throw new ConfigException( | ||
String.format("Unable to compile the regex pattern %s to retrieve the partition id.", regexString), | ||
iae); | ||
} | ||
} | ||
|
||
public static Optional<String> getTopic(final Pattern filePattern, final String sourceName) { | ||
return matchPattern(filePattern, sourceName).map(matcher -> matcher.group(PATTERN_TOPIC_KEY)); | ||
} | ||
|
||
public static Optional<Integer> getPartitionId(final Pattern filePattern, final String sourceName) { | ||
return matchPattern(filePattern, sourceName).flatMap(matcher -> { | ||
try { | ||
return Optional.of(Integer.parseInt(matcher.group(PATTERN_PARTITION_KEY))); | ||
} catch (NumberFormatException e) { | ||
return Optional.empty(); | ||
} | ||
}); | ||
} | ||
|
||
private static Optional<Matcher> matchPattern(final Pattern filePattern, final String sourceName) { | ||
if (filePattern == null || sourceName == null) { | ||
throw new IllegalArgumentException("filePattern and sourceName must not be null"); | ||
} | ||
|
||
final Matcher matcher = filePattern.matcher(sourceName); | ||
return matcher.find() ? Optional.of(matcher) : Optional.empty(); | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
commons/src/main/java/io/aiven/kafka/connect/common/source/task/DistributionStrategy.java
This file contains 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,51 @@ | ||
/* | ||
* Copyright 2024 Aiven Oy | ||
* | ||
* 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 io.aiven.kafka.connect.common.source.task; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* An {@link DistributionStrategy} provides a mechanism to share the work of processing records from objects (or files) | ||
* into tasks, which are subsequently processed (potentially in parallel) by Kafka Connect workers. | ||
* <p> | ||
* The number of objects in cloud storage can be very high, and they are distributed amongst tasks to minimize the | ||
* overhead of assigning work to Kafka worker threads. All objects assigned to the same task will be processed together | ||
* sequentially by the same worker, which can be useful for maintaining order between objects. There are usually fewer | ||
* workers than tasks, and they will be assigned the remaining tasks as work completes. | ||
*/ | ||
public interface DistributionStrategy { | ||
/** | ||
* Check if the object should be processed by the task with the given {@code taskId}. Any single object should be | ||
* assigned deterministically to a single taskId. | ||
* | ||
* @param taskId | ||
* a task ID, usually for the currently running task | ||
* @param valueToBeEvaluated | ||
* The value to be evaluated to determine if it should be processed by the task. | ||
* @return true if the task should process the object, false if it should not. | ||
*/ | ||
boolean isPartOfTask(int taskId, String valueToBeEvaluated, Pattern filePattern); | ||
|
||
/** | ||
* When a connector receives a reconfigure event this method should be called to ensure that the distribution | ||
* strategy is updated correctly. | ||
* | ||
* @param maxTasks | ||
* The maximum number of tasks created for the Connector | ||
*/ | ||
void configureDistributionStrategy(int maxTasks); | ||
} |
This file contains 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
Oops, something went wrong.