-
Notifications
You must be signed in to change notification settings - Fork 19
feat: add parallelExecution mode for direct I/O thread command execution #191
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| import static com.github.tonivade.resp.SessionListener.nullListener; | ||
| import static java.util.concurrent.Executors.newSingleThreadExecutor; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.function.Function; | ||
|
|
@@ -33,14 +34,10 @@ public class RespServerContext implements ServerContext { | |
|
|
||
| private static final Logger LOGGER = LoggerFactory.getLogger(RespServerContext.class); | ||
|
|
||
| private final StateHolder state = new StateHolder(); | ||
| private final StateHolder state; | ||
| private final ConcurrentHashMap<String, Session> clients = new ConcurrentHashMap<>(); | ||
| private final Scheduler scheduler = Schedulers.from(newSingleThreadExecutor(runnable -> { | ||
| Thread thread = new Thread(runnable); | ||
| thread.setName(RESP_SERVER); | ||
| thread.setDaemon(true); | ||
| return thread; | ||
| })); | ||
| private final Scheduler scheduler; | ||
| private final boolean parallelExecution; | ||
|
|
||
| private final String host; | ||
| private final int port; | ||
|
|
@@ -52,10 +49,24 @@ public RespServerContext(String host, int port, CommandSuite commands) { | |
| } | ||
|
|
||
| public RespServerContext(String host, int port, CommandSuite commands, SessionListener sessionListener) { | ||
| this(host, port, commands, sessionListener, false); | ||
| } | ||
|
|
||
| public RespServerContext(String host, int port, CommandSuite commands, | ||
| SessionListener sessionListener, boolean parallelExecution) { | ||
| this.host = checkNonEmpty(host); | ||
| this.port = checkRange(port, 1024, 65535); | ||
| this.commands = checkNonNull(commands); | ||
| this.sessionListener = checkNonNull(sessionListener); | ||
| this.parallelExecution = parallelExecution; | ||
| if (parallelExecution) { | ||
| this.state = new StateHolder(new ConcurrentHashMap<>()); | ||
| this.scheduler = null; | ||
| } else { | ||
| this.state = new StateHolder(new HashMap<>()); | ||
| this.scheduler = Schedulers.from( | ||
| newSingleThreadExecutor(runnable -> newDaemonThread(runnable, RESP_SERVER))); | ||
| } | ||
| } | ||
|
|
||
| public void start() { | ||
|
|
@@ -64,7 +75,9 @@ public void start() { | |
|
|
||
| public void stop() { | ||
| clear(); | ||
| scheduler.shutdown(); | ||
| if (scheduler != null) { | ||
| scheduler.shutdown(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -114,12 +127,21 @@ void processCommand(Request request) { | |
| LOGGER.debug("received command: {}", request); | ||
|
|
||
| var command = getCommand(request.getCommand()); | ||
| try { | ||
| enqueue(Observable.fromCallable(() -> executeCommand(command, request))) | ||
| .subscribe(response -> processResponse(request, response), | ||
| ex -> LOGGER.error("error executing command: " + request, ex)); | ||
| } catch (RuntimeException ex) { | ||
| LOGGER.error("error executing command: " + request, ex); | ||
| if (parallelExecution) { | ||
|
||
| try { | ||
| RedisToken response = executeCommand(command, request); | ||
| processResponse(request, response); | ||
| } catch (RuntimeException ex) { | ||
| LOGGER.error("error executing command: " + request, ex); | ||
| } | ||
| } else { | ||
| try { | ||
| enqueue(Observable.fromCallable(() -> executeCommand(command, request))) | ||
| .subscribe(response -> processResponse(request, response), | ||
| ex -> LOGGER.error("error executing command: " + request, ex)); | ||
| } catch (RuntimeException ex) { | ||
| LOGGER.error("error executing command: " + request, ex); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -143,6 +165,9 @@ protected RedisToken executeCommand(RespCommand command, Request request) { | |
| } | ||
|
|
||
| protected <T> Observable<T> enqueue(Observable<T> observable) { | ||
| if (scheduler == null) { | ||
| return observable; | ||
| } | ||
| return observable.subscribeOn(scheduler); | ||
| } | ||
|
|
||
|
|
@@ -157,4 +182,11 @@ private void clear() { | |
| clients.clear(); | ||
| state.clear(); | ||
| } | ||
|
|
||
| private static Thread newDaemonThread(Runnable runnable, String name) { | ||
| Thread thread = new Thread(runnable); | ||
| thread.setName(name); | ||
| thread.setDaemon(true); | ||
| return thread; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.