scwhittle commented on a change in pull request #17125:
URL: https://github.com/apache/beam/pull/17125#discussion_r831193011



##########
File path: 
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/pubsublite/internal/MemoryBufferedSubscriberImpl.java
##########
@@ -0,0 +1,175 @@
+/*
+ * 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.io.gcp.pubsublite.internal;
+
+import com.google.api.core.ApiFuture;
+import com.google.api.core.ApiFutures;
+import com.google.api.core.SettableApiFuture;
+import com.google.api.gax.rpc.ApiException;
+import com.google.cloud.pubsublite.Offset;
+import com.google.cloud.pubsublite.Partition;
+import com.google.cloud.pubsublite.internal.CheckedApiException;
+import com.google.cloud.pubsublite.internal.ProxyService;
+import com.google.cloud.pubsublite.internal.wire.Subscriber;
+import com.google.cloud.pubsublite.proto.FlowControlRequest;
+import com.google.cloud.pubsublite.proto.SequencedMessage;
+import java.util.ArrayDeque;
+import java.util.List;
+import java.util.Optional;
+import java.util.Queue;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import org.apache.beam.sdk.io.gcp.pubsublite.internal.MemoryLimiter.Block;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class MemoryBufferedSubscriberImpl extends ProxyService implements 
MemoryBufferedSubscriber {
+  private static final Logger LOG = 
LoggerFactory.getLogger(MemoryBufferedSubscriberImpl.class);
+
+  private final Partition partition;
+  private final MemoryLimiter limiter;
+  private final Subscriber subscriber;
+  private final long maxMemory;
+  private long targetMemory;
+  private Offset fetchOffset;
+  private Block memBlock;
+
+  private long bytesOutstandingToServer = 0;
+  private long bytesOutstanding = 0;
+  private final Queue<SequencedMessage> messages = new ArrayDeque<>();
+  private SettableApiFuture<Void> newData = SettableApiFuture.create();
+  private boolean shutdown = false;
+
+  // onReceive will not be called inline as subscriber is not started.
+  // addServices is intended to be called from the constructor.
+  @SuppressWarnings({"methodref.receiver.bound.invalid", 
"method.invocation.invalid"})
+  public MemoryBufferedSubscriberImpl(
+      Partition partition,
+      Offset startOffset,
+      MemoryLimiter limiter,
+      Function<Consumer<List<SequencedMessage>>, Subscriber> subscriberFactory,
+      long maxMemory) {

Review comment:
       should max be part of the memorylimiter similar to min?

##########
File path: 
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/pubsublite/internal/MemoryBufferedSubscriberImpl.java
##########
@@ -0,0 +1,175 @@
+/*
+ * 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.io.gcp.pubsublite.internal;
+
+import com.google.api.core.ApiFuture;
+import com.google.api.core.ApiFutures;
+import com.google.api.core.SettableApiFuture;
+import com.google.api.gax.rpc.ApiException;
+import com.google.cloud.pubsublite.Offset;
+import com.google.cloud.pubsublite.Partition;
+import com.google.cloud.pubsublite.internal.CheckedApiException;
+import com.google.cloud.pubsublite.internal.ProxyService;
+import com.google.cloud.pubsublite.internal.wire.Subscriber;
+import com.google.cloud.pubsublite.proto.FlowControlRequest;
+import com.google.cloud.pubsublite.proto.SequencedMessage;
+import java.util.ArrayDeque;
+import java.util.List;
+import java.util.Optional;
+import java.util.Queue;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import org.apache.beam.sdk.io.gcp.pubsublite.internal.MemoryLimiter.Block;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class MemoryBufferedSubscriberImpl extends ProxyService implements 
MemoryBufferedSubscriber {
+  private static final Logger LOG = 
LoggerFactory.getLogger(MemoryBufferedSubscriberImpl.class);
+
+  private final Partition partition;
+  private final MemoryLimiter limiter;
+  private final Subscriber subscriber;
+  private final long maxMemory;
+  private long targetMemory;
+  private Offset fetchOffset;
+  private Block memBlock;
+
+  private long bytesOutstandingToServer = 0;
+  private long bytesOutstanding = 0;
+  private final Queue<SequencedMessage> messages = new ArrayDeque<>();
+  private SettableApiFuture<Void> newData = SettableApiFuture.create();
+  private boolean shutdown = false;
+
+  // onReceive will not be called inline as subscriber is not started.
+  // addServices is intended to be called from the constructor.
+  @SuppressWarnings({"methodref.receiver.bound.invalid", 
"method.invocation.invalid"})
+  public MemoryBufferedSubscriberImpl(
+      Partition partition,
+      Offset startOffset,
+      MemoryLimiter limiter,
+      Function<Consumer<List<SequencedMessage>>, Subscriber> subscriberFactory,
+      long maxMemory) {
+    this.partition = partition;
+    this.fetchOffset = startOffset;
+    this.limiter = limiter;
+    this.maxMemory = maxMemory;
+    this.targetMemory = maxMemory;
+    this.subscriber = subscriberFactory.apply(this::onReceive);
+    addServices(this.subscriber);
+    memBlock = limiter.claim(targetMemory);
+  }
+
+  @Override
+  protected synchronized void start() throws CheckedApiException {
+    bytesOutstandingToServer += memBlock.claimed();
+    bytesOutstanding += memBlock.claimed();
+    subscriber.allowFlow(
+        FlowControlRequest.newBuilder()
+            .setAllowedBytes(memBlock.claimed())
+            .setAllowedMessages(Long.MAX_VALUE)
+            .build());
+  }
+
+  @Override
+  protected synchronized void stop() {
+    if (shutdown) {
+      return;
+    }
+    shutdown = true;
+    newData.set(null);
+    memBlock.close();
+  }
+
+  @Override
+  protected synchronized void handlePermanentError(CheckedApiException e) {
+    stop();
+  }
+
+  private synchronized void onReceive(List<SequencedMessage> batch) {
+    if (shutdown) {
+      return;
+    }
+    for (SequencedMessage message : batch) {
+      bytesOutstandingToServer -= message.getSizeBytes();
+    }
+    messages.addAll(batch);
+    newData.set(null);
+    newData = SettableApiFuture.create();
+  }
+
+  @Override
+  public synchronized Offset fetchOffset() {
+    return fetchOffset;
+  }
+
+  @Override
+  public synchronized void rebuffer() throws ApiException {
+    if (shutdown) {
+      return;
+    }
+    if (bytesOutstandingToServer < (targetMemory / 3)) {
+      // Server is delivering lots of data

Review comment:
       How about expanding comments here.  
   // Server is delivering lots of data, increase the target so that it is not 
throttled.
   and below
   // Server is delivering little data, decrease the target so that memory can 
be used for other users of the limiter.
   
   And if my understanding is wrong even more reason to have comments :)

##########
File path: 
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/pubsublite/internal/MemoryLimiterImpl.java
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.io.gcp.pubsublite.internal;
+
+import static 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkState;
+
+import javax.annotation.concurrent.GuardedBy;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class MemoryLimiterImpl implements MemoryLimiter {
+  private static final Logger LOG = 
LoggerFactory.getLogger(MemoryLimiterImpl.class);
+  private final long minBlockSize;
+  private final long maxAvailable;
+
+  @GuardedBy("this")
+  private long available;
+
+  public MemoryLimiterImpl(long minBlockSize, long maxAvailable) {
+    this.minBlockSize = minBlockSize;
+    this.maxAvailable = available;
+    this.available = maxAvailable;
+  }
+
+  @Override
+  public synchronized Block claim(long toAcquire) {

Review comment:
       any concerns about fairness/prioritization?
   Seems like you could have one highthroughput subscriber starve another 
subscriber with a lot of backlog. 

##########
File path: 
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/pubsublite/internal/PerSubscriptionPartitionSdf.java
##########
@@ -61,8 +64,8 @@ public void teardown() {
   }
 
   @GetInitialWatermarkEstimatorState
-  public Instant getInitialWatermarkState() {
-    return Instant.EPOCH;
+  public Instant getInitialWatermarkState(@Timestamp Instant inputTs) {

Review comment:
       I think a comment would still be good




-- 
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