Beispiel #1
0
        private async Task AcquireLock(IDistributedLock consulLock, CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                if (!consulLock.IsHeld)
                {
                    if (_currentRole != NodeRole.Follower)
                    {
                        _applicationEvents.Publish(new NodeRoleTransitioned(NodeRole.Follower));
                        _currentRole = NodeRole.Follower;
                    }

                    consulLock.Acquire(cancellationToken);
                }

                while (!cancellationToken.IsCancellationRequested && consulLock.IsHeld)
                {
                    if (_currentRole != NodeRole.Leader)
                    {
                        _applicationEvents.Publish(new NodeRoleTransitioned(NodeRole.Leader));
                        _currentRole = NodeRole.Leader;
                    }

                    await Task.Delay(TimeSpan.FromSeconds(10), cancellationToken).ConfigureAwait(false);
                }
            }
        }
Beispiel #2
0
 private void BecomeCandidate()
 {
     if (Role == NodeRole.Follower)
     {
         Role = NodeRole.Candidate;
     }
 }
Beispiel #3
0
 public VoteBackStruct voteForMe(int term, int CandidateId, int lastLogIdx, int lastLogTerm)
 {
     lock (this)
     {
         VoteBackStruct ret = new VoteBackStruct();
         ret.Term        = term_;
         ret.VoteGranted = false;
         if (term > term_) // recv higher term request
         {
             Console.WriteLine(String.Format("{0} [{1}] voted in term {2}, recv_term {3} from {4}, isVoteInThisTerm_ {5}!", id, role, term, term, CandidateId, isVoteInThisTerm_));
             role_             = NodeRole.Follower;
             state_            = ServerState.HigherTerm;
             term_             = term;
             isVoteInThisTerm_ = false;
             if (lastLogTerm < getLastLogTerm()) //log最后的term比我小
             {
                 return(ret);
             }
             if (lastLogTerm == getLastLogTerm() && lastLogIdx < log_.Count) //log最后的term和我一样,但是log没有我多
             {
                 return(ret);
             }
             //if(timeout_timer_ != null)
             //    timeout_timer_.Change(curRandTime_, curRandTime_);
             ret.VoteGranted = isVoteInThisTerm_ = true;
             return(ret);
         }
         else
         {
             ret.VoteGranted = false;
             return(ret);
         }
     }
 }
Beispiel #4
0
        public void SetRoles(Node node)
        {
            //foreach (NodeRole np in node.NodeRoles)
            //{
            //    node.RemoveNoderole(np.Id);
            //}

            foreach (RepeaterItem ri in rptRoles.Items)
            {
                CheckBox chkView = (CheckBox)ri.FindControl("chkViewAllowed");
                CheckBox chkEdit = (CheckBox)ri.FindControl("chkEditAllowed");
                if (chkView.Checked || chkEdit.Checked)
                {
                    if (!node.Exists(Convert.ToInt32(ViewState[ri.UniqueID])))
                    {
                        NodeRole np = new NodeRole();
                        np.Node        = node;
                        np.Role        = _presenter.GetRole((int)ViewState[ri.UniqueID]);
                        np.ViewAllowed = chkView.Checked;
                        np.EditAllowed = chkEdit.Checked;

                        node.NodeRoles.Add(np);
                    }
                }
            }
        }
 public IDtpNode CreateNode(NodeRole role, int port = -1, IEnumerable <IInstructionSet> instructionSets = null)
 {
     instructionSets = instructionSets == null ? kNoInstructionSets : instructionSets.ToArray();
     if (role == NodeRole.ServerOrClient)
     {
         IDtpNode result = null;
         while (result == null)
         {
             try {
                 result = CreateNode(NodeRole.Server, port, instructionSets);
             } catch {
                 try {
                     result = CreateNode(NodeRole.Client, -1, instructionSets).With(n => { n.Connect(port); });
                 } catch {
                     // gotta catch em all
                 }
             }
         }
         return(result);
     }
     else if (role == NodeRole.Client)
     {
         if (port != -1)
         {
             throw new InvalidOperationException("Use node.Connect, not CreateNode constructor to connect to server!");
         }
         return(new DtpNode(new NullClientSource(), instructionSets));
     }
     else
     {
         return(new DtpNode(new SocketClientSource(port), instructionSets));
     }
 }
