|
11 | 11 | import org.apache.logging.log4j.Level;
|
12 | 12 | import org.apache.logging.log4j.LogManager;
|
13 | 13 | import org.apache.logging.log4j.Logger;
|
| 14 | +import org.elasticsearch.action.ActionRunnable; |
| 15 | +import org.elasticsearch.action.support.PlainActionFuture; |
14 | 16 | import org.elasticsearch.common.logging.Loggers;
|
15 | 17 | import org.elasticsearch.common.settings.Settings;
|
| 18 | +import org.elasticsearch.common.util.concurrent.AbstractRunnable; |
16 | 19 | import org.elasticsearch.common.util.concurrent.EsExecutors;
|
| 20 | +import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; |
17 | 21 | import org.elasticsearch.common.util.concurrent.FutureUtils;
|
18 | 22 | import org.elasticsearch.core.TimeValue;
|
19 | 23 | import org.elasticsearch.test.ESTestCase;
|
20 | 24 | import org.elasticsearch.test.MockLogAppender;
|
21 | 25 |
|
22 | 26 | import java.util.concurrent.CountDownLatch;
|
23 | 27 | import java.util.concurrent.ExecutorService;
|
| 28 | +import java.util.concurrent.TimeUnit; |
24 | 29 |
|
25 | 30 | import static org.elasticsearch.threadpool.ThreadPool.ESTIMATED_TIME_INTERVAL_SETTING;
|
26 | 31 | import static org.elasticsearch.threadpool.ThreadPool.LATE_TIME_INTERVAL_WARN_THRESHOLD_SETTING;
|
@@ -304,4 +309,146 @@ public String toString() {
|
304 | 309 | assertTrue(terminate(threadPool));
|
305 | 310 | }
|
306 | 311 | }
|
| 312 | + |
| 313 | + public void testScheduledOneShotRejection() { |
| 314 | + final String name = "fixed-bounded"; |
| 315 | + final ThreadPool threadPool = new TestThreadPool( |
| 316 | + getTestName(), |
| 317 | + new FixedExecutorBuilder(Settings.EMPTY, name, between(1, 5), between(1, 5), "prefix") |
| 318 | + ); |
| 319 | + |
| 320 | + final PlainActionFuture<Void> future = new PlainActionFuture<>(); |
| 321 | + final CountDownLatch latch = new CountDownLatch(1); |
| 322 | + try { |
| 323 | + blockExecution(threadPool.executor(name), latch); |
| 324 | + threadPool.schedule( |
| 325 | + ActionRunnable.run(future, () -> fail("should not execute")), |
| 326 | + TimeValue.timeValueMillis(between(1, 100)), |
| 327 | + name |
| 328 | + ); |
| 329 | + |
| 330 | + expectThrows(EsRejectedExecutionException.class, () -> FutureUtils.get(future, 10, TimeUnit.SECONDS)); |
| 331 | + } finally { |
| 332 | + latch.countDown(); |
| 333 | + assertTrue(terminate(threadPool)); |
| 334 | + } |
| 335 | + } |
| 336 | + |
| 337 | + public void testScheduledOneShotForceExecution() { |
| 338 | + final String name = "fixed-bounded"; |
| 339 | + final ThreadPool threadPool = new TestThreadPool( |
| 340 | + getTestName(), |
| 341 | + new FixedExecutorBuilder(Settings.EMPTY, name, between(1, 5), between(1, 5), "prefix") |
| 342 | + ); |
| 343 | + |
| 344 | + final PlainActionFuture<Void> future = new PlainActionFuture<>(); |
| 345 | + final CountDownLatch latch = new CountDownLatch(1); |
| 346 | + try { |
| 347 | + blockExecution(threadPool.executor(name), latch); |
| 348 | + threadPool.schedule(forceExecution(ActionRunnable.run(future, () -> {})), TimeValue.timeValueMillis(between(1, 100)), name); |
| 349 | + |
| 350 | + Thread.yield(); |
| 351 | + assertFalse(future.isDone()); |
| 352 | + |
| 353 | + latch.countDown(); |
| 354 | + FutureUtils.get(future, 10, TimeUnit.SECONDS); // shouldn't throw |
| 355 | + } finally { |
| 356 | + latch.countDown(); |
| 357 | + assertTrue(terminate(threadPool)); |
| 358 | + } |
| 359 | + } |
| 360 | + |
| 361 | + public void testScheduledFixedDelayRejection() { |
| 362 | + final String name = "fixed-bounded"; |
| 363 | + final ThreadPool threadPool = new TestThreadPool( |
| 364 | + getTestName(), |
| 365 | + new FixedExecutorBuilder(Settings.EMPTY, name, between(1, 5), between(1, 5), "prefix") |
| 366 | + ); |
| 367 | + |
| 368 | + final PlainActionFuture<Void> future = new PlainActionFuture<>(); |
| 369 | + final CountDownLatch latch = new CountDownLatch(1); |
| 370 | + try { |
| 371 | + threadPool.scheduleWithFixedDelay( |
| 372 | + ActionRunnable.wrap(future, ignored -> Thread.yield()), |
| 373 | + TimeValue.timeValueMillis(between(1, 100)), |
| 374 | + name |
| 375 | + ); |
| 376 | + |
| 377 | + while (future.isDone() == false) { |
| 378 | + // might not block all threads the first time round if the scheduled runnable is running, so must keep trying |
| 379 | + blockExecution(threadPool.executor(name), latch); |
| 380 | + } |
| 381 | + expectThrows(EsRejectedExecutionException.class, () -> FutureUtils.get(future)); |
| 382 | + } finally { |
| 383 | + latch.countDown(); |
| 384 | + assertTrue(terminate(threadPool)); |
| 385 | + } |
| 386 | + } |
| 387 | + |
| 388 | + public void testScheduledFixedDelayForceExecution() { |
| 389 | + final String name = "fixed-bounded"; |
| 390 | + final ThreadPool threadPool = new TestThreadPool( |
| 391 | + getTestName(), |
| 392 | + new FixedExecutorBuilder(Settings.EMPTY, name, between(1, 5), between(1, 5), "prefix") |
| 393 | + ); |
| 394 | + |
| 395 | + final PlainActionFuture<Void> future = new PlainActionFuture<>(); |
| 396 | + final CountDownLatch latch = new CountDownLatch(1); |
| 397 | + try { |
| 398 | + blockExecution(threadPool.executor(name), latch); |
| 399 | + |
| 400 | + threadPool.scheduleWithFixedDelay( |
| 401 | + forceExecution(ActionRunnable.run(future, Thread::yield)), |
| 402 | + TimeValue.timeValueMillis(between(1, 100)), |
| 403 | + name |
| 404 | + ); |
| 405 | + |
| 406 | + assertFalse(future.isDone()); |
| 407 | + |
| 408 | + latch.countDown(); |
| 409 | + FutureUtils.get(future, 10, TimeUnit.SECONDS); // shouldn't throw |
| 410 | + } finally { |
| 411 | + latch.countDown(); |
| 412 | + assertTrue(terminate(threadPool)); |
| 413 | + } |
| 414 | + } |
| 415 | + |
| 416 | + private static AbstractRunnable forceExecution(AbstractRunnable delegate) { |
| 417 | + return new AbstractRunnable() { |
| 418 | + @Override |
| 419 | + public void onFailure(Exception e) { |
| 420 | + delegate.onFailure(e); |
| 421 | + } |
| 422 | + |
| 423 | + @Override |
| 424 | + protected void doRun() { |
| 425 | + delegate.run(); |
| 426 | + } |
| 427 | + |
| 428 | + @Override |
| 429 | + public void onRejection(Exception e) { |
| 430 | + delegate.onRejection(e); |
| 431 | + } |
| 432 | + |
| 433 | + @Override |
| 434 | + public void onAfter() { |
| 435 | + delegate.onAfter(); |
| 436 | + } |
| 437 | + |
| 438 | + @Override |
| 439 | + public boolean isForceExecution() { |
| 440 | + return true; |
| 441 | + } |
| 442 | + }; |
| 443 | + } |
| 444 | + |
| 445 | + private static void blockExecution(ExecutorService executor, CountDownLatch latch) { |
| 446 | + while (true) { |
| 447 | + try { |
| 448 | + executor.execute(() -> safeAwait(latch)); |
| 449 | + } catch (EsRejectedExecutionException e) { |
| 450 | + break; |
| 451 | + } |
| 452 | + } |
| 453 | + } |
307 | 454 | }
|
0 commit comments