You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2020/10/12 19:11:29 UTC

[GitHub] [beam] robertwb commented on a change in pull request #13069: [BEAM-10475] Add a well-known coder for ShardedKey in Java SDK

robertwb commented on a change in pull request #13069:
URL: https://github.com/apache/beam/pull/13069#discussion_r503424858



##########
File path: model/pipeline/src/main/proto/beam_runner_api.proto
##########
@@ -1215,23 +1243,23 @@ message StandardArtifacts {
   enum Types {
     // A URN for locally-accessible artifact files.
     // payload: ArtifactFilePayload
-    FILE      = 0 [(beam_urn) = "beam:artifact:type:file:v1"];
+    FILE = 0 [(beam_urn) = "beam:artifact:type:file:v1"];

Review comment:
       Can you remove the irrelevant whitespace changes? 

##########
File path: sdks/python/apache_beam/portability/api/standard_coders.yaml
##########
@@ -0,0 +1,410 @@
+#

Review comment:
       We shouldn't add this, it should be copied on demand. Otherwise things may diverge which defeats the purpose of standard coders. 

##########
File path: runners/core-construction-java/src/test/java/org/apache/beam/runners/core/construction/CommonCoderTest.java
##########
@@ -89,6 +90,7 @@
 /** Tests that Java SDK coders standardized by the Fn API meet the common spec. */
 @RunWith(Parameterized.class)
 public class CommonCoderTest {
+

Review comment:
       There seems to be lots of whitespace changes in this file (and elsewhere). 

##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/util/ShardedKey.java
##########
@@ -0,0 +1,117 @@
+/*
+ * 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 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 javax.annotation.Nullable;
+import org.apache.beam.sdk.coders.BooleanCoder;
+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, @Nullable byte[] shardId) {
+    return new AutoValue_ShardedKey(shardId, key);
+  }
+
+  @SuppressWarnings("mutable")
+  @Nullable
+  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:
+      //   null indicator
+      //   length of shard id
+      //   shard id
+      //   encoded user key
+      BooleanCoder.of().encode(shardedKey.getShardId() != null, outStream);
+      if (shardedKey.getShardId() != null) {
+        shardCoder.encode(shardedKey.getShardId(), outStream);

Review comment:
       The two encodings are the same for all values in the range we'll be dealing with (and neither Java nor GRPC can handle >4G sized byte arrays) so we should be fine here. 

##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/util/ShardedKey.java
##########
@@ -0,0 +1,117 @@
+/*
+ * 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 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 javax.annotation.Nullable;
+import org.apache.beam.sdk.coders.BooleanCoder;
+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, @Nullable byte[] shardId) {
+    return new AutoValue_ShardedKey(shardId, key);
+  }
+
+  @SuppressWarnings("mutable")
+  @Nullable
+  public abstract byte[] getShardId();

Review comment:
       Are we leveraging AutoValue for hash and equality? If so, I don't think that'll work with byte[] here. 




----------------------------------------------------------------
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:
users@infra.apache.org