forked from opensearch-project/sql
-
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.
Schedule request in worker thread (opensearch-project#748)
Signed-off-by: penghuo <penghuo@gmail.com>
- Loading branch information
Showing
4 changed files
with
130 additions
and
45 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
33 changes: 33 additions & 0 deletions
33
opensearch/src/main/java/org/opensearch/sql/opensearch/executor/Scheduler.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,33 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.opensearch.executor; | ||
|
||
import java.util.Map; | ||
import lombok.experimental.UtilityClass; | ||
import org.apache.logging.log4j.ThreadContext; | ||
import org.opensearch.client.node.NodeClient; | ||
import org.opensearch.common.unit.TimeValue; | ||
import org.opensearch.threadpool.ThreadPool; | ||
|
||
/** The scheduler which schedule the task run in sql-worker thread pool. */ | ||
@UtilityClass | ||
public class Scheduler { | ||
|
||
public static final String SQL_WORKER_THREAD_POOL_NAME = "sql-worker"; | ||
|
||
public static void schedule(NodeClient client, Runnable task) { | ||
ThreadPool threadPool = client.threadPool(); | ||
threadPool.schedule(withCurrentContext(task), new TimeValue(0), SQL_WORKER_THREAD_POOL_NAME); | ||
} | ||
|
||
private static Runnable withCurrentContext(final Runnable task) { | ||
final Map<String, String> currentContext = ThreadContext.getImmutableContext(); | ||
return () -> { | ||
ThreadContext.putAll(currentContext); | ||
task.run(); | ||
}; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
opensearch/src/test/java/org/opensearch/sql/opensearch/executor/SchedulerTest.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.sql.opensearch.executor; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.doAnswer; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.opensearch.client.node.NodeClient; | ||
import org.opensearch.threadpool.ThreadPool; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class SchedulerTest { | ||
@Test | ||
public void schedule() { | ||
NodeClient nodeClient = mock(NodeClient.class); | ||
ThreadPool threadPool = mock(ThreadPool.class); | ||
when(nodeClient.threadPool()).thenReturn(threadPool); | ||
|
||
doAnswer( | ||
invocation -> { | ||
Runnable task = invocation.getArgument(0); | ||
task.run(); | ||
return null; | ||
}) | ||
.when(threadPool) | ||
.schedule(any(), any(), any()); | ||
AtomicBoolean isRun = new AtomicBoolean(false); | ||
Scheduler.schedule(nodeClient, () -> isRun.set(true)); | ||
assertTrue(isRun.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