Skip to content

Commit 67d1666

Browse files
Implement review suggestions
1 parent 58d560a commit 67d1666

File tree

6 files changed

+15
-73
lines changed

6 files changed

+15
-73
lines changed

x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/CrossClusterQueryLogUnavailableRemotesIT.java

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@
1010
import org.apache.logging.log4j.Level;
1111
import org.apache.logging.log4j.LogManager;
1212
import org.apache.logging.log4j.Logger;
13-
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsAction;
14-
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest;
1513
import org.elasticsearch.common.logging.ESLogMessage;
1614
import org.elasticsearch.common.logging.Loggers;
1715
import org.elasticsearch.common.logging.MockAppender;
1816
import org.elasticsearch.common.settings.Settings;
19-
import org.elasticsearch.core.TimeValue;
2017
import org.elasticsearch.core.Tuple;
2118
import org.elasticsearch.xpack.esql.plugin.EsqlPlugin;
2219
import org.elasticsearch.xpack.esql.querylog.EsqlQueryLog;
@@ -68,6 +65,11 @@ protected boolean reuseClusters() {
6865
return false;
6966
}
7067

68+
@Override
69+
protected Settings nodeSettings() {
70+
return Settings.builder().put(super.nodeSettings()).put(EsqlPlugin.ESQL_QUERYLOG_THRESHOLD_TRACE_SETTING.getKey(), "0ms").build();
71+
}
72+
7173
/**
7274
* Tests that query logging works correctly when all remote clusters are unavailable
7375
* with skip_unavailable=true. This reproduces issue #142915 where NPE occurred due to
@@ -78,8 +80,6 @@ public void testQueryLoggingWithAllRemotesUnavailable() throws Exception {
7880
setupClusters(numClusters);
7981
setSkipUnavailable(REMOTE_CLUSTER_1, true);
8082

81-
enableQueryLogging();
82-
8383
try {
8484
cluster(REMOTE_CLUSTER_1).close();
8585

@@ -106,7 +106,6 @@ public void testQueryLoggingWithAllRemotesUnavailable() throws Exception {
106106
assertQueryLogged(query);
107107
}
108108
} finally {
109-
disableQueryLogging();
110109
clearSkipUnavailable(numClusters);
111110
}
112111
}
@@ -122,8 +121,6 @@ public void testQueryLoggingWithPartialRemoteFailure() throws Exception {
122121
setSkipUnavailable(REMOTE_CLUSTER_1, true);
123122
setSkipUnavailable(REMOTE_CLUSTER_2, true);
124123

125-
enableQueryLogging();
126-
127124
try {
128125
cluster(REMOTE_CLUSTER_1).close();
129126

@@ -160,7 +157,6 @@ public void testQueryLoggingWithPartialRemoteFailure() throws Exception {
160157
assertQueryLogged(query);
161158
}
162159
} finally {
163-
disableQueryLogging();
164160
clearSkipUnavailable(numClusters);
165161
}
166162
}
@@ -175,8 +171,6 @@ public void testQueryLoggingWithAllRemotesUnavailableLocalSucceeds() throws Exce
175171
setSkipUnavailable(REMOTE_CLUSTER_1, true);
176172
setSkipUnavailable(REMOTE_CLUSTER_2, true);
177173

178-
enableQueryLogging();
179-
180174
try {
181175
cluster(REMOTE_CLUSTER_1).close();
182176
cluster(REMOTE_CLUSTER_2).close();
@@ -210,29 +204,10 @@ public void testQueryLoggingWithAllRemotesUnavailableLocalSucceeds() throws Exce
210204
assertQueryLogged(query);
211205
}
212206
} finally {
213-
disableQueryLogging();
214207
clearSkipUnavailable(numClusters);
215208
}
216209
}
217210

218-
private void enableQueryLogging() throws Exception {
219-
client(LOCAL_CLUSTER).execute(
220-
ClusterUpdateSettingsAction.INSTANCE,
221-
new ClusterUpdateSettingsRequest(TimeValue.THIRTY_SECONDS, TimeValue.THIRTY_SECONDS).persistentSettings(
222-
Settings.builder().put(EsqlPlugin.ESQL_QUERYLOG_THRESHOLD_TRACE_SETTING.getKey(), "0ms")
223-
)
224-
).get();
225-
}
226-
227-
private void disableQueryLogging() throws Exception {
228-
client(LOCAL_CLUSTER).execute(
229-
ClusterUpdateSettingsAction.INSTANCE,
230-
new ClusterUpdateSettingsRequest(TimeValue.THIRTY_SECONDS, TimeValue.THIRTY_SECONDS).persistentSettings(
231-
Settings.builder().putNull(EsqlPlugin.ESQL_QUERYLOG_THRESHOLD_TRACE_SETTING.getKey())
232-
)
233-
).get();
234-
}
235-
236211
private void assertQueryLogged(String expectedQuery) {
237212
assertThat("Query should have been logged", appender.lastEvent(), is(notNullValue()));
238213
var msg = (ESLogMessage) appender.lastMessage();

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/action/EsqlExecutionInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public IncludeExecutionMetadata includeExecutionMetadata() {
172172
*/
173173
public void markEndQuery() {
174174
if (isMainPlan()) {
175-
queryProfile.stop();
175+
queryProfile.stopAllStartedMarkers();
176176
}
177177
}
178178

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/action/TimeSpanMarker.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ public TimeValue timeTook() {
6565
return timeSpan == null ? null : timeSpan.toTimeValue();
6666
}
6767

