From 64197ce0aa5a3b1830a946aa7c5e657e7d241ba4 Mon Sep 17 00:00:00 2001 From: Michael Plump Date: Mon, 28 Oct 2019 14:14:59 -0400 Subject: [PATCH] fix(google): don't leave orphaned applications in the cache (#4123) * refactor(google): convert CacheResultBuilderSpec to Java * test(google): add another test for CacheResultBuilderTest * refactor(google): keep an empty list around for authoritative types If a AbstractGoogleServerGroupCachingAgent has some applications stored, but in the next run it finds no applications, it doesn't put anything under the "applications" key. DefaultProviderCache then has some weird behavior where it removes all relationships associated with that application, but leaves the existing application in the cache. That means we'll just repeat that behavior again the next time the caching agent runs. The application data is just orphaned but triggers relationship evictions every single caching cycle. Amazon doesn't have this problem because they always stick a List under the applications key, even if it's empty. This is enough to tell putCacheResult() to store that empty list, overwriting whatever was there before. The orphaned data isn't the cause of spinnaker/spinnaker#4511, but it dramatically exacerbates that issue. (The real issue is that every single regional/zonal caching agent asserts ownership over the application and will happily delete it, which triggers deletions of relationships that were put there by other caching agents.) I was working on a larger refactor of CATS to make this bug less likely to happen, but since we're about to cut a release branch, I'd rather just check in this smaller fix. --- .../google/cache/CacheResultBuilder.groovy | 28 ++++ ...AbstractGoogleServerGroupCachingAgent.java | 2 +- .../cache/CacheResultBuilderSpec.groovy | 48 ------- .../google/cache/CacheResultBuilderTest.java | 123 ++++++++++++++++++ 4 files changed, 152 insertions(+), 49 deletions(-) delete mode 100644 clouddriver-google/src/test/groovy/com/netflix/spinnaker/clouddriver/google/cache/CacheResultBuilderSpec.groovy create mode 100644 clouddriver-google/src/test/groovy/com/netflix/spinnaker/clouddriver/google/cache/CacheResultBuilderTest.java diff --git a/clouddriver-google/src/main/groovy/com/netflix/spinnaker/clouddriver/google/cache/CacheResultBuilder.groovy b/clouddriver-google/src/main/groovy/com/netflix/spinnaker/clouddriver/google/cache/CacheResultBuilder.groovy index 133b8d16978..72be39a307f 100644 --- a/clouddriver-google/src/main/groovy/com/netflix/spinnaker/clouddriver/google/cache/CacheResultBuilder.groovy +++ b/clouddriver-google/src/main/groovy/com/netflix/spinnaker/clouddriver/google/cache/CacheResultBuilder.groovy @@ -16,11 +16,15 @@ package com.netflix.spinnaker.clouddriver.google.cache +import com.google.common.collect.ImmutableSet +import com.netflix.spinnaker.cats.agent.AgentDataType import com.netflix.spinnaker.cats.agent.DefaultCacheResult import com.netflix.spinnaker.cats.cache.CacheData import com.netflix.spinnaker.cats.cache.DefaultCacheData import groovy.util.logging.Slf4j +import static com.google.common.collect.ImmutableSet.toImmutableSet +import static com.netflix.spinnaker.cats.agent.AgentDataType.Authority.AUTHORITATIVE import static com.netflix.spinnaker.clouddriver.google.cache.Keys.Namespace.ON_DEMAND @Slf4j @@ -30,6 +34,27 @@ class CacheResultBuilder { CacheMutation onDemand = new CacheMutation() + Set authoritativeTypes = ImmutableSet.of() + + CacheResultBuilder() {} + + /** + * Create a CacheResultBuilder for the given dataTypes. + * + * Any authoritative types in dataTypes are guaranteed to be listed in the + * output. If you say you are authoritative for "clusters", but don't include + * any data under that namespace, an empty list will be included in the + * result. (Whereas if you don't pass dataTypes to the constructor, "clusters" + * will just be missing from the result if you don't specify any, and any + * existing clusters will remain in the cache). + */ + CacheResultBuilder(Collection dataTypes) { + authoritativeTypes = dataTypes.stream() + .filter({ dataType -> dataType.getAuthority() == AUTHORITATIVE }) + .map({ dataType -> dataType.getTypeName() }) + .collect(toImmutableSet()) + } + Map namespaceBuilders = [:].withDefault { String ns -> new NamespaceBuilder(namespace: ns) } @@ -42,6 +67,9 @@ class CacheResultBuilder { Map> keep = [:] Map> evict = [:] + authoritativeTypes.each { namespace -> + keep[namespace] = [] + } if (!onDemand.toKeep.empty) { keep += [(ON_DEMAND.ns): onDemand.toKeep.values()] } diff --git a/clouddriver-google/src/main/groovy/com/netflix/spinnaker/clouddriver/google/provider/agent/AbstractGoogleServerGroupCachingAgent.java b/clouddriver-google/src/main/groovy/com/netflix/spinnaker/clouddriver/google/provider/agent/AbstractGoogleServerGroupCachingAgent.java index 1cb197d6dd2..4f9c3462e6b 100644 --- a/clouddriver-google/src/main/groovy/com/netflix/spinnaker/clouddriver/google/provider/agent/AbstractGoogleServerGroupCachingAgent.java +++ b/clouddriver-google/src/main/groovy/com/netflix/spinnaker/clouddriver/google/provider/agent/AbstractGoogleServerGroupCachingAgent.java @@ -150,7 +150,7 @@ abstract class AbstractGoogleServerGroupCachingAgent public CacheResult loadData(ProviderCache providerCache) { try { - CacheResultBuilder cacheResultBuilder = new CacheResultBuilder(); + CacheResultBuilder cacheResultBuilder = new CacheResultBuilder(DATA_TYPES); cacheResultBuilder.setStartTime(System.currentTimeMillis()); List serverGroups = getServerGroups(providerCache); diff --git a/clouddriver-google/src/test/groovy/com/netflix/spinnaker/clouddriver/google/cache/CacheResultBuilderSpec.groovy b/clouddriver-google/src/test/groovy/com/netflix/spinnaker/clouddriver/google/cache/CacheResultBuilderSpec.groovy deleted file mode 100644 index d0b0027969c..00000000000 --- a/clouddriver-google/src/test/groovy/com/netflix/spinnaker/clouddriver/google/cache/CacheResultBuilderSpec.groovy +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2016 Google, 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.clouddriver.google.cache - -import spock.lang.Specification - -class CacheResultBuilderSpec extends Specification { - - def "should build a cache result"() { - given: - CacheResultBuilder crb = new CacheResultBuilder() - - crb.namespace("applications").keep("appKey").with { - attributes.santa = "clause" - relationships["clusters"].add("clusterKey") - } - crb.namespace("clusters").keep("clusterKey").with { - attributes.xmen = "wolverine" - relationships["foo"].add("bar") - } - - when: - def result = crb.build() - - then: - !result.cacheResults.empty - result.cacheResults.applications[0].id == "appKey" - result.cacheResults.applications[0].attributes.santa == "clause" - result.cacheResults.applications[0].relationships.clusters[0] == "clusterKey" - result.cacheResults.clusters[0].id == "clusterKey" - result.cacheResults.clusters[0].attributes.xmen == "wolverine" - result.cacheResults.clusters[0].relationships.foo[0] == "bar" - } -} diff --git a/clouddriver-google/src/test/groovy/com/netflix/spinnaker/clouddriver/google/cache/CacheResultBuilderTest.java b/clouddriver-google/src/test/groovy/com/netflix/spinnaker/clouddriver/google/cache/CacheResultBuilderTest.java new file mode 100644 index 00000000000..720fc5f84a7 --- /dev/null +++ b/clouddriver-google/src/test/groovy/com/netflix/spinnaker/clouddriver/google/cache/CacheResultBuilderTest.java @@ -0,0 +1,123 @@ +/* + * Copyright 2019 Google, LLC + * + * 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.clouddriver.google.cache; + +import static com.google.common.collect.Iterables.getOnlyElement; +import static com.netflix.spinnaker.cats.agent.AgentDataType.Authority.AUTHORITATIVE; +import static com.netflix.spinnaker.cats.agent.AgentDataType.Authority.INFORMATIVE; +import static com.netflix.spinnaker.clouddriver.google.cache.Keys.Namespace.ON_DEMAND; +import static org.assertj.core.api.Assertions.assertThat; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.netflix.spinnaker.cats.agent.DefaultCacheResult; +import com.netflix.spinnaker.cats.cache.CacheData; +import com.netflix.spinnaker.cats.cache.DefaultCacheData; +import com.netflix.spinnaker.clouddriver.google.cache.CacheResultBuilder.CacheDataBuilder; +import java.util.Collection; +import java.util.Map; +import org.junit.jupiter.api.Test; +import org.junit.platform.runner.JUnitPlatform; +import org.junit.runner.RunWith; + +@RunWith(JUnitPlatform.class) +final class CacheResultBuilderTest { + + @Test + public void testBuild() { + CacheResultBuilder cacheResultBuilder = new CacheResultBuilder(); + + CacheDataBuilder appBuilder = cacheResultBuilder.namespace("applications").keep("appKey"); + appBuilder.setAttributes(ImmutableMap.of("santa", "claus")); + appBuilder.setRelationships(ImmutableMap.of("clusters", ImmutableList.of("clusterKey"))); + CacheDataBuilder clusterBuilder = cacheResultBuilder.namespace("clusters").keep("clusterKey"); + clusterBuilder.setAttributes(ImmutableMap.of("xmen", "wolverine")); + clusterBuilder.setRelationships(ImmutableMap.of("foo", ImmutableList.of("bar"))); + + Map> cacheResults = cacheResultBuilder.build().getCacheResults(); + + assertThat(cacheResults).isNotEmpty(); + assertThat(cacheResults.get("applications")).hasSize(1); + CacheData application = getOnlyElement(cacheResults.get("applications")); + assertThat(application.getId()).isEqualTo("appKey"); + assertThat(application.getAttributes().get("santa")).isEqualTo("claus"); + assertThat(application.getRelationships().get("clusters")).containsExactly("clusterKey"); + assertThat(cacheResults.get("clusters")).hasSize(1); + CacheData cluster = getOnlyElement(cacheResults.get("clusters")); + assertThat(cluster.getId()).isEqualTo("clusterKey"); + assertThat(cluster.getAttributes().get("xmen")).isEqualTo("wolverine"); + assertThat(cluster.getRelationships().get("foo")).containsExactly("bar"); + } + + @Test + public void testOnDemandEntries() { + CacheResultBuilder cacheResultBuilder = new CacheResultBuilder(); + + cacheResultBuilder.getOnDemand().getToEvict().add("evict1"); + cacheResultBuilder.getOnDemand().getToEvict().add("evict2"); + cacheResultBuilder + .getOnDemand() + .getToKeep() + .put( + "applications", + new DefaultCacheData( + "appKey", + ImmutableMap.of("santa", "claus"), + ImmutableMap.of("clusters", ImmutableList.of("clusterKey")))); + + DefaultCacheResult result = cacheResultBuilder.build(); + + Map> evictions = result.getEvictions(); + assertThat(evictions).hasSize(1); + assertThat(evictions.get(ON_DEMAND.getNs())).containsExactly("evict1", "evict2"); + + Map> cacheResults = result.getCacheResults(); + assertThat(cacheResults).hasSize(1); + assertThat(cacheResults.get(ON_DEMAND.getNs())).hasSize(1); + CacheData application = getOnlyElement(cacheResults.get(ON_DEMAND.getNs())); + assertThat(application.getId()).isEqualTo("appKey"); + assertThat(application.getAttributes().get("santa")).isEqualTo("claus"); + assertThat(application.getRelationships().get("clusters")).containsExactly("clusterKey"); + } + + @Test + public void keepsEmptyListForAuthoritativeTypes() { + CacheResultBuilder cacheResultBuilder = + new CacheResultBuilder( + ImmutableSet.of( + AUTHORITATIVE.forType("auth1"), + AUTHORITATIVE.forType("auth2"), + INFORMATIVE.forType("inf1"), + INFORMATIVE.forType("inf2"))); + + cacheResultBuilder + .namespace("auth2") + .keep("id2") + .setAttributes(ImmutableMap.of("attr2", "value2")); + cacheResultBuilder + .namespace("auth3") + .keep("id3") + .setAttributes(ImmutableMap.of("attr3", "value3")); + + Map> cacheResults = cacheResultBuilder.build().getCacheResults(); + assertThat(cacheResults.get("auth1")).isEmpty(); + // Just to make sure the dataTypes constructor doesn't mess anything else up + assertThat(cacheResults.get("auth2")).extracting(CacheData::getId).containsExactly("id2"); + assertThat(cacheResults.get("auth3")).extracting(CacheData::getId).containsExactly("id3"); + } +}