public void Does_retry_failed_commands() { //LogManager.LogFactory = new ConsoleLogFactory(debugEnabled: true); RedisStats.Reset(); var redisCtrl = new RedisClient(RedisConfig.DefaultHost); redisCtrl.FlushAll(); redisCtrl.SetClient("redisCtrl"); var redis = new RedisClient(RedisConfig.DefaultHost); redis.SetClient("redisRetry"); var clientInfo = redisCtrl.GetClientsInfo(); var redisId = clientInfo.First(m => m["name"] == "redisRetry")["id"]; Assert.That(redisId.Length, Is.GreaterThan(0)); Assert.That(redis.IncrementValue("retryCounter"), Is.EqualTo(1)); redis.OnBeforeFlush = () => { redisCtrl.KillClients(withId: redisId); }; Assert.That(redis.IncrementValue("retryCounter"), Is.EqualTo(2)); Assert.That(redis.Get <int>("retryCounter"), Is.EqualTo(2)); Assert.That(RedisStats.TotalRetryCount, Is.EqualTo(1)); Assert.That(RedisStats.TotalRetrySuccess, Is.EqualTo(1)); Assert.That(RedisStats.TotalRetryTimedout, Is.EqualTo(0)); }
public void Can_support_64_threads_using_the_client_simultaneously() { var before = Stopwatch.GetTimestamp(); const int noOfConcurrentClients = 64; //WaitHandle.WaitAll limit is <= 64 var clientAsyncResults = new List <IAsyncResult>(); using (var manager = new PooledRedisClientManager(TestConfig.MasterHosts, TestConfig.SlaveHosts)) { manager.GetClient().Run(x => x.FlushAll()); for (var i = 0; i < noOfConcurrentClients; i++) { var clientNo = i; var action = (Action)(() => UseClientAsync(manager, clientNo)); clientAsyncResults.Add(action.BeginInvoke(null, null)); } } WaitHandle.WaitAll(clientAsyncResults.ConvertAll(x => x.AsyncWaitHandle).ToArray()); Debug.WriteLine(string.Format("Completed in {0} ticks", (Stopwatch.GetTimestamp() - before))); RedisStats.ToDictionary().PrintDump(); }
public void Can_support_64_threads_using_the_client_simultaneously() { const int noOfConcurrentClients = 64; //WaitHandle.WaitAll limit is <= 64 var clientUsageMap = new Dictionary <string, int>(); var clientAsyncResults = new List <IAsyncResult>(); using (var manager = CreateAndStartManager()) { for (var i = 0; i < noOfConcurrentClients; i++) { var clientNo = i; var action = (Action)(() => UseClient(manager, clientNo, clientUsageMap)); clientAsyncResults.Add(action.BeginInvoke(null, null)); } } WaitHandle.WaitAll(clientAsyncResults.ConvertAll(x => x.AsyncWaitHandle).ToArray()); RedisStats.ToDictionary().PrintDump(); Debug.WriteLine(TypeSerializer.SerializeToString(clientUsageMap)); var hostCount = 0; foreach (var entry in clientUsageMap) { Assert.That(entry.Value, Is.GreaterThanOrEqualTo(2), "Host has unproportianate distribution: " + entry.Value); Assert.That(entry.Value, Is.LessThanOrEqualTo(30), "Host has unproportianate distribution: " + entry.Value); hostCount += entry.Value; } Assert.That(hostCount, Is.EqualTo(noOfConcurrentClients), "Invalid no of clients used"); }
public void Does_retry_failed_commands_auth() { // -> Redis must have "requirepass testpassword" in config var connstr = "testpassword@localhost"; RedisStats.Reset(); var redisCtrl = new RedisClient(connstr); //RedisConfig.DefaultHost redisCtrl.FlushAll(); redisCtrl.SetClient("redisCtrl"); var redis = new RedisClient(connstr); redis.SetClient("redisRetry"); var clientInfo = redisCtrl.GetClientsInfo(); var redisId = clientInfo.First(m => m["name"] == "redisRetry")["id"]; Assert.That(redisId.Length, Is.GreaterThan(0)); Assert.That(redis.IncrementValue("retryCounter"), Is.EqualTo(1)); redis.OnBeforeFlush = () => { redisCtrl.KillClients(withId: redisId); }; Assert.That(redis.IncrementValue("retryCounter"), Is.EqualTo(2)); Assert.That(redis.Get <int>("retryCounter"), Is.EqualTo(2)); Assert.That(RedisStats.TotalRetryCount, Is.EqualTo(1)); Assert.That(RedisStats.TotalRetrySuccess, Is.EqualTo(1)); Assert.That(RedisStats.TotalRetryTimedout, Is.EqualTo(0)); }
public void Does_retry_failed_commands_auth() { RedisStats.Reset(); var redisCtrl = new RedisClient(Config.MasterHost); redisCtrl.FlushAll(); redisCtrl.SetClient("redisCtrl"); var redis = new RedisClient(Config.MasterHost); redis.SetClient("redisRetry"); var clientInfo = redisCtrl.GetClientsInfo(); var redisId = clientInfo.First(m => m["name"] == "redisRetry")["id"]; Assert.That(redisId.Length, Is.GreaterThan(0)); Assert.That(redis.IncrementValue("retryCounter"), Is.EqualTo(1)); redis.OnBeforeFlush = () => { redisCtrl.KillClients(withId: redisId); }; Assert.That(redis.IncrementValue("retryCounter"), Is.EqualTo(2)); Assert.That(redis.Get <int>("retryCounter"), Is.EqualTo(2)); Assert.That(RedisStats.TotalRetryCount, Is.EqualTo(1)); Assert.That(RedisStats.TotalRetrySuccess, Is.EqualTo(1)); Assert.That(RedisStats.TotalRetryTimedout, Is.EqualTo(0)); }
public void Can_support_64_threads_using_the_client_simultaneously() { void UseClient(IRedisClientManager manager1, int clientNo1, Dictionary <string, int> hostCountMap1) { using (var client = manager1.GetClient()) { lock (hostCountMap1) { if (!hostCountMap1.TryGetValue(client.Host, out var hostCount1)) { hostCount1 = 0; } hostCountMap1[client.Host] = ++hostCount1; } Console.WriteLine("Client '{0}' is using '{1}'", clientNo1, client.Host); } } const int noOfConcurrentClients = 64; var clientUsageMap = new Dictionary <string, int>(); var clientAsyncResults = new List <Task>(); using (var manager = this.CreateAndStartManager()) { for (var i = 0; i < noOfConcurrentClients; i++) { var clientNo = i; var item = Task.Run(() => UseClient(manager, clientNo, clientUsageMap)); clientAsyncResults.Add(item); } Task.WaitAll(clientAsyncResults.ToArray()); } Console.WriteLine(RedisStats.ToDictionary()); Console.WriteLine(clientUsageMap.ToJson()); var hostCount = 0; foreach (var entry in clientUsageMap) { Assert.That(entry.Value, Is.GreaterThanOrEqualTo(2), "Host has unproportianate distribution: " + entry.Value); Assert.That(entry.Value, Is.LessThanOrEqualTo(30), "Host has unproportianate distribution: " + entry.Value); hostCount += entry.Value; } Assert.That(hostCount, Is.EqualTo(noOfConcurrentClients), "Invalid no of clients used"); }
public void Does_retry_failed_commands_with_SocketException() { RedisStats.Reset(); var redis = new RedisClient(Config.MasterHost); redis.FlushAll(); Assert.That(redis.IncrementValue("retryCounter"), Is.EqualTo(1)); redis.OnBeforeFlush = () => { redis.OnBeforeFlush = null; throw new SocketException(); }; Assert.That(redis.IncrementValue("retryCounter"), Is.EqualTo(2)); Assert.That(redis.Get <int>("retryCounter"), Is.EqualTo(2)); Assert.That(RedisStats.TotalRetryCount, Is.EqualTo(1)); Assert.That(RedisStats.TotalRetrySuccess, Is.EqualTo(1)); Assert.That(RedisStats.TotalRetryTimedout, Is.EqualTo(0)); }
public async Task Does_retry_failed_commands_with_SocketException() { RedisStats.Reset(); var redis = new RedisClient(RedisConfig.DefaultHost).ForAsyncOnly(); await redis.FlushAllAsync(); Assert.That(await redis.IncrementValueAsync("retryCounter"), Is.EqualTo(1)); ((RedisClient)redis).OnBeforeFlush = () => { ((RedisClient)redis).OnBeforeFlush = null; throw new SocketException(); }; Assert.That(await redis.IncrementValueAsync("retryCounter"), Is.EqualTo(2)); Assert.That(await redis.GetAsync <int>("retryCounter"), Is.EqualTo(3)); Assert.That(RedisStats.TotalRetryCount, Is.EqualTo(1)); Assert.That(RedisStats.TotalRetrySuccess, Is.EqualTo(1)); Assert.That(RedisStats.TotalRetryTimedout, Is.EqualTo(0)); }
public void Does_retry_failed_commands() { // warning: this test looks brittle; is often failing "Expected: 3 But was: 2" (on main branch); // LogManager.LogFactory = new ConsoleLogFactory(debugEnabled: true); // RedisConfig.EnableVerboseLogging = true; RedisStats.Reset(); var redisCtrl = new RedisClient(RedisConfig.DefaultHost); redisCtrl.FlushAll(); redisCtrl.SetClient("redisCtrl"); var redis = new RedisClient(RedisConfig.DefaultHost); redis.SetClient("redisRetry"); var clientInfo = redisCtrl.GetClientsInfo(); var redisId = clientInfo.First(m => m["name"] == "redisRetry")["id"]; Assert.That(redisId.Length, Is.GreaterThan(0)); Assert.That(redis.IncrementValue("retryCounter"), Is.EqualTo(1)); redis.OnBeforeFlush = () => { redisCtrl.KillClients(withId: redisId); }; Assert.That(redis.IncrementValue("retryCounter"), Is.EqualTo(2)); Assert.That(redis.Get <int>("retryCounter"), Is.EqualTo(3)); Assert.That(RedisStats.TotalRetryCount, Is.EqualTo(1)); Assert.That(RedisStats.TotalRetrySuccess, Is.EqualTo(1)); Assert.That(RedisStats.TotalRetryTimedout, Is.EqualTo(0)); }
public void Can_support_64_threads_using_the_client_simultaneously() { var before = Stopwatch.GetTimestamp(); const int noOfConcurrentClients = 64; using (var manager = new PooledRedisClientManager(Config.MasterHost)) { var clientAsyncResults = new List <Task>(); using (var client = manager.GetClient()) { client.FlushAll(); } for (var i = 0; i < noOfConcurrentClients; i++) { var clientNo = i; var item = Task.Run(() => UseClientAsync(manager, clientNo)); clientAsyncResults.Add(item); } Task.WaitAll(clientAsyncResults.ToArray()); } Console.WriteLine("Completed in {0} ticks", Stopwatch.GetTimestamp() - before); Console.WriteLine(RedisStats.ToDictionary().ToJson()); }
public object Any(GetRedisClientStats request) { return(new GetRedisClientStatsResponse { Result = RedisStats.ToDictionary() }); }
public void Execute() { RedisConfig.EnableVerboseLogging = false; LogManager.LogFactory = new ConsoleLogFactory(debugEnabled: true); log = LogManager.GetLogger(GetType()); RedisConfig.DefaultReceiveTimeout = 10000; OnSetUp(); using (var sentinel = CreateSentinel()) { if (UseRedisManagerPool) { sentinel.RedisManagerFactory = (masters, slaves) => new RedisManagerPool(masters); } var redisManager = sentinel.Start(); int i = 0; var clientTimer = new Timer { Interval = MessageInterval, Enabled = true }; clientTimer.Elapsed += (sender, args) => { log.Debug("clientTimer.Elapsed: " + (i++)); try { string key = null; using (var master = (RedisClient)redisManager.GetClient()) { var counter = master.Increment("key", 1); key = "key" + counter; log.DebugFormat("Set key {0} in read/write client #{1}@{2}", key, master.Id, master.GetHostString()); master.SetValue(key, "value" + 1); } using (var readOnly = (RedisClient)redisManager.GetReadOnlyClient()) { log.DebugFormat("Get key {0} in read-only client #{1}@{2}", key, readOnly.Id, readOnly.GetHostString()); var value = readOnly.GetValue(key); log.DebugFormat("{0} = {1}", key, value); } } catch (ObjectDisposedException) { log.DebugFormat("ObjectDisposedException detected, disposing timer..."); clientTimer.Dispose(); } catch (Exception ex) { log.Error("Error in Timer", ex); } if (i % 10 == 0) { log.Debug(RedisStats.ToDictionary().Dump()); } }; log.Debug("Sleeping for 5000ms..."); Thread.Sleep(5000); log.Debug("Failing over master..."); sentinel.ForceMasterFailover(); log.Debug("master was failed over"); log.Debug("Sleeping for 20000ms..."); Thread.Sleep(20000); try { var debugConfig = sentinel.GetMaster(); using (var master = new RedisClient(debugConfig)) { log.Debug("Putting master '{0}' to sleep for 35 seconds...".Fmt(master.GetHostString())); master.DebugSleep(35); } } catch (Exception ex) { log.Error("Error retrieving master for DebugSleep()", ex); } log.Debug("After DEBUG SLEEP... Sleeping for 5000ms..."); Thread.Sleep(5000); log.Debug("RedisStats:"); log.Debug(RedisStats.ToDictionary().Dump()); System.Console.ReadLine(); } OnTearDown(); }
public void Execute() { RedisConfig.DisableVerboseLogging = true; RedisConfig.DefaultReceiveTimeout = 10000; using (var sentinel = new RedisSentinel(Config.SentinelHosts)) { // if (this._useRedisManagerPool) { // sentinel.RedisManagerFactory = (masters, slaves) => new RedisManagerPool(masters); var redisManager = sentinel.Start(); var i = 0; Timer clientTimer = null; void OnClientTimerOnElapsed(object state) { Console.WriteLine(); Console.WriteLine("clientTimer.Elapsed: " + i++); try { string key = null; using (var master = (RedisClient)redisManager.GetClient()) { var counter = master.Increment("key", 1); key = "key" + counter; Console.WriteLine("Set key {0} in read/write client #{1}@{2}", key, master.Id, master.GetHostString()); master.SetValue(key, "value" + 1); } using (var readOnly = (RedisClient)redisManager.GetReadOnlyClient()) { Console.WriteLine("Get key {0} in read-only client #{1}@{2}", key, readOnly.Id, readOnly.GetHostString()); var value = readOnly.GetValue(key); Console.WriteLine("{0} = {1}", key, value); } } catch (ObjectDisposedException) { Console.WriteLine("ObjectDisposedException detected, disposing timer..."); clientTimer?.Dispose(); } catch (Exception ex) { Console.WriteLine("Error in Timer, {0}", ex); } if (i % 10 == 0) { Console.WriteLine(RedisStats.ToDictionary().ToJson()); } } clientTimer = new Timer(OnClientTimerOnElapsed, null, 0, 1000); Console.WriteLine("Sleeping for 5000ms..."); Thread.Sleep(5000); Console.WriteLine("Failing over master..."); sentinel.ForceMasterFailover(); Console.WriteLine("master was failed over"); Console.WriteLine("Sleeping for 20000ms..."); Thread.Sleep(20000); try { var debugConfig = sentinel.GetMaster(); using (var master = new RedisClient(debugConfig)) { Console.WriteLine("Putting master '{0}' to sleep for 35 seconds...", master.GetHostString()); master.DebugSleep(35); } } catch (Exception ex) { Console.WriteLine("Error retrieving master for DebugSleep(), {0}", ex); } Console.WriteLine("After DEBUG SLEEP... Sleeping for 5000ms..."); Thread.Sleep(5000); Console.WriteLine("RedisStats:"); Console.WriteLine(RedisStats.ToDictionary().ToJson()); } }