pulsar-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [pulsar] srkukarni commented on a change in pull request #7237: Fix leader/scheduler assignment processing lag problem
Date Fri, 12 Jun 2020 17:09:54 GMT

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



##########
File path: pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/LeaderService.java
##########
@@ -0,0 +1,118 @@
+/**
+ * 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.pulsar.functions.worker;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.client.api.Consumer;
+import org.apache.pulsar.client.api.ConsumerEventListener;
+import org.apache.pulsar.client.api.PulsarClient;
+import org.apache.pulsar.client.api.PulsarClientException;
+import org.apache.pulsar.client.api.SubscriptionType;
+import org.apache.pulsar.client.impl.ConsumerImpl;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+@Slf4j
+public class LeaderService implements AutoCloseable, ConsumerEventListener {
+
+    private final String consumerName;
+    private final FunctionAssignmentTailer functionAssignmentTailer;
+    private final ErrorNotifier errorNotifier;
+    private ConsumerImpl<byte[]> consumer;
+    private final WorkerConfig workerConfig;
+    private final PulsarClient pulsarClient;
+    private final AtomicBoolean isLeader = new AtomicBoolean(false);
+
+    static final String COORDINATION_TOPIC_SUBSCRIPTION = "participants";
+
+    private static String WORKER_IDENTIFIER = "id";
+    
+    public LeaderService(WorkerService workerService,
+                         PulsarClient pulsarClient,
+                         FunctionAssignmentTailer functionAssignmentTailer,
+                         ErrorNotifier errorNotifier) {
+        this.workerConfig = workerService.getWorkerConfig();
+        this.pulsarClient = pulsarClient;
+        this.functionAssignmentTailer = functionAssignmentTailer;
+        this.errorNotifier = errorNotifier;
+        consumerName = String.format(
+                "%s:%s:%d",
+                workerConfig.getWorkerId(),
+                workerConfig.getWorkerHostname(),
+                workerConfig.getWorkerPort()
+        );
+
+    }
+
+    public void start() throws PulsarClientException {
+        // the leaders service is using a `coordination` topic for leader election.
+        // we don't produce any messages into this topic, we only use the `failover` subscription
+        // to elect an active consumer as the leader worker. The leader worker will be responsible
+        // for scheduling snapshots for FMT and doing task assignment.
+        consumer = (ConsumerImpl<byte[]>) pulsarClient.newConsumer()
+                .topic(workerConfig.getClusterCoordinationTopic())
+                .subscriptionName(COORDINATION_TOPIC_SUBSCRIPTION)
+                .subscriptionType(SubscriptionType.Failover)
+                .consumerEventListener(this)
+                .property(WORKER_IDENTIFIER, consumerName)
+                .subscribe();
+
+    }
+
+    @Override
+    public void becameActive(Consumer<?> consumer, int partitionId) {
+        if (isLeader.compareAndSet(false, true)) {
+            log.info("Worker {} became the leader.", consumerName);
+            try {
+                // trigger read to the end of the topic and exit
+                // Since the leader can just update its in memory assignments cache directly
+                functionAssignmentTailer.triggerReadToTheEndAndExit().get();

Review comment:
       Yup. That is the same pattern in https://github.com/apache/pulsar/pull/7255 as well




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



Mime
View raw message