[ 
https://issues.apache.org/jira/browse/BEAM-10475?focusedWorklogId=500201&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-500201
 ]

ASF GitHub Bot logged work on BEAM-10475:
-----------------------------------------

                Author: ASF GitHub Bot
            Created on: 13/Oct/20 17:40
            Start Date: 13/Oct/20 17:40
    Worklog Time Spent: 10m 
      Work Description: lukecwik commented on a change in pull request #13069:
URL: https://github.com/apache/beam/pull/13069#discussion_r504123726



##########
File path: model/pipeline/src/main/proto/beam_runner_api.proto
##########
@@ -879,6 +879,29 @@ message StandardCoders {
     // Components: None
     // Experimental.
     ROW = 13 [(beam_urn) = "beam:coder:row:v1"];
+
+    // Encodes a use key and a shard id which is an opaque byte string.
+    //
+    // The encoding for a sharded key consists of the length prefixed shard id
+    // and the encoded user key in the following order:
+    //
+    //     varInt(len(shard id))

Review comment:
       Instead of referencing varInt(len(shard id)) shard id we should just say 
that the shard id is encoded using `beam:coder:bytes:v1`
   
   Other coders descriptions should be more like row where we refer to other 
well known encodings.

##########
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}.

Review comment:
       In the long run we should aim to replace the existing implementation and 
its usage within WriteFiles.

##########
File path: sdks/python/apache_beam/coders/coder_impl.py
##########
@@ -1365,3 +1366,38 @@ def estimate_size(self, value, nested=False):
     # type: (Any, bool) -> int
     value_size = self._value_coder.estimate_size(value)
     return get_varint_size(value_size) + value_size
+
+
+class ShardedKeyCoderImpl(StreamCoderImpl):
+  """For internal use only; no backwards-compatibility guarantees.
+
+  A coder for sharded user keys.
+
+  The encoding and decoding should follow the order:
+      length of shard id byte string
+      shard id byte string
+      encoded user key
+  """
+  def __init__(self, key_coder_impl):
+    self._shard_id_coder_impl = LengthPrefixCoderImpl(BytesCoderImpl())
+    self._key_coder_impl = key_coder_impl
+
+  def encode_to_stream(self, value, out, nested):
+    # type: (ShardedKey, create_OutputStream, bool) -> None
+    self._shard_id_coder_impl.encode_to_stream(value.shard_id, out, True)
+    self._key_coder_impl.encode_to_stream(value.key, out, True)
+
+  def decode_from_stream(self, in_stream, nested):
+    # type: (create_InputStream, bool) -> ShardedKey
+    shard_id = self._shard_id_coder_impl.decode_from_stream(in_stream, True)
+    key = self._key_coder_impl.decode_from_stream(in_stream, True)
+    return ShardedKey(key=key, shard_id=shard_id)
+
+  def estimate_size(self, value, nested=False):
+    # type: (Any, bool) -> int
+    estimated_size = 0
+    estimated_size += (
+        self._shard_id_coder_impl.estimate_size(value.shard_id, nested=True))
+    estimated_size += (
+        self._key_coder_impl.estimate_size(value.key, nested=True))

Review comment:
       ```suggestion
           self._key_coder_impl.estimate_size(value.key, nested=nested))
   ```

##########
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:
       the bytearraycoder isn't consistent with equals yet the AutoValue 
ShardedKey is so we should state that ShardedKey is consistent with equals iff 
keyCoder is.
   ```suggestion
         return keyCoder.consistentWithEquals();
   ```

##########
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();

Review comment:
       No point in checking something that is required to be deterministic
   ```suggestion
   ```

##########
File path: sdks/python/apache_beam/coders/coder_impl.py
##########
@@ -1365,3 +1366,38 @@ def estimate_size(self, value, nested=False):
     # type: (Any, bool) -> int
     value_size = self._value_coder.estimate_size(value)
     return get_varint_size(value_size) + value_size
+
+
+class ShardedKeyCoderImpl(StreamCoderImpl):
+  """For internal use only; no backwards-compatibility guarantees.
+
+  A coder for sharded user keys.
+
+  The encoding and decoding should follow the order:
+      length of shard id byte string
+      shard id byte string
+      encoded user key
+  """
+  def __init__(self, key_coder_impl):
+    self._shard_id_coder_impl = LengthPrefixCoderImpl(BytesCoderImpl())
+    self._key_coder_impl = key_coder_impl
+
+  def encode_to_stream(self, value, out, nested):
+    # type: (ShardedKey, create_OutputStream, bool) -> None
+    self._shard_id_coder_impl.encode_to_stream(value.shard_id, out, True)
+    self._key_coder_impl.encode_to_stream(value.key, out, True)

Review comment:
       ```suggestion
       self._key_coder_impl.encode_to_stream(value.key, out, nested)
   ```

##########
File path: sdks/python/apache_beam/coders/coder_impl.py
##########
@@ -1365,3 +1366,38 @@ def estimate_size(self, value, nested=False):
     # type: (Any, bool) -> int
     value_size = self._value_coder.estimate_size(value)
     return get_varint_size(value_size) + value_size
+
+
+class ShardedKeyCoderImpl(StreamCoderImpl):
+  """For internal use only; no backwards-compatibility guarantees.
+
+  A coder for sharded user keys.
+
+  The encoding and decoding should follow the order:
+      length of shard id byte string
+      shard id byte string
+      encoded user key
+  """
+  def __init__(self, key_coder_impl):
+    self._shard_id_coder_impl = LengthPrefixCoderImpl(BytesCoderImpl())

Review comment:
       Why do you need the LengthPrefixCoderImpl here?

##########
File path: sdks/python/apache_beam/coders/coder_impl.py
##########
@@ -1365,3 +1366,38 @@ def estimate_size(self, value, nested=False):
     # type: (Any, bool) -> int
     value_size = self._value_coder.estimate_size(value)
     return get_varint_size(value_size) + value_size
+
+
+class ShardedKeyCoderImpl(StreamCoderImpl):
+  """For internal use only; no backwards-compatibility guarantees.
+
+  A coder for sharded user keys.
+
+  The encoding and decoding should follow the order:
+      length of shard id byte string
+      shard id byte string
+      encoded user key
+  """
+  def __init__(self, key_coder_impl):
+    self._shard_id_coder_impl = LengthPrefixCoderImpl(BytesCoderImpl())
+    self._key_coder_impl = key_coder_impl
+
+  def encode_to_stream(self, value, out, nested):
+    # type: (ShardedKey, create_OutputStream, bool) -> None
+    self._shard_id_coder_impl.encode_to_stream(value.shard_id, out, True)
+    self._key_coder_impl.encode_to_stream(value.key, out, True)
+
+  def decode_from_stream(self, in_stream, nested):
+    # type: (create_InputStream, bool) -> ShardedKey
+    shard_id = self._shard_id_coder_impl.decode_from_stream(in_stream, True)
+    key = self._key_coder_impl.decode_from_stream(in_stream, True)

Review comment:
       ```suggestion
       key = self._key_coder_impl.decode_from_stream(in_stream, nested)
   ```

##########
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();

Review comment:
       See KvCoder for an example:
   ```suggestion
         verifyDeterministic(this, "Key coder must be deterministic", keyCoder);
   ```

##########
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);

Review comment:
       Please make `byte[0]` a constant. Please add comment saying that the 
default shard identifier is the empty byte string.




----------------------------------------------------------------
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: 500201)
    Time Spent: 8h 40m  (was: 8.5h)

> 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: 8h 40m
>  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)

Reply via email to