Skip to content

Commit

Permalink
feat(core/search): Improve search speed: faster validation that searc…
Browse files Browse the repository at this point in the history
…h keys exists (#1984)
  • Loading branch information
christopherthielen authored Oct 11, 2017
1 parent d237bbd commit 90e4818
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package com.netflix.spinnaker.clouddriver.aws.search

import com.netflix.spinnaker.cats.cache.Cache
import com.netflix.spinnaker.cats.cache.CacheData
import com.netflix.spinnaker.cats.cache.RelationshipCacheFilter
import com.netflix.spinnaker.clouddriver.aws.data.Keys
import com.netflix.spinnaker.clouddriver.aws.model.AmazonServerGroup
import com.netflix.spinnaker.clouddriver.aws.provider.view.AmazonClusterProvider
import com.netflix.spinnaker.clouddriver.cache.KeyProcessor
import com.netflix.spinnaker.clouddriver.model.view.ServerGroupViewModelPostProcessor
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.SERVER_GROUPS

@Component("AmazonServerGroupKeyProcessor")
class ServerGroupKeyProcessor implements KeyProcessor {

@Autowired
AmazonClusterProvider amazonClusterProvider

@Autowired(required = false)
ServerGroupViewModelPostProcessor serverGroupViewModelPostProcessor
private final Cache cacheView

@Override
Boolean canProcess(String type) {
Expand All @@ -30,6 +29,9 @@ class ServerGroupKeyProcessor implements KeyProcessor {
String region = parsed['region']
String name = parsed['serverGroup']

return amazonClusterProvider.getServerGroup(account, region, name) != null
String serverGroupKey = Keys.getServerGroupKey(name, account, region)
CacheData serverGroupData = cacheView.get(SERVER_GROUPS.ns, serverGroupKey, RelationshipCacheFilter.none())

return serverGroupData != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class CatsSearchProvider implements SearchProvider {
resultSet
}

private List<String> findMatches(String q, List<String> toQuery, Map<String, String> filters) {
private List<String> findMatches(String q, List<String> cachesToQuery, Map<String, String> filters) {

if (!q && keyParsers) {
// no keyword search so find sensible default value to set for searching
Expand All @@ -173,22 +173,26 @@ class CatsSearchProvider implements SearchProvider {
log.info("no query string specified, looked for sensible default and found: ${q}")
}

log.info("Querying ${toQuery} for term: ${q}")
log.info("Querying ${cachesToQuery} for term: ${q}")
String normalizedWord = q.toLowerCase()
List<String> matches = new ArrayList<String>()
toQuery.each { String cache ->
matches.addAll(cacheView.filterIdentifiers(cache, "*:${cache}:*${normalizedWord}*").findAll { String key ->
try {

// if the key represented in the cache doesn't actually exist, don't process it
if (keyProcessors && !keyProcessors.any { it.canProcess(cache) && it.exists(key) }) {
log.warn("found ${cache} key that did not exist: ${key}")
return false;
}
List<String> matches = cachesToQuery.collect { String cache ->
List<KeyProcessor> keyProcessors = (this.keyProcessors ?: []).findAll { it.canProcess(cache) }

// if the key represented in the cache doesn't actually exist, don't process it
Closure keyExists = { String key ->
boolean exists = keyProcessors.empty || keyProcessors.any { it.exists(key) }
if (!exists) {
log.warn("found ${cache} key that did not exist: ${key}")
}
return exists
}

Closure filtersMatch = { String key ->
try {
if (!filters) {
return true
}

if (keyParsers) {
KeyParser parser = keyParsers.find { it.cloudProvider == filters.cloudProvider && it.canParseType(cache) }
if (parser) {
Expand All @@ -205,8 +209,16 @@ class CatsSearchProvider implements SearchProvider {
} catch (Exception e) {
log.warn("Failed on $cache:$key", e)
}
})
}
}

Collection<String> identifiers = cacheView
.filterIdentifiers(cache, "*:${cache}:*${normalizedWord}*")
.findAll(keyExists)
.findAll(filtersMatch)

return identifiers
}.flatten()

matches.sort { String a, String b ->
def aKey = a.toLowerCase().substring(a.indexOf(':'))
def bKey = b.toLowerCase().substring(b.indexOf(':'))
Expand Down

0 comments on commit 90e4818

Please sign in to comment.