org.apache.zookeeper.Transaction Java Examples

The following examples show how to use org.apache.zookeeper.Transaction. 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: AutoCommitTransactionWrapperTest.java    From zkcopy with Apache License 2.0 6 votes vote down vote up
@Test
public void testManualCommit() throws InterruptedException, KeeperException {
    ZooKeeper mockZK = mock(ZooKeeper.class);
    Transaction transaction = mock(Transaction.class);
    when(mockZK.transaction()).thenReturn(transaction);
    AutoCommitTransactionWrapper wrapper = new AutoCommitTransactionWrapper(mockZK, TRANSACTION_SIZE);
    for(int i = 0; i < TRANSACTION_SIZE - 1; i++) {
        wrapper.create("/test/blah", new byte[] {0x0, 0x0}, null, CreateMode.PERSISTENT);
    }
    verify(transaction, never()).commit();
    
    wrapper.commit();
    
    verify(transaction, times(1)).commit();

}
 
Example #2
Source File: ZookeeperTransactionUnitTest.java    From Scribengin with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testInvalidTransactionOperation() throws Exception {
  zkClient.create("/transaction", "transaction".getBytes(), OPEN_ACL, CreateMode.PERSISTENT) ;
  Transaction transaction = zkClient.transaction();
  transaction.create("/transaction/good", new byte[0], OPEN_ACL, CreateMode.PERSISTENT);
  transaction.create("/transaction/bad/nested", new byte[0], OPEN_ACL, CreateMode.PERSISTENT);
  KeeperException expectError = null;
  try {
    transaction.commit();
  } catch(KeeperException ex) {
    expectError = ex ;
  }
  Assert.assertNotNull(expectError);
  Assert.assertTrue(expectError instanceof KeeperException.NoNodeException);
  Assert.assertNull(zkClient.exists("/transaction/good", false));
}
 
Example #3
Source File: TestZKLogStreamMetadataStore.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
private static void createLog(ZooKeeperClient zk, URI uri, String logName, String logIdentifier)
        throws Exception {
    final String logRootPath = getLogRootPath(uri, logName, logIdentifier);
    final String logSegmentsPath = logRootPath + LOGSEGMENTS_PATH;
    final String maxTxIdPath = logRootPath + MAX_TXID_PATH;
    final String lockPath = logRootPath + LOCK_PATH;
    final String readLockPath = logRootPath + READ_LOCK_PATH;
    final String versionPath = logRootPath + VERSION_PATH;
    final String allocationPath = logRootPath + ALLOCATION_PATH;

    Utils.zkCreateFullPathOptimistic(zk, logRootPath, new byte[0],
            zk.getDefaultACL(), CreateMode.PERSISTENT);
    Transaction txn = zk.get().transaction();
    txn.create(logSegmentsPath, DLUtils.serializeLogSegmentSequenceNumber(
                    DistributedLogConstants.UNASSIGNED_LOGSEGMENT_SEQNO),
            zk.getDefaultACL(), CreateMode.PERSISTENT);
    txn.create(maxTxIdPath, DLUtils.serializeTransactionId(0L),
            zk.getDefaultACL(), CreateMode.PERSISTENT);
    txn.create(lockPath, EMPTY_BYTES,
            zk.getDefaultACL(), CreateMode.PERSISTENT);
    txn.create(readLockPath, EMPTY_BYTES,
            zk.getDefaultACL(), CreateMode.PERSISTENT);
    txn.create(versionPath, intToBytes(LAYOUT_VERSION),
            zk.getDefaultACL(), CreateMode.PERSISTENT);
    txn.create(allocationPath, EMPTY_BYTES,
            zk.getDefaultACL(), CreateMode.PERSISTENT);
    txn.commit();
}
 
