Search in sources :

Example 6 with ZooKeeper

use of org.apache.zookeeper.ZooKeeper in project zookeeper by apache.

the class Base method setUp.

@Before
public void setUp() throws Exception {
    RestCfg cfg = new RestCfg(new ByteArrayInputStream(String.format("rest.port=%s\n" + "rest.endpoint.1=%s;%s\n", GRIZZLY_PORT, CONTEXT_PATH, ZKHOSTPORT).getBytes()));
    rest = new RestMain(cfg);
    rest.start();
    zk = new ZooKeeper(ZKHOSTPORT, 30000, new MyWatcher());
    client = Client.create();
    znodesr = client.resource(BASEURI).path("znodes/v1");
    sessionsr = client.resource(BASEURI).path("sessions/v1/");
}
Also used : RestCfg(org.apache.zookeeper.server.jersey.cfg.RestCfg) ZooKeeper(org.apache.zookeeper.ZooKeeper) ByteArrayInputStream(java.io.ByteArrayInputStream) MyWatcher(org.apache.zookeeper.server.jersey.SetTest.MyWatcher) Before(org.junit.Before)

Example 7 with ZooKeeper

use of org.apache.zookeeper.ZooKeeper in project zookeeper by apache.

the class BaseSysTest method setUp.

@Before
public void setUp() throws Exception {
    if (!fakeMachines) {
        zk = new ZooKeeper(zkHostPort, 15000, new Watcher() {

            public void process(WatchedEvent e) {
            }
        });
        im = new InstanceManager(zk, prefix);
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Watcher(org.apache.zookeeper.Watcher) Before(org.junit.Before)

Example 8 with ZooKeeper

use of org.apache.zookeeper.ZooKeeper in project zookeeper by apache.

the class GenerateLoad method main.

/**
     * @param args
     * @throws InterruptedException
     * @throws KeeperException
     * @throws DuplicateNameException
     * @throws NoAvailableContainers
     * @throws NoAssignmentException
     */
public static void main(String[] args) throws InterruptedException, KeeperException, NoAvailableContainers, DuplicateNameException, NoAssignmentException {
    args = processOptions(args);
    if (args.length == 5) {
        try {
            StatusWatcher statusWatcher = new StatusWatcher();
            ZooKeeper zk = new ZooKeeper(args[0], 15000, statusWatcher);
            if (!statusWatcher.waitConnected(5000)) {
                System.err.println("Could not connect to " + args[0]);
                return;
            }
            InstanceManager im = new InstanceManager(zk, args[1]);
            ss = new ServerSocket(0);
            int port = ss.getLocalPort();
            int serverCount = Integer.parseInt(args[2]);
            int clientCount = Integer.parseInt(args[3]);
            StringBuilder quorumHostPort = new StringBuilder();
            StringBuilder zkHostPort = new StringBuilder();
            for (int i = 0; i < serverCount; i++) {
                String[] r = QuorumPeerInstance.createServer(im, i, leaderServes);
                if (i > 0) {
                    quorumHostPort.append(',');
                    zkHostPort.append(',');
                }
                // r[0] == "host:clientPort"
                zkHostPort.append(r[0]);
                // r[1] == "host:leaderPort:leaderElectionPort"
                quorumHostPort.append(r[1]);
                // Appending ";clientPort"
                quorumHostPort.append(";" + (r[0].split(":"))[1]);
            }
            for (int i = 0; i < serverCount; i++) {
                QuorumPeerInstance.startInstance(im, quorumHostPort.toString(), i);
            }
            if (leaderOnly) {
                int tries = 0;
                outer: while (true) {
                    Thread.sleep(1000);
                    IOException lastException = null;
                    String[] parts = zkHostPort.toString().split(",");
                    for (int i = 0; i < parts.length; i++) {
                        try {
                            String mode = getMode(parts[i]);
                            if (mode.equals("leader")) {
                                zkHostPort = new StringBuilder(parts[i]);
                                System.out.println("Connecting exclusively to " + zkHostPort.toString());
                                break outer;
                            }
                        } catch (IOException e) {
                            lastException = e;
                        }
                    }
                    if (tries++ > 3) {
                        throw lastException;
                    }
                }
            }
            for (int i = 0; i < clientCount; i++) {
                im.assignInstance("client" + i, GeneratorInstance.class, zkHostPort.toString() + ' ' + InetAddress.getLocalHost().getCanonicalHostName() + ':' + port, 1);
            }
            new AcceptorThread();
            new ReporterThread();
            BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
            String line;
            while ((line = is.readLine()) != null) {
                try {
                    String[] cmdNumber = line.split(" ");
                    if (cmdNumber[0].equals("percentage") && cmdNumber.length > 1) {
                        int number = Integer.parseInt(cmdNumber[1]);
                        if (number < 0 || number > 100) {
                            throw new NumberFormatException("must be between 0 and 100");
                        }
                        sendChange(number);
                    } else if (cmdNumber[0].equals("sleep") && cmdNumber.length > 1) {
                        int number = Integer.parseInt(cmdNumber[1]);
                        Thread.sleep(number * 1000);
                    } else if (cmdNumber[0].equals("save") && cmdNumber.length > 1) {
                        sf = new PrintStream(cmdNumber[1]);
                    } else {
                        System.err.println("Commands must be:");
                        System.err.println("\tpercentage new_write_percentage");
                        System.err.println("\tsleep seconds_to_sleep");
                        System.err.println("\tsave file_to_save_output");
                    }
                } catch (NumberFormatException e) {
                    System.out.println("Not a valid number: " + e.getMessage());
                }
            }
        } catch (NumberFormatException e) {
            doUsage();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(2);
        }
    } else {
        doUsage();
    }
}
Also used : PrintStream(java.io.PrintStream) InputStreamReader(java.io.InputStreamReader) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) ZooKeeper(org.apache.zookeeper.ZooKeeper) BufferedReader(java.io.BufferedReader)

Example 9 with ZooKeeper

use of org.apache.zookeeper.ZooKeeper in project zookeeper by apache.

the class InstanceContainer method run.

public void run() throws IOException, InterruptedException, KeeperException {
    zk = new ZooKeeper(zkHostPort, sessTimeout, this);
    mknod(assignmentsNode, CreateMode.PERSISTENT);
    mknod(statusNode, CreateMode.EPHEMERAL);
    mknod(reportsNode, CreateMode.PERSISTENT);
    // Now we just start watching the assignments directory
    zk.getChildren(assignmentsNode, true, this, null);
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper)

Example 10 with ZooKeeper

use of org.apache.zookeeper.ZooKeeper in project zookeeper by apache.

the class InvalidSnapshotTest method testInvalidSnapshot.

/**
     * Validate that the server can come up on an invalid snapshot - by
     * reverting to a prior snapshot + associated logs.
     */
@Test
public void testInvalidSnapshot() throws Exception {
    ZooKeeper zk = createClient();
    try {
        for (int i = 0; i < 2000; i++) {
            zk.create("/invalidsnap-" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
    } finally {
        zk.close();
    }
    NIOServerCnxnFactory factory = (NIOServerCnxnFactory) serverFactory;
    stopServer();
    // now corrupt the snapshot
    File snapFile = factory.zkServer.getTxnLogFactory().findMostRecentSnapshot();
    LOG.info("Corrupting " + snapFile);
    RandomAccessFile raf = new RandomAccessFile(snapFile, "rws");
    raf.setLength(3);
    raf.close();
    // now restart the server
    startServer();
    // verify that the expected data exists and wasn't lost
    zk = createClient();
    try {
        assertTrue("the node should exist", (zk.exists("/invalidsnap-1999", false) != null));
    } finally {
        zk.close();
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) RandomAccessFile(java.io.RandomAccessFile) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Test(org.junit.Test)

Aggregations

ZooKeeper (org.apache.zookeeper.ZooKeeper)311 Test (org.junit.Test)172 KeeperException (org.apache.zookeeper.KeeperException)89 Stat (org.apache.zookeeper.data.Stat)43 WatchedEvent (org.apache.zookeeper.WatchedEvent)36 ArrayList (java.util.ArrayList)35 TestableZooKeeper (org.apache.zookeeper.TestableZooKeeper)33 IOException (java.io.IOException)32 CountDownLatch (java.util.concurrent.CountDownLatch)30 Watcher (org.apache.zookeeper.Watcher)25 File (java.io.File)23 ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)21 CountdownWatcher (org.apache.zookeeper.test.ClientBase.CountdownWatcher)21 ServerCnxnFactory (org.apache.zookeeper.server.ServerCnxnFactory)20 ReconfigTest (org.apache.zookeeper.test.ReconfigTest)16 ZooKeeperAdmin (org.apache.zookeeper.admin.ZooKeeperAdmin)15 TimeoutException (java.util.concurrent.TimeoutException)8 AsyncCallback (org.apache.zookeeper.AsyncCallback)8 ACL (org.apache.zookeeper.data.ACL)8 Id (org.apache.zookeeper.data.Id)8