From 205b30a982eb3a219870baff8688c79c8b61d508 Mon Sep 17 00:00:00 2001 From: Stephen Crawford <65832608+scrawfor99@users.noreply.github.com> Date: Tue, 25 Apr 2023 09:28:32 -0400 Subject: [PATCH] Resolve ImmutableOpenMap issue from core refactor (#2715) Signed-off-by: Maciej Mierzwa --- .../framework/matcher/AliasExistsMatcher.java | 15 +++++++++++---- ...lusterContainTemplateWithAliasMatcher.java | 19 +++++++++++++++---- ...ettingsResponseContainsIndicesMatcher.java | 13 ++++++++++--- .../matcher/IndexMappingIsEqualToMatcher.java | 5 +---- .../matcher/IndexStateIsEqualToMatcher.java | 5 +++-- .../privileges/PrivilegesEvaluator.java | 15 +++++++++------ 6 files changed, 49 insertions(+), 23 deletions(-) diff --git a/src/integrationTest/java/org/opensearch/test/framework/matcher/AliasExistsMatcher.java b/src/integrationTest/java/org/opensearch/test/framework/matcher/AliasExistsMatcher.java index ef2ffb365b..4999da8e95 100644 --- a/src/integrationTest/java/org/opensearch/test/framework/matcher/AliasExistsMatcher.java +++ b/src/integrationTest/java/org/opensearch/test/framework/matcher/AliasExistsMatcher.java @@ -10,12 +10,15 @@ package org.opensearch.test.framework.matcher; import java.util.Collection; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.concurrent.ExecutionException; import java.util.stream.Collectors; import java.util.stream.StreamSupport; +import com.carrotsearch.hppc.cursors.ObjectObjectCursor; import org.hamcrest.Description; import org.hamcrest.TypeSafeDiagnosingMatcher; @@ -23,7 +26,6 @@ import org.opensearch.action.admin.indices.alias.get.GetAliasesResponse; import org.opensearch.client.Client; import org.opensearch.cluster.metadata.AliasMetadata; -import org.opensearch.common.collect.ImmutableOpenMap; import static java.util.Objects.requireNonNull; import static java.util.Spliterator.IMMUTABLE; @@ -41,8 +43,13 @@ public AliasExistsMatcher(String aliasName) { protected boolean matchesSafely(Client client, Description mismatchDescription) { try { GetAliasesResponse response = client.admin().indices().getAliases(new GetAliasesRequest(aliasName)).get(); - ImmutableOpenMap> aliases = response.getAliases(); - Set actualAliasNames = StreamSupport.stream(spliteratorUnknownSize(aliases.valuesIt(), IMMUTABLE), false) + + final Map> aliases = new HashMap<>(); + for (ObjectObjectCursor> cursor : response.getAliases()) { + aliases.put(cursor.key, cursor.value); + } + + Set actualAliasNames = StreamSupport.stream(spliteratorUnknownSize(aliases.values().iterator(), IMMUTABLE), false) .flatMap(Collection::stream) .map(AliasMetadata::getAlias) .collect(Collectors.toSet()); @@ -53,7 +60,7 @@ protected boolean matchesSafely(Client client, Description mismatchDescription) } return true; } catch (InterruptedException | ExecutionException e) { - mismatchDescription.appendText("Error occured during checking if cluster contains alias ") + mismatchDescription.appendText("Error occurred during checking if cluster contains alias ") .appendValue(e); return false; } diff --git a/src/integrationTest/java/org/opensearch/test/framework/matcher/ClusterContainTemplateWithAliasMatcher.java b/src/integrationTest/java/org/opensearch/test/framework/matcher/ClusterContainTemplateWithAliasMatcher.java index 907fb30257..d59792e715 100644 --- a/src/integrationTest/java/org/opensearch/test/framework/matcher/ClusterContainTemplateWithAliasMatcher.java +++ b/src/integrationTest/java/org/opensearch/test/framework/matcher/ClusterContainTemplateWithAliasMatcher.java @@ -9,11 +9,14 @@ */ package org.opensearch.test.framework.matcher; +import java.util.HashMap; +import java.util.Map; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; import java.util.stream.StreamSupport; +import com.carrotsearch.hppc.cursors.ObjectObjectCursor; import org.hamcrest.Description; import org.hamcrest.TypeSafeDiagnosingMatcher; @@ -21,7 +24,6 @@ import org.opensearch.action.admin.indices.template.get.GetIndexTemplatesResponse; import org.opensearch.client.Client; import org.opensearch.cluster.metadata.AliasMetadata; -import org.opensearch.common.collect.ImmutableOpenMap; import static java.util.Objects.requireNonNull; @@ -55,15 +57,24 @@ protected boolean matchesSafely(Client client, Description mismatchDescription) private Set getAliases(GetIndexTemplatesResponse response) { return response.getIndexTemplates() .stream() - .map(metadata -> metadata.getAliases()) + .map(metadata -> { + Map aliases = new HashMap<>(); + for (ObjectObjectCursor cursor : metadata.getAliases()) { + aliases.put(cursor.key, cursor.value); + } + return aliases; + }) .flatMap(aliasMap -> aliasNames(aliasMap)) .collect(Collectors.toSet()); } - private Stream aliasNames(ImmutableOpenMap aliasMap) { - return StreamSupport.stream(aliasMap.keys().spliterator(), false).map(objectCursor -> objectCursor.value); + private Stream aliasNames(Map aliasMap) { + Iterable> iterable = () -> aliasMap.entrySet().iterator(); + return StreamSupport.stream(iterable.spliterator(), false) + .map(entry -> entry.getValue().getAlias()); } + @Override public void describeTo(Description description) { description.appendText("template ").appendValue(templateName).appendText(" exists and "); diff --git a/src/integrationTest/java/org/opensearch/test/framework/matcher/GetSettingsResponseContainsIndicesMatcher.java b/src/integrationTest/java/org/opensearch/test/framework/matcher/GetSettingsResponseContainsIndicesMatcher.java index 15c558d32f..79ac1c64ac 100644 --- a/src/integrationTest/java/org/opensearch/test/framework/matcher/GetSettingsResponseContainsIndicesMatcher.java +++ b/src/integrationTest/java/org/opensearch/test/framework/matcher/GetSettingsResponseContainsIndicesMatcher.java @@ -9,11 +9,14 @@ */ package org.opensearch.test.framework.matcher; +import java.util.HashMap; +import java.util.Map; + +import com.carrotsearch.hppc.cursors.ObjectObjectCursor; import org.hamcrest.Description; import org.hamcrest.TypeSafeDiagnosingMatcher; import org.opensearch.action.admin.indices.settings.get.GetSettingsResponse; -import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.settings.Settings; import static java.util.Objects.isNull; @@ -31,12 +34,16 @@ class GetSettingsResponseContainsIndicesMatcher extends TypeSafeDiagnosingMatche @Override protected boolean matchesSafely(GetSettingsResponse response, Description mismatchDescription) { - ImmutableOpenMap indexToSettings = response.getIndexToSettings(); + + final Map indexToSettings = new HashMap<>(); + for (ObjectObjectCursor cursor : response.getIndexToSettings()) { + indexToSettings.put(cursor.key, cursor.value); + } for (String index : expectedIndices) { if (!indexToSettings.containsKey(index)) { mismatchDescription .appendText("Response contains settings of indices: ") - .appendValue(indexToSettings.keys()); + .appendValue(indexToSettings.keySet()); return false; } } diff --git a/src/integrationTest/java/org/opensearch/test/framework/matcher/IndexMappingIsEqualToMatcher.java b/src/integrationTest/java/org/opensearch/test/framework/matcher/IndexMappingIsEqualToMatcher.java index 9621aff449..b90defef7d 100644 --- a/src/integrationTest/java/org/opensearch/test/framework/matcher/IndexMappingIsEqualToMatcher.java +++ b/src/integrationTest/java/org/opensearch/test/framework/matcher/IndexMappingIsEqualToMatcher.java @@ -17,8 +17,6 @@ import org.opensearch.action.admin.indices.mapping.get.GetMappingsRequest; import org.opensearch.action.admin.indices.mapping.get.GetMappingsResponse; import org.opensearch.client.Client; -import org.opensearch.cluster.metadata.MappingMetadata; -import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.index.IndexNotFoundException; import org.opensearch.test.framework.cluster.LocalCluster; @@ -44,8 +42,7 @@ protected boolean matchesSafely(LocalCluster cluster, Description mismatchDescri GetMappingsResponse response = client.admin().indices() .getMappings(new GetMappingsRequest().indices(expectedIndexName)).actionGet(); - ImmutableOpenMap actualMappings = response.mappings(); - Map actualIndexMapping = actualMappings.get(expectedIndexName).getSourceAsMap(); + Map actualIndexMapping = response.getMappings().get(expectedIndexName).sourceAsMap(); if (!expectedMapping.equals(actualIndexMapping)) { mismatchDescription.appendText("Actual mapping ").appendValue(actualIndexMapping).appendText(" does not match expected"); diff --git a/src/integrationTest/java/org/opensearch/test/framework/matcher/IndexStateIsEqualToMatcher.java b/src/integrationTest/java/org/opensearch/test/framework/matcher/IndexStateIsEqualToMatcher.java index a9538b4b8b..ecff3fe2df 100644 --- a/src/integrationTest/java/org/opensearch/test/framework/matcher/IndexStateIsEqualToMatcher.java +++ b/src/integrationTest/java/org/opensearch/test/framework/matcher/IndexStateIsEqualToMatcher.java @@ -9,6 +9,8 @@ */ package org.opensearch.test.framework.matcher; +import java.util.Map; + import org.hamcrest.Description; import org.hamcrest.TypeSafeDiagnosingMatcher; @@ -16,7 +18,6 @@ import org.opensearch.action.admin.cluster.state.ClusterStateResponse; import org.opensearch.client.Client; import org.opensearch.cluster.metadata.IndexMetadata; -import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.test.framework.cluster.LocalCluster; import static java.util.Objects.requireNonNull; @@ -37,7 +38,7 @@ protected boolean matchesSafely(LocalCluster cluster, Description mismatchDescri ClusterStateRequest clusterStateRequest = new ClusterStateRequest().indices(expectedIndexName); ClusterStateResponse clusterStateResponse = client.admin().cluster().state(clusterStateRequest).actionGet(); - ImmutableOpenMap indicesMetadata = clusterStateResponse.getState().getMetadata().indices(); + Map indicesMetadata = clusterStateResponse.getState().getMetadata().indices(); if (!indicesMetadata.containsKey(expectedIndexName)) { mismatchDescription.appendValue("Index does not exist"); } diff --git a/src/main/java/org/opensearch/security/privileges/PrivilegesEvaluator.java b/src/main/java/org/opensearch/security/privileges/PrivilegesEvaluator.java index 2c0e7ac7c0..b77b7a000f 100644 --- a/src/main/java/org/opensearch/security/privileges/PrivilegesEvaluator.java +++ b/src/main/java/org/opensearch/security/privileges/PrivilegesEvaluator.java @@ -29,6 +29,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; @@ -37,6 +38,7 @@ import java.util.StringJoiner; import java.util.regex.Pattern; +import com.carrotsearch.hppc.cursors.ObjectObjectCursor; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Sets; import org.apache.logging.log4j.LogManager; @@ -78,7 +80,6 @@ import org.opensearch.cluster.metadata.IndexNameExpressionResolver; import org.opensearch.cluster.service.ClusterService; import org.opensearch.common.Strings; -import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.settings.Settings; import org.opensearch.common.transport.TransportAddress; import org.opensearch.common.util.concurrent.ThreadContext; @@ -378,8 +379,6 @@ public PrivilegesEvaluatorResponse evaluate(final User user, String action0, fin presponse.allowed = true; return presponse; } - - } } @@ -632,6 +631,7 @@ public static boolean isClusterPerm(String action0) { ) ; } + @SuppressWarnings("unchecked") private boolean checkFilteredAliases(Resolved requestedResolved, String action, boolean isDebugEnabled) { final String faMode = dcm.getFilteredAliasMode();// getConfigSettings().dynamic.filtered_alias_mode; @@ -649,7 +649,7 @@ private boolean checkFilteredAliases(Resolved requestedResolved, String action, indexMetaDataCollection = new Iterable() { @Override public Iterator iterator() { - return clusterService.state().getMetadata().getIndices().valuesIt(); + return clusterService.state().getMetadata().getIndices().values().iterator(); } }; } else { @@ -674,14 +674,17 @@ public Iterator iterator() { final List filteredAliases = new ArrayList(); - final ImmutableOpenMap aliases = indexMetaData.getAliases(); + final Map aliases = new HashMap<>(); + for (ObjectObjectCursor cursor : indexMetaData.getAliases()) { + aliases.put(cursor.key, cursor.value); + } if(aliases != null && aliases.size() > 0) { if (isDebugEnabled) { log.debug("Aliases for {}: {}", indexMetaData.getIndex().getName(), aliases); } - final Iterator it = aliases.keysIt(); + final Iterator it = aliases.keySet().iterator(); while(it.hasNext()) { final String alias = it.next(); final AliasMetadata aliasMetadata = aliases.get(alias);