debugjavaMinor
Simplifying asynchronous "executeAsync" method along with "onFailure" callback
Viewed 0 times
methodasynchronouswithcallbackonfailuresimplifyingalongexecuteasync
Problem
I have a system in which user id is "sharded" across all the machines which means each machine is responsible for certain user id data. I am working on a library which will take
Customer will pass
We have three datacenters with 5 machines in each datacenters. And each user data is in two machines (primary and secondary) in each datacenters. Let's say if somebody is calling my library from DC1, then I will first call my primary machine server in DC1 only and depending on what flags they have enabled in DataKey, I will call other machines if
DataKey builder object which contains user id, timeout, and some other stuff. I will be using this user id to figure out which machines have the data for this user id.Customer will pass
DataKey builder object to my executeAsync method and then I will use DataKey builder object to figure out the machines which has the data for this user id and then I will use those machines to make the call and get the data for them. For example, DataKey object has user id 12345 so below are steps which I will be executing.- Get all the
hostnameswhich will have the data for this user id 12345. It should be linked list.
- Now, pick the first machine from the
hostnameslist and generate the URL for that and execute the URL. If response is successful then make a DataResponse object and return the response and we will not log anything. But if the response is not successful like, server returned back any HttpClientErrorException or HttpServerErrorException, then I need to log it and make DataResponse object accordingly and return it.
- But let's say if the first machine in that list was down for some reason then I will log it that this machine is down and then I will try calling second machine in the
hostnameslist and if it is also down, then I will try third or fourth and keep doing the same thing. And suppose if all the machines are down in thathostnameslist, then I will make DataResponse object saying all servers are down and log it as well.
We have three datacenters with 5 machines in each datacenters. And each user data is in two machines (primary and secondary) in each datacenters. Let's say if somebody is calling my library from DC1, then I will first call my primary machine server in DC1 only and depending on what flags they have enabled in DataKey, I will call other machines if
Solution
If I'm having the logic correctly, I believe you can move from this:
To this:
You don't need your hostIds in
becomes
if (remoteFlag && secondaryFlag) {
hostnames.add(localPrimaryHostname);
hostnames.add(localSecondaryHostname);
hostnames.add(remotePrimaryHostname);
hostnames.add(remoteSecondaryHostname);
} else if (remoteFlag && !secondaryFlag) {
hostnames.add(localPrimaryHostname);
hostnames.add(remotePrimaryHostname);
} else if (!remoteFlag && !secondaryFlag) {
hostnames.add(localPrimaryHostname);
} else if (!remoteFlag && secondaryFlag) {
hostnames.add(localPrimaryHostname);
hostnames.add(localSecondaryHostname);
}To this:
hostnames.add(localPrimaryHostname);
if(remoteFlag) {
hostnames.add(remotePrimaryHostname);
if(secondaryFlag) {
hostnames.add(remoteSecondaryHostname);
}
}
if(secondaryFlag) {
hostnames.add(localSecondaryHostname);
}You don't need your hostIds in
String representation so you might as well immediately parse them:String localPrimaryHostId = mappings.primaryHostIdToPartitionMapping.get(localDataCenterPath).get(shardId);
localPrimaryHostname = mappings.hostIdToMachineMapping.get(localDataCenterPath).get(Integer.parseInt(localPrimaryHostId));becomes
int localPrimaryHostId = Integer.parseInt(mappings.primaryHostIdToPartitionMapping.get(localDataCenterPath).get(shardId));
localPrimaryHostname = mappings.hostIdToMachineMapping.get(localDataCenterPath).get(localPrimaryHostId);Code Snippets
if (remoteFlag && secondaryFlag) {
hostnames.add(localPrimaryHostname);
hostnames.add(localSecondaryHostname);
hostnames.add(remotePrimaryHostname);
hostnames.add(remoteSecondaryHostname);
} else if (remoteFlag && !secondaryFlag) {
hostnames.add(localPrimaryHostname);
hostnames.add(remotePrimaryHostname);
} else if (!remoteFlag && !secondaryFlag) {
hostnames.add(localPrimaryHostname);
} else if (!remoteFlag && secondaryFlag) {
hostnames.add(localPrimaryHostname);
hostnames.add(localSecondaryHostname);
}hostnames.add(localPrimaryHostname);
if(remoteFlag) {
hostnames.add(remotePrimaryHostname);
if(secondaryFlag) {
hostnames.add(remoteSecondaryHostname);
}
}
if(secondaryFlag) {
hostnames.add(localSecondaryHostname);
}String localPrimaryHostId = mappings.primaryHostIdToPartitionMapping.get(localDataCenterPath).get(shardId);
localPrimaryHostname = mappings.hostIdToMachineMapping.get(localDataCenterPath).get(Integer.parseInt(localPrimaryHostId));int localPrimaryHostId = Integer.parseInt(mappings.primaryHostIdToPartitionMapping.get(localDataCenterPath).get(shardId));
localPrimaryHostname = mappings.hostIdToMachineMapping.get(localDataCenterPath).get(localPrimaryHostId);Context
StackExchange Code Review Q#85347, answer score: 6
Revisions (0)
No revisions yet.