Пример #1
0
            public override void CoordinatorIsElected(InstanceId coordinatorId)
            {
                lock (this)
                {
                    try
                    {
                        HighAvailabilityMemberState oldState = outerInstance.state;
                        InstanceId previousElected           = outerInstance.context.ElectedMasterId;

                        outerInstance.context.AvailableHaMasterId = null;
                        if (!AcceptNewState(outerInstance.state.masterIsElected(outerInstance.context, coordinatorId)))
                        {
                            return;
                        }

                        outerInstance.context.ElectedMasterId = coordinatorId;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final HighAvailabilityMemberChangeEvent event = new HighAvailabilityMemberChangeEvent(oldState, state, coordinatorId, null);
                        HighAvailabilityMemberChangeEvent @event = new HighAvailabilityMemberChangeEvent(oldState, outerInstance.state, coordinatorId, null);
                        outerInstance.memberListeners.Notify(listener => listener.masterIsElected(@event));

                        if (oldState.AccessAllowed && oldState != outerInstance.state)
                        {
                            outerInstance.databaseAvailabilityGuard.Require(AvailabilityRequirement);
                        }

                        outerInstance.log.Debug("Got masterIsElected(" + coordinatorId + "), moved to " + outerInstance.state + " from " + oldState + ". Previous elected master is " + previousElected);
                    }
                    catch (Exception t)
                    {
                        throw new Exception(t);
                    }
                }
            }
Пример #2
0
            public override void MemberIsFailed(InstanceId instanceId)
            {
                // If we don't have quorum anymore with the currently alive members, then go to pending

                /*
                 * Unless this is a two instance cluster and we are the MASTER. This is an edge case in which a cluster
                 * of two instances gets a partition and we want to maintain write capability on one side.
                 * This, in combination with use of slave_only, is a cheap way to provide quasi-read-replica
                 * functionality for HA under the 2-instance scenario.
                 */
                if (!isQuorum(AliveCount, TotalCount) && !(TotalCount == 2 && outerInstance.state == HighAvailabilityMemberState.Master))
                {
                    HighAvailabilityMemberState oldState = outerInstance.state;
                    ChangeStateToDetached();
                    outerInstance.log.Debug("Got memberIsFailed(" + instanceId + ") and cluster lost quorum to continue, moved to " + outerInstance.state + " from " + oldState + ", while maintaining read only capability.");
                }
                else if (instanceId.Equals(outerInstance.context.ElectedMasterId) && outerInstance.state == HighAvailabilityMemberState.Slave)
                {
                    HighAvailabilityMemberState oldState = outerInstance.state;
                    ChangeStateToDetached();
                    outerInstance.log.Debug("Got memberIsFailed(" + instanceId + ") which was the master and i am a slave, moved to " + outerInstance.state + " from " + oldState + ", while maintaining read only capability.");
                }
                else
                {
                    outerInstance.log.Debug("Got memberIsFailed(" + instanceId + ")");
                }
            }
Пример #3
0
 public HighAvailabilityMemberStateMachine(HighAvailabilityMemberContext context, AvailabilityGuard databaseAvailabilityGuard, ObservedClusterMembers members, ClusterMemberEvents events, Election election, LogProvider logProvider)
 {
     this._context = context;
     this._databaseAvailabilityGuard = databaseAvailabilityGuard;
     this._members  = members;
     this._events   = events;
     this._election = election;
     this._log      = logProvider.getLog(this.GetType());
     _state         = HighAvailabilityMemberState.Pending;
 }
Пример #4
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testMasterSlaveIsAvailable()
        public virtual void TestMasterSlaveIsAvailable()
        {
            // CASE 1: Got SlaveIsAvailable for me - should fail.
            HighAvailabilityMemberState illegal = MASTER.slaveIsAvailable(_context, _myId, SampleUri);

            assertEquals(ILLEGAL, illegal);

            // CASE 2: Got SlaveIsAvailable for someone else - who cares? Should succeed.
            HighAvailabilityMemberState newState = MASTER.slaveIsAvailable(_context, new InstanceId(2), SampleUri);

            assertEquals(MASTER, newState);
        }
Пример #5
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testMasterMasterIsAvailable()
        public virtual void TestMasterMasterIsAvailable()
        {
            // CASE 1: Got MasterIsAvailable for someone else - should fail.
            HighAvailabilityMemberState illegal = MASTER.masterIsAvailable(_context, new InstanceId(2), SampleUri);

            assertEquals(ILLEGAL, illegal);

            // CASE 2: Got MasterIsAvailable for us - it's ok, should pass
            HighAvailabilityMemberState newState = MASTER.masterIsAvailable(_context, _myId, SampleUri);

            assertEquals(MASTER, newState);
        }
