From cdae4945a66b5cf01bb657135f5024a646bb8849 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Thu, 14 Jan 2016 17:22:27 +0100 Subject: [PATCH] add unittest for indexing slow log settings --- .../elasticsearch/index/IndexingSlowLog.java | 39 ++++- .../index/IndexingSlowLogTests.java | 156 ++++++++++++++++++ 2 files changed, 189 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/index/IndexingSlowLog.java b/core/src/main/java/org/elasticsearch/index/IndexingSlowLog.java index 859985c9580..e35ee4b3c80 100644 --- a/core/src/main/java/org/elasticsearch/index/IndexingSlowLog.java +++ b/core/src/main/java/org/elasticsearch/index/IndexingSlowLog.java @@ -40,9 +40,7 @@ import java.util.logging.Level; /** */ public final class IndexingSlowLog implements IndexingOperationListener { - private volatile boolean reformat; - private volatile long indexWarnThreshold; private volatile long indexInfoThreshold; private volatile long indexDebugThreshold; @@ -60,10 +58,10 @@ public final class IndexingSlowLog implements IndexingOperationListener { private final ESLogger deleteLogger; private static final String INDEX_INDEXING_SLOWLOG_PREFIX = "index.indexing.slowlog"; - public static final Setting INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_WARN_SETTING = Setting.timeSetting(INDEX_INDEXING_SLOWLOG_PREFIX +".threshold.index.warn", TimeValue.timeValueNanos(-1), TimeValue.timeValueNanos(-1), true, Setting.Scope.INDEX); - public static final Setting INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_INFO_SETTING = Setting.timeSetting(INDEX_INDEXING_SLOWLOG_PREFIX +".threshold.index.info", TimeValue.timeValueNanos(-1), TimeValue.timeValueNanos(-1), true, Setting.Scope.INDEX); - public static final Setting INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_DEBUG_SETTING = Setting.timeSetting(INDEX_INDEXING_SLOWLOG_PREFIX +".threshold.index.debug", TimeValue.timeValueNanos(-1), TimeValue.timeValueNanos(-1), true, Setting.Scope.INDEX); - public static final Setting INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_TRACE_SETTING = Setting.timeSetting(INDEX_INDEXING_SLOWLOG_PREFIX +".threshold.index.trace", TimeValue.timeValueNanos(-1), TimeValue.timeValueNanos(-1), true, Setting.Scope.INDEX); + public static final Setting INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_WARN_SETTING = Setting.timeSetting(INDEX_INDEXING_SLOWLOG_PREFIX +".threshold.index.warn", TimeValue.timeValueNanos(-1), TimeValue.timeValueMillis(-1), true, Setting.Scope.INDEX); + public static final Setting INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_INFO_SETTING = Setting.timeSetting(INDEX_INDEXING_SLOWLOG_PREFIX +".threshold.index.info", TimeValue.timeValueNanos(-1), TimeValue.timeValueMillis(-1), true, Setting.Scope.INDEX); + public static final Setting INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_DEBUG_SETTING = Setting.timeSetting(INDEX_INDEXING_SLOWLOG_PREFIX +".threshold.index.debug", TimeValue.timeValueNanos(-1), TimeValue.timeValueMillis(-1), true, Setting.Scope.INDEX); + public static final Setting INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_TRACE_SETTING = Setting.timeSetting(INDEX_INDEXING_SLOWLOG_PREFIX +".threshold.index.trace", TimeValue.timeValueNanos(-1), TimeValue.timeValueMillis(-1), true, Setting.Scope.INDEX); public static final Setting INDEX_INDEXING_SLOWLOG_REFORMAT_SETTING = Setting.boolSetting(INDEX_INDEXING_SLOWLOG_PREFIX +".reformat", true, true, Setting.Scope.INDEX); public static final Setting INDEX_INDEXING_SLOWLOG_LEVEL_SETTING = new Setting<>(INDEX_INDEXING_SLOWLOG_PREFIX +".level", Level.TRACE.name(), Level::parse, true, Setting.Scope.INDEX); /** @@ -199,4 +197,33 @@ public final class IndexingSlowLog implements IndexingOperationListener { return valueOf(level.toUpperCase(Locale.ROOT)); } } + + boolean isReformat() { + return reformat; + } + + long getIndexWarnThreshold() { + return indexWarnThreshold; + } + + long getIndexInfoThreshold() { + return indexInfoThreshold; + } + + long getIndexTraceThreshold() { + return indexTraceThreshold; + } + + long getIndexDebugThreshold() { + return indexDebugThreshold; + } + + int getMaxSourceCharsToLog() { + return maxSourceCharsToLog; + } + + Level getLevel() { + return level; + } + } diff --git a/core/src/test/java/org/elasticsearch/index/IndexingSlowLogTests.java b/core/src/test/java/org/elasticsearch/index/IndexingSlowLogTests.java index e39c0a805fe..59cf6861570 100644 --- a/core/src/test/java/org/elasticsearch/index/IndexingSlowLogTests.java +++ b/core/src/test/java/org/elasticsearch/index/IndexingSlowLogTests.java @@ -22,7 +22,12 @@ package org.elasticsearch.index; import org.apache.lucene.document.Field.Store; import org.apache.lucene.document.IntField; import org.apache.lucene.document.StringField; +import org.elasticsearch.Version; +import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.settings.Setting; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.IndexingSlowLog.SlowLogParsedDocumentPrinter; import org.elasticsearch.index.mapper.ParsedDocument; @@ -51,4 +56,155 @@ public class IndexingSlowLogTests extends ESTestCase { p = new SlowLogParsedDocumentPrinter(pd, 10, true, 3); assertThat(p.toString(), containsString("source[{\"f]")); } + + public void testReformatSetting() { + IndexMetaData metaData = newIndexMeta("index", Settings.settingsBuilder() + .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexingSlowLog.INDEX_INDEXING_SLOWLOG_REFORMAT_SETTING.getKey(), false) + .build()); + IndexSettings settings = new IndexSettings(metaData, Settings.EMPTY); + IndexingSlowLog log = new IndexingSlowLog(settings); + assertFalse(log.isReformat()); + settings.updateIndexMetaData(newIndexMeta("index", Settings.builder().put(IndexingSlowLog.INDEX_INDEXING_SLOWLOG_REFORMAT_SETTING.getKey(), "true").build())); + assertTrue(log.isReformat()); + + settings.updateIndexMetaData(newIndexMeta("index", Settings.builder().put(IndexingSlowLog.INDEX_INDEXING_SLOWLOG_REFORMAT_SETTING.getKey(), "false").build())); + assertFalse(log.isReformat()); + + settings.updateIndexMetaData(newIndexMeta("index", Settings.EMPTY)); + assertTrue(log.isReformat()); + + metaData = newIndexMeta("index", Settings.settingsBuilder() + .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) + .build()); + settings = new IndexSettings(metaData, Settings.EMPTY); + log = new IndexingSlowLog(settings); + assertTrue(log.isReformat()); + try { + settings.updateIndexMetaData(newIndexMeta("index", Settings.builder().put(IndexingSlowLog.INDEX_INDEXING_SLOWLOG_REFORMAT_SETTING.getKey(), "NOT A BOOLEAN").build())); + fail(); + } catch (IllegalArgumentException ex) { + assertEquals(ex.getMessage(), "Failed to parse value [NOT A BOOLEAN] cannot be parsed to boolean [ true/1/on/yes OR false/0/off/no ]"); + } + assertTrue(log.isReformat()); + } + + public void testLevelSetting() { + IndexingSlowLog.Level level = randomFrom(IndexingSlowLog.Level.values()); + IndexMetaData metaData = newIndexMeta("index", Settings.settingsBuilder() + .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexingSlowLog.INDEX_INDEXING_SLOWLOG_LEVEL_SETTING.getKey(), level) + .build()); + IndexSettings settings = new IndexSettings(metaData, Settings.EMPTY); + IndexingSlowLog log = new IndexingSlowLog(settings); + assertEquals(level, log.getLevel()); + level = randomFrom(IndexingSlowLog.Level.values()); + settings.updateIndexMetaData(newIndexMeta("index", Settings.builder().put(IndexingSlowLog.INDEX_INDEXING_SLOWLOG_LEVEL_SETTING.getKey(), level).build())); + assertEquals(level, log.getLevel()); + level = randomFrom(IndexingSlowLog.Level.values()); + settings.updateIndexMetaData(newIndexMeta("index", Settings.builder().put(IndexingSlowLog.INDEX_INDEXING_SLOWLOG_LEVEL_SETTING.getKey(), level).build())); + assertEquals(level, log.getLevel()); + + + settings.updateIndexMetaData(newIndexMeta("index", Settings.builder().put(IndexingSlowLog.INDEX_INDEXING_SLOWLOG_LEVEL_SETTING.getKey(), level).build())); + assertEquals(level, log.getLevel()); + + settings.updateIndexMetaData(newIndexMeta("index", Settings.EMPTY)); + assertEquals(IndexingSlowLog.Level.TRACE, log.getLevel()); + + metaData = newIndexMeta("index", Settings.settingsBuilder() + .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) + .build()); + settings = new IndexSettings(metaData, Settings.EMPTY); + log = new IndexingSlowLog(settings); + assertTrue(log.isReformat()); + try { + settings.updateIndexMetaData(newIndexMeta("index", Settings.builder().put(IndexingSlowLog.INDEX_INDEXING_SLOWLOG_LEVEL_SETTING.getKey(), "NOT A LEVEL").build())); + fail(); + } catch (IllegalArgumentException ex) { + assertEquals(ex.getMessage(), "No enum constant org.elasticsearch.index.IndexingSlowLog.Level.NOT A LEVEL"); + } + assertEquals(IndexingSlowLog.Level.TRACE, log.getLevel()); + } + + public void testSetLevels() { + IndexMetaData metaData = newIndexMeta("index", Settings.settingsBuilder() + .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexingSlowLog.INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_TRACE_SETTING.getKey(), "100ms") + .put(IndexingSlowLog.INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_DEBUG_SETTING.getKey(), "200ms") + .put(IndexingSlowLog.INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_INFO_SETTING.getKey(), "300ms") + .put(IndexingSlowLog.INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_WARN_SETTING.getKey(), "400ms") + .build()); + IndexSettings settings = new IndexSettings(metaData, Settings.EMPTY); + IndexingSlowLog log = new IndexingSlowLog(settings); + assertEquals(TimeValue.timeValueMillis(100).nanos(), log.getIndexTraceThreshold()); + assertEquals(TimeValue.timeValueMillis(200).nanos(), log.getIndexDebugThreshold()); + assertEquals(TimeValue.timeValueMillis(300).nanos(), log.getIndexInfoThreshold()); + assertEquals(TimeValue.timeValueMillis(400).nanos(), log.getIndexWarnThreshold()); + + settings.updateIndexMetaData(newIndexMeta("index", Settings.builder().put(IndexingSlowLog.INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_TRACE_SETTING.getKey(), "120ms") + .put(IndexingSlowLog.INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_DEBUG_SETTING.getKey(), "220ms") + .put(IndexingSlowLog.INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_INFO_SETTING.getKey(), "320ms") + .put(IndexingSlowLog.INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_WARN_SETTING.getKey(), "420ms").build())); + + + assertEquals(TimeValue.timeValueMillis(120).nanos(), log.getIndexTraceThreshold()); + assertEquals(TimeValue.timeValueMillis(220).nanos(), log.getIndexDebugThreshold()); + assertEquals(TimeValue.timeValueMillis(320).nanos(), log.getIndexInfoThreshold()); + assertEquals(TimeValue.timeValueMillis(420).nanos(), log.getIndexWarnThreshold()); + + metaData = newIndexMeta("index", Settings.settingsBuilder() + .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) + .build()); + settings.updateIndexMetaData(metaData); + assertEquals(TimeValue.timeValueMillis(-1).nanos(), log.getIndexTraceThreshold()); + assertEquals(TimeValue.timeValueMillis(-1).nanos(), log.getIndexDebugThreshold()); + assertEquals(TimeValue.timeValueMillis(-1).nanos(), log.getIndexInfoThreshold()); + assertEquals(TimeValue.timeValueMillis(-1).nanos(), log.getIndexWarnThreshold()); + + settings = new IndexSettings(metaData, Settings.EMPTY); + log = new IndexingSlowLog(settings); + + assertEquals(TimeValue.timeValueMillis(-1).nanos(), log.getIndexTraceThreshold()); + assertEquals(TimeValue.timeValueMillis(-1).nanos(), log.getIndexDebugThreshold()); + assertEquals(TimeValue.timeValueMillis(-1).nanos(), log.getIndexInfoThreshold()); + assertEquals(TimeValue.timeValueMillis(-1).nanos(), log.getIndexWarnThreshold()); + try { + settings.updateIndexMetaData(newIndexMeta("index", Settings.builder().put(IndexingSlowLog.INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_TRACE_SETTING.getKey(), "NOT A TIME VALUE").build())); + fail(); + } catch (IllegalArgumentException ex) { + assertEquals(ex.getMessage(), "Failed to parse setting [index.indexing.slowlog.threshold.index.trace] with value [NOT A TIME VALUE] as a time value: unit is missing or unrecognized"); + } + + try { + settings.updateIndexMetaData(newIndexMeta("index", Settings.builder().put(IndexingSlowLog.INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_DEBUG_SETTING.getKey(), "NOT A TIME VALUE").build())); + fail(); + } catch (IllegalArgumentException ex) { + assertEquals(ex.getMessage(), "Failed to parse setting [index.indexing.slowlog.threshold.index.debug] with value [NOT A TIME VALUE] as a time value: unit is missing or unrecognized"); + } + + try { + settings.updateIndexMetaData(newIndexMeta("index", Settings.builder().put(IndexingSlowLog.INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_INFO_SETTING.getKey(), "NOT A TIME VALUE").build())); + fail(); + } catch (IllegalArgumentException ex) { + assertEquals(ex.getMessage(), "Failed to parse setting [index.indexing.slowlog.threshold.index.info] with value [NOT A TIME VALUE] as a time value: unit is missing or unrecognized"); + } + + try { + settings.updateIndexMetaData(newIndexMeta("index", Settings.builder().put(IndexingSlowLog.INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_WARN_SETTING.getKey(), "NOT A TIME VALUE").build())); + fail(); + } catch (IllegalArgumentException ex) { + assertEquals(ex.getMessage(), "Failed to parse setting [index.indexing.slowlog.threshold.index.warn] with value [NOT A TIME VALUE] as a time value: unit is missing or unrecognized"); + } + } + + private IndexMetaData newIndexMeta(String name, Settings indexSettings) { + Settings build = Settings.settingsBuilder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1) + .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1) + .put(indexSettings) + .build(); + IndexMetaData metaData = IndexMetaData.builder(name).settings(build).build(); + return metaData; + } }