Example usage for org.apache.zookeeper ZooKeeper setData

List of usage examples for org.apache.zookeeper ZooKeeper setData

Introduction

In this page you can find the example usage for org.apache.zookeeper ZooKeeper setData.

Prototype

public Stat setData(final String path, byte[] data, int version) throws KeeperException, InterruptedException 

Source Link

Document

Set the data for the node of the given path if such a node exists and the given version matches the version of the node (if the given version is -1, it matches any node's versions).

Usage

From source file:com.bigdata.service.jini.AbstractServer.java

License:Open Source License

/**
 * Creates a znode to represent this physical service within the
 * {@link BigdataZooDefs#PHYSICAL_SERVICES_CONTAINER}. The znode will be
 * persistent iff this service {@link #isPersistent()}. The znode name is
 * the <i>serviceUUID</i>. That name is <strong>stable</strong>, which is
 * a requirement for persistent services.
 * <p>//from   w  ww.j  av a2  s  .c  om
 * Note: we need to monitor (Watcher) the zookeeper connection state. If the
 * client is not connected when this method is invoked when we need to
 * create the ephemeral znode for the physical service when the client
 * becomes connected to zookeeper. This is done as part of ctor.
 * 
 * @param fed
 * @param serviceUUID
 * 
 * @throws KeeperException
 * @throws InterruptedException
 * 
 * @todo Any failover protocol in which the service can restart MUST provide
 *       for re-synchronization of the service when it restarts with the
 *       current primary / active ensemble.
 * 
 * @todo test failover w/ death and restart of both individual zookeeper
 *       instances and of the zookeeper ensemble. unless there are failover
 *       zookeeper instances it is going to lose track of our services (they
 *       are represented by ephemeral znodes). On reconnect to zookeeper,
 *       the service should should verify that its ephemeral node is present
 *       and create it if it is not present and re-negotiate the service
 *       failover chain.
 * 
 * @todo When supporting failover, a logicalService {@link UUID} will be
 *       required for compatibility with the bigdata APIs, which expect to
 *       refer to a service by a UUID. Lookup by UUID against jini will
 *       require hashing the physical services by their logical service UUID
 *       in the client, which is not hard.
 */
private void notifyZookeeper(final JiniFederation fed, final UUID serviceUUID)
        throws KeeperException, InterruptedException {

    if (fed == null)
        throw new IllegalArgumentException();

    if (serviceUUID == null)
        throw new IllegalArgumentException();

    //        if (logicalServiceZPath == null) {
    //
    //            throw new IllegalStateException(
    //                    "Logical service zpath not assigned.");
    //
    //        }

    final ZooKeeper zookeeper = fed.getZookeeper();

    if (zookeeper == null) {

        /*
         * @todo This is checked in case zookeeper is not integrated. If
         * we decide that zookeeper is a manditory component then change
         * this to throw an exception instead (or perhaps
         * fed.getZookeeper() will throw that exception).
         */

        log.warn("No zookeeper: will not create service znode: " + this);

        return;

    }

    if (logicalServiceZPath == null) {

        /*
         * @todo This is checked so that you can use a standalone
         * configuration file without having an assigned logical service
         * zpath that exists in zookeeper.
         * 
         * If we take out this test then all configuration files would have
         * to specify the logical service path, which might be an end state
         * for the zookeeper integration.
         */

        log.warn("No logicalServiceZPath in config file: will not create service znode: cls="
                + getClass().getName());

        return;

    }

    // Note: The services manager is responsible for doing this.
    //        try {
    //
    //            // make sure the parent node exists.
    //            zookeeper.create(logicalServiceZPath + "/"
    //                    + BigdataZooDefs.PHYSICAL_SERVICES_CONTAINER, new byte[0],
    //                    acl, CreateMode.PERSISTENT);
    //
    //        } catch (NodeExistsException ex) {
    //
    //            // ignore.
    //
    //        }

    /*
     * Note: The znode is created using the assigned service UUID. This
     * means that the total zpath for the physical service is stable and can
     * be re-created on restart of the service.
     */

    physicalServiceZPath = logicalServiceZPath + "/" + BigdataZooDefs.PHYSICAL_SERVICES_CONTAINER + "/"
            + serviceUUID;

    /*
     * Note: The znode data contains the attributes currently associated
     * with the service. We ensure during startup (and it won't change
     * unless someone messes with it) that this includes Hostname()
     * attributes which let us know what host the service is running on. The
     * ServicesManagerServer needs that information in order to decide if it
     * has responsibility for restarting the service.
     */

    final byte[] data = SerializerUtil.serialize(joinManager.getAttributes());

    try {

        /*
         * Create the znode for this service.
         */

        final List<ACL> acl = fed.getZooConfig().acl;

        zookeeper.create(physicalServiceZPath, data, acl,
                isPersistent() ? CreateMode.PERSISTENT : CreateMode.EPHEMERAL);

    } catch (NodeExistsException ex) {

        /*
         * Note: This code gets executed on each zookeeper (re-)connect. We
         * just update the Entry[] on the znode if it already exists.
         */

        zookeeper.setData(physicalServiceZPath, data, -1/* version */);

    }

    /*
     * Enter into the master / failover competition for the logical service.
     * 
     * Note: Synchronized so that creating and cancelling the master
     * election future are atomic.
     */
    synchronized (this) {

        if (masterElectionFuture == null) {

            masterElectionFuture = fed.submitMonitoredTask(new MasterElectionTask());

        }

    }

    if (log.isInfoEnabled())
        log.info("(Re)-registered with zookeeper: zpath=" + physicalServiceZPath);

}

From source file:com.bigdata.service.jini.master.TaskMaster.java

License:Open Source License

/**
 * Sets up the {@link JobState} in zookeeper, including the assignment of
 * service {@link UUID}s to each client. {@link #jobState} will be replaced
 * with the {@link JobState} read from zookeeper if a pre-existing job is
 * found in zookeeper./*from  w  ww .ja va2  s .c  o m*/
 * 
 * @return The global lock for the master running the job.
 * 
 * @throws KeeperException
 * @throws InterruptedException
 * @throws TimeoutException
 */
protected ZLock setupJob() throws KeeperException, InterruptedException, TimeoutException {

    final ZooKeeper zookeeper = fed.getZookeeperAccessor().getZookeeper();

    try {
        // ensure znode exists.
        zookeeper.create(fed.getZooConfig().zroot + "/" + BigdataZooDefs.JOBS, new byte[0],
                fed.getZooConfig().acl, CreateMode.PERSISTENT);
    } catch (NodeExistsException ex) {
        // ignore.
    }

    final String jobClassZPath = jobState.getJobClassZPath(fed);

    try {
        // ensure znode exists.
        zookeeper.create(jobClassZPath, new byte[0], fed.getZooConfig().acl, CreateMode.PERSISTENT);
    } catch (NodeExistsException ex) {
        // ignore.
    }

    /*
     * Use a global lock to protect the job.
     * 
     * Note: We just created parent of this lock node (or at any rate,
     * ensured that it exists).
     */
    final ZLock zlock = ZLockImpl.getLock(zookeeper, jobClassZPath + "/" + "locknode_" + jobState.jobName,
            fed.getZooConfig().acl);

    zlock.lock();
    try {

        final String jobZPath = jobState.getJobZPath(fed);

        if (jobState.deleteJob && zookeeper.exists(jobZPath, false/* watch */) != null) {

            /*
             * Delete old job.
             */

            log.warn("Deleting old job: " + jobZPath);

            ZooHelper.destroyZNodes(fed.getZookeeperAccessor().getZookeeper(), jobZPath, 0/* depth */);

            // detach the performance counters for the old job.
            detachPerformanceCounters();

        }

        try {

            // create znode that is the root for the job.
            zookeeper.create(jobZPath, SerializerUtil.serialize(jobState), fed.getZooConfig().acl,
                    CreateMode.PERSISTENT);

            if (log.isInfoEnabled())
                log.info("New job: " + jobState);

            try {

                /*
                 * Assign clients to services.
                 */
                final DiscoveredServices discoveredServices = new DiscoverServicesWithPreconditionsTask()
                        .call();

                jobState.clientServiceMap.assignClientsToServices(discoveredServices.clientServiceItems);

                jobState.aggregatorServiceMap
                        .assignClientsToServices(discoveredServices.aggregatorServiceItems);

                // write those assignments into zookeeper.
                zookeeper.setData(jobZPath, SerializerUtil.serialize(jobState), -1/* version */);

                if (log.isInfoEnabled())
                    log.info("Wrote client assignments into zookeeper.");

            } catch (Throwable t) {

                /*
                 * Since we created the jobState znode, delete the jobState
                 * while we are still holding the zlock.
                 */
                try {
                    zookeeper.delete(jobZPath, -1/* version */);
                } catch (Throwable t2) {
                    log.error(t2);
                }

                throw new RuntimeException(t);

            }

        } catch (NodeExistsException ex) {

            /*
             * Resuming a job already in progress and/or providing backup
             * clients for a job that is currently running.
             * 
             * Note: We use the client to data service UUID assignments read
             * from the znode data which are part of the jobState
             * 
             * @todo stable assignments are only required when clients will
             * read or write local data. This is not the common case and
             * should be a declarative configuration option.
             */

            jobState = (S) SerializerUtil.deserialize(zookeeper.getData(jobZPath, false, new Stat()));

            jobState.clientServiceMap.resolveServiceUUIDs(fed);

            jobState.aggregatorServiceMap.resolveServiceUUIDs(fed);

            jobState.resumedJob = true;

            log.warn("Pre-existing job: " + jobZPath);

        }

    } catch (KeeperException t) {

        zlock.unlock();

        throw t;

    } catch (InterruptedException t) {

        zlock.unlock();

        throw t;

    } catch (Throwable t) {

        zlock.unlock();

        throw new RuntimeException(t);

    }

    return zlock;

}

From source file:com.bigdata.zookeeper.TestZookeeperSessionSemantics.java

License:Open Source License

public void test_handleExpiredSession() throws InterruptedException, KeeperException, IOException {

    final String hosts = "localhost:" + clientPort;

    final Lock lock = new ReentrantLock();
    final Condition expireCond = lock.newCondition();
    final Condition connectCond = lock.newCondition();
    final Condition disconnectCond = lock.newCondition();
    final AtomicBoolean didExpire = new AtomicBoolean(false);
    final AtomicBoolean didDisconnect = new AtomicBoolean(false);

    /*// www . ja v  a 2s  . c  o  m
     * Start an instance and run until it gets an assigned sessionId.
     */
    {
        final ZooKeeper zk1a = new ZooKeeper(hosts, requestedSessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                log.warn(event);
            }
        });
        int i = 0;
        while (i < 10) {
            boolean done = false;
            if (zk1a.getState() == ZooKeeper.States.CONNECTED) {
                done = true;
            }
            log.info("zk.getState()=" + zk1a.getState() + ", zk.getSessionId()=" + zk1a.getSessionId());
            if (done)
                break;
            Thread.sleep(500);
            i++;
        }
        if (zk1a.getState() != ZooKeeper.States.CONNECTED) {
            fail("Did not connect.");
        }
        zk1a.close();
    }

    final ZooKeeper zk1 = new ZooKeeper(hosts, requestedSessionTimeout, new Watcher() {
        /**
         * Note: The default watcher will not receive any events
         * after a session expire. A {@link Zookeeper#close()}
         * causes an immediate session expire. Thus, no events
         * (include the session expire) will be received after a
         * close().
         */
        @Override
        public void process(final WatchedEvent event) {
            log.warn(event);
            switch (event.getState()) {
            case AuthFailed:
                break;
            case Disconnected:
                lock.lock();
                try {
                    didDisconnect.set(true);
                    disconnectCond.signalAll();
                } finally {
                    lock.unlock();
                }
                break;
            case Expired:
                lock.lock();
                try {
                    didExpire.set(true);
                    expireCond.signalAll();
                } finally {
                    lock.unlock();
                }
                break;
            //                        case ConnectedReadOnly: // not in 3.3.3
            //                            break;
            //                        case NoSyncConnected: // not in 3.3.3
            //                            break;
            //                        case SaslAuthenticated: // not in 3.3.3
            //                            break;
            case SyncConnected:
                lock.lock();
                try {
                    connectCond.signalAll();
                } finally {
                    lock.unlock();
                }
                break;
            case Unknown:
                break;
            }

        }
    });

    /*
     * Note: You can not obtain the negotiated session timeout until the
     * zookeeper client has connected to a zookeeper service (or rather,
     * it will return ZERO until it is connected).
     */
    final int negotiatedSessionTimeout;
    lock.lock();
    try {
        log.info("Waiting zk connected.");
        connectCond.await(10, TimeUnit.SECONDS);
        negotiatedSessionTimeout = zk1.getSessionTimeout();
        if (log.isInfoEnabled())
            log.info("Negotiated sessionTimeout=" + negotiatedSessionTimeout);
        assertNotSame(0, negotiatedSessionTimeout);
        assertTrue(negotiatedSessionTimeout > 0);
    } finally {
        lock.unlock();
    }

    assertTrue(zk1.getState().isAlive());

    assertFalse(didDisconnect.get());

    assertFalse(didExpire.get());

    // clear out test znodes.
    destroyZNodes(zk1, "/test");

    // ensure root /test znode exists.
    try {
        zk1.create("/test", new byte[] {}, acl, CreateMode.PERSISTENT);
    } catch (KeeperException.NodeExistsException ex) {
        log.warn("Ignoring: " + ex);
    }

    // look at that znode, establishing a watcher.
    zk1.getData("/test", true/* watch */, null/* stat */);

    // update the znode's data.
    zk1.setData("/test", new byte[] { 1 }, -1/* version */);

    // create an ephemeral sequential znode that is a child of /test.
    final String foozpath = zk1.create("/test/foo", new byte[] {}, acl, CreateMode.EPHEMERAL_SEQUENTIAL);

    // create a 2nd ephemeral sequential znode that is a child of /test.
    final String foozpath2 = zk1.create("/test/foo", new byte[] {}, acl, CreateMode.EPHEMERAL_SEQUENTIAL);

    /*
     * Look at that znode, establishing a watcher.
     * 
     * Note: We appear to see node deleted events for the ephemeral znodes
     * if the client connection is closed, but the state is still reported
     * as SyncConnected rather than SessionExpired.
     * 
     * Note: If we do not establish a watcher for an ephemeral znode, then
     * we DO NOT see an node deleted event when the client is closed!
     */
    zk1.getData(foozpath, true/* watch */, null/* stat */);
    //        zk1.getData(foozpath2, true/* watch */, null/* stat */);

    ////      close the existing instance.
    //        log.info("Closing ZK client");
    //        zk1.close();

    //        log.fatal("killing local zookeeper service.");
    //        killZKServer();
    //        Thread.sleep(5000);
    //        fail("done");

    if (false) {
        log.info("Spin loop awaiting !isAlive() for client.");
        final long begin = System.currentTimeMillis();
        while (zk1.getState().isAlive()) {
            log.info("zk.getState()=" + zk1.getState() + ", zk.getSessionId()=" + zk1.getSessionId());
            final long elapsed = System.currentTimeMillis() - begin;
            if (elapsed > 60000 * 2)
                fail("Client still isAlive().");
            Thread.sleep(1000);
        }
        log.info("Continuing");
    }

    if (true) {
        log.error("KILL ZOOKEEPER.");
        Thread.sleep(5000);
        log.info("Spin loop on ephemeral znode getData() for client.");
        while (true) {
            try {
                zk1.getData(foozpath, true/* watch */, null/* stat */);
            } catch (KeeperException ex) {
                log.error(ex, ex);
                Thread.sleep(1000);
                continue;
            }
            log.info("zk.getState()=" + zk1.getState() + ", zk.getSessionId()=" + zk1.getSessionId());
            break;
            //                final long elapsed = System.currentTimeMillis() - begin;
            //                if (elapsed > 60000 * 2)
            //                    fail("Client still isAlive().");
            //                Thread.sleep(1000);
        }
        log.info("Continuing");
        final byte[] a = zk1.getData(foozpath, true/* watch */, null/* stat */);
        assertTrue("Expected " + Arrays.toString(new byte[] { 1 }) + ", not " + Arrays.toString(a),
                BytesUtil.bytesEqual(new byte[] { 1 }, a));
    }

    // // The disconnect event should be immediate.
    // lock.lock();
    // try {
    // disconnectCond.await(100, TimeUnit.MILLISECONDS);
    // } finally {
    // lock.unlock();
    // }
    //
    // assertTrue(didDisconnect.get());

    assertFalse(didDisconnect.get());
    assertFalse(didExpire.get());

    assertFalse(zk1.getState().isAlive());

    /*
     * Wait up to a little more than the negotiated session timeout for the
     * session to be expired.
     */
    lock.lock();
    try {
        // Attempt to get the znode again.
        new Thread(new Runnable() {
            public void run() {
                try {
                    final byte[] tmp = zk1.getData("/test", true/* watch */, null/* stat */);
                } catch (KeeperException e) {
                    log.error(e, e);
                } catch (InterruptedException e) {
                    log.error(e, e);
                }
            }
        }).start();
        expireCond.await(negotiatedSessionTimeout + 10000, TimeUnit.MILLISECONDS);
        /*
         * Note: No events are ever delivered to the watcher with
         * KeeperStates:=SessionExpired. This appears to be a design
         * decision.
         */
        assertFalse(didExpire.get());
    } finally {
        lock.unlock();
    }

    /*
     * Now obtain a new session.
     */
    {
        log.warn("Starting new ZK connection");
        final ZooKeeper zk2 = new ZooKeeper(hosts, requestedSessionTimeout, new Watcher() {

            @Override
            public void process(WatchedEvent event) {
                log.warn(event);
            }
        });

        assertTrue(zk2.getState().isAlive());

    }

}

