Skip to content
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

Refactoring: Simplify OpenCryptoFile #238

Merged
merged 3 commits into from
Dec 7, 2024

Conversation

infeo
Copy link
Member

@infeo infeo commented Aug 9, 2024

As already observed in other PRs, the class OpenCryptoFile can be simplified. This PR applies these simplifications.

  • replaced ChannelCloseListener interface by Consumer<FileChannel>
  • changed type of openChannels counter to AtomicInteger

* move (un)registration of cipherChannel to CleartextFileChannel
* removed ChannelCloseListener interface
* changed type openChannels counter to volatile int
@infeo infeo requested a review from overheadhunter August 9, 2024 15:52
@infeo infeo self-assigned this Aug 9, 2024
Copy link

coderabbitai bot commented Aug 9, 2024

Walkthrough

The recent changes involve modifications to the ChannelComponent interface, the CleartextFileChannel class, and the OpenCryptoFile class, primarily focusing on the handling of channel closure events. The type of the closeListener parameter has been updated from ChannelCloseListener to Consumer<FileChannel>, allowing for a more flexible listener implementation. This change is reflected in the constructors and method signatures across the affected classes. Additionally, the OpenCryptoFile class has simplified its management of open channels by replacing a ConcurrentMap with an AtomicInteger to track the count of open channels. The associated test classes have also been updated to accommodate these changes, ensuring that the new listener type is correctly utilized in test scenarios. Overall, the updates enhance the clarity and efficiency of channel management while maintaining the overall structure of the existing code.

Possibly related PRs

  • Feature: First close file channel, then persist lastModified #228: The changes in this PR involve modifications to the CleartextFileChannel class, specifically related to the handling of the closeListener, which is directly relevant to the changes made in the main PR regarding the transition from ChannelCloseListener to Consumer<FileChannel>.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 674bdab and e432b1e.

Files selected for processing (6)
  • src/main/java/org/cryptomator/cryptofs/ch/ChannelComponent.java (1 hunks)
  • src/main/java/org/cryptomator/cryptofs/ch/CleartextFileChannel.java (4 hunks)
  • src/main/java/org/cryptomator/cryptofs/fh/ChunkIO.java (1 hunks)
  • src/main/java/org/cryptomator/cryptofs/fh/OpenCryptoFile.java (6 hunks)
  • src/test/java/org/cryptomator/cryptofs/ch/CleartextFileChannelTest.java (9 hunks)
  • src/test/java/org/cryptomator/cryptofs/fh/OpenCryptoFileTest.java (9 hunks)
Additional comments not posted (23)
src/main/java/org/cryptomator/cryptofs/ch/ChannelComponent.java (1)

20-20: Verify the usage of the create method.

The parameter type change from ChannelCloseListener to Runnable simplifies the interface but requires verification to ensure all usages are compatible with the new type.

src/main/java/org/cryptomator/cryptofs/fh/ChunkIO.java (1)

14-14: Verify the impact of changing class visibility to public.

Changing the ChunkIO class from package-private to public increases its accessibility. Ensure that this change does not unintentionally expose sensitive functionality or lead to misuse.

Verification successful

Visibility Change of ChunkIO Class Verified

The change of the ChunkIO class to public visibility does not expose any sensitive functionality. The methods within the class are related to channel management and are used appropriately within the codebase. No unintended exposure has been detected.

  • Methods: public ChunkIO(), public void registerChannel(FileChannel channel, boolean writable), public void unregisterChannel(FileChannel channel)
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the impact of changing `ChunkIO` class visibility to public.

# Test: Search for usages of `ChunkIO` across the codebase. Expect: Appropriate usage with no unintended exposure.
rg --type java -A 5 $'ChunkIO'

Length of output: 8900


Script:

#!/bin/bash
# Check for any usage of ChunkIO outside its package
rg --type java --glob '!src/main/java/org/cryptomator/cryptofs/fh/*' 'ChunkIO'

# List all methods in the ChunkIO class to identify potentially sensitive functionality
ast-grep --lang java --pattern 'class ChunkIO { $$$ }'

Length of output: 1374


Script:

#!/bin/bash
# List all methods in the ChunkIO class to identify potentially sensitive functionality
ast-grep --lang java --pattern 'class ChunkIO { $$$ }'

Length of output: 56


Script:

#!/bin/bash
# List all method declarations in the ChunkIO class
ast-grep --lang java --pattern 'class ChunkIO { $_() { $$$ } }'

Length of output: 65


Script:

