-
Notifications
You must be signed in to change notification settings - Fork 25k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Track EWMA[1] of task execution time in search threadpool executor
This is the first step towards adaptive replica selection (#24915). This PR tracks the execution time, also known as the "service time" of a task in the threadpool. The `QueueResizingEsThreadPoolExecutor` then stores a moving average of these task times which can be retrieved from the executor. Currently there is no functionality using the EWMA yet (other than tests), this is only a bite-sized building block so that it's easier to review. [1]: EWMA = Exponentially Weighted Moving Average
- Loading branch information
Showing
5 changed files
with
219 additions
and
9 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
core/src/main/java/org/elasticsearch/common/ExponentiallyWeightedMovingAverage.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,61 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.common; | ||
|
||
|
||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
/** | ||
* Implements exponentially weighted moving averages (commonly abbreviated EWMA) for a single value. | ||
* This class is safe to share between threads. | ||
*/ | ||
public class ExponentiallyWeightedMovingAverage { | ||
|
||
private final double alpha; | ||
private final AtomicLong averageBits; | ||
|
||
/** | ||
* Create a new EWMA with a given {@code alpha} and {@code initialAvg}. A smaller alpha means | ||
* that new data points will have less weight, where a high alpha means older data points will | ||
* have a lower influence. | ||
*/ | ||
public ExponentiallyWeightedMovingAverage(double alpha, double initialAvg) { | ||
if (alpha < 0 || alpha > 1) { | ||
throw new IllegalArgumentException("alpha must be greater or equal to 0 and less than or equal to 1"); | ||
} | ||
this.alpha = alpha; | ||
this.averageBits = new AtomicLong(Double.doubleToLongBits(initialAvg)); | ||
} | ||
|
||
public double getAverage() { | ||
return Double.longBitsToDouble(this.averageBits.get()); | ||
} | ||
|
||
public void addValue(double newValue) { | ||
boolean successful = false; | ||
do { | ||
final long currentBits = this.averageBits.get(); | ||
final double currentAvg = getAverage(); | ||
final double newAvg = (alpha * newValue) + ((1 - alpha) * currentAvg); | ||
final long newBits = Double.doubleToLongBits(newAvg); | ||
successful = averageBits.compareAndSet(currentBits, newBits); | ||
} while (successful == false); | ||
} | ||
} |
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
66 changes: 66 additions & 0 deletions
66
core/src/test/java/org/elasticsearch/common/ExponentiallyWeightedMovingAverageTests.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,66 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.common; | ||
|
||
import org.elasticsearch.test.ESTestCase; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.lessThan; | ||
import static org.junit.Assert.assertThat; | ||
|
||
/** | ||
* Implements exponentially weighted moving averages (commonly abbreviated EWMA) for a single value. | ||
*/ | ||
public class ExponentiallyWeightedMovingAverageTests extends ESTestCase { | ||
|
||
public void testEWMA() { | ||
final ExponentiallyWeightedMovingAverage ewma = new ExponentiallyWeightedMovingAverage(0.5, 10); | ||
ewma.addValue(12); | ||
assertThat(ewma.getAverage(), equalTo(11.0)); | ||
ewma.addValue(10); | ||
ewma.addValue(15); | ||
ewma.addValue(13); | ||
assertThat(ewma.getAverage(), equalTo(12.875)); | ||
} | ||
|
||
public void testInvalidAlpha() { | ||
try { | ||
ExponentiallyWeightedMovingAverage ewma = new ExponentiallyWeightedMovingAverage(-0.5, 10); | ||
fail("should have failed to create EWMA"); | ||
} catch (IllegalArgumentException e) { | ||
assertThat(e.getMessage(), equalTo("alpha must be greater or equal to 0 and less than or equal to 1")); | ||
} | ||
|
||
try { | ||
ExponentiallyWeightedMovingAverage ewma = new ExponentiallyWeightedMovingAverage(1.5, 10); | ||
fail("should have failed to create EWMA"); | ||
} catch (IllegalArgumentException e) { | ||
assertThat(e.getMessage(), equalTo("alpha must be greater or equal to 0 and less than or equal to 1")); | ||
} | ||
} | ||
|
||
public void testConvergingToValue() { | ||
final ExponentiallyWeightedMovingAverage ewma = new ExponentiallyWeightedMovingAverage(0.5, 10000); | ||
for (int i = 0; i < 100000; i++) { | ||
ewma.addValue(1); | ||
} | ||
assertThat(ewma.getAverage(), lessThan(2.0)); | ||
} | ||
} |
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