Пример #1
0
        private void Init(ClusterEvent.CurrentClusterState snapshot)
        {
            var nodes       = snapshot.Members.Select(x => x.UniqueAddress).ToImmutableHashSet();
            var unreachable = snapshot.Unreachable.Select(c => c.UniqueAddress).ToImmutableHashSet();

            _state = _state.Init(nodes, unreachable);
        }
Пример #2
0
        /// <summary>
        /// The current snapshot state corresponding to latest gossip
        /// to mimic what you would have seen if you were listening to the events.
        /// </summary>
        private void SendCurrentClusterState(ActorRef receiver)
        {
            var state = new ClusterEvent.CurrentClusterState(
                _latestGossip.Members,
                _latestGossip.Overview.Reachability.AllUnreachableOrTerminated.Select(_latestGossip.GetMember)
                .ToImmutableHashSet(),
                _latestGossip.SeenBy.Select(s => s.Address).ToImmutableHashSet(),
                _latestGossip.Leader == null ? null : _latestGossip.Leader.Address,
                _latestGossip.AllRoles.ToImmutableDictionary(r => r, r => _latestGossip.RoleLeader(r).Address)
                );

            receiver.Tell(state);
        }
Пример #3
0
        /// <summary>
        /// TBD
        /// </summary>
        /// <param name="cluster">TBD</param>
        public ClusterReadView(Cluster cluster)
        {
            _cluster      = cluster;
            _state        = new ClusterEvent.CurrentClusterState();
            _reachability = Reachability.Empty;
            _latestStats  = new ClusterEvent.CurrentInternalStats(new GossipStats(), new VectorClockStats());
            _selfAddress  = cluster.SelfAddress;

            _eventBusListener =
                cluster.System.SystemActorOf(
                    Props.Create(() => new EventBusListener(cluster, this))
                    .WithDispatcher(cluster.Settings.UseDispatcher)
                    .WithDeploy(Deploy.Local), "clusterEventBusListener");
        }
Пример #4
0
        public ClusterReadView(Cluster cluster)
        {
            _cluster = cluster;
            _state = new ClusterEvent.CurrentClusterState();
            _reachability = Reachability.Empty;
            _latestStats = new ClusterEvent.CurrentInternalStats(new GossipStats(), new VectorClockStats());
            _selfAddress = cluster.SelfAddress;

            _eventBusListener =
                cluster.System.SystemActorOf(
                    Props.Create(() => new EventBusListener(cluster, this))
                        .WithDispatcher(cluster.Settings.UseDispatcher)
                        .WithDeploy(Deploy.Local), "clusterEventBusListener");
        }
        public HttpResponseMessage Get(string id)
        {
            var t1 = Program.ClusterStatus.Ask(new ClusterStatus.GetClusterState(), TimeSpan.FromSeconds(2))
                .ContinueWith(
                        tr =>
                        {
                            ClusterState = (ClusterEvent.CurrentClusterState)tr.Result;
                        }, TaskContinuationOptions.AttachedToParent & TaskContinuationOptions.ExecuteSynchronously);

            t1.Wait(TimeSpan.FromSeconds(2));


            var retval = Request.CreateResponse(HttpStatusCode.OK, new
            {
                clusterState = ClusterState
            });
            return retval;
        }
Пример #6
0
        /// <summary>
        /// The current snapshot state corresponding to latest gossip 
        /// to mimic what you would have seen if you were listening to the events.
        /// </summary>
        private void SendCurrentClusterState(IActorRef receiver)
        {
            var unreachable = _latestGossip.Overview.Reachability.AllUnreachableOrTerminated
                .Where(node => !node.Equals(_selfUniqueAddress))
                .Select(node => _latestGossip.GetMember(node))
                .ToImmutableHashSet();

            var state = new ClusterEvent.CurrentClusterState(
                members: _latestGossip.Members,
                unreachable: unreachable,
                seenBy: _latestGossip.SeenBy.Select(s => s.Address).ToImmutableHashSet(),
                leader: _latestGossip.Leader(_selfUniqueAddress) == null ? null : _latestGossip.Leader(_selfUniqueAddress).Address,
                roleLeaderMap: _latestGossip.AllRoles.ToImmutableDictionary(r => r, r =>
                {
                    var leader = _latestGossip.RoleLeader(r, _selfUniqueAddress);
                    return leader == null ? null : leader.Address;
                }));
            receiver.Tell(state);
        }
