Skip to content

Commit 324971c

Browse files
authored
Keep PPLQueryTask free of opensearch module types (#5924)
#5910 made PPLQueryTask implement ProgressTrackingTask from the opensearch module. Plugins such as alerting load PPLQueryTask with this plugin's classes plus core, ppl, and protocol, but not the opensearch module, so creating the task threw NoClassDefFoundError and crashed the node. Revert PPLQueryTask, drop ProgressTrackingTask, and keep each async query's tracker in a weak map keyed by its task. Add a test that the classes other plugins load reference no opensearch module types. Resolves #5923 Signed-off-by: Kai Huang <ahkcs@amazon.com>
1 parent 07b80d7 commit 324971c

6 files changed

Lines changed: 90 additions & 43 deletions

File tree

‎opensearch/src/main/java/org/opensearch/sql/opensearch/executor/progress/ProgressTrackingTask.java‎

Lines changed: 0 additions & 15 deletions
This file was deleted.

‎opensearch/src/main/java/org/opensearch/sql/opensearch/executor/progress/QueryProgressTracker.java‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,37 @@
55

66
package org.opensearch.sql.opensearch.executor.progress;
77

8+
import java.util.Collections;
89
import java.util.List;
10+
import java.util.Map;
911
import java.util.Optional;
12+
import java.util.WeakHashMap;
1013
import java.util.concurrent.CopyOnWriteArrayList;
1114
import org.opensearch.sql.opensearch.executor.OpenSearchQueryManager;
15+
import org.opensearch.tasks.CancellableTask;
1216

1317
/** Progress of one query: the mean of its index scans, each added at code generation. */
1418
public final class QueryProgressTracker {
1519

20+
/**
21+
* Trackers by query task. Kept beside the task rather than on it: plugins such as alerting load
22+
* the task class without this module, so it must not reference this module's types.
23+
*/
24+
private static final Map<CancellableTask, QueryProgressTracker> TRACKERS =
25+
Collections.synchronizedMap(new WeakHashMap<>());
26+
1627
private final List<SourceProgress> sources = new CopyOnWriteArrayList<>();
1728

29+
/** Starts tracking the progress of the query run by {@code task}. */
30+
public static QueryProgressTracker attach(CancellableTask task) {
31+
QueryProgressTracker tracker = new QueryProgressTracker();
32+
TRACKERS.put(task, tracker);
33+
return tracker;
34+
}
35+
1836
public static Optional<QueryProgressTracker> current() {
19-
return OpenSearchQueryManager.getCancellableTask() instanceof ProgressTrackingTask task
20-
? Optional.ofNullable(task.progressTracker())
21-
: Optional.empty();
37+
CancellableTask task = OpenSearchQueryManager.getCancellableTask();
38+
return task == null ? Optional.empty() : Optional.ofNullable(TRACKERS.get(task));
2239
}
2340

2441
public SourceProgress addSource() {

‎opensearch/src/test/java/org/opensearch/sql/opensearch/executor/progress/QueryProgressTrackerTest.java‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
import static org.junit.jupiter.api.Assertions.assertSame;
1010
import static org.junit.jupiter.api.Assertions.assertTrue;
1111
import static org.mockito.Mockito.mock;
12-
import static org.mockito.Mockito.when;
13-
import static org.mockito.Mockito.withSettings;
1412

1513
import org.junit.jupiter.api.AfterEach;
1614
import org.junit.jupiter.api.Test;
@@ -38,17 +36,15 @@ void fractionDone_isMeanOfSources() {
3836
}
3937

4038
@Test
41-
void current_readsTrackerFromProgressTrackingTask() {
42-
QueryProgressTracker tracker = new QueryProgressTracker();
43-
CancellableTask task =
44-
mock(CancellableTask.class, withSettings().extraInterfaces(ProgressTrackingTask.class));
45-
when(((ProgressTrackingTask) task).progressTracker()).thenReturn(tracker);
39+
void current_readsTrackerAttachedToTheTask() {
40+
CancellableTask task = mock(CancellableTask.class);
41+
QueryProgressTracker tracker = QueryProgressTracker.attach(task);
4642
OpenSearchQueryManager.setCancellableTask(task);
4743
assertSame(tracker, QueryProgressTracker.current().orElseThrow());
4844
}
4945

5046
@Test
51-
void current_isEmptyWithoutProgressTrackingTask() {
47+
void current_isEmptyWithoutAttachedTracker() {
5248
assertTrue(QueryProgressTracker.current().isEmpty());
5349
OpenSearchQueryManager.setCancellableTask(mock(CancellableTask.class));
5450
assertTrue(QueryProgressTracker.current().isEmpty());

‎plugin/src/main/java/org/opensearch/sql/plugin/transport/PPLQueryTask.java‎

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,9 @@
77

88
import java.util.Map;
99
import org.opensearch.core.tasks.TaskId;
10-
import org.opensearch.sql.opensearch.executor.progress.ProgressTrackingTask;
11-
import org.opensearch.sql.opensearch.executor.progress.QueryProgressTracker;
1210
import org.opensearch.tasks.CancellableTask;
1311

14-
public class PPLQueryTask extends CancellableTask implements ProgressTrackingTask {
15-
16-
private volatile QueryProgressTracker progressTracker;
12+
public class PPLQueryTask extends CancellableTask {
1713

1814
public PPLQueryTask(
1915
long id,
@@ -29,15 +25,4 @@ public PPLQueryTask(
2925
public boolean shouldCancelChildrenOnCancellation() {
3026
return true;
3127
}
32-
33-
/** Called only for async submissions. */
34-
public QueryProgressTracker trackProgress() {
35-
progressTracker = new QueryProgressTracker();
36-
return progressTracker;
37-
}
38-
39-
@Override
40-
public QueryProgressTracker progressTracker() {
41-
return progressTracker;
42-
}
4328
}

‎plugin/src/main/java/org/opensearch/sql/plugin/transport/TransportPPLQueryAction.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ private void submitAsync(
318318
throw new IllegalArgumentException("Async PPL queries require a cancellable PPLQueryTask");
319319
}
320320
Runnable cancelExecution = () -> pplQueryTask.cancel("async PPL query cancelled");
321-
QueryProgressTracker progressTracker = pplQueryTask.trackProgress();
321+
QueryProgressTracker progressTracker = QueryProgressTracker.attach(pplQueryTask);
322322
QueryRunner runner =
323323
new PPLQueryRunner(
324324
pplService,
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.opensearch.sql.plugin.transport;
7+
8+
import static org.junit.Assert.assertTrue;
9+
10+
import java.lang.reflect.Constructor;
11+
import java.lang.reflect.Field;
12+
import java.lang.reflect.Method;
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
import org.junit.Test;
16+
17+
/**
18+
* Alerting and other plugins call {@code PPLQueryAction} with this module's classes plus core, ppl,
19+
* and protocol, but without the {@code opensearch} module (#5923). The classes they load must not
20+
* reference that module's types.
21+
*/
22+
public class ThinJarClassesTest {
23+
24+
private static final String EXCLUDED_PACKAGE = "org.opensearch.sql.opensearch.";
25+
26+
private static final List<Class<?>> LOADED_BY_OTHER_PLUGINS =
27+
List.of(
28+
PPLQueryAction.class,
29+
TransportPPLQueryRequest.class,
30+
TransportPPLQueryResponse.class,
31+
PPLQueryTask.class);
32+
33+
@Test
34+
public void classesLoadedByOtherPluginsDoNotReferenceTheOpenSearchModule() {
35+
List<String> violations = new ArrayList<>();
36+
for (Class<?> type : LOADED_BY_OTHER_PLUGINS) {
37+
for (Class<?> referenced : referencedTypes(type)) {
38+
if (referenced.getName().startsWith(EXCLUDED_PACKAGE)) {
39+
violations.add(type.getSimpleName() + " -> " + referenced.getName());
40+
}
41+
}
42+
}
43+
assertTrue("Types outside the thin jar: " + violations, violations.isEmpty());
44+
}
45+
46+
private static List<Class<?>> referencedTypes(Class<?> type) {
47+
List<Class<?>> types = new ArrayList<>();
48+
for (Class<?> c = type; c != null && c != Object.class; c = c.getSuperclass()) {
49+
types.add(c);
50+
types.addAll(List.of(c.getInterfaces()));
51+
for (Field field : c.getDeclaredFields()) {
52+
types.add(field.getType());
53+
}
54+
for (Method method : c.getDeclaredMethods()) {
55+
types.add(method.getReturnType());
56+
types.addAll(List.of(method.getParameterTypes()));
57+
}
58+
for (Constructor<?> constructor : c.getDeclaredConstructors()) {
59+
types.addAll(List.of(constructor.getParameterTypes()));
60+
}
61+
}
62+
return types;
63+
}
64+
}

0 commit comments

Comments
 (0)