Java Code Examples for org.apache.curator.utils.ZKPaths#mkdirs()

The following examples show how to use org.apache.curator.utils.ZKPaths#mkdirs() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CreateBuilderImpl.java    From xian with Apache License 2.0 6 votes vote down vote up
static <T> void backgroundCreateParentsThenNode(final CuratorFrameworkImpl client, final OperationAndData<T> mainOperationAndData, final String path, Backgrounding backgrounding, final boolean createParentsAsContainers)
{
    BackgroundOperation<T> operation = new BackgroundOperation<T>()
    {
        @Override
        public void performBackgroundOperation(OperationAndData<T> dummy) throws Exception
        {
            try
            {
                ZKPaths.mkdirs(client.getZooKeeper(), path, false, client.getAclProvider(), createParentsAsContainers);
            }
            catch ( KeeperException e )
            {
                if ( !RetryLoop.isRetryException(e) )
                {
                    throw e;
                }
                // otherwise safe to ignore as it will get retried
            }
            client.queueOperation(mainOperationAndData);
        }
    };
    OperationAndData<T> parentOperation = new OperationAndData<T>(operation, mainOperationAndData.getData(), null, null, backgrounding.getContext());
    client.queueOperation(parentOperation);
}
 
Example 2
Source File: ZKHandler.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 向节点发送命令
 * @param myId 节点的id信息
 * @param command 命令内容 
 * @throws Exception 异常信息
 */
private static void sendZkCommand(String myId, String command) throws Exception {
    CuratorFramework zkConn = ZKUtils.getConnection();

    String basePath = ZKUtils.getZKBasePath();

    String nodePath = ZKPaths.makePath(basePath, ZK_NODE_PATH + "/" + myId);

    Stat stat;
    try {
        stat = zkConn.checkExists().forPath(nodePath);

        if (null == stat) {
            // 进行目录的创建操作
            ZKPaths.mkdirs(zkConn.getZookeeperClient().getZooKeeper(), nodePath);
        }
        // 设置节点信息
        zkConn.setData().inBackground().forPath(nodePath, command.getBytes());
    } catch (Exception e) {
        LOGGER.error("ZKHandler sendZkCommand exception", e);
        throw e;
    }

}
 
Example 3
Source File: ZkMultLoader.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 创建配制信息
 * 方法描述
 * @param configKey 配制的当前路径名称信息
 * @param filterInnerMap  最终的信息是否为map
 * @param configDirectory 配制的目录
 * @param restDirectory 子目录信息
 * @创建日期 2016年9月11日
 */
public boolean createPath(String path) {

    // 得到当前的目录信息
    LOGGER.trace("createPath child path is {}", path);

    boolean result = true;
    try {
        // 进行目录的创建操作
        ZKPaths.mkdirs(curator.getZookeeperClient().getZooKeeper(), path);
    } catch (Exception e) {
        LOGGER.error(" createPath error", e);
        result = false;
    }

    return result;
}
 
Example 4
Source File: BaseTestClass.java    From cloud-config with MIT License 6 votes vote down vote up
@Before
public void init() throws Throwable {
    zkRootClient = CuratorFrameworkFactory.builder()
            .connectString(server.getConnectString())
            .retryPolicy(new BoundedExponentialBackoffRetry(10, 100, 7))
            .build();
    zkRootClient.start();

    ZooKeeper zk = zkRootClient.getZookeeperClient().getZooKeeper();
    ZKPaths.mkdirs(zk, "/"+ CloudConfigCommon.CONFIG_ROOT);
    ZKPaths.mkdirs(zk, "/"+CloudConfigCommon.PROPERTY_ROOT);

    zkConfigClient = zkRootClient.usingNamespace(CloudConfigCommon.CONFIG_ROOT);
    zkPropsClient  = zkRootClient.usingNamespace(CloudConfigCommon.PROPERTY_ROOT);

    prepare();
}
 
Example 5
Source File: CreateBuilderImpl.java    From curator with Apache License 2.0 6 votes vote down vote up
static <T> void backgroundCreateParentsThenNode(final CuratorFrameworkImpl client, final OperationAndData<T> mainOperationAndData, final String path, Backgrounding backgrounding, final InternalACLProvider aclProvider, final boolean createParentsAsContainers)
{
    BackgroundOperation<T> operation = new BackgroundOperation<T>()
    {
        @Override
        public void performBackgroundOperation(OperationAndData<T> dummy) throws Exception
        {
            try
            {
                ZKPaths.mkdirs(client.getZooKeeper(), path, false, aclProvider, createParentsAsContainers);
            }
            catch ( KeeperException e )
            {
                if ( !client.getZookeeperClient().getRetryPolicy().allowRetry(e) )
                {
                    throw e;
                }
                // otherwise safe to ignore as it will get retried
            }
            client.queueOperation(mainOperationAndData);
        }
    };
    OperationAndData<T> parentOperation = new OperationAndData<>(operation, mainOperationAndData.getData(), null, null, backgrounding.getContext(), null);
    client.queueOperation(parentOperation);
}
 
