[
https://issues.apache.org/jira/browse/BEAM-5865?focusedWorklogId=239696&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-239696
]
ASF GitHub Bot logged work on BEAM-5865:
----------------------------------------
Author: ASF GitHub Bot
Created on: 09/May/19 10:38
Start Date: 09/May/19 10:38
Worklog Time Spent: 10m
Work Description: JozoVilcek commented on pull request #8499: [BEAM-5865]
Create optional auto-balancing sharding function for Flink
URL: https://github.com/apache/beam/pull/8499#discussion_r282431407
##########
File path:
runners/flink/src/main/java/org/apache/beam/runners/flink/FlinkStreamingPipelineTranslator.java
##########
@@ -325,74 +357,63 @@ private FlinkAutoBalancedShardKeyShardingFunction(
int destinationKey =
Arrays.hashCode(CoderUtils.encodeToByteArray(destinationCoder,
destination));
+
+ if (cache == null) {
+ cache =
+ CacheBuilder.newBuilder()
+ .maximumSize(CACHE_MAX_SIZE)
+ .expireAfterAccess(CACHE_EXPIRE_SECONDS, TimeUnit.SECONDS)
+ .build();
+ }
+
// we need to ensure that keys are always stable no matter at which
worker they
// are created and what is an order of observed shard numbers
- if (cache.size() < shardNumber) {
- for (int i = 0; i < shardNumber; i++) {
- generateInternal(new CacheKey(destinationKey, i));
- }
+ if (cache.getIfPresent(destinationKey) == null) {
+ cache.put(destinationKey, generateShardedKeys(destinationKey,
shardCount));
}
- return generateInternal(new CacheKey(destinationKey, shardNumber));
+ return cache.getIfPresent(destinationKey).get(shardNumber);
}
- private ShardedKey<Integer> generateInternal(CacheKey key) {
-
- ShardedKey<Integer> result = cache.get(key);
- if (result != null) {
- return result;
- }
-
- int salt = -1;
- while (true) {
- salt++;
- ShardedKey<Integer> shk = ShardedKey.of(Objects.hash(key.key, salt),
key.shard);
- int targetPartition = key.shard % parallelism;
-
- // create effective key in the same way Beam/Flink will do so we can
see if it gets
- // allocated to the partition we want
- ByteBuffer effectiveKey;
- try {
- byte[] bytes =
CoderUtils.encodeToByteArray(ShardedKeyCoder.of(VarIntCoder.of()), shk);
- effectiveKey = ByteBuffer.wrap(bytes);
- } catch (CoderException e) {
- throw new RuntimeException(e);
- }
-
- int partition =
- KeyGroupRangeAssignment.assignKeyToParallelOperator(
- effectiveKey, maxParallelism, parallelism);
-
- if (partition == targetPartition && !usedSalts.contains(salt)) {
- usedSalts.add(salt);
- cache.put(key, shk);
- return shk;
+ private Map<Integer, ShardedKey<Integer>> generateShardedKeys(int key, int
shardCount) {
+
+ Set<Integer> usedSalts = new HashSet<>();
+ Map<Integer, ShardedKey<Integer>> shardedKeys = new HashMap<>();
+
+ for (int shard = 0; shard < shardCount; shard++) {
+
+ int salt = -1;
+ while (true) {
+ if (salt++ == Integer.MAX_VALUE) {
+ throw new RuntimeException(
+ "Failed to find sharded key in [ " + Integer.MAX_VALUE + " ]
iterations");
+ }
+ ShardedKey<Integer> shk = ShardedKey.of(Objects.hash(key, salt),
shard);
+ int targetPartition = shard % parallelism;
+
+ // create effective key in the same way Beam/Flink will do so we can
see if it gets
+ // allocated to the partition we want
+ ByteBuffer effectiveKey;
+ try {
+ byte[] bytes =
CoderUtils.encodeToByteArray(ShardedKeyCoder.of(VarIntCoder.of()), shk);
+ effectiveKey = ByteBuffer.wrap(bytes);
Review comment:
`FlinkKeyUtils` is package private. I assume it is OK to make it public?
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
Issue Time Tracking
-------------------
Worklog Id: (was: 239696)
Time Spent: 4.5h (was: 4h 20m)
> Auto sharding of streaming sinks in FlinkRunner
> -----------------------------------------------
>
> Key: BEAM-5865
> URL: https://issues.apache.org/jira/browse/BEAM-5865
> Project: Beam
> Issue Type: Improvement
> Components: runner-flink
> Reporter: Maximilian Michels
> Assignee: Jozef Vilcek
> Priority: Major
> Time Spent: 4.5h
> Remaining Estimate: 0h
>
> The Flink Runner should do auto-sharding of streaming sinks, similar to
> BEAM-1438. That way, the user doesn't have to set shards manually which
> introduces additional shuffling and might cause skew in the distribution of
> data.
> As per discussion:
> https://lists.apache.org/thread.html/7b92145dd9ae68da1866f1047445479f51d31f103d6407316bb4114c@%3Cuser.beam.apache.org%3E
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)