[ https://issues.apache.org/jira/browse/BEAM-10475?focusedWorklogId=501271&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-501271 ]
ASF GitHub Bot logged work on BEAM-10475: ----------------------------------------- Author: ASF GitHub Bot Created on: 15/Oct/20 19:58 Start Date: 15/Oct/20 19:58 Worklog Time Spent: 10m Work Description: nehsyc commented on a change in pull request #13069: URL: https://github.com/apache/beam/pull/13069#discussion_r505803886 ########## File path: sdks/java/core/src/main/java/org/apache/beam/sdk/util/ShardedKey.java ########## @@ -0,0 +1,122 @@ +/* + * 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.beam.sdk.util; + +import static org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument; + +import com.google.auto.value.AutoValue; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Collections; +import java.util.List; +import org.apache.beam.sdk.coders.ByteArrayCoder; +import org.apache.beam.sdk.coders.StructuredCoder; +import org.apache.beam.sdk.util.common.ElementByteSizeObserver; + +/** + * A sharded key consisting of a user key and a shard id represented by bytes. + * + * <p>This is a more generic definition of {@link org.apache.beam.sdk.values.ShardedKey}. + */ +@AutoValue +public abstract class ShardedKey<K> { + + public static <K> ShardedKey<K> of(K key) { + return new AutoValue_ShardedKey(new byte[0], key); + } + + public static <K> ShardedKey<K> of(K key, byte[] shardId) { + checkArgument(shardId != null, "Shard id should not be null!"); + return new AutoValue_ShardedKey(shardId, key); + } + + @SuppressWarnings("mutable") + public abstract byte[] getShardId(); + + public abstract K getKey(); + + public static class Coder<K> extends StructuredCoder<ShardedKey<K>> { + + private final ByteArrayCoder shardCoder = ByteArrayCoder.of(); + private final org.apache.beam.sdk.coders.Coder<K> keyCoder; + + private Coder(org.apache.beam.sdk.coders.Coder<K> coder) { + keyCoder = coder; + } + + public static <K> ShardedKey.Coder<K> of(org.apache.beam.sdk.coders.Coder<K> keyCoder) { + return new ShardedKey.Coder<K>(keyCoder); + } + + public org.apache.beam.sdk.coders.Coder<K> getKeyCoder() { + return keyCoder; + } + + @Override + public void encode(ShardedKey<K> shardedKey, OutputStream outStream) throws IOException { + // The encoding should follow the order: + // length of shard id + // shard id + // encoded user key + shardCoder.encode(shardedKey.getShardId(), outStream); + keyCoder.encode(shardedKey.getKey(), outStream); + } + + @Override + public ShardedKey<K> decode(InputStream inStream) throws IOException { + byte[] shardId = shardCoder.decode(inStream); + K key = keyCoder.decode(inStream); + return ShardedKey.of(key, shardId); + } + + @Override + public List<? extends org.apache.beam.sdk.coders.Coder<?>> getCoderArguments() { + return Collections.singletonList(keyCoder); + } + + @Override + public void verifyDeterministic() throws NonDeterministicException { + shardCoder.verifyDeterministic(); + keyCoder.verifyDeterministic(); + } + + @Override + public boolean consistentWithEquals() { + return shardCoder.consistentWithEquals() && keyCoder.consistentWithEquals(); Review comment: Done. ---------------------------------------------------------------- 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: us...@infra.apache.org Issue Time Tracking ------------------- Worklog Id: (was: 501271) Time Spent: 11h 10m (was: 11h) > GroupIntoBatches with Runner-determined Sharding > ------------------------------------------------ > > Key: BEAM-10475 > URL: https://issues.apache.org/jira/browse/BEAM-10475 > Project: Beam > Issue Type: Improvement > Components: runner-dataflow > Reporter: Siyuan Chen > Assignee: Siyuan Chen > Priority: P2 > Labels: GCP, performance > Time Spent: 11h 10m > Remaining Estimate: 0h > > [https://s.apache.org/sharded-group-into-batches|https://s.apache.org/sharded-group-into-batches__] > Improve the existing Beam transform, GroupIntoBatches, to allow runners to > choose different sharding strategies depending on how the data needs to be > grouped. The goal is to help with the situation where the elements to process > need to be co-located to reduce the overhead that would otherwise be incurred > per element, while not losing the ability to scale the parallelism. The > essential idea is to build a stateful DoFn with shardable states. > -- This message was sent by Atlassian Jira (v8.3.4#803005)