-
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.
Signed-off-by: Ajay Kumar Movva <[email protected]>
- Loading branch information
Ajay Kumar Movva
committed
Feb 9, 2024
1 parent
30c4210
commit 8c5e6ed
Showing
13 changed files
with
320 additions
and
36 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
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
95 changes: 95 additions & 0 deletions
95
.../opensearch/ratelimitting/admissioncontrol/transport/AdmissionControlInterceptSender.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,95 @@ | ||
/* | ||
* 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.transport; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.common.util.concurrent.ThreadContext; | ||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.transport.TransportResponse; | ||
import org.opensearch.threadpool.ThreadPool; | ||
import org.opensearch.transport.*; | ||
|
||
import java.io.IOException; | ||
|
||
public class AdmissionControlInterceptSender { | ||
|
||
ThreadPool threadPool; | ||
public AdmissionControlInterceptSender(ThreadPool threadPool) { | ||
this.threadPool = threadPool; | ||
} | ||
private static final Logger logger = LogManager.getLogger(AdmissionControlInterceptSender.class); | ||
public <T extends TransportResponse> void sendRequestDecorate( | ||
TransportInterceptor.AsyncSender sender, | ||
Transport.Connection connection, | ||
String action, | ||
TransportRequest request, | ||
TransportRequestOptions options, | ||
TransportResponseHandler<T> handler | ||
) { | ||
try (ThreadContext.StoredContext stashedContext = this.getThreadContext().stashContext()) { | ||
if(isActionIndexingOrSearch(action)){ | ||
logger.info("AdmissionControlInterceptSender is Triggered Action: {}", action); | ||
} | ||
RestoringTransportResponseHandler restoringTransportResponseHandler = new RestoringTransportResponseHandler(handler, stashedContext, action); | ||
sender.sendRequest(connection, action, request, options, restoringTransportResponseHandler); | ||
} | ||
} | ||
|
||
private boolean isActionIndexingOrSearch(String action) { | ||
return action.startsWith("indices:data/read/search") || action.startsWith("indices:data/write/bulk"); | ||
} | ||
|
||
private ThreadContext getThreadContext() { | ||
return threadPool.getThreadContext(); | ||
} | ||
|
||
private static class RestoringTransportResponseHandler<T extends TransportResponse> implements TransportResponseHandler<T> { | ||
|
||
private final ThreadContext.StoredContext contextToRestore; | ||
private final TransportResponseHandler<T> innerHandler; | ||
|
||
private final String action; | ||
|
||
private RestoringTransportResponseHandler(TransportResponseHandler<T> innerHandler, ThreadContext.StoredContext contextToRestore, String action) { | ||
this.contextToRestore = contextToRestore; | ||
this.innerHandler = innerHandler; | ||
this.action = action; | ||
} | ||
|
||
@Override | ||
public T read(StreamInput in) throws IOException { | ||
return innerHandler.read(in); | ||
} | ||
|
||
@Override | ||
public void handleResponse(T response) { | ||
if (this.isActionIndexingOrSearch(this.action)){ | ||
logger.info("Handle Response Triggered in: RestoringTransportResponseHandler"); | ||
} | ||
contextToRestore.restore(); | ||
innerHandler.handleResponse(response); | ||
} | ||
|
||
@Override | ||
public void handleException(TransportException e) { | ||
contextToRestore.restore(); | ||
innerHandler.handleException(e); | ||
} | ||
|
||
@Override | ||
public String executor() { | ||
return innerHandler.executor(); | ||
} | ||
|
||
private boolean isActionIndexingOrSearch(String action) { | ||
return action.startsWith("indices:data/read/search") || action.startsWith("indices:data/write/bulk"); | ||
} | ||
} | ||
} |
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
40 changes: 40 additions & 0 deletions
40
server/src/main/java/org/opensearch/tasks/ResourceUsageStatsTCPropagator.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,40 @@ | ||
/* | ||
* 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.tasks; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.opensearch.common.util.concurrent.ThreadContextStatePropagator; | ||
|
||
public class ResourceUsageStatsTCPropagator implements ThreadContextStatePropagator { | ||
public static final String NODE_RESOURCE_STATS = "PERF_STATS"; | ||
@Override | ||
public Map<String, Object> transients(Map<String, Object> source) { | ||
final Map<String, Object> transients = new HashMap<>(); | ||
for(Map.Entry<String, Object> entry : source.entrySet()) { | ||
if(entry.getKey().startsWith(NODE_RESOURCE_STATS)) { | ||
// key starts with prefix | ||
transients.put(entry.getKey(), entry.getValue()); | ||
} | ||
} | ||
return transients; | ||
} | ||
|
||
@Override | ||
public Map<String, String> headers(Map<String, Object> source) { | ||
final Map<String, String> headers = new HashMap<>(); | ||
for(Map.Entry<String, Object> entry : source.entrySet()) { | ||
if(entry.getKey().startsWith(NODE_RESOURCE_STATS)) { | ||
// key starts with prefix | ||
headers.put(entry.getKey(), entry.getValue().toString()); | ||
} | ||
} | ||
return headers; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
server/src/main/java/org/opensearch/transport/ResourceUsageStatsReference.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,31 @@ | ||
/* | ||
* 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; | ||
|
||
public class ResourceUsageStatsReference { | ||
private String resourceUsageStats; | ||
|
||
public ResourceUsageStatsReference(String stats) { | ||
this.resourceUsageStats = stats; | ||
} | ||
|
||
public String getResourceUsageStats() { | ||
return resourceUsageStats; | ||
} | ||
|
||
public void setResourceUsageStats(String stats) { | ||
this.resourceUsageStats = new String(stats); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return this.resourceUsageStats; | ||
} | ||
|
||
} |
Oops, something went wrong.