use of com.hazelcast.internal.util.LockGuard in project hazelcast by hazelcast.
the class ClusterStateManager method commitClusterState.
public void commitClusterState(ClusterStateChange stateChange, Address initiator, String 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 ACTIVE, then remove all members which left while not active.
if (newState == ClusterState.ACTIVE) {
node.getClusterService().removeMembersDeadWhileClusterIsNotActive();
}
} else if (stateChange.isOfType(Version.class)) {
// version is validated on cluster-state-lock, thus we can commit without checking compatibility
doSetClusterVersion((Version) stateChange.getNewState());
} 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 ClusterStateManager method lockClusterState.
/**
* Validate cluster state change requested and set a {@code ClusterStateLock}.
* @param stateChange
* @param initiator
* @param txnId
* @param leaseTime
* @param partitionStateVersion
*/
public void lockClusterState(ClusterStateChange stateChange, Address initiator, String txnId, long leaseTime, int partitionStateVersion) {
Preconditions.checkNotNull(stateChange);
clusterServiceLock.lock();
try {
if (!node.getNodeExtension().isStartCompleted()) {
throw new IllegalStateException("Can not lock cluster state! Startup is not completed yet!");
}
if (stateChange.isOfType(Version.class)) {
validateNodeCompatibleWith((Version) stateChange.getNewState());
validateClusterVersionChange((Version) stateChange.getNewState());
}
checkMigrationsAndPartitionStateVersion(stateChange, partitionStateVersion);
final LockGuard currentLock = getStateLock();
if (!currentLock.allowsLock(txnId)) {
throw new TransactionException("Locking failed for " + initiator + ", tx: " + txnId + ", current state: " + toString());
}
stateLockRef.set(new LockGuard(initiator, txnId, leaseTime));
try {
// check migration status and partition-state version again
// if partition state is changed then release the lock and fail.
checkMigrationsAndPartitionStateVersion(stateChange, partitionStateVersion);
} catch (IllegalStateException e) {
stateLockRef.set(LockGuard.NOT_LOCKED);
throw e;
}
} finally {
clusterServiceLock.unlock();
}
}
use of com.hazelcast.internal.util.LockGuard in project hazelcast by hazelcast.
the class ClusterStateManager method lockOrExtendClusterState.
private void lockOrExtendClusterState(Address initiator, UUID txnId, long leaseTime) {
Preconditions.checkPositive("leaseTime", leaseTime);
LockGuard currentLock = getStateLock();
if (!currentLock.allowsLock(txnId)) {
throw new TransactionException("Locking failed for " + initiator + ", tx: " + txnId + ", current state: " + toString());
}
long newLeaseTime = currentLock.getRemainingTime() + leaseTime;
if (newLeaseTime < 0L) {
newLeaseTime = Long.MAX_VALUE;
}
stateLockRef.set(new LockGuard(initiator, txnId, newLeaseTime));
}
use of com.hazelcast.internal.util.LockGuard in project hazelcast by hazelcast.
the class ClusterStateManager method rollbackClusterState.
public boolean rollbackClusterState(UUID txnId) {
clusterServiceLock.lock();
try {
final LockGuard currentLock = getStateLock();
if (!currentLock.allowsUnlock(txnId)) {
return false;
}
logger.fine("Rolling back cluster state transaction: " + txnId);
stateLockRef.set(LockGuard.NOT_LOCKED);
// if state allows join after rollback, then remove all members which left during transaction.
if (state.isJoinAllowed()) {
node.getClusterService().getMembershipManager().removeAllMissingMembers();
}
return true;
} finally {
clusterServiceLock.unlock();
}
}
use of com.hazelcast.internal.util.LockGuard in project hazelcast by hazelcast.
the class ClusterStateManagerTest method assertLockedBy.
private void assertLockedBy(Address initiator) {
assertEquals(IN_TRANSITION, clusterStateManager.getState());
LockGuard stateLock = clusterStateManager.getStateLock();
assertTrue(stateLock.isLocked());
assertEquals(TXN, stateLock.getLockOwnerId());
assertEquals(initiator, stateLock.getLockOwner());
}
Aggregations