-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added in-flight cancellation of SearchShardTask based on resource consumption #4575
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,57 @@ | ||||||||||||||||||||||||||||||||||
/* | ||||||||||||||||||||||||||||||||||
* 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. | ||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||
* @opensearch.internal | ||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||
public class MovingAverage { | ||||||||||||||||||||||||||||||||||
private final int windowSize; | ||||||||||||||||||||||||||||||||||
private final long[] observations; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
private long count = 0; | ||||||||||||||||||||||||||||||||||
private long sum = 0; | ||||||||||||||||||||||||||||||||||
private double average = 0; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
public MovingAverage(int windowSize) { | ||||||||||||||||||||||||||||||||||
if (windowSize <= 0) { | ||||||||||||||||||||||||||||||||||
throw new IllegalArgumentException("window size must be greater than zero"); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
this.windowSize = windowSize; | ||||||||||||||||||||||||||||||||||
this.observations = new long[windowSize]; | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||
* Records a new observation and evicts the n-th last observation. | ||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||
public synchronized double record(long value) { | ||||||||||||||||||||||||||||||||||
long delta = value - observations[(int) (count % observations.length)]; | ||||||||||||||||||||||||||||||||||
observations[(int) (count % observations.length)] = value; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
count++; | ||||||||||||||||||||||||||||||||||
sum += delta; | ||||||||||||||||||||||||||||||||||
average = (double) sum / Math.min(count, observations.length); | ||||||||||||||||||||||||||||||||||
return average; | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
Comment on lines
+35
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use CAS here with a do-while loop There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using CAS backed by a queue or a ring-buffer, it may be possible to track the moving average similar to how it's done in indexing backpressure (though the current implementation has subtle race-condition bugs which I have highlighted in an above comment). To successfully implement this, we need to ensure the last 'n' items, running sum, and count of inserted items are updated atomically; which may not be possible with CAS alone. We need to treat the entire state (even the backing queue/buffer) as immutable and create a new copy with every update (similar concept to Our use-case is write heavy (on task completion) with infrequent reads (on search backpressure service iteration), creating copies may be very expensive especially for larger window sizes. I'm still inclined to use the current approach. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Benchmark results comparing the existing approach v/s CAS backed by an immutable ring-buffer. Using
Using compare-and-set backed by an immutable ring-buffer (implementation reference):
Key observations:
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
public double getAverage() { | ||||||||||||||||||||||||||||||||||
return average; | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
public long getCount() { | ||||||||||||||||||||||||||||||||||
return count; | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
public boolean isReady() { | ||||||||||||||||||||||||||||||||||
return count >= windowSize; | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* 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; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
/** | ||
* Streak is a data structure that keeps track of the number of successive successful events. | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public class Streak { | ||
private final AtomicInteger successiveSuccessfulEvents = new AtomicInteger(); | ||
|
||
public int record(boolean isSuccessful) { | ||
if (isSuccessful) { | ||
return successiveSuccessfulEvents.incrementAndGet(); | ||
} else { | ||
successiveSuccessfulEvents.set(0); | ||
return 0; | ||
} | ||
} | ||
|
||
public int length() { | ||
return successiveSuccessfulEvents.get(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* 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; | ||
|
||
import java.util.Objects; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import java.util.function.LongSupplier; | ||
|
||
/** | ||
* TokenBucket is used to limit the number of operations at a constant rate while allowing for short bursts. | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public class TokenBucket { | ||
/** | ||
* Defines a monotonically increasing counter. | ||
* | ||
* Usage examples: | ||
* 1. clock = System::nanoTime can be used to perform rate-limiting per unit time | ||
* 2. clock = AtomicLong::get can be used to perform rate-limiting per unit number of operations | ||
*/ | ||
private final LongSupplier clock; | ||
|
||
/** | ||
* Defines the number of tokens added to the bucket per clock cycle. | ||
*/ | ||
private final double rate; | ||
|
||
/** | ||
* Defines the capacity and the maximum number of operations that can be performed per clock cycle before | ||
* the bucket runs out of tokens. | ||
*/ | ||
private final double burst; | ||
|
||
/** | ||
* Defines the current state of the token bucket. | ||
*/ | ||
private final AtomicReference<State> state; | ||
|
||
public TokenBucket(LongSupplier clock, double rate, double burst) { | ||
this(clock, rate, burst, burst); | ||
} | ||
|
||
public TokenBucket(LongSupplier clock, double rate, double burst, double initialTokens) { | ||
if (rate <= 0.0) { | ||
throw new IllegalArgumentException("rate must be greater than zero"); | ||
} | ||
|
||
if (burst <= 0.0) { | ||
throw new IllegalArgumentException("burst must be greater than zero"); | ||
} | ||
|
||
this.clock = clock; | ||
this.rate = rate; | ||
this.burst = burst; | ||
this.state = new AtomicReference<>(new State(Math.min(initialTokens, burst), clock.getAsLong())); | ||
} | ||
|
||
/** | ||
* If there are enough tokens in the bucket, it requests/deducts 'n' tokens and returns true. | ||
* Otherwise, returns false and leaves the bucket untouched. | ||
*/ | ||
public boolean request(double n) { | ||
if (n <= 0) { | ||
throw new IllegalArgumentException("requested tokens must be greater than zero"); | ||
} | ||
|
||
// Refill tokens | ||
State currentState, updatedState; | ||
do { | ||
currentState = state.get(); | ||
long now = clock.getAsLong(); | ||
double incr = (now - currentState.lastRefilledAt) * rate; | ||
updatedState = new State(Math.min(currentState.tokens + incr, burst), now); | ||
} while (state.compareAndSet(currentState, updatedState) == false); | ||
|
||
// Deduct tokens | ||
do { | ||
currentState = state.get(); | ||
if (currentState.tokens < n) { | ||
return false; | ||
} | ||
updatedState = new State(currentState.tokens - n, currentState.lastRefilledAt); | ||
} while (state.compareAndSet(currentState, updatedState) == false); | ||
|
||
return true; | ||
} | ||
|
||
public boolean request() { | ||
return request(1.0); | ||
} | ||
|
||
/** | ||
* Represents an immutable token bucket state. | ||
*/ | ||
private static class State { | ||
final double tokens; | ||
final double lastRefilledAt; | ||
|
||
public State(double tokens, double lastRefilledAt) { | ||
this.tokens = tokens; | ||
this.lastRefilledAt = lastRefilledAt; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
State state = (State) o; | ||
return Double.compare(state.tokens, tokens) == 0 && Double.compare(state.lastRefilledAt, lastRefilledAt) == 0; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(tokens, lastRefilledAt); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use a queue and see if we can avoid the synchronized block by using CAS if possible
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually plan to benchmark CAS vs synchronized approaches before committing to either one of them, especially since the operations are pretty simple and quick to execute.
If there are major gains with CAS then it makes sense to go with it. Otherwise, I would prefer keeping it simple and readable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not observe any major gains with queue + CAS approach as followed in indexing back-pressure (here). With higher thread contention, it performed slightly poorly.
It is also worth noting that the current implementation of moving average in shard indexing back-pressure has subtle race-condition bugs. Two or more concurrent threads could reach this point and remove excessive elements from the queue leading to incorrect results, or even NPE in the worst case. Another problem is lack of causal ordering – an older thread may overwrite a new thread's average at this point.