public void SessionReadCycle_Valid() { using (RedisServer redisServer = new RedisServer()) { string sessionId = ResetRedisConnectionWrapperAndConfiguration(); // Inserting empty session with "SessionStateActions.InitializeItem" flag into redis server RedisSessionStateProvider ssp = new RedisSessionStateProvider(); ssp.CreateUninitializedItem(null, sessionId, (int)RedisSessionStateProvider.configuration.SessionTimeout.TotalMinutes); // Get write lock and session from cache bool locked; TimeSpan lockAge; object lockId; SessionStateActions actions; SessionStateStoreData storeData = ssp.GetItem(null, sessionId, out locked, out lockAge, out lockId, out actions); // Get actual connection and varify lock and session timeout IDatabase actualConnection = GetRealRedisConnection(); Assert.True(actualConnection.StringGet(ssp.cache.Keys.LockKey).IsNull); Assert.Equal(((int)RedisSessionStateProvider.configuration.SessionTimeout.TotalSeconds).ToString(), actualConnection.HashGet(ssp.cache.Keys.InternalKey, "SessionTimeout").ToString()); // reset sessions timoue ssp.ResetItemTimeout(null, sessionId); // End request ssp.EndRequest(null); // remove data and lock from redis DisposeRedisConnectionWrapper(); } }
public async void TestSendAndReceive() { var resolver = new DependencyResolver(); using (var server = new RedisServer(new RedisConnection(RedisHost, RedisPort, RedisPassword), ServerQueue, new ServiceFactory(resolver))) { server.Listen(); using (var client = new RedisClient(new RedisConnection(RedisHost, RedisPort, RedisPassword), ClientQueue, ServerQueue)) { var clientFactory = new ServiceProxy.ServiceClientFactory(client); var serviceClient = clientFactory.CreateServiceClient<ITestService2>(); Assert.That(serviceClient.GetPerson(1), Is.Not.Null); var persons = await serviceClient.ListPersonsAsync(5); Assert.That(persons, Is.Not.Null); Assert.AreEqual(5, persons.Count()); var nullCollection = await serviceClient.ListPersonsAsync(-1); Assert.IsNull(nullCollection); var nullObject = serviceClient.GetPerson(-1); Assert.IsNull(nullObject); } } }
public void Set_ExpireData() { using (RedisServer redisServer = new RedisServer()) { RedisConnectionWrapper redisConn = GetRedisConnectionWrapperWithUniqueSession(); // Inserting data into redis server that expires after 1 second ChangeTrackingSessionStateItemCollection data = new ChangeTrackingSessionStateItemCollection(); data["key"] = "value"; redisConn.Set(data, 1); // Wait for 2 seconds so that data will expire System.Threading.Thread.Sleep(1100); // Get actual connection and get data blob from redis IDatabase actualConnection = GetRealRedisConnection(redisConn); HashEntry[] sessionDataFromRedis = actualConnection.HashGetAll(redisConn.Keys.DataKey); // Check that data shoud not be there Assert.Equal(0, sessionDataFromRedis.Length); DisposeRedisConnectionWrapper(redisConn); } }
/// <summary> /// 初始化数据库环境、实例化子类中,所有Set属性 /// </summary> public void Initializer() { if (IsInitializer) { return; } _client = new RedisClient(_redisConnection); IsInitializer = true; Connection = new RedisConnection(_client); Hash = new RedisHash(_client); Key = new RedisKey(_client); List = new RedisList(_client); PubSub = new RedisPubSub(_client); Script = new RedisScript(_client); Server = new RedisServer(_client); Set = new RedisSet(_client); SortedSet = new RedisSortedSet(_client); String = new RedisString(_client); Transaction = new RedisTransaction(_client); Bit = new RedisBit(_client); Expire = new RedisExpire(_client); Sort = new RedisSort(_client); Number = new RedisNumber(_client); }
public LoginHandler( IDatabaseManager dbm, CryptographyManager cpm, RedisServer rs, ILoggerUtil logger, SessionService sessionService) : base(RouteConfig.Login) { // Todo: Refactor this into POST request to mitigate CSRF. // http://www.jhovgaard.com/nancy-csrf/ Get[RouteConfig.Login_Request, true] = _handleLogin; _databaseManager = dbm; _cryptographyManager = cpm; _redisServer = rs; _logger = logger; _sessionService = sessionService; //This might add a bit of latency to login requests, but I doubt it will be a problem. Adding this comment in case we need to debug it in the future. _instanceId = rs.GenerateUniqueId().Result; _redisServer.Subscribe(ChannelTypes.WebLogin, _instanceId, _handleRedisLoginDataResponse); }
public async void TestSendAndReceiveExceptions() { var resolver = new DependencyResolver(); using (var server = new RedisServer(new RedisConnection(RedisHost, RedisPort, RedisPassword), ServerQueue, new ServiceFactory(resolver))) { server.Listen(); using (var client = new RedisClient(new RedisConnection(RedisHost, RedisPort, RedisPassword), ClientQueue, ServerQueue)) { var clientFactory = new ServiceClientFactory(client); var serviceClient = clientFactory.CreateServiceClient <ITestService>(); //Synchronous var err = Assert.Catch(async() => await serviceClient.FailAsync()); Assert.IsNotNull(err); Assert.IsNotInstanceOf <AggregateException>(err); //Asynchronous task based err = Assert.Catch(() => serviceClient.Fail()); Assert.IsNotNull(err); Assert.IsNotInstanceOf <AggregateException>(err); //Asynchronous IAsyncResult based , awaiting with Task err = Assert.Catch(async() => await Task.Factory.FromAsync(serviceClient.BeginFail, serviceClient.EndFail, null)); Assert.IsNotNull(err); Assert.IsNotInstanceOf <AggregateException>(err); //Timeout exceptions var factoryWithTimeout = new ServiceClientFactory(client); var serviceClientWithTimeout = factoryWithTimeout.CreateServiceClient <ITestService>(50); //50ms Assert.Throws <TimeoutException>(async() => await serviceClientWithTimeout.ReplyAfter(1000)); } } }
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(); }
public async void TestSendAndReceiveExceptions() { var resolver = new DependencyResolver(); using (var server = new RedisServer(new RedisConnection(RedisHost, RedisPort, RedisPassword), ServerQueue, new ServiceFactory(resolver))) { server.Listen(); using (var client = new RedisClient(new RedisConnection(RedisHost, RedisPort, RedisPassword), ClientQueue, ServerQueue)) { var clientFactory = new ServiceClientFactory(client); var serviceClient = clientFactory.CreateServiceClient<ITestService>(); //Synchronous var err = Assert.Catch(async () => await serviceClient.FailAsync()); Assert.IsNotNull(err); Assert.IsNotInstanceOf<AggregateException>(err); //Asynchronous task based err = Assert.Catch(() => serviceClient.Fail()); Assert.IsNotNull(err); Assert.IsNotInstanceOf<AggregateException>(err); //Asynchronous IAsyncResult based , awaiting with Task err = Assert.Catch(async () => await Task.Factory.FromAsync(serviceClient.BeginFail, serviceClient.EndFail, null)); Assert.IsNotNull(err); Assert.IsNotInstanceOf<AggregateException>(err); //Timeout exceptions var factoryWithTimeout = new ServiceClientFactory(client); var serviceClientWithTimeout = factoryWithTimeout.CreateServiceClient<ITestService>(50); //50ms Assert.Throws<TimeoutException>(async () => await serviceClientWithTimeout.ReplyAfter(1000)); } } }
public void MultiTheadTest() { RedisServer r = new RedisServer("localhost", 6379, null, 100); r.Db = 3; DateTime start1 = DateTime.Now; Parallel.For(0, 100000, new ParallelOptions() { MaxDegreeOfParallelism = 20 }, j => { int i = j; r.ListLeftPush("list", "1" + i); r.ListLeftPush("list", "2" + i); r.ListLeftPush("list", "3" + i); r.ListLeftPush("list", "4" + i); r.ListRightPush("list", "5" + i); r.ListLeftPop("list"); r.ListRightPop("list"); }); DateTime end1 = DateTime.Now; double seconds1 = (end1 - start1).TotalSeconds; DateTime start2 = DateTime.Now; for (int i = 0; i < 100000; ++i) { r.ListLeftPush("list", "1" + i); r.ListLeftPush("list", "2" + i); r.ListLeftPush("list", "3" + i); r.ListLeftPush("list", "4" + i); r.ListRightPush("list", "5" + i); r.ListLeftPop("list"); r.ListRightPop("list"); } DateTime end2 = DateTime.Now; double seconds2 = (end2 - start2).TotalSeconds; }
private static ConnectionMultiplexer getConnection(RedisServer cfg) { try { StringBuilder sb = new StringBuilder(); sb.AppendFormat("{0}:{1}", cfg.Server, cfg.Port); sb.AppendFormat(",allowAdmin={0}", cfg.AllowAdmin); sb.AppendFormat(",abortConnect=false,connectRetry=3,syncTimeout=3000"); sb.AppendFormat(",connectTimeout={0}", cfg.ConnectTimeout > 0 ? cfg.ConnectTimeout : 5000); if (cfg.Ssl) { // sb.AppendFormat(",ssl={0},password={1}", cfg.Ssl,cfg.Pwd); sb.AppendFormat(",password={1}", cfg.Ssl, cfg.Pwd); } //return new Lazy<ConnectionMultiplexer>(() => ConnectionMultiplexer.Connect(sb.ToString())).Value; return(ConnectionMultiplexer.Connect(sb.ToString())); } catch (Exception ex) { Logger?.Log(LogLevel.Error, new Exception("redis连接创建失败", ex), "", null); return(null); } }
public void Set_NullData() { // this also tests host:port config part ProviderConfiguration pc = Utility.GetDefaultConfigUtility(); pc.ApplicationName = "APPTEST"; pc.Port = 6379; using (RedisServer redisServer = new RedisServer()) { RedisConnectionWrapper redisConn = GetRedisConnectionWrapperWithUniqueSession(pc); // Inserting data into redis server ChangeTrackingSessionStateItemCollection data = new ChangeTrackingSessionStateItemCollection(); data["key"] = "value"; data["key1"] = null; redisConn.Set(data, 900); // Get actual connection and get data blob from redis IDatabase actualConnection = GetRealRedisConnection(redisConn); HashEntry[] sessionDataFromRedis = actualConnection.HashGetAll(redisConn.Keys.DataKey); // Check that data shoud be same as what inserted Assert.Equal(2, sessionDataFromRedis.Length); ChangeTrackingSessionStateItemCollection dataFromRedis = new ChangeTrackingSessionStateItemCollection(); foreach (HashEntry entry in sessionDataFromRedis) { dataFromRedis[entry.Name] = RedisUtility.GetObjectFromBytes(entry.Value); } Assert.Equal("value", dataFromRedis["key"]); Assert.Equal(null, dataFromRedis["key1"]); // remove data from redis actualConnection.KeyDelete(redisConn.Keys.DataKey); DisposeRedisConnectionWrapper(redisConn); } }
/// <summary> /// redis server select-item event /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void redisServerBox_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs <object> e) { selectDatabaseItem = (TreeViewItem)redisServerBox.ItemContainerGenerator.ContainerFromItem(e.NewValue); if (e.NewValue.GetType() == typeof(RedisServer)) { RedisServer redisServer = (RedisServer)e.NewValue; if (redisServer == null) { return; } if (!redisServer.RedisClient.IsConnected) { redisServer.IsConnectioning = true; RedisKeyUtils.sp = redisServer.Connection.KeySeparator.ToCharArray(); ThreadStart threadStart = new ThreadStart(() => { redisConnecton(redisServer); }); new Thread(threadStart).Start(); } } else if (e.NewValue.GetType() == typeof(RedisDatabase)) { this.currentRedisDatabase = (RedisDatabase)e.NewValue; ThreadStart threadStart = new ThreadStart(() => { getKeys(currentRedisDatabase); }); new Thread(threadStart).Start(); } this.btnAddKey.Dispatcher.Invoke(new Action(delegate { this.btnAddKey.IsEnabled = false; })); }
public void TryUpdateAndReleaseLockIfLockIdMatch_LargeLockTime_ExpireManuallyTest() { using (RedisServer redisServer = new RedisServer()) { RedisConnectionWrapper redisConn = GetRedisConnectionWrapperWithUniqueSession(); // Inserting data into redis server ChangeTrackingSessionStateItemCollection data = new ChangeTrackingSessionStateItemCollection(); data["key1"] = "value1"; redisConn.Set(data, 900); int lockTimeout = 120000; DateTime lockTime = DateTime.Now; object lockId; ISessionStateItemCollection dataFromRedis; int sessionTimeout; Assert.True(redisConn.TryTakeWriteLockAndGetData(lockTime, lockTimeout, out lockId, out dataFromRedis, out sessionTimeout)); redisConn.TryUpdateAndReleaseLockIfLockIdMatch(lockId, dataFromRedis, 900); // Get actual connection and check that lock is released IDatabase actualConnection = GetRealRedisConnection(redisConn); Assert.False(actualConnection.KeyExists(redisConn.Keys.LockKey)); actualConnection.KeyDelete(redisConn.Keys.DataKey); DisposeRedisConnectionWrapper(redisConn); } }
public void TryTakeWriteLockAndGetData_WriteLockWithOtherWriteLockWithSameLockId() { using (RedisServer redisServer = new RedisServer()) { RedisConnectionWrapper redisConn = GetRedisConnectionWrapperWithUniqueSession(); // Inserting data into redis server ChangeTrackingSessionStateItemCollection data = new ChangeTrackingSessionStateItemCollection(); data["key"] = "value"; redisConn.Set(data, 900); int lockTimeout = 900; // Same LockId DateTime lockTime = DateTime.Now; // Takewrite lock successfully first time object lockId_1; ISessionStateItemCollection dataFromRedis_1; int sessionTimeout; Assert.True(redisConn.TryTakeWriteLockAndGetData(lockTime, lockTimeout, out lockId_1, out dataFromRedis_1, out sessionTimeout)); Assert.Equal(lockTime.Ticks.ToString(), lockId_1.ToString()); Assert.Equal(1, dataFromRedis_1.Count); // try to take write lock and fail and get earlier lock id object lockId_2; ISessionStateItemCollection dataFromRedis_2; Assert.False(redisConn.TryTakeWriteLockAndGetData(lockTime, lockTimeout, out lockId_2, out dataFromRedis_2, out sessionTimeout)); Assert.Equal(lockTime.Ticks.ToString(), lockId_2.ToString()); Assert.Equal(null, dataFromRedis_2); // Get actual connection IDatabase actualConnection = GetRealRedisConnection(redisConn); // remove data and lock from redis actualConnection.KeyDelete(redisConn.Keys.DataKey); actualConnection.KeyDelete(redisConn.Keys.LockKey); DisposeRedisConnectionWrapper(redisConn); } }
/// <summary> /// update serdis-server info /// </summary> /// <param name="redisServer"></param> /// <param name="connection"></param> public static void updateConnection(RedisServer redisServer, RedisConnection connection) { redisServer.Connection = connection; redisServer.RedisClient = new RedisClient(connection.Host, Convert.ToInt32(connection.Port)); redisServer.RedisClient.Encoding = Encoding.UTF8; }
public void RedisInsertManyTest() { RedisServer r = new RedisServer("localhost"); r.Db = 3; r.KeyDelete("hash"); r.HashSetMany("hash", new Dictionary<string, string> { { "1", "2" }, { "2", "3" } }); var hash = r.HashGetAll("hash"); Assert.AreEqual(hash.Length, 2); Assert.AreEqual("2", r.HashGet("hash", "1")); Assert.AreEqual("3", r.HashGet("hash", "2")); r.KeyDelete("hash"); r.KeyDelete("set"); r.SetAddManay("set", new List<string> { "1", "2", "3" }); var set = r.SetMembers("set"); Assert.AreEqual(set.Count, 3); Assert.AreEqual("1", set[0]); Assert.AreEqual("2", set[1]); Assert.AreEqual("3", set[2]); r.KeyDelete("set"); r.KeyDelete("list"); r.ListRightPushMany("list", new List<string> { "1", "2", "3", "4", "5" }); Assert.AreEqual(5, r.ListLength("list")); Assert.AreEqual("1", r.ListLeftPop("list")); Assert.AreEqual("5", r.ListRightPop("list")); Assert.AreEqual("4", r.ListRightPop("list")); Assert.AreEqual("2", r.ListLeftPop("list")); r.KeyDelete("list"); r.Dispose(); }
/// <summary> /// Connects to a redis server. /// </summary> public void Connect(bool allowAdmin = false) { lock ( _syncRoot ) { if (IsConnected) { return; } RedisConfiguration configuration = ConfigurationSettings.GetRedisConfigurationSection( ); if (configuration == null) { throw new RedisConnectionException("Invalid Redis configuration."); } if (configuration.Servers == null || configuration.Servers.Count <= 0) { throw new RedisConnectionException("No Redis servers found."); } ///// // Randomly pick a redis server from the pool. // (Load balance this at a later stage). ///// var random = new Random(Environment.TickCount); int serverId = random.Next(0, configuration.Servers.Count - 1); RedisServer redisServer = configuration.Servers[serverId]; if (redisServer == null || string.IsNullOrEmpty(redisServer.HostName)) { throw new RedisConnectionException("Invalid Redis server configuration."); } string host = string.Format("{0}:{1}", redisServer.HostName, redisServer.Port); ConfigurationOptions configurationOptions = ConfigurationOptions.Parse(host); configurationOptions.ClientName = GetClientName( ); configurationOptions.AbortOnConnectFail = false; configurationOptions.AllowAdmin = allowAdmin; EventLog.Application.WriteInformation("Connecting to Redis server ('{0}')...", host); try { ConnectionMultiplexer multiplexer = ConnectionMultiplexer.Connect(configurationOptions); ///// // Allow concurrent message processing. ///// multiplexer.PreserveAsyncOrder = false; multiplexer.ConnectionFailed += ConnectionFailed; multiplexer.ConnectionRestored += ConnectionRestored; EventLog.Application.WriteInformation(multiplexer.IsConnected ? "Connection to Redis server ('{0}') established." : "Connection to Redis server ('{0}') failed. Connection will be automatically reestablished when possible.", host); IServer server = multiplexer.GetServer(host); if (server != null) { PrepareScripts(server); } ActiveConnection = multiplexer; } catch (StackExchange.Redis.RedisConnectionException exc) { EventLog.Application.WriteWarning("Connection to Redis server '{0}' failed.\n{1}", host, exc); } } }
protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines) { container.Register <IDatabaseManager, DBStateResolver>().AsSingleton(); #if !DEBUG DiagnosticsHook.Disable(pipelines); #else StaticConfiguration.EnableRequestTracing = true; #endif redisServer = new RedisServer(LogError, LogDebug, (new RedisConfig()).Address); container.Register <RedisServer>(redisServer); var serviceId = redisServer.GenerateUniqueId().Result; container.Register <IServerConfigService, WebServerConfigService>(new WebServerConfigService(redisServer, serviceId)); sessionService = container.Resolve <SessionService>(); // CSRF that uses Redis for shared token generation. Tokens currently don't expire. Csrf.Enable(pipelines, new CryptographyConfiguration( new RijndaelEncryptionProvider(new RedisBasedKeyGenerator(redisServer)), new DefaultHmacProvider(new RedisBasedKeyGenerator(redisServer))) ); pipelines.BeforeRequest.AddItemToEndOfPipeline(ctx => { var origin = ctx.Request.Headers["Origin"].FirstOrDefault(); if (origin == null) { return(null); } var matches = corsDomains.FirstOrDefault( allowed => Regex.IsMatch(origin, "^" + allowed + "$", RegexOptions.IgnoreCase)); // No matches, so let's abort. if (matches == null) { var responseJson = (Response)"CORS not allowed."; responseJson.ContentType = "application/json"; responseJson.StatusCode = HttpStatusCode.BadRequest; return(responseJson); } return(null); }); pipelines.AfterRequest.AddItemToEndOfPipeline(ctx => { var origin = ctx.Request.Headers["Origin"].FirstOrDefault(); if (origin == null) { return; } ctx.Response.Headers.Add("Access-Control-Allow-Origin", origin); ctx.Response.Headers.Add("Access-Control-Allow-Methods", "POST,GET,DELETE,PUT,OPTIONS"); ctx.Response.Headers.Add("Access-Control-Allow-Credentials", "true"); ctx.Response.Headers.Add("Access-Control-Allow-Headers", "Accept,Origin,Content-type"); ctx.Response.Headers.Add("Access-Control-Expose-Headers", "Accept,Origin,Content-type"); }); pipelines.BeforeRequest.AddItemToEndOfPipeline(ProcessSessionAuth); pipelines.OnError += (ctx, ex) => { throw ex; }; }
public void SessionTimoutChangeFromGlobalAspx() { using (RedisServer redisServer = new RedisServer()) { string sessionId = ResetRedisConnectionWrapperAndConfiguration(); // Inserting empty session with "SessionStateActions.InitializeItem" flag into redis server RedisSessionStateProvider ssp = new RedisSessionStateProvider(); ssp.CreateUninitializedItem(null, sessionId, (int)RedisSessionStateProvider.configuration.SessionTimeout.TotalMinutes); // Get write lock and session from cache bool locked; TimeSpan lockAge; object lockId; SessionStateActions actions; SessionStateStoreData storeData = ssp.GetItemExclusive(null, sessionId, out locked, out lockAge, out lockId, out actions); // Get actual connection and varify lock and session timeout IDatabase actualConnection = GetRealRedisConnection(); Assert.Equal(lockId.ToString(), actualConnection.StringGet(ssp.cache.Keys.LockKey).ToString()); Assert.Equal(((int)RedisSessionStateProvider.configuration.SessionTimeout.TotalSeconds).ToString(), actualConnection.HashGet(ssp.cache.Keys.InternalKey, "SessionTimeout").ToString()); // setting data as done by any normal session operation storeData.Items["key"] = "value"; storeData.Timeout = 5; // session update ssp.SetAndReleaseItemExclusive(null, sessionId, storeData, lockId, false); Assert.Equal(1, actualConnection.HashGetAll(ssp.cache.Keys.DataKey).Length); Assert.Equal("300", actualConnection.HashGet(ssp.cache.Keys.InternalKey, "SessionTimeout").ToString()); // reset sessions timoue ssp.ResetItemTimeout(null, sessionId); // End request ssp.EndRequest(null); // Verify that GetItemExclusive returns timeout from redis bool locked_1; TimeSpan lockAge_1; object lockId_1; SessionStateActions actions_1; SessionStateStoreData storeData_1 = ssp.GetItemExclusive(null, sessionId, out locked_1, out lockAge_1, out lockId_1, out actions_1); Assert.Equal(5, storeData_1.Timeout); // remove data and lock from redis DisposeRedisConnectionWrapper(); } }
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. }
public ShoutCommand(RedisServer redisServer) { CommandSignatures.AddRange(new [] { "shout", "s" }); _redisServer = redisServer; }
/// <summary> /// Initializes the entire server /// </summary> private void Initialize() { for (int i = 0; i < 20; i++) { msgAvgQue.Enqueue(0); } var rand = new Random(3); // Logging Enabled Logger.Initialize(); _synchronizers = new List <ISynchronizer>(); //Load configs ConnectionManagerConfig connectionManagerConfig = new ConnectionManagerConfig(new CoreNetworkConfig()); GalacticProperties galacticProperties = new GalacticProperties(); _consoleManager = new ConsoleManager(); _projectileManager = new ProjectileManager(); ConsoleManager.WriteLine("Starting database...", ConsoleMessageType.Startup); _databaseManager = new MongoDatabaseManager(); ShipStatManager.ReadShipsFromDBSList(_databaseManager.GetStatsFromDBAsync().Result); ConsoleManager.WriteLine("Ship types loaded.", ConsoleMessageType.Startup); RedisConfig rc = new RedisConfig(); _redisServer = new RedisServer(LogRedisError, LogRedisInfo, rc.Address); var slaveId = SlaveServerConfigService.GetFreeSlaveID(_redisServer).Result; _slaveServerConfigService = new SlaveServerConfigService(_redisServer, slaveId); _masterServerManager = new MasterServerManager(new MasterServerConfig(), new GalacticProperties(), _databaseManager, _databaseManager, _redisServer, _slaveServerConfigService); _cargoSynchronizer = new CargoSynchronizer(); _synchronizers.Add(_cargoSynchronizer); connectionManagerConfig.MyConfig.Port = ConnectionManager.GetFreePort(28002, 28010); _connectionManager = new ConnectionManager(); _connectionManager.Initialize(connectionManagerConfig); //Poll to listen to Lidgren until it is ready //LidgrenMessagePoller_Init initializationPoller = new LidgrenMessagePoller_Init(_connectionManager.Server, this); //initializationPoller.Poll(); _galaxyIDManager = new LocalIDManager(_masterServerManager, IDTypes.GalaxyID); _teamIDManager = new LocalIDManager(_masterServerManager, IDTypes.TeamID); _accountIDManager = new LocalIDManager(_masterServerManager, IDTypes.AccountID); _transactionIDManager = new LocalIDManager(_masterServerManager, IDTypes.TransactionID); _accountManager = new AccountManager(_accountIDManager, _databaseManager); _messageManager = new MessageManager(_connectionManager); _clientUpdateManager = new ClientUpdateManager(_playerManager); _playerManager = new PlayerManager(_databaseManager, _connectionManager, _redisServer, _galaxyIDManager, _clientUpdateManager); var chatCommands = new List <IChatCommand>() { new HelpCommand(), new ShoutCommand(_redisServer), new RadioCommand(), new TellCommand(_playerManager) }; var asyncChatCommands = new List <IAsyncChatCommand>() { new AdminWarpCommand(_databaseManager, _redisServer, new Random()) }; _teamManager = new GlobalTeamManager(_teamIDManager, _connectionManager, _redisServer, _playerManager, _databaseManager); _galaxyManager = new GalaxyManager(galacticProperties.SolID, _teamManager); _chatManager = new ChatManager(chatCommands, asyncChatCommands, _playerManager, _messageManager, _redisServer); _warpManager = new WarpManager(_galaxyManager, _messageManager, _chatManager, _redisServer, _accountManager, _databaseManager); _shipManager = new ShipManager(_messageManager, _galaxyManager, _warpManager, _connectionManager, _databaseManager); _structureManager = new StructureManager(_databaseManager, _galaxyManager, _galaxyIDManager, _cargoSynchronizer); loginManager = new LoginManager(_accountManager, _playerManager, _connectionManager, _redisServer); // Todo: Convert everything over to ServerNetworkMessage to propogate full request context. _simulatorManager = new SimulatorManager(new SimulatorConfig(), _redisServer, (sender, container) => ProcessMessage(sender, new ServerNetworkMessage(container, null))); StructureStatManager.Initialize(); ConsoleManager.WriteLine("Completed Initialization", ConsoleMessageType.Startup); _economyManager = new EconomyManager(_transactionIDManager, _playerManager, _galaxyManager, _cargoSynchronizer, _shipManager, _databaseManager, _masterServerManager); _killManager = new KillManager(_cargoSynchronizer, _playerManager, _galaxyManager, _messageManager, _connectionManager, _warpManager, _chatManager, _economyManager); _collisionManager = new CollisionManager(_galaxyManager, _messageManager, _killManager, _projectileManager); _registrationManager = new GalaxyRegistrationManager(_galaxyManager, _shipManager, _collisionManager, _galaxyIDManager, _playerManager, _accountManager, _cargoSynchronizer, _structureManager); _warpManager.SetRegistrationManager(_registrationManager);//Gross, I know. _locatorService = new LocatorService(_registrationManager, _playerManager, _galaxyManager, _shipManager, _accountManager, _teamManager, _teamManager, _messageManager, _structureManager, _masterServerManager); _msMessageHandler = new MasterServerMessageHandler((int)_masterServerManager.SlaveID, _redisServer, _connectionManager, _locatorService, _accountManager, _accountIDManager, _databaseManager, _galaxyManager, _galaxyIDManager, _playerManager, _shipManager, _registrationManager, _teamIDManager, _messageManager, _teamManager, _warpManager, _transactionIDManager, ProcessRoutedMessage); StructureFactory.Initialize(_galaxyIDManager, _registrationManager); ColonyFactory.Initialize(_galaxyIDManager, _registrationManager); dbSyncer = new DBSyncer(_databaseManager, _galaxyManager, _shipManager, _playerManager, _accountManager, _structureManager); #if DEBUG _typesToExcludeIgnore.Add(MessageTypes.PositionUpdateData); _typesToExcludeIgnore.Add(MessageTypes.ShipFireRequest); _typesToExcludeIgnore.Add(MessageTypes.ProjectileCollisionReport); _typesToExcludeIgnore.Add(MessageTypes.StructureFireRequest); _typesToExcludeIgnore.Add(MessageTypes.ObjectPickupRequest); #endif }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { #region 跨域设置 services.AddCors(c => { c.AddPolicy("LimitRequests", policy => { policy .WithOrigins(AppSettings.Configuration["Startup:AllowOrigins"].Split('|')) .AllowAnyHeader()//Ensures that the policy allows any header. .AllowAnyMethod(); }); }); #endregion #region 自动映射 services.AddScoped <IMapper, ServiceMapper>(); #endregion #region 说明文档 services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Version = "v1", Title = $"{AppSettings.Configuration["Startup:ApiName"]} 接口文档", Description = $"{AppSettings.Configuration["Startup:ApiName"]} HTTP API " }); try { //就是这里 var xmlPath = Path.Combine(AppContext.BaseDirectory, "Meiam.System.Hostd.xml"); //这个就是刚刚配置的xml文件名 c.IncludeXmlComments(xmlPath, true); //默认的第二个参数是false,这个是controller的注释,记得修改 } catch (Exception ex) { Console.WriteLine($"Xml 文件丢失,请检查并拷贝。\n{ ex.Message}"); } // 开启加权小锁 c.OperationFilter <AppendAuthorizeFilter>(); }); #endregion #region 配置Json格式 services.AddMvc().AddNewtonsoftJson(options => { // 忽略循环引用 options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; // 不使用驼峰 //options.SerializerSettings.ContractResolver = new DefaultContractResolver(); // 设置时间格式 options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; // 如字段为null值,该字段不会返回到前端 //options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; }); #endregion #region 获取客户端 IP services.Configure <ForwardedHeadersOptions>(options => { options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto; options.KnownNetworks.Clear(); options.KnownProxies.Clear(); }); #endregion #region 加载项 //注入缓存 services.AddMemoryCache(); //注入 HTTPCONTEXT services.AddHttpContextAccessor(); //注入 TokenManager services.AddScoped <TokenManager>(); //注入实体映射服务 services.AddScoped <IMapper, ServiceMapper>(); //注入短信服务 services.AddSingleton <IAliyunSmsServices, AliyunSmsServices>(); //注入全局异常过滤 services.AddControllers(options => { //全局异常过滤 options.Filters.Add <GlobalExceptions>(); //全局日志 options.Filters.Add <GlobalActionMonitor>(); }) .ConfigureApiBehaviorOptions(options => { //抑制系统自带模型验证 options.SuppressModelStateInvalidFilter = true; }); //开启计划任务 services.AddTaskSchedulers(); //注册REDIS 服务 RedisServer.Initalize(); #endregion }
public SlaveServerConfigService(RedisServer redis, int currentServiceId) { CurrentServiceId = currentServiceId; _redis = redis; }
/// <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 }
private void DoValidate() { RedisServer redis = new RedisServer(ConfigurationManager.Get("redisHost"), 6379, ConfigurationManager.Get("redisPassword")); string key = "locker-validate-" + Name; try { if (SpiderContext.Validations == null) { return; } var validations = SpiderContext.Validations.GetValidations(); if (validations != null && validations.Count > 0) { foreach (var validation in validations) { validation.CheckArguments(); } } if (redis != null) { while (!redis.LockTake(key, "0", TimeSpan.FromMinutes(10))) { Thread.Sleep(1000); } } var lockerValue = redis?.HashGet(ValidateStatusName, Name); bool needInitStartRequest = lockerValue != "validate finished"; if (needInitStartRequest) { Logger.Info("开始数据验证 ..."); if (validations != null && validations.Count > 0) { MailBodyBuilder builder = new MailBodyBuilder(Name, SpiderContext.Validations.Corporation); foreach (var validation in validations) { builder.AddValidateResult(validation.Validate()); } string mailBody = builder.Build(); using (EmailClient client = new EmailClient(SpiderContext.Validations.EmailSmtpServer, SpiderContext.Validations.EmailUser, SpiderContext.Validations.EmailPassword, SpiderContext.Validations.EmailSmtpPort)) { client.SendMail(new EmaillMessage($"{Name} " + "validation report", mailBody, SpiderContext.Validations.EmailTo) { IsHtml = true }); } } } else { Logger.Info("有其他线程执行了数据验证."); } if (needInitStartRequest) { redis?.HashSet(ValidateStatusName, Name, "validate finished"); } } catch (Exception e) { Logger.Error(e.Message, e); } finally { redis?.LockRelease(key, 0); } }
private RedisCheckDuplicateAdapter() { redisServer = RedisManager.GetRedisServer("redisCacheCrawlerDuplicate"); database = redisServer.GetDatabase(4); }
public void RemoveItemWithNullLockId() { using (RedisServer redisServer = new RedisServer()) { string sessionId = ResetRedisConnectionWrapperAndConfiguration(); RedisSessionStateProvider ssp = new RedisSessionStateProvider(); ssp.RemoveItem(null, sessionId, null, null); DisposeRedisConnectionWrapper(); } }
public KeysScanEnumerable(RedisServer server, int db, RedisValue pattern, int pageSize, long cursor, int pageOffset, CommandFlags flags) : base(server, server.server, db, pageSize, cursor, pageOffset, flags) { this.pattern = pattern; }
public void TryRemoveIfLockIdMatch_ValidLockIdAndRemove() { using (RedisServer redisServer = new RedisServer()) { RedisConnectionWrapper redisConn = GetRedisConnectionWrapperWithUniqueSession(); // Inserting data into redis server ChangeTrackingSessionStateItemCollection data = new ChangeTrackingSessionStateItemCollection(); data["key"] = "value"; redisConn.Set(data, 900); int lockTimeout = 900; DateTime lockTime = DateTime.Now; object lockId; ISessionStateItemCollection dataFromRedis; int sessionTimeout; Assert.True(redisConn.TryTakeWriteLockAndGetData(lockTime, lockTimeout, out lockId, out dataFromRedis, out sessionTimeout)); Assert.Equal(lockTime.Ticks.ToString(), lockId.ToString()); Assert.Equal(1, dataFromRedis.Count); redisConn.TryRemoveAndReleaseLockIfLockIdMatch(lockId); // Get actual connection and get data from redis IDatabase actualConnection = GetRealRedisConnection(redisConn); Assert.False(actualConnection.KeyExists(redisConn.Keys.DataKey)); // check lock removed from redis Assert.False(actualConnection.KeyExists(redisConn.Keys.LockKey)); DisposeRedisConnectionWrapper(redisConn); } }
public void RedisBaseTest() { RedisServer r = new RedisServer("localhost"); r.Db = 3; #region set long i; r.SetLength("a"); r.SetAdd("foo", "bar"); r.FlushDb(); Assert.IsTrue((i = r.SetLength("foo")) == 0, "there should be no keys but there were {0}", i); r.SetAdd("foo", "bar"); Assert.IsTrue((i = r.SetLength("foo")) == 1, "there should be one key but there were {0}", i); r.SetAdd("foo bär", "bär foo"); r.SetAdd("foo", "bär foo"); Assert.IsTrue((i = r.SetLength("foo")) == 2, "there should be two keys but there were {0}", i); Assert.IsTrue(r.TypeOf("foo") == KeyType.Set, "type is not string"); r.SetAdd("bar", "foo"); Assert.IsTrue(r.SetContains("bar", "foo")); var mems = r.SetMembers("foo"); Assert.AreEqual("bar", mems[0]); Assert.AreEqual("bär foo", mems[1]); r.SetRemove("foo", "bar"); mems = r.SetMembers("foo"); Assert.AreEqual("bär foo", mems[0]); Assert.AreEqual(1, mems.Count); string item = r.SetPop("foo"); Assert.AreEqual("bär foo", item); Assert.AreEqual(0, r.SetLength("foo")); #endregion #region hasset r.HashSet("set", "a", "a"); r.HashSet("set", "b", "b"); r.HashSet("set", "c", "c"); var hash = r.HashGetAll("set"); Assert.AreEqual(hash.Length, 3); r.HashDelete("set", "a"); hash = r.HashGetAll("set"); Assert.AreEqual(hash.Length, 2); Assert.AreEqual(true, r.HashExists("set", "b")); Assert.AreEqual("b", r.HashGet("set", "b")); Assert.AreEqual(2, r.HashLength("set")); #endregion #region storedset r.SortedSetAdd("sortedset", "a", 0L); r.SortedSetAdd("sortedset", "b", 0L); r.SortedSetAdd("sortedset", "c", 0L); r.SortedSetAdd("sortedset", "d", 0L); Assert.AreEqual(4, r.SortedSetLength("sortedset")); r.SortedSetRemove("sortedset", "a"); Assert.AreEqual(3, r.SortedSetLength("sortedset")); string[] sr = r.SortedSetRangeByRank("sortedset", 0, 2); Assert.AreEqual("b", sr[0]); Assert.AreEqual("c", sr[1]); Assert.AreEqual("d", sr[2]); #endregion #region list r.ListLeftPush("list", "1"); r.ListLeftPush("list", "2"); r.ListLeftPush("list", "3"); r.ListLeftPush("list", "4"); r.ListRightPush("list", "5"); Assert.AreEqual(5, r.ListLength("list")); Assert.AreEqual("4", r.ListLeftPop("list")); Assert.AreEqual("5", r.ListRightPop("list")); Assert.AreEqual("1", r.ListRightPop("list")); Assert.AreEqual("3", r.ListLeftPop("list")); #endregion #region locker r.LockTake("locker", DateTime.Now.ToString(), new TimeSpan(0, 0, 0, 30)); #endregion r.FlushDb(); r.Dispose(); }
public void TryTakeWriteLockAndGetData_ExpireWriteLock() { using (RedisServer redisServer = new RedisServer()) { RedisConnectionWrapper redisConn = GetRedisConnectionWrapperWithUniqueSession(); // Inserting data into redis server ChangeTrackingSessionStateItemCollection data = new ChangeTrackingSessionStateItemCollection(); data["key"] = "value"; redisConn.Set(data, 900); int lockTimeout = 1; DateTime lockTime = DateTime.Now; object lockId; ISessionStateItemCollection dataFromRedis; int sessionTimeout; Assert.True(redisConn.TryTakeWriteLockAndGetData(lockTime, lockTimeout, out lockId, out dataFromRedis, out sessionTimeout)); Assert.Equal(lockTime.Ticks.ToString(), lockId.ToString()); Assert.Equal(1, dataFromRedis.Count); // Wait for 2 seconds so that lock will expire System.Threading.Thread.Sleep(1100); // Get actual connection and check that lock do not exists IDatabase actualConnection = GetRealRedisConnection(redisConn); string lockValueFromRedis = actualConnection.StringGet(redisConn.Keys.LockKey); Assert.Equal(null, lockValueFromRedis); // remove data from redis actualConnection.KeyDelete(redisConn.Keys.DataKey); DisposeRedisConnectionWrapper(redisConn); } }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { #region 务注入 //启用数据库链接 services.AddSqlsugarSetup(); //跨域设置 services.AddCorsSetup(); //说明文档 services.AddSwaggerSetup(); //注入Cap (EventBus) 按需引入 //services.AddCapSetup(); //注入缓存 services.AddMemoryCache(); //注入 HTTPCONTEXT services.AddHttpContextAccessor(); //注入 TokenManager services.AddScoped <TokenManager>(); //注入实体映射服务 services.AddScoped <IMapper, ServiceMapper>(); //注入短信服务 services.AddSingleton <IAliyunSmsServices, AliyunSmsServices>(); //注册REDIS 服务 RedisServer.Initalize(); //开启计划任务 services.AddTaskSchedulers(); #endregion #region 全局设置 //配置Json格式 services.AddMvc().AddNewtonsoftJson(options => { // 忽略循环引用 options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; // 不使用驼峰 //options.SerializerSettings.ContractResolver = new DefaultContractResolver(); // 设置时间格式 options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; // 如字段为null值,该字段不会返回到前端 //options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; }); //获取客户端 IP services.Configure <ForwardedHeadersOptions>(options => { options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto; options.KnownNetworks.Clear(); options.KnownProxies.Clear(); }); //注入全局异常过滤 services.AddControllers(options => { //全局异常过滤 options.Filters.Add <GlobalExceptions>(); //全局日志 options.Filters.Add <GlobalActionMonitor>(); }) .ConfigureApiBehaviorOptions(options => { //抑制系统自带模型验证 options.SuppressModelStateInvalidFilter = true; }); #endregion }
public DBStateResolver(RedisServer redisServer) : base(redisServer) { }
public void TryReleaseLockIfLockIdMatch_InvalidWriteLockRelease() { using (RedisServer redisServer = new RedisServer()) { RedisConnectionWrapper redisConn = GetRedisConnectionWrapperWithUniqueSession(); // Inserting data into redis server ChangeTrackingSessionStateItemCollection data = new ChangeTrackingSessionStateItemCollection(); data["key"] = "value"; redisConn.Set(data, 900); int lockTimeout = 900; DateTime lockTime = DateTime.Now; object lockId; ISessionStateItemCollection dataFromRedis; int sessionTimeout; Assert.True(redisConn.TryTakeWriteLockAndGetData(lockTime, lockTimeout, out lockId, out dataFromRedis, out sessionTimeout)); Assert.Equal(lockTime.Ticks.ToString(), lockId.ToString()); Assert.Equal(1, dataFromRedis.Count); object wrongLockId = lockTime.AddSeconds(1).Ticks.ToString(); redisConn.TryReleaseLockIfLockIdMatch(wrongLockId); // Get actual connection and check that lock do not exists IDatabase actualConnection = GetRealRedisConnection(redisConn); string lockValueFromRedis = actualConnection.StringGet(redisConn.Keys.LockKey); Assert.Equal(lockId, lockValueFromRedis); // remove data from redis actualConnection.KeyDelete(redisConn.Keys.DataKey); actualConnection.KeyDelete(redisConn.Keys.LockKey); DisposeRedisConnectionWrapper(redisConn); } }
public RedisOutgoingMessageService(RedisServer redisServer, NPCPlayer player) { _player = player; _redisServer = redisServer; }
public void TryTakeReadLockAndGetData_WithoutAnyLock() { using (RedisServer redisServer = new RedisServer()) { RedisConnectionWrapper redisConn = GetRedisConnectionWrapperWithUniqueSession(); // Inserting data into redis server ChangeTrackingSessionStateItemCollection data = new ChangeTrackingSessionStateItemCollection(); data["key"] = "value"; redisConn.Set(data, 900); object lockId; ISessionStateItemCollection dataFromRedis; int sessionTimeout; Assert.True(redisConn.TryCheckWriteLockAndGetData(out lockId, out dataFromRedis, out sessionTimeout)); Assert.Equal(null, lockId); Assert.Equal(1, dataFromRedis.Count); Assert.Equal("value", dataFromRedis["key"]); // Get actual connection // remove data from redis IDatabase actualConnection = GetRealRedisConnection(redisConn); actualConnection.KeyDelete(redisConn.Keys.DataKey); DisposeRedisConnectionWrapper(redisConn); } }
/// <summary> /// Use a different RedisMessenger object for each simulated area, setting AreaID as appropriate. /// </summary> /// <param name="redisServer"></param> /// <param name="areaID"></param> public RedisMessenger(RedisServer redisServer, int areaID) { _redisServer = redisServer; AreaID = areaID; }
public RedisBasedKeyGenerator(RedisServer redis) { _redis = redis; _cachedKeys = new Dictionary <int, byte[]>(); _timeOfLastFetchLookup = new Dictionary <int, DateTime>(); }
private CacheTrackDeleteProduct() { redisServer = RedisManager.GetRedisServer("cacheTrackValidProduct"); database = redisServer.GetDatabase(1); }
public void TryTakeWriteLockAndGetData_WriteLockWithoutAnyOtherLock() { using (RedisServer redisServer = new RedisServer()) { RedisConnectionWrapper redisConn = GetRedisConnectionWrapperWithUniqueSession(); // Inserting data into redis server ChangeTrackingSessionStateItemCollection data = new ChangeTrackingSessionStateItemCollection(); data["key"] = "value"; redisConn.Set(data, 900); DateTime lockTime = DateTime.Now; int lockTimeout = 900; object lockId; ISessionStateItemCollection dataFromRedis; int sessionTimeout; Assert.True(redisConn.TryTakeWriteLockAndGetData(lockTime, lockTimeout, out lockId, out dataFromRedis, out sessionTimeout)); Assert.Equal(lockTime.Ticks.ToString(), lockId.ToString()); Assert.Equal(1, dataFromRedis.Count); // Get actual connection and get data lock from redis IDatabase actualConnection = GetRealRedisConnection(redisConn); string lockValueFromRedis = actualConnection.StringGet(redisConn.Keys.LockKey); Assert.Equal(lockTime.Ticks.ToString(), lockValueFromRedis); // remove data and lock from redis actualConnection.KeyDelete(redisConn.Keys.DataKey); actualConnection.KeyDelete(redisConn.Keys.LockKey); DisposeRedisConnectionWrapper(redisConn); } }
public RedisHistoryCrawler() { redisServer = RedisManager.GetRedisServer("redisCacheCrawlerDuplicate"); database = redisServer.GetDatabase(10); }
public void TryUpdateIfLockIdMatch_ExpiryTime_OnValidData() { using (RedisServer redisServer = new RedisServer()) { RedisConnectionWrapper redisConn = GetRedisConnectionWrapperWithUniqueSession(); // Inserting data into redis server ChangeTrackingSessionStateItemCollection data = new ChangeTrackingSessionStateItemCollection(); data["key"] = "value"; data["key1"] = "value1"; redisConn.Set(data, 900); // Check that data shoud exists int lockTimeout = 90; DateTime lockTime = DateTime.Now; object lockId; ISessionStateItemCollection dataFromRedis; int sessionTimeout; redisConn.TryTakeWriteLockAndGetData(lockTime, lockTimeout, out lockId, out dataFromRedis, out sessionTimeout); Assert.Equal(2, dataFromRedis.Count); // Update expiry time to only 1 sec and than verify that. redisConn.TryUpdateAndReleaseLockIfLockIdMatch(lockId, dataFromRedis, 1); // Wait for 1.1 seconds so that data will expire System.Threading.Thread.Sleep(1100); // Get data blob from redis IDatabase actualConnection = GetRealRedisConnection(redisConn); HashEntry[] sessionDataFromRedisAfterExpire = actualConnection.HashGetAll(redisConn.Keys.DataKey); // Check that data shoud not be there Assert.Equal(0, sessionDataFromRedisAfterExpire.Length); DisposeRedisConnectionWrapper(redisConn); } }
/// <summary> /// Instantiates and registers all objects associated with a PSystem (planets, players, ships, etc) /// </summary> public static async Task <PSystem> DeserializePSystemAsync(PSystemModel system, RedisServer redisServer, LocatorService ls, IGalaxyRegistrationManager rm, IDatabaseManager dbm) { PSystem retSys = new PSystem(system, ls); List <IShip> deserializedShips = new List <IShip>(); List <IStructure> loadedStructures = new List <IStructure>(); List <Player> loadedPlayers = new List <Player>(); //For now we just run the whole lambda synchronously in its own thread. Marking it async makes this method return complete before the task is complete, need to investigate... await Task.Factory.StartNew(() => { HashSet <int> colonyIDsToLoad = new HashSet <int>(); HashSet <int> shipIDsToLoad = new HashSet <int>(); HashSet <int> areaIDsToLoad = new HashSet <int>(); List <int> structureIDsToLoad = new List <int>(); HashSet <int> layoutIDsToLoad = new HashSet <int>(); foreach (int id in system.ShipIDs) { if (shipIDsToLoad.Contains(id)) { throw new CorruptStateException("Multiple areas contain the same shipID."); } shipIDsToLoad.Add(id); } foreach (var id in retSys.MoonIDs) { areaIDsToLoad.Add(id); } foreach (var id in retSys.PlanetIDs) { areaIDsToLoad.Add(id); } foreach (var id in retSys.PortIDs) { areaIDsToLoad.Add(id); } IEnumerable <AreaModel> loadedAreaModels = dbm.GetAreasAsync(areaIDsToLoad).Result; foreach (var am in loadedAreaModels) { switch (am.AreaType) { case AreaTypes.Planet: layoutIDsToLoad.Add(((PlanetModel)am).LayoutId); break; } } IEnumerable <PlanetLayout> loadedLayouts = dbm.GetLayoutsAsync(layoutIDsToLoad).Result.Select(s => (PlanetLayout)s); Dictionary <int, PlanetLayout> layouts = new Dictionary <int, PlanetLayout>(); foreach (var l in loadedLayouts) { layouts.Add(l.Id, l); } var loadedAreas = new List <IArea>(); // Instantiate all areas foreach (AreaModel am in loadedAreaModels) { IArea loadedArea = null; structureIDsToLoad.AddRange(am.StructureIDs); // Planets if (am.AreaType == AreaTypes.Planet) { loadedArea = new Planet((PlanetModel)am, layouts[((PlanetModel)am).LayoutId], ls); var p = loadedArea as Planet; //rm.RegisterObject(p); if (p.ColonyID != null) { colonyIDsToLoad.Add((int)p.ColonyID); } } // Ports else if (am.AreaType == AreaTypes.Port) { loadedArea = new Port((PortModel)am, ls); } else { throw new Exception("Error: Loaded area not handled in DeserializePSystem()"); } foreach (var id in am.ShipIDs) { if (shipIDsToLoad.Contains(id)) { throw new CorruptStateException("Multiple areas contain the same shipID."); } shipIDsToLoad.Add(id); } loadedAreas.Add(loadedArea); rm.RegisterObject(loadedArea); } // Colonies IEnumerable <AreaModel> LoadedColonies = dbm.GetAreasAsync(colonyIDsToLoad).Result; List <Colony> deserializedColonies = new List <Colony>(); foreach (AreaModel am in LoadedColonies) { if (am.AreaType == AreaTypes.Colony) { Colony c = new Colony((ColonyModel)am, ls); c.DisableUpdates = true; rm.RegisterObject(c); deserializedColonies.Add(c); } else { throw new Exception("AreaID query resulted in an AreaModel which was not a ColonyModel in DeserializePSystem()"); } foreach (var id in am.ShipIDs) { if (shipIDsToLoad.Contains(id)) { throw new CorruptStateException("Multiple areas contain the same shipID."); } shipIDsToLoad.Add(id); } } // Structures loadedStructures.AddRange(LoadStructures(retSys, dbm, rm, ls.PlayerLocator).Result); foreach (IArea loadedArea in loadedAreas) { if (loadedArea is IHasStructures) { loadedStructures.AddRange(LoadStructures((IHasStructures)loadedArea, dbm, rm, ls.PlayerLocator).Result); } } // Ships IEnumerable <ShipModel> loadedShipModels = dbm.GetShipsAsync(shipIDsToLoad).Result; HashSet <int> playerIDsToLoad = new HashSet <int>(); foreach (var s in loadedShipModels) { var loadedShip = DeserializeShip(s, ls, rm); deserializedShips.Add(loadedShip); if (loadedShip.PlayerID != null) { playerIDsToLoad.Add((int)s.PlayerID); } } // Players IEnumerable <PlayerModel> loadedPlayerModels = dbm.GetPlayersAsync(playerIDsToLoad).Result; HashSet <int> accountIDsToLoad = new HashSet <int>(); foreach (var p in loadedPlayerModels) { if (p.PlayerType == PlayerTypes.Human) { HumanPlayer hp = new HumanPlayer(p, ls); rm.RegisterObject(hp); loadedPlayers.Add(hp); } else { NPCPlayer np = new NPCPlayer(p, ls); np.MessageService = new RedisOutgoingMessageService(redisServer, np); rm.RegisterObject(np); loadedPlayers.Add(np); } if (p.AccountID != null) { accountIDsToLoad.Add((int)p.AccountID); } } //Accounts IEnumerable <AccountModel> loadedAccounts = dbm.GetAccountsAsync(accountIDsToLoad).Result; foreach (var a in loadedAccounts) { rm.RegisterObject(new Account(a)); } foreach (var c in deserializedColonies) { c.DisableUpdates = false; } } ); rm.RegisterObject(retSys); foreach (Player p in loadedPlayers) { if (p.PlayerType == PlayerTypes.NPC) { p.GetArea().MovePlayerHere(p, false); } } foreach (var s in deserializedShips) { IArea a = s.GetArea(); if (a == null)//Corrupt state, the ship's CurrentAreaId doesn't match the list is for an area which hasn't been loaded yet. Abort the ship read. { //I'm not sure that there's a way to gracefully handle this without a major rewrite and a bunch of overhead, so it'll be an exception for now. throw new CorruptStateException("Ship's CurrentAreaId does not match the area from which it was loaded. Expect exceptions."); } else { a.AddShip(s, true); } } return(retSys); }
public void TryUpdateIfLockIdMatch_WithValidUpdateAndDelete() { using (RedisServer redisServer = new RedisServer()) { RedisConnectionWrapper redisConn = GetRedisConnectionWrapperWithUniqueSession(); // Inserting data into redis server ChangeTrackingSessionStateItemCollection data = new ChangeTrackingSessionStateItemCollection(); data["key1"] = "value1"; data["key2"] = "value2"; data["key3"] = "value3"; redisConn.Set(data, 900); int lockTimeout = 900; DateTime lockTime = DateTime.Now; object lockId; ISessionStateItemCollection dataFromRedis; int sessionTimeout; Assert.True(redisConn.TryTakeWriteLockAndGetData(lockTime, lockTimeout, out lockId, out dataFromRedis, out sessionTimeout)); Assert.Equal(lockTime.Ticks.ToString(), lockId.ToString()); Assert.Equal(3, dataFromRedis.Count); Assert.Equal("value1", dataFromRedis["key1"]); Assert.Equal("value2", dataFromRedis["key2"]); Assert.Equal("value3", dataFromRedis["key3"]); dataFromRedis["key2"] = "value2-updated"; dataFromRedis.Remove("key3"); redisConn.TryUpdateAndReleaseLockIfLockIdMatch(lockId, dataFromRedis, 900); // Get actual connection and get data from redis IDatabase actualConnection = GetRealRedisConnection(redisConn); HashEntry[] sessionDataFromRedis = actualConnection.HashGetAll(redisConn.Keys.DataKey); Assert.Equal(2, sessionDataFromRedis.Length); ChangeTrackingSessionStateItemCollection sessionDataFromRedisAsCollection = new ChangeTrackingSessionStateItemCollection(); foreach (HashEntry entry in sessionDataFromRedis) { sessionDataFromRedisAsCollection[entry.Name] = RedisUtility.GetObjectFromBytes(entry.Value).ToString(); } Assert.Equal("value1", sessionDataFromRedisAsCollection["key1"]); Assert.Equal("value2-updated", sessionDataFromRedisAsCollection["key2"]); // check lock removed and remove data from redis actualConnection.KeyDelete(redisConn.Keys.DataKey); Assert.False(actualConnection.KeyExists(redisConn.Keys.LockKey)); DisposeRedisConnectionWrapper(redisConn); } }
public static SpaceStateManager BuildSpaceStateManager(int areaID, RedisServer _redisServer, INetworkingService _networkingService) { return(DrawlessStateBuilder.BuildSpaceStateManager(areaID, new RedisMessenger(_redisServer, areaID), _networkingService)); }
private Core.Spider PrepareSpider(params string[] args) { RedisServer redis = GetManageRedisServer(); var schedulerType = SpiderContext.Scheduler.Type; bool isTestSpider = args != null && args.Contains("test"); switch (schedulerType) { case Configuration.Scheduler.Types.Queue: { PrepareSite(); var spider = GenerateSpider(SpiderContext.Scheduler.GetScheduler()); if (isTestSpider && spider.Site.StartRequests.Count > 0) { spider.Site.StartRequests = new List <Request> { spider.Site.StartRequests[0] }; } spider.InitComponent(); return(spider); } case Configuration.Scheduler.Types.Redis: { var scheduler = (Scheduler.RedisScheduler)(SpiderContext.Scheduler.GetScheduler()); string key = "locker-" + Name; if (args != null && args.Length > 0) { if (args.Contains("rerun")) { if (redis != null) { redis.KeyDelete(key); redis.HashDelete("init-status", Name); redis.HashDelete("validate-status", Name); redis.HashDelete(Scheduler.RedisScheduler.TaskStatus, Name); redis.SortedSetRemove(Scheduler.RedisScheduler.TaskList, Name); } scheduler.Redis.KeyDelete(Scheduler.RedisScheduler.GetQueueKey(Name)); scheduler.Redis.KeyDelete(Scheduler.RedisScheduler.GetSetKey(Name)); scheduler.Redis.KeyDelete(Scheduler.RedisScheduler.GetItemKey(Name)); } if (args.Contains("noconsole")) { Log.WriteLine("No console log info."); Log.NoConsole = true; } } try { if (redis != null) { while (!redis.LockTake(key, "0", TimeSpan.FromMinutes(10))) { Thread.Sleep(1000); } } var lockerValue = redis?.HashGet(InitStatusSetName, Name); bool needInitStartRequest = lockerValue != "init finished"; if (needInitStartRequest) { PrepareSite(); } else { Logger.Info("Site 已经初始化"); SpiderContext.Site.ClearStartRequests(); } Logger.Info("创建爬虫..."); var spider = GenerateSpider(scheduler); spider.SaveStatus = true; SpiderMonitor.Default.Register(spider); Logger.Info("构建内部模块..."); if (isTestSpider && spider.Site.StartRequests.Count > 0) { spider.Site.StartRequests = new List <Request> { spider.Site.StartRequests[0] }; } spider.InitComponent(); if (needInitStartRequest) { redis?.HashSet(InitStatusSetName, Name, "init finished"); } return(spider); } catch (Exception e) { Logger.Error(e.Message, e); return(null); } finally { try { redis?.LockRelease(key, 0); } catch { // ignored } } } } throw new SpiderExceptoin("初始化失败."); }
public static PlanetStateManager BuildPlanetStateManager(int areaID, RedisServer _redisServer, INetworkingService _networkingService, PhysicsConfig _physicsConfig) { return(DrawlessStateBuilder.BuildPlanetStateManager(areaID, new RedisMessenger(_redisServer, areaID), _networkingService, _physicsConfig)); }
public RedisCheckDuplicateAdapter(string server) { redisServer = RedisManager.GetRedisServer(server); database = redisServer.GetDatabase(4); }
public RedisScheduler(RedisServer redis) : this() { Redis = redis; Redis.Db = 0; }