Пример #7
0
        /// <summary>
        /// The current snapshot state corresponding to latest gossip
        /// to mimic what you would have seen if you were listening to the events.
        /// </summary>
        private void SendCurrentClusterState(IActorRef receiver)
        {
            var unreachable = _membershipState.LatestGossip.Overview.Reachability
                              .AllUnreachableOrTerminated.Where(x => x != _selfUniqueAddress)
                              .Select(x => _membershipState.LatestGossip.GetMember(x))
                              .ToImmutableHashSet();

            var state = new ClusterEvent.CurrentClusterState(
                members: _membershipState.Members,
                unreachable: unreachable,
                seenBy: _membershipState.LatestGossip.SeenBy.Select(s => s.Address).ToImmutableHashSet(),
                leader: _membershipState.Leader?.Address,
                roleLeaderMap: _membershipState.LatestGossip.AllRoles
                .ToImmutableDictionary(r => r, r =>
            {
                var leader = _membershipState.RoleLeader(r);
                return(leader?.Address);
            }));

            receiver.Tell(state);
        }
Пример #8
0
            public EventBusListener(Cluster cluster, ClusterReadView readView)
            {
                _cluster = cluster;
                _readView = readView;

                Receive<ClusterEvent.IClusterDomainEvent>(clusterDomainEvent =>
                {
                    clusterDomainEvent.Match()
                        .With<ClusterEvent.SeenChanged>(changed =>
                        {
                            State = State.Copy(seenBy: changed.SeenBy);
                        })
                        .With<ClusterEvent.ReachabilityChanged>(changed =>
                        {
                            _readView._reachability = changed.Reachability;
                        })
                        .With<ClusterEvent.MemberRemoved>(removed =>
                        {
                            State = State.Copy(members: State.Members.Remove(removed.Member),
                                unreachable: State.Unreachable.Remove(removed.Member));
                        })
                        .With<ClusterEvent.UnreachableMember>(member =>
                        {
                            // replace current member with new member (might have different status, only address is used in == comparison)
                            State = State.Copy(unreachable: State.Unreachable.Remove(member.Member).Add(member.Member));
                        })
                        .With<ClusterEvent.ReachableMember>(member =>
                        {
                            State = State.Copy(unreachable: State.Unreachable.Remove(member.Member));
                        })
                        .With<ClusterEvent.IMemberEvent>(memberEvent =>
                        {
                            var newUnreachable = State.Unreachable;
                            // replace current member with new member (might have different status, only address is used in == comparison)
                            if (State.Unreachable.Contains(memberEvent.Member))
                                newUnreachable = State.Unreachable.Remove(memberEvent.Member).Add(memberEvent.Member);
                            State = State.Copy(
                                members: State.Members.Remove(memberEvent.Member).Add(memberEvent.Member),
                                unreachable: newUnreachable);
                        })
                        .With<ClusterEvent.LeaderChanged>(changed =>
                        {
                            State = State.Copy(leader: changed.Leader);
                        })
                        .With<ClusterEvent.RoleLeaderChanged>(changed =>
                        {
                            State = State.Copy(roleLeaderMap: State.RoleLeaderMap.SetItem(changed.Role, changed.Leader));
                        })
                        .With<ClusterEvent.CurrentInternalStats>(stats =>
                        {
                            readView._latestStats = stats;
                        })
                        .With<ClusterEvent.ClusterMetricsChanged>(changed =>
                        {
                            readView._clusterMetrics = changed.NodeMetrics;
                        })
                        .With<ClusterEvent.ClusterShuttingDown>(_ => { });
                });

                Receive<ClusterEvent.CurrentClusterState>(state =>
                {
                    State = state;
                });
            }
