From 5655d6a1c1ba0c8d59c4756d9d8e9d13bca350fd Mon Sep 17 00:00:00 2001 From: kkewwei Date: Fri, 3 Jan 2020 19:23:20 +0800 Subject: [PATCH] Log index name when updating index settings (#49969) Today we log changes to index settings like this: updating [index.setting.blah] from [A] to [B] The identity of the index whose settings were updated is conspicuously absent from this message. This commit addresses this by adding the index name to these messages. Fixes #49818. --- .../settings/AbstractScopedSettings.java | 6 ++- .../common/settings/IndexScopedSettings.java | 3 +- .../common/settings/SettingTests.java | 41 +++++++++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/common/settings/AbstractScopedSettings.java b/server/src/main/java/org/elasticsearch/common/settings/AbstractScopedSettings.java index 9a329daf0492c..17dcba4f2fb70 100644 --- a/server/src/main/java/org/elasticsearch/common/settings/AbstractScopedSettings.java +++ b/server/src/main/java/org/elasticsearch/common/settings/AbstractScopedSettings.java @@ -55,7 +55,7 @@ public abstract class AbstractScopedSettings { private static final Pattern GROUP_KEY_PATTERN = Pattern.compile("^(?:[-\\w]+[.])+$"); private static final Pattern AFFIX_KEY_PATTERN = Pattern.compile("^(?:[-\\w]+[.])+[*](?:[.][-\\w]+)+$"); - protected final Logger logger = LogManager.getLogger(this.getClass()); + private final Logger logger; private final Settings settings; private final List> settingUpdaters = new CopyOnWriteArrayList<>(); @@ -70,6 +70,7 @@ protected AbstractScopedSettings( final Set> settingsSet, final Set> settingUpgraders, final Setting.Property scope) { + this.logger = LogManager.getLogger(this.getClass()); this.settings = settings; this.lastSettingsApplied = Settings.EMPTY; @@ -110,7 +111,8 @@ protected void validateSettingKey(Setting setting) { } } - protected AbstractScopedSettings(Settings nodeSettings, Settings scopeSettings, AbstractScopedSettings other) { + protected AbstractScopedSettings(Settings nodeSettings, Settings scopeSettings, AbstractScopedSettings other, Logger logger) { + this.logger = logger; this.settings = nodeSettings; this.lastSettingsApplied = scopeSettings; this.scope = other.scope; diff --git a/server/src/main/java/org/elasticsearch/common/settings/IndexScopedSettings.java b/server/src/main/java/org/elasticsearch/common/settings/IndexScopedSettings.java index 847a3e70349eb..7fd6a5245d667 100644 --- a/server/src/main/java/org/elasticsearch/common/settings/IndexScopedSettings.java +++ b/server/src/main/java/org/elasticsearch/common/settings/IndexScopedSettings.java @@ -24,6 +24,7 @@ import org.elasticsearch.cluster.routing.allocation.decider.EnableAllocationDecider; import org.elasticsearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider; import org.elasticsearch.cluster.routing.allocation.decider.ShardsLimitAllocationDecider; +import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.settings.Setting.Property; import org.elasticsearch.index.IndexModule; import org.elasticsearch.index.IndexSettings; @@ -190,7 +191,7 @@ public IndexScopedSettings(Settings settings, Set> settingsSet) { } private IndexScopedSettings(Settings settings, IndexScopedSettings other, IndexMetaData metaData) { - super(settings, metaData.getSettings(), other); + super(settings, metaData.getSettings(), other, Loggers.getLogger(IndexScopedSettings.class, metaData.getIndex())); } public IndexScopedSettings copy(Settings settings, IndexMetaData metaData) { diff --git a/server/src/test/java/org/elasticsearch/common/settings/SettingTests.java b/server/src/test/java/org/elasticsearch/common/settings/SettingTests.java index 86387f2104bcf..a4b52f9d1d2ec 100644 --- a/server/src/test/java/org/elasticsearch/common/settings/SettingTests.java +++ b/server/src/test/java/org/elasticsearch/common/settings/SettingTests.java @@ -18,14 +18,23 @@ */ package org.elasticsearch.common.settings; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.core.LogEvent; +import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.collect.Tuple; +import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.settings.AbstractScopedSettings.SettingUpdater; import org.elasticsearch.common.settings.Setting.Property; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.index.IndexSettings; import org.elasticsearch.monitor.jvm.JvmInfo; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.MockLogAppender; +import org.elasticsearch.test.junit.annotations.TestLogging; import java.util.Arrays; import java.util.Collections; @@ -40,6 +49,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import static org.elasticsearch.index.IndexSettingsTests.newIndexMeta; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasSize; @@ -1085,4 +1095,35 @@ public void testNonSecureSettingInKeystore() { assertThat(e.getMessage(), containsString("must be stored inside elasticsearch.yml")); } + @TestLogging(value="org.elasticsearch.common.settings.IndexScopedSettings:INFO", + reason="to ensure we log INFO-level messages from IndexScopedSettings") + public void testLogSettingUpdate() throws Exception { + final IndexMetaData metaData = newIndexMeta("index1", + Settings.builder().put(IndexSettings.INDEX_REFRESH_INTERVAL_SETTING.getKey(), "20s").build()); + final IndexSettings settings = new IndexSettings(metaData, Settings.EMPTY); + + final MockLogAppender mockLogAppender = new MockLogAppender(); + mockLogAppender.addExpectation(new MockLogAppender.SeenEventExpectation( + "message", + "org.elasticsearch.common.settings.IndexScopedSettings", + Level.INFO, + "updating [index.refresh_interval] from [20s] to [10s]") { + @Override + public boolean innerMatch(LogEvent event) { + return event.getMarker().getName().equals(" [index1]"); + } + }); + mockLogAppender.start(); + final Logger logger = LogManager.getLogger(IndexScopedSettings.class); + try { + Loggers.addAppender(logger, mockLogAppender); + settings.updateIndexMetaData(newIndexMeta("index1", + Settings.builder().put(IndexSettings.INDEX_REFRESH_INTERVAL_SETTING.getKey(), "10s").build())); + + mockLogAppender.assertAllExpectationsMatched(); + } finally { + Loggers.removeAppender(logger, mockLogAppender); + mockLogAppender.stop(); + } + } }