Java Code Examples for org.apache.curator.framework.api.transaction.CuratorTransactionFinal#commit()

The following examples show how to use org.apache.curator.framework.api.transaction.CuratorTransactionFinal#commit() . 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: ZkLeaderSelector.java    From binlake with Apache License 2.0 6 votes vote down vote up
/**
 * only startSelector and over can update the counter
 *
 * @param metaInfo
 * @throws Exception
 */
public void updateCounter(MetaInfo metaInfo) throws Exception {
    LogUtils.debug.debug("updateCounter");
    metaInfo.refreshCounter();
    CuratorFramework client = this.client;

    if (client != null) {
        CuratorTransaction trx = client.inTransaction();
        CuratorTransactionFinal trf = trx.setData().forPath(counterPath, Meta.Counter.marshalJson(metaInfo.getCounter())).and();

        Meta.Error err = null;
        if ((err = metaInfo.getError()) != null) {
            trf = trf.setData().forPath(errorPath, Meta.Error.marshalJson(err)).and();
        }
        trf.commit();
    }
}
 
Example 2
Source File: DefaultZooKeeperClient.java    From helios with Apache License 2.0 6 votes vote down vote up
@Override
public void deleteRecursive(final String path) throws KeeperException {
  assertClusterIdFlagTrue();
  try {
    final List<String> nodes = listRecursive(path);
    if (nodes.isEmpty()) {
      return;
    }
    final CuratorTransactionFinal t = client.inTransaction().check().forPath(path).and();
    for (final String node : reverse(nodes)) {
      t.delete().forPath(node).and();
    }
    t.commit();
  } catch (Exception e) {
    throwIfInstanceOf(e, KeeperException.class);
    throw new RuntimeException(e);
  }
}
 
Example 3
Source File: ZkService.java    From binlake with Apache License 2.0 5 votes vote down vote up
/**
 * @param c:        curator client
 * @param delPaths: path to delete
 * @param newDbKVs: new db key values
 * @throws Exception
 */
private void refreshDbInfo(CuratorFramework c, List<String> delPaths, Map<String, byte[]> newDbKVs) throws Exception {
    if (delPaths.size() == 0 && newDbKVs.size() == 0) {
        return;
    }

    // transaction
    CuratorTransaction trx = c.inTransaction();
    CuratorTransactionFinal trxf = null;

    for (String p : delPaths) {
        trxf = trx.delete().forPath(p).and();
    }

    for (Map.Entry<String, byte[]> entry : newDbKVs.entrySet()) {
        if (c.checkExists().forPath(entry.getKey()) == null) {
            // node not exist then create
            if (logger.isDebugEnabled()) {
                logger.warn("path " + entry.getKey() + " not exit under zookeeper node " + zkPath);
            }
            trxf = trx.create().forPath(entry.getKey(), entry.getValue()).and();
            continue;
        }
        trxf = trx.setData().forPath(entry.getKey(), entry.getValue()).and();
    }

    trxf.commit();
}
 
Example 4
Source File: ZkService.java    From binlake with Apache License 2.0 5 votes vote down vote up
/**
 * 批量创建MySQL dump实例
 *
 * @param ms : meta data list
 * @throws Exception
 */