Пример #9
0
 /// <summary>
 /// Update the initial node ring for those nodes that are <see cref="MemberStatus.Up"/>
 /// </summary>
 private void ReceiveState(ClusterEvent.CurrentClusterState state)
 {
     Nodes = state.Members.Where(x => x.Status == MemberStatus.Up).Select(x => x.Address).ToImmutableHashSet();
 }
Пример #10
0
            public EventBusListener(Cluster cluster, ClusterReadView readView)
            {
                _cluster  = cluster;
                _readView = readView;

                Receive <ClusterEvent.IClusterDomainEvent>(clusterDomainEvent =>
                {
                    clusterDomainEvent.Match()
                    .With <ClusterEvent.SeenChanged>(changed =>
                    {
                        State = State.Copy(seenBy: changed.SeenBy);
                    })
                    .With <ClusterEvent.ReachabilityChanged>(changed =>
                    {
                        _readView._reachability = changed.Reachability;
                    })
                    .With <ClusterEvent.MemberRemoved>(removed =>
                    {
                        State = State.Copy(members: State.Members.Remove(removed.Member),
                                           unreachable: State.Unreachable.Remove(removed.Member));
                    })
                    .With <ClusterEvent.UnreachableMember>(member =>
                    {
                        // replace current member with new member (might have different status, only address is used in == comparison)
                        State = State.Copy(unreachable: State.Unreachable.Remove(member.Member).Add(member.Member));
                    })
                    .With <ClusterEvent.ReachableMember>(member =>
                    {
                        State = State.Copy(unreachable: State.Unreachable.Remove(member.Member));
                    })
                    .With <ClusterEvent.IMemberEvent>(memberEvent =>
                    {
                        var newUnreachable = State.Unreachable;
                        // replace current member with new member (might have different status, only address is used in == comparison)
                        if (State.Unreachable.Contains(memberEvent.Member))
                        {
                            newUnreachable = State.Unreachable.Remove(memberEvent.Member).Add(memberEvent.Member);
                        }
                        State = State.Copy(
                            members: State.Members.Remove(memberEvent.Member).Add(memberEvent.Member),
                            unreachable: newUnreachable);
                    })
                    .With <ClusterEvent.LeaderChanged>(changed =>
                    {
                        State = State.Copy(leader: changed.Leader);
                    })
                    .With <ClusterEvent.RoleLeaderChanged>(changed =>
                    {
                        State = State.Copy(roleLeaderMap: State.RoleLeaderMap.SetItem(changed.Role, changed.Leader));
                    })
                    .With <ClusterEvent.CurrentInternalStats>(stats =>
                    {
                        readView._latestStats = stats;
                    })
                    .With <ClusterEvent.ClusterShuttingDown>(_ => { });
                });

                Receive <ClusterEvent.CurrentClusterState>(state =>
                {
                    State = state;
                });
            }
Пример #11
0
        /// <summary>
        /// The current snapshot state corresponding to latest gossip 
        /// to mimic what you would have seen if you were listening to the events.
        /// </summary>
        private void SendCurrentClusterState(IActorRef receiver)
        {
            var unreachable = _latestGossip.Overview.Reachability.AllUnreachableOrTerminated
                .Where(node => node != _selfUniqueAddress)
                .Select(_latestGossip.GetMember)
                .ToImmutableHashSet();

            var state = new ClusterEvent.CurrentClusterState(
                _latestGossip.Members,
                unreachable,
                _latestGossip.SeenBy.Select(s => s.Address).ToImmutableHashSet(),
                _latestGossip.Leader(_selfUniqueAddress) == null ? null : _latestGossip.Leader(_selfUniqueAddress).Address,
                _latestGossip.AllRoles.ToImmutableDictionary(r => r, r =>
                {
                    var leader = _latestGossip.RoleLeader(r, _selfUniqueAddress);
                    return leader == null ? null : leader.Address;
                }));
            receiver.Tell(state);
        }
