Hi Friends,
In an Aerospike cluster, is there a method (ideally java client API) which can locate the node id(s) of the host where a given record is store?
For example, I know a record with namespace, set and PK, how can I find out in which node(s) the record is stored?
I haven’t tested, but looking at the Java client it seems you would create a either a WriteCommand or ReadCommand with the applicable policy and key and then call the commands getNode method. For this you will need access to the client’s cluster object which is protected by default so you would need to extend the AerospikeClient to expose the cluster.
Something like
public class MyClient extends AerospikeClient {
public Cluster getCluster() {
return this.cluster;
}
}
MyClient client = new MyClient("192.168.1.150", 3000);
Key key = new Key("test", "test-set", "test-key");
WriteCommand cmd = new WriteCommand(client.getCluster(), key, NULLL, NULL);
Node node = cmd.getNode();
My java is a bit rusty, hopefully this is close to something that will work.
Here is a hack… … (key1 is the record of interest, I am assuming it exists on the cluster already - so this is expected to fail. AerospikeException can retrieve the node.)
try{
WritePolicy wp = new WritePolicy();
wp.recordExistsAction=RecordExistsAction.CREATE_ONLY;
Bin b1 = Bin.asNull("b1");
client.put(wp, key1, b1);
}
catch(AerospikeException e){
System.out.println(e.getNode().getName());
}
Technically, the java client does know who the replica node is for this key. You will have to dig into the java client source code to figure that one out, as far as I know. Never seen a request for such info. Why does your application need it? May be there is another way to achieve what you are trying to achieve at the application level.
Hm, again this isn’t something that the client exposes by default so you will need to extend the WriteCommand and expose the protected partition so that you can access partition.replicas.
public class MyCommand extends WriteCommand {
public AtomicReferenceArray<Node>[] getReplicas() {
return this.partition.replicas;
}
}
Could you share what it is you need this information for? I assume this is for testing?
However, there is also a ClusterUtilities class which has functions like getting the partition id for a key, finding a key which has a master and replica of given nodes and I’ve just added what you’re after – given a key find the master and replica nodes.
There is also a main() function so it can be built as a jar and it exposes these utilities to the command line so you can use it as a helper for testing. Very useful if you want to test strong consistency in the face of a network split and you need to know how to force the network split to see what happens when the master and replica fall into different sub-clusters for example.