Elffuss BraidHub

kikocis/java-driver public

Parallelize pool creations when creating the first session (JAVA-522).
Changeset3d0416253712a15d371454a313a919fc81d12a9600437c695b9c4a435f8bb70e
Authorolim7t <omichallat+github@gmail.com> · 2014-10-31 12:53 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2014-11-03 11:32 +01:00
Parents468145015408

Browse the files at this changeset

1 file changed

modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +33 −4
@@ -64,7 +64,39 @@
6464 // If we haven't initialized the cluster, do it now
6565 cluster.init();
6666
67- // Create pool to initial nodes (and wait for them to be created)
67+ // Create pools to initial nodes (and wait for them to be created)
68+ Collection<Host> hosts = cluster.getMetadata().allHosts();
69+ if (cluster.manager.sessions.size() == 1) {
70+ // We only do it in parallel if this is the first session (meaning that the cluster just initialized).
71+ createPoolsInParallel(hosts);
72+ } else {
73+ // Otherwise, we don't want to fill executor() because this is also where up/down notifications are processed,
74+ // it's important that existing sessions get them in a timely manner. So we create the pools one by one:
75+ createPoolsSequentially(hosts);
76+ }
77+
78+ isInit = true;
79+ updateCreatedPools(executor());
80+ return this;
81+ }
82+
83+ private void createPoolsInParallel(Collection<Host> hosts) {
84+ List<ListenableFuture<Boolean>> futures = new ArrayList<ListenableFuture<Boolean>>(hosts.size());
85+ for (Host host : hosts)
86+ if (host.state != Host.State.DOWN)
87+ futures.add(maybeAddPool(host, executor()));
88+ ListenableFuture<List<Boolean>> f = Futures.allAsList(futures);
89+ try {
90+ f.get();
91+ } catch (ExecutionException e) {
92+ // This is not supposed to happen
93+ throw new DriverInternalError(e);
94+ } catch (InterruptedException e) {
95+ Thread.currentThread().interrupt();
96+ }
97+ }
98+
99+ private void createPoolsSequentially(Collection<Host> hosts) {
68100 for (Host host : cluster.getMetadata().allHosts()) {
69101 try {
70102 if (host.state != Host.State.DOWN)
@@ -76,9 +108,6 @@
76108 Thread.currentThread().interrupt();
77109 }
78110 }
79- isInit = true;
80- updateCreatedPools(executor());
81- return this;
82111 }
83112
84113 public String getLoggedKeyspace() {