-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Implement rollback and scan #59
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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 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 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 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 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 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 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 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,94 @@ | ||
package commands | ||
|
||
import ( | ||
"github.com/pingcap-incubator/tinykv/kv/tikv/storage/kvstore" | ||
"github.com/pingcap-incubator/tinykv/proto/pkg/kvrpcpb" | ||
) | ||
|
||
type Rollback struct { | ||
request *kvrpcpb.BatchRollbackRequest | ||
} | ||
|
||
func NewRollback(request *kvrpcpb.BatchRollbackRequest) Rollback { | ||
return Rollback{request} | ||
} | ||
|
||
func (r *Rollback) BuildTxn(txn *kvstore.MvccTxn) error { | ||
txn.StartTS = &r.request.StartVersion | ||
for _, k := range r.request.Keys { | ||
err := rollbackKey(k, txn) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func rollbackKey(key []byte, txn *kvstore.MvccTxn) error { | ||
lock, err := txn.GetLock(key) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if lock == nil || lock.TS != *txn.StartTS { | ||
// There is no lock, check the write status. | ||
existingWrite, ts, err := txn.FindWrite(key, *txn.StartTS) | ||
if err != nil { | ||
return err | ||
} | ||
if existingWrite == nil { | ||
// There is no write either, presumably the prewrite was lost. We insert a rollback write anyway. | ||
write := kvstore.Write{StartTS: *txn.StartTS, Kind: kvstore.WriteKindRollback} | ||
txn.PutWrite(key, &write, *txn.StartTS) | ||
|
||
return nil | ||
} else { | ||
if existingWrite.Kind == kvstore.WriteKindRollback { | ||
// The key has already been rolled back, so nothing to do. | ||
return nil | ||
} | ||
|
||
// The key has already been committed. This should not happen since the client should never send both | ||
// commit and rollback requests. | ||
return &Committed{key, ts} | ||
} | ||
} | ||
|
||
if lock.Kind == kvstore.WriteKindPut { | ||
txn.DeleteValue(key) | ||
} | ||
|
||
write := kvstore.Write{StartTS: *txn.StartTS, Kind: kvstore.WriteKindRollback} | ||
txn.PutWrite(key, &write, *txn.StartTS) | ||
txn.DeleteLock(key) | ||
|
||
return nil | ||
} | ||
|
||
func (r *Rollback) Context() *kvrpcpb.Context { | ||
return r.request.Context | ||
} | ||
|
||
func (r *Rollback) Response() interface{} { | ||
return &kvrpcpb.BatchRollbackResponse{} | ||
} | ||
|
||
func (r *Rollback) HandleError(err error) interface{} { | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
if regionErr := extractRegionError(err); regionErr != nil { | ||
resp := kvrpcpb.BatchRollbackResponse{} | ||
resp.RegionError = regionErr | ||
return &resp | ||
} | ||
|
||
if e, ok := err.(KeyError); ok { | ||
resp := kvrpcpb.BatchRollbackResponse{} | ||
resp.Error = e.KeyErrors()[0] | ||
return &resp | ||
} | ||
|
||
return nil | ||
} |
This file contains 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,75 @@ | ||
package commands | ||
|
||
import ( | ||
"github.com/pingcap-incubator/tinykv/kv/tikv/storage/kvstore" | ||
"github.com/pingcap-incubator/tinykv/proto/pkg/kvrpcpb" | ||
) | ||
|
||
type Scan struct { | ||
request *kvrpcpb.ScanRequest | ||
response *kvrpcpb.ScanResponse | ||
} | ||
|
||
func NewScan(request *kvrpcpb.ScanRequest) Scan { | ||
return Scan{request, &kvrpcpb.ScanResponse{}} | ||
} | ||
|
||
func (s *Scan) BuildTxn(txn *kvstore.MvccTxn) error { | ||
txn.StartTS = &s.request.Version | ||
|
||
scanner := kvstore.NewScanner(s.request.StartKey, txn) | ||
limit := s.request.Limit | ||
for { | ||
if limit == 0 { | ||
// We've scanned up to the requested limit. | ||
return nil | ||
} | ||
limit -= 1 | ||
|
||
key, value, err := scanner.Next() | ||
if err != nil { | ||
// Key error (e.g., key is locked) is saved as an error in the scan for the client to handle. | ||
if e, ok := err.(KeyError); ok { | ||
keyErrs := e.KeyErrors() | ||
if len(keyErrs) == 0 { | ||
pair := kvrpcpb.KvPair{} | ||
pair.Error = keyErrs[0] | ||
s.response.Pairs = append(s.response.Pairs, &pair) | ||
continue | ||
} | ||
} | ||
// Any other kind of error, we can't handle so quit the scan. | ||
return err | ||
} | ||
if key == nil { | ||
// Reached the end of the DB | ||
return nil | ||
} | ||
|
||
pair := kvrpcpb.KvPair{} | ||
pair.Key = key | ||
pair.Value = value | ||
s.response.Pairs = append(s.response.Pairs, &pair) | ||
} | ||
} | ||
|
||
func (s *Scan) Context() *kvrpcpb.Context { | ||
return s.request.Context | ||
} | ||
|
||
func (s *Scan) Response() interface{} { | ||
return s.response | ||
} | ||
|
||
func (s *Scan) HandleError(err error) interface{} { | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
if regionErr := extractRegionError(err); regionErr != nil { | ||
s.response.RegionError = regionErr | ||
return &s.response | ||
} | ||
|
||
return nil | ||
} |
This file contains 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.
let's move all errors into one file?
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.
This error can't be with the others in commands because that would create an import cycle. I could pull it out into its own file in kvstore, but since there is just one error I thought it was better to put it here. I don't mind pulling it out though if you prefer.
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.
oh, seems can't still put it in
commands/errors.go
? 🙁let's keep it now, and make a reorganization later. As I assumed, the course would be only left the predefined errors, types and latch in final, so the errors should be in one file, not everywhere.