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 from older version of micro-infra-s…
Browse files Browse the repository at this point in the history
…pring, should degrade and emulate new format
  • Loading branch information
nurkiewicz committed Dec 29, 2014
1 parent ff01cb3 commit 99657ad
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.ofg.infrastructure.healthcheck

import com.google.common.base.Optional
import com.google.common.base.Optional as GuavaOptional
import com.ofg.infrastructure.discovery.ServiceAlias
import com.ofg.infrastructure.discovery.ServicePath
import com.ofg.infrastructure.discovery.ServiceResolver
import groovy.transform.CompileStatic
Expand Down Expand Up @@ -67,14 +68,39 @@ class CollaboratorsConnectivityController {
}

private Map checkCollaborators(URI url) {
Optional<Map> collaborators = pingClient.checkCollaborators(url)
//todo Check older version of /collaborators, degrade gracefully
Optional<Map> collaborators = pingClient
.checkCollaborators(url)
.transform({tryAdjustLegacyCollaboratorsResponse(it)})
return [
status: CollaboratorStatus.of(collaborators.isPresent()),
collaborators: collaborators.or([:])
]
}

private Map tryAdjustLegacyCollaboratorsResponse(Map collaboratorsResponse) {
if (isLegacyResponse(collaboratorsResponse)) {
return adjustLegacyCollaboratorsResponse(collaboratorsResponse)
} else {
return collaboratorsResponse;
}
}

private Map adjustLegacyCollaboratorsResponse(Map collaboratorsResponse) {
collaboratorsResponse.collectEntries {alias, statusStr ->
final ServicePath path = serviceResolver.resolveAlias(new ServiceAlias(alias as String))
final Set<URI> allInstances = serviceResolver.fetchAllUris(path)
CollaboratorStatus status = CollaboratorStatus.of(statusStr == 'CONNECTED')
return [
path.path, allInstances.collectEntries{uri -> [uri, status]}
]
}
}

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

private String checkConnectionStatus(URI url) {
final GuavaOptional<String> pingResult = pingClient.ping(url)
return CollaboratorStatus.of(pingResult == GuavaOptional.of('OK'))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.ofg.infrastructure.healthcheck

import com.google.common.base.Optional as GuavaOptional
import com.ofg.infrastructure.discovery.ServiceAlias
import com.ofg.infrastructure.discovery.ServicePath
import com.ofg.infrastructure.discovery.ServiceResolver
import org.apache.commons.lang.StringUtils
import spock.lang.Specification

import static com.ofg.infrastructure.healthcheck.CollaboratorStatus.DOWN
Expand Down Expand Up @@ -71,7 +73,9 @@ class CollaboratorsConnectivityControllerSpec extends Specification {
private void instancesOfMicroservices(Map<String, List<String>> instances) {
serviceResolverMock.fetchAllServices() >> instances.keySet().collect { new ServicePath(it) }.toSet()
instances.each { path, urls ->
ServicePath wrappedPath = new ServicePath(path)
final ServicePath wrappedPath = new ServicePath(path)
final ServiceAlias alias = new ServiceAlias(StringUtils.substringAfterLast(path, '/'))
serviceResolverMock.resolveAlias(alias) >> wrappedPath
serviceResolverMock.fetchAllUris(wrappedPath) >> urls.toSet()
}
}
Expand Down Expand Up @@ -175,4 +179,36 @@ class CollaboratorsConnectivityControllerSpec extends Specification {
info['/com/micro1'] == [:]
}

def 'should adjust collaborators response from micro-infra-spring version < 0.7.4'() {
given:
instancesOfMicroservices(['/com/micro1': [MICRO_1A_URL],
'/com/micro3': [MICRO_3A_URL, MICRO_3B_URL]])
serviceReturnsLegacyCollaboratorsFormat(MICRO_1A_URL)
noCollaboratorsOfRemainingServices()

when:
Map info = controller.allCollaboratorsConnectivityInfo

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

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

}

0 comments on commit 99657ad

Please sign in to comment.