-
Notifications
You must be signed in to change notification settings - Fork 39
Implement scanner API for Consensus Commit #2711
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
feeblefakie
merged 4 commits into
add-scanner-api-to-transaction-abstraction
from
implement-scanner-for-consensus-commit
Jun 2, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -24,8 +24,10 @@ | |
import com.scalar.db.exception.transaction.UnsatisfiedConditionException; | ||
import com.scalar.db.util.ScalarDbUtils; | ||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
import javax.annotation.concurrent.NotThreadSafe; | ||
import org.slf4j.Logger; | ||
|
@@ -98,7 +100,41 @@ public List<Result> scan(Scan scan) throws CrudException { | |
|
||
@Override | ||
public Scanner getScanner(Scan scan) throws CrudException { | ||
throw new UnsupportedOperationException("Implement later"); | ||
scan = copyAndSetTargetToIfNot(scan); | ||
Scanner scanner = crud.getScanner(scan); | ||
|
||
return new Scanner() { | ||
@Override | ||
public Optional<Result> one() throws CrudException { | ||
try { | ||
return scanner.one(); | ||
} catch (UncommittedRecordException e) { | ||
lazyRecovery(e); | ||
throw e; | ||
} | ||
} | ||
|
||
@Override | ||
public List<Result> all() throws CrudException { | ||
try { | ||
return scanner.all(); | ||
} catch (UncommittedRecordException e) { | ||
lazyRecovery(e); | ||
throw e; | ||
} | ||
} | ||
|
||
@Override | ||
public void close() throws CrudException { | ||
scanner.close(); | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public Iterator<Result> iterator() { | ||
return scanner.iterator(); | ||
} | ||
}; | ||
} | ||
|
||
/** @deprecated As of release 3.13.0. Will be removed in release 5.0.0. */ | ||
|
@@ -213,6 +249,10 @@ public void mutate(List<? extends Mutation> mutations) throws CrudException { | |
|
||
@Override | ||
public void commit() throws CommitException, UnknownTransactionStatusException { | ||
if (!crud.areAllScannersClosed()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check if all the scanners are closed before committing a transaction. |
||
throw new IllegalStateException(CoreError.CONSENSUS_COMMIT_SCANNER_NOT_CLOSED.buildMessage()); | ||
} | ||
|
||
// Execute implicit pre-read | ||
try { | ||
crud.readIfImplicitPreReadEnabled(); | ||
|
@@ -234,6 +274,12 @@ public void commit() throws CommitException, UnknownTransactionStatusException { | |
|
||
@Override | ||
public void rollback() { | ||
try { | ||
crud.closeScanners(); | ||
} catch (CrudException e) { | ||
logger.warn("Failed to close the scanner", e); | ||
} | ||
|
||
if (groupCommitter != null) { | ||
groupCommitter.remove(crud.getSnapshot().getId()); | ||
} | ||
|
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
Oops, something went wrong.
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.
[minor] Is there any reason not to close
scanner
here whileConsensusCommitManager.getScanner()
closes that?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.
We don't close the scanner here, but we close here:
https://github.com/scalar-labs/scalardb/pull/2711/files#diff-acc93f1366b03168017001646c7217f7dd1f3c7cbe8a41b6a9a8484dc5ef0905R406
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.
The reason is probably
ConsensusCommitManager
commits after one scan, whereasConsensusCommit
commits after doing several operations?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.
(Oops, sorry I thought here was in
catch (CrudException e) { ... }
block).I just thought we can immediately close the failed scanner as well as
ConsensusCommitManager.getScanner()
, for consistency and a bit faster resource release. It's just out of curiosity.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.
Ah, sorry for the lack of explanation.
This anonymous
Scanner
class wraps the scanner instance returned byCrudHandler
here:https://github.com/scalar-labs/scalardb/pull/2711/files#diff-ec7fb790c921b21b0690f23d557c721585b0c83ef32b729e7dada7009d6142d0R104
The
CrudHandler.getScanner()
method returns an instance ofConsensusCommitStorageScanner
if the scan result is not present in the snapshot.The
ConsensusCommitStorageScanner.one()
method immediately closes the underlying scanner when a failure occurs:https://github.com/scalar-labs/scalardb/pull/2711/files#diff-acc93f1366b03168017001646c7217f7dd1f3c7cbe8a41b6a9a8484dc5ef0905R424-R452
So, the behavior is consistent with
ConsensusCommitManager.getScanner()
.By the way, the anonymous
Scanner
class simply executes lazy recovery when anUncommittedRecordException
is caught.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.
Thanks for the explanation! Sounds good 💯