Example #1
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldAcceptOnlyFirstRequestWithSameId()
        public virtual void ShouldAcceptOnlyFirstRequestWithSameId()
        {
            // given
            ReplicatedLockTokenStateMachine stateMachine = new ReplicatedLockTokenStateMachine(new InMemoryStateStorage <ReplicatedLockTokenState>(new ReplicatedLockTokenState()));
            int firstCandidateId = LockToken.nextCandidateId(stateMachine.CurrentToken().id());

            // when
            stateMachine.ApplyCommand(new ReplicatedLockTokenRequest(member(0), firstCandidateId), 1, r =>
            {
            });
            stateMachine.ApplyCommand(new ReplicatedLockTokenRequest(member(1), firstCandidateId), 2, r =>
            {
            });

            // then
            assertEquals(0, stateMachine.CurrentToken().id());
            assertEquals(member(0), stateMachine.CurrentToken().owner());

            // when
            stateMachine.ApplyCommand(new ReplicatedLockTokenRequest(member(1), firstCandidateId + 1), 3, r =>
            {
            });
            stateMachine.ApplyCommand(new ReplicatedLockTokenRequest(member(0), firstCandidateId + 1), 4, r =>
            {
            });

            // then
            assertEquals(1, stateMachine.CurrentToken().id());
            assertEquals(member(1), stateMachine.CurrentToken().owner());
        }
Example #2
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldIssueNextLockTokenCandidateId()
        public virtual void ShouldIssueNextLockTokenCandidateId()
        {
            // given
            ReplicatedLockTokenStateMachine stateMachine = new ReplicatedLockTokenStateMachine(new InMemoryStateStorage <ReplicatedLockTokenState>(new ReplicatedLockTokenState()));
            int firstCandidateId = LockToken.nextCandidateId(stateMachine.CurrentToken().id());

            // when
            stateMachine.ApplyCommand(new ReplicatedLockTokenRequest(member(0), firstCandidateId), 0, r =>
            {
            });

            // then
            assertEquals(firstCandidateId + 1, LockToken.nextCandidateId(stateMachine.CurrentToken().id()));
        }
Example #3
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldOnlyAcceptNextImmediateId()
        public virtual void ShouldOnlyAcceptNextImmediateId()
        {
            // given
            ReplicatedLockTokenStateMachine stateMachine = new ReplicatedLockTokenStateMachine(new InMemoryStateStorage <ReplicatedLockTokenState>(new ReplicatedLockTokenState()));
            int firstCandidateId = LockToken.nextCandidateId(stateMachine.CurrentToken().id());

            // when
            stateMachine.ApplyCommand(new ReplicatedLockTokenRequest(member(0), firstCandidateId + 1), 1, r =>
            {
            });               // not accepted

            // then
            assertEquals(stateMachine.CurrentToken().id(), LockToken_Fields.INVALID_LOCK_TOKEN_ID);

            // when
            stateMachine.ApplyCommand(new ReplicatedLockTokenRequest(member(0), firstCandidateId), 2, r =>
            {
            });               // accepted

            // then
            assertEquals(stateMachine.CurrentToken().id(), firstCandidateId);

            // when
            stateMachine.ApplyCommand(new ReplicatedLockTokenRequest(member(0), firstCandidateId + 1), 3, r =>
            {
            });               // accepted

            // then
            assertEquals(stateMachine.CurrentToken().id(), firstCandidateId + 1);

            // when
            stateMachine.ApplyCommand(new ReplicatedLockTokenRequest(member(0), firstCandidateId), 4, r =>
            {
            });               // not accepted

            // then
            assertEquals(stateMachine.CurrentToken().id(), firstCandidateId + 1);

            // when
            stateMachine.ApplyCommand(new ReplicatedLockTokenRequest(member(0), firstCandidateId + 3), 5, r =>
            {
            });               // not accepted

            // then
            assertEquals(stateMachine.CurrentToken().id(), firstCandidateId + 1);
        }
Example #4
0
        public override void ApplyCommand(ReplicatedLockTokenRequest tokenRequest, long commandIndex, System.Action <Result> callback)
        {
            lock (this)
            {
                if (commandIndex <= State().ordinal())
                {
                    return;
                }

                bool requestAccepted = tokenRequest.Id() == LockToken.nextCandidateId(CurrentToken().id());
                if (requestAccepted)
                {
                    State().set(tokenRequest, commandIndex);
                }

                callback(Result.of(requestAccepted));
            }
        }
Example #5
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);
                }
            }
        }