hangc0276 commented on a change in pull request #13316:
URL: https://github.com/apache/pulsar/pull/13316#discussion_r777925124



##########
File path: 
pulsar-client/src/main/java/org/apache/pulsar/client/impl/AutoClusterFailover.java
##########
@@ -0,0 +1,232 @@
+/**
+ * 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.client.impl;
+
+import static 
org.apache.pulsar.common.util.Runnables.catchingAndLoggingThrowables;
+import com.google.common.base.Strings;
+import io.netty.util.concurrent.DefaultThreadFactory;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.client.api.AutoClusterFailoverBuilder;
+import org.apache.pulsar.client.api.PulsarClient;
+import org.apache.pulsar.client.api.PulsarClientException;
+import org.apache.pulsar.client.api.ServiceUrlProvider;
+
+@Slf4j
+@Data
+public class AutoClusterFailover implements ServiceUrlProvider {
+    private PulsarClient pulsarClient;
+    private volatile String currentPulsarServiceUrl;
+    private final String primary;
+    private final String secondary;
+    private final long failoverDelayNs;
+    private final long switchBackDelayNs;
+    private final ScheduledExecutorService executor;
+    private long recoverTimestamp;
+    private long failedTimestamp;
+    private final int interval = 30_000;
+    private final int TIMEOUT = 30_000;
+
+    private AutoClusterFailover(String primary, String secondary, long 
failoverDelayNs, long switchBackDelayNs) {
+        this.primary = primary;
+        this.secondary = secondary;
+        this.failoverDelayNs = failoverDelayNs;
+        this.switchBackDelayNs = switchBackDelayNs;
+        this.currentPulsarServiceUrl = primary;
+        this.recoverTimestamp = -1;
+        this.failedTimestamp = -1;
+        this.executor = Executors.newSingleThreadScheduledExecutor(
+                new DefaultThreadFactory("pulsar-service-provider"));
+    }
+
+    @Override
+    public void initialize(PulsarClient client) {
+        this.pulsarClient = client;
+
+        // start to probe primary cluster active or not
+        this.executor.scheduleAtFixedRate(catchingAndLoggingThrowables(() -> {
+            if (currentPulsarServiceUrl.equals(primary)) {
+                // current service url is primary, probe whether it is down
+                probeAndUpdateServiceUrl(secondary);
+            } else {
+                // current service url is secondary, probe whether it is down
+                probeAndUpdateServiceUrl(primary);
+                // secondary cluster is up, check whether need to switch back 
to primary
+                probeAndCheckSwitchBack(primary);
+            }
+        }), getInterval(), getInterval(), TimeUnit.MILLISECONDS);
+
+    }
+
+    @Override
+    public String getServiceUrl() {
+        return this.currentPulsarServiceUrl;
+    }
+
+    @Override
+    public void close() {
+        this.executor.shutdown();
+    }
+
+    boolean probeAvailable(String url) {
+        try {
+            String hostAndPort = parseHostAndPort(url);
+            if (Strings.isNullOrEmpty(hostAndPort)) {
+                return false;
+            }
+
+            Socket socket = new Socket();
+            socket.connect(new InetSocketAddress(parseHost(hostAndPort), 
parsePort(hostAndPort)), TIMEOUT);

Review comment:
       Yes, the current probe port method has many disadvantages. We can use 
this method first, and in next step, we'd better provide a health check 
command, just like Zookeeper `ruok` command, to probe the healthy of the target 
Pulsar cluster.

##########
File path: 
pulsar-client-api/src/main/java/org/apache/pulsar/client/api/AutoClusterFailoverBuilder.java
##########
@@ -0,0 +1,74 @@
+/**
+ * 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.client.api;
+
+import java.util.concurrent.TimeUnit;
+import org.apache.pulsar.common.classification.InterfaceAudience;
+import org.apache.pulsar.common.classification.InterfaceStability;
+
+/**
+ * {@link AutoClusterFailoverBuilder} is used to configure and create instance 
of {@link ServiceUrlProvider}
+ *
+ * @since 2.10.0
+ */
[email protected]
[email protected]
+public interface AutoClusterFailoverBuilder {
+    /**
+     * Set the primary service url.
+     *
+     * @param primary
+     * @return
+     */
+    AutoClusterFailoverBuilder primary(String primary);
+
+    /**
+     * Set the secondary service url.
+     *
+     * @param secondary
+     * @return
+     */
+    AutoClusterFailoverBuilder secondary(String secondary);

Review comment:
       > We could take a list/set of serviceUrls, just to make it more general
   
   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