amrishlal commented on a change in pull request #7174: URL: https://github.com/apache/pinot/pull/7174#discussion_r687249727
########## File path: pinot-core/src/test/java/org/apache/pinot/core/periodictask/PeriodicTaskSchedulerTest.java ########## @@ -104,4 +105,79 @@ protected void cleanUpTask() { assertEquals(numTimesRunCalled.get(), numTasks * 2); assertEquals(numTimesStopCalled.get(), numTasks); } + + + /** Test that {@link PeriodicTaskScheduler} does not run the same task more than once at any time. */ + @Test + public void testConcurrentExecutionOfSameTask() throws Exception { + // Count how many tasks were run. + final AtomicInteger counter = new AtomicInteger(); + + // Count how many attempts were made to run task + final AtomicInteger attempts = new AtomicInteger(); + + + // Create periodic task. + PeriodicTask task = new BasePeriodicTask("TestTask", 1L, 0L) { + private volatile boolean isRunning = false; + @Override + protected void runTask() { + try { + if (isRunning) { + Assert.fail("More than one thread attempting to execute task at the same time."); + } + isRunning = true; + counter.incrementAndGet(); + Thread.sleep(250); + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + } finally { + isRunning = false; + } + } + }; + + // Start scheduler with periodic task. + List<PeriodicTask> periodicTasks = new ArrayList<>(); + periodicTasks.add(task); + + PeriodicTaskScheduler taskScheduler = new PeriodicTaskScheduler(); + taskScheduler.init(periodicTasks); + taskScheduler.start(); + + // Create multiple "execute" threads that try to run the same task that is already being run by scheduler + // on a periodic basis. + final int threadCount = 20; + Thread[] threads = new Thread[threadCount]; + for (int i = 0; i < threads.length; i++) { + threads[i] = new Thread(() -> { + attempts.incrementAndGet(); + taskScheduler.scheduleNow("TestTask", null); + }); + + threads[i].start(); + try { + threads[i].join(); + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + } + } + + // Run for 3 seconds to let as many "execute" threads finish as possible. + Thread.sleep(3000); Review comment: I avoided using this earlier since I didn't want to set a specific count for count down latch and just see how many threads complete vs how many ran, but countdown latch also works here. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org