org.apache.curator.framework.api.transaction.CuratorTransactionBridge Java Examples

The following examples show how to use org.apache.curator.framework.api.transaction.CuratorTransactionBridge. 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: JobNodeStorageTest.java    From shardingsphere-elasticjob-lite with Apache License 2.0 6 votes vote down vote up
@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 #2
Source File: JobNodeStorageTest.java    From shardingsphere-elasticjob-lite with Apache License 2.0 6 votes vote down vote up
@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 #3
Source File: JobHistoryZKStateManager.java    From eagle with Apache License 2.0 6 votes vote down vote up
public void addFinishedApplication(String appId, String queue, String yarnState, String yarnStatus, String user, String name) {
    String path = zkRoot + "/jobs/" + appId;
    try {
        if (curator.checkExists().forPath(path) != null) {
            curator.delete().deletingChildrenIfNeeded().forPath(path);
        }

        name = name.replace("/", "_");
        if (name.length() > 50) {
            name = name.substring(0, 50);
        }

        CuratorTransactionBridge result =  curator.inTransaction().create().withMode(CreateMode.PERSISTENT).forPath(path, ZKStateConstant.AppStatus.INIT.toString().getBytes("UTF-8"));
        result = result.and().create().withMode(CreateMode.PERSISTENT).forPath(path + "/info", String.format("%s/%s/%s/%s/%s", queue, yarnState, yarnStatus, user, name).getBytes("UTF-8"));

        result.and().commit();
    } catch (Exception e) {
        LOG.error("fail adding finished application", e);
        throw new RuntimeException(e);
    }
}
 
Example #4
Source File: ShardingServiceTest.java    From shardingsphere-elasticjob-lite with Apache License 2.0 5 votes vote down vote up
@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 #5
Source File: DefaultZookeeperService.java    From hermes with Apache License 2.0 4 votes vote down vote up
@Override
public void persistBulk(Map<String, byte[]> pathAndDatas) throws Exception {
	if (pathAndDatas != null && !pathAndDatas.isEmpty()) {
		try {
			CuratorTransaction transaction = null;
			CuratorTransactionBridge bridge = null;

			int uncommittedCount = 0;

			for (Map.Entry<String, byte[]> pathAndData : pathAndDatas.entrySet()) {
				String path = pathAndData.getKey();
				byte[] data = pathAndData.getValue();

				ensurePath(path);

				if (transaction == null) {
					transaction = m_zkClient.get().inTransaction();
					bridge = transaction.setData().forPath(path, data);
				} else {
					bridge.and().setData().forPath(path, data);
				}

				uncommittedCount++;

				if (uncommittedCount % BULK_PERSISTENCE_MAX_SIZE == 0) {
					bridge.and().commit();
					transaction = null;
					bridge = null;
					uncommittedCount = 0;
				}
			}

			if (uncommittedCount > 0) {
				bridge.and().commit();
			}
		} catch (Exception e) {
			log.error("Exception occurred in persist", e);
			throw e;
		}
	}
}
 
Example #6
Source File: MockCurator.java    From vespa with Apache License 2.0 4 votes vote down vote up
@Override
public PathAndBytesable<CuratorTransactionBridge> withACL(List<ACL> list) {
    throw new UnsupportedOperationException("Not implemented in MockCurator");
}
 
Example #7
Source File: MockCurator.java    From vespa with Apache License 2.0 4 votes vote down vote up
@Override
public ACLPathAndBytesable<CuratorTransactionBridge> compressed() {
    throw new UnsupportedOperationException("Not implemented in MockCurator");
}
 
Example #8
Source File: MockCurator.java    From vespa with Apache License 2.0 4 votes vote down vote up
@Override
public ACLPathAndBytesable<CuratorTransactionBridge> withMode(CreateMode createMode) {
    this.createMode = createMode;
    return this;
}
 
Example #9
Source File: MockCurator.java    From vespa with Apache License 2.0 4 votes vote down vote up
@Override
public CuratorTransactionBridge forPath(String s, byte[] bytes) throws Exception {
    createNode(s, bytes, false, createMode, newRoot, delayedListener);
    return new MockCuratorTransactionBridge();
}
 
Example #10
Source File: MockCurator.java    From vespa with Apache License 2.0 4 votes vote down vote up
@Override
public CuratorTransactionBridge forPath(String s) throws Exception {
    createNode(s, new byte[0], false, createMode, newRoot, delayedListener);
    return new MockCuratorTransactionBridge();
}
 
Example #11
Source File: MockCurator.java    From vespa with Apache License 2.0 4 votes vote down vote up
@Override
public Pathable<CuratorTransactionBridge> withVersion(int i) {
    throw new UnsupportedOperationException("Not implemented in MockCurator");
}
 
Example #12
Source File: MockCurator.java    From vespa with Apache License 2.0 4 votes vote down vote up
@Override
public CuratorTransactionBridge forPath(String path) throws Exception {
    deleteNode(path, false, newRoot, delayedListener);
    return new MockCuratorTransactionBridge();
}
 
Example #13
Source File: MockCurator.java    From vespa with Apache License 2.0 4 votes vote down vote up
@Override
public PathAndBytesable<CuratorTransactionBridge> compressed() {
    throw new UnsupportedOperationException("Not implemented in MockCurator");
}
 
Example #14
Source File: MockCurator.java    From vespa with Apache License 2.0 4 votes vote down vote up
@Override
public PathAndBytesable<CuratorTransactionBridge> withVersion(int i) {
    throw new UnsupportedOperationException("Not implemented in MockCurator");
}
 
Example #15
Source File: MockCurator.java    From vespa with Apache License 2.0 4 votes vote down vote up
@Override
public CuratorTransactionBridge forPath(String s, byte[] bytes) throws Exception {
    MockCurator.this.setData(s, bytes, newRoot, delayedListener);
    return new MockCuratorTransactionBridge();
}
 
Example #16
Source File: MockCurator.java    From vespa with Apache License 2.0 4 votes vote down vote up
@Override
public CuratorTransactionBridge forPath(String s) throws Exception {
    MockCurator.this.setData(s, new byte[0], newRoot, delayedListener);
    return new MockCuratorTransactionBridge();
}