List of usage examples for org.apache.zookeeper Op setData
public static Op setData(String path, byte[] data, int version)
From source file:com.ery.estorm.zk.RecoverableZooKeeper.java
License:Apache License
/** * Convert Iterable of {@link ZKOp} we got into the ZooKeeper.Op instances * to actually pass to multi (need to do this in order to appendMetaData). */// w ww .j a v a 2s . c o m private Iterable<Op> prepareZKMulti(Iterable<Op> ops) throws UnsupportedOperationException { if (ops == null) return null; List<Op> preparedOps = new LinkedList<Op>(); for (Op op : ops) { if (op.getType() == ZooDefs.OpCode.create) { CreateRequest create = (CreateRequest) op.toRequestRecord(); preparedOps.add(Op.create(create.getPath(), appendMetaData(create.getData()), create.getAcl(), create.getFlags())); } else if (op.getType() == ZooDefs.OpCode.delete) { // no need to appendMetaData for delete preparedOps.add(op); } else if (op.getType() == ZooDefs.OpCode.setData) { SetDataRequest setData = (SetDataRequest) op.toRequestRecord(); preparedOps.add( Op.setData(setData.getPath(), appendMetaData(setData.getData()), setData.getVersion())); } else { throw new UnsupportedOperationException("Unexpected ZKOp type: " + op.getClass().getName()); } } return preparedOps; }
From source file:com.navercorp.nbasearc.confmaster.repository.dao.zookeeper.ZkGatewayDao.java
License:Apache License
@Override public String createGw(String gwid, GatewayData data, String clusterName, String pmName, PhysicalMachineCluster pmCluster) throws MgmtZooKeeperException { // Prepare//from www . j a v a 2 s . c o m List<Op> ops = new ArrayList<Op>(); byte gwDataOfBytes[] = mapper.writeValueAsBytes(data); ops.add(Op.create(PathUtil.gwPath(gwid, clusterName), gwDataOfBytes, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)); if (null != pmCluster) { PmClusterData cimDataClon = (PmClusterData) pmCluster.getData().clone(); cimDataClon.addGwId(Integer.valueOf(gwid)); ops.add(Op.setData(pmCluster.getPath(), mapper.writeValueAsBytes(cimDataClon), -1)); } else { PmClusterData cimData = new PmClusterData(); cimData.addGwId(Integer.valueOf(gwid)); ops.add(Op.create(PathUtil.pmClusterPath(clusterName, pmName), mapper.writeValueAsBytes(cimData), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)); } List<OpResult> results = zookeeper.multi(ops); zookeeper.handleResultsOfMulti(results); return ((OpResult.CreateResult) results.get(0)).getPath(); }
From source file:com.navercorp.nbasearc.confmaster.repository.dao.zookeeper.ZkGatewayDao.java
License:Apache License
@Override public void deleteGw(final String name, final String clusterName, PhysicalMachineCluster pmCluster) throws MgmtZooKeeperException, MgmtZNodeDoesNotExistException { String path = PathUtil.gwPath(name, clusterName); List<Op> ops = new ArrayList<Op>(); List<String> children = zookeeper.getChildren(path); for (String childName : children) { StringBuilder builder = new StringBuilder(path); builder.append("/").append(childName); String childPath = builder.toString(); ops.add(Op.delete(childPath, -1)); }// w ww .j av a2 s .c o m ops.add(Op.delete(path, -1)); PmClusterData pmClusterDataClone = (PmClusterData) pmCluster.getData().clone(); pmClusterDataClone.deleteGwId(Integer.valueOf(name)); byte cimDataOfBytes[] = mapper.writeValueAsBytes(pmClusterDataClone); ops.add(Op.setData(pmCluster.getPath(), cimDataOfBytes, -1)); if (pmClusterDataClone.getPgsIdList().isEmpty() && pmClusterDataClone.getGwIdList().isEmpty()) { ops.add(Op.delete(pmCluster.getPath(), -1)); } if (notificationDao.isGatewayExist(clusterName, name)) { notificationDao.addDeleteGatewayOp(ops, clusterName, name); } List<OpResult> results = zookeeper.multi(ops); zookeeper.handleResultsOfMulti(results); }
From source file:com.navercorp.nbasearc.confmaster.repository.dao.zookeeper.ZkNotificationDao.java
License:Apache License
@Override public Op createGatewayAffinityUpdateOperation(Cluster cluster) { final String gatewayAffinity = cluster.getGatewayAffinity(context); final String path = pathOfGWAffinity(cluster.getName()); try {/*from w w w .j a v a2 s.com*/ return Op.setData(path, gatewayAffinity.getBytes(config.getCharset()), -1); } catch (UnsupportedEncodingException e) { Logger.error("Update gateway affinity fail. {}", cluster, e); throw new AssertionError(config.getCharset() + " is unknown."); } }
From source file:com.navercorp.nbasearc.confmaster.repository.dao.zookeeper.ZkPartitionGroupServerDao.java
License:Apache License
@Override public String createPgs(String pgsId, String clusterName, PartitionGroupServerData data, PartitionGroup pg, PhysicalMachineCluster pmCluster) throws MgmtZooKeeperException { List<Op> ops = new ArrayList<Op>(); byte pgsDataOfBytes[] = mapper.writeValueAsBytes(data); ops.add(Op.create(PathUtil.pgsPath(pgsId, clusterName), pgsDataOfBytes, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT));//from www. j a va2 s. c o m RedisServerData redisServerData = new RedisServerData(); redisServerData.initialize(data.getPmName(), data.getPmIp(), data.getRedisPort(), data.getState(), data.getHb()); byte rsDataOfBytes[] = mapper.writeValueAsBytes(redisServerData); ops.add(Op.create(PathUtil.rsPath(pgsId, clusterName), rsDataOfBytes, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)); if (null != pmCluster) { PmClusterData cimDataClone = (PmClusterData) pmCluster.getData().clone(); cimDataClone.addPgsId(Integer.valueOf(pgsId)); ops.add(Op.setData(pmCluster.getPath(), mapper.writeValueAsBytes(cimDataClone), -1)); } else { PmClusterData cimData = new PmClusterData(); cimData.addPgsId(Integer.valueOf(pgsId)); ops.add(Op.create(PathUtil.pmClusterPath(clusterName, data.getPmName()), mapper.writeValueAsBytes(cimData), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)); } PartitionGroupData pgModified = PartitionGroupData.builder().from(pg.getData()) .addPgsId(Integer.parseInt(pgsId)).build(); ops.add(Op.setData(pg.getPath(), mapper.writeValueAsBytes(pgModified), -1)); List<OpResult> results = zookeeper.multi(ops); zookeeper.handleResultsOfMulti(results); OpResult.CreateResult resultForCreatingPGS = (OpResult.CreateResult) results.get(0); return resultForCreatingPGS.getPath(); }
From source file:com.navercorp.nbasearc.confmaster.repository.dao.zookeeper.ZkPartitionGroupServerDao.java
License:Apache License
/** * /* ww w .j av a2s. c o m*/ * SUBJECT * Q. Why deletePgs() method uses for-loop statement? * A. Due to the synchronization problem for creating or deleting opinions * with pgs_leave and pgs_del * * SCENARIO (remove an opinion) * * PGS * / \ OP1 : Leader's * / \ OP2 : Follower's * OP1 OP2 * * LEADER FOLLOWER * 1. pgs_leave * 2. pgs_del get a watch event of pgs_leave * 2.1. get children of PGS z-node. * 2.2. delete an opinion, OP2 * F 2.3. (fail) delete the children. * * SCENARIO (put an opinion) * * PGS * / OP1 : Leader's * / * OP1 * * LEADER FOLLOWER * 1. pgs_leave * 2. pgs_del * 2.1. get children of PGS z-node. * 2.2. put an opinion, OP2 * 2.3. delete the children. * PGS * \ * \ OP2 : Follower's * OP2 * F 2.4. (fail) delete the PGS z-node. * 2.5. get a watch event of pgs_leave * * But, eventually good things will happen. * * @Return Returns the number of retries if successful. */ @Override public int deletePgs(String name, String clusterName, PartitionGroup pg, PhysicalMachineCluster pmCluster) throws MgmtZooKeeperException { final String path = PathUtil.pgsPath(name, clusterName); final String pathForRs = PathUtil.rsPath(name, clusterName); final int MAX = config.getServerCommandPgsdelMaxretry(); int retryCnt; for (retryCnt = 1; retryCnt <= MAX; retryCnt++) { // Delete children(opinions) of PGS try { zookeeper.deleteChildren(path); } catch (MgmtZooKeeperException e) { if (e.getCause() instanceof KeeperException.NoNodeException && retryCnt != MAX) { // Retry Logger.info("Delete children of {} fail. retry {}", PartitionGroupServer.fullName(clusterName, name), retryCnt, e); continue; } else { throw e; } } // Delete children(opinions) of RS try { zookeeper.deleteChildren(pathForRs); } catch (MgmtZooKeeperException e) { if (e.getCause() instanceof KeeperException.NoNodeException && retryCnt != MAX) { // Retry Logger.info("Delete children of {} fail. retry {}", RedisServer.fullName(clusterName, name), retryCnt, e); continue; } else { throw e; } } // Delete PGS and RS & Update PG List<Op> ops = new ArrayList<Op>(); ops.add(Op.delete(path, -1)); ops.add(Op.delete(pathForRs, -1)); PmClusterData cimData = (PmClusterData) pmCluster.getData().clone(); cimData.deletePgsId(Integer.valueOf(name)); byte cimDataOfBytes[] = mapper.writeValueAsBytes(cimData); ops.add(Op.setData(pmCluster.getPath(), cimDataOfBytes, -1)); PartitionGroupData pgModified = PartitionGroupData.builder().from(pg.getData()) .deletePgsId(Integer.parseInt(name)).build(); byte pgDataOfBytes[] = mapper.writeValueAsBytes(pgModified); ops.add(Op.setData(pg.getPath(), pgDataOfBytes, -1)); if (cimData.getPgsIdList().isEmpty() && cimData.getGwIdList().isEmpty()) { ops.add(Op.delete(pmCluster.getPath(), -1)); } try { List<OpResult> results = zookeeper.multi(ops); zookeeper.handleResultsOfMulti(results); return retryCnt; } catch (MgmtZooKeeperException e) { if (e.getCause() instanceof KeeperException.NotEmptyException && retryCnt != MAX) { // Retry Logger.info("Delete {} fail. retry {}", PartitionGroupServer.fullName(clusterName, name), retryCnt, e); } else { throw e; } } } return retryCnt; }
From source file:com.navercorp.nbasearc.confmaster.repository.dao.zookeeper.ZkWorkflowLogDao.java
License:Apache License
@Override public synchronized void log(long jobID, String severity, String name, String type, String clusterName, String msg, String jsonArg) { while (numLogs >= config.getServerJobWorkflowLogMax()) { deleteLog(getNumOfStartLog());/* w w w.j a v a2 s . co m*/ } byte[] data; String path = pathOfLog(getLast()); List<Op> ops = new ArrayList<Op>(); ZkWorkflowLog workflowLog = new ZkWorkflowLog(getLast(), new Date(), jobID, type, severity, name, msg, clusterName, jsonArg); try { data = mapper.writeValueAsBytes(workflowLog); Long maxLogNo = getNumOfStartLog() + getNumLogs(); ops.add(Op.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)); ops.add(Op.setData(rootPathOfLog(), String.valueOf(maxLogNo).getBytes(config.getCharset()), -1)); ZooKeeper zk = zookeeper.getZooKeeper(); List<OpResult> results = zk.multi(ops); zookeeper.handleResultsOfMulti(results); } catch (Exception e) { Logger.error(workflowLog.toString(), e); } Logger.info(workflowLog.toStringWithoutInfo()); increaseNumLogs(); }
From source file:com.navercorp.nbasearc.confmaster.repository.ZooKeeperHolder.java
License:Apache License
public <T> Op createReflectMemoryIntoZkOperation(ZNode<T> znode, final int version) { return Op.setData(znode.getPath(), mapper.writeValueAsBytes(znode.getData()), version); }
From source file:com.navercorp.nbasearc.confmaster.repository.ZooKeeperHolder.java
License:Apache License
public <T> Op createReflectMemoryIntoZkOperation(String path, T data, final int version) { return Op.setData(path, mapper.writeValueAsBytes(data), version); }
From source file:com.navercorp.nbasearc.confmaster.server.cluster.GatewayLookup.java
License:Apache License
public Op createGatewayAffinityUpdateOperation(Cluster cluster) { final String gatewayAffinity = cluster.getGatewayAffinity(context); final String path = PathUtil.pathOfGwAffinity(cluster.getName()); try {//from ww w. ja v a2 s .c om return Op.setData(path, gatewayAffinity.getBytes(config.getCharset()), -1); } catch (UnsupportedEncodingException e) { Logger.error("Update gateway affinity fail. {}", cluster, e); throw new AssertionError(config.getCharset() + " is unknown."); } }