Skip to content

Commit

Permalink
fix(google): don't leave orphaned applications in the cache (#4123)
Browse files Browse the repository at this point in the history
* 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<CacheData> 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.
  • Loading branch information
plumpy committed Oct 28, 2019
1 parent 1bbbf53 commit 64197ce
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,6 +34,27 @@ class CacheResultBuilder {

CacheMutation onDemand = new CacheMutation()

Set<String> 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<AgentDataType> dataTypes) {
authoritativeTypes = dataTypes.stream()
.filter({ dataType -> dataType.getAuthority() == AUTHORITATIVE })
.map({ dataType -> dataType.getTypeName() })
.collect(toImmutableSet())
}

Map<String, NamespaceBuilder> namespaceBuilders = [:].withDefault {
String ns -> new NamespaceBuilder(namespace: ns)
}
Expand All @@ -42,6 +67,9 @@ class CacheResultBuilder {
Map<String, Collection<CacheData>> keep = [:]
Map<String, Collection<String>> evict = [:]

authoritativeTypes.each { namespace ->
keep[namespace] = []
}
if (!onDemand.toKeep.empty) {
keep += [(ON_DEMAND.ns): onDemand.toKeep.values()]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<GoogleServerGroup> serverGroups = getServerGroups(providerCache);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<String, Collection<CacheData>> 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<String, Collection<String>> evictions = result.getEvictions();
assertThat(evictions).hasSize(1);
assertThat(evictions.get(ON_DEMAND.getNs())).containsExactly("evict1", "evict2");

Map<String, Collection<CacheData>> 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<String, Collection<CacheData>> 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");
}
}

0 comments on commit 64197ce

Please sign in to comment.