Optimize TimeSlot fields
This commit is contained in:
@@ -175,7 +175,7 @@ public class FlowStat {
|
||||
|
||||
// check QPS
|
||||
if (maxQPS > 0) {
|
||||
long total = resourceStat.getTimeSlot(curTimeSlotId).getCounter().get();
|
||||
long total = resourceStat.getTimeSlot(curTimeSlotId).getCounter();
|
||||
if (total >= maxQPS) {
|
||||
resourceStat.incrBlockRequestToTimeSlot(curTimeSlotId);
|
||||
if (totalBlockFunc != null) {
|
||||
@@ -242,7 +242,7 @@ public class FlowStat {
|
||||
|
||||
// check QPS
|
||||
if (maxQPS > 0) {
|
||||
long total = resourceStat.getTimeSlot(curTimeSlotId).getCounter().get();
|
||||
long total = resourceStat.getTimeSlot(curTimeSlotId).getCounter();
|
||||
if (total >= maxQPS) {
|
||||
resourceStat.incrBlockRequestToTimeSlot(curTimeSlotId);
|
||||
if (totalBlockFunc != null) {
|
||||
|
||||
@@ -127,7 +127,8 @@ public class ResourceStat {
|
||||
*
|
||||
*/
|
||||
public void incrBlockRequestToTimeSlot(long timeSlotId) {
|
||||
this.getTimeSlot(timeSlotId).getBlockRequests().incrementAndGet();
|
||||
// this.getTimeSlot(timeSlotId).getBlockRequests().incrementAndGet();
|
||||
this.getTimeSlot(timeSlotId).incrBlockRequests();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -135,7 +136,8 @@ public class ResourceStat {
|
||||
*
|
||||
*/
|
||||
public void incrTotalBlockRequestToTimeSlot(long timeSlotId) {
|
||||
this.getTimeSlot(timeSlotId).getTotalBlockRequests().incrementAndGet();
|
||||
// this.getTimeSlot(timeSlotId).getTotalBlockRequests().incrementAndGet();
|
||||
this.getTimeSlot(timeSlotId).incrTotalBlockRequests();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -156,7 +158,7 @@ public class ResourceStat {
|
||||
// }
|
||||
|
||||
// time slot unit is one second
|
||||
long total = this.getTimeSlot(timeSlotId).getCounter().get();
|
||||
long total = this.getTimeSlot(timeSlotId).getCounter();
|
||||
long max = Long.valueOf(maxRPS);
|
||||
if (total >= max) {
|
||||
isExceeded = true;
|
||||
@@ -179,28 +181,28 @@ public class ResourceStat {
|
||||
}
|
||||
|
||||
public void incrCircuitBreakNum(long timeSlot) {
|
||||
getTimeSlot(timeSlot).getCircuitBreakNum().incrementAndGet();
|
||||
getTimeSlot(timeSlot).incrCircuitBreakNum();
|
||||
}
|
||||
|
||||
public void decrCircuitBreakNum(long timeSlot) {
|
||||
getTimeSlot(timeSlot).getCircuitBreakNum().decrementAndGet();
|
||||
}
|
||||
|
||||
public void incrGradualResumeNum(long timeSlot) {
|
||||
getTimeSlot(timeSlot).getGradualResumeNum().incrementAndGet();
|
||||
}
|
||||
|
||||
public void decrGradualResumeNum(long timeSlot) {
|
||||
getTimeSlot(timeSlot).getGradualResumeNum().decrementAndGet();
|
||||
}
|
||||
|
||||
public void incrGradualRejectNum(long timeSlot) {
|
||||
getTimeSlot(timeSlot).getGradualRejectNum().incrementAndGet();
|
||||
}
|
||||
|
||||
public void decrGradualRejectNum(long timeSlot) {
|
||||
getTimeSlot(timeSlot).getGradualRejectNum().decrementAndGet();
|
||||
}
|
||||
// public void decrCircuitBreakNum(long timeSlot) {
|
||||
// getTimeSlot(timeSlot).getCircuitBreakNum().decrementAndGet();
|
||||
// }
|
||||
//
|
||||
// public void incrGradualResumeNum(long timeSlot) {
|
||||
// getTimeSlot(timeSlot).getGradualResumeNum().incrementAndGet();
|
||||
// }
|
||||
//
|
||||
// public void decrGradualResumeNum(long timeSlot) {
|
||||
// getTimeSlot(timeSlot).getGradualResumeNum().decrementAndGet();
|
||||
// }
|
||||
//
|
||||
// public void incrGradualRejectNum(long timeSlot) {
|
||||
// getTimeSlot(timeSlot).getGradualRejectNum().incrementAndGet();
|
||||
// }
|
||||
//
|
||||
// public void decrGradualRejectNum(long timeSlot) {
|
||||
// getTimeSlot(timeSlot).getGradualRejectNum().decrementAndGet();
|
||||
// }
|
||||
|
||||
public void incr2xxStatusCount(long timeSlot) {
|
||||
getTimeSlot(timeSlot).get2xxStatusCount().incrementAndGet();
|
||||
@@ -267,13 +269,13 @@ public class ResourceStat {
|
||||
peakConcurrences = timeSlot.getPeakConcurrentRequests() > peakConcurrences
|
||||
? timeSlot.getPeakConcurrentRequests()
|
||||
: peakConcurrences;
|
||||
peakRps = timeSlot.getCounter().get() > peakRps ? timeSlot.getCounter().get() : peakRps;
|
||||
totalReqs = totalReqs + timeSlot.getCounter().get();
|
||||
totalRt = totalRt + timeSlot.getTotalRt().get();
|
||||
errors = errors + timeSlot.getErrors().get();
|
||||
blockReqs = blockReqs + timeSlot.getBlockRequests().get();
|
||||
totalBlockReqs = totalBlockReqs + timeSlot.getTotalBlockRequests().get();
|
||||
compReqs = compReqs + timeSlot.getCompReqs().get();
|
||||
peakRps = timeSlot.getCounter() > peakRps ? timeSlot.getCounter() : peakRps;
|
||||
totalReqs = totalReqs + timeSlot.getCounter();
|
||||
totalRt = totalRt + timeSlot.getTotalRt();
|
||||
errors = errors + timeSlot.getErrors();
|
||||
blockReqs = blockReqs + timeSlot.getBlockRequests();
|
||||
totalBlockReqs = totalBlockReqs + timeSlot.getTotalBlockRequests();
|
||||
compReqs = compReqs + timeSlot.getCompReqs();
|
||||
|
||||
_2xxStatus = _2xxStatus + timeSlot.get2xxStatusCount().get();
|
||||
_4xxStatus = _4xxStatus + timeSlot.get4xxStatusCount().get();
|
||||
|
||||
@@ -37,12 +37,14 @@ public class TimeSlot {
|
||||
/**
|
||||
* Request counter
|
||||
*/
|
||||
private AtomicLong counter = new AtomicLong();
|
||||
// private AtomicLong counter = new AtomicLong();
|
||||
private volatile int counter = 0;
|
||||
|
||||
/**
|
||||
* Error request counter
|
||||
*/
|
||||
private AtomicLong errors = new AtomicLong();
|
||||
// private AtomicLong errors = new AtomicLong();
|
||||
private volatile int errors = 0;
|
||||
|
||||
/**
|
||||
* Minimum response time
|
||||
@@ -57,12 +59,14 @@ public class TimeSlot {
|
||||
/**
|
||||
* Total response time
|
||||
*/
|
||||
private AtomicLong totalRt = new AtomicLong(0);
|
||||
// private AtomicLong totalRt = new AtomicLong(0);
|
||||
private volatile long totalRt = 0;
|
||||
|
||||
/**
|
||||
* Completed Request counter
|
||||
*/
|
||||
private AtomicLong compReqs = new AtomicLong();
|
||||
// private AtomicLong compReqs = new AtomicLong();
|
||||
private volatile int compReqs = 0;
|
||||
|
||||
|
||||
/**
|
||||
@@ -73,23 +77,28 @@ public class TimeSlot {
|
||||
/**
|
||||
* Block requests <br/>
|
||||
*/
|
||||
private AtomicLong blockRequests = new AtomicLong(0);
|
||||
// private AtomicLong blockRequests = new AtomicLong(0);
|
||||
private volatile int blockRequests = 0;
|
||||
|
||||
/**
|
||||
* Total block requests of the resource and its underlying resources <br/>
|
||||
*/
|
||||
private AtomicLong totalBlockRequests = new AtomicLong(0);
|
||||
|
||||
// private AtomicLong totalBlockRequests = new AtomicLong(0);
|
||||
private volatile long totalBlockRequests = 0;
|
||||
|
||||
private AtomicReference<CircuitBreaker.State> circuitBreakState = new AtomicReference<>(CircuitBreaker.State.CLOSED);
|
||||
|
||||
private AtomicLong circuitBreakNum = new AtomicLong(0);
|
||||
// private AtomicLong circuitBreakNum = new AtomicLong(0);
|
||||
private volatile int circuitBreakNum = 0;
|
||||
|
||||
private AtomicLong gradualResumeNum = new AtomicLong(0);
|
||||
// private AtomicLong gradualResumeNum = new AtomicLong(0);
|
||||
private volatile int gradualResumeNum = 0;
|
||||
|
||||
private AtomicInteger resumeTrafficFactor = new AtomicInteger(1);
|
||||
// private AtomicInteger resumeTrafficFactor = new AtomicInteger(1);
|
||||
private volatile int resumeTrafficFactor = 1;
|
||||
|
||||
private AtomicLong gradualRejectNum = new AtomicLong(0);
|
||||
// private AtomicLong gradualRejectNum = new AtomicLong(0);
|
||||
private volatile int gradualRejectNum = 0;
|
||||
|
||||
private AtomicInteger _2xxStatusCount = new AtomicInteger(0);
|
||||
|
||||
@@ -119,22 +128,50 @@ public class TimeSlot {
|
||||
return circuitBreakState;
|
||||
}
|
||||
|
||||
public AtomicLong getCircuitBreakNum() {
|
||||
public int getCircuitBreakNum() {
|
||||
return circuitBreakNum;
|
||||
}
|
||||
|
||||
public AtomicLong getGradualResumeNum() {
|
||||
public void setCircuitBreakNum(int v) {
|
||||
circuitBreakNum = v;
|
||||
}
|
||||
|
||||
public void incrCircuitBreakNum() {
|
||||
++circuitBreakNum;
|
||||
}
|
||||
|
||||
public int getGradualResumeNum() {
|
||||
return gradualResumeNum;
|
||||
}
|
||||
|
||||
public AtomicInteger getResumeTrafficFactor() {
|
||||
public int incrGradualResumeNum() {
|
||||
return ++gradualResumeNum;
|
||||
}
|
||||
|
||||
public int decrGradualResumeNum() {
|
||||
return --gradualResumeNum;
|
||||
}
|
||||
|
||||
public int getResumeTrafficFactor() {
|
||||
return resumeTrafficFactor;
|
||||
}
|
||||
|
||||
public AtomicLong getGradualRejectNum() {
|
||||
public void incrResumeTrafficFactor() {
|
||||
++resumeTrafficFactor;
|
||||
}
|
||||
|
||||
public int getGradualRejectNum() {
|
||||
return gradualRejectNum;
|
||||
}
|
||||
|
||||
public int incrGradualRejectNum() {
|
||||
return ++gradualRejectNum;
|
||||
}
|
||||
|
||||
public int decrGradualRejectNum() {
|
||||
return --gradualRejectNum;
|
||||
}
|
||||
|
||||
|
||||
public TimeSlot(long id) {
|
||||
this.id = id;
|
||||
@@ -149,7 +186,8 @@ public class TimeSlot {
|
||||
*
|
||||
*/
|
||||
public void incr() {
|
||||
counter.incrementAndGet();
|
||||
// counter.incrementAndGet();
|
||||
++counter;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -159,10 +197,10 @@ public class TimeSlot {
|
||||
* @param isSuccess Whether the request is success or not
|
||||
*/
|
||||
public synchronized void addRequestRT(long rt, boolean isSuccess) {
|
||||
totalRt.addAndGet(rt);
|
||||
compReqs.incrementAndGet();
|
||||
totalRt += rt;
|
||||
++compReqs;
|
||||
if (!isSuccess) {
|
||||
errors.incrementAndGet();
|
||||
++errors;
|
||||
}
|
||||
min = rt < min ? rt : min;
|
||||
max = rt > max ? rt : max;
|
||||
@@ -182,11 +220,11 @@ public class TimeSlot {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public AtomicLong getCounter() {
|
||||
public int getCounter() {
|
||||
return counter;
|
||||
}
|
||||
|
||||
public void setCounter(AtomicLong counter) {
|
||||
public void setCounter(int counter) {
|
||||
this.counter = counter;
|
||||
}
|
||||
|
||||
@@ -206,11 +244,11 @@ public class TimeSlot {
|
||||
this.max = max;
|
||||
}
|
||||
|
||||
public AtomicLong getTotalRt() {
|
||||
public long getTotalRt() {
|
||||
return totalRt;
|
||||
}
|
||||
|
||||
public void setTotalRt(AtomicLong totalRt) {
|
||||
public void setTotalRt(long totalRt) {
|
||||
this.totalRt = totalRt;
|
||||
}
|
||||
|
||||
@@ -222,35 +260,43 @@ public class TimeSlot {
|
||||
this.peakConcurrentRequests = peakConcurrentRequests;
|
||||
}
|
||||
|
||||
public AtomicLong getErrors() {
|
||||
public int getErrors() {
|
||||
return errors;
|
||||
}
|
||||
|
||||
public void setErrors(AtomicLong errors) {
|
||||
public void setErrors(int errors) {
|
||||
this.errors = errors;
|
||||
}
|
||||
|
||||
public AtomicLong getBlockRequests() {
|
||||
public int getBlockRequests() {
|
||||
return blockRequests;
|
||||
}
|
||||
|
||||
public void setBlockRequests(AtomicLong blockRequests) {
|
||||
public void setBlockRequests(int blockRequests) {
|
||||
this.blockRequests = blockRequests;
|
||||
}
|
||||
|
||||
public AtomicLong getCompReqs() {
|
||||
public void incrBlockRequests() {
|
||||
++blockRequests;
|
||||
}
|
||||
|
||||
public int getCompReqs() {
|
||||
return compReqs;
|
||||
}
|
||||
|
||||
public void setCompReqs(AtomicLong compReqs) {
|
||||
public void setCompReqs(int compReqs) {
|
||||
this.compReqs = compReqs;
|
||||
}
|
||||
|
||||
public AtomicLong getTotalBlockRequests() {
|
||||
public long getTotalBlockRequests() {
|
||||
return totalBlockRequests;
|
||||
}
|
||||
|
||||
public void setTotalBlockRequests(AtomicLong totalBlockRequests) {
|
||||
public void incrTotalBlockRequests() {
|
||||
++totalBlockRequests;
|
||||
}
|
||||
|
||||
public void setTotalBlockRequests(long totalBlockRequests) {
|
||||
this.totalBlockRequests = totalBlockRequests;
|
||||
}
|
||||
|
||||
|
||||
@@ -264,7 +264,7 @@ public class CircuitBreakManager {
|
||||
}
|
||||
}*/
|
||||
|
||||
public void correctCircuitBreakerStateAsError(ServerWebExchange exchange, long currentTimeWindow, FlowStat flowStat, String service, String path) {
|
||||
/*public void correctCircuitBreakerStateAsError(ServerWebExchange exchange, long currentTimeWindow, FlowStat flowStat, String service, String path) {
|
||||
String resource = ResourceIdUtils.buildResourceId(null, null, null, service, path);
|
||||
// correctCircuitBreakerState4error(exchange, currentTimeWindow, flowStat, resource);
|
||||
CircuitBreaker cb = resource2circuitBreakerMap.get(resource);
|
||||
@@ -290,7 +290,7 @@ public class CircuitBreakManager {
|
||||
}
|
||||
cb.correctCircuitBreakerStateAsError(currentTimeWindow, flowStat);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
/*public void correctCircuitBreakerState4error(ServerWebExchange exchange, long currentTimeWindow, FlowStat flowStat, String resource) {
|
||||
while (true) {
|
||||
|
||||
@@ -77,25 +77,31 @@ public class CircuitBreaker {
|
||||
public boolean permit(ResourceStat resourceStat, long currentTimeWindow) {
|
||||
|
||||
TimeSlot timeSlot = resourceStat.getTimeSlot(currentTimeWindow);
|
||||
AtomicLong resumeCount = timeSlot.getGradualResumeNum();
|
||||
AtomicInteger resumeTrafficFactor = timeSlot.getResumeTrafficFactor();
|
||||
long n = resumeTrafficFactor.get();
|
||||
if (resumeCount.incrementAndGet() <= resumeTraffic * n) {
|
||||
// AtomicLong resumeCount = timeSlot.getGradualResumeNum();
|
||||
// AtomicInteger resumeTrafficFactor = timeSlot.getResumeTrafficFactor();
|
||||
// long n = resumeTrafficFactor.get();
|
||||
long n = timeSlot.getResumeTrafficFactor();
|
||||
// if (resumeCount.incrementAndGet() <= resumeTraffic * n) {
|
||||
if (timeSlot.incrGradualResumeNum() <= resumeTraffic * n) {
|
||||
LOGGER.debug("{} current time window {}, resume traffic {}, resume traffic factor {}, resume count {}, resume current request",
|
||||
resourceStat.getResourceId(), currentTimeWindow, resumeTraffic, n, resumeCount.get());
|
||||
resourceStat.getResourceId(), currentTimeWindow, resumeTraffic, n, timeSlot.getGradualResumeNum());
|
||||
return true;
|
||||
}
|
||||
AtomicLong rejectCount = timeSlot.getGradualRejectNum();
|
||||
if (rejectCount.incrementAndGet() <= rejectTraffic * n) {
|
||||
resumeCount.decrementAndGet();
|
||||
// AtomicLong rejectCount = timeSlot.getGradualRejectNum();
|
||||
// if (rejectCount.incrementAndGet() <= rejectTraffic * n) {
|
||||
if (timeSlot.incrGradualRejectNum() <= rejectTraffic * n) {
|
||||
// resumeCount.decrementAndGet();
|
||||
timeSlot.decrGradualResumeNum();
|
||||
LOGGER.debug("{} current time window {}, reject traffic {}, resume traffic factor {}, reject count {}, reject current request",
|
||||
resourceStat.getResourceId(), currentTimeWindow, rejectTraffic, n, rejectCount.get());
|
||||
resourceStat.getResourceId(), currentTimeWindow, rejectTraffic, n, timeSlot.getGradualRejectNum());
|
||||
return false;
|
||||
}
|
||||
rejectCount.decrementAndGet();
|
||||
resumeTrafficFactor.incrementAndGet();
|
||||
// rejectCount.decrementAndGet();
|
||||
timeSlot.decrGradualRejectNum();
|
||||
// resumeTrafficFactor.incrementAndGet();
|
||||
timeSlot.incrResumeTrafficFactor();
|
||||
LOGGER.debug("{} current time window {}, resume traffic {}, reject traffic {}, resume traffic factor {}, resume count {}, reject count {}, resume current request",
|
||||
resourceStat.getResourceId(), currentTimeWindow, resumeTraffic, rejectTraffic, n, resumeCount.get(), rejectCount.get());
|
||||
resourceStat.getResourceId(), currentTimeWindow, resumeTraffic, rejectTraffic, n, timeSlot.getGradualResumeNum(), timeSlot.getGradualRejectNum());
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -286,7 +292,7 @@ public class CircuitBreaker {
|
||||
}
|
||||
}
|
||||
|
||||
public void correctCircuitBreakerStateAsError(long currentTimeWindow, FlowStat flowStat) {
|
||||
/*public void correctCircuitBreakerStateAsError(long currentTimeWindow, FlowStat flowStat) {
|
||||
if (stateRef.get() == State.CLOSED) {
|
||||
long endTimeWindow = currentTimeWindow + 1000;
|
||||
// TimeWindowStat timeWindowStat = flowStat.getTimeWindowStat(resource, endTimeWindow - monitorDuration, endTimeWindow);
|
||||
@@ -309,14 +315,15 @@ public class CircuitBreaker {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
public boolean transit(State current, State target, long currentTimeWindow, FlowStat flowStat) {
|
||||
if (stateRef.compareAndSet(current, target)) {
|
||||
stateStartTime = currentTimeWindow;
|
||||
ResourceStat resourceStat = flowStat.getResourceStat(resource);
|
||||
AtomicLong circuitBreakNum = resourceStat.getTimeSlot(currentTimeWindow).getCircuitBreakNum();
|
||||
circuitBreakNum.set(0);
|
||||
/*AtomicLong circuitBreakNum = resourceStat.getTimeSlot(currentTimeWindow).getCircuitBreakNum();
|
||||
circuitBreakNum.set(0);*/
|
||||
resourceStat.getTimeSlot(currentTimeWindow).setCircuitBreakNum(0);
|
||||
resourceStat.updateCircuitBreakState(currentTimeWindow, current, target);
|
||||
LOGGER.debug("transit {} current time window {} from {} which start at {} to {}", resource, currentTimeWindow, current, stateStartTime, target);
|
||||
return true;
|
||||
|
||||
@@ -92,8 +92,8 @@ public class CircuitBreakManagerTests {
|
||||
|
||||
ResourceStat resourceStat = flowStat.getResourceStat(cb.resource);
|
||||
TimeSlot timeSlot = resourceStat.getTimeSlot(currentTimeWindow);
|
||||
timeSlot.getCompReqs().set(200);
|
||||
timeSlot.getErrors().set(11);
|
||||
timeSlot.setCompReqs(200);
|
||||
timeSlot.setErrors(11);
|
||||
|
||||
boolean permit = circuitBreakManager.permit(mockServerWebExchange, currentTimeWindow, flowStat, service, path);
|
||||
Assertions.assertFalse(permit);
|
||||
@@ -101,6 +101,6 @@ public class CircuitBreakManagerTests {
|
||||
permit = circuitBreakManager.permit(mockServerWebExchange, currentTimeWindow, flowStat, service, path);
|
||||
Assertions.assertFalse(permit);
|
||||
Assertions.assertEquals(CircuitBreaker.State.OPEN, timeSlot.getCircuitBreakState().get());
|
||||
Assertions.assertEquals(2, timeSlot.getCircuitBreakNum().get());
|
||||
Assertions.assertEquals(2, timeSlot.getCircuitBreakNum());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user