Search in sources :

Example 1 with CuratorTransactionFinal

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

the class JobNodeStorageTest method assertExecuteInTransactionFailure.

@Test(expected = RuntimeException.class)
public void assertExecuteInTransactionFailure() 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()).thenThrow(new RuntimeException());
    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, times(0)).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 2 with CuratorTransactionFinal

use of org.apache.curator.framework.api.transaction.CuratorTransactionFinal in project druid by druid-io.

the class Announcer method stop.

@LifecycleStop
public void stop() {
    synchronized (toAnnounce) {
        if (!started) {
            return;
        }
        started = false;
        Closer closer = Closer.create();
        for (PathChildrenCache cache : listeners.values()) {
            closer.register(cache);
        }
        try {
            CloseQuietly.close(closer);
        } finally {
            pathChildrenCacheExecutor.shutdown();
        }
        for (Map.Entry<String, ConcurrentMap<String, byte[]>> entry : announcements.entrySet()) {
            String basePath = entry.getKey();
            for (String announcementPath : entry.getValue().keySet()) {
                unannounce(ZKPaths.makePath(basePath, announcementPath));
            }
        }
        if (!parentsIBuilt.isEmpty()) {
            CuratorTransaction transaction = curator.inTransaction();
            for (String parent : parentsIBuilt) {
                try {
                    transaction = transaction.delete().forPath(parent).and();
                } catch (Exception e) {
                    log.info(e, "Unable to delete parent[%s], boooo.", parent);
                }
            }
            try {
                ((CuratorTransactionFinal) transaction).commit();
            } catch (Exception e) {
                log.info(e, "Unable to commit transaction. Please feed the hamsters");
            }
        }
    }
}
Also used : Closer(com.google.common.io.Closer) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) CuratorTransaction(org.apache.curator.framework.api.transaction.CuratorTransaction) CuratorTransactionFinal(org.apache.curator.framework.api.transaction.CuratorTransactionFinal) ConcurrentMap(java.util.concurrent.ConcurrentMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) KeeperException(org.apache.zookeeper.KeeperException) LifecycleStop(io.druid.java.util.common.lifecycle.LifecycleStop)

Example 3 with CuratorTransactionFinal

use of org.apache.curator.framework.api.transaction.CuratorTransactionFinal in project coprhd-controller by CoprHD.

the class CoordinatorClientImpl method persistServiceConfiguration.

