m-trieu commented on code in PR #29156:
URL: https://github.com/apache/beam/pull/29156#discussion_r1375013385


##########
runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/windmill/work/budget/GetWorkBudget.java:
##########
@@ -0,0 +1,105 @@
+/*
+ * 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.runners.dataflow.worker.windmill.work.budget;
+
+import com.google.auto.value.AutoValue;
+import 
org.apache.beam.runners.dataflow.worker.windmill.Windmill.GetWorkRequest;
+import org.apache.beam.runners.dataflow.worker.windmill.streams.WindmillStream;
+
+/**
+ * Budget of items and bytes for fetching {@link
+ * org.apache.beam.runners.dataflow.worker.windmill.Windmill.WorkItem}(s) via 
{@link
+ * WindmillStream.GetWorkStream}. Used to control how "much" work is returned 
from Windmill.
+ */
+@AutoValue
+public abstract class GetWorkBudget {
+  public static GetWorkBudget.Builder builder() {
+    return new AutoValue_GetWorkBudget.Builder();
+  }
+
+  /** {@link GetWorkBudget} of 0. */
+  public static GetWorkBudget noBudget() {
+    return builder().setItems(0).setBytes(0).build();
+  }
+
+  public static GetWorkBudget from(GetWorkRequest getWorkRequest) {
+    return builder()
+        .setItems(getWorkRequest.getMaxItems())
+        .setBytes(getWorkRequest.getMaxBytes())
+        .build();
+  }
+
+  /**
+   * Adds the given bytes and items or the current budget, returning a new 
{@link GetWorkBudget}.
+   * Does not drop below 0.
+   */
+  public GetWorkBudget add(long items, long bytes) {
+    return GetWorkBudget.builder()
+        .setBytes(Math.max(0, bytes() + bytes))
+        .setItems(Math.max(0, items() + items))
+        .build();
+  }
+
+  public GetWorkBudget add(GetWorkBudget other) {
+    return add(other.items(), other.bytes());
+  }
+
+  /**
+   * Subtracts the given bytes and items or the current budget, returning a 
new {@link
+   * GetWorkBudget}. Does not drop below 0.
+   */
+  public GetWorkBudget subtract(long items, long bytes) {
+    return GetWorkBudget.builder()
+        .setBytes(Math.max(0, bytes() - bytes))
+        .setItems(Math.max(0, items() - items))
+        .build();
+  }
+
+  public GetWorkBudget subtract(GetWorkBudget other) {
+    return subtract(other.items(), other.bytes());
+  }
+
+  public boolean isLessThanFiftyPercentOf(GetWorkBudget target) {

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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to