Hi all,

Currently we have a `ServiceUrlProvider` interface to configure when
constructing a PulsarClient in `ClientBuilder#serviceUrlProvider`.
>From the beginning, I thought the `getServiceUrl` method is called
each time the service URL is used, e.g. topic metadata lookup.
However, the `getServiceUrl` method is only called when constructing
the PulsarClient object. To update the PulsarClient's internal service
URL, `PulsarClient#updateServiceUrl` must be called. Therefore, if we
want to implement a `ServiceUrlProvider` that retrieves the latest
service URL from a database, I have to implement it like:

```java
class DataBaseServiceUrlProvider implements ServiceUrlProvider {

    private final ScheduledExecutorService executor =
Executors.newSingleThreadScheduledExecutor();

    @Override
    public void initialize(PulsarClient client) {
        executor.schedule(() -> {
            try {
                client.updateServiceUrl(readServiceUrlFromDB()/* a
fake method */);
            } catch (PulsarClientException e) {
                throw new RuntimeException(e);
            }
        }, 1, TimeUnit.SECONDS);
    }

    @Override
    public String getServiceUrl() {
        return "pulsar://localhost:6650";
    }

    @Override
    public void close() {
        executor.shutdown();
    }
}
```

The key point is, if we didn't call `client.updateServiceUrl` and only
modified the returned value of `getServiceUrl` periodically, the
internal service URL would never be updated.

Based on the provider above, the following two code snippets could be
nearly the same.

```java
var client = PulsarClient.builder().serviceUrlProvider(new
DataBaseServiceUrlProvider()).build();
/* ... */
client.close();
```

```java
var client = 
PulsarClient.builder().serviceUrl("pulsar://localhost:6650").build();
var provider = new DataBaseServiceUrlProvider();
provider.initialize(client);
/* ... */
provider.close();
client.close();
```

Obviously, the `ServiceUrlProvider` config is redundant.

PIP-121 implements the `AutoClusterFailover` as the service URL
provider. However, it also calls the following methods periodically:
- PulsarClientImpl#updateAuthentication
- PulsarClientImpl#updateTlsTrustCertsFilePath

It's unnatural and intuitive to say a service URL provider could
modify the internal states of `PulsarClient`, including:
- the service URL
- the authentication
- the TLS trust certificate file
- ...

BTW, the implementation of PIP-121 [1] is different from the design [2].

[1] https://github.com/apache/pulsar/pull/13316
[2] https://github.com/apache/pulsar/issues/13315

Thanks,
Yunze

Reply via email to