68+
/**
69+
* Returns true if this marker was started (regardless of whether it was stopped).
70+
*/
71+
public boolean wasStarted() {
72+
return timeSpanBuilder != null;
73+
}
74+
6875
public TimeValue timeSinceStarted() {
6976
return timeSpanBuilder != null ? timeSpanBuilder.stop().toTimeValue() : TimeValue.ZERO;
7077
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/querylog/EsqlQueryLog.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ private static void addResultFields(Map<String, Object> fieldMap, Result esqlRes
145145
for (TimeSpanMarker timeSpanMarker : esqlQueryProfile.timeSpanMarkers()) {
146146
TimeValue timeTook = timeSpanMarker.timeTook();
147147
if (timeTook == null) {
148-
logger.error("TimeSpanMarker [{}] was not stopped", timeSpanMarker.name());
148+
assert timeSpanMarker.wasStarted() == false
149+
: "TimeSpanMarker [" + timeSpanMarker.name() + "] was started but not stopped before query logging";
149150
continue;
150151
}
151152
String namePrefix = ELASTICSEARCH_QUERYLOG_PREFIX + timeSpanMarker.name();

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/session/EsqlCCSUtils.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ static boolean returnSuccessWithEmptyResult(EsqlExecutionInfo executionInfo, Exc
137137
static void updateExecutionInfoToReturnEmptyResult(EsqlExecutionInfo executionInfo, Exception e) {
138138
// This applies even for subplans - if we had an error and have to skip a cluster, then it will remain skipped.
139139
executionInfo.markEndQuery();
140-
executionInfo.queryProfile().stopAllStartedMarkers();
141140
Exception exceptionForResponse;
142141
if (e instanceof ConnectTransportException) {
143142
// when field-caps has no field info (since no clusters could be connected to or had matching indices)

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/querylog/EsqlQueryLogTests.java

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -212,44 +212,4 @@ private static TimeSpan randomTimeSpan() {
212212
return new TimeSpan(startNanos / 1_000_000, startNanos, stopNanos / 1_000_000, stopNanos);
213213
}
214214

215-
/**
216-
* Tests that query logging does not throw NPE when some TimeSpanMarkers have null timeTook values.
217-
* This can happen in scenarios like CCS queries where a remote cluster is unavailable.
218-
* See <a href="https://github.com/elastic/elasticsearch/issues/142915">issue #142915</a>.
219-
*/
220-
public void testQueryLoggingWithNullTimeSpanMarkers() {
221-
EsqlQueryLog queryLog = new EsqlQueryLog(settings, mockLogFieldProvider());
222-
String query = "from " + randomAlphaOfLength(10);
223-
224-
long tookNanos = randomLongBetween(40_000_000, 50_000_000);
225-
EsqlExecutionInfo executionInfo = getEsqlExecutionInfoWithNullTimeSpans(tookNanos);
226-
queryLog.onQueryPhase(
227-
new Versioned<>(
228-
new Result(List.of(), List.of(), EsqlTestUtils.TEST_CFG, DriverCompletionInfo.EMPTY, executionInfo),
229-
TransportVersion.current()
230-
),
231-
query
232-
);
233-
234-
assertThat(appender.lastEvent(), is(not(nullValue())));
235-
var msg = (ESLogMessage) appender.lastMessage();
236-
long took = Long.valueOf(msg.get(ELASTICSEARCH_QUERYLOG_TOOK));
237-
assertThat(took, is(tookNanos));
238-
assertThat(msg.get(ELASTICSEARCH_QUERYLOG_QUERY), is(query));
239-
assertThat(appender.getLastEventAndReset().getLevel(), equalTo(Level.WARN));
240-
}
241-
242-
private static EsqlExecutionInfo getEsqlExecutionInfoWithNullTimeSpans(long tookNanos) {
243-
return new EsqlExecutionInfo(Predicates.always(), EsqlExecutionInfo.IncludeExecutionMetadata.CCS_ONLY) {
244-
@Override
245-
public TimeValue overallTook() {
246-
return new TimeValue(tookNanos, TimeUnit.NANOSECONDS);
247-
}
248-
249-
@Override
250-
public EsqlQueryProfile queryProfile() {
251-
return new EsqlQueryProfile(null, null, null, null, null, null, 0);
252-
}
253-
};
254-
}
255215
}

0 commit comments

Comments
 (0)