Skip to content

Commit

Permalink
Convert Metadata templates return type to Map (elastic#86776)
Browse files Browse the repository at this point in the history
The methods for getting templates from Metadata expose the underlying
ImmutableOpenMap, but the callers all just need a Map. This commit
converts the return type of these methods and adjusts the few places
assuming it was an ImmutableOpenMap.

relates elastic#86239
  • Loading branch information
rjernst authored May 18, 2022
1 parent 0418e8a commit d6519b4
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ private interface RandomPart<T> {
/**
* Returns list of parts from metadata
*/
ImmutableOpenMap<String, T> parts(Metadata metadata);
Map<String, T> parts(Metadata metadata);

/**
* Puts the part back into metadata
Expand Down Expand Up @@ -502,7 +502,7 @@ private interface RandomPart<T> {
*/
private <T> Metadata randomParts(Metadata metadata, String prefix, RandomPart<T> randomPart) {
Metadata.Builder builder = Metadata.builder(metadata);
ImmutableOpenMap<String, T> parts = randomPart.parts(metadata);
Map<String, T> parts = randomPart.parts(metadata);
int partCount = parts.size();
if (partCount > 0) {
List<String> randomParts = randomSubsetOf(randomInt(partCount - 1), randomPart.parts(metadata).keySet().toArray(new String[0]));
Expand Down Expand Up @@ -589,7 +589,7 @@ public IndexMetadata randomChange(IndexMetadata part) {
private Metadata randomTemplates(Metadata metadata) {
return randomParts(metadata, "template", new RandomPart<IndexTemplateMetadata>() {
@Override
public ImmutableOpenMap<String, IndexTemplateMetadata> parts(Metadata metadata) {
public Map<String, IndexTemplateMetadata> parts(Metadata metadata) {
return metadata.templates();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -875,11 +875,11 @@ public Set<String> aliasedIndices() {
return aliasedIndices.keySet();
}

public ImmutableOpenMap<String, IndexTemplateMetadata> templates() {
public Map<String, IndexTemplateMetadata> templates() {
return this.templates;
}

public ImmutableOpenMap<String, IndexTemplateMetadata> getTemplates() {
public Map<String, IndexTemplateMetadata> getTemplates() {
return templates();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.core.Tuple;
Expand Down Expand Up @@ -63,7 +62,7 @@ public class TemplateUpgradeService implements ClusterStateListener {

final AtomicInteger upgradesInProgress = new AtomicInteger();

private ImmutableOpenMap<String, IndexTemplateMetadata> lastTemplateMetadata;
private Map<String, IndexTemplateMetadata> lastTemplateMetadata;

public TemplateUpgradeService(
Client client,
Expand Down Expand Up @@ -103,7 +102,7 @@ public void clusterChanged(ClusterChangedEvent event) {
return;
}

ImmutableOpenMap<String, IndexTemplateMetadata> templates = state.getMetadata().getTemplates();
Map<String, IndexTemplateMetadata> templates = state.getMetadata().getTemplates();

if (templates == lastTemplateMetadata) {
// we already checked these sets of templates - no reason to check it again
Expand Down Expand Up @@ -197,9 +196,7 @@ void tryFinishUpgrade(AtomicBoolean anyUpgradeFailed) {
// Check upgraders are satisfied after the update completed. If they still
// report that changes are required, this might indicate a bug or that something
// else tinkering with the templates during the upgrade.
final ImmutableOpenMap<String, IndexTemplateMetadata> upgradedTemplates = clusterService.state()
.getMetadata()
.getTemplates();
final Map<String, IndexTemplateMetadata> upgradedTemplates = clusterService.state().getMetadata().getTemplates();
final boolean changesRequired = calculateTemplateChanges(upgradedTemplates).isPresent();
if (changesRequired) {
logger.warn("Templates are still reported as out of date after the upgrade. The template upgrade will be retried.");
Expand All @@ -211,9 +208,7 @@ void tryFinishUpgrade(AtomicBoolean anyUpgradeFailed) {
}
}

Optional<Tuple<Map<String, BytesReference>, Set<String>>> calculateTemplateChanges(
ImmutableOpenMap<String, IndexTemplateMetadata> templates
) {
Optional<Tuple<Map<String, BytesReference>, Set<String>>> calculateTemplateChanges(Map<String, IndexTemplateMetadata> templates) {
// collect current templates
Map<String, IndexTemplateMetadata> existingMap = new HashMap<>();
for (Map.Entry<String, IndexTemplateMetadata> customCursor : templates.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.AbstractRunnable;
import org.elasticsearch.common.util.concurrent.EsExecutors;
Expand Down Expand Up @@ -243,7 +242,7 @@ static Metadata upgradeMetadata(Metadata metadata, IndexMetadataVerifier indexMe
}

private static boolean applyPluginUpgraders(
ImmutableOpenMap<String, IndexTemplateMetadata> existingData,
Map<String, IndexTemplateMetadata> existingData,
UnaryOperator<Map<String, IndexTemplateMetadata>> upgrader,
Consumer<String> removeData,
BiConsumer<String, IndexTemplateMetadata> putData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -634,28 +635,25 @@ public void testFindDefaultPipelineFromTemplateMatch() {
Exception exception = new Exception("fake exception");
ClusterState state = clusterService.state();

ImmutableOpenMap.Builder<String, IndexTemplateMetadata> templateMetadataBuilder = ImmutableOpenMap.builder();
templateMetadataBuilder.put(
Map<String, IndexTemplateMetadata> templateMetadata = new HashMap<>();
templateMetadata.put(
"template1",
IndexTemplateMetadata.builder("template1")
.patterns(Arrays.asList("missing_index"))
.order(1)
.settings(Settings.builder().put(IndexSettings.DEFAULT_PIPELINE.getKey(), "pipeline1").build())
.build()
);
templateMetadataBuilder.put(
templateMetadata.put(
"template2",
IndexTemplateMetadata.builder("template2")
.patterns(Arrays.asList("missing_*"))
.order(2)
.settings(Settings.builder().put(IndexSettings.DEFAULT_PIPELINE.getKey(), "pipeline2").build())
.build()
);
templateMetadataBuilder.put(
"template3",
IndexTemplateMetadata.builder("template3").patterns(Arrays.asList("missing*")).order(3).build()
);
templateMetadataBuilder.put(
templateMetadata.put("template3", IndexTemplateMetadata.builder("template3").patterns(Arrays.asList("missing*")).order(3).build());
templateMetadata.put(
"template4",
IndexTemplateMetadata.builder("template4")
.patterns(Arrays.asList("nope"))
Expand All @@ -667,7 +665,6 @@ public void testFindDefaultPipelineFromTemplateMatch() {
Metadata metadata = mock(Metadata.class);
when(state.metadata()).thenReturn(metadata);
when(state.getMetadata()).thenReturn(metadata);
final ImmutableOpenMap<String, IndexTemplateMetadata> templateMetadata = templateMetadataBuilder.build();
when(metadata.templates()).thenReturn(templateMetadata);
when(metadata.getTemplates()).thenReturn(templateMetadata);
when(metadata.indices()).thenReturn(ImmutableOpenMap.of());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.util.Maps;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.core.Tuple;
Expand Down Expand Up @@ -316,7 +315,7 @@ void upgradeTemplates(Map<String, BytesReference> changes, Set<String> deletions

@Override
Optional<Tuple<Map<String, BytesReference>, Set<String>>> calculateTemplateChanges(
ImmutableOpenMap<String, IndexTemplateMetadata> templates
Map<String, IndexTemplateMetadata> templates
) {
final Optional<Tuple<Map<String, BytesReference>, Set<String>>> ans = super.calculateTemplateChanges(templates);
calculateInvocation.release();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.elasticsearch.cluster.node.DiscoveryNodes;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.core.TimeValue;
Expand Down Expand Up @@ -206,11 +205,10 @@ private static AbstractAuditMessageTests.TestAuditMessage parseAuditMessage(Byte
}

private TestAuditor createTestAuditorWithTemplateInstalled() {
ImmutableOpenMap.Builder<String, IndexTemplateMetadata> templates = ImmutableOpenMap.builder(1);
templates.put(TEST_INDEX, mock(IndexTemplateMetadata.class));
Map<String, IndexTemplateMetadata> templates = Map.of(TEST_INDEX, mock(IndexTemplateMetadata.class));
Map<String, ComposableIndexTemplate> templatesV2 = Collections.singletonMap(TEST_INDEX, mock(ComposableIndexTemplate.class));
Metadata metadata = mock(Metadata.class);
when(metadata.getTemplates()).thenReturn(templates.build());
when(metadata.getTemplates()).thenReturn(templates);
when(metadata.templatesV2()).thenReturn(templatesV2);
DiscoveryNodes nodes = mock(DiscoveryNodes.class);
when(nodes.getMinNodeVersion()).thenReturn(Version.CURRENT);
Expand Down Expand Up @@ -253,9 +251,8 @@ private TestAuditor createTestAuditorWithoutTemplate(CountDownLatch latch) {
when(adminClient.indices()).thenReturn(indicesAdminClient);
when(client.admin()).thenReturn(adminClient);

ImmutableOpenMap.Builder<String, IndexTemplateMetadata> templates = ImmutableOpenMap.builder(0);
Metadata metadata = mock(Metadata.class);
when(metadata.getTemplates()).thenReturn(templates.build());
when(metadata.getTemplates()).thenReturn(Map.of());
DiscoveryNodes nodes = mock(DiscoveryNodes.class);
when(nodes.getMinNodeVersion()).thenReturn(Version.CURRENT);
ClusterState state = mock(ClusterState.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1253,7 +1253,7 @@ public void testMigrateLegacyIndexTemplates() {
)
);

ImmutableOpenMap<String, IndexTemplateMetadata> migratedTemplates = mb.build().templates();
Map<String, IndexTemplateMetadata> migratedTemplates = mb.build().templates();
assertThat(migratedTemplates.get("template-with-require-routing").settings().size(), is(1));
assertThat(migratedTemplates.get("template-with-include-routing").settings().size(), is(1));
assertThat(migratedTemplates.get("template-with-require-and-include-routing").settings().size(), is(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
import org.elasticsearch.cluster.metadata.IndexTemplateMetadata;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.xpack.core.common.notifications.Level;
import org.elasticsearch.xpack.core.transform.transforms.persistence.TransformInternalIndexConstants;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

import static org.elasticsearch.xpack.core.transform.transforms.persistence.TransformInternalIndexConstants.AUDIT_INDEX;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.mock;
Expand All @@ -38,10 +38,9 @@ public class MockTransformAuditor extends TransformAuditor {

@SuppressWarnings("unchecked")
public static MockTransformAuditor createMockAuditor() {
ImmutableOpenMap.Builder<String, IndexTemplateMetadata> templates = ImmutableOpenMap.builder(1);
templates.put(TransformInternalIndexConstants.AUDIT_INDEX, mock(IndexTemplateMetadata.class));
Map<String, IndexTemplateMetadata> templates = Map.of(AUDIT_INDEX, mock(IndexTemplateMetadata.class));
Metadata metadata = mock(Metadata.class);
when(metadata.getTemplates()).thenReturn(templates.build());
when(metadata.getTemplates()).thenReturn(templates);
ClusterState state = mock(ClusterState.class);
when(state.getMetadata()).thenReturn(metadata);
ClusterService clusterService = mock(ClusterService.class);
Expand Down

0 comments on commit d6519b4

Please sign in to comment.