Package org.apache.zookeeper_voltpatches

Examples of org.apache.zookeeper_voltpatches.ZooKeeper$ChildWatchRegistration


    }

    @Test
    public void testAddChildWithPut() throws Exception
    {
        ZooKeeper zk = getClient(0);
        configure("/cache04", zk);

        LeaderCache dut = new LeaderCache(zk, "/cache04");
        dut.start(true);
        Map<Integer, Long> cache = dut.pointInTimeCache();

        dut.put(3, 88776655);

        while(true) {
            cache = dut.pointInTimeCache();
            if (cache.size() == 3) {
                Thread.sleep(1);
            }
            else {
                break;
            }
        }
        assertEquals("Item added", 4, cache.size());
        assertEquals(12345678, cache.get(0).longValue());
        assertEquals(87654321, cache.get(1).longValue());
        assertEquals(11223344, cache.get(2).longValue());
        assertEquals(88776655, cache.get(3).longValue());

        // modify the new child and make sure it has a watch set.
        dut.put(3, 99887766);
        while(true) {
            cache = dut.pointInTimeCache();
            if (cache.get(3) == 99887766) {
                break;
            }
        }
        assertEquals("Items accounted for.", 4, cache.size());
        assertEquals(99887766L, cache.get(3).longValue());

        dut.shutdown();
        zk.close();
    }
View Full Code Here


    }

    @Test
    public void testAddChildWithPutWithCallback() throws Exception
    {
        ZooKeeper zk = getClient(0);
        configure("/cache04", zk);

        TestCallback cb = new TestCallback();
        LeaderCache dut = new LeaderCache(zk, "/cache04", cb);
        dut.start(true);
        Map<Integer, Long> cache = cb.m_cache;

        dut.put(3, 88776655);

        while(true) {
            cache = cb.m_cache;
            if (cache.size() == 3) {
                Thread.sleep(1);
            }
            else {
                break;
            }
        }
        assertEquals("Item added", 4, cache.size());
        assertEquals(12345678, cache.get(0).longValue());
        assertEquals(87654321, cache.get(1).longValue());
        assertEquals(11223344, cache.get(2).longValue());
        assertEquals(88776655, cache.get(3).longValue());

        // modify the new child and make sure it has a watch set.
        dut.put(3, 99887766);
        while(true) {
            cache = cb.m_cache;
            if (cache.get(3) == 99887766) {
                break;
            }
        }
        assertEquals("Items accounted for.", 4, cache.size());
        assertEquals(99887766, cache.get(3).longValue());

        dut.shutdown();
        zk.close();
    }
