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();
}
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");
}
}
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations