Esempio n. 1
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldKeepAtMostGivenNumberOfInstances()
        public virtual void ShouldKeepAtMostGivenNumberOfInstances()
        {
            // Given
            const int          instancesToKeep = 10;
            PaxosInstanceStore theStore        = new PaxosInstanceStore(instancesToKeep);

            // Keeps the first instance inserted, which is the first to be removed
            PaxosInstance firstInstance = null;

            // When
            for (int i = 0; i < instancesToKeep + 1; i++)
            {
                InstanceId    currentInstanceId = new InstanceId(i);
                PaxosInstance currentInstance   = theStore.GetPaxosInstance(currentInstanceId);
                theStore.Delivered(currentInstance.Id);
                if (firstInstance == null)
                {
                    firstInstance = currentInstance;
                }
            }

            // Then
            // The first instance must have been removed now
            PaxosInstance toTest = theStore.GetPaxosInstance(firstInstance.Id);

            assertNotSame(firstInstance, toTest);
        }
Esempio n. 2
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void learnerServingOldInstanceShouldNotLogErrorIfItDoesNotHaveIt() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void LearnerServingOldInstanceShouldNotLogErrorIfItDoesNotHaveIt()
        {
            // Given
            LearnerState   state    = LearnerState.Learner;
            LearnerContext ctx      = mock(typeof(LearnerContext));
            MessageHolder  outgoing = mock(typeof(MessageHolder));
            // The instance will be asked for paxos instance 4...
            InstanceId paxosInstanceIdIDontHave          = new InstanceId(4);
            Message <LearnerMessage> messageRequestingId = Message.to(LearnerMessage.LearnRequest, URI.create("c:/1")).setHeader(Message.HEADER_FROM, "c:/2").setHeader(InstanceId.INSTANCE, "4");

            // ...but it does not have it yet
            when(ctx.GetPaxosInstance(paxosInstanceIdIDontHave)).thenReturn(new PaxosInstance(mock(typeof(PaxosInstanceStore)), paxosInstanceIdIDontHave));

            // When
            state.handle(ctx, messageRequestingId, outgoing);

            // Then
            // verify there is no logging of the failure
            verify(ctx, never()).notifyLearnMiss(paxosInstanceIdIDontHave);
            // but the learn failed went out anyway
//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: verify(outgoing, times(1)).offer(org.mockito.ArgumentMatchers.argThat<org.neo4j.cluster.com.message.Message<? extends org.neo4j.cluster.com.message.MessageType>>(new org.neo4j.cluster.protocol.MessageArgumentMatcher()
            verify(outgoing, times(1)).offer(ArgumentMatchers.argThat <Message <MessageType> >(new MessageArgumentMatcher()
                                                                                               .onMessageType(LearnerMessage.LearnFailed).to(URI.create("c:/2"))));
        }
Esempio n. 3
0
        public virtual PaxosInstance GetPaxosInstance(InstanceId instanceId)
        {
            if (instanceId == null)
            {
                throw new System.NullReferenceException("InstanceId may not be null");
            }

            return(_instances.computeIfAbsent(instanceId, i => new PaxosInstance(this, i)));
        }
Esempio n. 4
0
        public virtual void Delivered(InstanceId instanceId)
        {
            _queued++;
            _delivered.AddLast(instanceId);

            if (_queued > _maxInstancesToStore)
            {
                InstanceId removeInstanceId = _delivered.RemoveFirst();
                _instances.Remove(removeInstanceId);
                _queued--;
            }
        }
Esempio n. 5
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldReturnSameObjectWhenAskedById()
        public virtual void ShouldReturnSameObjectWhenAskedById()
        {
            // Given
            PaxosInstanceStore theStore          = new PaxosInstanceStore();
            InstanceId         currentInstanceId = new InstanceId(1);

            // When
            PaxosInstance currentInstance = theStore.GetPaxosInstance(currentInstanceId);

            // Then
            assertSame(currentInstance, theStore.GetPaxosInstance(currentInstanceId));
        }
