Skip to content

Commit ce93650

Browse files
committed
storage: add a generation counter to RangeDescriptors
Add a generation counter to RangeDescriptors which tracks the number of splits and merges that range has seen. The counter is necessary to avoid a rare edge case when a merge and replica addition occur concurrently. See the comment on TestStoreRangeMergeAddReplicaRace for details. The approach taken here to preserve encoding compatibility with existing RangeDescriptors is to make RangeDescriptor a proto3 message. Adding a new default zero integer field to a proto2 message is complicated because proto2 makes a distinction between an integer that is zero and integer that is unset. Release note: None
1 parent 28c7edc commit ce93650

9 files changed

Lines changed: 312 additions & 82 deletions

File tree

c-deps/libroach/protos/roachpb/metadata.pb.cc

Lines changed: 35 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

c-deps/libroach/protos/roachpb/metadata.pb.h

Lines changed: 37 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/roachpb/metadata.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"strings"
2323

2424
"github.com/cockroachdb/cockroach/pkg/util/humanizeutil"
25+
"github.com/gogo/protobuf/proto"
2526
"github.com/pkg/errors"
2627
)
2728

@@ -139,6 +140,22 @@ func (r RangeDescriptor) IsInitialized() bool {
139140
return len(r.EndKey) != 0
140141
}
141142

143+
// GetGeneration returns the generation of this RangeDescriptor.
144+
func (r RangeDescriptor) GetGeneration() int64 {
145+
if r.Generation != nil {
146+
return *r.Generation
147+
}
148+
return 0
149+
}
150+
151+
// IncrementGeneration increments the generation of this RangeDescriptor.
152+
func (r *RangeDescriptor) IncrementGeneration() {
153+
// Create a new *int64 for the new generation. We permit shallow copies of
154+
// RangeDescriptors, so we need to be careful not to mutate the
155+
// potentially-shared generation counter.
156+
r.Generation = proto.Int64(r.GetGeneration() + 1)
157+
}
158+
142159
// Validate performs some basic validation of the contents of a range descriptor.
143160
func (r RangeDescriptor) Validate() error {
144161
if r.NextReplicaID == 0 {
@@ -182,7 +199,7 @@ func (r RangeDescriptor) String() string {
182199
} else {
183200
buf.WriteString("<no replicas>")
184201
}
185-
fmt.Fprintf(&buf, ", next=%d]", r.NextReplicaID)
202+
fmt.Fprintf(&buf, ", next=%d, gen=%d]", r.NextReplicaID, r.Generation)
186203

187204
return buf.String()
188205
}

0 commit comments

Comments
 (0)