You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2020/06/05 07:32:11 UTC

[GitHub] [pulsar] srkukarni commented on a change in pull request #7180: FunctionAssignmentTailer should use its own thread

srkukarni commented on a change in pull request #7180:
URL: https://github.com/apache/pulsar/pull/7180#discussion_r435739838



##########
File path: pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/FunctionAssignmentTailer.java
##########
@@ -18,56 +18,90 @@
  */
 package org.apache.pulsar.functions.worker;
 
-import java.io.IOException;
-import java.util.function.Function;
-
+import lombok.Getter;
+import lombok.extern.slf4j.Slf4j;
 import org.apache.pulsar.client.api.Message;
-import org.apache.pulsar.client.api.PulsarClientException.AlreadyClosedException;
+import org.apache.pulsar.client.api.MessageId;
+import org.apache.pulsar.client.api.PulsarClientException;
 import org.apache.pulsar.client.api.Reader;
-import org.apache.pulsar.common.util.FutureUtil;
+import org.apache.pulsar.client.api.ReaderBuilder;
 import org.apache.pulsar.functions.proto.Function.Assignment;
 
-import lombok.extern.slf4j.Slf4j;
+import java.io.IOException;
 
 @Slf4j
-public class FunctionAssignmentTailer
-    implements java.util.function.Consumer<Message<byte[]>>, Function<Throwable, Void>, AutoCloseable {
+public class FunctionAssignmentTailer implements AutoCloseable {
 
     private final FunctionRuntimeManager functionRuntimeManager;
+    @Getter
     private final Reader<byte[]> reader;
-    private boolean closed = false;
+    private volatile boolean isRunning = false;
 
-    public FunctionAssignmentTailer(FunctionRuntimeManager functionRuntimeManager, Reader<byte[]> reader) {
+    private final Thread tailerThread;
+    
+    public FunctionAssignmentTailer(FunctionRuntimeManager functionRuntimeManager, ReaderBuilder readerBuilder, WorkerConfig workerConfig) throws PulsarClientException {
         this.functionRuntimeManager = functionRuntimeManager;
-        this.reader = reader;
-    }
+        
+        this.reader = readerBuilder
+          .subscriptionRolePrefix(workerConfig.getWorkerId() + "-function-runtime-manager")
+          .readerName(workerConfig.getWorkerId() + "-function-runtime-manager")
+          .topic(workerConfig.getFunctionAssignmentTopic())
+          .readCompacted(true)
+          .startMessageId(MessageId.earliest)
+          .create();
 
-    public void start() {
-        receiveOne();
+        this.tailerThread = new Thread(() -> {
+            while(true) {
+                try {
+                    while(isRunning) {
+                        Message<byte[]> msg = reader.readNext();
+                        processAssignment(msg);
+                    }
+                } catch (Exception e) {
+                    if (isRunning) {
+                        log.error("Encountered error in assignment tailer", e);
+
+                        // trigger fatal error
+                        // TODO add mechanism to notify main thread
+                    } else {
+                        if (!(e instanceof InterruptedException)) {
+                            log.warn("Encountered error when assignment tailer is not running", e);
+                        }
+                    }
+
+                }
+            }
+        });
+        this.tailerThread.setName("assignment-tailer-thread");
     }
 
-    private void receiveOne() {
-        reader.readNextAsync()
-                .thenAccept(this)
-                .exceptionally(this);
+    public void start() {
+        isRunning = true;
+        tailerThread.start();
     }
 
     @Override
     public void close() {
-        if (closed) {
+        if (!isRunning) {

Review comment:
       reader is still not closed 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