From 02d18f5c1ead0a386e5a7a20d8e2927e28754a10 Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Thu, 17 Oct 2019 20:04:57 +0200 Subject: [PATCH] [7.x] Slow log must use separate underlying logger for each index BACKPORT(#47234) (#48176) * Slow log must use separate underlying logger for each index (#47234) SlowLog instances should not share the same underlying logger, as it would cause different indexes override each other levels. When creating underlying logger, unique per index identifier should be used. Name + IndexSettings.UUID Closes #42432 --- .../elasticsearch/index/IndexingSlowLog.java | 7 ++----- .../elasticsearch/index/SearchSlowLog.java | 10 ++++------ .../index/IndexingSlowLogTests.java | 20 +++++++++++++++++++ .../index/SearchSlowLogTests.java | 20 +++++++++++++++++++ 4 files changed, 46 insertions(+), 11 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/index/IndexingSlowLog.java b/server/src/main/java/org/elasticsearch/index/IndexingSlowLog.java index faeb9d3bc26..a3b87395a2b 100644 --- a/server/src/main/java/org/elasticsearch/index/IndexingSlowLog.java +++ b/server/src/main/java/org/elasticsearch/index/IndexingSlowLog.java @@ -56,8 +56,6 @@ public final class IndexingSlowLog implements IndexingOperationListener { */ private int maxSourceCharsToLog; - private SlowLogLevel level; - private final Logger indexLogger; private static final String INDEX_INDEXING_SLOWLOG_PREFIX = "index.indexing.slowlog"; @@ -94,7 +92,7 @@ public final class IndexingSlowLog implements IndexingOperationListener { }, Property.Dynamic, Property.IndexScope); IndexingSlowLog(IndexSettings indexSettings) { - this.indexLogger = LogManager.getLogger(INDEX_INDEXING_SLOWLOG_PREFIX + ".index"); + this.indexLogger = LogManager.getLogger(INDEX_INDEXING_SLOWLOG_PREFIX + ".index." + indexSettings.getUUID()); this.index = indexSettings.getIndex(); indexSettings.getScopedSettings().addSettingsUpdateConsumer(INDEX_INDEXING_SLOWLOG_REFORMAT_SETTING, this::setReformat); @@ -123,7 +121,6 @@ public final class IndexingSlowLog implements IndexingOperationListener { } private void setLevel(SlowLogLevel level) { - this.level = level; Loggers.setLevel(this.indexLogger, level.name()); } @@ -261,7 +258,7 @@ public final class IndexingSlowLog implements IndexingOperationListener { } SlowLogLevel getLevel() { - return level; + return SlowLogLevel.parse(indexLogger.getLevel().name()); } } diff --git a/server/src/main/java/org/elasticsearch/index/SearchSlowLog.java b/server/src/main/java/org/elasticsearch/index/SearchSlowLog.java index b4d8419d071..2fc4955efd0 100644 --- a/server/src/main/java/org/elasticsearch/index/SearchSlowLog.java +++ b/server/src/main/java/org/elasticsearch/index/SearchSlowLog.java @@ -50,8 +50,6 @@ public final class SearchSlowLog implements SearchOperationListener { private long fetchDebugThreshold; private long fetchTraceThreshold; - private SlowLogLevel level; - private final Logger queryLogger; private final Logger fetchLogger; @@ -88,8 +86,8 @@ public final class SearchSlowLog implements SearchOperationListener { public SearchSlowLog(IndexSettings indexSettings) { - this.queryLogger = LogManager.getLogger(INDEX_SEARCH_SLOWLOG_PREFIX + ".query"); - this.fetchLogger = LogManager.getLogger(INDEX_SEARCH_SLOWLOG_PREFIX + ".fetch"); + this.queryLogger = LogManager.getLogger(INDEX_SEARCH_SLOWLOG_PREFIX + ".query." + indexSettings.getUUID()); + this.fetchLogger = LogManager.getLogger(INDEX_SEARCH_SLOWLOG_PREFIX + ".fetch." + indexSettings.getUUID()); indexSettings.getScopedSettings().addSettingsUpdateConsumer(INDEX_SEARCH_SLOWLOG_THRESHOLD_QUERY_WARN_SETTING, this::setQueryWarnThreshold); @@ -122,7 +120,6 @@ public final class SearchSlowLog implements SearchOperationListener { } private void setLevel(SlowLogLevel level) { - this.level = level; Loggers.setLevel(queryLogger, level.name()); Loggers.setLevel(fetchLogger, level.name()); } @@ -297,6 +294,7 @@ public final class SearchSlowLog implements SearchOperationListener { } SlowLogLevel getLevel() { - return level; + assert queryLogger.getLevel().equals(fetchLogger.getLevel()); + return SlowLogLevel.parse(queryLogger.getLevel().name()); } } diff --git a/server/src/test/java/org/elasticsearch/index/IndexingSlowLogTests.java b/server/src/test/java/org/elasticsearch/index/IndexingSlowLogTests.java index 49747993ec1..719bc88b655 100644 --- a/server/src/test/java/org/elasticsearch/index/IndexingSlowLogTests.java +++ b/server/src/test/java/org/elasticsearch/index/IndexingSlowLogTests.java @@ -23,6 +23,7 @@ import com.fasterxml.jackson.core.JsonParseException; import org.apache.lucene.document.NumericDocValuesField; import org.elasticsearch.Version; import org.elasticsearch.cluster.metadata.IndexMetaData; +import org.elasticsearch.common.UUIDs; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; @@ -199,6 +200,25 @@ public class IndexingSlowLogTests extends ESTestCase { assertThat(cause, hasToString(containsString("No enum constant org.elasticsearch.index.SlowLogLevel.NOT A LEVEL"))); } assertEquals(SlowLogLevel.TRACE, log.getLevel()); + + metaData = newIndexMeta("index", Settings.builder() + .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetaData.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()) + .put(IndexingSlowLog.INDEX_INDEXING_SLOWLOG_LEVEL_SETTING.getKey(), SlowLogLevel.DEBUG) + .build()); + settings = new IndexSettings(metaData, Settings.EMPTY); + IndexingSlowLog debugLog = new IndexingSlowLog(settings); + + metaData = newIndexMeta("index", Settings.builder() + .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetaData.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()) + .put(IndexingSlowLog.INDEX_INDEXING_SLOWLOG_LEVEL_SETTING.getKey(), SlowLogLevel.INFO) + .build()); + settings = new IndexSettings(metaData, Settings.EMPTY); + IndexingSlowLog infoLog = new IndexingSlowLog(settings); + + assertEquals(SlowLogLevel.DEBUG, debugLog.getLevel()); + assertEquals(SlowLogLevel.INFO, infoLog.getLevel()); } public void testSetLevels() { diff --git a/server/src/test/java/org/elasticsearch/index/SearchSlowLogTests.java b/server/src/test/java/org/elasticsearch/index/SearchSlowLogTests.java index 4da6ef9a7ab..88e636fe8e9 100644 --- a/server/src/test/java/org/elasticsearch/index/SearchSlowLogTests.java +++ b/server/src/test/java/org/elasticsearch/index/SearchSlowLogTests.java @@ -22,6 +22,7 @@ package org.elasticsearch.index; import org.elasticsearch.Version; import org.elasticsearch.action.search.SearchTask; import org.elasticsearch.cluster.metadata.IndexMetaData; +import org.elasticsearch.common.UUIDs; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.util.BigArrays; @@ -190,6 +191,25 @@ public class SearchSlowLogTests extends ESSingleNodeTestCase { assertThat(cause, hasToString(containsString("No enum constant org.elasticsearch.index.SlowLogLevel.NOT A LEVEL"))); } assertEquals(SlowLogLevel.TRACE, log.getLevel()); + + metaData = newIndexMeta("index", Settings.builder() + .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetaData.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()) + .put(SearchSlowLog.INDEX_SEARCH_SLOWLOG_LEVEL.getKey(), SlowLogLevel.DEBUG) + .build()); + settings = new IndexSettings(metaData, Settings.EMPTY); + SearchSlowLog debugLog = new SearchSlowLog(settings); + + metaData = newIndexMeta("index", Settings.builder() + .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetaData.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()) + .put(SearchSlowLog.INDEX_SEARCH_SLOWLOG_LEVEL.getKey(), SlowLogLevel.INFO) + .build()); + settings = new IndexSettings(metaData, Settings.EMPTY); + SearchSlowLog infoLog = new SearchSlowLog(settings); + + assertEquals(SlowLogLevel.DEBUG, debugLog.getLevel()); + assertEquals(SlowLogLevel.INFO, infoLog.getLevel()); } public void testSetQueryLevels() {