本文整理汇总了Java中org.apache.zookeeper.KeeperException.ConnectionLossException方法的典型用法代码示例。如果您正苦于以下问题:Java KeeperException.ConnectionLossException方法的具体用法?Java KeeperException.ConnectionLossException怎么用?Java KeeperException.ConnectionLossException使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.zookeeper.KeeperException
的用法示例。
在下文中一共展示了KeeperException.ConnectionLossException方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setData
import org.apache.zookeeper.KeeperException; //导入方法依赖的package包/类
@Override
public Stat setData(String path, byte[] data, int version)
throws KeeperException, InterruptedException {
int count = 0;
do {
try {
return super.setData(path, data, version);
} catch (KeeperException.ConnectionLossException e) {
LoggerFactory.getLogger().warn(
"ZooKeeper connection lost. Trying to reconnect.");
Stat s = exists(path, false);
if (s != null) {
if (getData(path, false, s) == data) {
return s;
}
} else {
return null;
}
}
} while (!closed && (limit == -1 || count++ < limit));
return null;
}
示例2: testNonExistingOpCode
import org.apache.zookeeper.KeeperException; //导入方法依赖的package包/类
/**
* We create a perfectly valid 'exists' request, except that the opcode is wrong.
* @return
* @throws Exception
*/
@Test
public void testNonExistingOpCode() throws Exception {
TestableZooKeeper zk = createClient();
final String path = "/m1";
RequestHeader h = new RequestHeader();
h.setType(888); // This code does not exists
ExistsRequest request = new ExistsRequest();
request.setPath(path);
request.setWatch(false);
ExistsResponse response = new ExistsResponse();
ReplyHeader r = zk.submitRequest(h, request, response, null);
Assert.assertEquals(r.getErr(), Code.UNIMPLEMENTED.intValue());
try {
zk.exists("/m1", false);
fail("The connection should have been closed");
} catch (KeeperException.ConnectionLossException expected) {
}
}
示例3: setACL
import org.apache.zookeeper.KeeperException; //导入方法依赖的package包/类
@Override
public Stat setACL(String path, List<ACL> acl, int version)
throws KeeperException, InterruptedException {
int count = 0;
do {
try {
return super.setACL(path, acl, version);
} catch (KeeperException.ConnectionLossException e) {
LoggerFactory.getLogger().warn(
"ZooKeeper connection lost. Trying to reconnect.");
Stat s = exists(path, false);
if (s != null) {
if (getACL(path, s).equals(acl)) {
return s;
}
} else {
return null;
}
}
} while (!closed && (limit == -1 || count++ < limit));
return null;
}
示例4: getZookeeperClient
import org.apache.zookeeper.KeeperException; //导入方法依赖的package包/类
public CuratorFramework getZookeeperClient() throws KeeperException.ConnectionLossException {
if (zkClient.getZookeeperClient().isConnected()) {
return this.zkClient;
} else {
throw new KeeperException.ConnectionLossException();
}
}
示例5: getChildren
import org.apache.zookeeper.KeeperException; //导入方法依赖的package包/类
@Override
public List<String> getChildren(String path, boolean watch)
throws KeeperException, InterruptedException {
int count = 0;
do {
try {
return super.getChildren(path, watch ? watcher : null);
} catch (KeeperException.ConnectionLossException e) {
LoggerFactory.getLogger().warn(
"ZooKeeper connection lost. Trying to reconnect.");
}
} while (!closed && (limit == -1 || count++ < limit));
return new ArrayList<String>();
}
示例6: getACL
import org.apache.zookeeper.KeeperException; //导入方法依赖的package包/类
@Override
public List<ACL> getACL(String path, Stat stat) throws KeeperException,
InterruptedException {
int count = 0;
do {
try {
return super.getACL(path, stat);
} catch (KeeperException.ConnectionLossException e) {
LoggerFactory.getLogger().warn(
"ZooKeeper connection lost. Trying to reconnect.");
}
} while (!closed && (limit == -1 || count++ < limit));
return null;
}
示例7: exists
import org.apache.zookeeper.KeeperException; //导入方法依赖的package包/类
@Override
public Stat exists(String path, Watcher watcher) throws KeeperException,
InterruptedException {
int count = 0;
do {
try {
return super.exists(path, watcher);
} catch (KeeperException.ConnectionLossException e) {
LoggerFactory.getLogger().warn(
"ZooKeeper connection lost. Trying to reconnect.");
}
} while (!closed && (limit == -1 || count++ < limit));
return null;
}
示例8: getData
import org.apache.zookeeper.KeeperException; //导入方法依赖的package包/类
@Override
public byte[] getData(String path, boolean watch, Stat stat)
throws KeeperException, InterruptedException {
int count = 0;
do {
try {
return super.getData(path, watch ? watcher : null, stat);
} catch (KeeperException.ConnectionLossException e) {
LoggerFactory.getLogger().warn(
"ZooKeeper connection lost. Trying to reconnect.");
}
} while (!closed && (limit == -1 || count++ < limit));
return null;
}
示例9: getChildren
import org.apache.zookeeper.KeeperException; //导入方法依赖的package包/类
@Override
public List<String> getChildren(String path, Watcher watcher)
throws KeeperException, InterruptedException {
int count = 0;
do {
try {
return super.getChildren(path, watcher);
} catch (KeeperException.ConnectionLossException e) {
LoggerFactory.getLogger().warn(
"ZooKeeper connection lost. Trying to reconnect.");
}
} while (!closed && (limit == -1 || count++ < limit));
return new ArrayList<String>();
}
示例10: getData
import org.apache.zookeeper.KeeperException; //导入方法依赖的package包/类
@Override
public byte[] getData(String path, Watcher watcher, Stat stat)
throws KeeperException, InterruptedException {
int count = 0;
do {
try {
return super.getData(path, watcher, stat);
} catch (KeeperException.ConnectionLossException e) {
LoggerFactory.getLogger().warn(
"ZooKeeper connection lost. Trying to reconnect.");
}
} while (!closed && (limit == -1 || count++ < limit));
return null;
}
示例11: testNormalOperation
import org.apache.zookeeper.KeeperException; //导入方法依赖的package包/类
public static void testNormalOperation(ZooKeeper writer, ZooKeeper reader)
throws KeeperException, InterruptedException {
boolean testReaderNodeExists = false;
boolean testWriterNodeExists = false;
for (int j = 0; j < 30; j++) {
try {
if (!testWriterNodeExists) {
createZNode(writer, "/test", "test");
testWriterNodeExists = true;
}
if (!testReaderNodeExists) {
createZNode(reader, "/dummy", "dummy");
testReaderNodeExists = true;
}
String data = "test" + j;
writer.setData("/test", data.getBytes(), -1);
// Use setData instead of sync API to force a view update.
// Check ZOOKEEPER-2137 for details.
reader.setData("/dummy", "dummy".getBytes(), -1);
byte[] res = reader.getData("/test", null, new Stat());
Assert.assertEquals(data, new String(res));
break;
} catch (KeeperException.ConnectionLossException e) {
if (j < 29) {
Thread.sleep(1000);
} else {
// test fails if we still can't connect to the quorum after
// 30 seconds.
Assert.fail("client could not connect to reestablished quorum: giving up after 30+ seconds.");
}
}
}
}
示例12: testSessionMove
import org.apache.zookeeper.KeeperException; //导入方法依赖的package包/类
/**
* Make sure that we cannot have two connections with the same
* session id.
*
* @throws IOException
* @throws InterruptedException
* @throws KeeperException
*/
@Test
public void testSessionMove() throws Exception {
String hostPorts[] = HOSTPORT.split(",");
DisconnectableZooKeeper zk = new DisconnectableZooKeeper(hostPorts[0],
CONNECTION_TIMEOUT, new MyWatcher("0"));
zk.create("/sessionMoveTest", new byte[0], Ids.OPEN_ACL_UNSAFE,
CreateMode.EPHEMERAL);
// we want to loop through the list twice
for(int i = 0; i < hostPorts.length*2; i++) {
zk.dontReconnect();
// This should stomp the zk handle
DisconnectableZooKeeper zknew = new DisconnectableZooKeeper(
hostPorts[(i+1)%hostPorts.length],
CONNECTION_TIMEOUT,
new MyWatcher(Integer.toString(i+1)),
zk.getSessionId(),
zk.getSessionPasswd());
final int result[] = new int[1];
result[0] = Integer.MAX_VALUE;
zknew.sync("/", new AsyncCallback.VoidCallback() {
public void processResult(int rc, String path, Object ctx) {
synchronized(result) { result[0] = rc; result.notify(); }
}
}, null);
synchronized(result) {
if(result[0] == Integer.MAX_VALUE) {
result.wait(5000);
}
}
LOG.info(hostPorts[(i+1)%hostPorts.length] + " Sync returned " + result[0]);
Assert.assertTrue(result[0] == KeeperException.Code.OK.intValue());
zknew.setData("/", new byte[1], -1);
try {
zk.setData("/", new byte[1], -1);
Assert.fail("Should have lost the connection");
} catch(KeeperException.ConnectionLossException e) {
LOG.info("Got connection loss exception as expected");
}
//zk.close();
zk = zknew;
}
zk.close();
}
示例13: testSessionMove
import org.apache.zookeeper.KeeperException; //导入方法依赖的package包/类
/**
* Connect to two different servers with two different handles using the same session and
* make sure we cannot do any changes.
*/
@Test
@Ignore
public void testSessionMove() throws Exception {
String hps[] = qb.hostPort.split(",");
DiscoWatcher oldWatcher = new DiscoWatcher();
DisconnectableZooKeeper zk = new DisconnectableZooKeeper(hps[0],
ClientBase.CONNECTION_TIMEOUT, oldWatcher);
zk.create("/t1", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
zk.dontReconnect();
// This should stomp the zk handle
DiscoWatcher watcher = new DiscoWatcher();
DisconnectableZooKeeper zknew = new DisconnectableZooKeeper(hps[1],
ClientBase.CONNECTION_TIMEOUT, watcher, zk.getSessionId(),
zk.getSessionPasswd());
zknew.create("/t2", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
try {
zk.create("/t3", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
Assert.fail("Should have lost the connection");
} catch(KeeperException.ConnectionLossException e) {
// wait up to 30 seconds for the disco to be delivered
for (int i = 0; i < 30; i++) {
if (oldWatcher.zkDisco) {
break;
}
Thread.sleep(1000);
}
Assert.assertTrue(oldWatcher.zkDisco);
}
ArrayList<ZooKeeper> toClose = new ArrayList<ZooKeeper>();
toClose.add(zknew);
// Let's just make sure it can still move
for(int i = 0; i < 10; i++) {
zknew.dontReconnect();
zknew = new DisconnectableZooKeeper(hps[1],
ClientBase.CONNECTION_TIMEOUT, new DiscoWatcher(),
zk.getSessionId(), zk.getSessionPasswd());
toClose.add(zknew);
zknew.create("/t-"+i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
}
for (ZooKeeper z: toClose) {
z.close();
}
zk.close();
}
示例14: testConnectionEvents
import org.apache.zookeeper.KeeperException; //导入方法依赖的package包/类
/**
* Ensures that upon connection to a read-only server client receives
* ConnectedReadOnly state notification.
*/
@Test(timeout = 90000)
public void testConnectionEvents() throws Exception {
final List<KeeperState> states = new ArrayList<KeeperState>();
ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT,
new Watcher() {
public void process(WatchedEvent event) {
states.add(event.getState());
}
}, true);
boolean success = false;
for (int i = 0; i < 30; i++) {
try {
zk.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
success=true;
break;
} catch(KeeperException.ConnectionLossException e) {
Thread.sleep(1000);
}
}
Assert.assertTrue("Did not succeed in connecting in 30s", success);
// kill peer and wait no more than 5 seconds for read-only server
// to be started (which should take one tickTime (2 seconds))
qu.shutdown(2);
long start = System.currentTimeMillis();
while (!(zk.getState() == States.CONNECTEDREADONLY)) {
Thread.sleep(200);
// FIXME this was originally 5 seconds, but realistically, on random/slow/virt hosts, there is no way to guarantee this
Assert.assertTrue("Can't connect to the server", System
.currentTimeMillis()
- start < 30000);
}
// At this point states list should contain, in the given order,
// SyncConnected, Disconnected, and ConnectedReadOnly states
Assert.assertTrue("ConnectedReadOnly event wasn't received", states
.get(2) == KeeperState.ConnectedReadOnly);
zk.close();
}