tvalentyn commented on a change in pull request #14607:
URL: https://github.com/apache/beam/pull/14607#discussion_r617860561



##########
File path: 
sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/resourcehints/ResourceHints.java
##########
@@ -0,0 +1,270 @@
+/*
+ * 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.transforms.resourcehints;
+
+import static 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkState;
+
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import org.apache.beam.model.pipeline.v1.RunnerApi;
+import org.apache.beam.model.pipeline.v1.RunnerApi.StandardResourceHints;
+import org.apache.beam.sdk.options.PipelineOptions;
+import 
org.apache.beam.vendor.grpc.v1p26p0.com.google.protobuf.ProtocolMessageEnum;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Charsets;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Splitter;
+import 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap;
+
+public class ResourceHints {
+  private static final String MIN_RAM_URN = "beam:resources:min_ram_bytes:v1";
+  private static final String ACCELERATOR_URN = 
"beam:resources:accelerator:v1";
+
+  // TODO: reference this from a common location in all packages that use this.
+  private static String getUrn(ProtocolMessageEnum value) {
+    return 
value.getValueDescriptor().getOptions().getExtension(RunnerApi.beamUrn);
+  }
+
+  static {
+    
checkState(MIN_RAM_URN.equals(getUrn(StandardResourceHints.Enum.MIN_RAM_BYTES)));
+    
checkState(ACCELERATOR_URN.equals(getUrn(StandardResourceHints.Enum.ACCELERATOR)));
+  }
+
+  private static ImmutableMap<String, String> hintNameToUrn =
+      ImmutableMap.<String, String>builder()
+          .put("minRam", MIN_RAM_URN)
+          .put("min_ram", MIN_RAM_URN) // Courtesy alias.
+          .put("accelerator", ACCELERATOR_URN)
+          .build();
+
+  private static ImmutableMap<String, Function<String, ResourceHint>> parsers =
+      ImmutableMap.<String, Function<String, ResourceHint>>builder()
+          .put(MIN_RAM_URN, s -> new BytesHint(BytesHint.parse(s)))
+          .put(ACCELERATOR_URN, s -> new StringHint(s))
+          .build();
+
+  private static ResourceHints empty = new ResourceHints(ImmutableMap.of());
+
+  private final ImmutableMap<String, ResourceHint> hints;
+
+  private ResourceHints(ImmutableMap<String, ResourceHint> hints) {
+    this.hints = hints;
+  }
+
+  public static ResourceHints create() {
+    return empty;
+  }
+
+  public static ResourceHints fromOptions(PipelineOptions options) {
+    ResourceHintsOptions resourceHintsOptions = 
options.as(ResourceHintsOptions.class);
+    ResourceHints result = create();
+    if (resourceHintsOptions.getResourceHints() == null) {
+      return result;
+    }
+    Splitter splitter = Splitter.on('=').limit(2);
+    for (String hint : resourceHintsOptions.getResourceHints()) {
+      List<String> parts = splitter.splitToList(hint);
+      if (parts.size() != 2) {
+        throw new IllegalArgumentException("Unparsable resource hint: " + 
hint);
+      }
+      String nameOrUrn = parts.get(0);
+      String stringValue = parts.get(1);
+      String urn;
+      if (hintNameToUrn.containsKey(nameOrUrn)) {
+        urn = hintNameToUrn.get(nameOrUrn);
+      } else if (!nameOrUrn.startsWith("beam:resources:")) {
+        // Allow unknown hints to be passed, but validate a little bit to 
prevent typos.
+        throw new IllegalArgumentException("Unknown resource hint: " + hint);
+      } else {
+        urn = nameOrUrn;
+      }
+      ResourceHint value = parsers.getOrDefault(urn, s -> new 
StringHint(s)).apply(stringValue);
+      result = result.withHint(urn, value);
+    }
+    return result;
+  }
+
+  /*package*/ static class BytesHint extends ResourceHint {
+    private static Map<String, Long> suffixes =
+        ImmutableMap.<String, Long>builder()
+            .put("B", 1L)
+            .put("KB", 1000L)
+            .put("MB", 1000_000L)
+            .put("GB", 1000_000_000L)
+            .put("TB", 1000_000_000_000L)
+            .put("PB", 1000_000_000_000_000L)
+            .put("KiB", 1L << 10)
+            .put("MiB", 1L << 20)
+            .put("GiB", 1L << 30)
+            .put("TiB", 1L << 40)
+            .put("PiB", 1L << 50)
+            .build();
+
+    private final long value;
+
+    @Override
+    public boolean equals(Object other) {
+      if (this == other) {
+        return true;
+      } else if (other instanceof BytesHint) {
+        return ((BytesHint) other).value == value;
+      } else {
+        return false;
+      }
+    }
+
+    @Override
+    public int hashCode() {
+      return Long.hashCode(value);
+    }
+
+    public BytesHint(long value) {
+      this.value = value;
+    }
+
+    public static long parse(String s) {
+      Matcher m = Pattern.compile("([\\d.]+)[\\s]?(([KMGTP]i?)?B)").matcher(s);
+      // Matcher m = Pattern.compile("^\\d\\.?\\d*").matcher(s);

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:
[email protected]


Reply via email to