-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added changes for AdmissionControl Interceptor and AdmissionControlSe…
…rvice for RateLimiting (#9286) * Changes for AdmissionControl Interceptor and AdmissionControlService for RateLimiting (#9286) Signed-off-by: Ajay Kumar Movva <movvaam@amazon.com>
- Loading branch information
Showing
28 changed files
with
1,594 additions
and
19 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
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
104 changes: 104 additions & 0 deletions
104
.../src/main/java/org/opensearch/ratelimitting/admissioncontrol/AdmissionControlService.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,104 @@ | ||
/* | ||
* 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.ratelimitting.admissioncontrol; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.common.settings.ClusterSettings; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.ratelimitting.admissioncontrol.controllers.AdmissionController; | ||
import org.opensearch.ratelimitting.admissioncontrol.controllers.CPUBasedAdmissionController; | ||
import org.opensearch.threadpool.ThreadPool; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
|
||
import static org.opensearch.ratelimitting.admissioncontrol.settings.CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER; | ||
|
||
/** | ||
* Admission control Service that bootstraps and manages all the Admission Controllers in OpenSearch. | ||
*/ | ||
public class AdmissionControlService { | ||
private final ThreadPool threadPool; | ||
public final AdmissionControlSettings admissionControlSettings; | ||
private final ConcurrentMap<String, AdmissionController> ADMISSION_CONTROLLERS; | ||
private static final Logger logger = LogManager.getLogger(AdmissionControlService.class); | ||
private final ClusterSettings clusterSettings; | ||
private final Settings settings; | ||
|
||
/** | ||
* | ||
* @param settings Immutable settings instance | ||
* @param clusterSettings ClusterSettings Instance | ||
* @param threadPool ThreadPool Instance | ||
*/ | ||
public AdmissionControlService(Settings settings, ClusterSettings clusterSettings, ThreadPool threadPool) { | ||
this.threadPool = threadPool; | ||
this.admissionControlSettings = new AdmissionControlSettings(clusterSettings, settings); | ||
this.ADMISSION_CONTROLLERS = new ConcurrentHashMap<>(); | ||
this.clusterSettings = clusterSettings; | ||
this.settings = settings; | ||
this.initialise(); | ||
} | ||
|
||
/** | ||
* Initialise and Register all the admissionControllers | ||
*/ | ||
private void initialise() { | ||
// Initialise different type of admission controllers | ||
registerAdmissionController(CPU_BASED_ADMISSION_CONTROLLER); | ||
} | ||
|
||
/** | ||
* Handler to trigger registered admissionController | ||
*/ | ||
public void applyTransportAdmissionControl(String action) { | ||
this.ADMISSION_CONTROLLERS.forEach((name, admissionController) -> { admissionController.apply(action); }); | ||
} | ||
|
||
/** | ||
* | ||
* @param admissionControllerName admissionControllerName to register into the service. | ||
*/ | ||
public void registerAdmissionController(String admissionControllerName) { | ||
AdmissionController admissionController = this.controllerFactory(admissionControllerName); | ||
this.ADMISSION_CONTROLLERS.put(admissionControllerName, admissionController); | ||
} | ||
|
||
/** | ||
* @return AdmissionController Instance | ||
*/ | ||
private AdmissionController controllerFactory(String admissionControllerName) { | ||
switch (admissionControllerName) { | ||
case CPU_BASED_ADMISSION_CONTROLLER: | ||
return new CPUBasedAdmissionController(admissionControllerName, this.settings, this.clusterSettings); | ||
default: | ||
throw new IllegalArgumentException("Not Supported AdmissionController : " + admissionControllerName); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @return list of the registered admissionControllers | ||
*/ | ||
public List<AdmissionController> getAdmissionControllers() { | ||
return new ArrayList<>(this.ADMISSION_CONTROLLERS.values()); | ||
} | ||
|
||
/** | ||
* | ||
* @param controllerName name of the admissionController | ||
* @return instance of the AdmissionController Instance | ||
*/ | ||
public AdmissionController getAdmissionController(String controllerName) { | ||
return this.ADMISSION_CONTROLLERS.getOrDefault(controllerName, null); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...src/main/java/org/opensearch/ratelimitting/admissioncontrol/AdmissionControlSettings.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,83 @@ | ||
/* | ||
* 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.ratelimitting.admissioncontrol; | ||
|
||
import org.opensearch.common.settings.ClusterSettings; | ||
import org.opensearch.common.settings.Setting; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.ratelimitting.admissioncontrol.enums.AdmissionControlMode; | ||
|
||
/** | ||
* Settings related to admission control. | ||
* @opensearch.internal | ||
*/ | ||
public final class AdmissionControlSettings { | ||
|
||
/** | ||
* Default parameters for the AdmissionControlSettings | ||
*/ | ||
public static class Defaults { | ||
public static final String MODE = "disabled"; | ||
} | ||
|
||
/** | ||
* Feature level setting to operate in shadow-mode or in enforced-mode. If enforced field is set | ||
* rejection will be performed, otherwise only rejection metrics will be populated. | ||
*/ | ||
public static final Setting<AdmissionControlMode> ADMISSION_CONTROL_TRANSPORT_LAYER_MODE = new Setting<>( | ||
"admission_control.transport.mode", | ||
Defaults.MODE, | ||
AdmissionControlMode::fromName, | ||
Setting.Property.Dynamic, | ||
Setting.Property.NodeScope | ||
); | ||
|
||
private volatile AdmissionControlMode transportLayeradmissionControlMode; | ||
|
||
/** | ||
* @param clusterSettings clusterSettings Instance | ||
* @param settings settings instance | ||
*/ | ||
public AdmissionControlSettings(ClusterSettings clusterSettings, Settings settings) { | ||
this.transportLayeradmissionControlMode = ADMISSION_CONTROL_TRANSPORT_LAYER_MODE.get(settings); | ||
clusterSettings.addSettingsUpdateConsumer(ADMISSION_CONTROL_TRANSPORT_LAYER_MODE, this::setAdmissionControlTransportLayerMode); | ||
} | ||
|
||
/** | ||
* | ||
* @param admissionControlMode update the mode of admission control feature | ||
*/ | ||
private void setAdmissionControlTransportLayerMode(AdmissionControlMode admissionControlMode) { | ||
this.transportLayeradmissionControlMode = admissionControlMode; | ||
} | ||
|
||
/** | ||
* | ||
* @return return the default mode of the admissionControl | ||
*/ | ||
public AdmissionControlMode getAdmissionControlTransportLayerMode() { | ||
return this.transportLayeradmissionControlMode; | ||
} | ||
|
||
/** | ||
* | ||
* @return true based on the admission control feature is enforced else false | ||
*/ | ||
public Boolean isTransportLayerAdmissionControlEnforced() { | ||
return this.transportLayeradmissionControlMode == AdmissionControlMode.ENFORCED; | ||
} | ||
|
||
/** | ||
* | ||
* @return true based on the admission control feature is enabled else false | ||
*/ | ||
public Boolean isTransportLayerAdmissionControlEnabled() { | ||
return this.transportLayeradmissionControlMode != AdmissionControlMode.DISABLED; | ||
} | ||
} |
Oops, something went wrong.