Пример #12
0
        private void Init(ClusterEvent.CurrentClusterState snapshot)
        {
            var nodes = snapshot.Members.Where(x => x.Status == MemberStatus.Up).Select(x => x.UniqueAddress).ToImmutableHashSet();

            _state = _state.Init(nodes);
        }
Пример #13
0
        private void Ready()
        {
            Receive<SubscribeToManager>(ic =>
            {
                if (!_clients.Contains(Sender))
                {
                    _clients.Add(this.Sender);
                    Sender.Tell(new ClusterMessage(string.Format("Subscribed to cluster events : {0}", Sender.ToString())));
                    return;
                }

                Sender.Tell(new ClusterMessage(string.Format("You have already subscribed to cluster events : {0}", Sender.ToString())));
            });

            Receive<UnSubscribeFromManager>(ic =>
            {
                if (_clients.Contains(Sender))
                {
                    _clients.Remove(this.Sender);
                    Sender.Tell(new ClusterMessage(string.Format("Unsubscribed to cluster events : {0}", Sender.ToString())));
                    return;
                }

                Sender.Tell(new ClusterMessage(string.Format("You must subscribe before you can unsubscribe : {0}", Sender.ToString())));
            });

            Receive<MemberDown>(ic =>
            {
                var message = string.Format("User {0} is forcing the following member down: {1}", ic.UserName, ic.Address.ToString());
                _logger.Warning(message);
                Sender.Tell(new ClusterMessage(message));
                Cluster.Down(ic.Address);

                Thread.Sleep(5000);
            });

            Receive<MemberLeave>(ic =>
            {
                var message = string.Format("User {0} is asking the following member to leave the cluster: {1}", ic.UserName, ic.Address.ToString());
                _logger.Warning(message);
                Sender.Tell(new ClusterMessage(message));
                Cluster.Leave(ic.Address);

                Thread.Sleep(5000);
            });

            Receive<ClusterEvent.CurrentClusterState>(state =>
            {
                _currentClusterState = state;
                foreach (var client in _clients)
                {
                    _logger.Info("Sending ClusterState to {0}", this.Sender.ToString());
                    client.Tell(_currentClusterState, Self);
                }
            });

            Receive<GetClusterState>(ic =>
            {
                _logger.Info("Sending ClusterState to {0}", this.Sender.ToString());
                Sender.Tell(_currentClusterState, Self);
            });

            Receive<ClusterEvent.IReachabilityEvent>(mem =>
            {
                foreach (var client in _clients)
                {
                    client.Tell(mem, Self);
                }
            });

            Receive<ClusterEvent.IMemberEvent>(mem =>
            {
                foreach (var client in _clients)
                {
                    client.Tell(mem, Self);
                }
            });

            Receive<SendCurrentClusterState>(ic =>
            {
                Cluster.SendCurrentClusterState(Self);
            });

            ReceiveAny(task =>
            {
                _logger.Error("Oh Snap! ClusterSender.Ready.ReceiveAny: \r\n{0}", task);
            });
        }