#!/bin/bash
# Use ripgrep to search for method declarations in the ChunkIO class
rg --type java --no-filename --no-line-number --only-matching -e 'public .*?\(.*?\)' src/main/java/org/cryptomator/cryptofs/fh/ChunkIO.java

Length of output: 276

src/main/java/org/cryptomator/cryptofs/fh/OpenCryptoFile.java (3)

38-38: Verify the removal of ChunkIO from the constructor.

The ChunkIO parameter has been removed from the constructor. Ensure that this does not affect any dependent functionality or lead to issues elsewhere in the codebase.

Verification successful

Removal of ChunkIO from OpenCryptoFile Constructor Verified

The removal of the ChunkIO parameter from the OpenCryptoFile constructor does not affect any dependent functionality. There are no instantiations of OpenCryptoFile that still use ChunkIO. The codebase and tests are aligned with this change.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the impact of removing `ChunkIO` from `OpenCryptoFile` constructor.

# Test: Search for `OpenCryptoFile` instantiations. Expect: No dependency on `ChunkIO`.
rg --type java -A 5 $'OpenCryptoFile('

Length of output: 120


Script:

#!/bin/bash
# Description: Verify the impact of removing `ChunkIO` from `OpenCryptoFile` constructor.

# Test: Search for `OpenCryptoFile` instantiations. Expect: No dependency on `ChunkIO`.
rg --type java -A 5 'OpenCryptoFile\('

Length of output: 6990


35-35: Ensure thread safety of openChannelsCount.

The openChannelsCount is now a volatile int, which may not be sufficient for complex operations. Verify that all operations on this variable are thread-safe.

Verification successful

Thread safety of openChannelsCount is ensured. The operations on openChannelsCount are performed within synchronized methods, ensuring thread safety. The use of volatile complements this by ensuring visibility across threads. No further action is needed.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify thread safety of `openChannelsCount` operations.

# Test: Search for operations on `openChannelsCount`. Expect: Proper synchronization or atomic operations.
rg --type java -A 5 $'openChannelsCount'

Length of output: 2157


Script:

#!/bin/bash
# Description: Search for synchronization mechanisms in OpenCryptoFile.java to verify thread safety of `openChannelsCount`.

# Test: Look for synchronized blocks or methods in the OpenCryptoFile.java file.
rg --type java 'synchronized|Lock|ReentrantLock' src/main/java/org/cryptomator/cryptofs/fh/OpenCryptoFile.java

Length of output: 400


172-174: Ensure correctness of channelClosed logic.

The channelClosed method now decrements the openChannelsCount and closes the file if the count reaches zero. Verify that this logic is correct and does not lead to premature file closure.

Verification successful

The channelClosed logic is correct and safe from premature closure.

The openChannelsCount variable is correctly managed within synchronized contexts, ensuring atomic operations for incrementing and decrementing. This prevents race conditions and ensures that the file is only closed when all channels are closed.

  • The variable is declared as volatile and manipulated within synchronized methods, confirming thread-safe operations.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify correctness of `channelClosed` logic.

# Test: Search for `channelClosed` method usage. Expect: Correct logic without premature file closure.
rg --type java -A 5 $'channelClosed'

Length of output: 1659


Script:

#!/bin/bash
# Search for all occurrences of `openChannelsCount` to verify its usage and modification.
rg --type java 'openChannelsCount'

Length of output: 522

src/test/java/org/cryptomator/cryptofs/fh/OpenCryptoFileTest.java (5)

72-72: Constructor simplification looks good.

The removal of the ChunkIO parameter from the OpenCryptoFile constructor simplifies the instantiation in tests. Ensure that the functionality covered by ChunkIO is adequately tested elsewhere.


83-83: Constructor simplification is consistent.

The constructor change is consistently applied across tests, reducing unnecessary dependencies. Verify that no functionality is lost due to the removal of the ChunkIO parameter.


102-102: Constructor simplification continues to improve clarity.

The consistent removal of the ChunkIO parameter across tests maintains clarity and reduces complexity in test setup.


197-197: Constructor changes in nested test class are consistent.

The removal of the ChunkIO parameter is consistently applied in the FileChannelFactoryTest class, aligning with the refactoring goals.


114-114: Ensure test coverage for removed functionality.

The removal of triggerCloseListener suggests a change in how channel closures are managed. Ensure that the new approach is adequately tested elsewhere.

Verification successful

Test coverage for closeListener is adequate.

