Example usage for org.apache.commons.lang3.tuple Pair equals

List of usage examples for org.apache.commons.lang3.tuple Pair equals

Introduction

In this page you can find the example usage for org.apache.commons.lang3.tuple Pair equals.

Prototype

@SuppressWarnings("deprecation") 
@Override
public boolean equals(final Object obj) 

Source Link

Document

Compares this pair to another based on the two elements.

Usage

From source file:com.twitter.distributedlog.lock.TestDistributedLock.java

@Test(timeout = 60000)
public void testCheckWriteLockFailureWhenLockIsAcquiredByOthers() throws Exception {
    String lockPath = "/test-check-write-lock-failure-when-lock-is-acquired-by-others-"
            + System.currentTimeMillis();
    String clientId = "test-check-write-lock-failure";

    createLockPath(zkc.get(), lockPath);

    SessionLockFactory lockFactory0 = createLockFactory(clientId, zkc0);
    ZKDistributedLock lock0 = new ZKDistributedLock(lockStateExecutor, lockFactory0, lockPath, Long.MAX_VALUE,
            NullStatsLogger.INSTANCE);/*  ww w  .j  a  v  a2s .c o m*/
    FutureUtils.result(lock0.asyncAcquire());

    Pair<String, Long> lockId0_1 = ((ZKSessionLock) lock0.getInternalLock()).getLockId();

    List<String> children = getLockWaiters(zkc, lockPath);
    assertEquals(1, children.size());
    assertTrue(lock0.haveLock());
    assertEquals(lockId0_1, Await.result(asyncParseClientID(zkc0.get(), lockPath, children.get(0))));

    // expire the session
    ZooKeeperClientUtils.expireSession(zkc0, zkServers, sessionTimeoutMs);

    // reacquire the lock and wait reacquire completed
    checkLockAndReacquire(lock0, true);

    Pair<String, Long> lockId0_2 = ((ZKSessionLock) lock0.getInternalLock()).getLockId();
    assertFalse("New lock should be created under different session", lockId0_1.equals(lockId0_2));

    children = getLockWaiters(zkc, lockPath);
    assertEquals(1, children.size());
    assertTrue(lock0.haveLock());
    assertEquals(lockId0_2, Await.result(asyncParseClientID(zkc0.get(), lockPath, children.get(0))));

    SessionLockFactory lockFactory = createLockFactory(clientId, zkc);
    final ZKDistributedLock lock1 = new ZKDistributedLock(lockStateExecutor, lockFactory, lockPath,
            Long.MAX_VALUE, NullStatsLogger.INSTANCE);
    final CountDownLatch lockLatch = new CountDownLatch(1);
    Thread lockThread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                FutureUtils.result(lock1.asyncAcquire());
                lockLatch.countDown();
            } catch (IOException e) {
                logger.error("Failed on locking lock1 : ", e);
            }
        }
    }, "lock-thread");
    lockThread.start();

    // ensure lock1 is waiting for lock0
    do {
        Thread.sleep(1);
        children = getLockWaiters(zkc, lockPath);
    } while (children.size() < 2);

    // expire the session
    ZooKeeperClientUtils.expireSession(zkc0, zkServers, sessionTimeoutMs);

    lockLatch.await();
    lockThread.join();

    try {
        checkLockAndReacquire(lock0, true);
        fail("Should fail on checking write lock since lock is acquired by lock1");
    } catch (LockingException le) {
        // expected
    }

    try {
        checkLockAndReacquire(lock0, false);
        fail("Should fail on checking write lock since lock is acquired by lock1");
    } catch (LockingException le) {
        // expected
    }
}

From source file:com.twitter.distributedlog.lock.TestDistributedLock.java

