-
Notifications
You must be signed in to change notification settings - Fork 40
Add close for storage, transaction in data loader core #2699
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
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4a2d1a8
Initial commit for resource closing
inv-jishnu 072b27b
Merge branch 'master' into jishnu/data-loader/storage-trn-close
inv-jishnu d885b56
Merge branch 'master' into jishnu/data-loader/storage-trn-close
ypeckstadt 4e266bc
Unit tests added
inv-jishnu 5a30ceb
Spotless applied
inv-jishnu 5fbd7ae
Merge branch 'master' into jishnu/data-loader/storage-trn-close
inv-jishnu 627c03c
Merge branch 'master' into jishnu/data-loader/storage-trn-close
ypeckstadt 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -152,8 +152,45 @@ public void onTaskComplete(ImportTaskResult taskResult) { | |
| /** {@inheritDoc} Forwards the event to all registered listeners. */ | ||
| @Override | ||
| public void onAllDataChunksCompleted() { | ||
| Throwable firstException = null; | ||
|
|
||
| for (ImportEventListener listener : listeners) { | ||
| listener.onAllDataChunksCompleted(); | ||
| try { | ||
| listener.onAllDataChunksCompleted(); | ||
| } catch (Throwable e) { | ||
| if (firstException == null) { | ||
| firstException = e; | ||
| } else { | ||
| firstException.addSuppressed(e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| closeResources(); | ||
| } catch (Throwable e) { | ||
| if (firstException != null) { | ||
| firstException.addSuppressed(e); | ||
| } else { | ||
| firstException = e; | ||
| } | ||
| } | ||
|
|
||
| if (firstException != null) { | ||
| throw new RuntimeException("Error during completion", firstException); | ||
| } | ||
| } | ||
|
|
||
| /** Close resources properly once the process is completed */ | ||
| public void closeResources() { | ||
| try { | ||
|
||
| if (distributedStorage != null) { | ||
| distributedStorage.close(); | ||
| } else if (distributedTransactionManager != null) { | ||
| distributedTransactionManager.close(); | ||
| } | ||
| } catch (Throwable e) { | ||
| throw new RuntimeException("Failed to close the resource", e); | ||
| } | ||
| } | ||
|
|
||
|
|
||
101 changes: 101 additions & 0 deletions
101
...loader/core/src/test/java/com/scalar/db/dataloader/core/dataimport/ImportManagerTest.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,101 @@ | ||
| package com.scalar.db.dataloader.core.dataimport; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.mockito.Mockito.doThrow; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.verify; | ||
|
|
||
| import com.scalar.db.api.DistributedStorage; | ||
| import com.scalar.db.api.DistributedTransactionManager; | ||
| import com.scalar.db.api.TableMetadata; | ||
| import com.scalar.db.dataloader.core.ScalarDbMode; | ||
| import com.scalar.db.dataloader.core.dataimport.processor.ImportProcessorFactory; | ||
| import java.io.BufferedReader; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class ImportManagerTest { | ||
|
|
||
| private ImportManager importManager; | ||
| private ImportEventListener listener1; | ||
| private ImportEventListener listener2; | ||
| private DistributedStorage distributedStorage; | ||
| private DistributedTransactionManager distributedTransactionManager; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| Map<String, TableMetadata> tableMetadata = new HashMap<>(); | ||
| BufferedReader reader = mock(BufferedReader.class); | ||
| ImportOptions options = mock(ImportOptions.class); | ||
| ImportProcessorFactory processorFactory = mock(ImportProcessorFactory.class); | ||
|
|
||
| listener1 = mock(ImportEventListener.class); | ||
| listener2 = mock(ImportEventListener.class); | ||
| distributedStorage = mock(DistributedStorage.class); | ||
| distributedTransactionManager = mock(DistributedTransactionManager.class); | ||
|
|
||
| importManager = | ||
| new ImportManager( | ||
| tableMetadata, | ||
| reader, | ||
| options, | ||
| processorFactory, | ||
| ScalarDbMode.STORAGE, | ||
| distributedStorage, | ||
| null); // Only one resource present | ||
| importManager.addListener(listener1); | ||
| importManager.addListener(listener2); | ||
| } | ||
|
|
||
| @Test | ||
| void onAllDataChunksCompleted_shouldNotifyListenersAndCloseStorage() { | ||
| importManager.onAllDataChunksCompleted(); | ||
|
|
||
| verify(listener1).onAllDataChunksCompleted(); | ||
| verify(listener2).onAllDataChunksCompleted(); | ||
| verify(distributedStorage).close(); | ||
| } | ||
|
|
||
| @Test | ||
| void onAllDataChunksCompleted_shouldAggregateListenerExceptionAndStillCloseResources() { | ||
| doThrow(new RuntimeException("Listener1 failed")).when(listener1).onAllDataChunksCompleted(); | ||
|
|
||
| RuntimeException thrown = | ||
| assertThrows(RuntimeException.class, () -> importManager.onAllDataChunksCompleted()); | ||
|
|
||
| assertTrue(thrown.getMessage().contains("Error during completion")); | ||
| assertEquals("Listener1 failed", thrown.getCause().getMessage()); | ||
| verify(distributedStorage).close(); | ||
| } | ||
|
|
||
| @Test | ||
| void closeResources_shouldCloseTransactionManagerIfStorageIsNull() { | ||
| ImportManager managerWithTx = | ||
| new ImportManager( | ||
| new HashMap<>(), | ||
| mock(BufferedReader.class), | ||
| mock(ImportOptions.class), | ||
| mock(ImportProcessorFactory.class), | ||
| ScalarDbMode.TRANSACTION, | ||
| null, | ||
| distributedTransactionManager); | ||
|
|
||
| managerWithTx.closeResources(); | ||
| verify(distributedTransactionManager).close(); | ||
| } | ||
|
|
||
| @Test | ||
| void closeResources_shouldThrowIfResourceCloseFails() { | ||
| doThrow(new RuntimeException("Close failed")).when(distributedStorage).close(); | ||
|
|
||
| RuntimeException ex = | ||
| assertThrows(RuntimeException.class, () -> importManager.closeResources()); | ||
|
|
||
| assertEquals("Failed to close the resource", ex.getMessage()); | ||
| assertEquals("Close failed", ex.getCause().getMessage()); | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Catching Throwable may inadvertently capture critical errors (e.g., OutOfMemoryError). Consider catching Exception to handle only recoverable errors.