Search in sources :

Example 11 with CuratorTransactionFinal

use of org.apache.curator.framework.api.transaction.CuratorTransactionFinal in project helios by spotify.

the class DefaultZooKeeperClient method deleteRecursive.

@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);
    }
}
Also used : CuratorTransactionFinal(org.apache.curator.framework.api.transaction.CuratorTransactionFinal) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) KeeperException(org.apache.zookeeper.KeeperException)

Example 12 with CuratorTransactionFinal

use of org.apache.curator.framework.api.transaction.CuratorTransactionFinal in project elastic-job by dangdangdotcom.

the class JobNodeStorage method executeInTransaction.

/**
     * 在事务中执行操作.
     * 
     * @param 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);
    }
}
Also used : CuratorTransactionFinal(org.apache.curator.framework.api.transaction.CuratorTransactionFinal) JobSystemException(com.dangdang.ddframe.job.exception.JobSystemException)

Example 13 with CuratorTransactionFinal

use of org.apache.curator.framework.api.transaction.CuratorTransactionFinal in project elastic-job by dangdangdotcom.

the class ShardingServiceTest method assertPersistShardingInfoTransactionExecutionCallback.

@Test
public void assertPersistShardingInfoTransactionExecutionCallback() throws Exception {
    CuratorTransactionFinal curatorTransactionFinal = mock(CuratorTransactionFinal.class);
    TransactionCreateBuilder transactionCreateBuilder = mock(TransactionCreateBuilder.class);
    TransactionDeleteBuilder transactionDeleteBuilder = mock(TransactionDeleteBuilder.class);
    CuratorTransactionBridge curatorTransactionBridge = mock(CuratorTransactionBridge.class);
    when(curatorTransactionFinal.create()).thenReturn(transactionCreateBuilder);
    when(transactionCreateBuilder.forPath("/test_job/servers/host0/sharding", "0,1,2".getBytes())).thenReturn(curatorTransactionBridge);
    when(curatorTransactionBridge.and()).thenReturn(curatorTransactionFinal);
    when(curatorTransactionFinal.delete()).thenReturn(transactionDeleteBuilder);
    when(transactionDeleteBuilder.forPath("/test_job/leader/sharding/necessary")).thenReturn(curatorTransactionBridge);
    when(curatorTransactionBridge.and()).thenReturn(curatorTransactionFinal);
    when(curatorTransactionFinal.delete()).thenReturn(transactionDeleteBuilder);
    when(transactionDeleteBuilder.forPath("/test_job/leader/sharding/processing")).thenReturn(curatorTransactionBridge);
    when(curatorTransactionBridge.and()).thenReturn(curatorTransactionFinal);
    Map<String, List<Integer>> shardingItems = new HashMap<>(1);
    shardingItems.put("host0", Arrays.asList(0, 1, 2));
    ShardingService.PersistShardingInfoTransactionExecutionCallback actual = shardingService.new PersistShardingInfoTransactionExecutionCallback(shardingItems);
    actual.execute(curatorTransactionFinal);
    verify(curatorTransactionFinal).create();
    verify(transactionCreateBuilder).forPath("/test_job/servers/host0/sharding", "0,1,2".getBytes());
    verify(curatorTransactionFinal, times(2)).delete();
    verify(transactionDeleteBuilder).forPath("/test_job/leader/sharding/necessary");
    verify(transactionDeleteBuilder).forPath("/test_job/leader/sharding/processing");
    verify(curatorTransactionBridge, times(3)).and();
}
Also used : TransactionCreateBuilder(org.apache.curator.framework.api.transaction.TransactionCreateBuilder) CuratorTransactionBridge(org.apache.curator.framework.api.transaction.CuratorTransactionBridge) HashMap(java.util.HashMap) CuratorTransactionFinal(org.apache.curator.framework.api.transaction.CuratorTransactionFinal) TransactionDeleteBuilder(org.apache.curator.framework.api.transaction.TransactionDeleteBuilder) List(java.util.List) Test(org.junit.Test)

Example 14 with CuratorTransactionFinal

use of org.apache.curator.framework.api.transaction.CuratorTransactionFinal in project elastic-job by dangdangdotcom.

the class JobNodeStorageTest method assertExecuteInTransactionSuccess.

@Test
public void assertExecuteInTransactionSuccess() throws Exception {
    CuratorFramework client = mock(CuratorFramework.class);
    CuratorTransaction curatorTransaction = mock(CuratorTransaction.class);
    TransactionCheckBuilder transactionCheckBuilder = mock(TransactionCheckBuilder.class);
    CuratorTransactionBridge curatorTransactionBridge = mock(CuratorTransactionBridge.class);
    CuratorTransactionFinal curatorTransactionFinal = mock(CuratorTransactionFinal.class);
    when(regCenter.getRawClient()).thenReturn(client);
    when(client.inTransaction()).thenReturn(curatorTransaction);
    when(curatorTransaction.check()).thenReturn(transactionCheckBuilder);
    when(transactionCheckBuilder.forPath("/")).thenReturn(curatorTransactionBridge);
    when(curatorTransactionBridge.and()).thenReturn(curatorTransactionFinal);
    TransactionCreateBuilder transactionCreateBuilder = mock(TransactionCreateBuilder.class);
    when(curatorTransactionFinal.create()).thenReturn(transactionCreateBuilder);
    when(transactionCreateBuilder.forPath("/test_transaction")).thenReturn(curatorTransactionBridge);
    when(curatorTransactionBridge.and()).thenReturn(curatorTransactionFinal);
    jobNodeStorage.executeInTransaction(new TransactionExecutionCallback() {

        @Override
        public void execute(final CuratorTransactionFinal curatorTransactionFinal) throws Exception {
            curatorTransactionFinal.create().forPath("/test_transaction").and();
        }
    });
    verify(regCenter).getRawClient();
    verify(client).inTransaction();
    verify(curatorTransaction).check();
    verify(transactionCheckBuilder).forPath("/");
    verify(curatorTransactionBridge, times(2)).and();
    verify(curatorTransactionFinal).create();
    verify(transactionCreateBuilder).forPath("/test_transaction");
    verify(curatorTransactionFinal).commit();
}
Also used : TransactionCreateBuilder(org.apache.curator.framework.api.transaction.TransactionCreateBuilder) CuratorTransactionBridge(org.apache.curator.framework.api.transaction.CuratorTransactionBridge) CuratorFramework(org.apache.curator.framework.CuratorFramework) TransactionCheckBuilder(org.apache.curator.framework.api.transaction.TransactionCheckBuilder) CuratorTransaction(org.apache.curator.framework.api.transaction.CuratorTransaction) CuratorTransactionFinal(org.apache.curator.framework.api.transaction.CuratorTransactionFinal) Test(org.junit.Test)

Example 15 with CuratorTransactionFinal

use of org.apache.curator.framework.api.transaction.CuratorTransactionFinal in project Saturn by vipshop.

the class NamespaceShardingContentService method persistJobsNecessaryInTransaction.

public void persistJobsNecessaryInTransaction(Map<String, /* jobName */
Map<String, /* executorName */
List<Integer>>> jobShardContent) throws Exception {
    if (!jobShardContent.isEmpty()) {
        log.info("Notify jobs sharding necessary, jobs is {}", jobShardContent.keySet());
        CuratorTransactionFinal curatorTransactionFinal = curatorFramework.inTransaction().check().forPath("/").and();
        Iterator<Map.Entry<String, Map<String, List<Integer>>>> iterator = jobShardContent.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Map<String, List<Integer>>> next = iterator.next();
            String jobName = next.getKey();
            Map<String, List<Integer>> shardContent = next.getValue();
            String shardContentJson = gson.toJson(shardContent);
            byte[] necessaryContent = shardContentJson.getBytes("UTF-8");
            // 更新$Jobs/xx/leader/sharding/neccessary 节点的内容为新分配的sharding 内容
            String jobLeaderShardingNodePath = SaturnExecutorsNode.getJobLeaderShardingNodePath(jobName);
            String jobLeaderShardingNecessaryNodePath = SaturnExecutorsNode.getJobLeaderShardingNecessaryNodePath(jobName);
            if (curatorFramework.checkExists().forPath(jobLeaderShardingNodePath) == null) {
                curatorFramework.create().creatingParentsIfNeeded().forPath(jobLeaderShardingNodePath);
            }
            if (curatorFramework.checkExists().forPath(jobLeaderShardingNecessaryNodePath) == null) {
                curatorTransactionFinal.create().forPath(jobLeaderShardingNecessaryNodePath, necessaryContent).and();
            } else {
                curatorTransactionFinal.setData().forPath(jobLeaderShardingNecessaryNodePath, necessaryContent).and();
            }
        }
        curatorTransactionFinal.commit();
    }
}
Also used : CuratorTransactionFinal(org.apache.curator.framework.api.transaction.CuratorTransactionFinal) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