Example #4
Source File: TestZKLogMetadataForWriter.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
private static void createLog(ZooKeeperClient zk, URI uri, String logName, String logIdentifier)
        throws Exception {
    final String logRootPath = getLogRootPath(uri, logName, logIdentifier);
    final String logSegmentsPath = logRootPath + LOGSEGMENTS_PATH;
    final String maxTxIdPath = logRootPath + MAX_TXID_PATH;
    final String lockPath = logRootPath + LOCK_PATH;
    final String readLockPath = logRootPath + READ_LOCK_PATH;
    final String versionPath = logRootPath + VERSION_PATH;
    final String allocationPath = logRootPath + ALLOCATION_PATH;

    Utils.zkCreateFullPathOptimistic(zk, logRootPath, new byte[0],
            zk.getDefaultACL(), CreateMode.PERSISTENT);
    Transaction txn = zk.get().transaction();
    txn.create(logSegmentsPath, DLUtils.serializeLogSegmentSequenceNumber(
                    DistributedLogConstants.UNASSIGNED_LOGSEGMENT_SEQNO),
            zk.getDefaultACL(), CreateMode.PERSISTENT);
    txn.create(maxTxIdPath, DLUtils.serializeTransactionId(0L),
            zk.getDefaultACL(), CreateMode.PERSISTENT);
    txn.create(lockPath, DistributedLogConstants.EMPTY_BYTES,
            zk.getDefaultACL(), CreateMode.PERSISTENT);
    txn.create(readLockPath, DistributedLogConstants.EMPTY_BYTES,
            zk.getDefaultACL(), CreateMode.PERSISTENT);
    txn.create(versionPath, ZKLogMetadataForWriter.intToBytes(LAYOUT_VERSION),
            zk.getDefaultACL(), CreateMode.PERSISTENT);
    txn.create(allocationPath, DistributedLogConstants.EMPTY_BYTES,
            zk.getDefaultACL(), CreateMode.PERSISTENT);
    txn.commit();
}
 
Example #5
Source File: WriterTest.java    From zkcopy with Apache License 2.0 5 votes vote down vote up
@Before
public void setupMocks() {
    mockZK = mock(ZooKeeper.class);
    mockNode = mock(Node.class);
    mockTransaction = mock(Transaction.class);
    mockStat = mock(Stat.class);
    mockChildNode = mock(Node.class);
    when(mockZK.transaction()).thenReturn(mockTransaction);
    when(mockNode.getAbsolutePath()).thenReturn("/destination/path");
    when(mockNode.getData()).thenReturn(THEDATA);
    when(mockChildNode.getAbsolutePath()).thenReturn("/destination/path/child");
    when(mockChildNode.getData()).thenReturn(THEDATA);
    when(mockNode.getChildren()).thenReturn(Arrays.asList(mockChildNode));
}
 
Example #6
Source File: AutoCommitTransactionWrapperTest.java    From zkcopy with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutoCommit() throws InterruptedException, KeeperException {
    ZooKeeper mockZK = mock(ZooKeeper.class);
    Transaction transaction = mock(Transaction.class);
    when(mockZK.transaction()).thenReturn(transaction);
    AutoCommitTransactionWrapper wrapper = new AutoCommitTransactionWrapper(mockZK, TRANSACTION_SIZE);
    for(int i = 0; i < TRANSACTION_SIZE * 50; i++) {
        wrapper.create("/test/blah", new byte[] {0x0, 0x0}, null, CreateMode.PERSISTENT);
    }
    verify(transaction, times(50)).commit();
}
 
Example #7
Source File: ZookeeperTransactionUnitTest.java    From Scribengin with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testTransaction() throws Exception {
  zkClient.create("/transaction", "transaction".getBytes(), OPEN_ACL, CreateMode.PERSISTENT) ;
  Transaction transaction = zkClient.transaction();
  transaction.create("/transaction/test", new byte[0], OPEN_ACL, CreateMode.PERSISTENT);
  transaction.create("/transaction/test/nested", new byte[0], OPEN_ACL, CreateMode.PERSISTENT);
  transaction.create("/transaction/test/delete", new byte[0], OPEN_ACL, CreateMode.PERSISTENT);
  transaction.delete("/transaction/test/delete", 0);
  Assert.assertNull(zkClient.exists("/transaction/test", false));
  Assert.assertNull(zkClient.exists("/transaction/test/nested", false));
  transaction.commit();
  Assert.assertNotNull(zkClient.exists("/transaction/test", false));
  Assert.assertNotNull(zkClient.exists("/transaction/test/nested", false));
  Assert.assertNull(zkClient.exists("/transaction/test/delete", false));
}
 
