Example #1
0
        public void ChannelDisconnected(IRequestResponseChannel channel, string reason)
        {
            try
            {
                if (channel != null && ((IDualChannel)channel).ShouldTryReconnecting && _primary != null && _primary.Address.Equals(channel.PeerAddress))
                {
                    if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsInfoEnabled)
                    {
                        LoggerManager.Instance.ShardLogger.Info("RemoteShard.ChannelDisconnected.ShoudTryReconnecting", ((IDualChannel)channel).ShouldTryReconnecting.ToString());
                    }

                    lock (_onChannel)
                    {
                        _remoteShardChannel = null;
                    }
                    BrokenConnectionInfo info = new BrokenConnectionInfo();
                    info.BrokenAddress = channel.PeerAddress;
                    info.SessionType   = SessionTypes.Shard;

                    _connectionRestoration.RegisterListener(info, this, context.LocalShardName);
                }
            }
            catch (Exception e)
            {
                if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsErrorEnabled)
                {
                    LoggerManager.Instance.ShardLogger.Error("RemoteShard.ChannelDisconnected()", e.ToString());
                }
            }
        }
Example #2
0
        /// <summary>
        /// Removes the broken connection of the shard from the restoration manager.
        /// </summary>
        public void RemoveBrokenConnection()
        {
            if (_primary != null)
            {
                BrokenConnectionInfo info = new BrokenConnectionInfo
                {
                    BrokenAddress = _primary.Address,
                    SessionType   = SessionTypes.Shard
                };

                _connectionRestoration.UnregisterListener(info);
            }
        }