CuratorTransactionFinal (org.apache.curator.framework.api.transaction.CuratorTransactionFinal)20 KeeperException (org.apache.zookeeper.KeeperException)8 IOException (java.io.IOException)7 CuratorTransaction (org.apache.curator.framework.api.transaction.CuratorTransaction)7 SchemaConfig (io.mycat.config.model.SchemaConfig)4 TableConfig (io.mycat.config.model.TableConfig)4 CuratorFramework (org.apache.curator.framework.CuratorFramework)4 CoordinatorException (com.emc.storageos.coordinator.exceptions.CoordinatorException)3 RetryableCoordinatorException (com.emc.storageos.coordinator.exceptions.RetryableCoordinatorException)3 UnknownHostException (java.net.UnknownHostException)3 List (java.util.List)3 CuratorTransactionBridge (org.apache.curator.framework.api.transaction.CuratorTransactionBridge)3 TransactionCreateBuilder (org.apache.curator.framework.api.transaction.TransactionCreateBuilder)3 Test (org.junit.Test)3 Configuration (com.emc.storageos.coordinator.common.Configuration)2 PropertyInfoMapper.decodeFromString (com.emc.storageos.coordinator.mapper.PropertyInfoMapper.decodeFromString)2 ClusterInfo (io.mycat.config.loader.zkprocess.zookeeper.ClusterInfo)2 MigrateTask (io.mycat.migrate.MigrateTask)2 TaskNode (io.mycat.migrate.TaskNode)2 AbstractPartitionAlgorithm (io.mycat.route.function.AbstractPartitionAlgorithm)2