Skip to content

Commit

Permalink
Add interface for Bookkeeper client to check metadata service is avai…
Browse files Browse the repository at this point in the history
…lable
  • Loading branch information
congbo committed May 6, 2024
1 parent b38b171 commit 131a89c
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1608,6 +1608,11 @@ public CompletableFuture<LedgerMetadata> getLedgerMetadata(long ledgerId) {
});
}

@Override
public CompletableFuture<Boolean> isDriverMetadataServiceAvailable() {
return metadataDriver.isMetadataServiceAvailable();
}

private final ClientContext clientCtx = new ClientContext() {
@Override
public ClientInternalConf getConf() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ static BookKeeperBuilder newBuilder(final ClientConfiguration clientConfiguratio
*/
CompletableFuture<LedgerMetadata> getLedgerMetadata(long ledgerId);

/**
* Return driver metadata service is available
*
* @return the metadata service is available.
*/
CompletableFuture<Boolean> isDriverMetadataServiceAvailable();
/**
* Close the client and release every resource.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,13 @@ interface SessionStateListener {
default CompletableFuture<Boolean> isHealthCheckEnabled() {
return FutureUtils.value(true);
}

/**
* Return driver metadata service is available
*
* @return the metadata service is available.
*/
default CompletableFuture<Boolean> isMetadataServiceAvailable() {
return FutureUtils.value(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.google.common.annotations.VisibleForTesting;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ScheduledExecutorService;
import lombok.extern.slf4j.Slf4j;
import org.apache.bookkeeper.conf.ClientConfiguration;
Expand Down Expand Up @@ -111,4 +112,8 @@ public void setSessionStateListener(SessionStateListener sessionStateListener) {
}
});
}

public CompletableFuture<Boolean> isMetadataServiceAvailable() {
return CompletableFuture.completedFuture(metadataServiceAvailable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
Expand All @@ -53,6 +54,7 @@
import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;
Expand All @@ -64,6 +66,8 @@
public class ZKMetadataDriverBase implements AutoCloseable {

protected static final String SCHEME = "zk";

protected volatile boolean metadataServiceAvailable;
private static final int ZK_CLIENT_WAIT_FOR_SHUTDOWN_TIMEOUT_MS = 5000;

public static String getZKServersFromServiceUri(URI uri) {
Expand Down Expand Up @@ -179,6 +183,7 @@ protected void initialize(AbstractConfiguration<?> conf,
// if an external zookeeper is added, use the zookeeper instance
this.zk = (ZooKeeper) (optionalCtx.get());
this.ownZKHandle = false;
this.metadataServiceAvailable = true;
} else {
final String metadataServiceUriStr;
try {
Expand Down Expand Up @@ -212,6 +217,10 @@ protected void initialize(AbstractConfiguration<?> conf,
.sessionTimeoutMs(conf.getZkTimeout())
.operationRetryPolicy(zkRetryPolicy)
.requestRateLimit(conf.getZkRequestRateLimit())
.watchers(Collections.singleton(watchedEvent -> {
log.info("Got ZK session watch event: {}", watchedEvent);
handleState(watchedEvent.getState());
}))
.statsLogger(statsLogger)
.build();

Expand Down Expand Up @@ -247,6 +256,19 @@ protected void initialize(AbstractConfiguration<?> conf,
acls);
}

private synchronized void handleState(Watcher.Event.KeeperState zkClientState) {
switch (zkClientState) {
case Expired:
case Disconnected:
this.metadataServiceAvailable = false;
break;

default:
this.metadataServiceAvailable = true;
break;
}
}

public LayoutManager getLayoutManager() {
return layoutManager;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
*
* 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.bookkeeper.client.api;

import org.apache.bookkeeper.conf.ClientConfiguration;
import org.apache.bookkeeper.test.BookKeeperClusterTestCase;
import org.awaitility.Awaitility;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
* Bookkeeper Client API driver metadata service available test.
*/
public class DriverMetadataServiceAvailableTest extends BookKeeperClusterTestCase {


public DriverMetadataServiceAvailableTest() {
super(3);
}

@Test
public void testDriverMetadataServiceAvailable()
throws Exception {

ClientConfiguration conf = new ClientConfiguration();
conf.setMetadataServiceUri(zkUtil.getMetadataServiceUri());
conf.setZkTimeout(3000);
try (BookKeeper bkc = BookKeeper.newBuilder(conf).build()) {
Awaitility.await().until(() -> bkc.isDriverMetadataServiceAvailable().get());
zkUtil.sleepCluster(5, TimeUnit.SECONDS, new CountDownLatch(1));
Awaitility.await().until(() -> !bkc.isDriverMetadataServiceAvailable().get());
Awaitility.await().until(() -> bkc.isDriverMetadataServiceAvailable().get());
}
}
}

0 comments on commit 131a89c

Please sign in to comment.