Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PIP-121: Pulsar cluster level auto failover on client side #13316

Merged
merged 18 commits into from
Jan 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/**
* 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.List;
import java.util.Map;
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
*/
@InterfaceAudience.Public
@InterfaceStability.Evolving
public interface AutoClusterFailoverBuilder {

@SuppressWarnings("checkstyle:javadoctype")
enum FailoverPolicy {
ORDER
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does ORDER mean?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use List to store the secondary serviceUrls, and probe them by given order. I provide the failoverPolicy interface to support different policies.

}
/**
* Set the primary service url.
*
* @param primary
* @return
*/
AutoClusterFailoverBuilder primary(String primary);

/**
* Set the secondary service url.
*
* @param secondary
* @return
*/
AutoClusterFailoverBuilder secondary(List<String> secondary);

/**
* Set secondary choose policy. The default secondary choose policy is `ORDER`.
* @param policy
* @return
*/
AutoClusterFailoverBuilder failoverPolicy(FailoverPolicy policy);

/**
* Set secondary authentication.
*
* @param authentication
* @return
*/
AutoClusterFailoverBuilder secondaryAuthentication(Map<String, Authentication> authentication);

/**
* Set secondary tlsTrustCertsFilePath.
*
* @param tlsTrustCertsFilePath
* @return
*/
AutoClusterFailoverBuilder secondaryTlsTrustCertsFilePath(Map<String, String> tlsTrustCertsFilePath);

/**
* Set secondary tlsTrustStorePath.
*
* @param tlsTrustStorePath
* @return
*/
AutoClusterFailoverBuilder secondaryTlsTrustStorePath(Map<String, String> tlsTrustStorePath);

/**
* Set secondary tlsTrustStorePassword.
*
* @param tlsTrustStorePassword
* @return
*/
AutoClusterFailoverBuilder secondaryTlsTrustStorePassword(Map<String, String> tlsTrustStorePassword);
/**
* Set the switch failoverDelay. When one cluster failed longer than failoverDelay, it will trigger cluster switch.
*
* @param failoverDelay
* @param timeUnit
* @return
*/
AutoClusterFailoverBuilder failoverDelay(long failoverDelay, TimeUnit timeUnit);

/**
* Set the switchBackDelay. When switched to the secondary cluster, and after the primary cluster comes back,
* it will wait for switchBackDelay to switch back to the primary cluster.
*
* @param switchBackDelay
* @param timeUnit
* @return
*/
AutoClusterFailoverBuilder switchBackDelay(long switchBackDelay, TimeUnit timeUnit);

/**
* Set the checkInterval for probe.
*
* @param interval
* @param timeUnit
* @return
*/
AutoClusterFailoverBuilder checkInterval(long interval, TimeUnit timeUnit);

/**
* Build the ServiceUrlProvider instance.
*
* @return
*/
ServiceUrlProvider build();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* 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.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.apache.pulsar.common.classification.InterfaceAudience;
import org.apache.pulsar.common.classification.InterfaceStability;

/**
* {@link ControlledClusterFailoverBuilder} is used to configure and create instance of {@link ServiceUrlProvider}.
*
* @since 2.10.0
*/
@InterfaceAudience.Public
@InterfaceStability.Evolving
public interface ControlledClusterFailoverBuilder {
/**
* Set default service url.
*
* @param serviceUrl
* @return
*/
ControlledClusterFailoverBuilder defaultServiceUrl(String serviceUrl);

/**
* Set the service url provider. ServiceUrlProvider will fetch serviceUrl from urlProvider periodically.
*
* @param urlProvider
* @return
*/
ControlledClusterFailoverBuilder urlProvider(String urlProvider);

/**
* Set the service url provider header to authenticate provider service.
* @param header
* @return
*/
ControlledClusterFailoverBuilder urlProviderHeader(Map<String, String> header);

/**
* Set the probe check interval.
* @param interval
* @param timeUnit
* @return
*/
ControlledClusterFailoverBuilder checkInterval(long interval, TimeUnit timeUnit);

/**
* Build the ServiceUrlProvider instance.
*
* @return
* @throws IOException
*/
ServiceUrlProvider build() throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*/
@InterfaceAudience.Public
@InterfaceStability.Stable
public interface ServiceUrlProvider {
public interface ServiceUrlProvider extends AutoCloseable {

/**
* Initialize the service url provider with Pulsar client instance.
Expand All @@ -51,4 +51,12 @@ public interface ServiceUrlProvider {
*/
String getServiceUrl();

/**
* Close the resource that the provider allocated.
*
*/
@Override
default void close() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to make the interface inherit AutoCloseable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, we can remove the default empty implementation here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other serviceUrlProvider implementation also need to implement close() method, so I add the default implementation.

// do nothing
}
}
Loading