Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rjernst committed Dec 27, 2024
1 parent 2188223 commit 5a274c2
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 24 deletions.
5 changes: 3 additions & 2 deletions server/src/main/java/org/elasticsearch/index/IndexModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,9 @@ public IndexModule(
this.engineFactory = Objects.requireNonNull(engineFactory);
// Need to have a mutable arraylist for plugins to add listeners to it
this.searchOperationListeners = new ArrayList<>(searchOperationListeners);
this.searchOperationListeners.add(new SearchSlowLog(indexSettings, slowLogFieldProvider));
this.indexOperationListeners.add(new IndexingSlowLog(indexSettings, slowLogFieldProvider));
SlowLogFields slowLogFields = slowLogFieldProvider.create(indexSettings);
this.searchOperationListeners.add(new SearchSlowLog(indexSettings, slowLogFields));
this.indexOperationListeners.add(new IndexingSlowLog(indexSettings, slowLogFields));
this.directoryFactories = Collections.unmodifiableMap(directoryFactories);
this.allowExpensiveQueries = allowExpensiveQueries;
this.expressionResolver = expressionResolver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ public final class IndexingSlowLog implements IndexingOperationListener {
Property.IndexScope
);

IndexingSlowLog(IndexSettings indexSettings, SlowLogFieldProvider slowLogFieldProvider) {
this.slowLogFields = slowLogFieldProvider.create(indexSettings);
IndexingSlowLog(IndexSettings indexSettings, SlowLogFields slowLogFields) {
this.slowLogFields = slowLogFields;
this.index = indexSettings.getIndex();

indexSettings.getScopedSettings().addSettingsUpdateConsumer(INDEX_INDEXING_SLOWLOG_REFORMAT_SETTING, this::setReformat);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ public final class SearchSlowLog implements SearchOperationListener {

private static final ToXContent.Params FORMAT_PARAMS = new ToXContent.MapParams(Collections.singletonMap("pretty", "false"));

public SearchSlowLog(IndexSettings indexSettings, SlowLogFieldProvider slowLogFieldProvider) {
this.slowLogFields = slowLogFieldProvider.create(indexSettings);
public SearchSlowLog(IndexSettings indexSettings, SlowLogFields slowLogFields) {
this.slowLogFields = slowLogFields;
indexSettings.getScopedSettings()
.addSettingsUpdateConsumer(INDEX_SEARCH_SLOWLOG_THRESHOLD_QUERY_WARN_SETTING, this::setQueryWarnThreshold);
this.queryWarnThreshold = indexSettings.getValue(INDEX_SEARCH_SLOWLOG_THRESHOLD_QUERY_WARN_SETTING).nanos();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void testLevelPrecedence() {
String uuid = UUIDs.randomBase64UUID();
IndexMetadata metadata = createIndexMetadata("index-precedence", settings(uuid));
IndexSettings settings = new IndexSettings(metadata, Settings.EMPTY);
IndexingSlowLog log = new IndexingSlowLog(settings, mock(SlowLogFieldProvider.class));
IndexingSlowLog log = new IndexingSlowLog(settings, mock(SlowLogFields.class));

ParsedDocument doc = EngineTestCase.createParsedDoc("1", null);
Engine.Index index = new Engine.Index(Uid.encodeId("doc_id"), randomNonNegativeLong(), doc);
Expand Down Expand Up @@ -142,7 +142,7 @@ public void testTwoLoggersDifferentLevel() {
),
Settings.EMPTY
);
IndexingSlowLog log1 = new IndexingSlowLog(index1Settings, mock(SlowLogFieldProvider.class));
IndexingSlowLog log1 = new IndexingSlowLog(index1Settings, mock(SlowLogFields.class));

IndexSettings index2Settings = new IndexSettings(
createIndexMetadata(
Expand All @@ -155,7 +155,7 @@ public void testTwoLoggersDifferentLevel() {
),
Settings.EMPTY
);
IndexingSlowLog log2 = new IndexingSlowLog(index2Settings, mock(SlowLogFieldProvider.class));
IndexingSlowLog log2 = new IndexingSlowLog(index2Settings, mock(SlowLogFields.class));

ParsedDocument doc = EngineTestCase.createParsedDoc("1", null);
Engine.Index index = new Engine.Index(Uid.encodeId("doc_id"), randomNonNegativeLong(), doc);
Expand All @@ -179,12 +179,12 @@ public void testMultipleSlowLoggersUseSingleLog4jLogger() {
LoggerContext context = (LoggerContext) LogManager.getContext(false);

IndexSettings index1Settings = new IndexSettings(createIndexMetadata("index1", settings(UUIDs.randomBase64UUID())), Settings.EMPTY);
IndexingSlowLog log1 = new IndexingSlowLog(index1Settings, mock(SlowLogFieldProvider.class));
IndexingSlowLog log1 = new IndexingSlowLog(index1Settings, mock(SlowLogFields.class));

int numberOfLoggersBefore = context.getLoggers().size();

IndexSettings index2Settings = new IndexSettings(createIndexMetadata("index2", settings(UUIDs.randomBase64UUID())), Settings.EMPTY);
IndexingSlowLog log2 = new IndexingSlowLog(index2Settings, mock(SlowLogFieldProvider.class));
IndexingSlowLog log2 = new IndexingSlowLog(index2Settings, mock(SlowLogFields.class));
context = (LoggerContext) LogManager.getContext(false);

int numberOfLoggersAfter = context.getLoggers().size();
Expand Down Expand Up @@ -355,7 +355,7 @@ public void testReformatSetting() {
.build()
);
IndexSettings settings = new IndexSettings(metadata, Settings.EMPTY);
IndexingSlowLog log = new IndexingSlowLog(settings, mock(SlowLogFieldProvider.class));
IndexingSlowLog log = new IndexingSlowLog(settings, mock(SlowLogFields.class));
assertFalse(log.isReformat());
settings.updateIndexMetadata(
newIndexMeta("index", Settings.builder().put(IndexingSlowLog.INDEX_INDEXING_SLOWLOG_REFORMAT_SETTING.getKey(), "true").build())
Expand All @@ -372,7 +372,7 @@ public void testReformatSetting() {

metadata = newIndexMeta("index", Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current()).build());
settings = new IndexSettings(metadata, Settings.EMPTY);
log = new IndexingSlowLog(settings, mock(SlowLogFieldProvider.class));
log = new IndexingSlowLog(settings, mock(SlowLogFields.class));
assertTrue(log.isReformat());
try {
settings.updateIndexMetadata(
Expand Down Expand Up @@ -405,7 +405,7 @@ public void testSetLevels() {
.build()
);
IndexSettings settings = new IndexSettings(metadata, Settings.EMPTY);
IndexingSlowLog log = new IndexingSlowLog(settings, mock(SlowLogFieldProvider.class));
IndexingSlowLog log = new IndexingSlowLog(settings, mock(SlowLogFields.class));
assertEquals(TimeValue.timeValueMillis(100).nanos(), log.getIndexTraceThreshold());
assertEquals(TimeValue.timeValueMillis(200).nanos(), log.getIndexDebugThreshold());
assertEquals(TimeValue.timeValueMillis(300).nanos(), log.getIndexInfoThreshold());
Expand Down Expand Up @@ -436,7 +436,7 @@ public void testSetLevels() {
assertEquals(TimeValue.timeValueMillis(-1).nanos(), log.getIndexWarnThreshold());

settings = new IndexSettings(metadata, Settings.EMPTY);
log = new IndexingSlowLog(settings, mock(SlowLogFieldProvider.class));
log = new IndexingSlowLog(settings, mock(SlowLogFields.class));

assertEquals(TimeValue.timeValueMillis(-1).nanos(), log.getIndexTraceThreshold());
assertEquals(TimeValue.timeValueMillis(-1).nanos(), log.getIndexDebugThreshold());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void testLevelPrecedence() {
try (SearchContext ctx = searchContextWithSourceAndTask(createIndex("index"))) {
String uuid = UUIDs.randomBase64UUID();
IndexSettings settings = new IndexSettings(createIndexMetadata("index", settings(uuid)), Settings.EMPTY);
SearchSlowLog log = new SearchSlowLog(settings, mock(SlowLogFieldProvider.class));
SearchSlowLog log = new SearchSlowLog(settings, mock(SlowLogFields.class));

// For this test, when level is not breached, the level below should be used.
{
Expand Down Expand Up @@ -187,7 +187,7 @@ public void testTwoLoggersDifferentLevel() {
),
Settings.EMPTY
);
SearchSlowLog log1 = new SearchSlowLog(settings1, mock(SlowLogFieldProvider.class));
SearchSlowLog log1 = new SearchSlowLog(settings1, mock(SlowLogFields.class));

IndexSettings settings2 = new IndexSettings(
createIndexMetadata(
Expand All @@ -200,7 +200,7 @@ public void testTwoLoggersDifferentLevel() {
),
Settings.EMPTY
);
SearchSlowLog log2 = new SearchSlowLog(settings2, mock(SlowLogFieldProvider.class));
SearchSlowLog log2 = new SearchSlowLog(settings2, mock(SlowLogFields.class));

{
// threshold set on WARN only, should not log
Expand All @@ -223,15 +223,15 @@ public void testMultipleSlowLoggersUseSingleLog4jLogger() {

try (SearchContext ctx1 = searchContextWithSourceAndTask(createIndex("index-1"))) {
IndexSettings settings1 = new IndexSettings(createIndexMetadata("index-1", settings(UUIDs.randomBase64UUID())), Settings.EMPTY);
SearchSlowLog log1 = new SearchSlowLog(settings1, mock(SlowLogFieldProvider.class));
SearchSlowLog log1 = new SearchSlowLog(settings1, mock(SlowLogFields.class));
int numberOfLoggersBefore = context.getLoggers().size();

try (SearchContext ctx2 = searchContextWithSourceAndTask(createIndex("index-2"))) {
IndexSettings settings2 = new IndexSettings(
createIndexMetadata("index-2", settings(UUIDs.randomBase64UUID())),
Settings.EMPTY
);
SearchSlowLog log2 = new SearchSlowLog(settings2, mock(SlowLogFieldProvider.class));
SearchSlowLog log2 = new SearchSlowLog(settings2, mock(SlowLogFields.class));

int numberOfLoggersAfter = context.getLoggers().size();
assertThat(numberOfLoggersAfter, equalTo(numberOfLoggersBefore));
Expand Down Expand Up @@ -323,7 +323,7 @@ public void testSetQueryLevels() {
.build()
);
IndexSettings settings = new IndexSettings(metadata, Settings.EMPTY);
SearchSlowLog log = new SearchSlowLog(settings, mock(SlowLogFieldProvider.class));
SearchSlowLog log = new SearchSlowLog(settings, mock(SlowLogFields.class));
assertEquals(TimeValue.timeValueMillis(100).nanos(), log.getQueryTraceThreshold());
assertEquals(TimeValue.timeValueMillis(200).nanos(), log.getQueryDebugThreshold());
assertEquals(TimeValue.timeValueMillis(300).nanos(), log.getQueryInfoThreshold());
Expand Down Expand Up @@ -354,7 +354,7 @@ public void testSetQueryLevels() {
assertEquals(TimeValue.timeValueMillis(-1).nanos(), log.getQueryWarnThreshold());

settings = new IndexSettings(metadata, Settings.EMPTY);
log = new SearchSlowLog(settings, mock(SlowLogFieldProvider.class));
log = new SearchSlowLog(settings, mock(SlowLogFields.class));

assertEquals(TimeValue.timeValueMillis(-1).nanos(), log.getQueryTraceThreshold());
assertEquals(TimeValue.timeValueMillis(-1).nanos(), log.getQueryDebugThreshold());
Expand Down Expand Up @@ -429,7 +429,7 @@ public void testSetFetchLevels() {
.build()
);
IndexSettings settings = new IndexSettings(metadata, Settings.EMPTY);
SearchSlowLog log = new SearchSlowLog(settings, mock(SlowLogFieldProvider.class));
SearchSlowLog log = new SearchSlowLog(settings, mock(SlowLogFields.class));
assertEquals(TimeValue.timeValueMillis(100).nanos(), log.getFetchTraceThreshold());
assertEquals(TimeValue.timeValueMillis(200).nanos(), log.getFetchDebugThreshold());
assertEquals(TimeValue.timeValueMillis(300).nanos(), log.getFetchInfoThreshold());
Expand Down Expand Up @@ -460,7 +460,7 @@ public void testSetFetchLevels() {
assertEquals(TimeValue.timeValueMillis(-1).nanos(), log.getFetchWarnThreshold());

settings = new IndexSettings(metadata, Settings.EMPTY);
log = new SearchSlowLog(settings, mock(SlowLogFieldProvider.class));
log = new SearchSlowLog(settings, mock(SlowLogFields.class));

assertEquals(TimeValue.timeValueMillis(-1).nanos(), log.getFetchTraceThreshold());
assertEquals(TimeValue.timeValueMillis(-1).nanos(), log.getFetchDebugThreshold());
Expand Down

0 comments on commit 5a274c2

Please sign in to comment.