本文整理汇总了Java中org.apache.zookeeper.ZooKeeper.create方法的典型用法代码示例。如果您正苦于以下问题:Java ZooKeeper.create方法的具体用法?Java ZooKeeper.create怎么用?Java ZooKeeper.create使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.zookeeper.ZooKeeper
的用法示例。
在下文中一共展示了ZooKeeper.create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testNodeCreated
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Test
public void testNodeCreated() throws Exception {
QuorumUtil qu = new QuorumUtil(1);
qu.startAll();
EventsWatcher watcher = new EventsWatcher();
ZooKeeper zk1 = createClient(qu, 1, watcher);
ZooKeeper zk2 = createClient(qu, 2);
String path = "/test1-created";
zk1.exists(path, watcher);
qu.shutdown(1);
zk2.create(path, new byte[2], ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
qu.start(1);
watcher.waitForConnected(TIMEOUT * 1000L);
watcher.assertEvent(TIMEOUT, EventType.NodeCreated);
qu.shutdownAll();
}
示例2: testNodeChildrenChanged
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Test
public void testNodeChildrenChanged() throws Exception {
QuorumUtil qu = new QuorumUtil(1);
qu.startAll();
EventsWatcher watcher = new EventsWatcher();
ZooKeeper zk1 = createClient(qu, 1, watcher);
ZooKeeper zk2 = createClient(qu, 2);
String path = "/test-children-changed";
zk1.create(path, new byte[1], ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zk1.getChildren(path, watcher);
qu.shutdown(1);
zk2.create(path + "/children-1", new byte[2],
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
qu.start(1);
watcher.waitForConnected(TIMEOUT * 1000L);
watcher.assertEvent(TIMEOUT, EventType.NodeChildrenChanged);
qu.shutdownAll();
}
示例3: testSessionEstablishment
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
/**
* Tests a situation when client firstly connects to a read-only server and
* then connects to a majority server. Transition should be transparent for
* the user.
*/
@Test(timeout = 90000)
public void testSessionEstablishment() throws Exception {
qu.shutdown(2);
CountdownWatcher watcher = new CountdownWatcher();
ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT,
watcher, true);
watcher.waitForConnected(CONNECTION_TIMEOUT);
Assert.assertSame("should be in r/o mode", States.CONNECTEDREADONLY, zk
.getState());
long fakeId = zk.getSessionId();
watcher.reset();
qu.start(2);
Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(
"127.0.0.1:" + qu.getPeer(2).clientPort, CONNECTION_TIMEOUT));
watcher.waitForConnected(CONNECTION_TIMEOUT);
zk.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
Assert.assertFalse("fake session and real session have same id", zk
.getSessionId() == fakeId);
zk.close();
}
示例4: testObserverWithValidCredentials
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
/**
* Test to verify that Observer server is able to join quorum.
*/
@Test(timeout = 30000)
public void testObserverWithValidCredentials() throws Exception {
Map<String, String> authConfigs = new HashMap<String, String>();
authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "true");
authConfigs.put(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, "true");
authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, "true");
// Starting auth enabled 5-node cluster. 3-Participants and 2-Observers.
int totalServerCount = 5;
int observerCount = 2;
String connectStr = startQuorum(totalServerCount, observerCount,
authConfigs, totalServerCount);
CountdownWatcher watcher = new CountdownWatcher();
zk = new ZooKeeper(connectStr.toString(), ClientBase.CONNECTION_TIMEOUT,
watcher);
watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT);
zk.create("/myTestRoot", new byte[0], Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
}
示例5: createNodes
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
/**
* Create the znodes, this may fail if the lower 32 roll over, if so
* wait for the clients to be re-connected after the re-election
*/
private int createNodes(ZooKeeper zk, int start, int count) throws Exception {
LOG.info("Creating nodes {} thru {}", start, (start + count));
int j = 0;
try {
for (int i = start; i < start + count; i++) {
zk.create("/foo" + i, new byte[0], Ids.READ_ACL_UNSAFE,
CreateMode.EPHEMERAL);
j++;
}
} catch (ConnectionLossException e) {
// this is ok - the leader has dropped leadership
waitForClientsConnected();
}
return j;
}
示例6: testFail
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
/** bring up 5 quorum peers and then shut them down
* and then bring one of the nodes as server
*
* @throws Exception might be thrown here
*/
@Test
public void testFail() throws Exception {
QuorumBase qb = new QuorumBase();
qb.setUp();
System.out.println("Comment: the servers are at " + qb.hostPort);
ZooKeeper zk = qb.createClient();
zk.create("/test", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.close();
QuorumBase.shutdown(qb.s1);
QuorumBase.shutdown(qb.s2);
QuorumBase.shutdown(qb.s3);
QuorumBase.shutdown(qb.s4);
QuorumBase.shutdown(qb.s5);
String hp = qb.hostPort.split(",")[0];
ZooKeeperServer zks = new ZooKeeperServer(qb.s1.getTxnFactory().getSnapDir(),
qb.s1.getTxnFactory().getDataDir(), 3000);
final int PORT = Integer.parseInt(hp.split(":")[1]);
ServerCnxnFactory factory = ServerCnxnFactory.createFactory(PORT, -1);
factory.startup(zks);
System.out.println("Comment: starting factory");
Assert.assertTrue("waiting for server up",
ClientBase.waitForServerUp("127.0.0.1:" + PORT,
QuorumTest.CONNECTION_TIMEOUT));
factory.shutdown();
zks.shutdown();
Assert.assertTrue("waiting for server down",
ClientBase.waitForServerDown("127.0.0.1:" + PORT,
QuorumTest.CONNECTION_TIMEOUT));
System.out.println("Comment: shutting down standalone");
}
示例7: testNodeDeleted
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Test
public void testNodeDeleted() throws Exception {
QuorumUtil qu = new QuorumUtil(1);
qu.startAll();
EventsWatcher watcher = new EventsWatcher();
ZooKeeper zk1 = createClient(qu, 1, watcher);
ZooKeeper zk2 = createClient(qu, 2);
String path = "/test-deleted";
zk1.create(path, new byte[1], ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zk1.getData(path, watcher, null);
qu.shutdown(1);
zk2.delete(path, -1);
qu.start(1);
watcher.waitForConnected(TIMEOUT * 1000L);
watcher.assertEvent(TIMEOUT, EventType.NodeDeleted);
zk1.create(path, new byte[1], ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zk1.exists(path, watcher);
qu.shutdown(1);
zk2.delete(path, -1);
qu.start(1);
watcher.waitForConnected(TIMEOUT * 1000L);
watcher.assertEvent(TIMEOUT, EventType.NodeDeleted);
zk1.create(path, new byte[1], ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zk1.getChildren(path, watcher);
qu.shutdown(1);
zk2.delete(path, -1);
qu.start(1);
watcher.waitForConnected(TIMEOUT * 1000L);
watcher.assertEvent(TIMEOUT, EventType.NodeDeleted);
qu.shutdownAll();
}
示例8: testRelectionWithValidCredentials
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
/**
* Test to verify that server is able to reform quorum if the Leader goes
* down.
*/
@Test(timeout = 30000)
public void testRelectionWithValidCredentials() throws Exception {
Map<String, String> authConfigs = new HashMap<String, String>();
authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "true");
authConfigs.put(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, "true");
authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, "true");
String connectStr = startQuorum(3, authConfigs, 3, false);
CountdownWatcher watcher = new CountdownWatcher();
zk = new ZooKeeper(connectStr, ClientBase.CONNECTION_TIMEOUT, watcher);
watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT);
zk.create("/myTestRoot", new byte[0], Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT_SEQUENTIAL);
watcher.reset();
// Shutdown Leader to trigger re-election
QuorumPeer leaderQP = getLeaderQuorumPeer(mt);
LOG.info("Shutdown Leader sid:{} to trigger quorum leader-election",
leaderQP.getId());
shutdownQP(leaderQP);
// Wait for quorum formation
QuorumPeer newLeaderQP = waitForLeader();
assertNotNull("New leader must have been elected by now", newLeaderQP);
watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT);
zk.create("/myTestRoot", new byte[0], Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT_SEQUENTIAL);
}
示例9: testCreatePersistent
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
/**
* When local session is enabled, leader will allow persistent node
* to be create for unknown session
*/
@Test
public void testCreatePersistent() throws Exception {
qu.enableLocalSession(true);
qu.startAll();
QuorumPeer leader = qu.getLeaderQuorumPeer();
ZooKeeper zk = ClientBase.createZKClient(qu.getConnectString(leader));
CreateRequest createRequest = new CreateRequest("/success",
new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT.toFlag());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos);
createRequest.serialize(boa, "request");
ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray());
// Mimic sessionId generated by follower's local session tracker
long sid = qu.getFollowerQuorumPeers().get(0).getActiveServer()
.getServerId();
long locallSession = (sid << 56) + 1;
LOG.info("Local session Id: " + Long.toHexString(locallSession));
Request request = new Request(null, locallSession, 0, OpCode.create,
bb, new ArrayList<Id>());
// Submit request directly to leader
leader.getActiveServer().submitRequest(request);
// Make sure that previous request is finished
zk.create("/ok", new byte[0], Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
Stat stat = zk.exists("/success", null);
Assert.assertTrue("Request from local sesson failed", stat != null);
}
开发者ID:didichuxing2,项目名称:https-github.com-apache-zookeeper,代码行数:42,代码来源:LeaderSessionTrackerTest.java
示例10: testSessionEstablishment
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
/**
* Tests a situation when client firstly connects to a read-only server and
* then connects to a majority server. Transition should be transparent for
* the user.
*/
@Test(timeout = 90000)
public void testSessionEstablishment() throws Exception {
qu.shutdown(2);
CountdownWatcher watcher = new CountdownWatcher();
ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT,
watcher, true);
watcher.waitForConnected(CONNECTION_TIMEOUT);
Assert.assertSame("should be in r/o mode", States.CONNECTEDREADONLY, zk
.getState());
long fakeId = zk.getSessionId();
LOG.info("Connected as r/o mode with state {} and session id {}",
zk.getState(), fakeId);
watcher.reset();
qu.start(2);
Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(
"127.0.0.1:" + qu.getPeer(2).clientPort, CONNECTION_TIMEOUT));
LOG.info("Server 127.0.0.1:{} is up", qu.getPeer(2).clientPort);
// ZOOKEEPER-2722: wait until we can connect to a read-write server after the quorum
// is formed. Otherwise, it is possible that client first connects to a read-only server,
// then drops the connection because of shutting down of the read-only server caused
// by leader election / quorum forming between the read-only server and the newly started
// server. If we happen to execute the zk.create after the read-only server is shutdown and
// before the quorum is formed, we will get a ConnectLossException.
watcher.waitForSyncConnected(CONNECTION_TIMEOUT);
Assert.assertEquals("Should be in read-write mode", States.CONNECTED,
zk.getState());
LOG.info("Connected as rw mode with state {} and session id {}",
zk.getState(), zk.getSessionId());
zk.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
Assert.assertFalse("fake session and real session have same id", zk
.getSessionId() == fakeId);
zk.close();
}
示例11: createPath
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
public static void createPath(ZooKeeper zk, String path, CreateMode createMode, List<ACL> acl) throws Exception {
String[] list = path.split("/");
String zkPath = "";
for (String str : list) {
if (StringUtils.isNotEmpty(str)) {
zkPath = zkPath + "/" + str;
if (zk.exists(zkPath, false) == null) {
zk.create(zkPath, null, acl, createMode);
}
}
}
}
示例12: testAuthLearnerAgainstNullAuthServer
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
/**
* Test to verify that servers are able to form quorum.
* peer0 -> quorum.auth.enableSasl=true, quorum.auth.learnerRequireSasl=false, quorum.auth.serverRequireSasl=false
* peer1 -> quorum.auth.enableSasl=false, quorum.auth.learnerRequireSasl=false, quorum.auth.serverRequireSasl=false
*/
@Test(timeout = 30000)
public void testAuthLearnerAgainstNullAuthServer() throws Exception {
Map<String, String> authConfigs = new HashMap<String, String>();
authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "true");
String connectStr = startQuorum(2, authConfigs, 1, false);
CountdownWatcher watcher = new CountdownWatcher();
ZooKeeper zk = new ZooKeeper(connectStr, ClientBase.CONNECTION_TIMEOUT,
watcher);
watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT);
zk.create("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zk.close();
}
示例13: testMultiTransaction
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
/**
* Test write operations using multi request.
*/
@Test(timeout = 90000)
public void testMultiTransaction() throws Exception {
CountdownWatcher watcher = new CountdownWatcher();
ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT,
watcher, true);
watcher.waitForConnected(CONNECTION_TIMEOUT); // ensure zk got connected
final String data = "Data to be read in RO mode";
final String node1 = "/tnode1";
final String node2 = "/tnode2";
zk.create(node1, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
watcher.reset();
qu.shutdown(2);
watcher.waitForConnected(CONNECTION_TIMEOUT);
Assert.assertEquals("Should be in r-o mode", States.CONNECTEDREADONLY,
zk.getState());
// read operation during r/o mode
String remoteData = new String(zk.getData(node1, false, null));
Assert.assertEquals("Failed to read data in r-o mode", data, remoteData);
try {
Transaction transaction = zk.transaction();
transaction.setData(node1, "no way".getBytes(), -1);
transaction.create(node2, data.getBytes(),
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
transaction.commit();
Assert.fail("Write operation using multi-transaction"
+ " api has succeeded during RO mode");
} catch (NotReadOnlyException e) {
// ok
}
Assert.assertNull("Should have created the znode:" + node2,
zk.exists(node2, false));
}
示例14: utestPrep
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
private void utestPrep(int port)
throws IOException, InterruptedException, KeeperException
{
ZooKeeper zk =
new ZooKeeper("127.0.0.1:" + port, CONNECTION_TIMEOUT, this);
for (int i = 0; i < 10000; i++) {
zk.create("/" + i, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
zk.close();
}
示例15: ephemeralCreateMultiOpTest
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Test
public void ephemeralCreateMultiOpTest() throws KeeperException, InterruptedException, IOException {
final ZooKeeper zk = createClient();
String data = "test";
String path = "/ephemeralcreatemultiop";
zk.create(path, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
QuorumZooKeeperServer server = getConnectedServer(zk.getSessionId());
Assert.assertNotNull("unable to find server interlocutor", server);
UpgradeableSessionTracker sessionTracker = (UpgradeableSessionTracker)server.getSessionTracker();
Assert.assertFalse("session already global", sessionTracker.isGlobalSession(zk.getSessionId()));
List<OpResult> multi = null;
try {
multi = zk.multi(Arrays.asList(
Op.setData(path, data.getBytes(), 0),
Op.create(path + "/e", data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL),
Op.create(path + "/p", data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT),
Op.create(path + "/q", data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL)
));
} catch (KeeperException.SessionExpiredException e) {
// the scenario that inspired this unit test
Assert.fail("received session expired for a session promotion in a multi-op");
}
Assert.assertNotNull(multi);
Assert.assertEquals(4, multi.size());
Assert.assertEquals(data, new String(zk.getData(path + "/e", false, null)));
Assert.assertEquals(data, new String(zk.getData(path + "/p", false, null)));
Assert.assertEquals(data, new String(zk.getData(path + "/q", false, null)));
Assert.assertTrue("session not promoted", sessionTracker.isGlobalSession(zk.getSessionId()));
}
开发者ID:didichuxing2,项目名称:https-github.com-apache-zookeeper,代码行数:34,代码来源:MultiOpSessionUpgradeTest.java