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;
}
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();
}
}
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());
});
}
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());
}
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();
}
}
Aggregations