Пример #6
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testMasterMasterIsElected()
        public virtual void TestMasterMasterIsElected()
        {
            // CASE 1: Got MasterIsElected for me. Should remain master.
            HighAvailabilityMemberState newState = MASTER.masterIsElected(_context, _myId);

            assertEquals(MASTER, newState);

            // CASE 2: Got MasterIsElected for someone else. Should switch to pending.
            HighAvailabilityMemberState newStateCase2 = MASTER.masterIsElected(_context, new InstanceId(2));

            assertEquals(PENDING, newStateCase2);
        }
Пример #7
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testToMasterSlaveIsAvailable()
        public virtual void TestToMasterSlaveIsAvailable()
        {
            // CASE 1: Got SlaveIsAvailable for me - not ok, i'm currently switching to master
            HighAvailabilityMemberState illegal = TO_MASTER.slaveIsAvailable(_context, _myId, SampleUri);

            assertEquals(ILLEGAL, illegal);

            // CASE 2: Got SlaveIsAvailable for someone else - don't really care
            HighAvailabilityMemberState newState = TO_MASTER.slaveIsAvailable(_context, new InstanceId(2), SampleUri);

            assertEquals(TO_MASTER, newState);
        }
Пример #8
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testToMasterMasterIsAvailable()
        public virtual void TestToMasterMasterIsAvailable()
        {
            // CASE 1: Got MasterIsAvailable for me - it's ok, that means we completed switching and should to to MASTER
            HighAvailabilityMemberState newState = TO_MASTER.masterIsAvailable(_context, _myId, SampleUri);

            assertEquals(MASTER, newState);

            // CASE 2: Got MasterIsAvailable for someone else - should not happen, should have received a MasterIsElected
            HighAvailabilityMemberState illegal = TO_MASTER.masterIsAvailable(_context, new InstanceId(2), SampleUri);

            assertEquals(ILLEGAL, illegal);
        }
Пример #9
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testToSlaveSlaveIsAvailable()
        public virtual void TestToSlaveSlaveIsAvailable()
        {
            // CASE 1: It is me that that is available as slave - cool, go to SLAVE
            HighAvailabilityMemberState newState = TO_SLAVE.slaveIsAvailable(_context, _myId, SampleUri);

            assertEquals(SLAVE, newState);

            // CASE 2: It is someone else that completed the switch - ignore, continue
            HighAvailabilityMemberState newStateCase2 = TO_SLAVE.slaveIsAvailable(_context, new InstanceId(2), SampleUri);

            assertEquals(TO_SLAVE, newStateCase2);
        }
Пример #10
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testPendingSlaveIsAvailable()
        public virtual void TestPendingSlaveIsAvailable()
        {
            // CASE 1: Got SlaveIsAvailable for me - should not happen, that's what TO_SLAVE exists for
            HighAvailabilityMemberState illegal = PENDING.slaveIsAvailable(_context, _myId, SampleUri);

            assertEquals(ILLEGAL, illegal);

            // CASE 2: Got SlaveIsAvailable for someone else - it's ok, remain in PENDING
            HighAvailabilityMemberState newState = PENDING.slaveIsAvailable(_context, new InstanceId(2), SampleUri);

            assertEquals(PENDING, newState);
        }
