Search in sources :

Example 6 with InternalLockNamespace

use of com.hazelcast.concurrent.lock.InternalLockNamespace in project hazelcast by hazelcast.

the class Invocation_BlockingTest method async_whenOperationTimeout.

@Test
public void async_whenOperationTimeout() {
    int callTimeout = 5000;
    Config config = new Config().setProperty(OPERATION_CALL_TIMEOUT_MILLIS.getName(), "" + callTimeout);
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance local = factory.newHazelcastInstance(config);
    HazelcastInstance remote = factory.newHazelcastInstance(config);
    warmUpPartitions(factory.getAllHazelcastInstances());
    NodeEngineImpl nodeEngine = getNodeEngineImpl(local);
    String key = generateKeyOwnedBy(remote);
    int partitionId = nodeEngine.getPartitionService().getPartitionId(key);
    // first we lock the lock by another thread
    InternalOperationService opService = nodeEngine.getOperationService();
    int otherThreadId = 2;
    opService.invokeOnPartition(new LockOperation(new InternalLockNamespace(key), nodeEngine.toData(key), otherThreadId, -1, -1).setPartitionId(partitionId)).join();
    // then we execute a lock operation that won't be executed because lock is already acquired
    // we are going to do some waiting (3x call timeout)
    int threadId = 1;
    Operation op = new LockOperation(new InternalLockNamespace(key), nodeEngine.toData(key), threadId, -1, 3 * callTimeout).setPartitionId(partitionId);
    final InternalCompletableFuture<Object> future = opService.invokeOnPartition(op);
    final ExecutionCallback<Object> callback = getExecutionCallbackMock();
    future.andThen(callback);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            verify(callback).onResponse(Boolean.FALSE);
        }
    });
}
Also used : NodeEngineImpl(com.hazelcast.spi.impl.NodeEngineImpl) LockOperation(com.hazelcast.concurrent.lock.operations.LockOperation) InternalLockNamespace(com.hazelcast.concurrent.lock.InternalLockNamespace) Config(com.hazelcast.config.Config) InternalOperationService(com.hazelcast.spi.impl.operationservice.InternalOperationService) UnlockOperation(com.hazelcast.concurrent.lock.operations.UnlockOperation) LockOperation(com.hazelcast.concurrent.lock.operations.LockOperation) IsLockedOperation(com.hazelcast.concurrent.lock.operations.IsLockedOperation) Operation(com.hazelcast.spi.Operation) TimeoutException(java.util.concurrent.TimeoutException) OperationTimeoutException(com.hazelcast.core.OperationTimeoutException) HazelcastInstance(com.hazelcast.core.HazelcastInstance) AssertTask(com.hazelcast.test.AssertTask) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 7 with InternalLockNamespace

use of com.hazelcast.concurrent.lock.InternalLockNamespace in project hazelcast by hazelcast.

the class PartitionControlledIdTest method testLock.

@Test
public void testLock() throws Exception {
    String partitionKey = "hazelcast";
    HazelcastInstance hz = getHazelcastInstance(partitionKey);
    ILock lock = hz.getLock("lock@" + partitionKey);
    lock.lock();
    assertEquals("lock@" + partitionKey, lock.getName());
    assertEquals(partitionKey, lock.getPartitionKey());
    Node node = getNode(hz);
    LockServiceImpl lockService = node.nodeEngine.getService(LockServiceImpl.SERVICE_NAME);
    Partition partition = instances[0].getPartitionService().getPartition(partitionKey);
    LockStore lockStore = lockService.getLockStore(partition.getPartitionId(), new InternalLockNamespace(lock.getName()));
    Data key = node.getSerializationService().toData(lock.getName(), StringPartitioningStrategy.INSTANCE);
    assertTrue(lockStore.isLocked(key));
}
Also used : Partition(com.hazelcast.core.Partition) HazelcastInstance(com.hazelcast.core.HazelcastInstance) InternalLockNamespace(com.hazelcast.concurrent.lock.InternalLockNamespace) Node(com.hazelcast.instance.Node) LockServiceImpl(com.hazelcast.concurrent.lock.LockServiceImpl) Data(com.hazelcast.nio.serialization.Data) ILock(com.hazelcast.core.ILock) LockStore(com.hazelcast.concurrent.lock.LockStore) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 8 with InternalLockNamespace

use of com.hazelcast.concurrent.lock.InternalLockNamespace in project hazelcast by hazelcast.

the class Invocation_BlockingTest method sync_whenManyGettersAndLotsOfWaiting.

/**
     * Tests if the future on a blocking operation can be shared by multiple threads. This tests fails in 3.6 because
     * only 1 thread will be able to swap out CONTINUE_WAIT and all other threads will fail with an OperationTimeoutExcepyion
     */
