Skip to content
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

POC: Response interceptor #897

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
added response interceptor
Signed-off-by: Ronnak Saxena <[email protected]>
  • Loading branch information
ronnaksaxena committed Aug 18, 2023
commit 8d8d06eef1d4ce0ae22a869cfa3879278c04419f
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ import org.opensearch.indexmanagement.rollup.action.start.TransportStartRollupAc
import org.opensearch.indexmanagement.rollup.action.stop.StopRollupAction
import org.opensearch.indexmanagement.rollup.action.stop.TransportStopRollupAction
import org.opensearch.indexmanagement.rollup.actionfilter.FieldCapsFilter
import org.opensearch.indexmanagement.rollup.interceptor.ResponseInterceptor
import org.opensearch.indexmanagement.rollup.interceptor.RollupInterceptor
import org.opensearch.indexmanagement.rollup.model.Rollup
import org.opensearch.indexmanagement.rollup.model.RollupMetadata
Expand Down Expand Up @@ -208,6 +209,7 @@ class IndexManagementPlugin : JobSchedulerExtension, NetworkPlugin, ActionPlugin
lateinit var clusterService: ClusterService
lateinit var indexNameExpressionResolver: IndexNameExpressionResolver
lateinit var rollupInterceptor: RollupInterceptor
lateinit var responseInterceptor: ResponseInterceptor
lateinit var fieldCapsFilter: FieldCapsFilter
lateinit var indexMetadataProvider: IndexMetadataProvider
private val indexMetadataServices: MutableList<Map<String, IndexMetadataService>> = mutableListOf()
Expand Down Expand Up @@ -391,6 +393,7 @@ class IndexManagementPlugin : JobSchedulerExtension, NetworkPlugin, ActionPlugin
environment
)
rollupInterceptor = RollupInterceptor(clusterService, settings, indexNameExpressionResolver)
responseInterceptor = ResponseInterceptor(clusterService, settings, indexNameExpressionResolver)
val jvmService = JvmService(environment.settings())
val transformRunner = TransformRunner.initialize(
client,
Expand Down Expand Up @@ -612,7 +615,7 @@ class IndexManagementPlugin : JobSchedulerExtension, NetworkPlugin, ActionPlugin
}

override fun getTransportInterceptors(namedWriteableRegistry: NamedWriteableRegistry, threadContext: ThreadContext): List<TransportInterceptor> {
return listOf(rollupInterceptor)
return listOf(rollupInterceptor, responseInterceptor)
}

override fun getActionFilters(): List<ActionFilter> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.indexmanagement.rollup.interceptor

import org.apache.logging.log4j.LogManager
import org.opensearch.cluster.metadata.IndexNameExpressionResolver
import org.opensearch.cluster.service.ClusterService
import org.opensearch.common.io.stream.StreamInput
import org.opensearch.common.settings.Settings
import org.opensearch.transport.TransportInterceptor
import org.opensearch.transport.TransportResponse
import org.opensearch.transport.TransportRequest
import org.opensearch.transport.Transport
import org.opensearch.transport.TransportRequestOptions
import org.opensearch.transport.TransportResponseHandler
import org.opensearch.transport.TransportException


class ResponseInterceptor(
val clusterService: ClusterService,
val settings: Settings,
val indexNameExpressionResolver: IndexNameExpressionResolver
) : TransportInterceptor {
private val logger = LogManager.getLogger(javaClass)

override fun interceptSender(sender: TransportInterceptor.AsyncSender): TransportInterceptor.AsyncSender {
return CustomAsyncSender(sender)
}

private inner class CustomAsyncSender(private val originalSender: TransportInterceptor.AsyncSender) : TransportInterceptor.AsyncSender {

override fun <T : TransportResponse?> sendRequest(
connection: Transport.Connection?,
action: String?,
request: TransportRequest?,
options: TransportRequestOptions?,
handler: TransportResponseHandler<T>?
) {
val interceptedHandler = CustomResponseHandler(handler)

originalSender.sendRequest(connection, action, request, options, interceptedHandler)
}
}

private inner class CustomResponseHandler<T : TransportResponse?>(
private val originalHandler: TransportResponseHandler<T>?
) : TransportResponseHandler<T> {

override fun read(inStream: StreamInput?): T {
val response = originalHandler?.read(inStream)
// Modify the response if necessary
return response!!
}

override fun handleResponse(response: T?) {
// Handle the response or delegate to the original handler
logger.error("ronsax response interceptoed!! $response")
originalHandler?.handleResponse(response)
}

override fun handleException(exp: TransportException?) {
// Handle exceptions or delegate to the original handler
originalHandler?.handleException(exp)
}

override fun executor(): String {
return originalHandler?.executor() ?: ""
}

}
}