public static string FormatEssentials(CircuitBreakerState request) { string displayname; bool hasDisplayName = request.InformationalPropertyBag.TryGetValue("circuit.displayname", out displayname); return(string.Format("Circuit: {0} on {1}, pid: {2} status: {3}, msg: {4} from {5}.", request.CircuitBreakerId, request.MachineName, request.ProcessId, request.Status, request.MessageSequenceNumber, (hasDisplayName) ? displayname : "no name.")); }
private static CircuitBreakerState MapNotificationToCircuitBreakerState(NotifyOfCircuitStateRequest request) { CircuitBreakerState updateRequest = new CircuitBreakerState(request.CircuitBreakerId, request.MachineName, request.Pid, request.Status, request.RequestMessageSequenceNumber); updateRequest.UpdateablePropertyBag = new Dictionary <string, string>(request.UpdateablePropertyBag); updateRequest.InformationalPropertyBag = new Dictionary <string, string>(request.InformationalPropertyBag); return(updateRequest); }
public CircuitBreakerState Merge(CircuitBreakerState updateMessage, CircuitBreakerState currentState) { //skip old messages, in case any have arrived (or get processed) out of order. if (currentState.MessageSequenceNumber > updateMessage.MessageSequenceNumber) { return(currentState); } if (currentState.MessageSequenceNumber == updateMessage.MessageSequenceNumber) { return new CircuitBreakerState(currentState.CircuitBreakerId, currentState.MachineName, currentState.ProcessId) { MessageSequenceNumber = currentState.MessageSequenceNumber + 1000, Status = currentState.Status, LastReceivedStatus = updateMessage.Status, InformationalPropertyBag = updateMessage.InformationalPropertyBag, UpdateablePropertyBag = updateMessage.UpdateablePropertyBag } } ; var resultingState = new CircuitBreakerState(currentState.CircuitBreakerId, currentState.MachineName, currentState.ProcessId) { MessageSequenceNumber = updateMessage.MessageSequenceNumber, LastReceivedStatus = updateMessage.Status, InformationalPropertyBag = updateMessage.InformationalPropertyBag, UpdateablePropertyBag = updateMessage.UpdateablePropertyBag }; switch (currentState.Status) { case CircuitBreakerStatus.ForcedClosed: case CircuitBreakerStatus.ForcedOpen: resultingState.Status = currentState.Status; break; case CircuitBreakerStatus.ForcedHalfOpen: if ((updateMessage.Status == CircuitBreakerStatus.ForcedClosed) || (updateMessage.Status == CircuitBreakerStatus.ForcedOpen)) { resultingState.Status = CircuitBreakerStatus.ForcedHalfOpen; } else { resultingState.Status = updateMessage.Status; } break; default: resultingState.Status = updateMessage.Status; break; } return(resultingState); } }
private static NotifyOfCircuitStateResponse MapCircuitBreakerStateToResponse(CircuitBreakerState response) { return(new NotifyOfCircuitStateResponse { Id = response.CircuitBreakerId, Status = response.Status, MessageSequenceNumber = response.MessageSequenceNumber, UpdateablePropertyBag = response.UpdateablePropertyBag }); }
public CircuitBreakerState(CircuitBreakerState source) { CircuitBreakerId = source.CircuitBreakerId; MachineName = source.MachineName; ProcessId = source.ProcessId; MessageSequenceNumber = source.MessageSequenceNumber; Status = source.Status; LastReceivedStatus = source.LastReceivedStatus; LastModifiedDate = SystemTime.Now(); InformationalPropertyBag = new Dictionary <string, string>(); UpdateablePropertyBag = new Dictionary <string, string>(); }
public static void Log(ILog logger, CircuitBreakerState request, bool isNew) { string logEntry = LogRequestFormatter.FormatEssentials(request); string props = LogRequestFormatter.FormatProperties(request); string message = logEntry + " " + props; message = isNew ? "(NEW) " + message : message; if ((request.Status == CircuitBreakerStatus.Open) || (request.Status == CircuitBreakerStatus.ForcedOpen) || (request.Status == CircuitBreakerStatus.ForcedClosed) || (request.Status == CircuitBreakerStatus.ForcedHalfOpen)) { logger.Warn(message); } else { logger.Info(message); } }
/// <summary> /// Handles inbound messages that need to be 'merged' into the central state repository. Old messages will be skipped. /// </summary> /// <param name="statusUpdate"></param> /// <returns></returns> public CircuitBreakerState ProcessCircuitStateMessage(CircuitBreakerState statusUpdate) { if (statusUpdate == null) { throw new ArgumentNullException("statusUpdate"); } var circuitBreakerId = statusUpdate.CircuitBreakerId; CircuitBreakerState dashboardBreakerState; CircuitBreakerState resultingState; lock (_circuitBreakerCollectionMonitor) { bool circuitExists = _circuitBreakers.TryGetValue(circuitBreakerId, out dashboardBreakerState); resultingState = circuitExists ? _messageMergingLogic.Merge(statusUpdate, dashboardBreakerState) : new CircuitBreakerState(statusUpdate); _circuitBreakers[circuitBreakerId] = resultingState; } bool isNewCircuit = (dashboardBreakerState == null); if (isNewCircuit) { LogHelper.Log(_logStateChanges, statusUpdate, true); } else { if (dashboardBreakerState.Status != resultingState.Status) { LogHelper.Log(_logStateChanges, statusUpdate, false); } } LogHelper.Log(_logStatus, statusUpdate, false); return(resultingState); }
public static string FormatProperties(CircuitBreakerState request) { var pairs = new List <string>(); foreach (var props in request.InformationalPropertyBag) { if (props.Key == "circuit.displayname") { continue; } if (props.Key == "circuit.calldurationlistinms") { continue; } if ((props.Key == "circuit.timestamps") && (props.Value.Length == 0)) { continue; /// for diskspace reasons, we skip empty ones. } pairs.Add(props.Key.Replace("circuit.", "") + ":" + props.Value); } return(string.Join(", ", pairs)); }
private DashboardServiceCircuitStateResponse MapCircuitBreakerStateToDashboardServiceReponse(CircuitBreakerState circuit) { return(new DashboardServiceCircuitStateResponse { Id = circuit.CircuitBreakerId, LastModifiedDate = circuit.LastModifiedDate, LastReceivedStatus = circuit.LastReceivedStatus, MachineName = circuit.MachineName, MessageSequenceNumber = circuit.MessageSequenceNumber, ProcessId = circuit.ProcessId, Status = circuit.Status, UpdateablePropertyBag = circuit.UpdateablePropertyBag, ReadonlyPropertyBag = circuit.InformationalPropertyBag }); }