Example usage for org.apache.zookeeper Op create

List of usage examples for org.apache.zookeeper Op create

Introduction

In this page you can find the example usage for org.apache.zookeeper Op create.

Prototype

public static Op create(String path, byte[] data, List<ACL> acl, CreateMode createMode) 

Source Link

Document

Constructs a create operation.

Usage

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).
 *///from  ww  w .ja  v a  2  s  .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.ZkClusterDao.java

License:Apache License

@Override
public String createCluster(String name, ClusterData data)
        throws MgmtZNodeAlreayExistsException, MgmtZooKeeperException {
    String path = PathUtil.clusterPath(name);
    byte rawData[] = mapper.writeValueAsBytes(data);

    List<Op> ops = new ArrayList<Op>();
    ops.add(Op.create(path, rawData, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT));
    ops.add(Op.create(PathUtil.pgRootPath(name), Constant.ZERO_BYTE, ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT));//from w ww. ja  va  2  s. c om
    ops.add(Op.create(PathUtil.pgsRootPath(name), Constant.ZERO_BYTE, ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT));
    ops.add(Op.create(PathUtil.gwRootPath(name), Constant.ZERO_BYTE, ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT));
    ops.add(Op.create(PathUtil.rsRootPath(name), Constant.ZERO_BYTE, ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT));
    notificationDao.addCreateClusterOp(ops, name);

    List<OpResult> results = zookeeper.multi(ops);
    zookeeper.handleResultsOfMulti(results);

    OpResult.CreateResult resultForCreatingRoot = (OpResult.CreateResult) results.get(0);

    return resultForCreatingRoot.getPath();
}

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 w  w  w .  j a  v  a  2 s .co 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.ZkNotificationDao.java

License:Apache License

@Override
public void addCreateClusterOp(List<Op> ops, String clusterName)
        throws MgmtZooKeeperException, MgmtZNodeAlreayExistsException {
    String path = pathOfCluster(clusterName);
    boolean exist = zookeeper.isExists(path);
    if (exist) {// w  ww.  java 2s  .com
        throw new MgmtZNodeAlreayExistsException(path, String
                .format("-ERR the cluster znode for notification already exists. cluster:%s.", clusterName));
    }

    ops.add(Op.create(path, Constant.ZERO_BYTE, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT));
    ops.add(Op.create(rootPathOfGateway(clusterName), Constant.ZERO_BYTE, ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT));
    try {
        ops.add(Op.create(pathOfGWAffinity(clusterName),
                Constant.AFFINITY_ZNODE_INITIAL_DATA.getBytes(config.getCharset()), ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.PERSISTENT));
    } catch (UnsupportedEncodingException e) {
        throw new AssertionError(config.getCharset() + " is unknown.");
    }
}

From source file:com.navercorp.nbasearc.confmaster.repository.dao.zookeeper.ZkNotificationDao.java

License:Apache License