Example 6
Source File: TransactionExample.java    From ZKRecipesByExample with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
	TestingServer server = new TestingServer();
	CuratorFramework client = null;
	try {
		client = createSimple(server.getConnectString());
		client.start();

		ZKPaths.mkdirs(client.getZookeeperClient().getZooKeeper(),"/a");
		ZKPaths.mkdirs(client.getZookeeperClient().getZooKeeper(),"/another/path");
		ZKPaths.mkdirs(client.getZookeeperClient().getZooKeeper(),"/yet/another/path");


		transaction(client);

	} catch (Exception ex) {
		ex.printStackTrace();
	} finally {
		CloseableUtils.closeQuietly(client);
		CloseableUtils.closeQuietly(server);
	}
}
 
Example 7
Source File: OthermsgTozkLoader.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean notiflyProcess() throws Exception {
    // 添加line目录,用作集群中节点,在线的基本目录信息
    String line = currZkPath + ZookeeperPath.ZK_SEPARATOR.getKey() + ZookeeperPath.FLOW_ZK_PATH_LINE.getKey();
    ZKPaths.mkdirs(this.getCurator().getZookeeperClient().getZooKeeper(), line);
    LOGGER.info("OthermsgTozkLoader zookeeper mkdir " + line + " success");

    // 添加序列目录信息
    String seqLine = currZkPath + ZookeeperPath.ZK_SEPARATOR.getKey()
            + ZookeeperPath.FLOW_ZK_PATH_SEQUENCE.getKey();
    seqLine = seqLine + ZookeeperPath.ZK_SEPARATOR.getKey() + ZookeeperPath.FLOW_ZK_PATH_SEQUENCE_INSTANCE.getKey();
    ZKPaths.mkdirs(this.getCurator().getZookeeperClient().getZooKeeper(), seqLine);

    String seqLeader = currZkPath + ZookeeperPath.ZK_SEPARATOR.getKey()
            + ZookeeperPath.FLOW_ZK_PATH_SEQUENCE.getKey();
    seqLeader = seqLeader + ZookeeperPath.ZK_SEPARATOR.getKey()
            + ZookeeperPath.FLOW_ZK_PATH_SEQUENCE_LEADER.getKey();
    ZKPaths.mkdirs(this.getCurator().getZookeeperClient().getZooKeeper(), seqLeader);

    String incrSeq = currZkPath + ZookeeperPath.ZK_SEPARATOR.getKey()
            + ZookeeperPath.FLOW_ZK_PATH_SEQUENCE.getKey();
    incrSeq = incrSeq + ZookeeperPath.ZK_SEPARATOR.getKey()
            + ZookeeperPath.FLOW_ZK_PATH_SEQUENCE_INCREMENT_SEQ.getKey();
    ZKPaths.mkdirs(this.getCurator().getZookeeperClient().getZooKeeper(), incrSeq);

    LOGGER.info("OthermsgTozkLoader zookeeper mkdir " + seqLine + " success");
    LOGGER.info("OthermsgTozkLoader zookeeper mkdir " + seqLeader + " success");
    LOGGER.info("OthermsgTozkLoader zookeeper mkdir " + incrSeq + " success");

    return true;
}
 
Example 8
Source File: DataHostDisable.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
public static void setStatusToZK(String nodePath, CuratorFramework curator, String value) throws Exception {
    Stat stat = curator.checkExists().forPath(nodePath);
    if (null == stat) {
        ZKPaths.mkdirs(curator.getZookeeperClient().getZooKeeper(), nodePath);
    }
    LOGGER.debug("ZkMultiLoader write file :" + nodePath + ", value :" + value);
    curator.setData().inBackground().forPath(nodePath, value.getBytes());
}
 
Example 9
Source File: ZkMultiLoader.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
public boolean createPath(String path) {

        LOGGER.trace("createPath child path is {}", path);

        boolean result = true;
        try {
            ZKPaths.mkdirs(curator.getZookeeperClient().getZooKeeper(), path);
        } catch (Exception e) {
            LOGGER.warn(" createPath error", e);
            result = false;
        }

        return result;
    }
 
Example 10
Source File: Zk.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
public static String zkEnsure(CuratorFramework client, String path) {
    try {
        ZKPaths.mkdirs(client.getZookeeperClient().getZooKeeper(), path, true);
        return path;
    } catch (Exception e) {
        LOG.error("Failed: mkdirs("+path+")",e);
        return null;
    }
}
 
