예제 #1
0
        public static CounterSet GetRedisCounters(this RedisDatabaseAdapter adapter, OperationContext context, Tracer tracer, Counter counter)
        {
            var redisInfo = adapter.GetRedisInfoAsync(context, tracer, counter, serverId: null, trace: false).GetAwaiter().GetResult();

            var counterSet = new CounterSet();

            if (!redisInfo)
            {
                // The error is already logged.
                return(counterSet);
            }

            foreach ((var serverId, var info) in redisInfo.Value.Info)
            {
                AddCounterValue($"{serverId}.{nameof(info.Uptime)}", (long?)info.Uptime?.TotalSeconds);
                AddCounterValue($"{serverId}.{nameof(info.KeyCount)}", info.KeyCount);
                AddCounterValue($"{serverId}.{nameof(info.ExpirableKeyCount)}", info.ExpirableKeyCount);
                AddCounterValue($"{serverId}.{nameof(info.EvictedKeys)}", info.EvictedKeys);
                AddCounterValue($"{serverId}.{nameof(info.KeySpaceHits)}", info.KeySpaceHits);
                AddCounterValue($"{serverId}.{nameof(info.KeySpaceMisses)}", info.KeySpaceMisses);
                AddCounterValue($"{serverId}.{nameof(info.UsedCpuAveragePersentage)}", info.UsedCpuAveragePersentage);
                AddCounterValue($"{serverId}.{nameof(info.UsedMemory)}Mb", (long?)(info.UsedMemory / Math.Pow(2, 20)));
                AddCounterValue($"{serverId}.{nameof(info.UsedMemoryRss)}Mb", (long?)(info.UsedMemoryRss / Math.Pow(2, 20)));
            }

            return(counterSet);

            void AddCounterValue(string name, long?value)
            {
                if (value != null)
                {
                    counterSet.Add(name, value.Value);
                }
            }
        }
예제 #2
0
 public RedisBlobAdapter(RedisDatabaseAdapter redis, TimeSpan blobExpiryTime, long maxCapacity, IClock clock)
 {
     _redis                 = redis;
     _blobExpiryTime        = blobExpiryTime;
     _capacityExpiryTime    = blobExpiryTime.Add(TimeSpan.FromMinutes(5));
     _maxCapacityPerTimeBox = maxCapacity / 2;
     _clock                 = clock;
 }
예제 #3
0
        /// <summary>
        /// Executes a batch with a set of operations defined by <paramref name="addOperations"/> callback.
        /// </summary>
        /// <exception cref="OperationCanceledException">
        /// If <code>context.Token</code> is cancelled or the connection is closed.
        /// </exception>
        public static async Task <T> ExecuteBatchAsync <T>(this RedisDatabaseAdapter adapter, OperationContext context, Func <RedisBatch, Task <T> > addOperations, RedisOperation operation)
        {
            var batch  = adapter.CreateBatchOperation(operation);
            var result = addOperations((RedisBatch)batch);
            await adapter.ExecuteBatchOperationAsync(context, batch, context.Token).IgnoreFailure();

            return(await result);
        }
예제 #4
0
        public RedisBlobAdapter(RedisDatabaseAdapter redis, IClock clock, RedisContentLocationStoreConfiguration configuration)
        {
            _redis                 = redis;
            _blobExpiryTime        = TimeSpan.FromMinutes(configuration.BlobExpiryTimeMinutes);
            _capacityExpiryTime    = _blobExpiryTime.Add(TimeSpan.FromMinutes(5));
            _maxCapacityPerTimeBox = configuration.MaxBlobCapacity / 2;
            _clock                 = clock;

            _operationThrottle = new OperationThrottle(configuration.BlobOperationLimitSpan, configuration.BlobOperationLimitCount, clock);
        }
예제 #5
0
 /// <summary>
 /// Gets the status of a redis instance.
 /// </summary>
 public static Task <Result <RedisInfoStats> > GetRedisInfoAsync(this RedisDatabaseAdapter adapter, OperationContext context, Tracer tracer, Counter counter, string serverId = null, bool trace = true)
 {
     return(context.PerformOperationAsync(
                tracer,
                operation: async() =>
     {
         var info = await adapter.GetInfoAsync(serverId);
         return new Result <RedisInfoStats>(new RedisInfoStats(info));
     },
                traceOperationStarted: false,
                extraEndMessage: (result) => result.Succeeded ? $"Redis info: {string.Join(Environment.NewLine, result.Value.Info.Select(t => $"ServerId={t.serverId}, Info={t.info.ToString()}"))}" : null,
                counter: counter));
 }
        /// <summary>
        /// Executes a batch with a set of operations defined by <paramref name="addOperations"/> callback.
        /// </summary>
        public static async Task <Result <T> > ExecuteBatchAsResultAsync <T>(this RedisDatabaseAdapter adapter, OperationContext context, Tracer tracer, Func <RedisBatch, Task <T> > addOperations, RedisOperation operation)
        {
            bool       isCancelled        = false;
            Result <T> executeBatchResult = await context.PerformOperationAsync(
                tracer,
                async() =>
            {
                var batch  = adapter.CreateBatchOperation(operation);
                var result = addOperations((RedisBatch)batch);

                var executeBatchOperationResult = await adapter.ExecuteBatchOperationAsync(context, batch, context.Token);

                // Tracking the cancellation of redis operation to return this aspect back to the caller.
                isCancelled = executeBatchOperationResult.IsCancelled;
                return(Result.Success(await result));
            },
                traceOperationStarted : false,
                traceOperationFinished : false);

            executeBatchResult.IsCancelled = isCancelled;
            return(executeBatchResult);
        }
예제 #7
0
            public bool IsTransient(Exception ex)
            {
                _exceptionObserver?.Invoke(ex);

                // naively retry all redis server exceptions.

                if (ex is RedisException redisException)
                {
                    if (RedisDatabaseAdapter.IsRedisUnableToConnectException(redisException))
                    {
                        return(false);
                    }

                    // If the error contains the following text, then the error is not transient.
                    return(!(redisException.ToString().Contains("Error compiling script") || redisException.ToString().Contains("Error running script")));
                }

                if (ex is RedisTimeoutException)
                {
                    return(true);
                }

                return(false);
            }