Skip to content

Commit

Permalink
Fixed compile issues related to replacing ImmutableOpenMap with java.…
Browse files Browse the repository at this point in the history
…util.Map (#412)

Signed-off-by: Petar Dzepina <petar.dzepina@gmail.com>
  • Loading branch information
petardz committed May 1, 2023
1 parent acfef46 commit f92e232
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 87 deletions.
3 changes: 2 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
package org.opensearch.securityanalytics.action;

import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.opensearch.Version;
import org.opensearch.action.ActionResponse;
import org.opensearch.cluster.metadata.MappingMetadata;
import org.opensearch.common.Strings;
import org.opensearch.common.collect.ImmutableOpenMap;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.xcontent.XContentType;
Expand All @@ -24,16 +26,16 @@ public class GetIndexMappingsResponse extends ActionResponse implements ToXConte

private static final ParseField MAPPINGS = new ParseField("mappings");

private final ImmutableOpenMap<String, MappingMetadata> mappings;
private final Map<String, MappingMetadata> mappings;

public GetIndexMappingsResponse(ImmutableOpenMap<String, MappingMetadata> mappings) {
public GetIndexMappingsResponse(final Map<String, MappingMetadata> mappings) {
this.mappings = mappings;
}

public GetIndexMappingsResponse(StreamInput in) throws IOException {
super(in);
int size = in.readVInt();
ImmutableOpenMap.Builder<String, MappingMetadata> indexMapBuilder = ImmutableOpenMap.builder();
final Map<String, MappingMetadata> indexMapBuilder = new HashMap<>();
for (int i = 0; i < size; i++) {
String index = in.readString();
if (in.getVersion().before(Version.V_2_0_0)) {
Expand All @@ -54,33 +56,33 @@ public GetIndexMappingsResponse(StreamInput in) throws IOException {
indexMapBuilder.put(index, hasMapping ? new MappingMetadata(in) : MappingMetadata.EMPTY_MAPPINGS);
}
}
mappings = indexMapBuilder.build();
mappings = Collections.unmodifiableMap(indexMapBuilder);
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(mappings.size());
for (ObjectObjectCursor<String, MappingMetadata> indexEntry : mappings) {
out.writeString(indexEntry.key);
for (Map.Entry<String, MappingMetadata> indexEntry : mappings.entrySet()) {
out.writeString(indexEntry.getKey());
if (out.getVersion().before(Version.V_2_0_0)) {
out.writeVInt(indexEntry.value == MappingMetadata.EMPTY_MAPPINGS ? 0 : 1);
if (indexEntry.value != MappingMetadata.EMPTY_MAPPINGS) {
out.writeVInt(indexEntry.getValue() == MappingMetadata.EMPTY_MAPPINGS ? 0 : 1);
if (indexEntry.getValue() != MappingMetadata.EMPTY_MAPPINGS) {
out.writeString(MapperService.SINGLE_MAPPING_NAME);
indexEntry.value.writeTo(out);
indexEntry.getValue().writeTo(out);
}
} else {
out.writeOptionalWriteable(indexEntry.value);
out.writeOptionalWriteable(indexEntry.getValue());
}
}
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
for (final ObjectObjectCursor<String, MappingMetadata> indexEntry : getMappings()) {
builder.startObject(indexEntry.key);
if (indexEntry.value != null) {
builder.field(MAPPINGS.getPreferredName(), indexEntry.value.sourceAsMap());
for (final Map.Entry<String, MappingMetadata> indexEntry : getMappings().entrySet()) {
builder.startObject(indexEntry.getKey());
if (indexEntry.getValue() != null) {
builder.field(MAPPINGS.getPreferredName(), indexEntry.getValue().sourceAsMap());
} else {
builder.startObject(MAPPINGS.getPreferredName()).endObject();
}
Expand All @@ -90,11 +92,11 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
return builder;
}

public ImmutableOpenMap<String, MappingMetadata> mappings() {
public Map<String, MappingMetadata> mappings() {
return mappings;
}

public ImmutableOpenMap<String, MappingMetadata> getMappings() {
public Map<String, MappingMetadata> getMappings() {
return mappings();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.Locale;
import org.opensearch.action.ActionRequest;
import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.common.collect.ImmutableOpenMap;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.xcontent.XContentParserUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,18 @@
*/
package org.opensearch.securityanalytics.action;

import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.opensearch.Version;
import org.opensearch.action.ActionResponse;
import org.opensearch.cluster.metadata.MappingMetadata;
import org.opensearch.common.Strings;
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.collect.ImmutableOpenMap;
import org.opensearch.common.compress.CompressedXContent;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.ParseField;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.index.mapper.MapperService;
import org.opensearch.securityanalytics.mapper.MapperUtils;

public class GetMappingsViewResponse extends ActionResponse implements ToXContentObject {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@ public void onFailure(Exception e) {

private List<String> getIndicesToDelete(ClusterStateResponse clusterStateResponse) {
List<String> indicesToDelete = new ArrayList<>();
for (ObjectCursor<IndexMetadata> in : clusterStateResponse.getState().metadata().indices().values()) {
IndexMetadata indexMetaData = in.value;
for (IndexMetadata indexMetadata : clusterStateResponse.getState().metadata().indices().values()) {
IndexMetadata indexMetaData = indexMetadata;
String indexToDelete = getHistoryIndexToDelete(indexMetaData, alertHistoryRetentionPeriod.millis(), alertHistoryIndices, alertHistoryEnabled);
if (indexToDelete != null) {
indicesToDelete.add(indexToDelete);
Expand All @@ -287,10 +287,10 @@ private String getHistoryIndexToDelete(
long creationTime = indexMetadata.getCreationDate();
if ((Instant.now().toEpochMilli() - creationTime) > retentionPeriodMillis) {
String alias = null;
for (ObjectCursor<AliasMetadata> aliasMetadata : indexMetadata.getAliases().values()) {
for (AliasMetadata aliasMetadata : indexMetadata.getAliases().values()) {
Optional<HistoryIndexInfo> historyIndexInfoOptional = historyIndices
.stream()
.filter(e -> e.indexAlias.equals(aliasMetadata.value.alias()))
.filter(e -> e.indexAlias.equals(aliasMetadata.alias()))
.findFirst();
if (historyIndexInfoOptional.isPresent()) {
alias = historyIndexInfoOptional.get().indexAlias;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
import org.opensearch.cluster.metadata.MappingMetadata;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.collect.ImmutableOpenMap;
import org.opensearch.rest.RestStatus;
import org.opensearch.securityanalytics.action.GetIndexMappingsResponse;
import org.opensearch.securityanalytics.action.GetMappingsViewResponse;
Expand Down Expand Up @@ -116,7 +115,7 @@ public void onFailure(Exception e) {
});
}

private void applyAliasMappings(ImmutableOpenMap<String, MappingMetadata> indexMappings, String ruleTopic, String aliasMappings, boolean partial, ActionListener<Collection<CreateMappingResult>> actionListener) {
private void applyAliasMappings(Map<String, MappingMetadata> indexMappings, String ruleTopic, String aliasMappings, boolean partial, ActionListener<Collection<CreateMappingResult>> actionListener) {
int numOfIndices = indexMappings.size();

GroupedActionListener doCreateMappingActionsListener = new GroupedActionListener(new ActionListener<Collection<CreateMappingResult>>() { @Override
Expand All @@ -134,9 +133,9 @@ public void onFailure(Exception e) {
}
}, numOfIndices);

indexMappings.forEach(iter -> {
String indexName = iter.key;
MappingMetadata mappingMetadata = iter.value;
indexMappings.forEach((k, v) -> {
String indexName = k;
MappingMetadata mappingMetadata = v;
// Try to apply mapping to index
doCreateMapping(indexName, mappingMetadata, ruleTopic, aliasMappings, partial, doCreateMappingActionsListener);
});
Expand Down Expand Up @@ -306,7 +305,7 @@ public void doGetMappingAction(String indexName, String concreteIndexName, Actio
public void onResponse(GetMappingsResponse getMappingsResponse) {
try {
// Extract MappingMetadata
MappingMetadata mappingMetadata = getMappingsResponse.mappings().iterator().next().value;
MappingMetadata mappingMetadata = getMappingsResponse.mappings().entrySet().iterator().next().getValue();
// List of all found applied aliases on index
Set<String> appliedAliases = new HashSet<>();
// Get list of alias -> path pairs from index mappings
Expand Down Expand Up @@ -344,12 +343,12 @@ public void onResponse(GetMappingsResponse getMappingsResponse) {


// Construct filtered mappings and return them as result
ImmutableOpenMap.Builder<String, MappingMetadata> outIndexMappings = ImmutableOpenMap.builder();
Map<String, MappingMetadata> outIndexMappings = new HashMap<>();
Map<String, Object> root = Map.of(org.opensearch.index.mapper.MapperService.SINGLE_MAPPING_NAME, filteredMapping);
MappingMetadata outMappingMetadata = new MappingMetadata(org.opensearch.index.mapper.MapperService.SINGLE_MAPPING_NAME, root);
outIndexMappings.put(indexName, outMappingMetadata);

actionListener.onResponse(new GetIndexMappingsResponse(outIndexMappings.build()));
actionListener.onResponse(new GetIndexMappingsResponse(outIndexMappings));
} catch (IOException e) {
actionListener.onFailure(e);
}
Expand Down Expand Up @@ -399,7 +398,7 @@ private void doGetMappingsView(String mapperTopic, ActionListener<GetMappingsVie
public void onResponse(GetMappingsResponse getMappingsResponse) {
try {
// Extract MappingMetadata from GET _mapping response
MappingMetadata mappingMetadata = getMappingsResponse.mappings().iterator().next().value;
MappingMetadata mappingMetadata = getMappingsResponse.mappings().entrySet().iterator().next().getValue();
// Get list of all non-alias fields in index
List<String> allFieldsFromIndex = MapperUtils.getAllNonAliasFieldsFromIndex(mappingMetadata);
// Get stored Alias Mappings as JSON string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@

package org.opensearch.securityanalytics.mapper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.tuple.Pair;
import org.opensearch.cluster.metadata.MappingMetadata;
import org.opensearch.common.collect.ImmutableOpenMap;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.opensearch.securityanalytics.util.SecurityAnalyticsException;

public class MapperUtils {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.opensearch.client.Response;
import org.opensearch.cluster.ClusterModule;
import org.opensearch.cluster.metadata.MappingMetadata;
import org.opensearch.common.collect.ImmutableOpenMap;
import org.opensearch.core.xcontent.DeprecationHandler;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.XContentParser;
Expand Down Expand Up @@ -59,9 +58,8 @@ public static GetMappingsResponse executeGetMappingsRequest(String indexName) th

mappings.put(_indexName, new MappingMetadata(MapperService.SINGLE_MAPPING_NAME, fieldMappings));
}
ImmutableOpenMap<String, MappingMetadata> immutableMappingsMap =
new ImmutableOpenMap.Builder<String, MappingMetadata>().putAll(mappings).build();
return new GetMappingsResponse(immutableMappingsMap);
Map<String, MappingMetadata> mappingsMap = new HashMap<>(mappings);
return new GetMappingsResponse(mappingsMap);
}

public static boolean executePutMappingRequest(String indexName, String mappings) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.opensearch.cluster.metadata.MappingMetadata;
import org.opensearch.common.Strings;
import org.opensearch.common.UUIDs;
import org.opensearch.common.collect.ImmutableOpenMap;

import org.opensearch.common.io.PathUtils;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.XContentFactory;
Expand Down Expand Up @@ -494,9 +494,8 @@ public static GetMappingsResponse executeGetMappingsRequest(String indexName) th

mappings.put(_indexName, new MappingMetadata(MapperService.SINGLE_MAPPING_NAME, fieldMappings));
}
ImmutableOpenMap<String, MappingMetadata> immutableMappingsMap =
new ImmutableOpenMap.Builder<String, MappingMetadata>().putAll(mappings).build();
return new GetMappingsResponse(immutableMappingsMap);
Map<String, MappingMetadata> mappingsMap = new HashMap<>(mappings);
return new GetMappingsResponse(mappingsMap);
}

public Response searchAlertingFindings(Map<String, String> params) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package org.opensearch.securityanalytics.action;

import org.opensearch.cluster.metadata.MappingMetadata;
import org.opensearch.common.collect.ImmutableOpenMap;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.index.mapper.MapperService;
Expand All @@ -24,10 +23,10 @@ public class GetIndexMappingsResponseTests extends OpenSearchTestCase {
public void testStreamInOut() throws IOException {
BytesStreamOutput out = new BytesStreamOutput();

ImmutableOpenMap.Builder<String, MappingMetadata> mappings = ImmutableOpenMap.builder();
Map<String, MappingMetadata> mappings = new HashMap<>();
mappings.put("my_index", createMappingsForIndex());

GetIndexMappingsResponse response = new GetIndexMappingsResponse(mappings.build());
GetIndexMappingsResponse response = new GetIndexMappingsResponse(mappings);
response.writeTo(out);

StreamInput sin = StreamInput.wrap(out.bytes().toBytesRef().bytes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void testCreateMappingSuccess() throws IOException {

// Verify mappings
GetMappingsResponse getMappingsResponse = SecurityAnalyticsClientUtils.executeGetMappingsRequest(testIndexName);
MappingsTraverser mappingsTraverser = new MappingsTraverser(getMappingsResponse.getMappings().iterator().next().value);
MappingsTraverser mappingsTraverser = new MappingsTraverser(getMappingsResponse.getMappings().entrySet().iterator().next().getValue());
// After applying netflow aliases, our index will have 4 alias mappings
List<String> flatProperties = mappingsTraverser.extractFlatNonAliasFields();
assertFalse(flatProperties.contains("source.ip"));
Expand Down Expand Up @@ -174,7 +174,7 @@ public void testCreateMappingWithAliasesSuccess() throws IOException {

// Verify mappings
GetMappingsResponse getMappingsResponse = SecurityAnalyticsClientUtils.executeGetMappingsRequest(testIndexName);
MappingsTraverser mappingsTraverser = new MappingsTraverser(getMappingsResponse.getMappings().iterator().next().value);
MappingsTraverser mappingsTraverser = new MappingsTraverser(getMappingsResponse.getMappings().entrySet().iterator().next().getValue());
List<String> flatProperties = mappingsTraverser.extractFlatNonAliasFields();
assertFalse(flatProperties.contains("source.ip"));
assertFalse(flatProperties.contains("source.port"));
Expand Down Expand Up @@ -1387,7 +1387,7 @@ public void testCreateDNSMapping() throws IOException{
Map<String, Object> mappings = (Map<String, Object>) parser.map().get("properties");
GetMappingsResponse getMappingsResponse = SecurityAnalyticsClientUtils.executeGetMappingsRequest(INDEX_NAME);

MappingsTraverser mappingsTraverser = new MappingsTraverser(getMappingsResponse.getMappings().iterator().next().value);
MappingsTraverser mappingsTraverser = new MappingsTraverser(getMappingsResponse.getMappings().entrySet().iterator().next().getValue());
List<String> flatProperties = mappingsTraverser.extractFlatNonAliasFields();
assertTrue(flatProperties.contains("dns.answers.type"));
assertTrue(flatProperties.contains("dns.question.name"));
Expand Down
Loading

0 comments on commit f92e232

Please sign in to comment.