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) (opensearch-project#777)

* 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 f490802 commit 454ee19
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 @@ -26,6 +26,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 @@ -78,8 +80,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 @@ -94,6 +94,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 @@ -106,6 +108,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 @@ -484,7 +487,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 @@ -772,8 +775,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 @@ -106,7 +106,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 454ee19

Please sign in to comment.