Java Code Examples for org.apache.zookeeper.CreateMode#PERSISTENT

The following examples show how to use org.apache.zookeeper.CreateMode#PERSISTENT . 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: PersistentNode.java    From xian with Apache License 2.0 6 votes vote down vote up
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
        }
        }
    }
    return mode;
}
 
Example 2
Source File: ZKUtil.java    From helix with Apache License 2.0 6 votes vote down vote up
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: TestPersistentNode.java    From curator with Apache License 2.0 6 votes vote down vote up
@Test
public void testQuickCloseNodeExists() throws Exception
{
    Timing timing = new Timing();
    PersistentNode pen = null;
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    try
    {
        client.start();
        client.create().creatingParentsIfNeeded().forPath("/test/one/two");

        pen = new PersistentNode(client, CreateMode.PERSISTENT, false, "/test/one/two", new byte[0]);
        pen.start();
        pen.close();
        timing.sleepABit();
        Assert.assertNull(client.checkExists().forPath("/test/one/two"));
    }
    finally
    {
        CloseableUtils.closeQuietly(pen);
        CloseableUtils.closeQuietly(client);
    }
}
 
Example 4
Source File: SnapshotDistribStateManager.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Populate this instance from a previously generated snapshot.
 * @param snapshot previous snapshot created using this class.
 * @param config optional config to override the one from snapshot, may be null
 */
public SnapshotDistribStateManager(Map<String, Object> snapshot, AutoScalingConfig config) {
  snapshot.forEach((path, value) -> {
    @SuppressWarnings({"unchecked"})
    Map<String, Object> map = (Map<String, Object>)value;
    Number version = (Number)map.getOrDefault("version", 0);
    String owner = (String)map.get("owner");
    String modeStr = (String)map.getOrDefault("mode", CreateMode.PERSISTENT.toString());
    CreateMode mode = CreateMode.valueOf(modeStr);
    byte[] bytes = null;
    if (map.containsKey("data")) {
      bytes = Base64.base64ToByteArray((String)map.get("data"));
    }
    dataMap.put(path, new VersionedData(version.intValue(), bytes, mode, owner));
  });
  if (config != null) { // overwrite existing
    VersionedData vd = new VersionedData(config.getZkVersion(), Utils.toJSON(config), CreateMode.PERSISTENT, "0");
    dataMap.put(ZkStateReader.SOLR_AUTOSCALING_CONF_PATH, vd);
  }
  if (log.isDebugEnabled()) {
    log.debug("- loaded snapshot of {} resources", dataMap.size());
  }
}
 
Example 5
Source File: TestPersistentNode.java    From curator with Apache License 2.0 5 votes vote down vote up
@Test
public void testQuickSetData() throws Exception
{
    final byte[] TEST_DATA = "hey".getBytes();
    final byte[] ALT_TEST_DATA = "there".getBytes();

    Timing timing = new Timing();
    PersistentNode pen = null;
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    try
    {
        client.start();
        pen = new PersistentNode(client, CreateMode.PERSISTENT, false, "/test", TEST_DATA);
        pen.start();
        try
        {
            pen.setData(ALT_TEST_DATA);
            Assert.fail("IllegalStateException should have been thrown");
        }
        catch ( IllegalStateException dummy )
        {
            // expected
        }
    }
    finally
    {
        CloseableUtils.closeQuietly(pen);
        CloseableUtils.closeQuietly(client);
    }
}
 
