-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Admission Controller Module Rest and Transport Interceptor Commit
Signed-off-by: Ajay Kumar Movva <movvaam@amazon.com>
- Loading branch information
Ajay Kumar Movva
committed
Aug 12, 2023
1 parent
db768d1
commit d9d0686
Showing
14 changed files
with
390 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
apply plugin: 'opensearch.java-rest-test' | ||
|
||
opensearchplugin { | ||
description 'Plugin intercepting requests and throttle based on resource consumption' | ||
classname 'org.opensearch.throttling.OpenSearchThrottlingModulePlugin' | ||
extendedPlugins = ['transport-netty4'] | ||
} | ||
|
||
dependencies { | ||
api project(path: ':modules:reindex') | ||
implementation project(path: ':modules:transport-netty4') | ||
compileOnly project(':modules:transport-netty4') | ||
} | ||
|
||
testClusters.all { | ||
module ':modules:reindex' | ||
} |
44 changes: 44 additions & 0 deletions
44
.../throttling/src/main/java/org/opensearch/throttling/OpenSearchThrottlingModulePlugin.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,44 @@ | ||
/* | ||
* 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; | ||
|
||
import io.netty.channel.ChannelHandler; | ||
import org.opensearch.common.util.concurrent.ThreadContext; | ||
import org.opensearch.core.common.io.stream.NamedWriteableRegistry; | ||
import org.opensearch.plugins.NetworkPlugin; | ||
import org.opensearch.plugins.Plugin; | ||
import org.opensearch.throttling.admissioncontroller.AdmissionControllerRestHandler; | ||
import org.opensearch.throttling.admissioncontroller.AdmissionControllerTransportInterceptor; | ||
import org.opensearch.transport.Netty4HandlerExtension; | ||
import org.opensearch.transport.TransportInterceptor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class OpenSearchThrottlingModulePlugin extends Plugin implements NetworkPlugin, Netty4HandlerExtension { | ||
|
||
private static final Map<String, ChannelHandler> HANDLERS = new HashMap<String, ChannelHandler>(); | ||
|
||
@Override | ||
public List<TransportInterceptor> getTransportInterceptors(NamedWriteableRegistry namedWriteableRegistry, ThreadContext threadContext) { | ||
List<TransportInterceptor> interceptors = new ArrayList<>(0); | ||
interceptors.add(new AdmissionControllerTransportInterceptor(null)); | ||
return interceptors; | ||
} | ||
|
||
@Override | ||
public Map<String, ChannelHandler> getHandlers() { | ||
if (HANDLERS.isEmpty()) { | ||
HANDLERS.put("opensearch-throttling-plugin:AdmissionControlRestHandler", new AdmissionControllerRestHandler()); | ||
} | ||
return HANDLERS; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...in/java/org/opensearch/throttling/admissioncontroller/AdmissionControllerRestHandler.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,63 @@ | ||
/* | ||
* 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.admissioncontroller; | ||
|
||
import io.netty.channel.ChannelDuplexHandler; | ||
import io.netty.channel.ChannelHandler; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelPromise; | ||
import io.netty.handler.codec.http.FullHttpRequest; | ||
import io.netty.handler.codec.http.HttpHeaderNames; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
@ChannelHandler.Sharable | ||
public class AdmissionControllerRestHandler extends ChannelDuplexHandler { | ||
private static final Logger LOGGER = LogManager.getLogger(AdmissionControllerRestHandler.class); | ||
|
||
@Override | ||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { | ||
assert msg instanceof FullHttpRequest : "Invalid message type: " + msg.getClass(); | ||
FullHttpRequest fullHttpRequest = (FullHttpRequest) msg; | ||
String uri = getUri(fullHttpRequest); | ||
applyAdmissionControl(uri); | ||
ctx.fireChannelRead(msg); | ||
} | ||
|
||
private String getUri(FullHttpRequest fullHttpRequest) { | ||
return fullHttpRequest.uri(); | ||
} | ||
|
||
private void applyAdmissionControl(String requestURI) { | ||
// apply admission controller | ||
// LOGGER.info("Apply Admission Controller Triggered URI: " + requestURI); | ||
} | ||
|
||
private void releaseAdmissionControl(ChannelHandlerContext ctx) { | ||
// release the acquired objects | ||
// LOGGER.info("Released Admission Controller Handler"); | ||
} | ||
|
||
private long getContentLength(FullHttpRequest fullHttpRequest) { | ||
String contentLengthHeader = fullHttpRequest.headers().get(HttpHeaderNames.CONTENT_LENGTH); | ||
return contentLengthHeader == null ? 0 : Long.parseLong(contentLengthHeader); | ||
} | ||
|
||
@Override | ||
public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { | ||
releaseAdmissionControl(ctx); | ||
super.close(ctx, promise); | ||
} | ||
|
||
@Override | ||
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { | ||
releaseAdmissionControl(ctx); | ||
super.write(ctx, msg, promise); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
modules/throttling/src/main/plugin-metadata/plugin-security.policy
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,39 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/* | ||
* 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. | ||
*/ | ||
|
||
/* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
grant { | ||
// needed to generate runtime classes | ||
permission java.lang.RuntimePermission "createClassLoader"; | ||
|
||
// needed to find the classloader to load allowlisted classes from | ||
permission java.lang.RuntimePermission "getClassLoader"; | ||
}; |
8 changes: 8 additions & 0 deletions
8
...ling/src/main/resources/META-INF/services/org.opensearch.transport.Netty4HandlerExtension
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,8 @@ | ||
# | ||
# 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. | ||
# | ||
org.opensearch.throttling.OpenSearchThrottlingModulePlugin |
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
20 changes: 20 additions & 0 deletions
20
modules/transport-netty4/src/main/java/org/opensearch/transport/Netty4HandlerExtension.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,20 @@ | ||
/* | ||
* 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.transport; | ||
|
||
import io.netty.channel.ChannelHandler; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
public interface Netty4HandlerExtension { | ||
default Map<String, ChannelHandler> getHandlers() { | ||
return Collections.emptyMap(); | ||
} | ||
} |
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
27 changes: 27 additions & 0 deletions
27
modules/transport-netty4/src/main/java/org/opensearch/transport/NettySettings.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,27 @@ | ||
/* | ||
* 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.transport; | ||
|
||
import org.opensearch.common.settings.Setting; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
|
||
public final class NettySettings { | ||
private NettySettings() {} | ||
|
||
// TODO: Evaluate which one is better, Moving this to yml(AMI Config) or keeping it here. | ||
public static final Setting<List<String>> HANDLER_ORDERING = Setting.listSetting( | ||
"opensearch.netty.plugin.handler.ordering", | ||
Arrays.asList("opensearch-throttling-plugin:AdmissionControlRestHandler"), | ||
Function.identity(), | ||
Setting.Property.NodeScope | ||
); | ||
} |
Oops, something went wrong.