public void batchCreate(List<Meta.MetaData> ms) throws Exception {
    if (ms.size() == 0) {
        logger.warn("no meta info exist on creating znode ");
        return;
    }

    // each time we push one into transaction for share
    Meta.Counter counter = new Meta.Counter().setKillTimes(0).setRetryTimes(0);
    byte[] cbts = Meta.Counter.marshalJson(counter);

    // transaction
    CuratorTransaction trx = client.inTransaction();
    CuratorTransactionFinal trxf = null;

    for (Meta.MetaData metaInfo : ms) {
        // parent path
        String path = zkPath + ApiCenter.makeZNodePath(metaInfo.getDbInfo().getHost(),
                metaInfo.getDbInfo().getPort() + "");

        // create db info node
        Meta.DbInfo dbInfo = metaInfo.getDbInfo();
        for (Map.Entry<String, byte[]> entry : MetaUtils.dbBytesMap(zkPath, dbInfo).entrySet()) {
            logger.debug("create path for " + entry.getKey());
            trxf = trx.create().forPath(entry.getKey(), entry.getValue()).and();
        }

        // other nodes
        Meta.Candidate candidate = new Meta.Candidate().setHost(metaInfo.getCandidate());
        trxf = trx.create().forPath(path + ConstUtils.ZK_DYNAMIC_PATH, Meta.BinlogInfo.marshalJson(metaInfo.getSlave()))
                .and().create().forPath(path + ConstUtils.ZK_COUNTER_PATH, cbts)
                .and().create().forPath(path + ConstUtils.ZK_ERROR_PATH, Meta.Error.marshalJson(Meta.Error.defalut()))
                .and().create().forPath(path + ConstUtils.ZK_ALARM_PATH, Meta.Alarm.marshalJson(metaInfo.getAlarm()))
                .and().create().forPath(path + ConstUtils.ZK_CANDIDATE_PATH, Meta.Candidate.marshalJson(candidate)).and();
    }

    // commit transaction final
    trxf.commit();
}
 
Example 5
Source File: ZkLeaderSelector.java    From binlake with Apache License 2.0 5 votes vote down vote up
/**
 * update candidates
 *
 * @param cans
 */
private void updateCandidate(byte[] cans, String leader) throws Exception {
    LogUtils.debug.debug("updateCandidate");
    CuratorFramework client = this.client;

    if (client != null) {
        CuratorTransaction trx = client.inTransaction();
        CuratorTransactionFinal ctf = trx.setData().forPath(candidatePath, cans).and();
        if (!StringUtils.equals("", leader)) {
            ctf = ctf.setData().forPath(leaderPath, leader.getBytes()).and();
        }
        // commit
        ctf.commit();
    }
}
 
Example 6
Source File: JobNodeStorage.java    From shardingsphere-elasticjob-lite with Apache License 2.0 5 votes vote down vote up
/**
 * Execute operator in transaction.
 * 
 * @param callback execute callback
 */
public void executeInTransaction(final TransactionExecutionCallback callback) {
    try {
        CuratorTransactionFinal curatorTransactionFinal = getClient().inTransaction().check().forPath("/").and();
        callback.execute(curatorTransactionFinal);
        curatorTransactionFinal.commit();
    //CHECKSTYLE:OFF
    } catch (final Exception ex) {
    //CHECKSTYLE:ON
        RegExceptionHandler.handleException(ex);
    }
}
 
Example 7
Source File: DruidBatchStatus.java    From storm-example with Apache License 2.0 5 votes vote down vote up
public void complete(List<Long> txIds) throws Exception {
    Iterator<Long> iterator = txIds.iterator();
    CuratorTransaction transaction = curatorFramework.inTransaction();
    while (iterator.hasNext()) {
        Long txId = iterator.next();
        transaction = transaction.delete().forPath(LIMBO_PATH + "/" + txId)
                .and().create().forPath(COMPLETED_PATH + "/" + txId).and();
    }
    CuratorTransactionFinal tx = (CuratorTransactionFinal) transaction;
    tx.commit();
}
 
Example 8
Source File: DruidPartitionStatus.java    From storm-example with Apache License 2.0 5 votes vote down vote up
public void complete(List<String> partitionIds) throws Exception {
    Iterator<String> iterator = partitionIds.iterator();
    CuratorTransaction transaction = curatorFramework.inTransaction();
    while (iterator.hasNext()) {
        String partitionId = iterator.next();
        transaction = transaction.delete().forPath(LIMBO_PATH + "/" + partitionId)
                .and().create().forPath(COMPLETED_PATH + "/" + partitionId).and();
    }
    CuratorTransactionFinal tx = (CuratorTransactionFinal) transaction;
    tx.commit();
}
 
Example 9
Source File: TransactionExamples.java    From xian with Apache License 2.0 4 votes vote down vote up
public static void                      commitTransaction(CuratorTransactionFinal transaction) throws Exception
{
    // commit the transaction
    transaction.commit();
}