@Test
public void sync_whenManyGettersAndLotsOfWaiting() throws Exception {
    int callTimeout = 10000;
    Config config = new Config().setProperty(OPERATION_CALL_TIMEOUT_MILLIS.getName(), "" + callTimeout);
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance local = factory.newHazelcastInstance(config);
    HazelcastInstance remote = factory.newHazelcastInstance(config);
    NodeEngineImpl nodeEngine = getNodeEngineImpl(local);
    String key = generateKeyOwnedBy(remote);
    int partitionId = nodeEngine.getPartitionService().getPartitionId(key);
    // first we execute an operation that stall the partition
    InternalOperationService opService = nodeEngine.getOperationService();
    // first we are going to lock
    int otherThreadId = 1;
    opService.createInvocationBuilder(null, new LockOperation(new InternalLockNamespace(key), nodeEngine.toData(key), otherThreadId, -1, -1), partitionId).setCallTimeout(callTimeout).invoke().join();
    // then we are going to send the invocation and share the future by many threads
    int thisThreadId = 2;
    final InternalCompletableFuture<Object> future = opService.createInvocationBuilder(null, new LockOperation(new InternalLockNamespace(key), nodeEngine.toData(key), thisThreadId, -1, -1), partitionId).setCallTimeout(callTimeout).invoke();
    // now we are going to do a get on the future by a whole bunch of threads
    final List<Future> futures = new LinkedList<Future>();
    for (int k = 0; k < 10; k++) {
        futures.add(spawn(new Callable() {

            @Override
            public Object call() throws Exception {
                return future.join();
            }
        }));
    }
    // lets do a very long wait so that the heartbeat/retrying mechanism have kicked in.
    // the lock remains locked; so the threads calling future.get remain blocked
    sleepMillis(callTimeout * 5);
    // unlocking the lock
    opService.createInvocationBuilder(null, new UnlockOperation(new InternalLockNamespace(key), nodeEngine.toData(key), otherThreadId), partitionId).setCallTimeout(callTimeout).invoke().join();
    // now the futures should all unblock
    for (Future f : futures) {
        assertEquals(Boolean.TRUE, f.get());
    }
}
Also used : NodeEngineImpl(com.hazelcast.spi.impl.NodeEngineImpl) LockOperation(com.hazelcast.concurrent.lock.operations.LockOperation) InternalLockNamespace(com.hazelcast.concurrent.lock.InternalLockNamespace) Config(com.hazelcast.config.Config) UnlockOperation(com.hazelcast.concurrent.lock.operations.UnlockOperation) InternalOperationService(com.hazelcast.spi.impl.operationservice.InternalOperationService) LinkedList(java.util.LinkedList) Callable(java.util.concurrent.Callable) HazelcastInstance(com.hazelcast.core.HazelcastInstance) InternalCompletableFuture(com.hazelcast.spi.InternalCompletableFuture) Future(java.util.concurrent.Future) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 9 with InternalLockNamespace

use of com.hazelcast.concurrent.lock.InternalLockNamespace in project hazelcast by hazelcast.

the class Invocation_BlockingTest method async_whenMultipleAndThenOnSameFuture.

/**
     * Tests if the future on a blocking operation can be shared by multiple threads. This tests fails in 3.6 because
     * only 1 thread will be able to swap out CONTINUE_WAIT and all other threads will fail with an OperationTimeoutExcepyion
     */
@Test
public void async_whenMultipleAndThenOnSameFuture() throws Exception {
    int callTimeout = 5000;
    Config config = new Config().setProperty(OPERATION_CALL_TIMEOUT_MILLIS.getName(), "" + callTimeout);
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance local = factory.newHazelcastInstance(config);
    final HazelcastInstance remote = factory.newHazelcastInstance(config);
    NodeEngineImpl nodeEngine = getNodeEngineImpl(local);
    String key = generateKeyOwnedBy(remote);
    int partitionId = nodeEngine.getPartitionService().getPartitionId(key);
    // first we execute an operation that stall the partition.
    InternalOperationService opService = nodeEngine.getOperationService();
    // first we are going to lock
    int otherThreadId = 1;
    opService.createInvocationBuilder(null, new LockOperation(new InternalLockNamespace(key), nodeEngine.toData(key), otherThreadId, -1, -1), partitionId).setCallTimeout(callTimeout).invoke().join();
    // then we are going to send another lock request by a different thread; so it can't complete
    int thisThreadId = 2;
    final InternalCompletableFuture<Object> future = opService.createInvocationBuilder(null, new LockOperation(new InternalLockNamespace(key), nodeEngine.toData(key), thisThreadId, -1, -1), partitionId).setCallTimeout(callTimeout).invoke();
    // then we register a bunch of listeners
    int listenerCount = 10;
    final CountDownLatch listenersCompleteLatch = new CountDownLatch(listenerCount);
    for (int k = 0; k < 10; k++) {
        future.andThen(new ExecutionCallback<Object>() {

            @Override
            public void onResponse(Object response) {
                if (Boolean.TRUE.equals(response)) {
                    listenersCompleteLatch.countDown();
                } else {
                    System.out.println(response);
                }
            }

            @Override
            public void onFailure(Throwable t) {
                t.printStackTrace();
            }
        });
    }
    // let's do a very long wait so that the heartbeat/retrying mechanism have kicked in.
    // the lock remains locked; so the threads calling future.get remain blocked
    sleepMillis(callTimeout * 5);
    // unlocking the lock
    opService.createInvocationBuilder(null, new UnlockOperation(new InternalLockNamespace(key), nodeEngine.toData(key), otherThreadId), partitionId).setCallTimeout(callTimeout).invoke().join();
    // and all the listeners should complete
    assertOpenEventually(listenersCompleteLatch);
}
Also used : NodeEngineImpl(com.hazelcast.spi.impl.NodeEngineImpl) LockOperation(com.hazelcast.concurrent.lock.operations.LockOperation) InternalLockNamespace(com.hazelcast.concurrent.lock.InternalLockNamespace) Config(com.hazelcast.config.Config) UnlockOperation(com.hazelcast.concurrent.lock.operations.UnlockOperation) InternalOperationService(com.hazelcast.spi.impl.operationservice.InternalOperationService) CountDownLatch(java.util.concurrent.CountDownLatch) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 10 with InternalLockNamespace

