public MasterServer(MasterServerConfig c, GalacticProperties gp, IEnumerable <PSystemModel> allSystemModels, IDatabaseManager dbm, IDbIdIoService dbIdIoService, RedisServer redisServer) { _config = c; Dictionary <IDTypes, GlobalIDManager> globalIDManagers = new Dictionary <IDTypes, GlobalIDManager>(); globalIDManagers.Add(IDTypes.GalaxyID, new GlobalGalaxyIDManager(dbIdIoService, gp)); globalIDManagers.Add(IDTypes.TeamID, new GlobalTeamIDManager(dbIdIoService, gp)); globalIDManagers.Add(IDTypes.TransactionID, new GlobalTransactionIDManager(dbIdIoService, gp)); globalIDManagers.Add(IDTypes.AccountID, new GlobalAccountIDManager(dbIdIoService, gp)); GlobalIDManagers = globalIDManagers; _redisServer = redisServer; _databaseManager = dbm; _systemIDToSlaveServer = new ConcurrentDictionary <int, SlaveServer>(); AccountManager_MasterServer acm = new AccountManager_MasterServer(new LocalIDManager_MS((GlobalAccountIDManager)globalIDManagers[IDTypes.AccountID], IDTypes.AccountID), dbm, true); _allSystemModels = new Dictionary <int, PSystemModel>(); foreach (var sm in allSystemModels) { if (!_allSystemModels.ContainsKey(sm.Id)) { _allSystemModels.Add(sm.Id, sm); } } _redisServer.Subscribe(MessageTypes.Redis_SlaveConnectionRequest, _slaveConnected); _redisServer.Subscribe(MessageTypes.Redis_IDRequest, _handleIDRequest); _initTimeMs = TimeKeeper.MsSinceInitialization;//If this class gets an initializer or a reset method, this should be moved there. }
/// <summary> /// 队列数据 写文件 /// </summary> /// <param name="node">消息队列节点</param> internal QueueWriter(QueueNode node) { this.node = node; Config = node.Cache.MasterConfig; bufferPool = SubBuffer.Pool.GetPool(Config.BufferSize); DirectoryInfo directory = new DirectoryInfo(node.FilePath); if (!directory.Exists) { directory.Create(); } FilePath = directory.fullName(); disposeHandle = Dispose; isWrite = 1; }
/// <summary> /// 队列数据 写文件 /// </summary> /// <param name="node">消息队列节点</param> internal FileWriter(Node node) { Node = node; Config = node.Cache.MasterConfig; bufferPool = SubBuffer.Pool.GetPool(Config.BufferSize); DirectoryInfo directory = new DirectoryInfo(node.FilePath); if (!directory.Exists) { directory.Create(); } FilePath = directory.fullName(); disposeHandle = Dispose; onStartQueue = new QueueTaskThread.Node.Queue(new QueueTaskThread.Null(this)); isWrite = 1; }
/// <summary> /// Sets IsMasterServer=true if succesful /// </summary> /// <param name="c"></param> /// <param name="gp"></param> /// <param name="allSystemModels"></param> /// <param name="dbm"></param> /// <param name="redisServer"></param> void _promoteToMasterServer(MasterServerConfig c, GalacticProperties gp, IEnumerable <PSystemModel> allSystemModels, IDatabaseManager dbm, IDbIdIoService dbIdIoService, RedisServer redisServer) { #if DEBUG ConsoleManager.WriteLine("Promoting to master server...", ConsoleMessageType.Notification); #endif string masterServerID = Rand.Random.Next(-int.MaxValue, int.MaxValue).ToString(); bool setSuccessful = _redisServer.SetValue( RedisDBKeyTypes.MasterServerTimestamp, new MasterServerTimestamp() { MasterServerID = masterServerID }, new TimeSpan(0, 0, 0, 0, c.InitializationTimestampTimeoutMS), SetWhen.NotExists ); // Check if this instance was the first to set a timestamp, so we're clear to initialize a master server if (!setSuccessful) { ConsoleManager.WriteLine("Slave promotion failed, master server already exists, although master server check passed. Race Condition?", ConsoleMessageType.Error); // Another server exists already. return; } // Clear to initialize a master server. _masterServer = new MasterServer(c, gp, allSystemModels, dbm, dbIdIoService, redisServer); _masterServer.Id = int.Parse(masterServerID); IsMasterServer = true; #if DEBUG ConsoleManager.WriteLine("Master Server spawn Successful", ConsoleMessageType.Notification); #endif }
public MasterServerManager(MasterServerConfig c, GalacticProperties gp, IDatabaseManager dbm, IDbIdIoService dbIdIoService, RedisServer redisServer, SlaveServerConfigService slaveServerConfigService) { _redisServer = redisServer; _databaseManager = dbm; _dbIdIoService = dbIdIoService; _slaveServerConfigService = slaveServerConfigService; MasterServerConfig = c; GalacticProperties = gp; SlaveID = _slaveServerConfigService.CurrentServiceId; if (!_checkForMasterServer()) { List <PSystemModel> allSystems = new List <PSystemModel>(_databaseManager.GetAllSystemsAsync().Result); _promoteToMasterServer(MasterServerConfig, GalacticProperties, allSystems, _databaseManager, _dbIdIoService, _redisServer); } else { #if DEBUG ConsoleManager.WriteLine("Existing master server detected. Initializating as slave only.", ConsoleMessageType.Debug); #endif } if (!IsMasterServer) { ConsoleManager.WriteLine("Initializing as slave only.", ConsoleMessageType.Notification);//Leaving this here to remember to log it later, might be useful } _connectToServer(); redisServer.Subscribe(MessageTypes.Redis_SlaveConnectionResponse, _handleSlaveConnectionResponse); _pingTimer = new Timer(c.SlaveHeartbeatPeriodMS); _pingTimer.Elapsed += _updateSlaveHeartbeat; _pingTimer.Start(); }