Beispiel #1
0
 private T ExecuteFallback()
 {
     if (FallbackExecutionSemaphore.TryAcquire())
     {
         try
         {
             var rtn = ToIFallback().GetFallback();
             Metrics.MarkExecutionEvent(CommandExecutionEventEnum.FallbackSuccess);
             Status = CommandStatusEnum.FallbackSuccess;
             Log.Log(LogLevelEnum.Warning, "HystrixCommand execution failed, use fallback instead.");
             return(rtn);
         }
         catch (Exception ex)
         {
             Log.Log(LogLevelEnum.Error, "HystrixCommand fallback execution failed.", ex,
                     GetLogTagInfo().AddLogTagData("FXD303038"));
             Metrics.MarkExecutionEvent(CommandExecutionEventEnum.FallbackFailed);
             Status = CommandStatusEnum.FallbackFailed;
             throw ex;
         }
         finally
         {
             FallbackExecutionSemaphore.Release();
         }
     }
     else
     {
         var fRejectMsg = "HystrixCommand fallback execution was rejected.";
         Log.Log(LogLevelEnum.Error, fRejectMsg, GetLogTagInfo().AddLogTagData("FXD303039"));
         Metrics.MarkExecutionEvent(CommandExecutionEventEnum.FallbackRejected);
         Status = CommandStatusEnum.FallbackRejected;
         throw new HystrixException(FailureTypeEnum.FallbackRejected, this.GetType(), Key, fRejectMsg);
     }
 }
Beispiel #2
0
        private T ExecuteFallback(Exception cause)
        {
            if (cause is HystrixException && cause.InnerException != null)
            {
                cause = cause.InnerException;
            }

            if (FallbackExecutionSemaphore.TryAcquire())
            {
                try
                {
                    T result = ToIFallback().GetFallback();
                    Metrics.MarkExecutionEvent(CommandExecutionEventEnum.FallbackSuccess);
                    Status = CommandStatusEnum.FallbackSuccess;
                    Log.Log(LogLevelEnum.Warning, "HystrixCommand execution failed, use fallback instead.");
                    return(result);
                }
                catch (Exception ex)
                {
                    Metrics.MarkExecutionEvent(CommandExecutionEventEnum.FallbackFailed);
                    Status = CommandStatusEnum.FallbackFailed;
                    if (ConfigSet.LogExecutionError)
                    {
                        Log.Log(LogLevelEnum.Error, "HystrixCommand fallback execution failed.", ex, GetLogTagInfo().AddLogTagData("FXD303024"));
                    }
                    throw;
                }
                finally
                {
                    FallbackExecutionSemaphore.Release();
                }
            }
            else
            {
                Status = CommandStatusEnum.FallbackRejected;
                Metrics.MarkExecutionEvent(CommandExecutionEventEnum.FallbackRejected);
                string errorMessage = "HystrixCommand fallback execution was rejected.";
                Log.Log(LogLevelEnum.Error, errorMessage, GetLogTagInfo().AddLogTagData("FXD303025"));
                throw new HystrixException(FailureTypeEnum.FallbackRejected, GetType(), Key, errorMessage, cause);
            }
        }
Beispiel #3
0
        private T ExecuteWithSemaphoreIsolation()
        {
            if (!CircuitBreaker.AllowRequest())
            {
                string errorMessage = "Circuit Breaker is open. Execution was short circuited.";
                Log.Log(LogLevelEnum.Error, errorMessage, GetLogTagInfo().AddLogTagData("FXD303019"));
                Status = CommandStatusEnum.ShortCircuited;
                Metrics.MarkExecutionEvent(CommandExecutionEventEnum.ShortCircuited);
                throw new HystrixException(FailureTypeEnum.ShortCircuited, this.GetType(), Key, errorMessage);
            }

            if (ExecutionSemaphore.TryAcquire())
            {
                Stopwatch stopwatch = new Stopwatch();
                try
                {
                    stopwatch.Start();
                    Status = CommandStatusEnum.Started;
                    T result = Execute();
                    stopwatch.Stop();
                    if (stopwatch.ElapsedMilliseconds > ConfigSet.CommandTimeoutInMilliseconds)
                    {
                        Status = CommandStatusEnum.Timeout;
                        Metrics.MarkExecutionEvent(CommandExecutionEventEnum.Timeout);
                        Log.Log(LogLevelEnum.Warning, string.Format("HystrixCommand execution timeout: {0}ms.", stopwatch.ElapsedMilliseconds),
                                GetLogTagInfo().AddLogTagData("FXD303020"));
                    }
                    else
                    {
                        Status = CommandStatusEnum.Success;
                        Metrics.MarkExecutionEvent(CommandExecutionEventEnum.Success);
                        CircuitBreaker.MarkSuccess();
                    }
                    return(result);
                }
                catch (Exception ex)
                {
                    if (ex.IsBadRequestException())
                    {
                        Status = CommandStatusEnum.Failed;
                        Metrics.MarkExecutionEvent(CommandExecutionEventEnum.BadRequest);
                        if (ConfigSet.LogExecutionError)
                        {
                            Log.Log(LogLevelEnum.Error, "HystrixCommand request is bad.", ex, GetLogTagInfo().AddLogTagData("FXD303021"));
                        }
                        throw;
                    }

                    Status = CommandStatusEnum.Failed;
                    Metrics.MarkExecutionEvent(CommandExecutionEventEnum.Failed);
                    if (ConfigSet.LogExecutionError)
                    {
                        Log.Log(LogLevelEnum.Error, "HystrixCommand execution failed.", ex, GetLogTagInfo().AddLogTagData("FXD303022"));
                    }
                    throw;
                }
                finally
                {
                    ExecutionSemaphore.Release();

                    stopwatch.Stop();
                    Metrics.MarkExecutionLatency(stopwatch.ElapsedMilliseconds);
                }
            }
            else
            {
                Status = CommandStatusEnum.Rejected;
                Metrics.MarkExecutionEvent(CommandExecutionEventEnum.Rejected);
                string errorMessage = "HystrixCommand execution was rejected.";
                Log.Log(LogLevelEnum.Error, errorMessage, GetLogTagInfo().AddLogTagData("FXD303023"));
                throw new HystrixException(FailureTypeEnum.SemaphoreIsolationRejected, GetType(), Key, errorMessage);
            }
        }