Java Code Examples for org.apache.zookeeper.CreateMode#EPHEMERAL
The following examples show how to use
org.apache.zookeeper.CreateMode#EPHEMERAL .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ZKJobMasterRegistrar.java From twister2 with Apache License 2.0 | 6 votes |
/** * create the znode for the job master */ private boolean createJobMasterZnode() { String jobMasterIPandPort = jobMasterIP + ":" + jobMasterPort; try { jobMasterNode = new PersistentNode( client, CreateMode.EPHEMERAL, false, jobMasterPath, jobMasterIPandPort.getBytes()); jobMasterNode.start(); jobMasterNode.waitForInitialCreate(10000, TimeUnit.MILLISECONDS); jobMasterPath = jobMasterNode.getActualPath(); LOG.info("An ephemeral znode is created for the Job Master: " + jobMasterPath); return true; } catch (Exception e) { LOG.log(Level.SEVERE, "Could not create znode for the Job Master: " + jobMasterIPandPort, e); return false; } }
Example 2
Source File: ZKUtil.java From helix with Apache License 2.0 | 6 votes |
public static void createOrReplace(RealmAwareZkClient client, String path, final ZNRecord record, final boolean persistent) { int retryCount = 0; while (retryCount < RETRYLIMIT) { try { if (client.exists(path)) { DataUpdater<Object> updater = new DataUpdater<Object>() { @Override public Object update(Object currentData) { return record; } }; client.updateDataSerialized(path, updater); } else { CreateMode mode = (persistent) ? CreateMode.PERSISTENT : CreateMode.EPHEMERAL; client.create(path, record, mode); } break; } catch (Exception e) { retryCount = retryCount + 1; logger.warn("Exception trying to createOrReplace " + path + " Exception:" + e.getMessage() + ". Will retry."); } } }
Example 3
Source File: ZKUtil.java From helix with Apache License 2.0 | 5 votes |
public static void createOrUpdate(RealmAwareZkClient client, String path, final ZNRecord record, final boolean persistent, final boolean mergeOnUpdate) { int retryCount = 0; while (retryCount < RETRYLIMIT) { try { if (client.exists(path)) { DataUpdater<ZNRecord> updater = new DataUpdater<ZNRecord>() { @Override public ZNRecord update(ZNRecord currentData) { if (currentData != null && mergeOnUpdate) { currentData.update(record); return currentData; } return record; } }; client.updateDataSerialized(path, updater); } else { CreateMode mode = (persistent) ? CreateMode.PERSISTENT : CreateMode.EPHEMERAL; client.create(path, record, mode); } break; } catch (Exception e) { retryCount = retryCount + 1; logger.warn("Exception trying to update " + path + " Exception:" + e.getMessage() + ". Will retry."); } } }
Example 4
Source File: RegistryImpl.java From Scribengin with GNU Affero General Public License v3.0 | 5 votes |
static CreateMode toCreateMode(NodeCreateMode mode) { if(mode == NodeCreateMode.PERSISTENT) return CreateMode.PERSISTENT ; else if(mode == NodeCreateMode.PERSISTENT_SEQUENTIAL) return CreateMode.PERSISTENT_SEQUENTIAL ; else if(mode == NodeCreateMode.EPHEMERAL) return CreateMode.EPHEMERAL ; else if(mode == NodeCreateMode.EPHEMERAL_SEQUENTIAL) return CreateMode.EPHEMERAL_SEQUENTIAL ; throw new RuntimeException("Mode " + mode + " is not supported") ; }
Example 5
Source File: AccessOption.java From helix with Apache License 2.0 | 5 votes |
/** * Helper method to get zookeeper create mode from options * @param options bitmask representing mode; least significant set flag is selected * @return zookeeper create mode */ public static CreateMode getMode(int options) { if ((options & PERSISTENT) > 0) { return CreateMode.PERSISTENT; } else if ((options & EPHEMERAL) > 0) { return CreateMode.EPHEMERAL; } else if ((options & PERSISTENT_SEQUENTIAL) > 0) { return CreateMode.PERSISTENT_SEQUENTIAL; } else if ((options & EPHEMERAL_SEQUENTIAL) > 0) { return CreateMode.EPHEMERAL_SEQUENTIAL; } return null; }
Example 6
Source File: TransactorNode.java From fluo with Apache License 2.0 | 5 votes |
/** * Creates a transactor node using given transactor id * * @param env Environment * @param tid Transactor ID used to create node */ public TransactorNode(Environment env, TransactorID tid) { this.env = env; this.tid = tid; node = new PersistentNode(env.getSharedResources().getCurator(), CreateMode.EPHEMERAL, false, getNodePath(), tid.toString().getBytes()); CuratorUtil.startAndWait(node, 10); status = TrStatus.OPEN; }
Example 7
Source File: PersistentNode.java From curator with Apache License 2.0 | 5 votes |
private CreateMode getCreateMode(boolean pathIsSet) { if ( pathIsSet ) { switch ( mode ) { default: { break; } case EPHEMERAL_SEQUENTIAL: { return CreateMode.EPHEMERAL; // protection case - node already set } case PERSISTENT_SEQUENTIAL: { return CreateMode.PERSISTENT; // protection case - node already set } case PERSISTENT_SEQUENTIAL_WITH_TTL: { return CreateMode.PERSISTENT_WITH_TTL; // protection case - node already set } } } return mode; }
Example 8
Source File: ServiceDiscoveryImpl.java From xian with Apache License 2.0 | 5 votes |
@VisibleForTesting protected void internalRegisterService(ServiceInstance<T> service) throws Exception { byte[] bytes = serializer.serialize(service); String path = pathForInstance(service.getName(), service.getId()); final int MAX_TRIES = 2; boolean isDone = false; for ( int i = 0; !isDone && (i < MAX_TRIES); ++i ) { try { CreateMode mode; switch (service.getServiceType()) { case DYNAMIC: mode = CreateMode.EPHEMERAL; break; case DYNAMIC_SEQUENTIAL: mode = CreateMode.EPHEMERAL_SEQUENTIAL; break; default: mode = CreateMode.PERSISTENT; break; } client.create().creatingParentContainersIfNeeded().withMode(mode).forPath(path, bytes); client.setData().forPath(pathForName(service.getName()), serviceDefinitionSerializer.serialize(service.getPayload())); isDone = true; } catch ( KeeperException.NodeExistsException e ) { client.delete().forPath(path); // must delete then re-create so that watchers fire } } }
Example 9
Source File: SimDistribStateManager.java From lucene-solr with Apache License 2.0 | 5 votes |
public void removeEphemeralChildren(String id) throws NoSuchElementException, BadVersionException, IOException { Set<String> kids = new HashSet<>(children.keySet()); for (String kid : kids) { Node n = children.get(kid); if (n == null) { continue; } if ((CreateMode.EPHEMERAL == n.mode || CreateMode.EPHEMERAL_SEQUENTIAL == n.mode) && id.equals(n.owner)) { removeChild(n.name, -1); } else { n.removeEphemeralChildren(id); } } }
Example 10
Source File: ServiceDiscoveryImpl.java From curator with Apache License 2.0 | 5 votes |
@VisibleForTesting protected void internalRegisterService(ServiceInstance<T> service) throws Exception { byte[] bytes = serializer.serialize(service); String path = pathForInstance(service.getName(), service.getId()); final int MAX_TRIES = 2; boolean isDone = false; for ( int i = 0; !isDone && (i < MAX_TRIES); ++i ) { try { CreateMode mode; switch (service.getServiceType()) { case DYNAMIC: mode = CreateMode.EPHEMERAL; break; case DYNAMIC_SEQUENTIAL: mode = CreateMode.EPHEMERAL_SEQUENTIAL; break; default: mode = CreateMode.PERSISTENT; break; } client.create().creatingParentContainersIfNeeded().withMode(mode).forPath(path, bytes); isDone = true; } catch ( KeeperException.NodeExistsException e ) { client.delete().forPath(path); // must delete then re-create so that watchers fire } } }
Example 11
Source File: GroupMember.java From curator with Apache License 2.0 | 5 votes |
/** * @param client client * @param membershipPath the path to use for membership * @param thisId ID of this group member. MUST be unique for the group * @param payload the payload to write in our member node */ public GroupMember(CuratorFramework client, String membershipPath, String thisId, byte[] payload) { this.membershipPath = membershipPath; this.thisId = Preconditions.checkNotNull(thisId, "thisId cannot be null"); cache = CuratorCache.bridgeBuilder(client, membershipPath).build(); pen = new PersistentNode(client, CreateMode.EPHEMERAL, false, ZKPaths.makePath(membershipPath, thisId), payload); }
Example 12
Source File: PersistentEphemeralNodeTest.java From curator-extensions with Apache License 2.0 | 4 votes |
@Test(expected = NullPointerException.class) public void testNullPath() throws Exception { new PersistentEphemeralNode(newCurator(), null, DATA, CreateMode.EPHEMERAL); }
Example 13
Source File: PersistentEphemeralNode.java From curator with Apache License 2.0 | 4 votes |
@Override protected CreateMode getCreateMode(boolean pathIsSet) { return CreateMode.EPHEMERAL; }
Example 14
Source File: PersistentEphemeralNodeTest.java From curator-extensions with Apache License 2.0 | 4 votes |
@Test(expected = NullPointerException.class) public void testNullData() throws Exception { new PersistentEphemeralNode(newCurator(), PATH, null, CreateMode.EPHEMERAL); }
Example 15
Source File: ZkClient.java From helix with Apache License 2.0 | 4 votes |
private boolean isSessionAwareOperation(String expectedSessionId, CreateMode mode) { return expectedSessionId != null && !expectedSessionId.isEmpty() && ( mode == CreateMode.EPHEMERAL || mode == CreateMode.EPHEMERAL_SEQUENTIAL); }
Example 16
Source File: PersistentEphemeralNode.java From xian with Apache License 2.0 | 4 votes |
@Override protected CreateMode getCreateMode(boolean pathIsSet) { return CreateMode.EPHEMERAL; }
Example 17
Source File: ZKClient.java From zkclient with Apache License 2.0 | 3 votes |
/** * 创建EPHEMERAL类型节点,该节点在被服务器删除后,重新连接会被自动创建。 * 而使用{@link ZKClient#create(String, Object, CreateMode)}创建的EPHEMERAL类型节点, * 会在会话失效后消失,并且在重新连接后并不会重新创建。 * @param path 路径 * @param data 数据 * @param acl 访问控制配置 * @param sequential * true:会创建{@link CreateMode#EPHEMERAL_SEQUENTIAL}类型的节点. * false:会创建{@link CreateMode#EPHEMERAL}类型的节点. * @return String * 创建的节点路径 */ public String createEphemerale(final String path, Object data, final List<ACL> acl,Boolean sequential){ CreateMode createMode = CreateMode.EPHEMERAL; if(sequential){ createMode = createMode.EPHEMERAL_SEQUENTIAL; } String retPath = create(path, data, acl, createMode); //将节点放入ephemeralNodeMap,用于重连后的自动创建 ephemeralNodeMap.put(path, new ZKNode(path,data,createMode)); return retPath; }
Example 18
Source File: ZKClient.java From zkclient with Apache License 2.0 | 3 votes |
/** * 创建EPHEMERAL类型节点,该节点在被服务器删除后,重新连接会被自动创建。 * 而使用{@link ZKClient#create(String, Object, CreateMode)}创建的EPHEMERAL类型节点, * 会话失效后临时节点会被删除,并且在重新连接后并不会重新创建。 * @param path 路径 * @param data 数据 * @param sequential * true:会创建{@link CreateMode#EPHEMERAL_SEQUENTIAL}类型的节点. * false:会创建{@link CreateMode#EPHEMERAL}类型的节点. * @return String * 创建的节点路径 */ public String createEphemerale(final String path, Object data,Boolean sequential){ CreateMode createMode = CreateMode.EPHEMERAL; if(sequential){ createMode = createMode.EPHEMERAL_SEQUENTIAL; } String retPath = create(path, data, createMode); //将节点放入ephemeralNodeMap,用于重连后的自动创建 ephemeralNodeMap.put(path, new ZKNode(path,data,createMode)); return retPath; }
Example 19
Source File: PersistentNodeTest.java From x-pipe with Apache License 2.0 | 3 votes |
@Test public void testNode() throws IOException{ String path = "/" + getTestName(); PersistentNode persistentNode = new PersistentNode(client, CreateMode.EPHEMERAL, false, path, "123456".getBytes()); persistentNode.start(); waitForAnyKeyToExit(); persistentNode.close(); }
Example 20
Source File: ZKUtils.java From twister2 with Apache License 2.0 | 2 votes |
/** * create a PersistentNode object in the given path * it is ephemeral and persistent * it will be deleted after the worker leaves or fails * it will be persistent for occasional network problems */ public static PersistentNode createPersistentEphemeralZnode(String path, byte[] payload) { return new PersistentNode(client, CreateMode.EPHEMERAL, true, path, payload); }