-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support of .opensearch-knn-model as system index to transport act…
…ions (#847) * Add thread context stashing to transport actions Signed-off-by: Martin Gaievski <[email protected]>
- Loading branch information
1 parent
427cd32
commit 1b6fd48
Showing
9 changed files
with
210 additions
and
112 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
39 changes: 39 additions & 0 deletions
39
src/main/java/org/opensearch/knn/common/ThreadContextHelper.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,39 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.knn.common; | ||
|
||
import org.opensearch.client.Client; | ||
import org.opensearch.common.util.concurrent.ThreadContext; | ||
|
||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Class abstracts execution of runnable or function in specific context | ||
*/ | ||
public class ThreadContextHelper { | ||
|
||
/** | ||
* Sets the thread context to default and execute function, this needed to allow actions on model system index | ||
* when security plugin is enabled | ||
* @param function runnable that needs to be executed after thread context has been stashed, accepts and returns nothing | ||
*/ | ||
public static void runWithStashedThreadContext(Client client, Runnable function) { | ||
try (ThreadContext.StoredContext context = client.threadPool().getThreadContext().stashContext()) { | ||
function.run(); | ||
} | ||
} | ||
|
||
/** | ||
* Sets the thread context to default and execute function, this needed to allow actions on model system index | ||
* when security plugin is enabled | ||
* @param function supplier function that needs to be executed after thread context has been stashed, return object | ||
*/ | ||
public static <T> T runWithStashedThreadContext(Client client, Supplier<T> function) { | ||
try (ThreadContext.StoredContext context = client.threadPool().getThreadContext().stashContext()) { | ||
return function.get(); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.