Example #3
0
        public void ChannelDisconnected(IRequestResponseChannel channel, string reason)
        {
            try
            {
                if (_shardChannels != null)
                {
                    bool connected = false;

                    if (channel != null)
                    {
                        Server server = new Server(channel.PeerAddress, Status.Stopped);
                        Server key    = null;
                        if (_shardChannels != null)
                        {
                            IList <Server> shardchannelKeys = _shardChannels.Keys.ToList();
                            if (server != null && server.Address != null && !server.Address.Equals(context.LocalAddress) && shardchannelKeys != null && shardchannelKeys.Count > 0)
                            {
                                foreach (Server node in shardchannelKeys)
                                {
                                    if (_shardChannels[node].PeerAddress != null && _shardChannels[node].PeerAddress.Equals(channel.PeerAddress))
                                    {
                                        key = node;
                                        break;
                                    }
                                }
                            }
                            IDualChannel tempChannel = channel as IDualChannel;
                            if (tempChannel != null && key != null && tempChannel.ShouldTryReconnecting)
                            {
                                lock (_shardChannels)
                                {
                                    //_shardChannels[key].Disconnect();
                                    _shardChannels.Remove(key);
                                    if (LoggerManager.Instance.ShardLogger != null &&
                                        LoggerManager.Instance.ShardLogger.IsDebugEnabled)
                                    {
                                        LoggerManager.Instance.ShardLogger.Debug("Localshard.Channeldisconnected(): ",
                                                                                 server.Address.ToString() + " removed from existing channels.");
                                    }
                                }
                                if (!key.Address.Equals(context.LocalAddress))
                                {
                                    BrokenConnectionInfo info = new BrokenConnectionInfo();
                                    info.BrokenAddress = key.Address;
                                    info.SessionType   = SessionTypes.Shard;

                                    _connectionRestoration.RegisterListener(info, this, context.LocalShardName);
                                }
                            }
                        }


                        try
                        {
                            if (!connected)
                            {
                                IShardListener listener = _shardListeners[Common.MiscUtil.CONFIGURATION_MANAGER];
                                listener.OnMemberLeft(new Server(channel.PeerAddress, Status.Stopped));
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
Example #4
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);
        }
Example #5
0
        public void OnPrimaryChanged(ServerInfo newPrimary, int port)
        {
            //RTD: review
            if ((newPrimary == null && _primary != null) ||
                (newPrimary != null && _primary != null && !this._primary.Address.Equals(newPrimary.Address)))
            {
                BrokenConnectionInfo info = new BrokenConnectionInfo();
                info.BrokenAddress = _primary.Address;
                info.SessionType   = SessionTypes.Shard;
                _connectionRestoration.UnregisterListener(info);
            }

            Address primaryAddress = null;

            if (newPrimary != null && newPrimary.Address != null)
            {
                primaryAddress = new Address(newPrimary.Address.IpAddress, port);
            }

            ConnectPrimary(primaryAddress);

            //// If primary is null, it means the respective shard has no primary anymore so
            //if (newPrimary == null)
            //{
            //    if (_remoteShardChannel != null)
            //    {
            //        ((DualChannel)_remoteShardChannel).ShouldTryReconnecting = false;
            //        _remoteShardChannel.Disconnect();
            //        _remoteShardChannel = null;
            //    }
            //}
            //else
            //{
            //    if (_remoteShardChannel != null)
            //    {
            //        if (_remoteShardChannel.PeerAddress.Equals(newPrimary) && ((IDualChannel)this._remoteShardChannel).Connected)
            //            return;
            //    }


            //    bool isConnected = false;
            //    IRequestResponseChannel channel = factory.GetChannel(newPrimary.Address.IpAddress.ToString(), _shardPort, context.LocalAddress.IpAddress.ToString(), SessionTypes.Shard, _traceProvider, _channelFormatter);
            //    try
            //    {
            //        isConnected = channel.Connect(false);
            //    }
            //    catch (ChannelException e)
            //    {
            //        if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsErrorEnabled)
            //            LoggerManager.Instance.ShardLogger.Error("Error: RemoteShard.OnPrimaryChanged()", e.ToString());
            //    }
            //    //catch (Exception ex)
            //    //{
            //    //    if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsErrorEnabled)
            //    //        LoggerManager.Instance.ShardLogger.Error("Error: RemoteShard.OnPrimaryChanged()", e.ToString());
            //    //}

            //    if (isConnected)
            //    {
            //        SessionInfo info = new SessionInfo();
            //        info.Cluster = this.Context.ClusterName;
            //        info.Shard = this.Context.LocalShardName;

            //        channel.SendMessage(info, true);

            //        lock (_onChannel)
            //        {
            //            _remoteShardChannel = _resolveDispute.GetValidChannel(_resolveDispute.SetConnectInfo(channel as IDualChannel, ConnectInfo.ConnectStatus.CONNECT_FIRST_TIME), _remoteShardChannel);
            //            ((IDualChannel)_remoteShardChannel).StartReceiverThread();
            //            ((IDualChannel)_remoteShardChannel).RegisterRequestHandler(this);
            //        }
            //        lock (_onPrimary)
            //        {
            //            _primary = new Server(new Address(newPrimary.Address.IpAddress.ToString(), port), Status.Running);
            //        }
            //    }
            //}
        }
Example #6
0
        public bool OnSessionEstablished(Session session)
        {
            // check if the shardConnected event is required to be raised
            bool   shardConnected           = false;
            Server server                   = new Server(new Common.Net.Address(session.IP.ToString(), this._shardPort), Status.Initializing);
            IRequestResponseChannel channel = factory.GetChannel(session.Connection, session.IP.ToString(), this._shardPort, context.LocalAddress.ToString(), SessionTypes.Shard, _traceProvider, _channelFormatter);

            LoggerManager.Instance.SetThreadContext(new LoggerContext()
            {
                ShardName = _name != null ? _name : "", DatabaseName = ""
            });
            try
            {
                if (_remoteShardChannel != null && !((IDualChannel)_remoteShardChannel).Connected && _remoteShardChannel.PeerAddress.Equals(server))
                {
                    session.Connection.Disconnect();
                    //throw new ChannelException("already connected with shard"+_name);
                }

                if (channel.Connect(false))
                {
                    ConnectInfo.ConnectStatus status = ConnectInfo.ConnectStatus.CONNECT_FIRST_TIME;
                    if (_remoteShardChannel != null && _remoteShardChannel.PeerAddress.Equals(server.Address))
                    {
                        status = ConnectInfo.ConnectStatus.RECONNECTING;
                    }

                    lock (_onChannel)
                    {
                        _remoteShardChannel = _resolveDispute.GetValidChannel(_resolveDispute.SetConnectInfo(channel as IDualChannel, status), _remoteShardChannel);
                        ((IDualChannel)_remoteShardChannel).StartReceiverThread();
                        ((IDualChannel)_remoteShardChannel).RegisterRequestHandler(this);

                        shardConnected = true;
                    }
                    if (_primary != null && !_primary.Address.Equals(server.Address))
                    {
                        BrokenConnectionInfo info = new BrokenConnectionInfo();
                        info.BrokenAddress = _primary.Address;
                        info.SessionType   = SessionTypes.Shard;
                        _connectionRestoration.UnregisterListener(info);
                    }
                    lock (_onPrimary)
                    {
                        _primary = server;
                    }

                    if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsInfoEnabled)
                    {
                        LoggerManager.Instance.ShardLogger.Info("RemoteShard.OnSessionEstd()", "Session of the shard " + _name + " estd successfully");
                    }
                    return(IsStarted = true);
                }
                else
                {
                    return(false);
                }
            }
            catch (ChannelException e)
            {
                if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsErrorEnabled)
                {
                    LoggerManager.Instance.ShardLogger.Error("Error: RemoteShard.OnSessionEstd()", e.ToString());
                }
                return(false);
            }
            finally
            {
                if (shardConnected)
                {
                    ShardConnected();
                }
            }
        }