Пример #11
0
            public override void MemberIsAvailable(string role, InstanceId instanceId, URI roleUri, StoreId storeId)
            {
                lock (this)
                {
                    try
                    {
                        /*
                         * Do different things depending on whether the cluster member is in master or slave state
                         */
                        if (role.Equals(HighAvailabilityModeSwitcher.MASTER))
                        {
                            HighAvailabilityMemberState oldState = outerInstance.state;
                            outerInstance.context.AvailableHaMasterId = roleUri;
                            if (!AcceptNewState(outerInstance.state.masterIsAvailable(outerInstance.context, instanceId, roleUri)))
                            {
                                return;
                            }
                            outerInstance.log.Debug("Got masterIsAvailable(" + instanceId + "), moved to " + outerInstance.state + " from " + oldState);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final HighAvailabilityMemberChangeEvent event = new HighAvailabilityMemberChangeEvent(oldState, state, instanceId, roleUri);
                            HighAvailabilityMemberChangeEvent @event = new HighAvailabilityMemberChangeEvent(oldState, outerInstance.state, instanceId, roleUri);
                            outerInstance.memberListeners.Notify(listener => listener.masterIsAvailable(@event));

                            if (oldState == HighAvailabilityMemberState.ToMaster && outerInstance.state == HighAvailabilityMemberState.Master)
                            {
                                outerInstance.databaseAvailabilityGuard.Fulfill(AvailabilityRequirement);
                            }
                        }
                        else if (role.Equals(HighAvailabilityModeSwitcher.SLAVE))
                        {
                            HighAvailabilityMemberState oldState = outerInstance.state;
                            if (!AcceptNewState(outerInstance.state.slaveIsAvailable(outerInstance.context, instanceId, roleUri)))
                            {
                                return;
                            }
                            outerInstance.log.Debug("Got slaveIsAvailable(" + instanceId + "), " + "moved to " + outerInstance.state + " from " + oldState);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final HighAvailabilityMemberChangeEvent event = new HighAvailabilityMemberChangeEvent(oldState, state, instanceId, roleUri);
                            HighAvailabilityMemberChangeEvent @event = new HighAvailabilityMemberChangeEvent(oldState, outerInstance.state, instanceId, roleUri);
                            outerInstance.memberListeners.Notify(listener => listener.slaveIsAvailable(@event));

                            if (oldState == HighAvailabilityMemberState.ToSlave && outerInstance.state == HighAvailabilityMemberState.Slave)
                            {
                                outerInstance.databaseAvailabilityGuard.Fulfill(AvailabilityRequirement);
                            }
                        }
                    }
                    catch (Exception throwable)
                    {
                        outerInstance.log.Warn("Exception while receiving member availability notification", throwable);
                    }
                }
            }
Пример #12
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testPendingMasterIsElected()
        public virtual void TestPendingMasterIsElected()
        {
            // CASE 1: Got MasterIsElected for me - should switch to TO_MASTER
            HighAvailabilityMemberState newState = PENDING.masterIsElected(_context, _myId);

            assertEquals(TO_MASTER, newState);

            // CASE 2: Got MasterIsElected for someone else - should remain to PENDING
            HighAvailabilityMemberState newStateCase2 = PENDING.masterIsElected(_context, new InstanceId(2));

            assertEquals(PENDING, newStateCase2);
        }
Пример #13
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testToMasterMasterIsElected()
        public virtual void TestToMasterMasterIsElected()
        {
            // CASE 1: Got MasterIsElected for me - it's ok, continue in TO_MASTER
            HighAvailabilityMemberState newState = TO_MASTER.masterIsElected(_context, _myId);

            assertEquals(TO_MASTER, newState);

            // CASE 2: Got MasterIsElected for someone else - switch to PENDING
            HighAvailabilityMemberState newStateCase2 = TO_MASTER.masterIsElected(_context, new InstanceId(2));

            assertEquals(PENDING, newStateCase2);
        }
Пример #14
0
 public override void MemberIsUnavailable(string role, InstanceId unavailableId)
 {
     if (outerInstance.context.MyId.Equals(unavailableId) && HighAvailabilityModeSwitcher.SLAVE.Equals(role) && outerInstance.state == HighAvailabilityMemberState.Slave)
     {
         HighAvailabilityMemberState oldState = outerInstance.state;
         ChangeStateToPending();
         outerInstance.log.Debug("Got memberIsUnavailable(" + unavailableId + "), moved to " + outerInstance.state + " from " + oldState);
     }
     else
     {
         outerInstance.log.Debug("Got memberIsUnavailable(" + unavailableId + ")");
     }
 }
Пример #15
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testPendingMasterIsAvailable()
        public virtual void TestPendingMasterIsAvailable()
        {
            // CASE 1: Got MasterIsAvailable for me - should not happen
            HighAvailabilityMemberState illegal = PENDING.masterIsAvailable(_context, _myId, SampleUri);

            assertEquals(ILLEGAL, illegal);

            // CASE 2: Got MasterIsAvailable for someone else - should transition to TO_SLAVE
            // TODO test correct info is passed through to context
            HighAvailabilityMemberState newState = PENDING.masterIsAvailable(_context, new InstanceId(2), SampleUri);

            assertEquals(TO_SLAVE, newState);
        }
