forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Bharathwaj G <[email protected]>
- Loading branch information
1 parent
20f5d1a
commit 2fc970d
Showing
12 changed files
with
447 additions
and
65 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
server/src/main/java/org/opensearch/common/util/DoubleMovingAverage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.common.util; | ||
|
||
/** | ||
* MovingAverage is used to calculate the moving average of last 'n' observations of double type. | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public class DoubleMovingAverage { | ||
private final int windowSize; | ||
private final double[] observations; | ||
|
||
private volatile long count = 0; | ||
private volatile long sum = 0; | ||
private volatile double average = 0; | ||
|
||
public DoubleMovingAverage(int windowSize) { | ||
checkWindowSize(windowSize); | ||
this.windowSize = windowSize; | ||
this.observations = new double[windowSize]; | ||
} | ||
|
||
/** | ||
* Used for changing the window size of {@code MovingAverage}. | ||
* | ||
* @param newWindowSize new window size. | ||
* @return copy of original object with updated size. | ||
*/ | ||
public DoubleMovingAverage copyWithSize(int newWindowSize) { | ||
DoubleMovingAverage copy = new DoubleMovingAverage(newWindowSize); | ||
// Start is inclusive, but end is exclusive | ||
long start, end = count; | ||
if (isReady() == false) { | ||
start = 0; | ||
} else { | ||
start = end - windowSize; | ||
} | ||
// If the newWindow Size is smaller than the elements eligible to be copied over, then we adjust the start value | ||
if (end - start > newWindowSize) { | ||
start = end - newWindowSize; | ||
} | ||
for (int i = (int) start; i < end; i++) { | ||
copy.record(observations[i % observations.length]); | ||
} | ||
return copy; | ||
} | ||
|
||
private void checkWindowSize(int size) { | ||
if (size <= 0) { | ||
throw new IllegalArgumentException("window size must be greater than zero"); | ||
} | ||
} | ||
|
||
/** | ||
* Records a new observation and evicts the n-th last observation. | ||
*/ | ||
public synchronized double record(double value) { | ||
double delta = value - observations[(int) (count % observations.length)]; | ||
observations[(int) (count % observations.length)] = value; | ||
|
||
count++; | ||
sum += delta; | ||
average = sum / Math.min(count, observations.length); | ||
return average; | ||
} | ||
|
||
public double getAverage() { | ||
return average; | ||
} | ||
|
||
public long getCount() { | ||
return count; | ||
} | ||
|
||
public boolean isReady() { | ||
return count >= windowSize; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
server/src/main/java/org/opensearch/throttling/tracker/AverageDiskStats.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.throttling.tracker; | ||
|
||
import org.opensearch.common.unit.TimeValue; | ||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.common.io.stream.StreamOutput; | ||
import org.opensearch.core.common.io.stream.Writeable; | ||
import org.opensearch.core.xcontent.ToXContent; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
|
||
import java.io.IOException; | ||
import java.util.Locale; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class AverageDiskStats implements Writeable { | ||
private final double readIopsAverage; | ||
private final double writeIopsAverage; | ||
private final double readKbAverage; | ||
private final double writeKbAverage; | ||
private final double readLatencyAverage; | ||
private final double writeLatencyAverage; | ||
private final double ioUtilizationPercent; | ||
|
||
public AverageDiskStats(double readIopsAverage, double writeIopsAverage, double readKbAverage, double writeKbAverage, | ||
double readLatencyAverage, double writeLatencyAverage, double ioUtilizationPercent) { | ||
this.readIopsAverage = readIopsAverage; | ||
this.writeIopsAverage = writeIopsAverage; | ||
this.readKbAverage = readKbAverage; | ||
this.writeKbAverage = writeKbAverage; | ||
this.readLatencyAverage = readLatencyAverage; | ||
this.writeLatencyAverage = writeLatencyAverage; | ||
this.ioUtilizationPercent = ioUtilizationPercent; | ||
} | ||
|
||
public AverageDiskStats(StreamInput in) throws IOException { | ||
this.readIopsAverage = in.readDouble(); | ||
this.readKbAverage = in.readDouble(); | ||
this.readLatencyAverage = in.readDouble(); | ||
this.writeIopsAverage = in.readDouble(); | ||
this.writeKbAverage = in.readDouble(); | ||
this.writeLatencyAverage = in.readDouble(); | ||
this.ioUtilizationPercent = in.readDouble(); | ||
} | ||
|
||
public double getIoUtilizationPercent() { | ||
return ioUtilizationPercent; | ||
} | ||
|
||
public double getReadIopsAverage() { | ||
return readIopsAverage; | ||
} | ||
|
||
public double getReadKbAverage() { | ||
return readKbAverage; | ||
} | ||
|
||
public double getReadLatencyAverage() { | ||
return readLatencyAverage; | ||
} | ||
|
||
public double getWriteIopsAverage() { | ||
return writeIopsAverage; | ||
} | ||
|
||
public double getWriteKbAverage() { | ||
return writeKbAverage; | ||
} | ||
|
||
public double getWriteLatencyAverage() { | ||
return writeLatencyAverage; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
|
||
} | ||
|
||
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { | ||
builder.startObject("io_stats"); | ||
builder.field("read_iops_average", String.format(Locale.ROOT, "%.1f", readIopsAverage )); | ||
builder.field("write_iops_average", String.format(Locale.ROOT, "%.1f", writeIopsAverage)); | ||
builder.field("read_throughput_average", String.format(Locale.ROOT, "%.1f", readKbAverage)); | ||
builder.field("write_throughput_average", String.format(Locale.ROOT, "%.1f", writeKbAverage)); | ||
builder.field("read_latency_average", String.format(Locale.ROOT, "%.8f", readLatencyAverage)); | ||
builder.field("write_latency_average", String.format(Locale.ROOT, "%.8f", writeLatencyAverage)); | ||
builder.field("io_utilization_percent", String.format(Locale.ROOT, "%.3f", ioUtilizationPercent)); | ||
builder.endObject(); | ||
return builder; | ||
} | ||
} |
Oops, something went wrong.