예제 #1
0
        /// <summary>
        /// Acquires a valid token id owned by us or throws.
        /// </summary>
        private int AcquireTokenOrThrow()
        {
            lock (this)
            {
                LockToken currentToken = _lockTokenStateMachine.currentToken();
                if (_myself.Equals(currentToken.Owner()))
                {
                    return(currentToken.Id());
                }

                /* If we are not the leader then we will not even attempt to get the token,
                 * since only the leader should take locks. */
                EnsureLeader();

                ReplicatedLockTokenRequest lockTokenRequest = new ReplicatedLockTokenRequest(_myself, LockToken.nextCandidateId(currentToken.Id()));

                Future <object> future;
                try
                {
                    future = _replicator.replicate(lockTokenRequest, true);
                }
                catch (ReplicationFailureException e)
                {
                    throw new AcquireLockTimeoutException(e, "Replication failure acquiring lock token.", ReplicationFailure);
                }

                try
                {
                    bool success = ( bool )future.get();
                    if (success)
                    {
                        return(lockTokenRequest.Id());
                    }
                    else
                    {
                        throw new AcquireLockTimeoutException("Failed to acquire lock token. Was taken by another candidate.", NotALeader);
                    }
                }
                catch (ExecutionException e)
                {
                    throw new AcquireLockTimeoutException(e, "Failed to acquire lock token.", NotALeader);
                }
                catch (InterruptedException e)
                {
                    Thread.CurrentThread.Interrupt();
                    throw new AcquireLockTimeoutException(e, "Failed to acquire lock token.", Interrupted);
                }
            }
        }
예제 #2
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldSetInitialPendingRequestToInitialState()
        public virtual void ShouldSetInitialPendingRequestToInitialState()
        {
            // Given
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @SuppressWarnings("unchecked") org.neo4j.causalclustering.core.state.storage.StateStorage<ReplicatedLockTokenState> storage = mock(org.neo4j.causalclustering.core.state.storage.StateStorage.class);
            StateStorage <ReplicatedLockTokenState> storage = mock(typeof(StateStorage));
            MemberId initialHoldingCoreMember     = member(0);
            ReplicatedLockTokenState initialState = new ReplicatedLockTokenState(123, new ReplicatedLockTokenRequest(initialHoldingCoreMember, 3));

            when(storage.InitialState).thenReturn(initialState);

            // When
            ReplicatedLockTokenStateMachine stateMachine = new ReplicatedLockTokenStateMachine(storage);

            // Then
            LockToken initialToken = stateMachine.CurrentToken();

            assertEquals(initialState.Get().owner(), initialToken.Owner());
            assertEquals(initialState.Get().id(), initialToken.Id());
        }