Пример #16
0
 /// <summary>
 /// Checks if the new state is ILLEGAL. If so, it sets the state to PENDING and issues a request for
 /// elections. Otherwise it sets the current state to newState. </summary>
 /// <returns> false iff the newState is illegal. true otherwise. </returns>
 internal virtual bool AcceptNewState(HighAvailabilityMemberState newState)
 {
     if (newState == HighAvailabilityMemberState.Illegal)
     {
         outerInstance.log.Warn(format("Message received resulted in illegal state transition. I was in state %s, " + "context was %s. The error message is %s. This instance will now transition to PENDING state " + "and " + "ask for new elections. While this may fix the error, it may indicate that there is some " + "connectivity issue or some instability of cluster members.", outerInstance.state, outerInstance.context, newState.errorMessage()));
         outerInstance.context.ElectedMasterId     = null;
         outerInstance.context.AvailableHaMasterId = null;
         ChangeStateToPending();
         outerInstance.election.PerformRoleElections();
         return(false);
     }
     else
     {
         outerInstance.state = newState;
     }
     return(true);
 }
Пример #17
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testSlaveMasterIsElected()
        public virtual void TestSlaveMasterIsElected()
        {
            // CASE 1: It is me that got elected master - should switch to TO_MASTER
            HighAvailabilityMemberState newState = SLAVE.masterIsElected(_context, _myId);

            assertEquals(TO_MASTER, newState);

            InstanceId masterInstanceId = new InstanceId(2);

            when(_context.ElectedMasterId).thenReturn(masterInstanceId);
            // CASE 2: It is someone else that got elected master - should switch to PENDING
            HighAvailabilityMemberState newStateCase2 = SLAVE.masterIsElected(_context, new InstanceId(3));

            assertEquals(PENDING, newStateCase2);

            // CASE 3: It is the current master that got elected again - ignore
            HighAvailabilityMemberState newStateCase3 = SLAVE.masterIsElected(_context, masterInstanceId);

            assertEquals(SLAVE, newStateCase3);
        }
Пример #18
0
        public override void Stop()
        {
            _events.removeClusterMemberListener(_eventsListener);
            HighAvailabilityMemberState oldState = _state;

            _state = HighAvailabilityMemberState.Pending;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final HighAvailabilityMemberChangeEvent event = new HighAvailabilityMemberChangeEvent(oldState, state, null, null);
            HighAvailabilityMemberChangeEvent @event = new HighAvailabilityMemberChangeEvent(oldState, _state, null, null);

            _memberListeners.notify(listener => listener.instanceStops(@event));

            // If we were previously in a state that allowed access, we must now deny access
            if (oldState.AccessAllowed)
            {
                _databaseAvailabilityGuard.require(AvailabilityRequirement);
            }

            _context.AvailableHaMasterId = null;
        }
Пример #19
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testSlaveMasterIsAvailable()
        public virtual void TestSlaveMasterIsAvailable()
        {
            // CASE 1: It is me who is available as master - i don't think so
            HighAvailabilityMemberState illegal = SLAVE.masterIsAvailable(_context, _myId, SampleUri);

            assertEquals(ILLEGAL, illegal);

            // CASE 2: It is someone else that is available as master and is not the master now - missed the election, fail
            InstanceId masterInstanceId = new InstanceId(2);

            when(_context.ElectedMasterId).thenReturn(masterInstanceId);
            HighAvailabilityMemberState moreIllegal = SLAVE.masterIsAvailable(_context, new InstanceId(3), SampleUri);

            assertEquals(ILLEGAL, moreIllegal);

            // CASE 3: It is the same master as now - it's ok, stay calm and carry on
            HighAvailabilityMemberState newState = SLAVE.masterIsAvailable(_context, masterInstanceId, SampleUri);

            assertEquals(SLAVE, newState);
        }
Пример #20
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testToSlaveMasterIsAvailable()
        public virtual void TestToSlaveMasterIsAvailable()
        {
            // CASE 1: Got MasterIsAvailable for me - should fail, i am currently trying to become slave
            HighAvailabilityMemberState illegal = TO_SLAVE.masterIsAvailable(_context, _myId, SampleUri);

            assertEquals(ILLEGAL, illegal);

            // CASE 2: Got MasterIsAvailable for someone else who is already the master - should continue switching
            InstanceId currentMaster = new InstanceId(2);

            when(_context.ElectedMasterId).thenReturn(currentMaster);
            HighAvailabilityMemberState newState = TO_SLAVE.masterIsAvailable(_context, currentMaster, SampleUri);

            assertEquals(TO_SLAVE, newState);

            // CASE 3: Got MasterIsAvailable for someone else who is not the master - should fail
            InstanceId instanceId = new InstanceId(3);
            HighAvailabilityMemberState moreIllegal = TO_SLAVE.masterIsAvailable(_context, instanceId, SampleUri);

            assertEquals(ILLEGAL, moreIllegal);
        }