Skip to content

Commit 3be380b

Browse files
committed
Truncate Raft logs even when no txn commits are happening
If there are no txn commits but many txns which allocate new read timestamps, the Raft log can still grow a lot because of MaxAssigned updates. This PR would truncate those logs as well to create new snapshot.
1 parent bf4f3e0 commit 3be380b

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

worker/draft.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -546,9 +546,13 @@ func (n *node) retrieveSnapshot(snap pb.Snapshot) error {
546546

547547
func (n *node) proposeSnapshot(discardN int) error {
548548
snap, err := n.calculateSnapshot(discardN)
549-
if err != nil || snap == nil {
549+
if err != nil {
550+
glog.Warningf("Got error while calculating snapshot: %v", err)
550551
return err
551552
}
553+
if snap == nil {
554+
return nil
555+
}
552556
proposal := &pb.Proposal{
553557
Snapshot: snap,
554558
}
@@ -959,7 +963,7 @@ func (n *node) abortOldTransactions() {
959963
// At i7, min pending start ts = S3, therefore snapshotIdx = i5 - 1 = i4.
960964
// At i7, max commit ts = C1, therefore readTs = C1.
961965
func (n *node) calculateSnapshot(discardN int) (*pb.Snapshot, error) {
962-
_, span := otrace.StartSpan(n.ctx, "Propose.Snapshot")
966+
_, span := otrace.StartSpan(n.ctx, "Calculate.Snapshot")
963967
defer span.End()
964968

965969
if atomic.LoadInt32(&n.streaming) > 0 {
@@ -974,6 +978,18 @@ func (n *node) calculateSnapshot(discardN int) (*pb.Snapshot, error) {
974978
}
975979
span.Annotatef(nil, "First index: %d", first)
976980

981+
rsnap, err := n.Store.Snapshot()
982+
if err != nil {
983+
return nil, err
984+
}
985+
var snap pb.Snapshot
986+
if len(rsnap.Data) > 0 {
987+
if err := snap.Unmarshal(rsnap.Data); err != nil {
988+
return nil, err
989+
}
990+
}
991+
span.Annotatef(nil, "Last snapshot: %+v", snap)
992+
977993
last := n.Applied.DoneUntil()
978994
if int(last-first) < discardN {
979995
span.Annotate(nil, "Skipping due to insufficient entries")
@@ -999,7 +1015,8 @@ func (n *node) calculateSnapshot(discardN int) (*pb.Snapshot, error) {
9991015
// snapshotIdx. In any case, we continue picking up txn updates, to generate
10001016
// a maxCommitTs, which would become the readTs for the snapshot.
10011017
minPendingStart := posting.Oracle().MinPendingStartTs()
1002-
var maxCommitTs, snapshotIdx, maxCommitIdx uint64
1018+
maxCommitTs := snap.ReadTs
1019+
var snapshotIdx uint64
10031020
for _, entry := range entries {
10041021
if entry.Type != raftpb.EntryNormal {
10051022
continue
@@ -1019,7 +1036,6 @@ func (n *node) calculateSnapshot(discardN int) (*pb.Snapshot, error) {
10191036
for _, txn := range proposal.Delta.GetTxns() {
10201037
maxCommitTs = x.Max(maxCommitTs, txn.CommitTs)
10211038
}
1022-
maxCommitIdx = entry.Index
10231039
}
10241040
}
10251041
if maxCommitTs == 0 {
@@ -1029,8 +1045,10 @@ func (n *node) calculateSnapshot(discardN int) (*pb.Snapshot, error) {
10291045
if snapshotIdx <= 0 {
10301046
// It is possible that there are no pending transactions. In that case,
10311047
// snapshotIdx would be zero.
1032-
span.Annotatef(nil, "Using maxCommitIdx as snapshotIdx: %d", maxCommitIdx)
1033-
snapshotIdx = maxCommitIdx
1048+
if len(entries) > 0 {
1049+
snapshotIdx = entries[len(entries)-1].Index
1050+
}
1051+
span.Annotatef(nil, "snapshotIdx is zero. Using last entry's index: %d", snapshotIdx)
10341052
}
10351053

10361054
numDiscarding := snapshotIdx - first + 1
@@ -1045,13 +1063,13 @@ func (n *node) calculateSnapshot(discardN int) (*pb.Snapshot, error) {
10451063
return nil, nil
10461064
}
10471065

1048-
snap := &pb.Snapshot{
1066+
result := &pb.Snapshot{
10491067
Context: n.RaftContext,
10501068
Index: snapshotIdx,
10511069
ReadTs: maxCommitTs,
10521070
}
1053-
span.Annotatef(nil, "Got snapshot: %+v", snap)
1054-
return snap, nil
1071+
span.Annotatef(nil, "Got snapshot: %+v", result)
1072+
return result, nil
10551073
}
10561074

10571075
func (n *node) joinPeers() error {

0 commit comments

Comments
 (0)