org.apache.curator.framework.api.transaction.CuratorTransactionFinal Java Examples
The following examples show how to use
org.apache.curator.framework.api.transaction.CuratorTransactionFinal.
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: DefaultZooKeeperClient.java From helios with Apache License 2.0 | 6 votes |
@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 #2
Source File: ZkLeaderSelector.java From binlake with Apache License 2.0 | 6 votes |
/** * 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 #3
Source File: JobNodeStorageTest.java From shardingsphere-elasticjob-lite with Apache License 2.0 | 6 votes |
@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); when(curatorTransactionBridge.and()).thenThrow(new RuntimeException()); jobNodeStorage.executeInTransaction(curatorTransactionFinalForCallback -> curatorTransactionFinalForCallback.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(curatorTransactionFinal, times(0)).commit(); }
Example #4
Source File: JobNodeStorageTest.java From shardingsphere-elasticjob-lite with Apache License 2.0 | 6 votes |
@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(curatorTransactionFinalForCallback -> curatorTransactionFinalForCallback.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(); }
Example #5
Source File: ZkService.java From binlake with Apache License 2.0 | 5 votes |
/** * 批量创建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 #6
Source File: DruidPartitionStatus.java From storm-example with Apache License 2.0 | 5 votes |
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 #7
Source File: DruidBatchStatus.java From storm-example with Apache License 2.0 | 5 votes |
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: ShardingServiceTest.java From shardingsphere-elasticjob-lite with Apache License 2.0 | 5 votes |
@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/sharding/0/instance", "host0@-@0".getBytes())).thenReturn(curatorTransactionBridge); when(transactionCreateBuilder.forPath("/test_job/sharding/1/instance", "host0@-@0".getBytes())).thenReturn(curatorTransactionBridge); when(transactionCreateBuilder.forPath("/test_job/sharding/2/instance", "host0@-@0".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<JobInstance, List<Integer>> shardingResult = new HashMap<>(); shardingResult.put(new JobInstance("host0@-@0"), Arrays.asList(0, 1, 2)); ShardingService.PersistShardingInfoTransactionExecutionCallback actual = shardingService.new PersistShardingInfoTransactionExecutionCallback(shardingResult); actual.execute(curatorTransactionFinal); verify(curatorTransactionFinal, times(3)).create(); verify(curatorTransactionFinal, times(2)).delete(); verify(transactionDeleteBuilder).forPath("/test_job/leader/sharding/necessary"); verify(transactionDeleteBuilder).forPath("/test_job/leader/sharding/processing"); verify(curatorTransactionBridge, times(5)).and(); }
Example #9
Source File: JobNodeStorage.java From shardingsphere-elasticjob-lite with Apache License 2.0 | 5 votes |
/** * 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 #10
Source File: ShardingService.java From shardingsphere-elasticjob-lite with Apache License 2.0 | 5 votes |
@Override public void execute(final CuratorTransactionFinal curatorTransactionFinal) throws Exception { for (Map.Entry<JobInstance, List<Integer>> entry : shardingResults.entrySet()) { for (int shardingItem : entry.getValue()) { curatorTransactionFinal.create().forPath(jobNodePath.getFullPath(ShardingNode.getInstanceNode(shardingItem)), entry.getKey().getJobInstanceId().getBytes()).and(); } } curatorTransactionFinal.delete().forPath(jobNodePath.getFullPath(ShardingNode.NECESSARY)).and(); curatorTransactionFinal.delete().forPath(jobNodePath.getFullPath(ShardingNode.PROCESSING)).and(); }
Example #11
Source File: Curator.java From vespa with Apache License 2.0 | 5 votes |
/** * Creates all the given paths in a single transaction. Any paths which already exists are ignored. */ public void createAtomically(Path... paths) { try { CuratorTransaction transaction = framework().inTransaction(); for (Path path : paths) { if ( ! exists(path)) { transaction = transaction.create().forPath(path.getAbsolute(), new byte[0]).and(); } } ((CuratorTransactionFinal)transaction).commit(); } catch (Exception e) { throw new RuntimeException("Could not create " + Arrays.toString(paths), e); } }
Example #12
Source File: CuratorTransaction.java From vespa with Apache License 2.0 | 5 votes |
/** Commits this transaction. If it is not already prepared this will prepare it first */ @Override public void commit() { try { if ( ! prepared) prepare(); org.apache.curator.framework.api.transaction.CuratorTransaction transaction = curator.framework().inTransaction(); for (Operation operation : operations()) { transaction = ((CuratorOperation)operation).and(transaction); } ((CuratorTransactionFinal) transaction).commit(); } catch (Exception e) { throw new IllegalStateException(e); } }
Example #13
Source File: SwitchCommitListener.java From Mycat2 with GNU General Public License v3.0 | 5 votes |
private static CuratorTransactionFinal modifyTableConfigRules(CuratorTransactionFinal transactionFinal, String schemal, String table, List<String> newDataNodes) throws Exception { CuratorFramework client = ZKUtils.getConnection(); String rulePath = ZKUtils.getZKBasePath() + "schema/schema"; TypeToken<List<Schema>> typeToken = new TypeToken<List<Schema>>() { }; List<Schema> schemas = new Gson().fromJson(new String(client.getData().forPath(rulePath), "UTF-8"),typeToken.getType()); for (Schema func : schemas) { if (schemal.equalsIgnoreCase(func.getName())) { List<Table> property = func.getTable(); for (Table o : property) { Table tt = (Table) o; String tableName = tt.getName(); String dataNode = tt.getDataNode(); if (table.equalsIgnoreCase(tableName)) { List<String> allDataNodes = new ArrayList<>(); allDataNodes.add(dataNode); allDataNodes.addAll(newDataNodes); tt.setDataNode( Joiner.on(",").join(allDataNodes)); if (transactionFinal == null) { transactionFinal = ZKUtils.getConnection().inTransaction().setData().forPath(rulePath, JSONUtils.toJSONString(schemas).getBytes()).and(); } else { transactionFinal.setData().forPath(rulePath, JSONUtils.toJSONString(schemas).getBytes()); } } } } } return transactionFinal; }
Example #14
Source File: SwitchCommitListener.java From Mycat2 with GNU General Public License v3.0 | 5 votes |
private static CuratorTransactionFinal modifyZkRules(CuratorTransactionFinal transactionFinal, String ruleName, List<String> newDataNodes) throws Exception { CuratorFramework client = ZKUtils.getConnection(); String rulePath = ZKUtils.getZKBasePath() + "rules/function"; TypeToken<List<Function>> typeToken = new TypeToken<List<Function>>() { }; List<Function> jsonArray = new Gson().fromJson(new String(client.getData().forPath(rulePath), "UTF-8"),typeToken.getType()); for (Function obj : jsonArray) { Function func = (Function) obj; if (ruleName.equalsIgnoreCase(func.getName())) { List<Property> property = func.getProperty(); for (Property o : property) { Property count = (Property) o; if ("count".equals(count.getName())) { Integer xcount = Integer.parseInt(count.getValue()); count.setValue( String.valueOf(xcount + newDataNodes.size())); if (transactionFinal == null) { transactionFinal = ZKUtils.getConnection().inTransaction().setData().forPath(rulePath, JSONUtils.toJSONString(jsonArray).getBytes()).and(); } else { transactionFinal.setData().forPath(rulePath, JSONUtils.toJSONString(jsonArray).getBytes()); } } } } } return transactionFinal; }
Example #15
Source File: ZkLeaderSelector.java From binlake with Apache License 2.0 | 5 votes |
/** * 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 #16
Source File: ZkService.java From binlake with Apache License 2.0 | 5 votes |
/** * @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 #17
Source File: MockCurator.java From vespa with Apache License 2.0 | 4 votes |
@Override public CuratorTransactionFinal and() { return MockCuratorTransactionFinal.this; }
Example #18
Source File: TransactionExamples.java From xian with Apache License 2.0 | 4 votes |
public static void commitTransaction(CuratorTransactionFinal transaction) throws Exception { // commit the transaction transaction.commit(); }
Example #19
Source File: TransactionExamples.java From xian with Apache License 2.0 | 4 votes |
public static CuratorTransactionFinal addDeleteToTransaction(CuratorTransaction transaction) throws Exception { // add a delete operation return transaction.delete().forPath("/another/path").and(); }
Example #20
Source File: TransactionExamples.java From xian with Apache License 2.0 | 4 votes |
public static CuratorTransactionFinal addCreateToTransaction(CuratorTransaction transaction) throws Exception { // add a create operation return transaction.create().forPath("/a/path", "some data".getBytes()).and(); }
Example #21
Source File: CuratorTransactionImpl.java From xian with Apache License 2.0 | 4 votes |
@Override public CuratorTransactionFinal and() { return this; }
Example #22
Source File: TransactionExecutionCallback.java From shardingsphere-elasticjob-lite with Apache License 2.0 | 2 votes |
/** * Execute in transaction. * * @param curatorTransactionFinal transaction execution context * @throws Exception exception */ void execute(CuratorTransactionFinal curatorTransactionFinal) throws Exception;