Пример #14
0
 /// <summary>
 /// The current snapshot state corresponding to latest gossip 
 /// to mimic what you would have seen if you were listening to the events.
 /// </summary>
 private void SendCurrentClusterState(ActorRef receiver)
 {
     var state = new ClusterEvent.CurrentClusterState(
         _latestGossip.Members,
         _latestGossip.Overview.Reachability.AllUnreachableOrTerminated.Select(_latestGossip.GetMember)
             .ToImmutableHashSet(),
         _latestGossip.SeenBy.Select(s => s.Address).ToImmutableHashSet(),
         _latestGossip.Leader == null ? null : _latestGossip.Leader.Address,
         _latestGossip.AllRoles.ToImmutableDictionary(r => r, r => _latestGossip.RoleLeader(r).Address)
         );
     receiver.Tell(state);
 }
        private void Ready()
        {
            Receive<GetClusterState>(ic =>
            {
                List<ClusterRoleLeader> clusterRoleLeaders = new List<ClusterRoleLeader>();
                foreach (var member in _clusterState.Members)
                {
                    var role = member.Roles.First();
                    var address = _clusterState.RoleLeader(role);
                    clusterRoleLeaders.Add(new ClusterRoleLeader(role, address));
                }

                var currentClusterStatus = new CurrentClusterStatus(clusterRoleLeaders, Cluster.SelfAddress,
                    _clusterState);

                Sender.Tell(currentClusterStatus);
            });

            Receive<ClusterEvent.CurrentClusterState>(state =>
            {
                _clusterState = state;
                
                // Check Cluster Leader
                if (state.Leader == null)
                {
                    _logger.Warning("ClusterLeader is null");
                }
                else
                {
                    _logger.Debug("ClusterLeader : {0}", state.Leader.ToString());
                }

                // Check Role Leaders
                var roles = _clusterState.Members.Where(y => y.Status == MemberStatus.Up).Select(x => x.Roles.First()).Distinct().ToList();
                foreach (var role in roles)
                {
                    var address = state.RoleLeader(role);
                    if (address == null)
                    {
                        _logger.Warning("RoleLeader: {0}, No leader found!", role);
                    }
                    else
                    {
                        _logger.Debug("RoleLeader: {0}, Address:{1}", role, address);
                    }
                }

                // Check Unreachable Members?
                foreach (var member in state.Unreachable)
                {
                    _logger.Warning("Unreachable Member; Role: {0}, Status: {1}, Address: {2}, ", member.Roles.Join(";"), member.Status, member.Address.ToString());
                }

                // Check who I am seen by?
                foreach (var seenBy in state.SeenBy)
                {
                    if (_clusterState.Members.Any(x => x.Address == seenBy))
                    {
                        _logger.Debug("SeenBy Members; Role: {0}, Status: {1}, Address: {2}, ",
                            _clusterState.Members.First(x => x.Address == seenBy).Roles.Join(";"),
                            _clusterState.Members.First(x => x.Address == seenBy).Status,
                            _clusterState.Members.First(x => x.Address == seenBy).Address.ToString());
                    }
                    else
                    {
                        _logger.Debug("SeenBy Members; Role: null, Status: null, Address: {0}, ",
                            _clusterState.Members.First(x => x.Address == seenBy).Address.ToString());
                    }

                }
            });

            Receive<SendCurrentClusterState>(ic =>
            {
                Cluster.SendCurrentClusterState(Self);
            });

            Receive<ClusterEvent.MemberUp>(mem =>
            {
                _logger.Info("MemberUp: {0}, Role(s): {1}", mem.Member, mem.Member.Roles.Join(","));
            });

            Receive<ClusterEvent.UnreachableMember>(mem =>
            {
                _logger.Info("UnreachableMember: {0}, Role(s): {1}", mem.Member, mem.Member.Roles.Join(","));
            });

            Receive<ClusterEvent.ReachableMember>(mem =>
            {
                _logger.Info("ReachableMember: {0}, Role(s): {1}", mem.Member, mem.Member.Roles.Join(","));
            });

            Receive<ClusterEvent.MemberRemoved>(mem =>
            {
                _logger.Info("MemberRemoved: {0}, Role(s): {1}", mem.Member, mem.Member.Roles.Join(","));
                
                // Check to see if we have been removed?
                if (Cluster.SelfAddress.Equals(mem.Member.Address))
                {
                    ClusterNeedsToRestart(string.Format("This member has been removed from the cluster.  This system needs to be restarted. Address:{0} ", Cluster.SelfAddress));
                }

            });

            Receive<ClusterEvent.IMemberEvent>(mem =>
            {
                _logger.Info("IMemberEvent: {0}, Role(s): {1}", mem.Member, mem.Member.Roles.Join(","));
            });

            Receive<ClusterEvent.ClusterShuttingDown>(cluster =>
            {
                _logger.Warning("ClusterShuttingDown");
            });
            
            ReceiveAny(task =>
            {
                _logger.Error("{{EventId:999}} [x] Oh Snap! ClusterStatus.Ready.ReceiveAny: \r\n{0}", task);
            });
        }