-
Notifications
You must be signed in to change notification settings - Fork 460
[FLUSS-1855][server] Add Null Safety for TimerTaskEntry Removal #1872
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+134
−3
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b53303a
[server] Added test to reproduce NPE during TimerTaskEntry removal
rionmonster bdd1820
[server] Added Safety Check for TimerTaskEntry Removal to Avoid NPE
rionmonster 0edf1b3
[server] Adjusted Null Removal Test Case Verbiage
rionmonster 66745c8
[FLUSS-1855][server] Updated Clarity for TimerTaskEntry Null-Safety Test
rionmonster 719e4ec
[FLUSS-1855][server] Improved Assertions for TimerTaskEntryList Null-…
rionmonster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
fluss-server/src/test/java/org/apache/fluss/server/utils/timer/TimerTaskEntryTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.fluss.server.utils.timer; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| /** Tests for {@link org.apache.fluss.server.utils.timer.TimerTaskEntry}. */ | ||
| public class TimerTaskEntryTest { | ||
|
|
||
| @Test | ||
| void testRemoveEnsuresCurrentListNullSafety() throws InterruptedException { | ||
| // Create two lists to reproduce the values that we are working | ||
| // with being added/removed. We will oscillate between adding | ||
| // and removing these elements until we encounter a NPE | ||
| AtomicInteger sharedTaskCounter = new AtomicInteger(0); | ||
| TimerTaskList primaryList = new TimerTaskList(sharedTaskCounter); | ||
| TimerTaskList secondaryList = new TimerTaskList(sharedTaskCounter); | ||
|
|
||
| // Set up our initial task that will handle coordinating this | ||
| // reproduction behavior | ||
| TestTask task = new TestTask(0L); | ||
| TimerTaskEntry entry = new TimerTaskEntry(task, 10L); | ||
| primaryList.add(entry); | ||
|
|
||
| // Container for any NullPointerException caught during remove() | ||
| AtomicReference<NullPointerException> thrownException = new AtomicReference<>(); | ||
|
|
||
| // Latch to handle coordinating addition/removal threads | ||
| CountDownLatch latch = new CountDownLatch(1); | ||
|
|
||
| // Create a thread responsible for continually removing entries, which | ||
| // will be responsible for triggering the exception | ||
| Thread removalThread = | ||
| new Thread( | ||
| () -> { | ||
| try { | ||
| latch.await(); | ||
| // Continually remove elements from the task (forward-oscillation) | ||
| for (int i = 0; i < 10000; i++) { | ||
| entry.remove(); | ||
| } | ||
| } catch (NullPointerException e) { | ||
| thrownException.set(e); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| } | ||
| }); | ||
|
|
||
| // Create a separate thread for adding the entry while the removal thread is | ||
| // still executing which results in the expected null reference | ||
| Thread additionThread = | ||
| new Thread( | ||
| () -> { | ||
| try { | ||
| // Wait for the initial removal to complete | ||
| latch.await(); | ||
| // Add the entry to our separate list while the removal thread is | ||
| // still verifying the condition (resulting in our null list within | ||
| // the internal removal call, and our exception) | ||
| for (int i = 0; i < 10000; i++) { | ||
| // Determine which list to add to the task | ||
| // (backwards-oscillation) | ||
| TimerTaskList currentList = entry.list; | ||
| if (currentList == null || currentList == primaryList) { | ||
| // If the entry is not in any list or in the primary list, | ||
| // move it to the secondary list | ||
| secondaryList.add(entry); | ||
| } else if (currentList == secondaryList) { | ||
| // If the entry is in the secondary list, move it to the | ||
| // primary list | ||
| primaryList.add(entry); | ||
| } | ||
| Thread.yield(); | ||
| } | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| } | ||
| }); | ||
|
|
||
| // Start both threads | ||
| removalThread.start(); | ||
| additionThread.start(); | ||
|
|
||
| // Release both threads to trigger our race condition | ||
| latch.countDown(); | ||
|
|
||
| // Wait for threads to complete | ||
| removalThread.join(); | ||
| additionThread.join(); | ||
|
|
||
| // Attempt to remove the last entry (to ensure empty list) | ||
| entry.remove(); | ||
|
|
||
| // Verify the list is empty after entry removal and ensure | ||
| // counter reflects the correct state | ||
| assertThat(entry.list).isNull(); | ||
| assertThat(sharedTaskCounter.get()).isEqualTo(0); | ||
|
|
||
| // Assert that no exception was originated | ||
| assertThat(thrownException.get()).isNull(); | ||
| } | ||
|
|
||
| private static class TestTask extends TimerTask { | ||
| public TestTask(long delayMs) { | ||
| super(delayMs); | ||
| } | ||
|
|
||
| @Override | ||
| public void run() {} | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.