Esempio n. 6
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void learnerShouldAskAllAliveInstancesAndTheseOnlyForMissingValue() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void LearnerShouldAskAllAliveInstancesAndTheseOnlyForMissingValue()
        {
            // Given

            IList <URI> allMembers = new List <URI>(3);
            URI         instance1  = URI.create("c:/1");        // this one is failed
            URI         instance2  = URI.create("c:/2");        // this one is ok and will respond
            URI         instance3  = URI.create("c:/3");        // this one is the requesting instance
            URI         instance4  = URI.create("c:/4");        // and this one is ok and will respond too

            allMembers.Add(instance1);
            allMembers.Add(instance2);
            allMembers.Add(instance3);
            allMembers.Add(instance4);

            ISet <Org.Neo4j.cluster.InstanceId> aliveInstanceIds = new HashSet <Org.Neo4j.cluster.InstanceId>();

            Org.Neo4j.cluster.InstanceId id2 = new Org.Neo4j.cluster.InstanceId(2);
            Org.Neo4j.cluster.InstanceId id4 = new Org.Neo4j.cluster.InstanceId(4);
            aliveInstanceIds.Add(id2);
            aliveInstanceIds.Add(id4);

            LearnerState   state    = LearnerState.Learner;
            LearnerContext ctx      = mock(typeof(LearnerContext));
            MessageHolder  outgoing = mock(typeof(MessageHolder));
            InstanceId     paxosInstanceIdIAskedFor = new InstanceId(4);

            when(ctx.LastDeliveredInstanceId).thenReturn(3L);
            when(ctx.LastKnownLearnedInstanceInCluster).thenReturn(5L);
            when(ctx.MemberURIs).thenReturn(allMembers);
            when(ctx.Alive).thenReturn(aliveInstanceIds);
            when(ctx.GetUriForId(id2)).thenReturn(instance2);
            when(ctx.GetUriForId(id4)).thenReturn(instance4);
            when(ctx.GetPaxosInstance(paxosInstanceIdIAskedFor)).thenReturn(new PaxosInstance(mock(typeof(PaxosInstanceStore)), paxosInstanceIdIAskedFor));

            Message <LearnerMessage> theCause = Message.to(LearnerMessage.CatchUp, instance2);                // could be anything, really

            // When
            state.handle(ctx, Message.timeout(LearnerMessage.LearnTimedout, theCause), outgoing);

            // Then
//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: verify(outgoing, times(1)).offer(org.mockito.ArgumentMatchers.argThat<org.neo4j.cluster.com.message.Message<? extends org.neo4j.cluster.com.message.MessageType>>(new org.neo4j.cluster.protocol.MessageArgumentMatcher()
            verify(outgoing, times(1)).offer(ArgumentMatchers.argThat <Message <MessageType> >(new MessageArgumentMatcher()
                                                                                               .onMessageType(LearnerMessage.LearnRequest).to(instance2)));
//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: verify(outgoing, times(1)).offer(org.mockito.ArgumentMatchers.argThat<org.neo4j.cluster.com.message.Message<? extends org.neo4j.cluster.com.message.MessageType>>(new org.neo4j.cluster.protocol.MessageArgumentMatcher()
            verify(outgoing, times(1)).offer(ArgumentMatchers.argThat <Message <MessageType> >(new MessageArgumentMatcher()
                                                                                               .onMessageType(LearnerMessage.LearnRequest).to(instance4)));
            verifyNoMoreInteractions(outgoing);
        }
Esempio n. 7
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void leaveShouldClearStoredInstances()
        public virtual void LeaveShouldClearStoredInstances()
        {
            // Given
            PaxosInstanceStore theStore          = new PaxosInstanceStore();
            InstanceId         currentInstanceId = new InstanceId(1);

            // When
            PaxosInstance currentInstance = theStore.GetPaxosInstance(currentInstanceId);

            theStore.Leave();

            // Then
            assertNotSame(currentInstance, theStore.GetPaxosInstance(currentInstanceId));
        }
Esempio n. 8
0
        public override AcceptorInstance GetAcceptorInstance(InstanceId instanceId)
        {
            AcceptorInstance instance = _instances[instanceId];

            if (instance == null)
            {
                instance = new AcceptorInstance();
                _instances[instanceId] = instance;

                // Make sure we only keep a maximum number of instances, to not run out of memory
                if (!_currentInstances.offer(instanceId))
                {
                    _instances.Remove(_currentInstances.poll());
                    _currentInstances.offer(instanceId);
                }
            }

            return(instance);
        }
