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/");
}
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);
}
}
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();
}
}
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);
}
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();
}
}
Aggregations