private void testLockReacquireSuccess(boolean checkOwnershipAndReacquire) throws Exception {
    String lockPath = "/test-lock-re-acquire-success-" + checkOwnershipAndReacquire + "-"
            + System.currentTimeMillis();
    String clientId = "test-lock-re-acquire";

    createLockPath(zkc.get(), lockPath);

    SessionLockFactory lockFactory0 = createLockFactory(clientId, zkc0);
    ZKDistributedLock lock0 = new ZKDistributedLock(lockStateExecutor, lockFactory0, lockPath, Long.MAX_VALUE,
            NullStatsLogger.INSTANCE);/*from  w ww  .j a v  a  2 s.  c  om*/
    FutureUtils.result(lock0.asyncAcquire());

    Pair<String, Long> lockId0_1 = ((ZKSessionLock) lock0.getInternalLock()).getLockId();

    List<String> children = getLockWaiters(zkc, lockPath);
    assertEquals(1, children.size());
    assertTrue(lock0.haveLock());
    assertEquals(lockId0_1, Await.result(asyncParseClientID(zkc0.get(), lockPath, children.get(0))));

    ZooKeeperClientUtils.expireSession(zkc0, zkServers, sessionTimeoutMs);

    if (checkOwnershipAndReacquire) {
        checkLockAndReacquire(lock0, true);
        checkLockAndReacquire(lock0, false);
    } else {
        // session expire will trigger lock re-acquisition
        Future<ZKDistributedLock> asyncLockAcquireFuture;
        do {
            Thread.sleep(1);
            asyncLockAcquireFuture = lock0.getLockReacquireFuture();
        } while (null == asyncLockAcquireFuture && lock0.getReacquireCount() < 1);
        if (null != asyncLockAcquireFuture) {
            Await.result(asyncLockAcquireFuture);
        }
        checkLockAndReacquire(lock0, false);
    }
    children = getLockWaiters(zkc, lockPath);
    assertEquals(1, children.size());
    assertTrue(lock0.haveLock());
    Pair<String, Long> lock0_2 = ((ZKSessionLock) lock0.getInternalLock()).getLockId();
    assertEquals(lock0_2, Await.result(asyncParseClientID(zkc.get(), lockPath, children.get(0))));
    assertEquals(clientId, lock0_2.getLeft());
    assertFalse(lockId0_1.equals(lock0_2));

    FutureUtils.result(lock0.asyncClose());

    children = getLockWaiters(zkc, lockPath);
    assertEquals(0, children.size());
}

From source file:org.apache.distributedlog.lock.TestDistributedLock.java

@Test(timeout = 60000)
public void testCheckWriteLockFailureWhenLockIsAcquiredByOthers() throws Exception {
    String lockPath = "/test-check-write-lock-failure-when-lock-is-acquired-by-others-"
            + System.currentTimeMillis();
    String clientId = "test-check-write-lock-failure";

    createLockPath(zkc.get(), lockPath);

    SessionLockFactory lockFactory0 = createLockFactory(clientId, zkc0);
    ZKDistributedLock lock0 = new ZKDistributedLock(lockStateExecutor, lockFactory0, lockPath, Long.MAX_VALUE,
            NullStatsLogger.INSTANCE);//from w w  w. ja  v a 2  s .  c om
    Utils.ioResult(lock0.asyncAcquire());

    Pair<String, Long> lockId0_1 = ((ZKSessionLock) lock0.getInternalLock()).getLockId();

    List<String> children = getLockWaiters(zkc, lockPath);
    assertEquals(1, children.size());
    assertTrue(lock0.haveLock());
    assertEquals(lockId0_1, Utils.ioResult(asyncParseClientID(zkc0.get(), lockPath, children.get(0))));

    // expire the session
    ZooKeeperClientUtils.expireSession(zkc0, zkServers, sessionTimeoutMs);

    // reacquire the lock and wait reacquire completed
    checkLockAndReacquire(lock0, true);

    Pair<String, Long> lockId0_2 = ((ZKSessionLock) lock0.getInternalLock()).getLockId();
    assertFalse("New lock should be created under different session", lockId0_1.equals(lockId0_2));

    children = getLockWaiters(zkc, lockPath);
    assertEquals(1, children.size());
    assertTrue(lock0.haveLock());
    assertEquals(lockId0_2, Utils.ioResult(asyncParseClientID(zkc0.get(), lockPath, children.get(0))));

    SessionLockFactory lockFactory = createLockFactory(clientId, zkc);
    final ZKDistributedLock lock1 = new ZKDistributedLock(lockStateExecutor, lockFactory, lockPath,
            Long.MAX_VALUE, NullStatsLogger.INSTANCE);
    final CountDownLatch lockLatch = new CountDownLatch(1);
    Thread lockThread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Utils.ioResult(lock1.asyncAcquire());
                lockLatch.countDown();
            } catch (Exception e) {
                logger.error("Failed on locking lock1 : ", e);
            }
        }
    }, "lock-thread");
    lockThread.start();

    // ensure lock1 is waiting for lock0
    do {
        Thread.sleep(1);
        children = getLockWaiters(zkc, lockPath);
    } while (children.size() < 2);

    // expire the session
    ZooKeeperClientUtils.expireSession(zkc0, zkServers, sessionTimeoutMs);

    lockLatch.await();
    lockThread.join();

    try {
        checkLockAndReacquire(lock0, true);
        fail("Should fail on checking write lock since lock is acquired by lock1");
    } catch (LockingException le) {
        // expected
    }

    try {
        checkLockAndReacquire(lock0, false);
        fail("Should fail on checking write lock since lock is acquired by lock1");
    } catch (LockingException le) {
        // expected
    }
}