@Override
public void addCreateGatewayOp(List<Op> ops, String clusterName, String gatewayName, String ip, int port)
        throws MgmtZNodeAlreayExistsException, MgmtZooKeeperException, MgmtZNodeDoesNotExistException {
    String clusterPath = pathOfCluster(clusterName);
    boolean exist = zookeeper.isExists(clusterPath);
    if (!exist) {
        throw new MgmtZNodeDoesNotExistException(clusterPath,
                String.format("-ERR the cluster znode for notification already exists. cluster:%s, gateway:%s",
                        clusterName, gatewayName));
    }/*from   w  ww. ja  v  a2  s  .co m*/

    String gatewayPath = pathOfGateway(clusterName, gatewayName);
    exist = zookeeper.isExists(gatewayPath);
    if (exist) {
        throw new MgmtZNodeAlreayExistsException(gatewayPath,
                String.format("-ERR the gateway znode for notification already exists. cluster:%s, gateway:%s",
                        clusterName, gatewayName));
    }

    DataOfGateway data = new DataOfGateway(ip, port);
    byte rawData[] = mapper.writeValueAsBytes(data);
    ops.add(Op.create(gatewayPath, rawData, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT));
}

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  w  ww .ja  v a 2 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.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());/*from w w w .  j  a v  a2  s  .c  o  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.server.cluster.GatewayLookup.java

License:Apache License

public void addCreateClusterOp(List<Op> ops, String clusterName)
        throws MgmtZooKeeperException, MgmtZNodeAlreayExistsException {
    String path = PathUtil.pathOfGwLookupCluster(clusterName);
    boolean exist = zk.isExists(path);
    if (exist) {/*from ww  w . j  av  a  2s.co  m*/
        throw new MgmtZNodeAlreayExistsException(path, String
                .format("-ERR the cluster znode for notification already exists. cluster:%s.", clusterName));
    }

    ops.add(Op.create(path, Constant.ZERO_BYTE, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT));
    ops.add(Op.create(PathUtil.rootPathOfGwLookup(clusterName), Constant.ZERO_BYTE, ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT));
    try {
        ops.add(Op.create(PathUtil.pathOfGwAffinity(clusterName),
                Constant.AFFINITY_ZNODE_INITIAL_DATA.getBytes(config.getCharset()), ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.PERSISTENT));
    } catch (UnsupportedEncodingException e) {
        throw new AssertionError(config.getCharset() + " is unknown.");
    }
}

From source file:com.navercorp.nbasearc.confmaster.server.cluster.GatewayLookup.java

License:Apache License

public void addCreateGatewayOp(List<Op> ops, String clusterName, String gatewayName, String ip, int port)
        throws MgmtZNodeAlreayExistsException, MgmtZooKeeperException, MgmtZNodeDoesNotExistException {
    String clusterPath = PathUtil.pathOfGwLookupCluster(clusterName);
    boolean exist = zk.isExists(clusterPath);
    if (!exist) {
        throw new MgmtZNodeDoesNotExistException(clusterPath,
                String.format("-ERR the cluster znode for notification already exists. cluster:%s, gateway:%s",
                        clusterName, gatewayName));
    }/*from   w w w . j a  va  2s .co  m*/

    String gatewayPath = PathUtil.pathOfGwLookup(clusterName, gatewayName);
    exist = zk.isExists(gatewayPath);
    if (exist) {
        throw new MgmtZNodeAlreayExistsException(gatewayPath,
                String.format("-ERR the gateway znode for notification already exists. cluster:%s, gateway:%s",
                        clusterName, gatewayName));
    }

    GatewayLookupData data = new GatewayLookupData(ip, port);
    byte rawData[] = mapper.writeValueAsBytes(data);
    ops.add(Op.create(gatewayPath, rawData, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT));
}

From source file:com.navercorp.nbasearc.confmaster.server.command.ClusterService.java

License:Apache License

protected void createClusterZooKeeperZnodes(String name, Cluster cluster)
        throws MgmtZNodeAlreayExistsException, MgmtZooKeeperException, NoNodeException {
    // DB/*  w w  w.  jav a2  s .  c  o  m*/
    String path = PathUtil.clusterPath(name);

    List<Op> ops = new ArrayList<Op>();
    ops.add(Op.create(path, cluster.persistentDataToBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT));
    ops.add(Op.create(PathUtil.pgRootPath(name), ZERO_BYTE, ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT));
    ops.add(Op.create(PathUtil.pgsRootPath(name), ZERO_BYTE, ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT));
    ops.add(Op.create(PathUtil.gwRootPath(name), ZERO_BYTE, ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT));
    ops.add(Op.create(PathUtil.rsRootPath(name), ZERO_BYTE, ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT));
    gwInfoNoti.addCreateClusterOp(ops, name);

    List<OpResult> results = zk.multi(ops);
    zk.handleResultsOfMulti(results);

    // In memory
    container.put(cluster.getPath(), cluster);
}