Beispiel #6
0
 /// <summary>
 /// Node info
 /// </summary>
 /// <param name="id"></param>
 /// <param name="serviceUri"></param>
 /// <param name="state"></param>
 /// <param name="role"></param>
 public NodeInfo(string id, Uri serviceUri, NodeState state, NodeRole role)
 {
     Id         = id;
     ServiceUri = serviceUri;
     State      = state;
     Role       = role;
 }
Beispiel #7
0
 public SellerNode(SellerNode mainSeller)
 {
     version     = mainSeller.version;
     uri         = new Uri(mainSeller.uri.AbsoluteUri);
     machineName = mainSeller.machineName;
     role        = mainSeller.role;
 }
Beispiel #8
0
        public bool AddNode(IKey key, NodeRole nodeRole, IPAddress ipAddress)
        {
            AccountIdentity accountIdentity = GetAccountIdentity(key);

            if (accountIdentity == null)
            {
                GetOrAddIdentity(key);

                accountIdentity = GetAccountIdentity(key);
            }

            if (accountIdentity != null)
            {
                lock (_sync)
                {
                    NodeRecord node = _dataContext.Nodes.FirstOrDefault(n => n.Identity == accountIdentity && n.NodeRole == (byte)nodeRole);

                    if (node == null)
                    {
                        node = new NodeRecord {
                            Identity = accountIdentity, IPAddress = ipAddress.GetAddressBytes(), NodeRole = (byte)nodeRole
                        };
                        _dataContext.Nodes.Add(node);
                        if (!_keyToNodeMap.ContainsKey(key))
                        {
                            _keyToNodeMap.Add(key, node);
                        }
                        return(true);
                    }
                }
            }

            return(false);
        }
 /// <inheritdoc/>
 public override int GetHashCode()
 {
     unchecked
     {
         return((NodeIndex * 397) ^ (NodeRole.GetHashCode() * 397) ^ StartTime.GetHashCode());
     }
 }
Beispiel #10
0
 private void BecomeSecondary(bool changeStatus = true)
 {
     _currentRole = NodeRole.Secondary;
     if (changeStatus)
     {
         _status.SetStatusBit(UNINITIIALIZED, RUNNING);
     }
 }
Beispiel #11
0
        public bool AddNode(IKey key, NodeRole nodeRole, string ipAddressExpression = null)
        {
            if (IPAddress.TryParse(ipAddressExpression ?? "127.0.0.1", out IPAddress ipAddress))
            {
                return(AddNode(key, nodeRole, ipAddress));
            }

            return(false);
        }
Beispiel #12
0
 /// <summary>
 /// Internal constructor used by RoadSystem
 /// </summary>
 /// <param name="id"></param>
 /// <param name="crashed"></param>
 /// <param name="role"></param>
 internal RoadNode(int id,
     bool crashed = false,
     NodeRole role = NodeRole.Normal)
 {
     this.ID = id;
     this.Crashed = crashed;
     this.Role = role;
     this.Links = new LinkCollection();
 }
        public void SetNodeStatus(ServerNode primary, NodeRole status)
        {
            ManagementCommand command = GetManagementCommand(ConfigurationCommandUtil.MethodName.SetNodeStatus, 1);

            command.Parameters.AddParameter(primary);
            command.Parameters.AddParameter(status);

            ExecuteCommandOnConfigurationServer(command, false);
        }
Beispiel #14
0
 private void BecomeLeader()
 {
     if (Role == NodeRole.Candidate)
     {
         Role = NodeRole.Leader;
         var nextIndex  = new Dictionary <string, long>();
         var matchIndex = new Dictionary <string, long>();
         State = new LeaderState(State, nextIndex, matchIndex);
     }
 }
Beispiel #15
0
        private void BecomeFollower()
        {
            if (Role != NodeRole.Follower)
            {
                Role  = NodeRole.Follower;
                State = new NodeState(State.CurrentTerm, null, State.CommitIndex, State.LastApplied);

                StartElection();
            }
        }
