Пример #1
0
        public bool OnMembershipChanged(MembershipChangeArgs args)
        {
            if (args != null)
            {
                if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsDebugEnabled)
                {
                    LoggerManager.Instance.ShardLogger.Debug("LocalShard.OnMembershipChanged()", "Membership change type: " + args.ChangeType);
                }

                switch (args.ChangeType)
                {
                case MembershipChangeArgs.MembershipChangeType.PrimarySet:
                case MembershipChangeArgs.MembershipChangeType.PrimarySelected:
                    if (args.ServerName != null)
                    {
                        if (args.ServerName.Equals(context.LocalAddress))
                        {
                            lock (_mutexOnnodeRole)
                            {
                                NodeRole = Common.Configuration.Services.NodeRole.Primary;
                            }
                        }
                        else
                        {
                            lock (_mutexOnnodeRole)
                            {
                                if (NodeRole != NodeRole.Intermediate)
                                {
                                    NodeRole = Common.Configuration.Services.NodeRole.Secondary;
                                }
                            }
                        }
                        Primary = new Server(args.ServerName, Status.Running);

                        if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsInfoEnabled && args.ElectionId != null)
                        {
                            LoggerManager.Instance.ShardLogger.Info("LocalShard.OnMembershipChanged()", "This term's election id is: " + args.ElectionId.Id);
                        }


                        if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsInfoEnabled)
                        {
                            if (args.ChangeType.Equals(MembershipChangeArgs.MembershipChangeType.PrimarySet))
                            {
                                LoggerManager.Instance.ShardLogger.Info("LocalShard.OnMembershipChanged()", "Node " + args.ServerName.IpAddress.ToString() + " set as the primary node for the shard.");
                            }
                            else if (args.ChangeType.Equals(MembershipChangeArgs.MembershipChangeType.PrimarySelected))
                            {
                                LoggerManager.Instance.ShardLogger.Info("LocalShard.OnMembershipChanged()", "Node " + args.ServerName.IpAddress.ToString() + " selected as the primary node for the shard.");
                            }
                        }

                        AppUtil.LogEvent(AppUtil.EventLogSource, string.Format("Node {0} is selected as primary for shard \"{1}\"", args.ServerName.ToString(), context.LocalShardName),
                                         EventLogEntryType.Information, EventCategories.Information, EventID.PrimaySelected);
                    }
                    break;

                case MembershipChangeArgs.MembershipChangeType.PrimaryLost:
                case MembershipChangeArgs.MembershipChangeType.PrimaryDemoted:
                case MembershipChangeArgs.MembershipChangeType.NodeLeft:

                    if (args.ServerName != null && Primary != null && args.ServerName.Equals(Primary.Address))
                    {
                        lock (_mutexOnnodeRole)
                        {
                            if (NodeRole != NodeRole.Intermediate)
                            {
                                NodeRole = Common.Configuration.Services.NodeRole.None;
                            }
                        }
                        Primary = null;

                        if (args.ServerName != null)
                        {
                            if (args.ChangeType.Equals(MembershipChangeArgs.MembershipChangeType.PrimaryDemoted))
                            {
                                AppUtil.LogEvent(AppUtil.EventLogSource, string.Format("Primary Node {0} is demoted for shard \"{1}\"", args.ServerName.ToString(), context.LocalShardName),
                                                 EventLogEntryType.Warning, EventCategories.Warning, EventID.PrimaryLost);

                                if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsWarnEnabled)
                                {
                                    LoggerManager.Instance.ShardLogger.Warn("LocalShard.OnMembershipChanged()", "The primary " + args.ServerName.ToString() + " is demoted.");
                                }
                            }

                            else if (args.ChangeType.Equals(MembershipChangeArgs.MembershipChangeType.PrimaryLost))
                            {
                                AppUtil.LogEvent(AppUtil.EventLogSource, string.Format("Connection with the primary node {0} lost \"{1}\"", args.ServerName.ToString(), context.LocalShardName),
                                                 EventLogEntryType.Warning, EventCategories.Warning, EventID.PrimaryLost);
                                if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsWarnEnabled)
                                {
                                    LoggerManager.Instance.ShardLogger.Warn("LocalShard.OnMembershipChanged()", "The primary " + args.ServerName.ToString() + " is lost.");
                                }
                            }
                        }
                    }

                    _clusterConfigMgr.UpdateClusterConfiguration();
                    if (args.ServerName != null)
                    {
                        ShardConfiguration sConfig = null;
                        if (_clusterConfigMgr != null && _clusterConfigMgr.LatestConfiguration != null && _clusterConfigMgr.LatestConfiguration.Deployment != null)
                        {
                            sConfig = _clusterConfigMgr.LatestConfiguration.Deployment.GetShardConfiguration(context.LocalShardName);
                        }
                        {
                            ServerNode node = null;
                            if (sConfig != null && sConfig.Servers != null)
                            {
                                node = sConfig.Servers.GetServerNode(args.ServerName.IpAddress.ToString());
                            }
                            if (node == null)
                            {
                                if (_connectionRestoration != null)
                                {
                                    BrokenConnectionInfo info = new BrokenConnectionInfo();
                                    info.BrokenAddress = args.ServerName;
                                    info.SessionType   = SessionTypes.Shard;
                                    _connectionRestoration.UnregisterListener(info);
                                }
                            }
                        }
                    }
                    break;

                case MembershipChangeArgs.MembershipChangeType.TimeoutOnRestrictedPrimary:
                    return(_membershipManager.AbortTakeoverMechanismTask(args));

                case MembershipChangeArgs.MembershipChangeType.ForcefullyDemotePrimary:
                    return(_membershipManager.OnForcefulPrimaryDemotion(args));
                }

                if (_membershipManager != null)
                {
                    _membershipManager.UpdateLocalMembership(args);
                    if (context != null && context.DatabasesManager != null &&
                        (args.ChangeType == MembershipChangeArgs.MembershipChangeType.PrimarySet ||
                         (args.ChangeType == MembershipChangeArgs.MembershipChangeType.PrimarySelected)))
                    {
                        context.DatabasesManager.ElectionResult = _membershipManager.LatestMembership.ElectionId;
                        context.ElectionResult            = new ElectionResult();
                        context.ElectionResult.ElectionId = _membershipManager.LatestMembership.ElectionId;
                    }

                    DatabaseMessage primaryChangedMessage = new DatabaseMessage();
                    primaryChangedMessage.OpCode = OpCode.PrimaryChanged;
                    IShardListener listener = _shardListeners[Common.MiscUtil.CLUSTER_MANAGER];
                    listener.OnMessageReceived(primaryChangedMessage, new Server(context.LocalAddress, Status.Running));
                }
            }
            return(false);
        }