From source file:com.bloomreach.bstore.highavailability.zookeeper.ZKAccessUtils.java

License:Apache License

/**
 * Set data on the path for zk node.//from  w w w. j a  v a 2 s .  com
 *
 * @param zkHandle
 * @param path
 * @return {@link org.apache.zookeeper.data.Stat} stats on the current zk node.
 * @throws KeeperException
 * @throws InterruptedException
 */
public static Stat setDataOnZkNode(ZooKeeper zkHandle, String path, byte[] nodeData)
        throws KeeperException, InterruptedException {
    return zkHandle.setData(path, nodeData, -1);
}

From source file:com.boundary.zoocreeper.Restore.java

License:Apache License

private void restoreNode(ZooKeeper zk, BackupZNode zNode) throws KeeperException, InterruptedException {
    createPath(zk, getParentPath(zNode.path));
    try {/* ww w .  java  2 s . c o  m*/
        zk.create(zNode.path, zNode.data, zNode.acls, CreateMode.PERSISTENT);
        LOGGER.info("Created node: {}", zNode.path);
    } catch (NodeExistsException e) {
        if (options.overwriteExisting) {
            // TODO: Compare with current data / acls
            zk.setACL(zNode.path, zNode.acls, -1);
            zk.setData(zNode.path, zNode.data, -1);
        } else {
            LOGGER.warn("Node already exists: {}", zNode.path);
        }
    }
}