Esempio n. 9
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void learnerReceivingLearnFailedShouldLogIt() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void LearnerReceivingLearnFailedShouldLogIt()
        {
            // Given
            LearnerState             state    = LearnerState.Learner;
            LearnerContext           ctx      = mock(typeof(LearnerContext));
            MessageHolder            outgoing = mock(typeof(MessageHolder));
            InstanceId               paxosInstanceIdIAskedFor = new InstanceId(4);
            Message <LearnerMessage> theLearnFailure          = Message.to(LearnerMessage.LearnFailed, URI.create("c:/1")).setHeader(Message.HEADER_FROM, "c:/2").setHeader(InstanceId.INSTANCE, "4");

            when(ctx.GetPaxosInstance(paxosInstanceIdIAskedFor)).thenReturn(new PaxosInstance(mock(typeof(PaxosInstanceStore)), paxosInstanceIdIAskedFor));
            when(ctx.MemberURIs).thenReturn(Collections.singletonList(URI.create("c:/2")));

            // When
            state.handle(ctx, theLearnFailure, outgoing);

            // Then
            // verify that the failure was logged
            verify(ctx, times(1)).notifyLearnMiss(paxosInstanceIdIAskedFor);
        }
Esempio n. 10
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldNotCloseTheGapIfItsTheCoordinatorAndTheGapIsSmallerThanTheThreshold() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldNotCloseTheGapIfItsTheCoordinatorAndTheGapIsSmallerThanTheThreshold()
        {
            // Given
            // A coordinator that knows that the last Paxos instance delivered is 3
            long         lastDelivered = 3L;
            LearnerState learner       = LearnerState.Learner;

            Org.Neo4j.cluster.InstanceId memberId = new Org.Neo4j.cluster.InstanceId(42);

            LearnerContext context = mock(typeof(LearnerContext));

            when(context.IsMe(any())).thenReturn(true);
            when(context.Coordinator).thenReturn(memberId);                   // so it's the coordinator
            when(context.LastDeliveredInstanceId).thenReturn(lastDelivered);
            // and has this list of pending instances (up to id 14)
            IList <PaxosInstance> pendingInstances = new LinkedList <PaxosInstance>();

            for (int i = 1; i < 12; i++)                 // start at 1 because instance 3 is already delivered
            {
                InstanceId    instanceId = new InstanceId(lastDelivered + i);
                PaxosInstance value      = new PaxosInstance(mock(typeof(PaxosInstanceStore)), instanceId);
                value.Closed("", "");
                when(context.GetPaxosInstance(instanceId)).thenReturn(value);
                pendingInstances.Add(value);
            }
            when(context.GetLog(any())).thenReturn(mock(typeof(Log)));

            Message <LearnerMessage> incomingInstance = Message.to(LearnerMessage.Learn, URI.create("c:/1"), new LearnerMessage.LearnState(new object())).setHeader(Message.HEADER_FROM, "c:/2").setHeader(Message.HEADER_CONVERSATION_ID, "conversation-id").setHeader(InstanceId.INSTANCE, "" + (lastDelivered + LearnerContext_Fields.LEARN_GAP_THRESHOLD));

            // When
            // it receives a message with Paxos instance id at the threshold
            learner.handle(context, incomingInstance, mock(typeof(MessageHolder)));

            // Then
            // it waits and doesn't deliver anything
            foreach (PaxosInstance pendingInstance in pendingInstances)
            {
                assertFalse(pendingInstance.IsState(PaxosInstance.State.Delivered));
            }
            verify(context, times(0)).LastDeliveredInstanceId = anyLong();
        }
