예제 #1
0
        private IRequestResponseChannel CreateConnection(string peerIP, int peerPort)
        {
            IRequestResponseChannel channel = _factory.GetChannel(peerIP, peerPort, null, _sessionType, _traceProvider,
                                                                  _channelFormatter);
            int retries = 3;

            while (retries > 0)
            {
                if (!channel.Connect(true))
                {
                    retries--;
                    if (retries == 0)
                    {
                        throw new DistributorException(ErrorCodes.Distributor.CHANNEL_CONNECT_FAILED,
                                                       new string[] { _name, peerIP, peerPort.ToString() });
                    }
                }
                break;
            }
            channel.IsAuthenticated = true; //set to false after authentication command failure
            channel.RegisterRequestHandler(this);
            return(channel);
        }
예제 #2
0
        public bool OnSessionEstablished(Session session)
        {
            if (_primaryChannel != null)
            {
                session.Connection.Disconnect();
                return(false);
            }
            IPAddress localIp = GetLocalAddress();

            _primaryChannel = _factory.GetChannel(session.Connection, session.IP.ToString(), session.RemotePort,
                                                  localIp.ToString(), _sessionType, _traceProvider, _channelFormatter);

            if (!_primaryChannel.Connect(true))
            {
                throw new DistributorException(ErrorCodes.Distributor.CHANNEL_CONNECT_FAILED, new string[]
                {
                    _name,
                    _primaryChannel.PeerAddress.IpAddress.ToString(), _primaryChannel.PeerAddress.Port.ToString()
                });
            }

            return(true);
        }
예제 #3
0
        private void ConnectPrimary(Address primary)
        {
            // check if the shardConnected event is required to be raised
            bool shardConnected = false;

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


                bool isConnected = false;
                IRequestResponseChannel channel = factory.GetChannel(primary.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);
                        shardConnected = true;
                    }
                    lock (_onPrimary)
                    {
                        _primary = new Server(new Address(primary.IpAddress.ToString(), primary.Port), Status.Running);
                    }
                }
            }

            if (shardConnected)
            {
                ShardConnected();
            }
        }
예제 #4
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();
                }
            }
        }