Example 11
Source File: CloudConfigSample.java    From cloud-config with MIT License 5 votes vote down vote up
private static void prepare(TestingServer server) throws Exception {
        CuratorFramework zkRootClient = null;
        try {
            zkRootClient = CuratorFrameworkFactory.builder()
                    .connectString(server.getConnectString())
                    .retryPolicy(new BoundedExponentialBackoffRetry(10, 100, 7))
                    .build();
            zkRootClient.start();

            ZooKeeper zk = zkRootClient.getZookeeperClient().getZooKeeper();
            ZKPaths.mkdirs(zk, "/" + CloudConfigCommon.CONFIG_ROOT);
            ZKPaths.mkdirs(zk, "/"+CloudConfigCommon.PROPERTY_ROOT);

            CuratorFramework zkConfigClient = zkRootClient.usingNamespace(CloudConfigCommon.CONFIG_ROOT);
//        CuratorFramework zkPropsClient  = zkRootClient.usingNamespace(CloudConfigCommon.PROPERTY_ROOT);

            String config = "{\n" +
                    "    \"driverClassName\" : \"com.mysql.jdbc.Driver\",\n" +
                    "    \"userName\" : \"root\",\n" +
                    "    \"password\" : \"1111\", \n"+
                    "    \"jdbcUrl\" : \"jdbc:mysql://127.0.0.1:3306/a?characterEncoding=utf8&createDatabaseIfNotExist=true\"\n"+
                    "}";

            zkConfigClient.create().creatingParentsIfNeeded().forPath("/database/mydb", config.getBytes());
        } finally {
            if(zkRootClient!=null) {
                zkRootClient.close();
            }
        }
    }
 
Example 12
Source File: OthermsgTozkLoader.java    From dble with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean notifyProcess() throws Exception {
    String line = KVPathUtil.getOnlinePath();
    ZKPaths.mkdirs(this.getCurator().getZookeeperClient().getZooKeeper(), line);
    LOGGER.info("OthermsgTozkLoader zookeeper mkdir " + line + " success");

    String seqLine = KVPathUtil.getSequencesInstancePath();
    ZKPaths.mkdirs(this.getCurator().getZookeeperClient().getZooKeeper(), seqLine);
    LOGGER.info("OthermsgTozkLoader zookeeper mkdir " + seqLine + " success");

    String seqLeader = KVPathUtil.getSequencesLeaderPath();
    ZKPaths.mkdirs(this.getCurator().getZookeeperClient().getZooKeeper(), seqLeader);
    LOGGER.info("OthermsgTozkLoader zookeeper mkdir " + seqLeader + " success");

    String incrSeq = KVPathUtil.getSequencesIncrPath();
    ZKPaths.mkdirs(this.getCurator().getZookeeperClient().getZooKeeper(), incrSeq);
    LOGGER.info("OthermsgTozkLoader zookeeper mkdir " + incrSeq + " success");

    String binlogPauseStatusPath = KVPathUtil.getBinlogPauseStatus();
    ZKPaths.mkdirs(this.getCurator().getZookeeperClient().getZooKeeper(), binlogPauseStatusPath);
    LOGGER.info("OthermsgTozkLoader zookeeper mkdir " + binlogPauseStatusPath + " success");

    String binlogPauseInstances = KVPathUtil.getBinlogPauseInstance();
    ZKPaths.mkdirs(this.getCurator().getZookeeperClient().getZooKeeper(), binlogPauseInstances);
    LOGGER.info("OthermsgTozkLoader zookeeper mkdir " + binlogPauseInstances + " success");

    String ddlPath = KVPathUtil.getDDLPath();
    ZKPaths.mkdirs(this.getCurator().getZookeeperClient().getZooKeeper(), ddlPath);
    LOGGER.info("OthermsgTozkLoader zookeeper mkdir " + ddlPath + " success");

    String confStatusPath = KVPathUtil.getConfStatusPath();
    ZKPaths.mkdirs(this.getCurator().getZookeeperClient().getZooKeeper(), confStatusPath);
    LOGGER.info("OthermsgTozkLoader zookeeper mkdir " + confStatusPath + " success");

    String lockBasePathPath = KVPathUtil.getLockBasePath();
    if (this.getCurator().checkExists().forPath(lockBasePathPath) == null) {
        ZKPaths.mkdirs(this.getCurator().getZookeeperClient().getZooKeeper(), lockBasePathPath);
        LOGGER.info("OthermsgTozkLoader zookeeper mkdir " + lockBasePathPath + " success");
    }
    return true;
}