Skip to content

Commit

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

* 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]>
(cherry picked from commit 7066923)

Co-authored-by: Joanne Wang <[email protected]>
  • Loading branch information
1 parent b164403 commit 9ddea79
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 @@ -27,6 +27,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 @@ -79,8 +81,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 @@ -95,6 +95,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 @@ -107,6 +109,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 @@ -485,7 +488,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 @@ -780,8 +783,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 9ddea79

Please sign in to comment.