Example usage for org.apache.zookeeper Transaction delete

List of usage examples for org.apache.zookeeper Transaction delete

Introduction

In this page you can find the example usage for org.apache.zookeeper Transaction delete.

Prototype

public Transaction delete(final String path, int version) 

Source Link

Usage

From source file:org.apache.distributedlog.service.placement.ZKPlacementStateManager.java

License:Apache License

@Override
public void saveOwnership(TreeSet<ServerLoad> serverLoads) throws StateManagerSaveException {
    logger.info("saving ownership");
    try {//  ww w .  ja  v  a 2s  .c  o m
        ZooKeeper zk = zkClient.get();
        // use timestamp as data so watchers will see any changes
        byte[] timestamp = ByteBuffer.allocate(8).putLong(System.currentTimeMillis()).array();

        if (zk.exists(serverLoadPath, false) == null) { //create path to rootnode if it does not yet exist
            createServerLoadPathIfNoExists(timestamp);
        }

        Transaction tx = zk.transaction();
        List<String> children = zk.getChildren(serverLoadPath, false);
        HashSet<String> servers = new HashSet<String>(children);
        tx.setData(serverLoadPath, timestamp, -1); // trigger the watcher that data has been updated
        for (ServerLoad serverLoad : serverLoads) {
            String server = serverToZkFormat(serverLoad.getServer());
            String serverPath = serverPath(server);
            if (servers.contains(server)) {
                servers.remove(server);
                tx.setData(serverPath, serverLoad.serialize(), -1);
            } else {
                tx.create(serverPath, serverLoad.serialize(), zkClient.getDefaultACL(), CreateMode.PERSISTENT);
            }
        }
        for (String server : servers) {
            tx.delete(serverPath(server), -1);
        }
        tx.commit();
    } catch (InterruptedException | IOException | KeeperException e) {
        throw new StateManagerSaveException(e);
    }
}

From source file:org.jhk.pulsing.zookeeper.taskdistribution.MasterMimic.java

License:Apache License

/**
 * @param path /assign/worker-1/task-1-1
 * @param data run xxx/*from ww w .  jav a  2 s.  com*/
 * @throws KeeperException 
 * @throws InterruptedException 
 */
private void assignTaskToWorker(String path, byte[] data) {
    // can use Transaction or zookeeper.multi with Op commands; however in nutshell needs it to be a single transaction
    // since assigning the task to a worker, must mean one creates the path and deletes the task from the task pool
    Transaction transaction = zookeeper.transaction();
    transaction.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    transaction.delete(TASKS_PATH + "/" + path.substring(path.lastIndexOf("/") + 1), -1);
    try {
        _LOGGER.info("Task assign and delete transaction for path {}, result {}", path, transaction.commit());
    } catch (InterruptedException | KeeperException exception) {
        _LOGGER.error("Error while assign and delete transaction ", exception);
    }
    /*
    zookeeper.multi(Arrays.asList(
        Op.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT),
        Op.delete(TASKS_PATH + "/" + path.substring( path.lastIndexOf("/") + 1), -1)
        ), ASSIGN_DELETE_TASK_CALLBACK, data);
    */
}