-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic thorttler/exponential backoff policy for retry/Defination o…
…f throttling exception (#3856) * Add basic thorttler/exponential backoff policy for retry/Defination of throttling exception Signed-off-by: Dhwanil Patel <[email protected]> * Incorporated comments Signed-off-by: Dhwanil Patel <[email protected]> * Incorporated minor comments Signed-off-by: Dhwanil Patel <[email protected]> * Removed overall thorttlingEnabled flag from Throttler Signed-off-by: Dhwanil Patel <[email protected]> * Corrected Java doc for Throttler Signed-off-by: Dhwanil Patel <[email protected]> * Incorporated comments Signed-off-by: Dhwanil Patel <[email protected]> * Changed the default behaviour of Throttler to return Optional Signed-off-by: Dhwanil Patel <[email protected]> * Removed generics from Throttler and used String as key Signed-off-by: Dhwanil Patel <[email protected]> * Ignore backport / autocut / dependabot branches for gradle checks on push Signed-off-by: Peter Zhu <[email protected]> Co-authored-by: Peter Zhu <[email protected]>
- Loading branch information
1 parent
f35b42f
commit 7ac6c8d
Showing
11 changed files
with
445 additions
and
81 deletions.
There are no files selected for viewing
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
28 changes: 28 additions & 0 deletions
28
server/src/main/java/org/opensearch/cluster/service/MasterTaskThrottlingException.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,28 @@ | ||
/* | ||
* 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.cluster.service; | ||
|
||
import org.opensearch.OpenSearchException; | ||
import org.opensearch.common.io.stream.StreamInput; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Exception raised from master node due to task throttling. | ||
*/ | ||
public class MasterTaskThrottlingException extends OpenSearchException { | ||
|
||
public MasterTaskThrottlingException(String msg, Object... args) { | ||
super(msg, args); | ||
} | ||
|
||
public MasterTaskThrottlingException(StreamInput in) throws IOException { | ||
super(in); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
server/src/main/java/org/opensearch/cluster/service/Throttler.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,93 @@ | ||
/* | ||
* 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.cluster.service; | ||
|
||
import org.opensearch.common.AdjustableSemaphore; | ||
|
||
import java.util.Optional; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
|
||
/** | ||
* Base class for Throttling logic. | ||
* It provides throttling functionality over multiple keys. | ||
*/ | ||
public class Throttler { | ||
protected ConcurrentMap<String, AdjustableSemaphore> semaphores = new ConcurrentHashMap<String, AdjustableSemaphore>(); | ||
|
||
/** | ||
* Method to acquire permits for a key type. | ||
* It will return true if permits can be acquired within threshold limits else false. | ||
* | ||
* If Throttler is not configured for the key then it will return Optional.empty(). | ||
* calling function need to handle this for determining the default behavior. | ||
* | ||
* @param key Key for which we want to acquire permits. | ||
* @param permits Number of permits to acquire. | ||
* @return Optional(Boolean) True/False - Throttler is configured for key and is able to acquire the permits or not | ||
* Optional.empty() - Throttler is not configured for key | ||
*/ | ||
public Optional<Boolean> acquire(final String key, final int permits) { | ||
assert permits > 0; | ||
AdjustableSemaphore semaphore = semaphores.get(key); | ||
if (semaphore != null) { | ||
return Optional.of(semaphore.tryAcquire(permits)); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
/** | ||
* Release the given permits for given type. | ||
* | ||
* @param key key for which we want to release permits. | ||
* @param permits number of permits to release. | ||
*/ | ||
public void release(final String key, final int permits) { | ||
assert permits > 0; | ||
AdjustableSemaphore semaphore = semaphores.get(key); | ||
if (semaphore != null) { | ||
semaphore.release(permits); | ||
assert semaphore.availablePermits() <= semaphore.getMaxPermits(); | ||
} | ||
} | ||
|
||
/** | ||
* Update the Threshold for throttling for given type. | ||
* | ||
* @param key Key for which we want to update limit. | ||
* @param newLimit Updated limit. | ||
*/ | ||
public void updateThrottlingLimit(final String key, final Integer newLimit) { | ||
assert newLimit >= 0; | ||
AdjustableSemaphore semaphore = semaphores.get(key); | ||
if (semaphore == null) { | ||
semaphore = semaphores.computeIfAbsent(key, k -> new AdjustableSemaphore(newLimit, true)); | ||
} | ||
semaphore.setMaxPermits(newLimit); | ||
} | ||
|
||
/** | ||
* Remove the threshold for given key. | ||
* Throttler will no longer do throttling for given key. | ||
* | ||
* @param key Key for which we want to remove throttling. | ||
*/ | ||
public void removeThrottlingLimit(final String key) { | ||
assert semaphores.containsKey(key); | ||
semaphores.remove(key); | ||
} | ||
|
||
public Integer getThrottlingLimit(final String key) { | ||
AdjustableSemaphore semaphore = semaphores.get(key); | ||
if (semaphore != null) { | ||
return semaphore.getMaxPermits(); | ||
} | ||
return null; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
server/src/main/java/org/opensearch/common/AdjustableSemaphore.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,48 @@ | ||
/* | ||
* 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; | ||
|
||
import java.util.concurrent.Semaphore; | ||
|
||
/** | ||
* AdjustableSemaphore is Extended Semphore where we can change maxPermits. | ||
*/ | ||
public class AdjustableSemaphore extends Semaphore { | ||
private final Object maxPermitsMutex = new Object(); | ||
private int maxPermits; | ||
|
||
public AdjustableSemaphore(int maxPermits, boolean fair) { | ||
super(maxPermits, fair); | ||
this.maxPermits = maxPermits; | ||
} | ||
|
||
/** | ||
* Update the maxPermits in semaphore | ||
*/ | ||
public void setMaxPermits(int permits) { | ||
synchronized (maxPermitsMutex) { | ||
final int diff = Math.subtractExact(permits, maxPermits); | ||
if (diff > 0) { | ||
// add permits | ||
release(diff); | ||
} else if (diff < 0) { | ||
// remove permits | ||
reducePermits(Math.negateExact(diff)); | ||
} | ||
maxPermits = permits; | ||
} | ||
} | ||
|
||
/** | ||
* Returns maxPermits. | ||
*/ | ||
public int getMaxPermits() { | ||
return maxPermits; | ||
} | ||
} |
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
Oops, something went wrong.