The existing tests in OpenCryptoFileTest.java verify the behavior of closeListener, ensuring that it is called or not called as expected in various scenarios. This indicates that the functionality related to channel closures is adequately tested.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify if the functionality covered by the removed `triggerCloseListener` test is tested elsewhere.

# Test: Search for tests involving channel closure and ensure they cover the necessary functionality.
rg --type java -A 5 'closeListener' src/test/java/org/cryptomator/cryptofs/fh/

Length of output: 6105

src/main/java/org/cryptomator/cryptofs/ch/CleartextFileChannel.java (4)

10-10: New dependency ChunkIO is introduced.

The addition of ChunkIO as a dependency in CleartextFileChannel enables enhanced chunk management capabilities.


50-58: Integration of ChunkIO and Runnable looks good.

The ChunkIO is integrated as a private member, and the closeListener is now a Runnable, simplifying the closure mechanism. Ensure that this change aligns with the intended refactoring goals.


334-335: Ensure proper resource management with ChunkIO.

The implCloseChannel method now unregisters the channel with ChunkIO after flushing. Ensure this order is correct for resource management.


Line range hint 62-83: Constructor changes enhance functionality.

The constructor now includes ChunkIO for channel registration and Runnable for closure, improving clarity and functionality. Verify the impact on existing functionality.

Verification successful

Constructor changes are verified through existing tests.

The integration of ChunkIO and Runnable in CleartextFileChannel is supported by tests in CleartextFileChannelTest, ensuring that the functionality is covered and verified. No issues were found with the changes.

  • CleartextFileChannelTest verifies the constructor changes.
  • OpenCryptoFileTest and CryptoFileSystemProviderIntegrationTest also involve CleartextFileChannel.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the impact of the new `ChunkIO` integration and `Runnable` closeListener.

# Test: Check for any existing functionality that might be affected by these changes.
rg --type java -A 5 'CleartextFileChannel' src/main/java/org/cryptomator/cryptofs/ch/

Length of output: 2563


Script:

#!/bin/bash
# Description: Investigate the usage of `ChunkIO` and `Runnable` within `CleartextFileChannel` and check for tests.

# Check how `ChunkIO` is used in `CleartextFileChannel`
rg --type java 'chunkIO' src/main/java/org/cryptomator/cryptofs/ch/CleartextFileChannel.java

# Check how `Runnable` is used in `CleartextFileChannel`
rg --type java 'closeListener' src/main/java/org/cryptomator/cryptofs/ch/CleartextFileChannel.java

# Check for any tests related to `CleartextFileChannel`
rg --type java 'CleartextFileChannel' src/test/java/

Length of output: 3509

src/test/java/org/cryptomator/cryptofs/ch/CleartextFileChannelTest.java (9)

8-8: ChunkIO integration in tests is appropriate.

The addition of ChunkIO as a mock in tests aligns with its integration in CleartextFileChannel, ensuring consistency.


Line range hint 63-81: Test setup changes are consistent with refactoring.

The refactoring of closeListener to Runnable and the inclusion of ChunkIO in the test setup are consistent with the changes in CleartextFileChannel.


92-93: Mock setup for ChunkIO is correctly implemented.

The mock setup for ChunkIO includes registration and unregistration, ensuring that the channel lifecycle is correctly tested.


108-108: Constructor changes in test setup are consistent.

The constructor for CleartextFileChannel in tests now includes ChunkIO and Runnable, maintaining consistency with the main class changes.


249-251: Test for implCloseChannel verifies correct operation order.

The test ensures that closeListener.run() and chunkIO.unregisterChannel() are called in the correct order, enhancing test robustness.


255-264: New test ensures flush before unregister.

The test testCloseCipherChannelFlushBeforeUnregister ensures the correct order of operations, verifying that flush occurs before unregistration.


298-299: Test verifies exception handling during close.

The test testCloseExceptionOnLastModifiedPersistenceIgnored ensures that exceptions during last modified persistence are handled correctly, maintaining robustness.


405-405: Constructor changes in test setup are consistent.

The inclusion of ChunkIO and Runnable in the test setup for CleartextFileChannel is consistent with the main class changes.


577-577: Constructor changes in test setup are consistent.

The constructor for CleartextFileChannel in tests now includes ChunkIO and Runnable, maintaining consistency with the main class changes.

@infeo infeo added this to the nex milestone Dec 6, 2024
@infeo infeo removed the request for review from overheadhunter December 6, 2024 23:42
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
src/main/java/org/cryptomator/cryptofs/fh/OpenCryptoFile.java (1)

177-184: Consider adding logging for channel closure.

