forked from etcd-io/etcd
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #300 from tjungblu/bump_mark_force_new_c
DOWNSTREAM: <carry>: ETCD-696: Add rev bumping to force-new-cluster
- Loading branch information
Showing
8 changed files
with
129 additions
and
4 deletions.
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,65 @@ | ||
package revbump | ||
|
||
import ( | ||
"go.etcd.io/etcd/server/v3/mvcc" | ||
"go.etcd.io/etcd/server/v3/mvcc/backend" | ||
"go.etcd.io/etcd/server/v3/mvcc/buckets" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func UnsafeModifyLastRevision(lg *zap.Logger, bumpAmount uint64, be backend.Backend) error { | ||
defer be.ForceCommit() | ||
|
||
tx := be.BatchTx() | ||
tx.LockOutsideApply() | ||
defer tx.Unlock() | ||
|
||
latest, err := unsafeGetLatestRevision(tx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
latest = unsafeBumpRevision(lg, tx, latest, int64(bumpAmount)) | ||
unsafeMarkRevisionCompacted(lg, tx, latest) | ||
return nil | ||
} | ||
|
||
func unsafeBumpRevision(lg *zap.Logger, tx backend.BatchTx, latest revision, amount int64) revision { | ||
lg.Info( | ||
"bumping latest revision", | ||
zap.Int64("latest-revision", latest.main), | ||
zap.Int64("bump-amount", amount), | ||
zap.Int64("new-latest-revision", latest.main+amount), | ||
) | ||
|
||
latest.main += amount | ||
latest.sub = 0 | ||
k := make([]byte, revBytesLen) | ||
revToBytes(k, latest) | ||
tx.UnsafePut(buckets.Key, k, []byte{}) | ||
|
||
return latest | ||
} | ||
|
||
func unsafeMarkRevisionCompacted(lg *zap.Logger, tx backend.BatchTx, latest revision) { | ||
lg.Info( | ||
"marking revision compacted", | ||
zap.Int64("revision", latest.main), | ||
) | ||
|
||
mvcc.UnsafeSetScheduledCompact(tx, latest.main) | ||
} | ||
|
||
func unsafeGetLatestRevision(tx backend.BatchTx) (revision, error) { | ||
var latest revision | ||
err := tx.UnsafeForEach(buckets.Key, func(k, _ []byte) (err error) { | ||
rev := bytesToRev(k) | ||
|
||
if rev.GreaterThan(latest) { | ||
latest = rev | ||
} | ||
|
||
return nil | ||
}) | ||
return latest, err | ||
} |
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,46 @@ | ||
package revbump | ||
|
||
import "encoding/binary" | ||
|
||
// revBytesLen is the byte length of a normal revision. | ||
// First 8 bytes is the revision.main in big-endian format. The 9th byte | ||
// is a '_'. The last 8 bytes is the revision.sub in big-endian format. | ||
const revBytesLen = 8 + 1 + 8 | ||
const markedRevBytesLen = revBytesLen + 1 | ||
|
||
// A revision indicates modification of the key-value space. | ||
// The set of changes that share same main revision changes the key-value space atomically. | ||
type revision struct { | ||
// main is the main revision of a set of changes that happen atomically. | ||
main int64 | ||
|
||
// sub is the sub revision of a change in a set of changes that happen | ||
// atomically. Each change has different increasing sub revision in that | ||
// set. | ||
sub int64 | ||
} | ||
|
||
func (a revision) GreaterThan(b revision) bool { | ||
if a.main > b.main { | ||
return true | ||
} | ||
if a.main < b.main { | ||
return false | ||
} | ||
return a.sub > b.sub | ||
} | ||
|
||
// revToBytes should be synced with function in server | ||
// https://github.com/etcd-io/etcd/blob/main/server/storage/mvcc/revision.go | ||
func revToBytes(bytes []byte, rev revision) { | ||
binary.BigEndian.PutUint64(bytes[0:8], uint64(rev.main)) | ||
bytes[8] = '_' | ||
binary.BigEndian.PutUint64(bytes[9:], uint64(rev.sub)) | ||
} | ||
|
||
func bytesToRev(bytes []byte) revision { | ||
return revision{ | ||
main: int64(binary.BigEndian.Uint64(bytes[0:8])), | ||
sub: int64(binary.BigEndian.Uint64(bytes[9:])), | ||
} | ||
} |