10
10
11
11
import org .opensearch .common .settings .Settings ;
12
12
import org .opensearch .test .OpenSearchTestCase ;
13
+ import org .opensearch .threadpool .ThreadPool ;
14
+ import org .junit .After ;
13
15
14
16
import java .util .concurrent .TimeUnit ;
15
17
import java .util .function .Function ;
23
25
* based on the time taken for each event.
24
26
*/
25
27
public class QueueResizableOpenSearchThreadPoolExecutorTests extends OpenSearchTestCase {
26
- public void testResizeQueueSameSize () throws Exception {
27
- ThreadContext context = new ThreadContext ( Settings . EMPTY ) ;
28
- ResizableBlockingQueue < Runnable > queue = new ResizableBlockingQueue <>( ConcurrentCollections .< Runnable > newBlockingQueue (), 2000 ) ;
28
+ private QueueResizableOpenSearchThreadPoolExecutor executor ;
29
+ private ResizableBlockingQueue < Runnable > queue ;
30
+ private int measureWindow ;
29
31
32
+ private void createExecutor (int queueSize , Function <Runnable , WrappedRunnable > runnableWrapper ) {
30
33
int threads = randomIntBetween (1 , 10 );
31
- int measureWindow = randomIntBetween (100 , 200 );
32
- logger .info ("--> auto-queue with a measurement window of {} tasks" , measureWindow );
33
- QueueResizableOpenSearchThreadPoolExecutor executor = new QueueResizableOpenSearchThreadPoolExecutor (
34
+ measureWindow = randomIntBetween (100 , 200 );
35
+ ThreadContext context = new ThreadContext (Settings .EMPTY );
36
+ this .queue = new ResizableBlockingQueue <>(ConcurrentCollections .newBlockingQueue (), queueSize );
37
+ this .executor = new QueueResizableOpenSearchThreadPoolExecutor (
34
38
"test-threadpool" ,
35
39
threads ,
36
40
threads ,
37
41
1000 ,
38
42
TimeUnit .MILLISECONDS ,
39
43
queue ,
40
- fastWrapper () ,
44
+ runnableWrapper ,
41
45
OpenSearchExecutors .daemonThreadFactory ("queuetest" ),
42
46
new OpenSearchAbortPolicy (),
43
47
context
44
48
);
45
49
executor .prestartAllCoreThreads ();
46
50
logger .info ("--> executor: {}" , executor );
51
+ }
52
+
53
+ @ After
54
+ public void stopExecutor () {
55
+ ThreadPool .terminate (executor , 10 , TimeUnit .SECONDS );
56
+ }
57
+
58
+ public void testResizeQueueSameSize () throws Exception {
59
+ createExecutor (2000 , fastWrapper ());
47
60
48
61
// Execute a task multiple times that takes 1ms
49
62
assertThat (executor .resize (1000 ), equalTo (1000 ));
50
63
executeTask (executor , (measureWindow * 5 ) + 2 );
51
64
52
- assertBusy (() -> { assertThat (queue .capacity (), lessThanOrEqualTo (1000 )); });
53
- executor .shutdown ();
54
- executor .awaitTermination (10 , TimeUnit .SECONDS );
65
+ assertBusy (() -> assertThat (queue .capacity (), lessThanOrEqualTo (1000 )));
55
66
}
56
67
57
68
public void testResizeQueueUp () throws Exception {
58
- ThreadContext context = new ThreadContext (Settings .EMPTY );
59
- ResizableBlockingQueue <Runnable > queue = new ResizableBlockingQueue <>(ConcurrentCollections .<Runnable >newBlockingQueue (), 2000 );
60
-
61
- int threads = randomIntBetween (1 , 10 );
62
- int measureWindow = randomIntBetween (100 , 200 );
63
- logger .info ("--> auto-queue with a measurement window of {} tasks" , measureWindow );
64
- QueueResizableOpenSearchThreadPoolExecutor executor = new QueueResizableOpenSearchThreadPoolExecutor (
65
- "test-threadpool" ,
66
- threads ,
67
- threads ,
68
- 1000 ,
69
- TimeUnit .MILLISECONDS ,
70
- queue ,
71
- fastWrapper (),
72
- OpenSearchExecutors .daemonThreadFactory ("queuetest" ),
73
- new OpenSearchAbortPolicy (),
74
- context
75
- );
76
- executor .prestartAllCoreThreads ();
77
- logger .info ("--> executor: {}" , executor );
78
-
69
+ createExecutor (2000 , fastWrapper ());
79
70
// Execute a task multiple times that takes 1ms
80
71
assertThat (executor .resize (3000 ), equalTo (3000 ));
81
72
executeTask (executor , (measureWindow * 5 ) + 2 );
82
73
83
- assertBusy (() -> { assertThat (queue .capacity (), greaterThanOrEqualTo (2000 )); });
84
- executor .shutdown ();
85
- executor .awaitTermination (10 , TimeUnit .SECONDS );
74
+ assertBusy (() -> assertThat (queue .capacity (), greaterThanOrEqualTo (2000 )));
86
75
}
87
76
88
77
public void testResizeQueueDown () throws Exception {
89
- ThreadContext context = new ThreadContext (Settings .EMPTY );
90
- ResizableBlockingQueue <Runnable > queue = new ResizableBlockingQueue <>(ConcurrentCollections .<Runnable >newBlockingQueue (), 2000 );
91
-
92
- int threads = randomIntBetween (1 , 10 );
93
- int measureWindow = randomIntBetween (100 , 200 );
94
- logger .info ("--> auto-queue with a measurement window of {} tasks" , measureWindow );
95
- QueueResizableOpenSearchThreadPoolExecutor executor = new QueueResizableOpenSearchThreadPoolExecutor (
96
- "test-threadpool" ,
97
- threads ,
98
- threads ,
99
- 1000 ,
100
- TimeUnit .MILLISECONDS ,
101
- queue ,
102
- fastWrapper (),
103
- OpenSearchExecutors .daemonThreadFactory ("queuetest" ),
104
- new OpenSearchAbortPolicy (),
105
- context
106
- );
107
- executor .prestartAllCoreThreads ();
108
- logger .info ("--> executor: {}" , executor );
109
-
78
+ createExecutor (2000 , fastWrapper ());
110
79
// Execute a task multiple times that takes 1ms
111
- assertThat (executor .resize (900 ), equalTo (900 ));
80
+ assertThat (executor .resize (1500 ), equalTo (1500 ));
112
81
executeTask (executor , (measureWindow * 5 ) + 2 );
113
82
114
- assertBusy (() -> { assertThat (queue .capacity (), lessThanOrEqualTo (900 )); });
115
- executor .shutdown ();
116
- executor .awaitTermination (10 , TimeUnit .SECONDS );
83
+ assertBusy (() -> assertThat (queue .capacity (), lessThanOrEqualTo (1500 )));
117
84
}
118
85
119
86
public void testExecutionEWMACalculation () throws Exception {
120
- ThreadContext context = new ThreadContext (Settings .EMPTY );
121
- ResizableBlockingQueue <Runnable > queue = new ResizableBlockingQueue <>(ConcurrentCollections .<Runnable >newBlockingQueue (), 100 );
122
-
123
- QueueResizableOpenSearchThreadPoolExecutor executor = new QueueResizableOpenSearchThreadPoolExecutor (
124
- "test-threadpool" ,
125
- 1 ,
126
- 1 ,
127
- 1000 ,
128
- TimeUnit .MILLISECONDS ,
129
- queue ,
130
- fastWrapper (),
131
- OpenSearchExecutors .daemonThreadFactory ("queuetest" ),
132
- new OpenSearchAbortPolicy (),
133
- context
134
- );
135
- executor .prestartAllCoreThreads ();
136
- logger .info ("--> executor: {}" , executor );
137
-
87
+ createExecutor (100 , fastWrapper ());
138
88
assertThat ((long ) executor .getTaskExecutionEWMA (), equalTo (0L ));
139
89
executeTask (executor , 1 );
140
- assertBusy (() -> { assertThat ((long ) executor .getTaskExecutionEWMA (), equalTo (30L )); } );
90
+ assertBusy (() -> assertThat ((long ) executor .getTaskExecutionEWMA (), equalTo (30L )));
141
91
executeTask (executor , 1 );
142
- assertBusy (() -> { assertThat ((long ) executor .getTaskExecutionEWMA (), equalTo (51L )); } );
92
+ assertBusy (() -> assertThat ((long ) executor .getTaskExecutionEWMA (), equalTo (51L )));
143
93
executeTask (executor , 1 );
144
- assertBusy (() -> { assertThat ((long ) executor .getTaskExecutionEWMA (), equalTo (65L )); } );
94
+ assertBusy (() -> assertThat ((long ) executor .getTaskExecutionEWMA (), equalTo (65L )));
145
95
executeTask (executor , 1 );
146
- assertBusy (() -> { assertThat ((long ) executor .getTaskExecutionEWMA (), equalTo (75L )); } );
96
+ assertBusy (() -> assertThat ((long ) executor .getTaskExecutionEWMA (), equalTo (75L )));
147
97
executeTask (executor , 1 );
148
- assertBusy (() -> { assertThat ((long ) executor .getTaskExecutionEWMA (), equalTo (83L )); });
149
-
150
- executor .shutdown ();
151
- executor .awaitTermination (10 , TimeUnit .SECONDS );
98
+ assertBusy (() -> assertThat ((long ) executor .getTaskExecutionEWMA (), equalTo (83L )));
152
99
}
153
100
154
101
/** Use a runnable wrapper that simulates a task with unknown failures. */
155
- public void testExceptionThrowingTask () throws Exception {
156
- ThreadContext context = new ThreadContext (Settings .EMPTY );
157
- ResizableBlockingQueue <Runnable > queue = new ResizableBlockingQueue <>(ConcurrentCollections .<Runnable >newBlockingQueue (), 100 );
158
-
159
- QueueResizableOpenSearchThreadPoolExecutor executor = new QueueResizableOpenSearchThreadPoolExecutor (
160
- "test-threadpool" ,
161
- 1 ,
162
- 1 ,
163
- 1000 ,
164
- TimeUnit .MILLISECONDS ,
165
- queue ,
166
- exceptionalWrapper (),
167
- OpenSearchExecutors .daemonThreadFactory ("queuetest" ),
168
- new OpenSearchAbortPolicy (),
169
- context
170
- );
171
- executor .prestartAllCoreThreads ();
172
- logger .info ("--> executor: {}" , executor );
173
-
102
+ public void testExceptionThrowingTask () {
103
+ createExecutor (100 , exceptionalWrapper ());
174
104
assertThat ((long ) executor .getTaskExecutionEWMA (), equalTo (0L ));
175
105
executeTask (executor , 1 );
176
- executor .shutdown ();
177
- executor .awaitTermination (10 , TimeUnit .SECONDS );
178
106
}
179
107
180
108
private Function <Runnable , WrappedRunnable > fastWrapper () {
@@ -198,7 +126,7 @@ private void executeTask(QueueResizableOpenSearchThreadPoolExecutor executor, in
198
126
}
199
127
}
200
128
201
- public class SettableTimedRunnable extends TimedRunnable {
129
+ private static class SettableTimedRunnable extends TimedRunnable {
202
130
private final long timeTaken ;
203
131
private final boolean testFailedOrRejected ;
204
132
0 commit comments