-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Multi node pipeline executor #4070
Open
dolavb
wants to merge
3
commits into
redis:master
Choose a base branch
from
dolavb:MultiNodePipelineExecutor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+93
−34
Open
Changes from all 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
package redis.clients.jedis; | ||
|
||
import java.util.Iterator; | ||
import java.util.LinkedHashMap; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Queue; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
|
@@ -29,6 +29,7 @@ public abstract class MultiNodePipelineBase extends PipelineBase { | |
|
||
private final Map<HostAndPort, Queue<Response<?>>> pipelinedResponses; | ||
private final Map<HostAndPort, Connection> connections; | ||
private ExecutorService executorService; | ||
private volatile boolean syncing = false; | ||
|
||
public MultiNodePipelineBase(CommandObjects commandObjects) { | ||
|
@@ -37,6 +38,24 @@ public MultiNodePipelineBase(CommandObjects commandObjects) { | |
connections = new LinkedHashMap<>(); | ||
} | ||
|
||
|
||
public MultiNodePipelineBase(CommandObjects commandObjects, ExecutorService executorService) { | ||
super(commandObjects); | ||
this.executorService = executorService; | ||
pipelinedResponses = new LinkedHashMap<>(); | ||
connections = new LinkedHashMap<>(); | ||
} | ||
|
||
/** | ||
* Sub-classes must call this method, if graph commands are going to be used. | ||
* @param connectionProvider connection provider | ||
*/ | ||
protected final void prepareGraphCommands(ConnectionProvider connectionProvider) { | ||
GraphCommandObjects graphCommandObjects = new GraphCommandObjects(connectionProvider); | ||
graphCommandObjects.setBaseCommandArgumentsCreator((comm) -> this.commandObjects.commandArguments(comm)); | ||
super.setGraphCommands(graphCommandObjects); | ||
} | ||
|
||
protected abstract HostAndPort getNodeKey(CommandArguments args); | ||
|
||
protected abstract Connection getConnection(HostAndPort nodeKey); | ||
|
@@ -84,44 +103,47 @@ public final void sync() { | |
return; | ||
} | ||
syncing = true; | ||
|
||
ExecutorService executorService = Executors.newFixedThreadPool(MULTI_NODE_PIPELINE_SYNC_WORKERS); | ||
|
||
CountDownLatch countDownLatch = new CountDownLatch(pipelinedResponses.size()); | ||
Iterator<Map.Entry<HostAndPort, Queue<Response<?>>>> pipelinedResponsesIterator | ||
= pipelinedResponses.entrySet().iterator(); | ||
while (pipelinedResponsesIterator.hasNext()) { | ||
Map.Entry<HostAndPort, Queue<Response<?>>> entry = pipelinedResponsesIterator.next(); | ||
HostAndPort nodeKey = entry.getKey(); | ||
Queue<Response<?>> queue = entry.getValue(); | ||
Connection connection = connections.get(nodeKey); | ||
executorService.submit(() -> { | ||
try { | ||
List<Object> unformatted = connection.getMany(queue.size()); | ||
for (Object o : unformatted) { | ||
queue.poll().set(o); | ||
} | ||
} catch (JedisConnectionException jce) { | ||
log.error("Error with connection to " + nodeKey, jce); | ||
// cleanup the connection | ||
pipelinedResponsesIterator.remove(); | ||
connections.remove(nodeKey); | ||
IOUtils.closeQuietly(connection); | ||
} finally { | ||
countDownLatch.countDown(); | ||
} | ||
}); | ||
} | ||
|
||
ExecutorService executorService = getExecutorService(); | ||
CompletableFuture[] futures | ||
= pipelinedResponses.entrySet().stream() | ||
.map(e -> CompletableFuture.runAsync(() -> closeConnection(e), executorService)) | ||
.toArray(CompletableFuture[]::new); | ||
CompletableFuture awaitAllCompleted = CompletableFuture.allOf(futures); | ||
try { | ||
countDownLatch.await(); | ||
awaitAllCompleted.get(); | ||
if (executorService != this.executorService) { | ||
executorService.shutdown(); | ||
} | ||
} catch (ExecutionException e) { | ||
log.error("Failed execution.", e); | ||
} catch (InterruptedException e) { | ||
log.error("Thread is interrupted during sync.", e); | ||
Thread.currentThread().interrupt(); | ||
} | ||
syncing = false; | ||
} | ||
|
||
executorService.shutdownNow(); | ||
private ExecutorService getExecutorService() { | ||
if (executorService == null) { | ||
return Executors.newFixedThreadPool(Math.min(this.pipelinedResponses.size(), MULTI_NODE_PIPELINE_SYNC_WORKERS)); | ||
} | ||
return executorService; | ||
} | ||
|
||
syncing = false; | ||
private void closeConnection(Map.Entry<HostAndPort, Queue<Response<?>>> entry) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method name is misleading. It is actually reading the command responses rather than closing the connections |
||
HostAndPort nodeKey = entry.getKey(); | ||
Queue<Response<?>> queue = entry.getValue(); | ||
Connection connection = connections.get(nodeKey); | ||
try { | ||
List<Object> unformatted = connection.getMany(queue.size()); | ||
for (Object o : unformatted) { | ||
queue.poll().set(o); | ||
} | ||
} catch (JedisConnectionException jce) { | ||
log.error("Error with connection to " + nodeKey, jce); | ||
connections.remove(nodeKey); | ||
IOUtils.closeQuietly(connection); | ||
} | ||
} | ||
|
||
@Deprecated | ||
|
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest a more explicit approach.
e.g, when creating the MultiNodePipelineBase, if an external executor is provided, have a flag
useSharedExecutor = true
.This way it will be clearer that lifecycle is managed externaly