本文整理汇总了Java中org.apache.zookeeper.CreateMode类的典型用法代码示例。如果您正苦于以下问题:Java CreateMode类的具体用法?Java CreateMode怎么用?Java CreateMode使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CreateMode类属于org.apache.zookeeper包,在下文中一共展示了CreateMode类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testCreate
import org.apache.zookeeper.CreateMode; //导入依赖的package包/类
@Test
public void testCreate()
throws IOException, KeeperException, InterruptedException {
Stat stat = new Stat();
zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL, stat, 100);
Assert.assertEquals(0, stat.getEphemeralOwner());
final AtomicLong fakeElapsed = new AtomicLong(0);
ContainerManager containerManager = newContainerManager(fakeElapsed);
containerManager.checkContainers();
Assert.assertNotNull("Ttl node should not have been deleted yet", zk.exists("/foo", false));
fakeElapsed.set(1000);
containerManager.checkContainers();
Assert.assertNull("Ttl node should have been deleted", zk.exists("/foo", false));
}
示例2: testChRootCreateDelete
import org.apache.zookeeper.CreateMode; //导入依赖的package包/类
@Test
public void testChRootCreateDelete() throws Exception {
// creating the subtree for chRoot clients.
String chRoot = createNameSpace();
// Creating child using chRoot client.
zk_chroot = createClient(this.hostPort + chRoot);
Op createChild = Op.create("/myid", new byte[0],
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
multi(zk_chroot, Arrays.asList(createChild));
Assert.assertNotNull("zNode is not created under chroot:" + chRoot, zk
.exists(chRoot + "/myid", false));
Assert.assertNotNull("zNode is not created under chroot:" + chRoot,
zk_chroot.exists("/myid", false));
Assert.assertNull("zNode is created directly under '/', ignored configured chroot",
zk.exists("/myid", false));
// Deleting child using chRoot client.
Op deleteChild = Op.delete("/myid", 0);
multi(zk_chroot, Arrays.asList(deleteChild));
Assert.assertNull("zNode exists under chroot:" + chRoot, zk.exists(
chRoot + "/myid", false));
Assert.assertNull("zNode exists under chroot:" + chRoot, zk_chroot
.exists("/myid", false));
}
示例3: testTtls
import org.apache.zookeeper.CreateMode; //导入依赖的package包/类
@Test
public void testTtls() {
long ttls[] = {100, 1, EphemeralType.MAX_TTL};
for (long ttl : ttls) {
long ephemeralOwner = EphemeralType.ttlToEphemeralOwner(ttl);
Assert.assertEquals(EphemeralType.TTL, EphemeralType.get(ephemeralOwner));
Assert.assertEquals(ttl, EphemeralType.getTTL(ephemeralOwner));
}
EphemeralType.validateTTL(CreateMode.PERSISTENT_WITH_TTL, 100);
EphemeralType.validateTTL(CreateMode.PERSISTENT_SEQUENTIAL_WITH_TTL, 100);
try {
EphemeralType.validateTTL(CreateMode.EPHEMERAL, 100);
Assert.fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException dummy) {
// expected
}
}
示例4: testValidCredentials
import org.apache.zookeeper.CreateMode; //导入依赖的package包/类
/**
* Test to verify that server is able to start with valid credentials
*/
@Test(timeout = 120000)
public void testValidCredentials() throws Exception {
String serverPrincipal = hostServerPrincipal.substring(0, hostServerPrincipal.lastIndexOf("@"));
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");
authConfigs.put(QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL, serverPrincipal);
String connectStr = startQuorum(3, authConfigs, 3, true);
CountdownWatcher watcher = new CountdownWatcher();
ZooKeeper zk = new ZooKeeper(connectStr, ClientBase.CONNECTION_TIMEOUT, watcher);
watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT);
for (int i = 0; i < 10; i++) {
zk.create("/" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
zk.close();
}
示例5: addPartner
import org.apache.zookeeper.CreateMode; //导入依赖的package包/类
public void addPartner(String key, Partner partner) {
String partnerPath = ZKPaths.makePath(partnerStorePath, key);
RetryRunner.create().onFinalError(e -> {
LOGGER.error("addPartner.error", e);
ReporterHolder.incException(e);
throw new ServiceErrorException(ErrorCode.SYSTEM_ERROR);
}).run((Callable<Void>) () -> {
if (client.checkExists().creatingParentsIfNeeded().forPath(partnerPath) != null) {
client.setData()
.forPath(partnerPath, JSONObject.toJSONBytes(partner));
} else {
client.create()
.creatingParentsIfNeeded()
.withMode(CreateMode.PERSISTENT)
.forPath(partnerPath, JSONObject.toJSONBytes(partner));
}
return null;
});
}
示例6: createScheduleTaskItem
import org.apache.zookeeper.CreateMode; //导入依赖的package包/类
/**
* 创建任务项,注意其中的 CurrentSever和RequestServer不会起作用
* @param taskItems
* @throws Exception
*/
public void createScheduleTaskItem(ScheduleTaskItem[] taskItems) throws Exception {
for (ScheduleTaskItem taskItem : taskItems){
String zkPath = this.PATH_BaseTaskType + "/" + taskItem.getBaseTaskType() + "/" + taskItem.getTaskType() +"/" + this.PATH_TaskItem;
if(this.getZooKeeper().exists(zkPath, false)== null){
ZKTools.createPath(this.getZooKeeper(), zkPath, CreateMode.PERSISTENT, this.zkManager.getAcl());
}
String zkTaskItemPath = zkPath + "/" + taskItem.getTaskItem();
this.getZooKeeper().create(zkTaskItemPath,null, this.zkManager.getAcl(),CreateMode.PERSISTENT);
this.getZooKeeper().create(zkTaskItemPath + "/cur_server",null, this.zkManager.getAcl(),CreateMode.PERSISTENT);
this.getZooKeeper().create(zkTaskItemPath + "/req_server",null, this.zkManager.getAcl(),CreateMode.PERSISTENT);
this.getZooKeeper().create(zkTaskItemPath + "/sts",taskItem.getSts().toString().getBytes(), this.zkManager.getAcl(),CreateMode.PERSISTENT);
this.getZooKeeper().create(zkTaskItemPath + "/parameter",taskItem.getDealParameter().getBytes(), this.zkManager.getAcl(),CreateMode.PERSISTENT);
this.getZooKeeper().create(zkTaskItemPath + "/deal_desc",taskItem.getDealDesc().getBytes(), this.zkManager.getAcl(),CreateMode.PERSISTENT);
}
}
示例7: testSessionEstablishment
import org.apache.zookeeper.CreateMode; //导入依赖的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();
}
示例8: writeBreadCrumbNode
import org.apache.zookeeper.CreateMode; //导入依赖的package包/类
/**
* Write the "ActiveBreadCrumb" node, indicating that this node may need
* to be fenced on failover.
* @param oldBreadcrumbStat
*/
private void writeBreadCrumbNode(Stat oldBreadcrumbStat)
throws KeeperException, InterruptedException {
Preconditions.checkState(appData != null, "no appdata");
LOG.info("Writing znode " + zkBreadCrumbPath +
" to indicate that the local node is the most recent active...");
if (oldBreadcrumbStat == null) {
// No previous active, just create the node
createWithRetries(zkBreadCrumbPath, appData, zkAcl,
CreateMode.PERSISTENT);
} else {
// There was a previous active, update the node
setDataWithRetries(zkBreadCrumbPath, appData, oldBreadcrumbStat.getVersion());
}
}
示例9: create
import org.apache.zookeeper.CreateMode; //导入依赖的package包/类
/**
* Create a node.
*
* @param path
* @param data
* @param mode
* @return create node's path
* @throws ZkInterruptedException if operation was interrupted, or a required reconnection got interrupted
* @throws IllegalArgumentException if called from anything except the ZooKeeper event thread
* @throws ZkException if any ZooKeeper exception occurred
* @throws RuntimeException if any other exception occurs
*/
public String create(final String path, Object data, final CreateMode mode) throws ZkInterruptedException,
IllegalArgumentException, ZkException,
RuntimeException {
if (path == null) {
throw new NullPointerException("path must not be null.");
}
final byte[] bytes = data == null ? null : serialize(data);
return retryUntilConnected(new Callable<String>() {
@Override
public String call() throws Exception {
return _connection.create(path, bytes, mode);
}
});
}
示例10: set_data
import org.apache.zookeeper.CreateMode; //导入依赖的package包/类
@Override
public void set_data(String path,byte[] data) throws Exception
{
if (data.length > Utils.SIZE_1_K * 800)
{
throw new Exception("Writing 800k+ data into ZK is not allowed!, data size is " + data.length);
}
if (zkobj.exists(zk,path,false))
{
zkobj.setData(zk,path,data);
}
else
{
zkobj.mkdirs(zk, PathUtils.parent_path(path));
zkobj.createNode(zk,path,data,CreateMode.PERSISTENT);
}
if (zkCache != null)
zkCache.put(path,data);
}
示例11: testCreateNodeResultRetryNoNode
import org.apache.zookeeper.CreateMode; //导入依赖的package包/类
/**
* verify that if create znode results in nodeexists and that znode is deleted
* before exists() watch is set then the return of the exists() method results
* in attempt to re-create the znode and become active
*/
@Test
public void testCreateNodeResultRetryNoNode() {
elector.joinElection(data);
elector.processResult(Code.CONNECTIONLOSS.intValue(), ZK_LOCK_NAME, mockZK,
ZK_LOCK_NAME);
elector.processResult(Code.CONNECTIONLOSS.intValue(), ZK_LOCK_NAME, mockZK,
ZK_LOCK_NAME);
elector.processResult(Code.NODEEXISTS.intValue(), ZK_LOCK_NAME, mockZK,
ZK_LOCK_NAME);
verifyExistCall(1);
elector.processResult(Code.NONODE.intValue(), ZK_LOCK_NAME, mockZK,
(Stat) null);
Mockito.verify(mockApp, Mockito.times(1)).enterNeutralMode();
Mockito.verify(mockZK, Mockito.times(4)).create(ZK_LOCK_NAME, data,
Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL, elector, mockZK);
}
示例12: createNodes
import org.apache.zookeeper.CreateMode; //导入依赖的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 " + start + " thru " + (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;
}
示例13: ZkAbstractStore
import org.apache.zookeeper.CreateMode; //导入依赖的package包/类
public ZkAbstractStore(CuratorFramework framework, PStoreConfig<V> config)
throws IOException {
this.parent = "/" + config.getName();
this.prefix = parent + "/";
this.framework = framework;
this.config = config;
// make sure the parent node exists.
try {
if (framework.checkExists().forPath(parent) == null) {
framework.create().withMode(CreateMode.PERSISTENT).forPath(parent);
}
this.childrenCache = new PathChildrenCache(framework, parent, true);
this.childrenCache.start(StartMode.BUILD_INITIAL_CACHE);
} catch (Exception e) {
throw new RuntimeException("Failure while accessing Zookeeper for PStore: " + e.getMessage(), e);
}
}
示例14: setUp
import org.apache.zookeeper.CreateMode; //导入依赖的package包/类
@Override
public void setUp() throws Exception {
String hp = hostPort;
hostPort = hostPort + "/chrootclienttest";
System.out.println(hostPort);
super.setUp();
LOG.info("STARTING " + getTestName());
ZooKeeper zk = createClient(hp);
try {
zk.create("/chrootclienttest", null, Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
} finally {
zk.close();
}
}
示例15: create
import org.apache.zookeeper.CreateMode; //导入依赖的package包/类
/**
* <p>
* NONSEQUENTIAL create is idempotent operation.
* Retry before throwing exceptions.
* But this function will not throw the NodeExist exception back to the
* application.
* </p>
* <p>
* But SEQUENTIAL is NOT idempotent operation. It is necessary to add
* identifier to the path to verify, whether the previous one is successful
* or not.
* </p>
*
* @return Path
*/
public String create(String path, byte[] data, List<ACL> acl,
CreateMode createMode)
throws KeeperException, InterruptedException {
TraceScope traceScope = null;
try {
traceScope = Trace.startSpan("RecoverableZookeeper.create");
byte[] newData = appendMetaData(data);
switch (createMode) {
case EPHEMERAL:
case PERSISTENT:
return createNonSequential(path, newData, acl, createMode);
case EPHEMERAL_SEQUENTIAL:
case PERSISTENT_SEQUENTIAL:
return createSequential(path, newData, acl, createMode);
default:
throw new IllegalArgumentException("Unrecognized CreateMode: " +
createMode);
}
} finally {
if (traceScope != null) traceScope.close();
}
}