Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
[#79] When calling /collaborators fails completely, fall back to /ping
Browse files Browse the repository at this point in the history
This can happen for very old versions of micro-infra-spring that do not support /collaborators.
Closes #79
  • Loading branch information
nurkiewicz committed Dec 21, 2014
1 parent cb02729 commit 909cf9c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class CollaboratorsConnectivityController {
private Map<String, String> statusOfAllCollaboratorInstances(ServicePath service) {
Set<URI> allUrisOfService = serviceResolver.fetchAllUris(service)
return allUrisOfService.collectEntries { URI instanceUrl ->
return [instanceUrl, checkConnectionStatus(instanceUrl)]
boolean status = checkConnectionStatus(instanceUrl)
return [instanceUrl, CollaboratorStatus.of(status)]
}
}

Expand All @@ -68,15 +69,34 @@ class CollaboratorsConnectivityController {
}

private Map checkCollaborators(URI url) {
Optional<Map> collaborators = pingClient
.checkCollaborators(url)
.transform({tryAdjustLegacyCollaboratorsResponse(it)})
Optional<Map> collaborators = establishCollaboratorsStatus(url)
return [
status: CollaboratorStatus.of(collaborators.isPresent()),
collaborators: collaborators.or([:])
]
}

private Optional<Map> establishCollaboratorsStatus(URI url) {
Optional<Map> collaborators = tryCallingCollaborators(url)
return fallbackWithPingIfCollaboratorsFailed(collaborators, url)
}

Optional<Map> fallbackWithPingIfCollaboratorsFailed(Optional<Map> maybeCollaborators, URI url) {
if (maybeCollaborators.isPresent()) {
return maybeCollaborators
}
return checkConnectionStatus(url) ?
Optional.of([:]) :
Optional.absent()
}

private Optional<Map> tryCallingCollaborators(URI url) {
Optional<Map> collaborators = pingClient
.checkCollaborators(url)
.transform({ tryAdjustLegacyCollaboratorsResponse(it) })
return collaborators
}

private Map tryAdjustLegacyCollaboratorsResponse(Map collaboratorsResponse) {
if (isLegacyResponse(collaboratorsResponse)) {
return adjustLegacyCollaboratorsResponse(collaboratorsResponse)
Expand All @@ -98,12 +118,12 @@ class CollaboratorsConnectivityController {

private boolean isLegacyResponse(Map collaboratorsResponse) {
return !collaboratorsResponse.empty &&
collaboratorsResponse.values().any{!it instanceof Map}
collaboratorsResponse.values().any{!(it instanceof Map)}
}

private String checkConnectionStatus(URI url) {
private boolean checkConnectionStatus(URI url) {
final GuavaOptional<String> pingResult = pingClient.ping(url)
return CollaboratorStatus.of(pingResult == GuavaOptional.of('OK'))
return pingResult == GuavaOptional.of('OK')
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ class CollaboratorsConnectivityControllerSpec extends Specification {
then:
info.size() == 2
info['/com/micro1'] == [
(MICRO_1A_URL): 'UP',
(MICRO_1B_URL): 'DOWN'
(MICRO_1A_URL): UP,
(MICRO_1B_URL): DOWN
]
info['/com/micro2'] == [
(MICRO_2_URL): 'UP'
(MICRO_2_URL): UP
]
}

Expand Down Expand Up @@ -207,6 +207,24 @@ class CollaboratorsConnectivityControllerSpec extends Specification {
collaborators: [:]]]
}

def 'when /collaborators is unavailable, try to at least call /ping and degrade response gracefully'() {
given:
instancesOfMicroservices(['/com/micro1': [MICRO_1A_URL]])
collaboratorsCheckFailed(MICRO_1A_URL)
and:
theseAreOk(MICRO_1A_URL)

when:
Map info = controller.allCollaboratorsConnectivityInfo

then:
info.size() == 1
info['/com/micro1'] == [
(MICRO_1A_URL): [
status : UP,
collaborators: [:]]]
}

private serviceReturnsLegacyCollaboratorsFormat(URI uri) {
collaboratorsStatusOf(uri, ['micro3': 'CONNECTED'])
}
Expand Down

0 comments on commit 909cf9c

Please sign in to comment.