Files
minio/cmd/restore-capability.go
T
Feng Ruohang 45b1e4bc00 fix(restore): stage native restores outside the object lock
The unreleased native-restore fixes after RELEASE.2026-10-26T00-00-00Z
let the lock decide who owns a restore. Since fa42a0982 a single-pool
worker holds the object lock for the whole tier download, and since
7f38bf628 a busy lock means "a restore runs". 7f38bf628 also keeps the
restore's data in a marked multipart upload. Two regressions followed:

- While an old version restored, every GET, HEAD and write of the same
  key waited for the download. On a single pool this is new; with
  several pools the released code already held the lock that long.
- An ordinary upload whose completion failed after staging its metadata
  became InvalidUploadID for every caller, and could not be aborted for
  an hour. The marker that hid restore uploads could not tell them apart
  once a completion had rewritten the upload's xl.meta.

A restore now runs in two phases:

- The worker erasure-codes the tier copy into the tmp volume of the
  version's set, without the object lock, as heal does. A drive that
  fails any part, or misses a part when CheckParts runs before the
  commit, takes no part in the commit.
- A restore lock per version tells whether a restore runs. POST takes it
  before the object lock and hands it to the worker. It is a liveness
  hint: only the checks under the object lock decide what is written.
- The commit takes the object lock briefly, on one pool or several. It
  reads the version again and checks the attempt's ownership, the
  version identity, the tier copy, the part layout and the erasure
  layout. It then publishes with RenameData drive by drive, never
  through erasure renameData's undo, and needs the write quorum. The
  replaced data directory goes only when every published drive reports
  the same one.
- A publication that reached some drives but not the write quorum
  converges under the same lock hold, in bounded retries after it: back
  to the unrestored version on every drive that holds it, or to a
  committed restore when the copy turns out to be on a write quorum.
  Each pass first checks that every drive holds what the version held
  before the commit, the commit wrote, or a convergence wrote, and stops
  on anything else. It deletes no directory.

Multipart goes back to main: erasure-multipart.go and
bucket-object-lock.go are identical to 69fcf50cd. The commit lock, the
marker and the restore exceptions of multipart completion and of the
null-version Object Lock check are gone, and single-pool completion
again takes the upload lock first and the object lock right before it
commits. With several pools, abandoned-data cleanup now takes the pools
lock that writes take, as heal does, so it cannot remove a directory
that a commit moved in place and has not yet referenced. The restore
capability becomes protocol 2, and a queued worker checks it again.

Recovery after a coordinator crash during publication, and storage RPCs
that complete after their caller returned, stay excluded. Main has the
same exposure, and they are tracked separately.

Tests:
- New regression tests cover both problems. They fail on f85d4a5fa,
  and the multipart one also passes on main.
- Fault-injection tests cover a publication below the write quorum,
  including seven of twelve drives on 8+4; lost answers; convergence
  retries and stops; the old data directory; a stale inline marker; a
  recreated tmp volume; a published revision read across its expiry; a
  second POST; a target deleted during the download; a tier 404; and
  pools-locked abandoned cleanup.
- Thirteen tests of the upload marker from 7f38bf628 are dropped, and
  so is TestNativeRestoreConcurrentCompletion from fa42a0982, which
  completed the restore's upload from a client. Four tests are reframed
  for the new semantics, including the lock-domain restore test, which
  now gates the commit.
- Three self-recursive WriteMetadata test doubles are fixed, and the
  helpers of the dropped tests are removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Feng Ruohang <rh@vonng.com>
2026-11-06 11:30:29 +08:00

185 lines
5.9 KiB
Go