Beispiel #16
0
        public async Task <ActionResult> NodeEnable(string node)
        {
            if (!Current.User.Is(Roles.ApiRequest))
            {
                return(JsonError("Invalid API key"));
            }

            await NodeRole.EnableAllAsync(node);

            return(NodeRoles(node));
        }
 public void SetNodeStatus(ServerNode primary, NodeRole status)
 {
     try
     {
         this.service.SetNodeStatus(this.Cluster, this.shard, primary, status);
         NotifyConfigurationChange(this.cluster);
     }
     catch (System.Exception ex)
     {
     }
 }
Beispiel #18
0
        public async Task <ActionResult> NodeDisable(string node)
        {
            if (!Current.IsInRole(Roles.ApiRequest))
            {
                return(JsonError("Invalid API key"));
            }

            await NodeRole.DisableAllAsync(node).ConfigureAwait(false);

            return(NodeRoles(node));
        }
Beispiel #19
0
        public NodeSession(Socket socket, NodeRole localRole)
        {
            this.socket    = socket;
            this.localRole = localRole;
            this.ns        = new NetworkStream(socket, FileAccess.ReadWrite, true);
            this.reader    = new BinaryReader(ns, Encoding.UTF8, true);
            this.writer    = new BinaryWriter(ns, Encoding.UTF8, true);

            ProcessHandshake();

            this.frameWriter = new FrameWriter(writer);
//         this.frameReader = new FrameReader(reader);
        }
Beispiel #20
0
        static public MemoryStream GenerateHandshake(NodeRole nr, params object[] info)
        {
            MemoryStream ms = new MemoryStream();

            List <object> args = new List <object>();

            args.Add(nr);
            args.AddRange(info);

            Serializer.Serialize(ms, args.ToArray());

            return(ms);
        }
Beispiel #21
0
 public NodeResults(string node)
 {
     Roles = NodeRole.Get(node).ToList();
     foreach (var r in Roles)
     {
         if (r.Active)
         {
             Active++;
         }
         else
         {
             Inactive++;
         }
     }
 }
        internal DtpNodeSession(DtpNode node, Stream connection, NodeRole localRole)
        {
            Trace.Assert(localRole.HasFlag(NodeRole.Client) != localRole.HasFlag(NodeRole.Server));

            aliveCancellationToken = aliveCancellationTokenSource.Token;

            m_node       = node;
            m_connection = connection;
            m_localRole  = localRole;

            if (m_localRole == NodeRole.Client)
            {
                m_locallyInitiatedUidSet = new UniqueIdentificationSet(kClientLitIdLow, kClientLitIdHigh);
            }
            else // Server
            {
                m_locallyInitiatedUidSet = new UniqueIdentificationSet(kServerLitIdLow, kServerLitIdHigh);
            }

            // - Setup Frame Processing --------------------------------------------------------------
            // : Setup Frame Processors :
            InitializeFrameProcessors(Environment.ProcessorCount);

            // : Setup Reader/Writer Workers :
            m_reader            = new BinaryReader(m_connection, Encoding.Default, true);
            m_frameReaderThread = new Thread(FrameReaderThreadStart)
            {
                IsBackground = true
            };
            m_frameReaderThread.Start();

            m_writer            = new BinaryWriter(m_connection, Encoding.Default, true);
            m_frameWriterThread = new Thread(FrameWriterThreadStart)
            {
                IsBackground = true
            };
            m_frameWriterThread.Start();

            // If we're the client, Send DSP_EX_INIT to elevate from DSP to DSPEx
            // If we're the server, the DSP_EX_INIT opcode will be read by the frame reader thread.
            if (m_localRole == NodeRole.Client)
            {
                m_writer.Write((byte)DTP.DSPEX_INIT);
            }
        }
 public IDtpNode CreateNode(NodeRole role, string pipeName, IEnumerable <IInstructionSet> instructionSets = null)
 {
     if (role == NodeRole.ServerOrClient)
     {
         throw new ArgumentException("Cannot use " + role + " with named pipe constructor.");
     }
     else if (role == NodeRole.Client)
     {
         if (pipeName != null)
         {
             throw new InvalidOperationException("Use node.Connect, not CreateNode constructor!");
         }
         return(new DtpNode(new NullClientSource(), instructionSets));
     }
     else
     {
         return(new DtpNode(new NamedPipeClientSource(pipeName), instructionSets));
     }
 }
