Skip to content

Commit 8df11ba

Browse files
daschlMichael Nitschinger
authored andcommitted
JCBC-1886: Allow to list the number of currently available replicas for a document ID.
Change-Id: I669bc4c1e11b92d4577cd4e177cbb8ff4ca79cc2 Reviewed-on: http://review.couchbase.org/c/couchbase-java-client/+/164078 Tested-by: Michael Nitschinger <michael.nitschinger@couchbase.com> Reviewed-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
1 parent edae4a5 commit 8df11ba

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

src/main/java/com/couchbase/client/java/util/NodeLocatorHelper.java

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,17 @@
5454
@InterfaceAudience.Public
5555
public class NodeLocatorHelper {
5656

57-
private final ConfigurationProvider configProvider;
5857
private final AtomicReference<BucketConfig> bucketConfig;
5958

6059
private NodeLocatorHelper(final Bucket bucket) {
61-
configProvider = bucket
62-
.core()
63-
.<GetConfigProviderResponse>send(new GetConfigProviderRequest())
64-
.toBlocking()
65-
.single()
66-
.provider();
60+
ConfigurationProvider configProvider = bucket
61+
.core()
62+
.<GetConfigProviderResponse>send(new GetConfigProviderRequest())
63+
.toBlocking()
64+
.single()
65+
.provider();
6766

68-
bucketConfig = new AtomicReference<BucketConfig>(configProvider.config().bucketConfig(bucket.name()));
67+
bucketConfig = new AtomicReference<>(configProvider.config().bucketConfig(bucket.name()));
6968

7069
configProvider
7170
.configs()
@@ -121,7 +120,7 @@ public List<InetAddress> replicaNodesForId(final String id) {
121120

122121
if (config instanceof CouchbaseBucketConfig) {
123122
CouchbaseBucketConfig cbc = (CouchbaseBucketConfig) config;
124-
List<InetAddress> replicas = new ArrayList<InetAddress>();
123+
List<InetAddress> replicas = new ArrayList<>();
125124
for (int i = 1; i <= cbc.numberOfReplicas(); i++) {
126125
replicas.add(replicaNodeForId(id, i));
127126
}
@@ -131,6 +130,31 @@ public List<InetAddress> replicaNodesForId(final String id) {
131130
}
132131
}
133132

133+
/**
134+
* Returns all target replica nodes {@link InetAddress} which are currently available on the bucket.
135+
*
136+
* @param id the document ID to check.
137+
* @return the list of nodes for the given document ID.
138+
*/
139+
public List<InetAddress> availableReplicaNodesForId(final String id) {
140+
BucketConfig config = bucketConfig.get();
141+
142+
if (config instanceof CouchbaseBucketConfig) {
143+
CouchbaseBucketConfig cbc = (CouchbaseBucketConfig) config;
144+
List<InetAddress> replicas = new ArrayList<>();
145+
for (int i = 1; i <= cbc.numberOfReplicas(); i++) {
146+
try {
147+
replicas.add(replicaNodeForId(id, i));
148+
} catch (IllegalStateException ex) {
149+
// We ignore -1 and -2 vbuckets on purpose.
150+
}
151+
}
152+
return replicas;
153+
} else {
154+
throw new UnsupportedOperationException("Bucket type not supported: " + config.getClass().getName());
155+
}
156+
}
157+
134158
/**
135159
* Returns the target replica node {@link InetAddress} for a given document ID and replica number on the bucket.
136160
*
@@ -171,7 +195,7 @@ public InetAddress replicaNodeForId(final String id, int replicaNum) {
171195
* @return all currently known nodes.
172196
*/
173197
public List<InetAddress> nodes() {
174-
List<InetAddress> allNodes = new ArrayList<InetAddress>();
198+
List<InetAddress> allNodes = new ArrayList<>();
175199
BucketConfig config = bucketConfig.get();
176200
for (NodeInfo nodeInfo : config.nodes()) {
177201
try {

0 commit comments

Comments
 (0)