// Copyright (c) 2026 Feng Ruohang
//
// This file is part of Silo Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"context"
"errors"
"fmt"
"slices"
"sync"
"time"
"github.com/minio/minio/internal/grid"
)
// A native restore keeps its multipart upload private and marks the version
// it rehydrates (restore-attempt.go). A server that predates this ignores
// the marker: it would list, change, abort or complete the upload. Native
// restore therefore runs only while every server of the cluster positively
// reports that it understands the marker. A server that does not answer,
// or does not know the question, as an older server does not, blocks it.
// nativeRestoreCapability is the native restore protocol this server speaks.
const nativeRestoreCapability = "2"
const (
peerRESTRestoreCapability = "restore"
// restoreCapabilityTTL bounds how long a positive answer is reused while
// no server reconnected.
restoreCapabilityTTL = 10 * time.Second
// restoreCapabilityTimeout bounds the question to each server.
restoreCapabilityTimeout = 5 * time.Second
)
var errNativeRestoreUnavailable = errors.New("native restore is unavailable during a rolling upgrade: not every server of the cluster supports it yet")
// restoreCapabilityHandler answers the question on this server.
func restoreCapabilityHandler(*grid.MSS) (*grid.MSS, *grid.RemoteErr) {
return grid.NewMSSWith(map[string]string{peerRESTRestoreCapability: nativeRestoreCapability}), nil
}
// restoreCapabilityPeer is another server of the cluster.
type restoreCapabilityPeer interface {
// restoreConnection identifies the current connection to the server; it
// changes when the server reconnects, as a restarted one does. ok is
// false when the server is not connected.
restoreConnection() (key string, ok bool)
restoreCapability(ctx context.Context) (string, error)
}
type restorePeerClient struct{ *peerRESTClient }
func (c restorePeerClient) restoreConnection() (string, bool) {
if c.peerRESTClient == nil || c.gridConn == nil {
return "", false
}
conn := c.gridConn()
if conn == nil {
return "", false
}
st := conn.Stats()
if st.Connected == 0 {
return "", false
}
// Keys are compared by the position of their server.
return fmt.Sprintf("%d/%d", st.LastConnectTime.UnixNano(), st.ReconnectCount), true
}
func (c restorePeerClient) restoreCapability(ctx context.Context) (string, error) {
conn := c.gridConn()
if conn == nil {
return "", errPeerOffline
}
resp, err := restoreCapabilityRPC.Call(ctx, conn, grid.NewMSS())
if err != nil {
return "", err
}
return resp.Get(peerRESTRestoreCapability), nil
}
// restoreCapabilityGate decides whether native restore may run, caching a
// positive answer for restoreCapabilityTTL while the connections to all
// servers stay the same. A negative answer is not cached.
type restoreCapabilityGate struct {
peers func() []restoreCapabilityPeer
once sync.Once
turn chan struct{} // one check at a time
checked time.Time
keys []string
}
var globalRestoreCapability = &restoreCapabilityGate{peers: notificationRestorePeers}
func notificationRestorePeers() []restoreCapabilityPeer {
if globalNotificationSys == nil {
return nil
}
peers := make([]restoreCapabilityPeer, 0, len(globalNotificationSys.peerClients))
for _, client := range globalNotificationSys.peerClients {
peers = append(peers, restorePeerClient{client})
}
return peers
}
// nativeRestoreAvailable returns errNativeRestoreUnavailable unless every
// other server of the cluster supports native restore.
func nativeRestoreAvailable(ctx context.Context) error {
return globalRestoreCapability.check(ctx)
}
func (g *restoreCapabilityGate) check(ctx context.Context) error {
g.once.Do(func() { g.turn = make(chan struct{}, 1) })
// One deadline covers the wait for another check and the questions.
ctx, cancel := context.WithTimeout(ctx, restoreCapabilityTimeout)
defer cancel()
select {
case g.turn <- struct{}{}:
case <-ctx.Done():
return errNativeRestoreUnavailable
}
defer func() { <-g.turn }()
// Read the connections only now: one may have changed while this check
// waited for another.
peers := g.peers()
connections := func() ([]string, bool) {
keys := make([]string, len(peers))
for i, p := range peers {
key, ok := p.restoreConnection()
if !ok {
return nil, false
}
keys[i] = key
}
return keys, true
}
keys, ok := connections()
if !ok {
g.checked, g.keys = time.Time{}, nil
return errNativeRestoreUnavailable
}
if ctx.Err() != nil {
return errNativeRestoreUnavailable
}
if !g.checked.IsZero() && time.Since(g.checked) < restoreCapabilityTTL && slices.Equal(keys, g.keys) {
return nil
}
g.checked, g.keys = time.Time{}, nil
answers := make([]bool, len(peers))
var wg sync.WaitGroup
for i, p := range peers {
wg.Add(1)
go func() {
defer wg.Done()
version, err := p.restoreCapability(ctx)
answers[i] = err == nil && version == nativeRestoreCapability
}()
}
wg.Wait()
// An answer counts only for the connection it was asked on: a server
// that reconnected meanwhile may be another binary.
after, ok := connections()
if !ok || !slices.Equal(keys, after) || slices.Contains(answers, false) || ctx.Err() != nil {
return errNativeRestoreUnavailable
}
g.checked, g.keys = time.Now(), keys
return nil
}