use of com.hazelcast.concurrent.lock.InternalLockNamespace in project hazelcast by hazelcast.

the class Invocation_BlockingTest method sync_whenGetTimeout.

/**
     * Checks if a get with a timeout is called, and the timeout expires, that we get a TimeoutException.
     */
@Test
public void sync_whenGetTimeout() throws Exception {
    Config config = new Config();
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance local = factory.newHazelcastInstance(config);
    HazelcastInstance remote = factory.newHazelcastInstance(config);
    warmUpPartitions(factory.getAllHazelcastInstances());
    NodeEngineImpl nodeEngine = getNodeEngineImpl(local);
    String key = generateKeyOwnedBy(remote);
    int partitionId = nodeEngine.getPartitionService().getPartitionId(key);
    // first we execute an operation that stall the partition
    InternalOperationService opService = nodeEngine.getOperationService();
    opService.invokeOnPartition(null, new SlowOperation(SECONDS.toMillis(5)), partitionId);
    // then we execute a lock operation that won't be executed because the partition is blocked.
    LockOperation op = new LockOperation(new InternalLockNamespace(key), nodeEngine.toData(key), 1, -1, -1);
    InternalCompletableFuture<Object> future = opService.createInvocationBuilder(null, op, partitionId).invoke();
    // we do a get with a very short timeout; so we should get a TimeoutException
    try {
        future.get(1, SECONDS);
        fail();
    } catch (TimeoutException expected) {
        ignore(expected);
    }
    //if we do a get with a long enough timeout, the call will complete without a problem
    Object result = future.get(60, SECONDS);
    assertEquals(Boolean.TRUE, result);
}
Also used : NodeEngineImpl(com.hazelcast.spi.impl.NodeEngineImpl) LockOperation(com.hazelcast.concurrent.lock.operations.LockOperation) InternalLockNamespace(com.hazelcast.concurrent.lock.InternalLockNamespace) Config(com.hazelcast.config.Config) InternalOperationService(com.hazelcast.spi.impl.operationservice.InternalOperationService) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) TimeoutException(java.util.concurrent.TimeoutException) OperationTimeoutException(com.hazelcast.core.OperationTimeoutException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

InternalLockNamespace (com.hazelcast.concurrent.lock.InternalLockNamespace)12 HazelcastInstance (com.hazelcast.core.HazelcastInstance)8 ParallelTest (com.hazelcast.test.annotation.ParallelTest)8 QuickTest (com.hazelcast.test.annotation.QuickTest)8 Test (org.junit.Test)8 LockOperation (com.hazelcast.concurrent.lock.operations.LockOperation)7 Config (com.hazelcast.config.Config)7 NodeEngineImpl (com.hazelcast.spi.impl.NodeEngineImpl)7 InternalOperationService (com.hazelcast.spi.impl.operationservice.InternalOperationService)7 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)7 Data (com.hazelcast.nio.serialization.Data)5 UnlockOperation (com.hazelcast.concurrent.lock.operations.UnlockOperation)4 OperationTimeoutException (com.hazelcast.core.OperationTimeoutException)4 IsLockedOperation (com.hazelcast.concurrent.lock.operations.IsLockedOperation)3 TimeoutException (java.util.concurrent.TimeoutException)3 SignalOperation (com.hazelcast.concurrent.lock.operations.SignalOperation)2 Operation (com.hazelcast.spi.Operation)2 AssertTask (com.hazelcast.test.AssertTask)2 LockServiceImpl (com.hazelcast.concurrent.lock.LockServiceImpl)1 LockStore (com.hazelcast.concurrent.lock.LockStore)1