The channel closure logic is correct, but consider adding debug-level logging to help with troubleshooting channel lifecycle issues.

 private synchronized void cleartextChannelClosed(FileChannel ciphertextFileChannel) {
+    LOG.debug("Closing channel. Remaining channels: {}", openChannelsCount - 1);
     if( ciphertextFileChannel != null ){
         chunkIO.unregisterChannel(ciphertextFileChannel);
     }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between e432b1e and ff50fba.

📒 Files selected for processing (5)
  • src/main/java/org/cryptomator/cryptofs/ch/ChannelComponent.java (2 hunks)
  • src/main/java/org/cryptomator/cryptofs/ch/CleartextFileChannel.java (3 hunks)
  • src/main/java/org/cryptomator/cryptofs/fh/OpenCryptoFile.java (4 hunks)
  • src/test/java/org/cryptomator/cryptofs/ch/CleartextFileChannelTest.java (4 hunks)
  • src/test/java/org/cryptomator/cryptofs/fh/OpenCryptoFileTest.java (3 hunks)
🔇 Additional comments (11)
src/main/java/org/cryptomator/cryptofs/ch/ChannelComponent.java (1)

8-8: LGTM! Good use of standard Java functional interfaces.

The change from a custom ChannelCloseListener interface to Consumer<FileChannel> is a good improvement that:

  • Simplifies the codebase by leveraging standard Java functional interfaces
  • Makes the code more flexible and easier to test
  • Reduces the number of custom interfaces to maintain

Also applies to: 21-21

src/main/java/org/cryptomator/cryptofs/fh/OpenCryptoFile.java (3)

35-36: Good simplification of channel tracking.

The change from a ConcurrentMap to a volatile int for tracking open channels is a good simplification that:

  • Reduces memory overhead
  • Simplifies the code
  • Still maintains thread safety through volatile and synchronized methods

39-41: LGTM! Improved readability.

The constructor parameters are now properly formatted with line breaks and indentation, making it more readable.


Line range hint 67-84: Verify thread safety of channel count management.

While the channel count increment is done in a synchronized context, we should ensure that all paths properly maintain the count, especially in error scenarios.

✅ Verification successful

Let me gather more context about the synchronization mechanism to verify thread safety.


Thread safety of channel count management is properly implemented

The channel count management is thread-safe because:

  • The openChannelsCount field is marked as volatile
  • All modifications to openChannelsCount occur within synchronized methods:
    • Increment in newFileChannel (synchronized method)
    • Decrement in cleartextChannelClosed (synchronized method)
  • The error handling in the finally block correctly calls cleartextChannelClosed to decrement the count if channel creation fails
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for all places where openChannelsCount is modified
rg -A 2 "openChannelsCount\s*=[^=]" 

Length of output: 1009


Script:

#!/bin/bash
# Search for synchronized blocks and methods in OpenCryptoFile
ast-grep --pattern 'synchronized $_($$$) { $$$ }'
rg -B 2 -A 2 "synchronized" src/main/java/org/cryptomator/cryptofs/fh/OpenCryptoFile.java

Length of output: 867

src/test/java/org/cryptomator/cryptofs/fh/OpenCryptoFileTest.java (1)

194-194: LGTM! Tests properly verify the new channel closure mechanism.

The tests have been properly updated to use Consumer<FileChannel> and verify the channel lifecycle, including:

  • Channel registration/unregistration
  • Error handling
  • Thread safety through atomic references

Also applies to: 283-284

src/main/java/org/cryptomator/cryptofs/ch/CleartextFileChannel.java (3)

37-37: LGTM! Good use of standard Java functional interfaces.

The change from custom ChannelCloseListener to Consumer<FileChannel> improves code maintainability by using standard Java interfaces.

Also applies to: 57-57


61-61: LGTM! Constructor signature updated consistently.

The constructor parameter type change aligns with the field type change while maintaining the same parameter order.


331-331: LGTM! Proper implementation of close listener invocation.

The change correctly implements the Consumer interface's accept method while maintaining the proper order of close operations.

✅ Verification successful

LGTM! Close listener implementation is correctly using Consumer interface

The verification confirms:

  • The closeListener is properly defined as Consumer<FileChannel> in the component and class
  • The close operation maintains correct order: listener.accept -> channel.close -> persist modifications
  • Tests verify the proper invocation using accept() method
  • No remnants of old ChannelCloseListener interface exist
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify no remaining references to ChannelCloseListener
# Expect: No matches as interface should be completely removed

rg "ChannelCloseListener"

Length of output: 25


Script:

#!/bin/bash
# Let's verify the close listener implementation and its usage pattern
# First, find the file we're analyzing
fd "CleartextFileChannel.java"

# Then examine the close-related code
rg -A 5 "closeListener" 

Length of output: 14765

src/test/java/org/cryptomator/cryptofs/ch/CleartextFileChannelTest.java (3)

46-46: LGTM! Test setup properly updated.

The test class correctly adapts to use Consumer instead of ChannelCloseListener.

Also applies to: 80-80


246-246: LGTM! Proper verification of close listener calls.

The test correctly verifies that the close listener is called with the ciphertext channel.

Also applies to: 294-294


251-260: LGTM! Good addition of order verification test.

The new test ensures that flush operations occur before unregistering the channel, which is critical for data integrity.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
src/main/java/org/cryptomator/cryptofs/fh/OpenCryptoFile.java (1)

40-42: Consider using standard line continuation style

While the formatting improves readability, using // for line continuation is unconventional. Consider using standard parameter alignment:

 public OpenCryptoFile(FileCloseListener listener, ChunkCache chunkCache, Cryptor cryptor, FileHeaderHolder headerHolder, ChunkIO chunkIO,
-                      @CurrentOpenFilePath AtomicReference<Path> currentFilePath, @OpenFileSize AtomicLong fileSize, //
-                      @OpenFileModifiedDate AtomicReference<Instant> lastModified, OpenCryptoFileComponent component) {
+                      @CurrentOpenFilePath AtomicReference<Path> currentFilePath, @OpenFileSize AtomicLong fileSize,
+                      @OpenFileModifiedDate AtomicReference<Instant> lastModified, OpenCryptoFileComponent component) {
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between ff50fba and eefc448.

📒 Files selected for processing (1)
  • src/main/java/org/cryptomator/cryptofs/fh/OpenCryptoFile.java (5 hunks)
🔇 Additional comments (4)
src/main/java/org/cryptomator/cryptofs/fh/OpenCryptoFile.java (4)

80-86: LGTM: Improved error handling with proper cleanup

The error handling ensures proper resource cleanup by calling cleartextChannelClosed and closing the channel if initialization fails.


18-18: LGTM: Simplified channel tracking with AtomicInteger

The change from ConcurrentMap to AtomicInteger is a good simplification that maintains thread safety while reducing complexity.

Let's verify there are no remaining references to the old openChannels map:

Also applies to: 36-37

✅ Verification successful

The change to AtomicInteger is correctly implemented

All references to openChannels have been updated to use the new openChannelsCount AtomicInteger field. The three occurrences found are the field declaration and its usage for incrementing and decrementing the counter, which is consistent with the refactoring.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any remaining references to openChannels
rg "openChannels" --type java

Length of output: 447


68-69: Verify thread safety of early counter increment

While the early increment is protected by the synchronized block, consider moving it after the channel creation to maintain the principle of performing operations as late as possible.


178-184: LGTM: Improved channel closure handling

The changes simplify the channel closure logic while maintaining proper cleanup order:

  1. Unregister the channel
  2. Decrement counter
  3. Close if no more channels

Let's verify the cleanup sequence is maintained in all code paths:

✅ Verification successful

Let me analyze the cleanup sequence from the search results:

  1. In OpenCryptoFile.java, the cleanup sequence is:

    • cleartextChannelClosed(): First unregisters channel, then decrements counter, and if counter is 0 calls close()
    • close(): Notifies the listener about file closure
  2. In ChunkIO.java, unregisterChannel() removes the channel from both readable and writable channel collections

  3. The test coverage in OpenCryptoFileTest.java verifies:

    • Channel closure triggers cleanup
    • Cleanup sequence is maintained (unregister before close)
    • Last modified time updates
    • Counter management
  4. The integration tests in CryptoFileSystemImplTest.java verify the full cleanup chain works correctly

The code shows proper cleanup sequence is maintained in all paths:

  1. Channel unregistration happens first via chunkIO.unregisterChannel()
  2. Channel count is decremented atomically
  3. File closure is triggered only when last channel is closed
  4. File closure notifies listeners for higher-level cleanup

LGTM: Channel cleanup sequence verified

The cleanup sequence is properly maintained in all code paths with good test coverage.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for all calls to unregisterChannel and close
rg "unregisterChannel|close\(" --type java -A 5

Length of output: 49192

@infeo infeo merged commit 4806744 into develop Dec 7, 2024
8 checks passed
@infeo infeo deleted the feature/inversion-of-chunkregister branch December 7, 2024 11:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant