-
Notifications
You must be signed in to change notification settings - Fork 903
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
[client] new API to check if Bookkeeper client is connected to metadata service #4342
Changes from 9 commits
131a89c
b4a7b44
315b4af
5f3aaa2
e5a2438
c7c905f
9c8ccf6
91e400a
7d9e9a6
b60341e
4db75f8
ef2419a
36ee703
0f39efd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -64,6 +66,9 @@ | |
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) { | ||
|
@@ -179,6 +184,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 { | ||
|
@@ -212,6 +218,12 @@ protected void initialize(AbstractConfiguration<?> conf, | |
.sessionTimeoutMs(conf.getZkTimeout()) | ||
.operationRetryPolicy(zkRetryPolicy) | ||
.requestRateLimit(conf.getZkRequestRateLimit()) | ||
.watchers(Collections.singleton(watchedEvent -> { | ||
if (log.isDebugEnabled()) { | ||
log.debug("Got ZK session watch event: {}", watchedEvent); | ||
} | ||
handleState(watchedEvent.getState()); | ||
})) | ||
.statsLogger(statsLogger) | ||
.build(); | ||
|
||
|
@@ -247,6 +259,18 @@ protected void initialize(AbstractConfiguration<?> conf, | |
acls); | ||
} | ||
|
||
private void handleState(Watcher.Event.KeeperState zkClientState) { | ||
switch (zkClientState) { | ||
case Expired: | ||
case Disconnected: | ||
this.metadataServiceAvailable = false; | ||
break; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: remove line break There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @congbobo184 looks like line break is not removed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why remove this? change metadataServiceAvailable twice? @shoothzj There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @congbobo184 line 256 blank line? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry, I thought it was to delete break |
||
default: | ||
this.metadataServiceAvailable = true; | ||
} | ||
} | ||
|
||
public LayoutManager getLayoutManager() { | ||
return layoutManager; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* | ||
* 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 java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import org.apache.bookkeeper.conf.ClientConfiguration; | ||
import org.apache.bookkeeper.test.BookKeeperClusterTestCase; | ||
import org.awaitility.Awaitility; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Bookkeeper Client API driver metadata service available test. | ||
*/ | ||
public class DriverMetadataServiceAvailableTest extends BookKeeperClusterTestCase { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: we only need one blank space here. |
||
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()); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nits: add blank line after