Example 6
Source File: SimDistribStateManager.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public String createData(String path, byte[] data, CreateMode mode) throws AlreadyExistsException, NoSuchElementException, IOException {
  if ((CreateMode.EPHEMERAL == mode || CreateMode.PERSISTENT == mode) && hasData(path)) {
    throw new AlreadyExistsException(path);
  }

  String relPath = path.charAt(0) == '/' ? path.substring(1) : path;
  if (relPath.length() == 0) { //Trying to create root-node, return null.
    // TODO should trying to create a root node throw an exception since its always init'd in the ctor?
    return null;
  }

  String[] elements = relPath.split("/");
  StringBuilder parentStringBuilder = new StringBuilder();
  Node parentNode = null;
  if (elements.length == 1) { // Direct descendant of '/'.
    parentNode = getRoot();
  } else { // Indirect descendant of '/', lookup parent node
    for (int i = 0; i < elements.length - 1; i++) {
      parentStringBuilder.append('/');
      parentStringBuilder.append(elements[i]);
    }
    if (!hasData(parentStringBuilder.toString())) {
      throw new NoSuchElementException(parentStringBuilder.toString());
    }
    parentNode = traverse(parentStringBuilder.toString(), false, mode);
  }

  multiLock.lock();
  try {
    String nodeName = elements[elements.length-1];
    Node childNode = createNode(parentNode, mode, parentStringBuilder.append("/"), nodeName, data,false);
    parentNode.setChild(childNode.name, childNode);
    return childNode.path;
  } finally {
    multiLock.unlock();
  }

}
 
Example 7
Source File: ZKUtil.java    From helix with Apache License 2.0 5 votes vote down vote up
public static void asyncCreateOrMerge(RealmAwareZkClient client, String path,
    final ZNRecord record, final boolean persistent, final boolean mergeOnUpdate) {
  try {
    if (client.exists(path)) {
      if (mergeOnUpdate) {
        ZNRecord curRecord = client.readData(path);
        if (curRecord != null) {
          curRecord.merge(record);
          client.asyncSetData(path, curRecord, -1, null);
        } else {
          client.asyncSetData(path, record, -1, null);
        }
      } else {
        client.asyncSetData(path, record, -1, null);
      }
    } else {
      CreateMode mode = (persistent) ? CreateMode.PERSISTENT : CreateMode.EPHEMERAL;
      if (record.getDeltaList().size() > 0) {
        ZNRecord newRecord = new ZNRecord(record.getId());
        newRecord.merge(record);
        client.create(path, null, mode);

        client.asyncSetData(path, newRecord, -1, null);
      } else {
        client.create(path, null, mode);

        client.asyncSetData(path, record, -1, null);
      }
    }
  } catch (Exception e) {
    logger.error("Exception in async create or update " + path + ". Exception: " + e.getMessage()
        + ". Give up.");
  }
}
 
Example 8
Source File: SnapshotDistribStateManager.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Populate this instance from another {@link DistribStateManager} instance.
 * @param other another instance
 * @param config optional {@link AutoScalingConfig}, which will overwrite any existing config.
 */
public SnapshotDistribStateManager(DistribStateManager other, AutoScalingConfig config) throws Exception {
  List<String> tree = other.listTree("/");
  if (log.isDebugEnabled()) {
    log.debug("- copying {} resources from {}", tree.size(), other.getClass().getSimpleName());
  }
  for (String path : tree) {
    dataMap.put(path, other.getData(path));
  }
  if (config != null) { // overwrite existing
    VersionedData vd = new VersionedData(config.getZkVersion(), Utils.toJSON(config), CreateMode.PERSISTENT, "0");
    dataMap.put(ZkStateReader.SOLR_AUTOSCALING_CONF_PATH, vd);
  }
}
 
Example 9
Source File: RegistryOperationsService.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Override
public void bind(String path,
    ServiceRecord record,
    int flags) throws IOException {
  Preconditions.checkArgument(record != null, "null record");
  validatePath(path);
  // validate the record before putting it
  RegistryTypeUtils.validateServiceRecord(path, record);
  LOG.info("Bound at {} : {}", path, record);

  CreateMode mode = CreateMode.PERSISTENT;
  byte[] bytes = serviceRecordMarshal.toBytes(record);
  zkSet(path, mode, bytes, getClientAcls(),
      ((flags & BindFlags.OVERWRITE) != 0));
}
 