View Full Code Here

     *
     * @param txnId The snapshot txnId
     * @param participantCount The number of hosts participating in this snapshot
     */
    public static void logParticipatingHostCount(long txnId, int participantCount) {
        ZooKeeper zk = VoltDB.instance().getHostMessenger().getZK();
        final String snapshotPath = VoltZK.completed_snapshots + "/" + txnId;

        boolean success = false;
        while (!success) {
            Stat stat = new Stat();
            byte data[] = null;
            try {
                data = zk.getData(snapshotPath, false, stat);
            } catch (KeeperException e) {
                if (e.code() == KeeperException.Code.NONODE) {
                    // If snapshot creation failed for some reason, the node won't exist. ignore
                    return;
                }
                VoltDB.crashLocalVoltDB("Failed to get snapshot completion node", true, e);
            } catch (InterruptedException e) {
                VoltDB.crashLocalVoltDB("Interrupted getting snapshot completion node", true, e);
            }
            if (data == null) {
                VoltDB.crashLocalVoltDB("Data should not be null if the node exists", false, null);
            }

            try {
                JSONObject jsonObj = new JSONObject(new String(data, Charsets.UTF_8));
                if (jsonObj.getLong("txnId") != txnId) {
                    VoltDB.crashLocalVoltDB("TxnId should match", false, null);
                }

                int hostCount = jsonObj.getInt("hostCount");
                // +1 because hostCount was initialized to -1
                jsonObj.put("hostCount", hostCount + participantCount + 1);
                zk.setData(snapshotPath, jsonObj.toString(4).getBytes(Charsets.UTF_8),
                        stat.getVersion());
            } catch (KeeperException.BadVersionException e) {
                continue;
            } catch (Exception e) {
                VoltDB.crashLocalVoltDB("This ZK call should never fail", true, e);
View Full Code Here

    }

    protected ZooKeeper getClient(int site) throws Exception {
        final Semaphore permit = new Semaphore(0);
        int clientPort = m_siteIdToZKPort.get(site);
        ZooKeeper keeper = new ZooKeeper("127.0.0.1:" + Integer.toString(clientPort), 4000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                if (event.getState() == KeeperState.SyncConnected) {
                    permit.release();
                }
View Full Code Here

        }
        return objects;
    }

    public static void updateClusterMetadata(Map<Integer, String> clusterMetadata) throws Exception {
        ZooKeeper zk = VoltDB.instance().getHostMessenger().getZK();

        List<String> metadataNodes = zk.getChildren(VoltZK.cluster_metadata, false);

        Set<Integer> hostIds = new HashSet<Integer>();
        for (String hostId : metadataNodes) {
            hostIds.add(Integer.valueOf(hostId));
        }

        /*
         * Remove anything that is no longer part of the cluster
         */
        Set<Integer> keySetCopy = new HashSet<Integer>(clusterMetadata.keySet());
        keySetCopy.removeAll(hostIds);
        for (Integer failedHostId : keySetCopy) {
            clusterMetadata.remove(failedHostId);
        }

        /*
         * Add anything that is new
         */
        Set<Integer> hostIdsCopy = new HashSet<Integer>(hostIds);
        hostIdsCopy.removeAll(clusterMetadata.keySet());
        List<Pair<Integer, ZKUtil.ByteArrayCallback>> callbacks =
            new ArrayList<Pair<Integer, ZKUtil.ByteArrayCallback>>();
        for (Integer hostId : hostIdsCopy) {
            ZKUtil.ByteArrayCallback cb = new ZKUtil.ByteArrayCallback();
            callbacks.add(Pair.of(hostId, cb));
            zk.getData(VoltZK.cluster_metadata + "/" + hostId, false, cb, null);
        }

        for (Pair<Integer, ZKUtil.ByteArrayCallback> p : callbacks) {
            Integer hostId = p.getFirst();
            ZKUtil.ByteArrayCallback cb = p.getSecond();
View Full Code Here

            public void run(List<String> children) {
                sem.release(1);
            }
        };

        ZooKeeper zk = getClient(0);
        zk.create("/babysitterroot", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        zk.create("/babysitterroot/c1", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
        Pair<BabySitter, List<String>> pair = BabySitter.blockingFactory(zk, "/babysitterroot", cb);
        BabySitter bs = pair.getFirst();
        sem.acquire();
        assertTrue(bs.lastSeenChildren().size() == 1);
        assertTrue(pair.getSecond().size() == 1);

        zk.create("/babysitterroot/c2", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
        sem.acquire();
        assertTrue(bs.lastSeenChildren().size() == 2);

        zk.delete("/babysitterroot/" + bs.lastSeenChildren().get(0), -1);
        sem.acquire();
        assertTrue(bs.lastSeenChildren().size() == 1);

        bs.shutdown();
    }
View Full Code Here

    private static void logSnapshotCompleteToZK(
            long txnId,
            boolean snapshotSuccess,
            Map<String, Map<Integer, Pair<Long, Long>>> exportSequenceNumbers) {
        ZooKeeper zk = VoltDB.instance().getHostMessenger().getZK();

        // Timeout after 10 minutes
        final long endTime = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(10);
        final String snapshotPath = VoltZK.completed_snapshots + "/" + txnId;
        boolean success = false;
        while (!success) {
            if (System.currentTimeMillis() > endTime) {
                VoltDB.crashLocalVoltDB("Timed out logging snapshot completion to ZK");
            }

            Stat stat = new Stat();
            byte data[] = null;
            try {
                data = zk.getData(snapshotPath, false, stat);
            } catch (NoNodeException e) {
                // The MPI creates the snapshot completion node asynchronously,
                // if the node doesn't exist yet, retry
                continue;
            } catch (Exception e) {
                VoltDB.crashLocalVoltDB("This ZK get should never fail", true, e);
            }
            if (data == null) {
                VoltDB.crashLocalVoltDB("Data should not be null if the node exists", false, null);
            }

            try {
                JSONObject jsonObj = new JSONObject(new String(data, "UTF-8"));
                if (jsonObj.getLong("txnId") != txnId) {
                    VoltDB.crashLocalVoltDB("TxnId should match", false, null);
                }
                int remainingHosts = jsonObj.getInt("hostCount") - 1;
                jsonObj.put("hostCount", remainingHosts);
                jsonObj.put("didSucceed", snapshotSuccess);
                if (!snapshotSuccess) {
                    jsonObj.put("isTruncation", false);
                }
                mergeExportSequenceNumbers(jsonObj, exportSequenceNumbers);
                zk.setData(snapshotPath, jsonObj.toString(4).getBytes("UTF-8"), stat.getVersion());
            } catch (KeeperException.BadVersionException e) {
                continue;
            } catch (Exception e) {
                VoltDB.crashLocalVoltDB("This ZK call should never fail", true, e);
            }
            success = true;
        }

        /*
         * If we are running without command logging there will be no consumer for
         * the completed snapshot messages. Consume them here to bound space usage in ZK.
         */
        try {
            TreeSet<String> snapshots = new TreeSet<String>(zk.getChildren(VoltZK.completed_snapshots, false));
            while (snapshots.size() > 30) {
                try {
                    zk.delete(VoltZK.completed_snapshots + "/" + snapshots.first(), -1);
                } catch (NoNodeException e) {}
                catch (Exception e) {
                    VoltDB.crashLocalVoltDB(
                            "Deleting a snapshot completion record from ZK should only fail with NoNodeException", true, e);
                }
View Full Code Here

        /*
         * Validate that no elastic join is in progress, blocking this catalog update.
         * If this update works with elastic then do the update anyways
         */
        ZooKeeper zk = VoltDB.instance().getHostMessenger().getZK();
        if (worksWithElastic == 0 &&
            !zk.getChildren(VoltZK.elasticJoinActiveBlockers, false).isEmpty()) {
            throw new VoltAbortException("Can't do a catalog update while an elastic join is active");
        }

        // Pull the current catalog and deployment version and hash info.  Validate that we're either:
        // (a) starting a new, valid catalog or deployment update
View Full Code Here

        return baos.toByteArray();
    }

    public static final ZooKeeper getClient(String zkAddress, int timeout, Set<Long> verbotenThreads) throws Exception {
        final Semaphore zkConnect = new Semaphore(0);
        ZooKeeper zk = new ZooKeeper(zkAddress, 2000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                if (event.getState() == KeeperState.SyncConnected) {
                    zkConnect.release();
                }
View Full Code Here

        }
    }

    private void validateStartAction() {
        try {
            ZooKeeper zk = m_messenger.getZK();
            boolean initCompleted = zk.exists(VoltZK.init_completed, false) != null;
            List<String> children = zk.getChildren(VoltZK.start_action, new StartActionWatcher(), null);
            if (!children.isEmpty()) {
                for (String child : children) {
                    byte[] data = zk.getData(VoltZK.start_action + "/" + child, false, null);
                    if (data == null) {
                        VoltDB.crashLocalVoltDB("Couldn't find " + VoltZK.start_action + "/" + child);
                    }
                    String startAction = new String(data);
                    if ((startAction.equals(StartAction.JOIN.toString()) ||
View Full Code Here

TOP

Related Classes of org.apache.zookeeper_voltpatches.ZooKeeper$ChildWatchRegistration

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.