Beispiel #24
0
 public void StartTimer()
 {
     curRandTime_ = rand_.Next(Config.Heartbeat_interval * 3, Config.Heartbeat_interval * 4);
     Console.WriteLine(String.Format("{0} [{1}] current timer = {2}", id, role, curRandTime_));
     timeout_timer_ = new Timer(
         (Object obj) => {
         lock (this)
         {
             role_             = NodeRole.Candidate;
             state_            = ServerState.Timeout;
             term_             = term_ + 1;
             isVoteInThisTerm_ = true;
             Console.WriteLine("{0} [{1}] ServerHandler {2}", id, role, state);
         }
     },
         null,
         curRandTime_,
         curRandTime_);
 }
Beispiel #25
0
        public Transform GetNodeTransform(NodeRole role = NodeRole.Default)
        {
            if (role == NodeRole.Root)
            {
                return(transform);
            }

            if (Nodes == null || Nodes.Length == 0)
            {
                return(transform);
            }

            var node = Nodes.FirstOrDefault(n => n.Role == role);

            if (node != null)
            {
                return(node.Transform);
            }

            return(Nodes[0].Transform);
        }
Beispiel #26
0
        public ClusterMonitor(NodeRole role, string localAddress, string partnerAddress,
                              Action <NodeState> stateChangedAction = null, Action <ClusterException> clusterExceptionAction = null,
                              IEnumerable <Func <NodeState, bool> > confirmActivationFuncs = null,
                              int partnerTimeout = 500, int connectTimeout = 0, bool becomeActiveWhenPrimaryOnInitialConnectionTimeout = false)
        {
            Role        = role;
            _localState = new InstanceState {
                Role = role, Status = NodeStatus.Initial
            };
            if (role == NodeRole.StandAlone)
            {
                return; //no need to run when standalone
            }
            _stateChangedAction     = stateChangedAction;
            _clusterExceptionAction = clusterExceptionAction;
            _machine = new ClusterStateMachine(HandleClusterException, CreateConfirmFunction(confirmActivationFuncs), becomeActiveWhenPrimaryOnInitialConnectionTimeout);
            _proxy   = new PubSubProxy(localAddress, partnerAddress, OnPartnerUpdate, OnConnectionStateChanged,
                                       connectTimeout);

            _partnerTimeout  = partnerTimeout;
            _pollingInterval = _partnerTimeout / 2;
            _timer           = new Timer(SendUpdateAndCheckForTimeout, null, Timeout.Infinite, _pollingInterval);
        }
        private Task TransitionTo(NodeRole newRole, IDictionary <string, string[]> hostArguments)
        {
            var startTasks = new ConcurrentBag <Task>();

            Parallel.ForEach(_appStarters, starter =>
            {
                AppFunc chain = null;

                if (Chains.ContainsKey(starter.Chain))
                {
                    chain = Chains[starter.Chain];
                }

                chain = chain ?? starter.GetDefaultChain(GetAppFunctionBuilder(starter.Chain), Environment, ApplicationEnvironment);

                if (chain != null)
                {
                    startTasks.Add(starter.BringToState(chain, Environment, ApplicationEnvironment, newRole, hostArguments));
                }
            });

            return(Task.WhenAll(startTasks));
        }