Example 10
Source File: AccessOption.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * 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 11
Source File: RegistryOperationsService.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public void bind(String path,
    ServiceRecord record,
    int flags) throws IOException {
  Preconditions.checkArgument(record != null, "null record");
  validatePath(path);
  // validate the record before putting it
  RegistryTypeUtils.validateServiceRecord(path, record);
  LOG.info("Bound at {} : {}", path, record);

  CreateMode mode = CreateMode.PERSISTENT;
  byte[] bytes = serviceRecordMarshal.toBytes(record);
  zkSet(path, mode, bytes, getClientAcls(),
      ((flags & BindFlags.OVERWRITE) != 0));
}
 
Example 12
Source File: ZKEphemStateManager.java    From twister2 with Apache License 2.0 5 votes vote down vote up
public static PersistentNode createWorkerZnode(CuratorFramework client,
                                               String rootPath,
                                               String jobID,
                                               int workerID) {

  String ephemDirPath = ZKUtils.ephemDir(rootPath, jobID);
  String workerPath = ZKUtils.workerPath(ephemDirPath, workerID);
  byte[] znodeBody = ("" + workerID).getBytes(StandardCharsets.UTF_8);

  // it is ephemeral and persistent
  // ephemeral: it will be deleted after the worker leaves or fails
  // persistent: it will be persistent for occasional network problems

  return new PersistentNode(client, CreateMode.PERSISTENT, true, workerPath, znodeBody);
}
 
Example 13
Source File: ServiceDiscoveryImpl.java    From xian with Apache License 2.0 5 votes vote down vote up
@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 14
Source File: TestFramework.java    From curator with Apache License 2.0 5 votes vote down vote up
private boolean checkForContainers()
{
    if ( ZKPaths.getContainerCreateMode() == CreateMode.PERSISTENT )
    {
        System.out.println("Not using CreateMode.CONTAINER enabled version of ZooKeeper");
        return false;
    }
    return true;
}
 
Example 15
Source File: CreateBuilderImpl.java    From xian with Apache License 2.0 5 votes vote down vote up
CreateBuilderImpl(CuratorFrameworkImpl client)
{
    this.client = client;
    createMode = CreateMode.PERSISTENT;
    backgrounding = new Backgrounding();
    acling = new ACLing(client.getAclProvider());
    createParentsIfNeeded = false;
    createParentsAsContainers = false;
    compress = false;
    doProtected = false;
    protectedId = null;
}
 
Example 16
Source File: PersistentNode.java    From curator with Apache License 2.0 5 votes vote down vote up
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 17
Source File: ZKUtil.java    From helix with Apache License 2.0 5 votes vote down vote up
public static void createOrMerge(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.merge(record);
              return currentData;
            }
            return record;
          }
        };
        client.updateDataSerialized(path, updater);
      } else {
        CreateMode mode = (persistent) ? CreateMode.PERSISTENT : CreateMode.EPHEMERAL;
        if (record.getDeltaList().size() > 0) {
          ZNRecord value = new ZNRecord(record.getId());
          value.merge(record);
          client.create(path, value, mode);
        } else {
          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 18
Source File: ZKUtils.java    From twister2 with Apache License 2.0 4 votes vote down vote up
/**
 * create a PersistentNode object in the given path
 * it needs to be deleted explicitly, not ephemeral
 * it will be persistent for occasional network problems
 */
public static PersistentNode createPersistentZnode(String path,
                                                   byte[] payload) {

  return new PersistentNode(client, CreateMode.PERSISTENT, true, path, payload);
}
 
Example 19
Source File: PersistentEphemeralNodeTest.java    From curator-extensions with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testNonPersistentMode() throws Exception {
    new PersistentEphemeralNode(newCurator(), PATH, DATA, CreateMode.PERSISTENT);
}
 
Example 20
Source File: ZookeeperPersistentStore.java    From Bats with Apache License 2.0 4 votes vote down vote up
public ZookeeperPersistentStore(final CuratorFramework framework, final PersistentStoreConfig<V> config) throws StoreException {
  this.config = Preconditions.checkNotNull(config);
  this.client = new ZookeeperClient(framework, PathUtils.join("/", config.getName()), CreateMode.PERSISTENT);
}