Search in sources :

Example 1 with LockGuard

use of com.hazelcast.internal.util.LockGuard in project hazelcast by hazelcast.

the class ClusterStateManager method getStateLock.

LockGuard getStateLock() {
    LockGuard stateLock = stateLockRef.get();
    while (stateLock.isLeaseExpired()) {
        if (stateLockRef.compareAndSet(stateLock, LockGuard.NOT_LOCKED)) {
            logger.fine("Cluster state lock: " + stateLock + " is expired.");
            stateLock = LockGuard.NOT_LOCKED;
            break;
        }
        stateLock = stateLockRef.get();
    }
    return stateLock;
}
Also used : LockGuard(com.hazelcast.internal.util.LockGuard)

Example 2 with LockGuard

use of com.hazelcast.internal.util.LockGuard in project hazelcast by hazelcast.

the class ClusterStateManager method commitClusterState.

public void commitClusterState(ClusterStateChange stateChange, Address initiator, UUID txnId, boolean isTransient) {
    Preconditions.checkNotNull(stateChange);
    stateChange.validate();
    clusterServiceLock.lock();
    try {
        final LockGuard stateLock = getStateLock();
        if (!stateLock.allowsUnlock(txnId)) {
            throw new TransactionException("Cluster state change [" + state + " -> " + stateChange + "] failed for " + initiator + ", current state: " + stateToString());
        }
        if (stateChange.isOfType(ClusterState.class)) {
            ClusterState newState = (ClusterState) stateChange.getNewState();
            doSetClusterState(newState, isTransient);
            // if state is changed to allow joins, then remove all members which left while not active.
            if (newState.isJoinAllowed()) {
                node.getClusterService().getMembershipManager().removeAllMissingMembers();
            }
        } else if (stateChange.isOfType(Version.class)) {
            // version is validated on cluster-state-lock, thus we can commit without checking compatibility
            Version newVersion = (Version) stateChange.getNewState();
            logger.info("Cluster version set to " + newVersion);
            doSetClusterVersion(newVersion);
        } else {
            throw new IllegalArgumentException("Illegal ClusterStateChange of type " + stateChange.getType() + ".");
        }
    } finally {
        clusterServiceLock.unlock();
    }
}
Also used : ClusterState(com.hazelcast.cluster.ClusterState) TransactionException(com.hazelcast.transaction.TransactionException) Version(com.hazelcast.version.Version) LockGuard(com.hazelcast.internal.util.LockGuard)

Example 3 with LockGuard

use of com.hazelcast.internal.util.LockGuard in project hazelcast by hazelcast.

the class ClusterStateManagerTest method test_lockClusterState_expiry.

@Test
public void test_lockClusterState_expiry() throws Exception {
    clusterStateManager.lockClusterState(ClusterStateChange.from(FROZEN), newAddress(), TXN, 1, MEMBERLIST_VERSION, PARTITION_STAMP);
    assertTrueEventually(() -> {
        LockGuard stateLock = clusterStateManager.getStateLock();
        assertFalse(stateLock.isLocked());
        assertEquals(ACTIVE, clusterStateManager.getState());
    });
}
Also used : LockGuard(com.hazelcast.internal.util.LockGuard) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 4 with LockGuard

use of com.hazelcast.internal.util.LockGuard in project hazelcast by hazelcast.

the class ClusterStateManagerTest method test_lockClusterState_getLockExpiryTime.

@Test
public void test_lockClusterState_getLockExpiryTime() throws Exception {
    Address initiator = newAddress();
    clusterStateManager.lockClusterState(ClusterStateChange.from(FROZEN), initiator, TXN, TimeUnit.DAYS.toMillis(1), MEMBERLIST_VERSION, PARTITION_STAMP);
    LockGuard stateLock = clusterStateManager.getStateLock();
    assertTrue(Clock.currentTimeMillis() + TimeUnit.HOURS.toMillis(12) < stateLock.getLockExpiryTime());
}
Also used : Address(com.hazelcast.cluster.Address) InetAddress(java.net.InetAddress) LockGuard(com.hazelcast.internal.util.LockGuard) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 5 with LockGuard

use of com.hazelcast.internal.util.LockGuard in project hazelcast by hazelcast.

the class ClusterStateManager method rollbackClusterState.

public boolean rollbackClusterState(String txnId) {
    clusterServiceLock.lock();
    try {
        final LockGuard currentLock = getStateLock();
        if (!currentLock.allowsUnlock(txnId)) {
            return false;
        }
        stateLockRef.set(LockGuard.NOT_LOCKED);
        // if state remains ACTIVE after rollback, then remove all members which left during transaction.
        if (state == ClusterState.ACTIVE) {
            node.getClusterService().removeMembersDeadWhileClusterIsNotActive();
        }
        return true;
    } finally {
        clusterServiceLock.unlock();
    }
}
Also used : LockGuard(com.hazelcast.internal.util.LockGuard)

Aggregations

LockGuard (com.hazelcast.internal.util.LockGuard)12 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)4 QuickTest (com.hazelcast.test.annotation.QuickTest)4 TransactionException (com.hazelcast.transaction.TransactionException)4 Test (org.junit.Test)4 Address (com.hazelcast.cluster.Address)3 InetAddress (java.net.InetAddress)3 ClusterState (com.hazelcast.cluster.ClusterState)2 Version (com.hazelcast.version.Version)2