Skip to content

Commit

Permalink
Logging - provides template usage information on index creation
Browse files Browse the repository at this point in the history
Closes #7421
  • Loading branch information
pkoenig10 authored and markharwood committed Nov 28, 2014
1 parent 4b8fe3f commit 1d51924
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class IndexTemplateMetaData {

// the mapping source should always include the type as top level
private final ImmutableOpenMap<String, CompressedString> mappings;

private final ImmutableOpenMap<String, AliasMetaData> aliases;

private final ImmutableOpenMap<String, IndexMetaData.Custom> customs;
Expand Down Expand Up @@ -112,7 +112,7 @@ public ImmutableOpenMap<String, CompressedString> getMappings() {
public ImmutableOpenMap<String, AliasMetaData> aliases() {
return this.aliases;
}

public ImmutableOpenMap<String, AliasMetaData> getAliases() {
return this.aliases;
}
Expand Down Expand Up @@ -176,7 +176,7 @@ public static class Builder {
private Settings settings = ImmutableSettings.Builder.EMPTY_SETTINGS;

private final ImmutableOpenMap.Builder<String, CompressedString> mappings;

private final ImmutableOpenMap.Builder<String, AliasMetaData> aliases;

private final ImmutableOpenMap.Builder<String, IndexMetaData.Custom> customs;
Expand Down Expand Up @@ -237,7 +237,7 @@ public Builder putMapping(String mappingType, String mappingSource) throws IOExc
mappings.put(mappingType, new CompressedString(mappingSource));
return this;
}

public Builder putAlias(AliasMetaData aliasMetaData) {
aliases.put(aliasMetaData.alias(), aliasMetaData);
return this;
Expand Down Expand Up @@ -309,7 +309,7 @@ public static void toXContent(IndexTemplateMetaData indexTemplateMetaData, XCont
IndexMetaData.lookupFactorySafe(cursor.key).toXContent(cursor.value, builder, params);
builder.endObject();
}

builder.startObject("aliases");
for (ObjectCursor<AliasMetaData> cursor : indexTemplateMetaData.aliases().values()) {
AliasMetaData.Builder.toXContent(cursor.value, builder, params);
Expand All @@ -319,8 +319,8 @@ public static void toXContent(IndexTemplateMetaData indexTemplateMetaData, XCont
builder.endObject();
}

public static IndexTemplateMetaData fromXContent(XContentParser parser) throws IOException {
Builder builder = new Builder(parser.currentName());
public static IndexTemplateMetaData fromXContent(XContentParser parser, String templateName) throws IOException {
Builder builder = new Builder(templateName);

String currentFieldName = skipTemplateName(parser);
XContentParser.Token token;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ public ImmutableOpenMap<String, ImmutableList<AliasMetaData>> findAliases(final
}
return mapBuilder.build();
}

private boolean matchAllAliases(final String[] aliases) {
for (String alias : aliases) {
if (alias.equals("_all")) {
Expand Down Expand Up @@ -1020,7 +1020,7 @@ public String[] filteringAliases(String index, String... indicesOrAliases) {
public static boolean isAllIndices(String[] aliasesOrIndices) {
return aliasesOrIndices == null || aliasesOrIndices.length == 0 || isExplicitAllPattern(aliasesOrIndices);
}

/**
* Identifies whether the array containing type names given as argument refers to all types
* The empty or null array identifies all types
Expand Down Expand Up @@ -1368,7 +1368,7 @@ public static MetaData fromXContent(XContentParser parser) throws IOException {
}
} else if ("templates".equals(currentFieldName)) {
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
builder.put(IndexTemplateMetaData.Builder.fromXContent(parser));
builder.put(IndexTemplateMetaData.Builder.fromXContent(parser, parser.currentName()));
}
} else {
// check if its a custom index metadata
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ public ClusterState execute(ClusterState currentState) throws Exception {

Map<String, AliasMetaData> templatesAliases = Maps.newHashMap();

List<String> templateNames = Lists.newArrayList();

for (Map.Entry<String, String> entry : request.mappings().entrySet()) {
mappings.put(entry.getKey(), parseMapping(entry.getValue()));
}
Expand All @@ -256,6 +258,7 @@ public ClusterState execute(ClusterState currentState) throws Exception {

// apply templates, merging the mappings into the request mapping if exists
for (IndexTemplateMetaData template : templates) {
templateNames.add(template.getName());
for (ObjectObjectCursor<String, CompressedString> cursor : template.mappings()) {
if (mappings.containsKey(cursor.key)) {
XContentHelper.mergeDefaults(mappings.get(cursor.key), parseMapping(cursor.value.string()));
Expand Down Expand Up @@ -446,7 +449,7 @@ public ClusterState execute(ClusterState currentState) throws Exception {
.put(indexMetaData, false)
.build();

logger.info("[{}] creating index, cause [{}], shards [{}]/[{}], mappings {}", request.index(), request.cause(), indexMetaData.numberOfShards(), indexMetaData.numberOfReplicas(), mappings.keySet());
logger.info("[{}] creating index, cause [{}], templates {}, shards [{}]/[{}], mappings {}", request.index(), request.cause(), templateNames, indexMetaData.numberOfShards(), indexMetaData.numberOfReplicas(), mappings.keySet());

ClusterBlocks.Builder blocks = ClusterBlocks.builder().blocks(currentState.blocks());
if (!request.blocks().isEmpty()) {
Expand Down Expand Up @@ -518,18 +521,20 @@ private List<IndexTemplateMetaData> findTemplates(CreateIndexClusterStateUpdateR
File[] templatesFiles = templatesDir.listFiles();
if (templatesFiles != null) {
for (File templatesFile : templatesFiles) {
XContentParser parser = null;
try {
byte[] templatesData = Streams.copyToByteArray(templatesFile);
parser = XContentHelper.createParser(templatesData, 0, templatesData.length);
IndexTemplateMetaData template = IndexTemplateMetaData.Builder.fromXContent(parser);
if (indexTemplateFilter.apply(request, template)) {
templates.add(template);
if (templatesFile.isFile()) {
XContentParser parser = null;
try {
byte[] templatesData = Streams.copyToByteArray(templatesFile);
parser = XContentHelper.createParser(templatesData, 0, templatesData.length);
IndexTemplateMetaData template = IndexTemplateMetaData.Builder.fromXContent(parser, templatesFile.getName());
if (indexTemplateFilter.apply(request, template)) {
templates.add(template);
}
} catch (Exception e) {
logger.warn("[{}] failed to read template [{}] from config", e, request.index(), templatesFile.getAbsolutePath());
} finally {
Releasables.closeWhileHandlingException(parser);
}
} catch (Exception e) {
logger.warn("[{}] failed to read template [{}] from config", e, request.index(), templatesFile.getAbsolutePath());
} finally {
Releasables.closeWhileHandlingException(parser);
}
}
}
Expand Down

0 comments on commit 1d51924

Please sign in to comment.