Skip to content

Commit

Permalink
Use a common constant to specify the version for log type mappings op…
Browse files Browse the repository at this point in the history
…ensearch-project#708 (opensearch-project#734)

* json parser

Signed-off-by: Joanne Wang <[email protected]>

* update gradle json dependency

Signed-off-by: Joanne Wang <[email protected]>

* use xcontenthelper to parse json file

Signed-off-by: Joanne Wang <[email protected]>

---------

Signed-off-by: Joanne Wang <[email protected]>
  • Loading branch information
jowg-amazon authored Dec 8, 2023
1 parent ae8e996 commit 7066923
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.opensearch.OpenSearchStatusException;
import org.opensearch.ResourceAlreadyExistsException;
import org.opensearch.cluster.routing.Preference;
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.core.action.ActionListener;
import org.opensearch.action.DocWriteRequest;
import org.opensearch.action.admin.indices.create.CreateIndexRequest;
Expand Down Expand Up @@ -76,8 +78,6 @@ public class LogTypeService {

public static final String LOG_TYPE_MAPPING_VERSION_META_FIELD = "schema_version";

public static final int LOG_TYPE_MAPPING_VERSION = 1; // must match version in log_type_config_mapping.json

public static final int MAX_LOG_TYPE_COUNT = 100;

private static volatile boolean isConfigIndexInitialized;
Expand All @@ -92,6 +92,8 @@ public class LogTypeService {

private String defaultSchemaField;

public int logTypeMappingVersion;

@Inject
public LogTypeService(Client client, ClusterService clusterService, NamedXContentRegistry xContentRegistry, BuiltinLogTypeLoader builtinLogTypeLoader) {
this.client = client;
Expand All @@ -104,6 +106,7 @@ public LogTypeService(Client client, ClusterService clusterService, NamedXConten
DEFAULT_MAPPING_SCHEMA,
newDefaultSchema -> this.defaultSchemaField = newDefaultSchema
);
setLogTypeMappingVersion();
}

public void getAllLogTypes(ActionListener<List<String>> listener) {
Expand Down Expand Up @@ -482,7 +485,7 @@ public void onFailure(Exception e) {
});
} else {
IndexMetadata metadata = state.getMetadata().index(LOG_TYPE_INDEX);
if (getConfigIndexMappingVersion(metadata) < LOG_TYPE_MAPPING_VERSION) {
if (getConfigIndexMappingVersion(metadata) < logTypeMappingVersion) {
// The index already exists but doesn't have our mapping
client.admin()
.indices()
Expand Down Expand Up @@ -777,8 +780,13 @@ public Map<String, String> getRuleFieldMappingsForBuiltinLogType(String builtinL
}
}


public String getDefaultSchemaField() {
return defaultSchemaField;
}

public void setLogTypeMappingVersion() {
Map<String, Object> logTypeConfigAsMap =
XContentHelper.convertToMap(JsonXContent.jsonXContent, logTypeIndexMapping(), false);
this.logTypeMappingVersion = (int)((Map)logTypeConfigAsMap.get("_meta")).get("schema_version");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ public void testIndexMappings() throws ExecutionException, InterruptedException

}


public void testSetLogTypeMappingSchema() {
int expectedVersion = 2;
int version = logTypeService.logTypeMappingVersion;
assertEquals(expectedVersion, version);
}

private void indexFieldMappings(List<FieldMappingDoc> fieldMappingDocs) {
PlainActionFuture<Void> fut = new PlainActionFuture<>();
Expand Down

0 comments on commit 7066923

Please sign in to comment.