diff --git a/sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/flow/statistic/metric/ClusterMetric.java b/sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/flow/statistic/metric/ClusterMetric.java index 1dbdae21bc..cfe6303e2d 100644 --- a/sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/flow/statistic/metric/ClusterMetric.java +++ b/sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/flow/statistic/metric/ClusterMetric.java @@ -83,7 +83,7 @@ public int tryOccupyNext(ClusterFlowEvent event, int acquireCount, double thresh } metric.addOccupyPass(acquireCount); add(ClusterFlowEvent.WAITING, acquireCount); - return 1000 / metric.getSampleCount(); + return metric.getWindowLengthInMs(); } private boolean canOccupy(ClusterFlowEvent event, int acquireCount, double latestQps, double threshold) { diff --git a/sentinel-cluster/sentinel-cluster-server-default/src/test/java/com/alibaba/csp/sentinel/cluster/flow/statistic/metric/ClusterMetricTest.java b/sentinel-cluster/sentinel-cluster-server-default/src/test/java/com/alibaba/csp/sentinel/cluster/flow/statistic/metric/ClusterMetricTest.java index b7891767f8..7d2aebd62f 100644 --- a/sentinel-cluster/sentinel-cluster-server-default/src/test/java/com/alibaba/csp/sentinel/cluster/flow/statistic/metric/ClusterMetricTest.java +++ b/sentinel-cluster/sentinel-cluster-server-default/src/test/java/com/alibaba/csp/sentinel/cluster/flow/statistic/metric/ClusterMetricTest.java @@ -36,11 +36,11 @@ public void testTryOccupyNext() { Assert.assertEquals(4, metric.getSum(ClusterFlowEvent.PASS)); Assert.assertEquals(1, metric.getSum(ClusterFlowEvent.BLOCK)); Assert.assertEquals(160, metric.getAvg(ClusterFlowEvent.PASS), 0.01); - Assert.assertEquals(200, metric.tryOccupyNext(ClusterFlowEvent.PASS, 111, 900)); + Assert.assertEquals(5, metric.tryOccupyNext(ClusterFlowEvent.PASS, 111, 900)); metric.add(ClusterFlowEvent.PASS, 1); metric.add(ClusterFlowEvent.PASS, 2); metric.add(ClusterFlowEvent.PASS, 1); - Assert.assertEquals(200, metric.tryOccupyNext(ClusterFlowEvent.PASS, 222, 900)); + Assert.assertEquals(5, metric.tryOccupyNext(ClusterFlowEvent.PASS, 222, 900)); metric.add(ClusterFlowEvent.PASS, 1); metric.add(ClusterFlowEvent.PASS, 2); metric.add(ClusterFlowEvent.PASS, 1); diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/base/LeapArray.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/base/LeapArray.java index e67b40e99a..4a8563ff3e 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/base/LeapArray.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/base/LeapArray.java @@ -380,6 +380,14 @@ public int getSampleCount() { return sampleCount; } + /** + * Get window length of a single bucket in milliseconds. + * @return window length in milliseconds + */ + public int getWindowLengthInMs() { + return windowLengthInMs; + } + /** * Get total interval length of the sliding window in milliseconds. *