private static void handleFilepath() { final BufferedReader br; try { br = new BufferedReader(new FileReader(filePath)); } catch (FileNotFoundException e) { throw new RuntimeException("File not found: " + filePath, e); } String line; try { while ((line = br.readLine()) != null) { try { cloudname.createCoordinate(Coordinate.parse(line)); System.out.println("Created " + line); } catch (Exception e) { System.err.println("Could not create: " + line + "Got error: " + e.getMessage()); } } } catch (IOException e) { throw new RuntimeException("Failed to read coordinate from file. " + e.getMessage(), e); } finally { cloudname.close(); try { br.close(); } catch (IOException e) { System.err.println("Failed while trying to close file reader. " + e.getMessage()); } } }
/** * Tests the behavior of Zookeeper upon a restart. ZK should clean up old coordinates. * * @throws Exception */ @Test public void testZookeeperRestarts() throws Exception { final CountDownLatch connectedLatch1 = new CountDownLatch(1); final CountDownLatch connectedLatch2 = new CountDownLatch(3); TestCoordinateListener listener = setUpListenerEnvironment(connectedLatch1, connectedLatch2); assertTrue(connectedLatch1.await(20, TimeUnit.SECONDS)); log.info("Killing zookeeper"); forwarder.terminate(); ezk.shutdown(); ezk.del(); ezk.init(); Thread.sleep(2000); forwarder = new PortForwarder(forwarderPort, "127.0.0.1", zkport); int timeoutSecs = 30; while (--timeoutSecs > 0) { Thread.sleep(1000); } Coordinate c = Coordinate.parse("1.service.user.cell"); cn.createCoordinate(c); Thread.sleep(9000); assertEquals( listener.events.get(listener.events.size() - 1), CoordinateListener.Event.COORDINATE_OK); }
private TestCoordinateListener setUpListenerEnvironment( CountDownLatch connectedLatch1, CountDownLatch connectedLatch2) throws Exception { forwarderPort = Net.getFreePort(); forwarder = new PortForwarder(forwarderPort, "127.0.0.1", zkport); final Coordinate c = Coordinate.parse("1.service.user.cell"); cn = makeLocalZkCloudname(forwarderPort); try { cn.createCoordinate(c); } catch (CoordinateException e) { fail(e.toString()); } final TestCoordinateListener listener = new TestCoordinateListener(connectedLatch1, connectedLatch2); ServiceHandle serviceHandle = cn.claim(c); serviceHandle.registerCoordinateListener(listener); return listener; }
private static void handleCoordinateOperation() { final Resolver resolver = cloudname.getResolver(); final Coordinate coordinate = Coordinate.parse(coordinateFlag); switch (operationFlag) { case CREATE: try { cloudname.createCoordinate(coordinate); } catch (CloudnameException e) { System.err.println("Got error: " + e.getMessage()); break; } catch (CoordinateExistsException e) { e.printStackTrace(); break; } System.err.println("Created coordinate."); break; case DELETE: try { cloudname.destroyCoordinate(coordinate); } catch (CoordinateDeletionException e) { System.err.println("Got error: " + e.getMessage()); return; } catch (CoordinateMissingException e) { System.err.println("Got error: " + e.getMessage()); break; } catch (CloudnameException e) { System.err.println("Got error: " + e.getMessage()); break; } System.err.println("Deleted coordinate."); break; case STATUS: { ServiceStatus status; try { status = cloudname.getStatus(coordinate); } catch (CloudnameException e) { System.err.println( "Problems loading status, is service running? Error:\n" + e.getMessage()); break; } System.err.println( "Status:\n" + status.getState().toString() + " " + status.getMessage()); List<Endpoint> endpoints = null; try { endpoints = resolver.resolve( "all." + coordinate.getService() + "." + coordinate.getUser() + "." + coordinate.getCell()); } catch (CloudnameException e) { System.err.println("Got error: " + e.getMessage()); break; } System.err.println("Endpoints:"); for (Endpoint endpoint : endpoints) { if (endpoint.getCoordinate().getInstance() == coordinate.getInstance()) { System.err.println( endpoint.getName() + "-->" + endpoint.getHost() + ":" + endpoint.getPort() + " protocol:" + endpoint.getProtocol()); System.err.println("Endpoint data:\n" + endpoint.getEndpointData()); } } break; } case HOST: { List<Endpoint> endpoints = null; try { endpoints = resolver.resolve(coordinate.asString()); } catch (CloudnameException e) { System.err.println( "Could not resolve " + coordinate.asString() + " Error:\n" + e.getMessage()); break; } for (Endpoint endpoint : endpoints) { System.out.println("Host: " + endpoint.getHost()); } } break; case SET_CONFIG: try { cloudname.setConfig(coordinate, configFlag, null); } catch (CloudnameException e) { System.err.println("Got error: " + e.getMessage()); break; } catch (CoordinateMissingException e) { System.err.println("Non-existing coordinate."); } System.err.println("Config updated."); break; case READ_CONFIG: try { System.out.println("Config is:" + cloudname.getConfig(coordinate)); } catch (CoordinateMissingException e) { System.err.println("Non-existing coordinate."); } catch (CloudnameException e) { System.err.println("Problem with cloudname: " + e.getMessage()); } break; default: System.out.println("Unknown command " + operationFlag); } }