@Override
public void persistServiceConfiguration(String siteId, Configuration... configs) throws CoordinatorException {
    try {
        for (Configuration config : configs) {
            String configParentPath = getKindPath(siteId, config.getKind());
            EnsurePath path = new EnsurePath(configParentPath);
            path.ensure(_zkConnection.curator().getZookeeperClient());
            String servicePath = String.format("%1$s/%2$s", configParentPath, config.getId());
            Stat stat = _zkConnection.curator().checkExists().forPath(servicePath);
            CuratorTransaction handler = zkTransactionHandler.get();
            if (stat != null) {
                if (handler != null) {
                    CuratorTransactionFinal tx = handler.setData().forPath(servicePath, config.serialize()).and();
                    zkTransactionHandler.set(tx);
                } else {
                    _zkConnection.curator().setData().forPath(servicePath, config.serialize());
                }
            } else {
                if (handler != null) {
                    CuratorTransactionFinal tx = handler.create().forPath(servicePath, config.serialize()).and();
                    zkTransactionHandler.set(tx);
                } else {
                    _zkConnection.curator().create().forPath(servicePath, config.serialize());
                }
            }
        }
    } catch (final Exception e) {
        log.error("Failed to persist service configuration e=", e);
        throw CoordinatorException.fatals.unableToPersistTheConfiguration(e);
    }
}
Also used : EnsurePath(org.apache.curator.utils.EnsurePath) Stat(org.apache.zookeeper.data.Stat) Configuration(com.emc.storageos.coordinator.common.Configuration) CuratorTransaction(org.apache.curator.framework.api.transaction.CuratorTransaction) CuratorTransactionFinal(org.apache.curator.framework.api.transaction.CuratorTransactionFinal) PropertyInfoMapper.decodeFromString(com.emc.storageos.coordinator.mapper.PropertyInfoMapper.decodeFromString) CoordinatorException(com.emc.storageos.coordinator.exceptions.CoordinatorException) RetryableCoordinatorException(com.emc.storageos.coordinator.exceptions.RetryableCoordinatorException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 4 with CuratorTransactionFinal

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

the class ShardingService method shardingIfNecessary.

/**
 * 如果需要分片且当前节点为主节点, 则作业分片.
 */
public synchronized void shardingIfNecessary() throws JobShuttingDownException {
    if (isShutdown) {
        return;
    }
    GetDataStat getDataStat = null;
    if (getJobNodeStorage().isJobNodeExisted(ShardingNode.NECESSARY)) {
        getDataStat = getNecessaryDataStat();
    }
    // sharding neccessary内容为空,或者内容是"0"则返回,否则,需要进行sharding处理
    if (getDataStat == null || SHARDING_UN_NECESSARY.equals(getDataStat.getData())) {
        return;
    }
    // 如果不是leader,则等待leader处理完成(这也是一个死循环,知道满足跳出循环的条件:1. 被shutdown 2. 无须sharding而且不处于processing状态)
    if (blockUntilShardingComplatedIfNotLeader()) {
        return;
    }
    // 如果有作业分片处于running状态则等待(无限期)
    waitingOtherJobCompleted();
    // 建立一个临时节点,标记shardig处理中
    getJobNodeStorage().fillEphemeralJobNode(ShardingNode.PROCESSING, "");
    try {
        // 删除作业下面的所有JobServer的sharding节点
        clearShardingInfo();
        int retryCount = 3;
        while (!isShutdown) {
            boolean needRetry = false;
            int version = getDataStat.getVersion();
            // 首先尝试从job/leader/sharding/neccessary节点获取,如果失败,会从$SaturnExecutors/sharding/content下面获取
            // key is executor, value is sharding items
            Map<String, List<Integer>> shardingItems = namespaceShardingContentService.getShardContent(jobName, getDataStat.getData());
            try {
                // 所有jobserver的(检查+创建),加上设置sharding necessary内容为0,都是一个事务
                CuratorTransactionFinal curatorTransactionFinal = getJobNodeStorage().getClient().inTransaction().check().forPath("/").and();
                for (Entry<String, List<Integer>> entry : shardingItems.entrySet()) {
                    curatorTransactionFinal.create().forPath(JobNodePath.getNodeFullPath(jobName, ShardingNode.getShardingNode(entry.getKey())), ItemUtils.toItemsString(entry.getValue()).getBytes(StandardCharsets.UTF_8)).and();
                }
                curatorTransactionFinal.setData().withVersion(version).forPath(JobNodePath.getNodeFullPath(jobName, ShardingNode.NECESSARY), SHARDING_UN_NECESSARY.getBytes(StandardCharsets.UTF_8)).and();
                curatorTransactionFinal.commit();
            } catch (BadVersionException e) {
                log.warn(String.format(SaturnConstant.LOG_FORMAT_FOR_STRING, jobName, "zookeeper bad version exception happens."), e);
                needRetry = true;
                retryCount--;
            } catch (Exception e) {
                log.error(String.format(SaturnConstant.LOG_FORMAT_FOR_STRING, jobName, "Commit shards failed"), e);
            }
            if (needRetry) {
                if (retryCount >= 0) {
                    log.info(SaturnConstant.LOG_FORMAT, jobName, "Bad version because of concurrency, will retry to get shards later");
                    // NOSONAR
                    Thread.sleep(200L);
                    getDataStat = getNecessaryDataStat();
                } else {
                    log.warn(SaturnConstant.LOG_FORMAT, jobName, "Bad version because of concurrency, give up to retry");
                    break;
                }
            } else {
                break;
            }
        }
    } catch (Exception e) {
        log.error(String.format(SaturnConstant.LOG_FORMAT_FOR_STRING, jobName, e.getMessage()), e);
    } finally {
        getJobNodeStorage().removeJobNodeIfExisted(ShardingNode.PROCESSING);
    }
}
Also used : CuratorTransactionFinal(org.apache.curator.framework.api.transaction.CuratorTransactionFinal) List(java.util.List) BadVersionException(org.apache.zookeeper.KeeperException.BadVersionException) BadVersionException(org.apache.zookeeper.KeeperException.BadVersionException) JobShuttingDownException(com.vip.saturn.job.exception.JobShuttingDownException)

Example 5 with CuratorTransactionFinal

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

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) JobException(com.vip.saturn.job.exception.JobException)

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