/** * Creates the serialized value of the object and stores this in ZooKeeper under the path. It * updates the lastStatusVersion. It does not set a watcher for the path. */ private void updateCoordinateData() throws CoordinateMissingException, CloudnameException { if (!started.get()) { throw new IllegalStateException("Not started."); } if (!zkClient.isConnected()) { throw new CloudnameException("No proper connection with zookeeper."); } synchronized (lastStatusVersionMonitor) { try { Stat stat = zkClient .getZookeeper() .setData( path, zkCoordinateData.snapshot().serialize().getBytes(Util.CHARSET_NAME), lastStatusVersion); LOG.fine("Updated coordinate, latest version is " + stat.getVersion()); lastStatusVersion = stat.getVersion(); } catch (KeeperException.NoNodeException e) { throw new CoordinateMissingException("Coordinate does not exist " + path); } catch (KeeperException e) { throw new CloudnameException( "ZooKeeper errror in updateCoordinateData: " + e.getMessage(), e); } catch (UnsupportedEncodingException e) { throw new CloudnameException(e); } catch (InterruptedException e) { throw new CloudnameException(e); } catch (IOException e) { throw new CloudnameException(e); } } }
private void tryClaim() { try { zkClient .getZookeeper() .create( path, zkCoordinateData.snapshot().serialize().getBytes(Util.CHARSET_NAME), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL, new ClaimCallback(), this); } catch (IOException e) { LOG.info("Got IO exception on claim with new ZooKeeper instance " + e.getMessage()); } }
/** * Creates a string for debugging etc * * @return serialized version of the instance data. */ public synchronized String toString() { return zkCoordinateData.snapshot().toString(); }
/** * Remove endpoints and persist it. Requires that this instance owns the tryClaim to the * coordinate. * * @param names names of endpoints to be removed. */ public void removeEndpoints(final List<String> names) throws CloudnameException, CoordinateMissingException { zkCoordinateData.removeEndpoints(names); updateCoordinateData(); }
/** * Adds new endpoints and persist them. Requires that this instance owns the tryClaim to the * coordinate. * * @param newEndpoints endpoints to be added. */ public void putEndpoints(final List<Endpoint> newEndpoints) throws CloudnameException, CoordinateMissingException { zkCoordinateData.putEndpoints(newEndpoints); updateCoordinateData(); }
/** * Updates the ServiceStatus and persists it. Only allowed if we claimed the coordinate. * * @param status The new value for serviceStatus. */ public void updateStatus(final ServiceStatus status) throws CloudnameException, CoordinateMissingException { zkCoordinateData.setStatus(status); updateCoordinateData(); }