当前位置:网站首页>Circuitbreaker fuse of resilience4j -- Measurement of circuitbreakermetrics index
Circuitbreaker fuse of resilience4j -- Measurement of circuitbreakermetrics index
2022-06-12 10:10:00 【Wang Rushuang】
List of articles
Mentioned in the last article ,onError() or onSuccess() I will calculate the error rate and other indicators , Specifically by CircuitBreakerMetrics Realization 
One :CircuitBreaker.Metrics Indicator measurement interface
This interface defines a series of methods to obtain metrics , These include the following :
// Metrics interface , Used to observe various indexes of fuse
interface Metrics {
// Returns the failure rate percentage , If the number of calls is less than MINIMUM_NUMBER_OF_CALLS, return -1
float getFailureRate();
// Returns the percentage of slow calls , If the number of calls is less than MINIMUM_NUMBER_OF_CALLS, return -1
float getSlowCallRate();
// Total slow call requests
int getNumberOfSlowCalls();
// Total number of successful slow calls
int getNumberOfSlowSuccessfulCalls();
// Total slow call failures
int getNumberOfSlowFailedCalls();
// The maximum number of calls to the ring buffer
int getNumberOfBufferedCalls();
// Total number of failed calls
int getNumberOfFailedCalls();
// In the open state , Returns the number of currently disallowed request calls , In the closed and semi open states , Always return to 0
long getNumberOfNotPermittedCalls();
// Total number of successful calls
int getNumberOfSuccessfulCalls();
}
Two :CircuitBreakerMetrics class
yes CircuitBreaker.Metrics Implementation class of interface , The specific content is to realize CircuitBreaker.Metrics Method of definition
// Slow call rate
private float getSlowCallRate(Snapshot snapshot) {
int bufferedCalls = snapshot.getTotalNumberOfCalls();
if (bufferedCalls == 0 || bufferedCalls < minimumNumberOfCalls) {
return -1.0f;
}
return snapshot.getSlowCallRate();
}
// Failure rate
private float getFailureRate(Snapshot snapshot) {
// Total number of calls
int bufferedCalls = snapshot.getTotalNumberOfCalls();
if (bufferedCalls == 0 || bufferedCalls < minimumNumberOfCalls) {
//ringBitSet The number of middle is not up to ringBufferSize size , return -1.0f, Failure rate is not calculated
return -1.0f;
}
// Return failure rate :totalNumberOfFailedCalls * 100.0f / totalNumberOfCalls
return snapshot.getFailureRate();
}
onError() and onSuccess() The method is the state machine mentioned in the previous article CircuitBreakerStateMachine In the state class of onError() and onSuccess() Method called , Check if the error rate threshold has been reached checkIfThresholdsExceeded() Then call the state transition method of the state machine stateTransition()
/**
* Records a successful call and checks if the thresholds are exceeded.
* Check whether it is a slow call when the call is successful , If it is a slow call , Publish the slow call event first , Then count the slow call rate
* @return the result of the check
*/
public Result onSuccess(long duration, TimeUnit durationUnit) {
Snapshot snapshot;
if (durationUnit.toNanos(duration) > slowCallDurationThresholdInNanos) {
snapshot = metrics.record(duration, durationUnit, Outcome.SLOW_SUCCESS);
} else {
snapshot = metrics.record(duration, durationUnit, Outcome.SUCCESS);
}
return checkIfThresholdsExceeded(snapshot);
}
/**
* This method is called when it fails , Slow release / Error events , And check whether the threshold is reached
* 1. Record the request first , That is, the total number of requests , Error request times, etc +1
* 2. Calculate the failure rate / Slow call rate , Whether the threshold is reached
*/
public Result onError(long duration, TimeUnit durationUnit) {
Snapshot snapshot;
if (durationUnit.toNanos(duration) > slowCallDurationThresholdInNanos) {
// Slow call , call Core.Metrics Record the number of requests
snapshot = metrics.record(duration, durationUnit, Outcome.SLOW_ERROR);
} else {
// Error call , call Core.Metrics Record the number of requests
snapshot = metrics.record(duration, durationUnit, Outcome.ERROR);
}
return checkIfThresholdsExceeded(snapshot);
}
// Check the error rate / Whether the slow call rate reaches the threshold
private Result checkIfThresholdsExceeded(Snapshot snapshot) {
// Get the failure rate
float failureRateInPercentage = getFailureRate(snapshot);
if (failureRateInPercentage == -1) {
// Less than buffer size , The check threshold has not been reached
return Result.BELOW_MINIMUM_CALLS_THRESHOLD;
}
if (failureRateInPercentage >= failureRateThreshold) {
// Threshold exceeded
return Result.ABOVE_THRESHOLDS;
}
// Slow call rate
float slowCallsInPercentage = getSlowCallRate(snapshot);
if (slowCallsInPercentage >= slowCallRateThreshold) {
return Result.ABOVE_THRESHOLDS;
}
return Result.BELOW_THRESHOLDS;
}
It also defines enumeration values to check whether the threshold is reached :
enum Result {
BELOW_THRESHOLDS,
ABOVE_THRESHOLDS,
BELOW_MINIMUM_CALLS_THRESHOLD
}
summary
CircuitBreakerMetrics It is mainly used to calculate various indicators of measurement
边栏推荐
- Theoretical explanation of hash table
- Reading notes of the fifth cultivation
- First NFT platform in dfinity Ecology: impossible thoughts
- 【clickhouse专栏】基础数据类型说明
- 7-4 网红点打卡攻略(dfs)
- 《第五项修炼》读书笔记
- Create simple windowing programs using Visual Studio 2017
- 价值投资.
- Combat tactics based on CEPH object storage
- Dazzle the "library" action - award solicitation from the golden warehouse of the National People's Congress - high availability failover and recovery of kingbasees cluster
猜你喜欢

在线电路仿真以及开源电子硬件设计介绍

Introduction to IOT

markdown_图片并排的方案

Transport layer protocol -- TCP protocol

Chained hash table

001: what is a data lake?

Research on autojs wechat: the control IP in wechat on different versions of wechat or simulators is different.

Autojs learning notes 6:text (txt) Findone() will report an error when switching apps. Finally, solve the implementation effect and switch any app until the script finds the control with the specified

1268_ Implementation of FreeRTOS task context switching

【926. 将字符串翻转到单调递增】
随机推荐
High performance computing framework for image processing
[CEGUI] resource loading process
《第五项修炼》读书笔记
Data processing and visualization of machine learning [iris data classification | feature attribute comparison]
Implementation of fruit mall wholesale platform based on SSM
PostgreSQL uses stored procedures to splice multiple tables and query data
UE4_以现成资源探索创建背景场景的方法
Pandorabox uses firewall rules to define non internet time
[926. flip the string to monotonic increment]
Combat tactics based on CEPH object storage
Reading notes of the fifth cultivation
JVM (III) Virtual machine performance monitoring & fault handling tool
Basic use of scratch
[CEGUI] concept introduction
《要领》读书笔记
Auto. JS learning note 9: basic methods such as using the script engine, starting the script file with the specified path, and closing
Periodic pains of cross-border e-commerce? Papaya mobile power as an independent station enabler
Shen Min, CIO of science and technology innovator Digital China Group: the best practice model is failing, and open source accelerates Distributed Innovation
Transport layer protocol -- TCP protocol
Strange error -- frame detected by contour detection, expansion corrosion, and reversal of opening and closing operation effect