Created
December 18, 2017 19:51
-
-
Save Romeh/258b648fedafcae87ecfd97646d5ddb4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* a fail fast map reducer to decide if it should keep waiting for other jobs to final reduce or it should terminate | |
* and fail fast with the current responses if any failed | |
*/ | |
@Component | |
@Scope("prototype") | |
public class FailFastReducer implements IgniteReducer<ServiceResponse, MapReduceResponse> { | |
private final Map<String, ServiceResponse> responseMap = new ConcurrentHashMap<>(); | |
/** | |
* @param serviceCallResponse the job response | |
* @return return a boolean to decide it is time to reduce or not | |
*/ | |
@Override | |
public boolean collect(ServiceResponse serviceCallResponse) { | |
if (serviceCallResponse != null) { | |
if (serviceCallResponse.isSuccess()) { | |
responseMap.put(serviceCallResponse.getServiceOrigin(), serviceCallResponse); | |
return true; | |
} else { | |
responseMap.put(serviceCallResponse.getServiceOrigin(), serviceCallResponse); | |
return false; | |
} | |
} | |
return false; | |
} | |
/** | |
* @return the final generic reduced response containing the list of jobs responses and global status | |
*/ | |
@Override | |
public MapReduceResponse reduce() { | |
return MapReduceResponse.builder().success(checkStatus()).reducedResponses(responseMap).build(); | |
} | |
/** | |
* @return the generic reduced response status based into the single status of each single collected jobs response | |
*/ | |
public boolean checkStatus() { | |
boolean status = true; | |
for (Map.Entry<String, ServiceResponse> key : responseMap.entrySet()) { | |
status = status && responseMap.get(key.getKey()).isSuccess(); | |
} | |
return status; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment