-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Multi node pipeline executor #4070
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
c196d7a
a50568a
1e9a4fb
8205d3a
5d3398f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) { | ||
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. I suggest a more explicit approach. 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. Thanks, will address. |
||
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 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. Thanks, will address. |
||
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 | ||
|
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.
Graph module support got removed with #4073