示例#1
0
        private bool ConnectToPeerServer(bool changeRole = true)
        {
            if (IsClustered)
            {
                try
                {
                    DatabaseRPCService   rpcService      = new DatabaseRPCService(_peerNode.Name);
                    IConfigurationServer remoteCfgServer = rpcService.GetConfigurationServer(new TimeSpan(0, 1, 30), Common.Communication.SessionTypes.Client, new ConfigurationChannelFormatter());
                    ((OutProcConfigurationClient)remoteCfgServer).AutoReconnect = false;
                    remoteCfgServer.MarkConfiguraitonSession();
                    OutProcConfigurationSession session = remoteCfgServer.OpenConfigurationSession(new SSPIClientAuthenticationCredential()) as OutProcConfigurationSession;
                    session.SetChannelDisconnectedListener(this);
                    session.AutoReconnect = false;
                    session.MarkSessionInternal();
                    _session = session;

                    if (LoggerManager.Instance.CONDBLogger != null && LoggerManager.Instance.CONDBLogger.IsInfoEnabled)
                    {
                        LoggerManager.Instance.CONDBLogger.Info("ConfigurationCluster.Connect", "connected with peer server " + _peerNode.Name);
                    }
                }
                catch (Exception e)
                {
                    //if (LoggerManager.Instance.CONDBLogger != null && LoggerManager.Instance.CONDBLogger.IsErrorEnabled)
                    //    LoggerManager.Instance.CONDBLogger.Error("ConfigurationCluster.Connect", e.ToString());
                }
            }

            if (changeRole)
            {
                DetermineRole();

                if (_currentRole == NodeRole.Secondary)
                {
                    if (_configuration != null)
                    {
                        ServerNode localNode = _configuration.Servers.GetNode(_localServerIP);

                        if (_peerNode != null && _peerNode.Priority > localNode.Priority)
                        {
                            //Let's take over
                            TakeOverPrimaryRole();
                        }
                        else
                        {
                            //Need to replicate state from peer node
                            ReplicateStateFromPeerServer();
                        }
                    }
                    _status.SetStatusBit(RUNNING, UNINITIIALIZED);
                }
            }

            return(_session != null);
        }
示例#2
0
        public static IConfigurationSession Connect(string[] configServers, int configServerPort, string cluster, out IConfigurationServer remote, IChannelFormatter channelFormatter, IClientAuthenticationCredential clientAuthenticationCredential, bool remoteRouter = false)
        {
            Exception          exception = null;
            DatabaseRPCService rpc       = null;

            remote = null;
            IConfigurationSession configurationSession = null;
            int     csPort = configServerPort;
            Boolean found  = false;

            foreach (String current in configServers)
            {
                if (found)
                {
                    break;
                }

                int retries = 3;
                while (retries > 0)
                {
                    try
                    {
                        if (configurationSession != null)
                        {
                            configurationSession.Close();
                            configurationSession = null;
                        }
                        rpc = new DatabaseRPCService(current, csPort);

                        //if (remote == null)
                        {
                            remote = rpc.GetConfigurationServer(new TimeSpan(0, 0, 90), SessionTypes.Client, channelFormatter);
                        }

                        if (remoteRouter)
                        {
                            remote.MarkDistributorSession();
                        }

                        configurationSession = remote.OpenConfigurationSession(clientAuthenticationCredential);

                        if (configurationSession != null)
                        {
                            List <Alachisoft.NosDB.Common.Net.Address> csServers = configurationSession.GetConfServers(cluster);

                            if (csServers == null || csServers.Count < 1)
                            {
                                throw new DatabaseException(Common.ErrorHandling.ErrorCodes.Distributor.CLUSTER_INFO_UNAVAILABLE, new[] { cluster });
                            }

                            foreach (Alachisoft.NosDB.Common.Net.Address add in csServers)
                            {
                                if (add.ip.Equals(current))
                                {
                                    found = true;
                                }
                            }

                            if (!found)
                            {
                                configurationSession.Close();
                                configurationSession = null;

                                foreach (Alachisoft.NosDB.Common.Net.Address cur in csServers)
                                {
                                    try
                                    {
                                        rpc    = new DatabaseRPCService(cur.ip, csPort);
                                        remote = rpc.GetConfigurationServer(new TimeSpan(0, 0, 90), SessionTypes.Client, channelFormatter);

                                        if (remoteRouter)
                                        {
                                            remote.MarkDistributorSession();
                                        }
                                        configurationSession = remote.OpenConfigurationSession(clientAuthenticationCredential);
                                        found = true;
                                    }
                                    catch (Exception ex)
                                    {
                                        exception = ex;
                                    }
                                }
                            }
                        }

                        if (found)
                        {
                            break;
                        }
                    }
                    catch (Alachisoft.NosDB.Common.Exceptions.TimeoutException)
                    {
                        if (configurationSession != null)
                        {
                            configurationSession.Close();
                            configurationSession = null;
                        }

                        exception = new DistributorException(ErrorCodes.Distributor.CONFIGURATION_SERVER_NOTRESPONDING);
                        retries--;
                        if (retries == 0)
                        {
                            break;
                        }
                    }
                    catch (Exception e)
                    {
                        if (configurationSession != null)
                        {
                            configurationSession.Close();
                            configurationSession = null;
                        }

                        exception = e;
                        retries--;
                        if (retries == 0)
                        {
                            break;
                        }
                    }
                }
            }

            if (configurationSession != null)
            {
                return(configurationSession);
            }
            else if (exception != null)
            {
                throw exception;
            }
            return(null);
        }