From source file:org.apache.distributedlog.lock.TestDistributedLock.java

private void testLockReacquireSuccess(boolean checkOwnershipAndReacquire) throws Exception {
    String lockPath = "/test-lock-re-acquire-success-" + checkOwnershipAndReacquire + "-"
            + System.currentTimeMillis();
    String clientId = "test-lock-re-acquire";

    createLockPath(zkc.get(), lockPath);

    SessionLockFactory lockFactory0 = createLockFactory(clientId, zkc0);
    ZKDistributedLock lock0 = new ZKDistributedLock(lockStateExecutor, lockFactory0, lockPath, Long.MAX_VALUE,
            NullStatsLogger.INSTANCE);/*from  w w w  .j a  v  a 2 s .  c  o m*/
    Utils.ioResult(lock0.asyncAcquire());

    Pair<String, Long> lockId0_1 = ((ZKSessionLock) lock0.getInternalLock()).getLockId();

    List<String> children = getLockWaiters(zkc, lockPath);
    assertEquals(1, children.size());
    assertTrue(lock0.haveLock());
    assertEquals(lockId0_1, Utils.ioResult(asyncParseClientID(zkc0.get(), lockPath, children.get(0))));

    ZooKeeperClientUtils.expireSession(zkc0, zkServers, sessionTimeoutMs);

    if (checkOwnershipAndReacquire) {
        checkLockAndReacquire(lock0, true);
        checkLockAndReacquire(lock0, false);
    } else {
        // session expire will trigger lock re-acquisition
        CompletableFuture<ZKDistributedLock> asyncLockAcquireFuture;
        do {
            Thread.sleep(1);
            asyncLockAcquireFuture = lock0.getLockReacquireFuture();
        } while (null == asyncLockAcquireFuture && lock0.getReacquireCount() < 1);
        if (null != asyncLockAcquireFuture) {
            Utils.ioResult(asyncLockAcquireFuture);
        }
        checkLockAndReacquire(lock0, false);
    }
    children = getLockWaiters(zkc, lockPath);
    assertEquals(1, children.size());
    assertTrue(lock0.haveLock());
    Pair<String, Long> lock0_2 = ((ZKSessionLock) lock0.getInternalLock()).getLockId();
    assertEquals(lock0_2, Utils.ioResult(asyncParseClientID(zkc.get(), lockPath, children.get(0))));
    assertEquals(clientId, lock0_2.getLeft());
    assertFalse(lockId0_1.equals(lock0_2));

    Utils.ioResult(lock0.asyncClose());

    children = getLockWaiters(zkc, lockPath);
    assertEquals(0, children.size());
}

From source file:org.mitre.mpf.nms.ChannelNode.java

private Address getNodeAddress(String hostname, NodeTypes nodeType) {
    Pair<String, NodeTypes> searchPair = Pair.of(hostname, nodeType);
    return getView().getMembers().stream().filter(addr -> searchPair.equals(AddressParser.parse(addr)))
            .findAny().orElseThrow(() -> new IllegalStateException(String
                    .format("Unable to locate node with hostname \"%s\" and type %s", hostname, nodeType)));
}

From source file:org.verdictdb.core.scrambling.ScrambleMetaSet.java

private ScrambleMeta getMetaFor(Pair<String, String> metakey) {
    for (Pair<Pair<String, String>, ScrambleMeta> item : metaSet) {
        Pair<String, String> key = item.getKey();
        ScrambleMeta m = item.getValue();
        if (key.equals(metakey)) {
            return m;
        }/*ww w . j  ava 2 s  .  c  o m*/
    }
    return null;
}

From source file:org.verdictdb.core.scrambling.ScrambleMetaSet.java

private boolean doesContain(Pair<String, String> metakey) {
    for (Pair<Pair<String, String>, ScrambleMeta> item : metaSet) {
        Pair<String, String> key = item.getKey();
        if (key.equals(metakey)) {
            return true;
        }//from   w  w w  . j  a va  2 s.  c  om
    }
    return false;
}