Skip to content

Commit

Permalink
[FLINK-19901][metrics] Fix caching offset for variables
Browse files Browse the repository at this point in the history
  • Loading branch information
zentol committed Nov 3, 2020
1 parent 6ed81d9 commit 477d37d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,14 @@ public Map<String, String> getAllVariables() {
}

public Map<String, String> getAllVariables(int reporterIndex, Set<String> excludedVariables) {
// offset cache location to account for general cache at position 0
reporterIndex += 1;
if (reporterIndex < 0 || reporterIndex >= logicalScopeStrings.length) {
reporterIndex = 0;
// invalid reporter index; either a programming mistake, or we try to retrieve variables outside of a reporter
reporterIndex = -1;
}

// offset cache location to account for general cache at position 0
reporterIndex += 1;

// if no variables are excluded (which is the default!) we re-use the general variables map to save space
return internalGetAllVariables(excludedVariables.isEmpty() ? 0 : reporterIndex, excludedVariables);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -93,6 +95,26 @@ public void testGetAllVariablesWithExclusions() {
assertEquals(group.getAllVariables(-1, Collections.singleton(ScopeFormat.SCOPE_HOST)).size(), 0);
}

@Test
public void testGetAllVariablesWithExclusionsForReporters() {
MetricRegistry registry = TestingMetricRegistry.builder().setNumberReporters(2).build();

AbstractMetricGroup<?> group = new GenericMetricGroup(registry, null, "test") {
@Override
protected void putVariables(Map<String, String> variables) {
variables.put("k1", "v1");
variables.put("k2", "v2");
}
};

group.getAllVariables(-1, Collections.emptySet());

assertThat(group.getAllVariables(0, Collections.singleton("k1")), not(IsMapContaining.hasKey("k1")));
assertThat(group.getAllVariables(0, Collections.singleton("k1")), IsMapContaining.hasKey("k2"));
assertThat(group.getAllVariables(1, Collections.singleton("k2")), IsMapContaining.hasKey("k1"));
assertThat(group.getAllVariables(1, Collections.singleton("k2")), not(IsMapContaining.hasKey("k2")));
}

// ========================================================================
// Scope Caching
// ========================================================================
Expand Down

0 comments on commit 477d37d

Please sign in to comment.