Example #8
Source File: TestZKLogStreamMetadataStore.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
private static void createLog(ZooKeeperClient zk,
                              URI uri,
                              String logName,
                              String logIdentifier,
                              int numSegments)
        throws Exception {
    final String logRootPath = getLogRootPath(uri, logName, logIdentifier);
    final String logSegmentsPath = logRootPath + LOGSEGMENTS_PATH;
    final String maxTxIdPath = logRootPath + MAX_TXID_PATH;
    final String lockPath = logRootPath + LOCK_PATH;
    final String readLockPath = logRootPath + READ_LOCK_PATH;
    final String versionPath = logRootPath + VERSION_PATH;
    final String allocationPath = logRootPath + ALLOCATION_PATH;

    Utils.zkCreateFullPathOptimistic(zk, logRootPath, new byte[0],
            zk.getDefaultACL(), CreateMode.PERSISTENT);
    Transaction txn = zk.get().transaction();
    txn.create(logSegmentsPath, DLUtils.serializeLogSegmentSequenceNumber(
                    DistributedLogConstants.UNASSIGNED_LOGSEGMENT_SEQNO),
            zk.getDefaultACL(), CreateMode.PERSISTENT);
    txn.create(maxTxIdPath, DLUtils.serializeTransactionId(0L),
            zk.getDefaultACL(), CreateMode.PERSISTENT);
    txn.create(lockPath, EMPTY_BYTES,
            zk.getDefaultACL(), CreateMode.PERSISTENT);
    txn.create(readLockPath, EMPTY_BYTES,
            zk.getDefaultACL(), CreateMode.PERSISTENT);
    txn.create(versionPath, intToBytes(LAYOUT_VERSION),
            zk.getDefaultACL(), CreateMode.PERSISTENT);
    txn.create(allocationPath, EMPTY_BYTES,
            zk.getDefaultACL(), CreateMode.PERSISTENT);

    for (int i = 0; i < numSegments; i++) {
        LogSegmentMetadata segment = DLMTestUtil.completedLogSegment(
            logSegmentsPath,
            i + 1L,
            1L + i * 1000L,
            (i + 1) * 1000L,
            1000,
            i + 1L,
            999L,
            0L);
        txn.create(
            segment.getZkPath(),
            segment.getFinalisedData().getBytes(UTF_8),
            zk.getDefaultACL(),
            CreateMode.PERSISTENT);
    }

    txn.commit();
}
 
Example #9
Source File: AutoCommitTransactionWrapper.java    From zkcopy with Apache License 2.0 4 votes vote down vote up
@Override
public Transaction create(String path, byte[] data, List<ACL> acl, CreateMode createMode) {
    maybeCommitTransaction();
    return transaction.create(path, data, acl, createMode);
}
 
Example #10
Source File: AutoCommitTransactionWrapper.java    From zkcopy with Apache License 2.0 4 votes vote down vote up
@Override
public Transaction delete(String path, int version) {
    maybeCommitTransaction();
    return transaction.delete(path, version);
}
 
Example #11
Source File: AutoCommitTransactionWrapper.java    From zkcopy with Apache License 2.0 4 votes vote down vote up
@Override
public Transaction check(String path, int version) {
    maybeCommitTransaction();
    return transaction.check(path, version);
}
 
Example #12
Source File: AutoCommitTransactionWrapper.java    From zkcopy with Apache License 2.0 4 votes vote down vote up
@Override
public Transaction setData(String path, byte[] data, int version) {
    maybeCommitTransaction();
    return transaction.setData(path, data, version);
}