示例#3
0
        public void Initialize(DbmClusterConfiguration configurationClusterConfig, string ipAddress, int port, string clusterName, string shardName)
        {
            try
            {
                bool      isConfigSessionInit = false;
                Exception csInitExc           = null;
                _nodeContext.ShardServer.Initialize(IPAddress.Parse(ipAddress), port);
                _nodeContext.ShardServer.RegisterSessionListener(SessionTypes.Client, _clientSessionManager);
                _nodeContext.ClusterName = clusterName;

                foreach (var configServer in configurationClusterConfig.ConfigServers.Nodes)
                {
                    try
                    {
                        if (LoggerManager.Instance.ServerLogger != null && LoggerManager.Instance.ServerLogger.IsInfoEnabled)
                        {
                            LoggerManager.Instance.ServerLogger.Info("ShardHost.Initialize()", "going to connect with configurration server " + configServer.Name + ":" + configServer.Port);
                        }

                        DatabaseRPCService   rpc    = new DatabaseRPCService(configServer.Name, configServer.Port);
                        IConfigurationServer remote = rpc.GetConfigurationServer(new TimeSpan(0, 0, 90), SessionTypes.Management, new ConfigurationChannelFormatter());
                        remote.MarkDatabaseSesion();
                        _nodeContext.ConfigurationSession = remote.OpenConfigurationSession(new SSPIClientAuthenticationCredential());
                        if (string.Compare(clusterName, MiscUtil.LOCAL, true) == 0)
                        {
                            ((Alachisoft.NosDB.Common.Configuration.Services.Client.OutProcConfigurationSession)_nodeContext.ConfigurationSession).MarkSessionInternal();
                            ((Alachisoft.NosDB.Common.Configuration.Services.Client.OutProcConfigurationSession)_nodeContext.ConfigurationSession).SwithToActiveNode = false;
                        }
                    }
                    catch (Exception e)
                    {
                        if (LoggerManager.Instance.ServerLogger != null && LoggerManager.Instance.ServerLogger.IsErrorEnabled)
                        {
                            LoggerManager.Instance.ServerLogger.Error("ShardHost.Initialize()", "Failed to connect with " + configServer.Name + ":" + configServer.Port, e);
                        }
                        csInitExc = e;
                    }

                    if (_nodeContext.ConfigurationSession != null)
                    {
                        isConfigSessionInit = true;
                        break;
                    }
                }
                if (isConfigSessionInit)
                {
                    _nodeContext.LocalShardName     = shardName;
                    _clientSessionManager.ShardName = shardName;
                    LoggerManager.Instance.SetThreadContext(new LoggerContext()
                    {
                        ShardName = _nodeContext.LocalShardName != null ? _nodeContext.LocalShardName : "", DatabaseName = ""
                    });


                    SecurityManager = new SecurityManager();
                    SecurityManager.Initialize(this.NodeContext.LocalShardName);
                    _initialized = true;
                }
                else
                {
                    if (csInitExc != null)
                    {
                        throw csInitExc;
                    }
                }
            }
            catch (Exception ex)
            {
                _initialized = false;
                if (LoggerManager.Instance.ServerLogger != null && LoggerManager.Instance.ServerLogger.IsErrorEnabled)
                {
                    LoggerManager.Instance.ServerLogger.Error("ShardHost.Initialize()", "Error:", ex);
                }
                throw;
            }
        }