From source file:com.codemacro.jcm.storage.ZookeeperPathWatcher.java

License:Apache License

protected boolean writeData(String path, byte[] data, boolean persistent) {
    ZooKeeper zk = zkStorage.getZooKeeper();
    try {// w  w w.ja va  2 s.c  om
        if (null == zk.exists(path, false)) {
            zk.create(path, data, Ids.OPEN_ACL_UNSAFE,
                    persistent ? CreateMode.PERSISTENT : CreateMode.EPHEMERAL);
        } else {
            zk.setData(path, data, -1);
        }
    } catch (Exception e) {
        logger.error("write data failed on {}", path);
        return false;
    }
    return true;
}

From source file:com.cpaladin.study.zookeeper.ConfigMain.java

public static void main(String[] args) {
    try {/*from  www  .  j ava  2s  .co m*/
        ZooKeeper zk = new ZooKeeper("10.11.200.90:2181", 3000, null);
        zk.setData("/xlgame/user3/conf/channel.properties", (DateUtil.currentString()).getBytes("utf-8"), -1);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:com.deem.zkui.utils.ZooKeeperUtil.java

License:Open Source License

public void setPropertyValue(String path, String name, String value, ZooKeeper zk)
        throws KeeperException, InterruptedException {
    String nodePath = path + name;
    logger.debug("Setting property " + nodePath + " to " + value);
    zk.setData(nodePath, value.getBytes(), -1);

}

From source file:com.github.mosuka.zookeeper.nicli.command.DelQuotaCommand.java

License:Apache License

@Override
public void run(Map<String, Object> parameters) {
    try {//from  w w  w  .  j  a  v  a 2s.  c om
        String path = (String) parameters.get("path");
        boolean bytes = parameters.containsKey("bytes") ? (Boolean) parameters.get("bytes") : DEFAULT_BYTES;
        boolean numNodes = parameters.containsKey("num_nodes") ? (Boolean) parameters.get("num_nodes")
                : DEFAULT_NUM_NODES;

        ZooKeeper zk = getZookeeperConnection().getZooKeeper();

        String parentPath = Quotas.quotaZookeeper + path;
        String quotaPath = Quotas.quotaZookeeper + path + "/" + Quotas.limitNode;

        if (zk.exists(quotaPath, false) == null) {
            setStatus(Command.STATUS_ERROR);
            setMessage("quota for " + path + " does not exist.");
            return;
        }

        byte[] data = zk.getData(quotaPath, false, new Stat());
        StatsTrack strack = new StatsTrack(new String(data));

        if (bytes && !numNodes) {
            strack.setBytes(-1L);
            zk.setData(quotaPath, strack.toString().getBytes(), -1);
        } else if (!bytes && numNodes) {
            strack.setCount(-1);
            zk.setData(quotaPath, strack.toString().getBytes(), -1);
        } else if (bytes && numNodes) {
            List<String> children = zk.getChildren(parentPath, false);
            for (String child : children) {
                zk.delete(parentPath + "/" + child, -1);
            }
            QuotaUtil.trimProcQuotas(zk, parentPath);
        } else {
            setStatus(Command.STATUS_ERROR);
            setMessage("too few arguments");
            return;
        }

        setStatus(Command.STATUS_SUCCESS);
        setMessage(Command.SUCCESS_MESSAGE);
    } catch (KeeperException e) {
        setStatus(Command.STATUS_ERROR);
        setMessage(e.getMessage());
    } catch (InterruptedException e) {
        setStatus(Command.STATUS_ERROR);
        setMessage(e.getMessage());
    } catch (IOException e) {
        setStatus(Command.STATUS_ERROR);
        setMessage(e.getMessage());
    } catch (ClassCastException e) {
        setStatus(Command.STATUS_ERROR);
        setMessage(e.getMessage());
    } catch (NullPointerException e) {
        setStatus(Command.STATUS_ERROR);
        setMessage(e.getMessage());
    }
}

From source file:com.github.mosuka.zookeeper.nicli.command.SetCommand.java

License:Apache License

@Override
public void run(Map<String, Object> parameters) {
    try {// w w w  .  j a  v a 2 s  . c  o m
        String path = (String) parameters.get("path");
        String data = (String) parameters.get("data");
        int version = parameters.containsKey("version") ? (Integer) parameters.get("version") : DEFAULT_VERSION;
        boolean withStat = parameters.containsKey("with_stat") ? (Boolean) parameters.get("with_stat")
                : DEFAULT_WITH_STAT;

        ZooKeeper zk = getZookeeperConnection().getZooKeeper();

        byte[] dataByte = data.getBytes();

        Stat stat = zk.setData(path, dataByte, version);

        if (stat != null && withStat) {
            putResponse("stat", StatUtil.stat2Map(stat));
        }

        setStatus(Command.STATUS_SUCCESS);
        setMessage(Command.SUCCESS_MESSAGE);
    } catch (KeeperException e) {
        setStatus(Command.STATUS_ERROR);
        setMessage(e.getMessage());
    } catch (InterruptedException e) {
        setStatus(Command.STATUS_ERROR);
        setMessage(e.getMessage());
    } catch (ClassCastException e) {
        setStatus(Command.STATUS_ERROR);
        setMessage(e.getMessage());
    } catch (NullPointerException e) {
        setStatus(Command.STATUS_ERROR);
        setMessage(e.getMessage());
    }
}