Skip to content
This repository was archived by the owner on Jun 20, 2023. It is now read-only.

Commit 4d6cd25

Browse files
author
Cihat Keser
committed
(possibly breaking) refactor:
- replaced custom round robin implementation with Iterators.cycle from google collections lib - replaced confusing-nano-optimization class PaddedAtomicReference with regular AtomicReference
1 parent f049c13 commit 4d6cd25

File tree

9 files changed

+46
-441
lines changed

9 files changed

+46
-441
lines changed

jest-common/src/main/java/io/searchbox/client/AbstractJestClient.java

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
11
package io.searchbox.client;
22

33

4+
import com.google.common.collect.ImmutableSet;
5+
import com.google.common.collect.Iterators;
46
import com.google.gson.Gson;
57
import com.google.gson.GsonBuilder;
6-
import io.searchbox.client.config.RoundRobinServerList;
7-
import io.searchbox.client.config.ServerList;
88
import io.searchbox.client.config.discovery.NodeChecker;
99
import io.searchbox.client.config.exception.NoServerConfiguredException;
1010
import io.searchbox.client.config.idle.IdleConnectionReaper;
11-
import io.searchbox.client.util.PaddedAtomicReference;
11+
import org.apache.commons.lang3.tuple.Pair;
1212
import org.slf4j.Logger;
1313
import org.slf4j.LoggerFactory;
1414

15-
import java.util.LinkedHashSet;
15+
import java.util.Iterator;
1616
import java.util.Set;
17+
import java.util.concurrent.atomic.AtomicReference;
1718

1819
/**
1920
* @author Dogukan Sonmez
2021
*/
2122
public abstract class AbstractJestClient implements JestClient {
2223

2324
final static Logger log = LoggerFactory.getLogger(AbstractJestClient.class);
25+
2426
public static final String ELASTIC_SEARCH_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";
25-
private final PaddedAtomicReference<ServerList> listOfServers = new PaddedAtomicReference<ServerList>();
27+
2628
protected Gson gson = new GsonBuilder()
2729
.setDateFormat(ELASTIC_SEARCH_DATE_FORMAT)
2830
.create();
2931

32+
// server pool = Pair of (pool size, pool iterator)
33+
private final AtomicReference<Pair<Integer, Iterator<String>>> serverPoolReference =
34+
new AtomicReference<Pair<Integer, Iterator<String>>>(Pair.<Integer, Iterator<String>>of(0, ImmutableSet.<String>of().iterator()));
3035
private NodeChecker nodeChecker;
3136
private IdleConnectionReaper idleConnectionReaper;
3237

@@ -38,23 +43,13 @@ public void setIdleConnectionReaper(IdleConnectionReaper idleConnectionReaper) {
3843
this.idleConnectionReaper = idleConnectionReaper;
3944
}
4045

41-
public LinkedHashSet<String> getServers() {
42-
ServerList server = listOfServers.get();
43-
if (server != null) return new LinkedHashSet<String>(server.getServers());
44-
else return null;
45-
}
46-
47-
public void setServers(ServerList list) {
48-
listOfServers.set(list);
49-
}
50-
5146
public void setServers(Set<String> servers) {
52-
try {
53-
RoundRobinServerList serverList = new RoundRobinServerList(servers);
54-
listOfServers.set(serverList);
55-
} catch (NoServerConfiguredException noServers) {
56-
listOfServers.set(null);
57-
log.warn("No servers are currently available for the client to talk to.");
47+
serverPoolReference.set(Pair.of(servers.size(), Iterators.cycle(servers)));
48+
49+
if (servers.isEmpty()) {
50+
log.warn("No servers are currently available to connect.");
51+
} else if(log.isDebugEnabled()) {
52+
log.debug("Server pool was updated to contain {} servers.", servers.size());
5853
}
5954
}
6055

@@ -69,12 +64,19 @@ public void shutdownClient() {
6964
}
7065
}
7166

72-
protected String getElasticSearchServer() {
73-
ServerList serverList = listOfServers.get();
74-
if (serverList != null) return serverList.getServer();
67+
/**
68+
* @throws io.searchbox.client.config.exception.NoServerConfiguredException
69+
*/
70+
protected String getNextServer() {
71+
Iterator<String> iterator = serverPoolReference.get().getValue();
72+
if (iterator.hasNext()) return iterator.next();
7573
else throw new NoServerConfiguredException("No Server is assigned to client to connect");
7674
}
7775

76+
protected int getServerPoolSize() {
77+
return serverPoolReference.get().getKey();
78+
}
79+
7880
protected String getRequestURL(String elasticSearchServer, String uri) {
7981
StringBuilder sb = new StringBuilder(elasticSearchServer);
8082

jest-common/src/main/java/io/searchbox/client/config/RoundRobinServerList.java

Lines changed: 0 additions & 162 deletions
This file was deleted.

jest-common/src/main/java/io/searchbox/client/config/ServerList.java

Lines changed: 0 additions & 25 deletions
This file was deleted.

jest-common/src/test/java/io/searchbox/client/AbstractJestClientTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
import java.util.Set;
1010
import java.util.concurrent.ExecutionException;
1111

12-
import static junit.framework.Assert.*;
12+
import static junit.framework.Assert.assertEquals;
13+
import static junit.framework.Assert.assertTrue;
1314

1415
/**
1516
* @author Dogukan Sonmez
@@ -74,7 +75,7 @@ public void testGetElasticSearchServer() throws Exception {
7475
Set<String> serverList = new HashSet<String>();
7576

7677
for (int i = 0; i < 3; i++) {
77-
serverList.add(client.getElasticSearchServer());
78+
serverList.add(client.getNextServer());
7879
}
7980

8081
assertEquals("round robin does not work", 3, serverList.size());

0 commit comments

Comments
 (0)