Beispiel #28
0
        public ServerHandler(int term, int _id, NodeRole _role, List <ClusterNodeSocket> _ExClusterNodeList)
        {
            TestNum = 0;

            id_                 = _id;
            role_               = _role;
            term_               = term;
            state_              = ServerState.Running;
            isVoteInThisTerm_   = false;
            leader_should_stop_ = false;
            is_election_done_   = false;
            lastApplied_        = -1; //与论文不同,论文下标从1开始,所以初始值为0
            commitIndex_        = -1;
            election_state_     = ElectionState.Stop;
            ExClusterNodeList_  = new List <ClusterNodeSocket>();
            ExClusterNodeList_  = _ExClusterNodeList;
            nextIndex_          = new int[ExClusterNodeList_.Count];
            matchIndex_         = new int[ExClusterNodeList_.Count];
            log_                = new List <LogEntryStruct>();
            rand_               = new Random();
            int seed = rand_.Next() * _id;

            rand_ = new Random(seed);
        }
 private static ClusterStateMachine CreateClusterStateMachine(MockStateObserver observer, NodeRole role,
     out InstanceState localState, Action<ClusterException> clusterExceptionAction = null)
 {
     var machine = new ClusterStateMachine(clusterExceptionAction ?? Assert.IsNull, s => true);
     machine.StateChanged.Subscribe(observer);
     localState = new InstanceState {Role = role};
     machine.RaiseEvent(localState, machine.Start);
     return machine;
 }
 public ClusterMonitorBuilder WithRole(NodeRole role)
 {
     _role = role;
     return this;
 }
Beispiel #31
0
        private void TransitionTo(NodeRole newState)
        {
            _logger.Info(Host + " Change {" + State + "} => {" + newState + "} " + CurrentTerm);

            State = newState;
        }
 public void TestCreateNewNode(int id, bool crashed, NodeRole role)
 {
     var newNode = _roadSystem.CreateNewNode(id, crashed, role);
     Assert.AreEqual(id, newNode.ID);
     Assert.AreEqual(crashed, newNode.Crashed);
     Assert.AreEqual(role, newNode.Role);
     Assert.IsTrue(_roadSystem.Contains(newNode));
     Assert.AreSame(_roadSystem[id], newNode);
 }
 private static ClusterStateMachine CreateClusterStateMachine(MockStateObserver observer, NodeRole role,
     out InstanceState localState, Action<ClusterException> clusterExceptionAction = null, bool becomeActiveWhenPrimaryOnInitialConnectionTimeout = false)
 {
     var machine = new ClusterStateMachine(clusterExceptionAction ?? Assert.IsNull, s => true, becomeActiveWhenPrimaryOnInitialConnectionTimeout);
     machine.StateChanged.Subscribe(observer);
     localState = new InstanceState { Role = role };
     machine.RaiseEvent(localState, machine.Start);
     return machine;
 }
Beispiel #34
0
        protected GameNodeProcessors AssignProcessor(Node n, MemoryStream nodeInfo)
        {
            NodeRole role = Serializer.Deserialize <NodeRole>(nodeInfo);

            if (role == NodeRole.CLIENT)
            {
                if (!AuthorizeClient(n))
                {
                    throw new GameNodeException(Log.StDump(n.info, role));
                }

                return(new GameNodeProcessors(ProcessClientMessage, ProcessClientDisconnect));
            }

            if (role == NodeRole.SERVER)
            {
                if (n.info.remote != serverHost)
                {
                    throw new GameNodeException(Log.StDump(n.info, role, "bad server host"));
                }

                if (!AuthorizeServer(n))
                {
                    throw new GameNodeException(Log.StDump(n.info, role));
                }

                return(new GameNodeProcessors(ProcessServerMessage, ProcessServerDisconnect));
            }

            if (role == NodeRole.PLAYER_VALIDATOR)
            {
                PlayerInfo inf = Serializer.Deserialize <PlayerInfo>(nodeInfo);

                if (!AuthorizePlayerValidator(n, inf))
                {
                    throw new GameNodeException(Log.StDump(role, n.info, inf));
                }

                return(new GameNodeProcessors(
                           (mt, stm, nd) => ProcessPlayerValidatorMessage(mt, stm, nd, inf),
                           (di) => ProcessPlayerValidatorDisconnect(di, inf)));
            }

            if (role == NodeRole.PLAYER_AGENT)
            {
                PlayerInfo inf = Serializer.Deserialize <PlayerInfo>(nodeInfo);

                if (!AuthorizePlayerAgent(n, inf))
                {
                    throw new GameNodeException(Log.StDump(role, n.info, inf));
                }

                return(new GameNodeProcessors(
                           (mt, stm, nd) => ProcessPlayerAgentMessage(mt, stm, nd, inf),
                           (di) => ProcessPlayerAgentDisconnect(di, inf)));
            }

            if (role == NodeRole.WORLD_VALIDATOR)
            {
                WorldInfo inf = Serializer.Deserialize <WorldInfo>(nodeInfo);

                if (!AuthorizeWorld(n, inf))
                {
                    throw new GameNodeException(Log.StDump(role, n.info, inf));
                }

                return(new GameNodeProcessors(
                           (mt, stm, nd) => ProcessWorldMessage(mt, stm, nd, inf),
                           (di) => ProcessWorldDisconnect(di, inf)));
            }

            throw new GameNodeException(Log.StDump(n.info, role, "unexpected"));
        }
