Skip to content
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

Fix bug the engine might choose a replica with a smaller head size to be the source of truth for auto-salvage (backport #1114) #1117

Merged
merged 2 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions pkg/controller/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,53 +540,52 @@ func (c *Controller) checkReplicaRevCounterSettingMatch() error {
return nil
}

// salvageRevisionCounterDisabledReplicas will find best replica
// salvageRevisionCounterDisabledReplicas will find the best replica
// for salvage recovering, based on lastModifyTime and HeadFileSize.
func (c *Controller) salvageRevisionCounterDisabledReplicas() error {
replicaCandidates := make(map[types.Replica]types.ReplicaSalvageInfo)
var lastModifyTime int64
var replicaCandidates []*types.ReplicaSalvageInfo
var lastModifyTime time.Time
for _, r := range c.replicas {
if r.Mode == types.ERR {
continue
}
repLastModifyTime, err := c.backend.backends[r.Address].backend.GetLastModifyTime()
repLastModifyTimeInt, err := c.backend.backends[r.Address].backend.GetLastModifyTime()
if err != nil {
return err
}
repLastModifyTime := time.Unix(0, repLastModifyTimeInt)

repHeadFileSize, err := c.backend.backends[r.Address].backend.GetHeadFileSize()
if err != nil {
return err
}

replicaCandidates[r] = types.ReplicaSalvageInfo{
replicaCandidates = append(replicaCandidates, &types.ReplicaSalvageInfo{
Address: r.Address,
LastModifyTime: repLastModifyTime,
HeadFileSize: repHeadFileSize,
}
if lastModifyTime == 0 ||
repLastModifyTime > lastModifyTime {
})
if lastModifyTime.IsZero() || repLastModifyTime.After(lastModifyTime) {
lastModifyTime = repLastModifyTime
}
}

if len(replicaCandidates) == 0 {
return fmt.Errorf("cannot find any replica for salvage")
}
var bestCandidate types.Replica
lastTime := time.Unix(0, lastModifyTime)
var largestSize int64
for r, salvageReplica := range replicaCandidates {
t := time.Unix(0, salvageReplica.LastModifyTime)
// Any replica within 5 seconds before lastModifyTime
// can be good replica.
if t.Add(lastModifyCheckPeriod).After(lastTime) {
if salvageReplica.HeadFileSize >= largestSize {
bestCandidate = r
var bestCandidate *types.ReplicaSalvageInfo
for _, salvageReplica := range replicaCandidates {
if salvageReplica.LastModifyTime.Add(lastModifyCheckPeriod).After(lastModifyTime) {
if bestCandidate == nil ||
salvageReplica.HeadFileSize > bestCandidate.HeadFileSize ||
(salvageReplica.HeadFileSize == bestCandidate.HeadFileSize &&
salvageReplica.LastModifyTime.After(bestCandidate.LastModifyTime)) {
bestCandidate = salvageReplica
}
}
}

if bestCandidate == (types.Replica{}) {
if bestCandidate == nil {
return fmt.Errorf("BUG: Should find one candidate for salvage")
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ type Replica struct {
}

type ReplicaSalvageInfo struct {
LastModifyTime int64
Address string
LastModifyTime time.Time
HeadFileSize int64
}

Expand Down