Esempio n. 11
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldNotConsiderInstanceJoiningWithSameIdAndIpAProblem() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldNotConsiderInstanceJoiningWithSameIdAndIpAProblem()
        {
            // Given

            Config config = mock(typeof(Config));

            when(config.Get(ClusterSettings.max_acceptors)).thenReturn(10);

            MultiPaxosContext ctx = new MultiPaxosContext(new InstanceId(1), Collections.emptyList(), mock(typeof(ClusterConfiguration)), mock(typeof(Executor)), NullLogProvider.Instance, new ObjectStreamFactory(), new ObjectStreamFactory(), mock(typeof(AcceptorInstanceStore)), mock(typeof(Timeouts)), mock(typeof(ElectionCredentialsProvider)), config);

            InstanceId joiningId  = new InstanceId(12);
            string     joiningUri = "http://127.0.0.1:900";

            // When
            ctx.ClusterContext.instanceIsJoining(joiningId, new URI(joiningUri));

            // Then
            assertFalse(ctx.ClusterContext.isInstanceJoiningFromDifferentUri(joiningId, new URI(joiningUri)));
            assertTrue(ctx.ClusterContext.isInstanceJoiningFromDifferentUri(joiningId, new URI("http://127.0.0.1:80")));
            assertFalse(ctx.ClusterContext.isInstanceJoiningFromDifferentUri(new InstanceId(13), new URI(joiningUri)));
        }
Esempio n. 12
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldBroadcastWhenHavingQuorumAndCoordinator() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldBroadcastWhenHavingQuorumAndCoordinator()
        {
            // GIVEN
            AtomicBroadcastContext context = mock(typeof(AtomicBroadcastContext));

            when(context.HasQuorum()).thenReturn(true);

            InstanceId coordinator = Id(1);

            when(context.Coordinator).thenReturn(coordinator);
            when(context.GetUriForId(coordinator)).thenReturn(Uri(1));

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.List<org.neo4j.cluster.com.message.Message<?>> messages = new java.util.ArrayList<>(1);
//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
            IList <Message <object> > messages = new List <Message <object> >(1);
            MessageHolder             outgoing = messages.add;

            // WHEN
            broadcasting.handle(context, Message(1), outgoing);
            // THEN
            assertEquals(1, messages.Count);
        }
Esempio n. 13
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldNotPickAWinnerIfAllVotesAreForIneligibleCandidates()
        public virtual void ShouldNotPickAWinnerIfAllVotesAreForIneligibleCandidates()
        {
            // given
            InstanceId instanceOne = new InstanceId(1);
            InstanceId instanceTwo = new InstanceId(2);

            ClusterContext clusterContext = mock(typeof(ClusterContext));

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.logging.Log log = mock(org.neo4j.logging.Log.class);
            Log         log         = mock(typeof(Log));
            LogProvider logProvider = new LogProviderAnonymousInnerClass2(this, log);

            when(clusterContext.GetLog(typeof(DefaultWinnerStrategy))).thenReturn(logProvider.GetLog(typeof(DefaultWinnerStrategy)));

            // when
            ICollection <Vote> votes = Arrays.asList(new Vote(instanceOne, new NotElectableElectionCredentials()), new Vote(instanceTwo, new NotElectableElectionCredentials()));

            DefaultWinnerStrategy strategy = new DefaultWinnerStrategy(clusterContext);
            InstanceId            winner   = strategy.PickWinner(votes);

            // then
            assertNull(winner);
        }
Esempio n. 14
0
 public void unelected(string role, InstanceId instanceId, URI electedMember)
 {
     Console.WriteLine(instanceId + " at URI " + electedMember + " was removed from " + role);
 }
Esempio n. 15
0
 public void elected(string role, InstanceId instanceId, URI electedMember)
 {
     Console.WriteLine(instanceId + " at URI " + electedMember + " was elected as " + role);
 }
Esempio n. 16
0
 public void leftCluster(InstanceId instanceId, URI member)
 {
     Console.WriteLine("Left cluster:" + instanceId);
 }
Esempio n. 17
0
 public void joinedCluster(InstanceId instanceId, URI member)
 {
     Console.WriteLine("Joined cluster:" + instanceId + " (at URI " + member + ")");
 }
Esempio n. 18
0
 public override void LastDelivered(InstanceId instanceId)
 {
     _lastDeliveredInstanceId = instanceId.Id;
 }
Esempio n. 19
0
 public void failed(InstanceId server)
 {
     Console.WriteLine(server + " failed");
 }
Esempio n. 20
0
 public void alive(InstanceId server)
 {
     Console.WriteLine(server + " alive");
 }
Esempio n. 21
0
 public PaxosInstance(PaxosInstanceStore store, InstanceId instanceId)
 {
     this.Store = store;
     this.Id    = instanceId;
 }