Beispiel #35
0
        private static ClusterStateMachine CreateClusterStateMachine(MockStateObserver observer, NodeRole role,
                                                                     out InstanceState localState, Action <ClusterException> clusterExceptionAction = null, bool becomeActiveWhenPrimaryOnInitialConnectionTimeout = false)
        {
            var machine = new ClusterStateMachine(clusterExceptionAction ?? Assert.IsNull, s => true, becomeActiveWhenPrimaryOnInitialConnectionTimeout);

            machine.StateChanged.Subscribe(observer);
            localState = new InstanceState {
                Role = role
            };
            machine.RaiseEvent(localState, machine.Start);
            return(machine);
        }
 public void Read (TProtocol iprot)
 {
   TField field;
   iprot.ReadStructBegin();
   while (true)
   {
     field = iprot.ReadFieldBegin();
     if (field.Type == TType.Stop) { 
       break;
     }
     switch (field.ID)
     {
       case 1:
         if (field.Type == TType.I32) {
           HttpPort = iprot.ReadI32();
         } else { 
           TProtocolUtil.Skip(iprot, field.Type);
         }
         break;
       case 2:
         if (field.Type == TType.I32) {
           ThriftPort = iprot.ReadI32();
         } else { 
           TProtocolUtil.Skip(iprot, field.Type);
         }
         break;
       case 3:
         if (field.Type == TType.String) {
           DataFolder = iprot.ReadString();
         } else { 
           TProtocolUtil.Skip(iprot, field.Type);
         }
         break;
       case 4:
         if (field.Type == TType.String) {
           PluginFolder = iprot.ReadString();
         } else { 
           TProtocolUtil.Skip(iprot, field.Type);
         }
         break;
       case 5:
         if (field.Type == TType.String) {
           ConfFolder = iprot.ReadString();
         } else { 
           TProtocolUtil.Skip(iprot, field.Type);
         }
         break;
       case 6:
         if (field.Type == TType.String) {
           NodeName = iprot.ReadString();
         } else { 
           TProtocolUtil.Skip(iprot, field.Type);
         }
         break;
       case 7:
         if (field.Type == TType.I32) {
           NodeRole = (NodeRole)iprot.ReadI32();
         } else { 
           TProtocolUtil.Skip(iprot, field.Type);
         }
         break;
       default: 
         TProtocolUtil.Skip(iprot, field.Type);
         break;
     }
     iprot.ReadFieldEnd();
   }
   iprot.ReadStructEnd();
 }
 public ServerSettings() {
   this._HttpPort = 9800;
   this.__isset.HttpPort = true;
   this._ThriftPort = 9900;
   this.__isset.ThriftPort = true;
   this._DataFolder = "./data";
   this.__isset.DataFolder = true;
   this._PluginFolder = "./plugins";
   this.__isset.PluginFolder = true;
   this._ConfFolder = "./conf";
   this.__isset.ConfFolder = true;
   this._NodeName = "FlexNode";
   this.__isset.NodeName = true;
   this._NodeRole = NodeRole.Master;
   this.__isset.NodeRole = true;
 }
Beispiel #38
0
 public CurrentPlatformNodeDto(short pNodeId, NodeRole pNodeRole, bool pBelongsToStandalonePlatform)
 {
     id       = pNodeId;
     nodeRole = pNodeRole;
     belongsToStandalonePlatform = pBelongsToStandalonePlatform;
 }