StackExchange.Redis.Extensions icon indicating copy to clipboard operation
StackExchange.Redis.Extensions copied to clipboard

Redis connection error restored

Open fjluquerodriguez opened this issue 4 years ago • 15 comments

In a basic console app when DI resolves in constructor a error log raised: "Redis connection error restored." It comes from ConnectionRestored in RedisCacheConnectionPoolManager.

DI config:

public static ServiceCollection RegisterCommonServices(this ServiceCollection services, RedisConfiguration redisConfiguration)
        {
            services.AddSingleton<ISerializer, NewtonsoftSerializer>();
            services.AddSingleton(redisConfiguration);
            services.AddSingleton<IRedisCacheClient, RedisCacheClient>();
            services.AddSingleton<IRedisCacheConnectionPoolManager, RedisCacheConnectionPoolManager>();

            services.AddSingleton((provider) =>
            {
                return provider.GetRequiredService<IRedisCacheClient>().GetDbFromConfiguration();
            });
            return services;
        }

Config:

"Redis": {
    "Hosts": [
      {
        "Host": "127.0.0.1",
        "Port": "6379"
      }
    ], 
    "Database": 0,
    "ConnectTimeout": 60,
    "ConnectRetry": 2,
    "IdleTimeOutSecs": 120,
    "Ssl": false,
    "RequiresAuth": false,
    "NamespacePrefix": "",
    "PoolSize": 10,
    "MaxValueLength": 102400,
    "ConfigurationOptions": {
      "KeepAlive": 10,
      "ConnectTimeout": 30000,
      "ResponseTimeout": 60000,
      "AbortOnConnectFail": true,
      "AsyncTimeout": 15000,
      "SyncTimeout": 15000
    }

image

I've tried multiple combinations without luck. Despite this, if I try to read or write to redis it works fine... but this error is a bit confusing. I don't know if I can ignore it...

fjluquerodriguez avatar Jun 03 '21 08:06 fjluquerodriguez

up i had same error

ismkdc avatar Jun 19 '21 17:06 ismkdc

Hi, there is a specific package for that

https://github.com/imperugo/StackExchange.Redis.Extensions/tree/master/src/aspnet/StackExchange.Redis.Extensions.AspNetCore

Here https://github.com/imperugo/StackExchange.Redis.Extensions/blob/master/samples/StackExchange.Redis.Samples.Web.Mvc/Startup.cs#L42 and example on "how to use it"

imperugo avatar Jun 25 '21 12:06 imperugo

i still got the same error

ismkdc avatar Jul 01 '21 13:07 ismkdc

How do you use it? Because I use it in production without problem. Also the code in this repo (see the example project) works fine. Please download it and try it.

Thanks

imperugo avatar Jul 01 '21 16:07 imperugo

//register redis services.AddStackExchangeRedisExtensions<SystemTextJsonSerializer>(new RedisConfiguration { ConnectionString = Configuration.GetConnectionString("AriPlusRedis"), PoolSize = 100, ServerEnumerationStrategy = new ServerEnumerationStrategy { Mode = ServerEnumerationStrategy.ModeOptions.All, TargetRole = ServerEnumerationStrategy.TargetRoleOptions.Any, UnreachableServerAction = ServerEnumerationStrategy.UnreachableServerActionOptions.Throw } });

in appsettings "AriPlusRedis": "redis-master,redis-replica,password=xxx,abortConnect=false,connectTimeout=15000,allowAdmin=true"

ismkdc avatar Jul 01 '21 20:07 ismkdc

I can't see the serializer in your code, but I can see it here

https://github.com/imperugo/StackExchange.Redis.Extensions/blob/master/samples/StackExchange.Redis.Samples.Web.Mvc/Startup.cs#L42

That's the problem!

imperugo avatar Jul 12 '21 07:07 imperugo

i updated my code but exception still throws :(

services.AddStackExchangeRedisExtensions<SystemTextJsonSerializer>(new RedisConfiguration { ConnectionString = Configuration.GetConnectionString("AriPlusRedis"), PoolSize = 100, ServerEnumerationStrategy = new ServerEnumerationStrategy { Mode = ServerEnumerationStrategy.ModeOptions.All, TargetRole = ServerEnumerationStrategy.TargetRoleOptions.Any, UnreachableServerAction = ServerEnumerationStrategy.UnreachableServerActionOptions.Throw } });

ismkdc avatar Jul 12 '21 11:07 ismkdc

debian@ns3100181:~/ariplus-backend$ docker-compose -f docker-compose.yml logs web Attaching to ariplus-backend_web_2, ariplus-backend_web_1, ariplus-backend_web_4, ariplus-backend_web_3 web_1 | fail: StackExchange.Redis.Extensions.Core.Implementations.RedisCacheConnectionPoolManager[0] web_1 | Redis connection error restored. web_4 | fail: StackExchange.Redis.Extensions.Core.Implementations.RedisCacheConnectionPoolManager[0] web_4 | Redis connection error restored. web_4 | fail: StackExchange.Redis.Extensions.Core.Implementations.RedisCacheConnectionPoolManager[0] web_4 | Redis connection error restored.

ismkdc avatar Jul 12 '21 11:07 ismkdc

I have removed PoolSize parameter but problem still continue

ismkdc avatar Jul 22 '21 12:07 ismkdc

I have using totally same conf with sample but problem still continue

//register redis var conf = new RedisConfiguration { AbortOnConnectFail = true, Password = Configuration.GetValue("RedisConf:Password"), Hosts = Configuration.GetSection("RedisConf:Hosts").Get<RedisHost[]>(), AllowAdmin = true, ConnectTimeout = 6000, Database = 0, PoolSize = 50, ServerEnumerationStrategy = new ServerEnumerationStrategy { Mode = ServerEnumerationStrategy.ModeOptions.All, TargetRole = ServerEnumerationStrategy.TargetRoleOptions.Any, UnreachableServerAction = ServerEnumerationStrategy.UnreachableServerActionOptions.Throw } };

        services.AddStackExchangeRedisExtensions<SystemTextJsonSerializer>(conf);

ismkdc avatar Jul 22 '21 13:07 ismkdc

I'm getting the same error in my production logs. Digging deeper into sources for both StackExchange.Redis.Extensions and StackExchange.Redis , the error is logged when Redis connection is recovered from previous failure.

Strange enough, I don't see any mention of previous errors in my logs, and no other signs of crashing. My question here is: why is this event logged here as error? Why not warning, since it is a recovery event, not actual failure?

            private void ConnectionRestored(object sender, ConnectionFailedEventArgs e)
            {
                logger.LogError("Redis connection error restored.");
            }

kirillito avatar Sep 13 '21 23:09 kirillito

Same issue for me. It does not appear if its its executed the first time, but after shut down and re-connection it begins to display this error

ahmb avatar Sep 28 '21 19:09 ahmb

I have the same logs. It happens when I deploy the application only. I agree with @kirillito. I believe this should be logged as a warning.

ibrahimuludag avatar Oct 07 '21 17:10 ibrahimuludag

I had the same issue in my setup. In my ASP.NET Core service I am using DataProtection and of course I would like to use Redis to store the Keys. At first I used the following code:

services
    .AddDataProtection()
    .SetDefaultKeyLifetime(TimeSpan.FromDays(keyLifeTime))
    .PersistKeysToStackExchangeRedis(() => 
    {
        var sp = services.BuildServiceProvider();
        var redisCacheConnectionPoolManager = sp.GetService<IRedisCacheConnectionPoolManager>(); 
        return redisCacheConnectionPoolManager.GetConnection().GetDatabase(); 
    }
    , key)
    .UseCryptographicAlgorithms(new AuthenticatedEncryptorConfiguration()
    {
    EncryptionAlgorithm = EncryptionAlgorithm.AES_256_CBC,
    ValidationAlgorithm = ValidationAlgorithm.HMACSHA256
    });

I have recognized that the "Redis connection error restored" error was printed when I was trying to get the IDatabase instance directly via the RedisCacheConnectionPoolManager.

Then I have changed the section like this:

services
    .AddDataProtection()
    .SetDefaultKeyLifetime(TimeSpan.FromDays(keyLifeTime))
    .PersistKeysToStackExchangeRedis(() => 
    {
        var sp = services.BuildServiceProvider();
        var redisClient = sp.GetService<IRedisCacheClient>();
        return redisClient.GetDbFromConfiguration().Database;
    }
    , key)
    .UseCryptographicAlgorithms(new AuthenticatedEncryptorConfiguration()
    {
    EncryptionAlgorithm = EncryptionAlgorithm.AES_256_CBC,
    ValidationAlgorithm = ValidationAlgorithm.HMACSHA256
    });

Now I am getting a RedisClient from the managed Redis connection pool to retrieve the IDatabase instance and the error was gone.

sprudel79 avatar Nov 04 '21 13:11 sprudel79

Also have this bug, in V7 version.

GlodenBoy avatar May 24 '22 02:05 GlodenBoy