Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

segment hystrix command keys by cloud provider #224

Merged
merged 1 commit into from
May 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2016 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.spinnaker.gate.services

import com.google.common.cache.CacheBuilder
import com.google.common.cache.CacheLoader
import com.google.common.cache.LoadingCache
import com.google.common.util.concurrent.UncheckedExecutionException
import com.netflix.spinnaker.gate.services.internal.ClouddriverService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

import java.util.concurrent.ExecutionException
import java.util.concurrent.TimeUnit

/**
* DefaultProviderLookupService.
*/
@Component("providerLookupService")
class DefaultProviderLookupService implements ProviderLookupService {

private static final String FALLBACK = "unknown"
private static final String ACCOUNTS_KEY = "all"

private final ClouddriverService clouddriverService

private final LoadingCache<String, List<ClouddriverService.Account>> accountsCache = CacheBuilder.newBuilder()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just to debounce calls to translate account name -> provider name so every hystrix command key generation doesn't result in another remote call

.initialCapacity(1)
.maximumSize(1)
.refreshAfterWrite(5, TimeUnit.SECONDS)
.build(new CacheLoader<String, List<ClouddriverService.Account>>() {
@Override
List<ClouddriverService.Account> load(String key) throws Exception {
return clouddriverService.accounts
}
})

@Autowired
public DefaultProviderLookupService(ClouddriverService clouddriverService) {
this.clouddriverService = clouddriverService
}

public String providerForAccount(String account) {
try {
return accountsCache.get(ACCOUNTS_KEY)?.find { it.name == account }?.type ?: FALLBACK
} catch (ExecutionException | UncheckedExecutionException ex) {
return FALLBACK
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2016 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.spinnaker.gate.services

interface ProviderLookupService {
String providerForAccount(String account)
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,23 @@ class ClusterService {
@Autowired
ClouddriverService clouddriverService

@Autowired
ProviderLookupService providerLookupService

Map getClusters(String app) {
HystrixFactory.newMapCommand(GROUP, "getClustersForApplication") {
clouddriverService.getClusters(app)
} execute()
}

List<Map> getClustersForAccount(String app, String account) {
HystrixFactory.newListCommand(GROUP, "getClustersForApplicationInAccount") {
HystrixFactory.newListCommand(GROUP, "getClustersForApplicationInAccount-${providerLookupService.providerForAccount(account)}") {
clouddriverService.getClustersForAccount(app, account)
} execute()
}

Map getCluster(String app, String account, String clusterName) {
HystrixFactory.newMapCommand(GROUP, "getCluster") {
HystrixFactory.newMapCommand(GROUP, "getCluster-${providerLookupService.providerForAccount(account)}") {
try {
clouddriverService.getCluster(app, account, clusterName)?.getAt(0) as Map
} catch (RetrofitError e) {
Expand All @@ -66,13 +69,13 @@ class ClusterService {
}

List<Map> getScalingActivities(String app, String account, String clusterName, String serverGroupName, String provider, String region) {
HystrixFactory.newListCommand(GROUP, "getScalingActivitiesForCluster") {
HystrixFactory.newListCommand(GROUP, "getScalingActivitiesForCluster-${providerLookupService.providerForAccount(account)}") {
clouddriverService.getScalingActivities(app, account, clusterName, provider, serverGroupName, region)
} execute()
}

Map getTargetServerGroup(String app, String account, String clusterName, String cloudProviderType, String scope, String target, Boolean onlyEnabled, Boolean validateOldest) {
HystrixFactory.newMapCommand(GROUP, "getTargetServerGroup") {
HystrixFactory.newMapCommand(GROUP, "getTargetServerGroup-${providerLookupService.providerForAccount(account)}") {
try {
return clouddriverService.getTargetServerGroup(app, account, clusterName, cloudProviderType, scope, target, onlyEnabled, validateOldest)
} catch (RetrofitError re) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@ class ImageService {
@Autowired
ClouddriverService clouddriverService

@Autowired
ProviderLookupService providerLookupService

List<Map> getForAccountAndRegion(String provider, String account, String region, String imageId) {
HystrixFactory.newListCommand(GROUP, "getImagesForAccountAndRegion") {
HystrixFactory.newListCommand(GROUP, "getImagesForAccountAndRegion-${providerLookupService.providerForAccount(account)}") {
clouddriverService.getImageDetails(provider, account, region, imageId)
} execute()
}

List<Map> search(String provider, String query, String region, String account) {
HystrixFactory.newListCommand(GROUP, "searchImages") {
HystrixFactory.newListCommand(GROUP, "searchImages-${providerLookupService.providerForAccount(account)}") {
clouddriverService.findImages(provider, query, region, account)
} execute()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ class InstanceService {
@Autowired
InsightConfiguration insightConfiguration

@Autowired
ProviderLookupService providerLookupService

Map getForAccountAndRegion(String account, String region, String instanceId) {
HystrixFactory.newMapCommand(GROUP, "getInstancesForAccountAndRegion") {
HystrixFactory.newMapCommand(GROUP, "getInstancesForAccountAndRegion-${providerLookupService.providerForAccount(account)}") {
def instanceDetails = clouddriverService.getInstanceDetails(account, region, instanceId)
def instanceContext = instanceDetails.collectEntries {
return it.value instanceof String ? [it.key, it.value] : [it.key, ""]
Expand All @@ -50,7 +53,7 @@ class InstanceService {
}

Map getConsoleOutput(String account, String region, String instanceId, String provider) {
HystrixFactory.newMapCommand(GROUP, "getConsoleOutput") {
HystrixFactory.newMapCommand(GROUP, "getConsoleOutput-${providerLookupService.providerForAccount(account)}") {
return clouddriverService.getConsoleOutput(account, region, instanceId, provider)
} execute()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class JobService {
@Autowired
InsightConfiguration insightConfiguration

@Autowired
ProviderLookupService providerLookupService

List getForApplication(String applicationName, String expand) {
String commandKey = Boolean.valueOf(expand) ? "getExpandedJobsForApplication" : "getJobsForApplication"
HystrixFactory.newListCommand(GROUP, commandKey) {
Expand All @@ -43,7 +46,7 @@ class JobService {
}

Map getForApplicationAndAccountAndRegion(String applicationName, String account, String region, String name) {
HystrixFactory.newMapCommand(GROUP, "getJobsForApplicationAccountAndRegion", {
HystrixFactory.newMapCommand(GROUP, "getJobsForApplicationAccountAndRegion-${providerLookupService.providerForAccount(account)}", {
try {
def context = getContext(applicationName, account, region, name)
return clouddriverService.getJobDetails(applicationName, account, region, name) + [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,26 @@ class LoadBalancerService {
ClouddriverService clouddriverService

List<Map> getAll(String provider = "aws") {
HystrixFactory.newListCommand(GROUP, "getAllLoadBalancersForProvider") {
HystrixFactory.newListCommand(GROUP, "getAllLoadBalancersForProvider-$provider") {
clouddriverService.getLoadBalancers(provider)
} execute()
}

Map get(String name, String provider = "aws") {
HystrixFactory.newMapCommand(GROUP, "getLoadBalancer") {
HystrixFactory.newMapCommand(GROUP, "getLoadBalancer-$provider") {
clouddriverService.getLoadBalancer(provider, name)
} execute()
}

List<Map> getDetailsForAccountAndRegion(String account, String region, String name, String provider = "aws") {
HystrixFactory.newListCommand(GROUP, "getLoadBalancerDetails") {
HystrixFactory.newListCommand(GROUP, "getLoadBalancerDetails-$provider") {
clouddriverService.getLoadBalancerDetails(provider, account, region, name)
} execute()
}

List getClusterLoadBalancers(String appName, String account, String provider, String clusterName) {
HystrixFactory.newListCommand(GROUP,
"getClusterLoadBalancers") {
"getClusterLoadBalancers-$provider") {
clouddriverService.getClusterLoadBalancers(appName, account, clusterName, provider)
} execute()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class NetworkService {
}

List<Map> getNetworks(String cloudProvider) {
command("networks") {
command("networks-$cloudProvider") {
clouddriverService.getNetworks(cloudProvider)
} execute()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class SecurityGroupService {
* @param region optional. nullable
*/
Map getForAccountAndProviderAndRegion(String account, String provider, String region) {
HystrixFactory.newMapCommand(GROUP, "getSecurityGroupsForAccountAndProvider") {
HystrixFactory.newMapCommand(GROUP, "getSecurityGroupsForAccountAndProvider-$provider") {
clouddriverService.getSecurityGroups(account, provider, region)
} execute()
}
Expand All @@ -87,7 +87,7 @@ class SecurityGroupService {
* @param region optional. nullable
*/
Map getSecurityGroup(String account, String provider, String name, String region, String vpcId = null) {
HystrixFactory.newMapCommand(GROUP, "getSecurityGroupByIdentifiers") {
HystrixFactory.newMapCommand(GROUP, "getSecurityGroupByIdentifiers-$provider") {
clouddriverService.getSecurityGroup(account, provider, name, region, vpcId)
} execute()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class ServerGroupService {
@Autowired
InsightConfiguration insightConfiguration

@Autowired
ProviderLookupService providerLookupService

List getForApplication(String applicationName, String expand) {
String commandKey = Boolean.valueOf(expand) ? "getExpandedServerGroupsForApplication" : "getServerGroupsForApplication"
HystrixFactory.newListCommand(GROUP, commandKey) {
Expand All @@ -44,7 +47,7 @@ class ServerGroupService {
}

Map getForApplicationAndAccountAndRegion(String applicationName, String account, String region, String serverGroupName) {
HystrixFactory.newMapCommand(GROUP, "getServerGroupsForApplicationAccountAndRegion") {
HystrixFactory.newMapCommand(GROUP, "getServerGroupsForApplicationAccountAndRegion-${providerLookupService.providerForAccount(account)}") {
try {
def context = getContext(applicationName, account, region, serverGroupName)
return clouddriverService.getServerGroupDetails(applicationName, account, region, serverGroupName) + [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class InstanceServiceSpec extends Specification {
clouddriverService: Mock(ClouddriverService) {
1 * getInstanceDetails(_, _, _) >> { return [privateIpAddress: "10.0.0.1", map: [:]] }
},
providerLookupService: Stub(ProviderLookupService) {
providerForAccount(_) >> "test"
},
insightConfiguration: new InsightConfiguration(
instance: [new InsightConfiguration.Link(url: '${account}-${region}-${instanceId}-{DNE}-${privateIpAddress}')]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class ServerGroupServiceSpec extends Specification {
clouddriverService: Mock(ClouddriverService) {
1 * getServerGroupDetails(_, _, _, _) >> { return [:] }
},
providerLookupService: Stub(ProviderLookupService) {
providerForAccount(_) >> "test"
},
insightConfiguration: new InsightConfiguration(
serverGroup: [new InsightConfiguration.Link(url: '${application}-${account}-${region}-${serverGroup}-{DNE}')]
)
Expand Down