forked from opensearch-project/geospatial
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run system index handling code with stashed thread context (opensearc…
…h-project#297) Signed-off-by: Heemin Kim <[email protected]>
- Loading branch information
Showing
3 changed files
with
142 additions
and
72 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
41 changes: 41 additions & 0 deletions
41
src/main/java/org/opensearch/geospatial/shared/StashedThreadContext.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,41 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.geospatial.shared; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import org.opensearch.client.Client; | ||
import org.opensearch.common.util.concurrent.ThreadContext; | ||
|
||
/** | ||
* Helper class to run code with stashed thread context | ||
* | ||
* Code need to be run with stashed thread context if it interacts with system index | ||
* when security plugin is enabled. | ||
*/ | ||
public class StashedThreadContext { | ||
/** | ||
* Set the thread context to default, this is 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 run(final Client client, final Runnable function) { | ||
try (ThreadContext.StoredContext context = client.threadPool().getThreadContext().stashContext()) { | ||
function.run(); | ||
} | ||
} | ||
|
||
/** | ||
* Set the thread context to default, this is 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 run(final Client client, final Supplier<T> function) { | ||
try (ThreadContext.StoredContext context = client.threadPool().getThreadContext().stashContext()) { | ||
return function.get(); | ||
} | ||
} | ||
} |