fix: recheck attach retry expiry and distinguish backup name conflicts

This commit is contained in:
pingqiu committed 2026-11-29 08:41:35 -08:00
1 parent 3a64c95f45
commit 18d2f65a3e
2 files changed
+21 -9

No files matched your search

+15 -9
View File
@@ -108,8 +108,12 @@ func volumeBackup(ctx context.Context, env verbEnv, o backupOpts, stdout, stderr
return 1
}
a, refusal, err := volumeSnapshot(ctx, env, e["SWB_STATUS_ADDR"], o.Volume, o.Name, id, o.Timeout)
if refusal != "" {
return refuse("snapshot", fmt.Sprintf("%s; a cut that timed out keeps what it uploaded; after correcting the refusal, run the same command (it resumes the same request and reuses completed uploads): %s", refusal, again))
if refusal != nil {
text := refusal.Code + ": " + refusal.Message
if refusal.Code == "snapshot_name_taken" {
return refuse("snapshot", fmt.Sprintf("%s; choose a new snapshot name: sudo sw-block volume backup %s --name <new-name>", text, o.Volume))
}
return refuse("snapshot", fmt.Sprintf("%s; a cut that timed out keeps what it uploaded; after correcting the refusal, run the same command (it resumes the same request and reuses completed uploads): %s", text, again))
}
if err != nil {
fmt.Fprintf(stderr, "sw-block volume backup: snapshot: %v; a cut that timed out keeps what it uploaded; to finish, run the same command (it resumes the same request and reuses completed uploads): %s\n", err, again)
@@ -121,24 +125,26 @@ func volumeBackup(ctx context.Context, env verbEnv, o backupOpts, stdout, stderr
return 0
}
type snapshotRefusal struct{ Code, Message string }
// volumeSnapshot is the holder's POST /chunk/snapshot with a fixed request
// id. Only a complete answer naming this volume and name is the snapshot; a
// refusal returns its code and message; anything else is an unknown outcome
// that the same request resolves (a snapshot is one conditional HEAD write:
// it exists or it does not, and a replay finds it).
func volumeSnapshot(ctx context.Context, env verbEnv, status, volume, name, requestID string, timeout time.Duration) (snapshotAnswer, string, error) {
func volumeSnapshot(ctx context.Context, env verbEnv, status, volume, name, requestID string, timeout time.Duration) (snapshotAnswer, *snapshotRefusal, error) {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
body, _ := json.Marshal(map[string]any{"volumeID": volume, "name": name, "requestID": requestID, "consistency": "crash", "labels": map[string]string{backupLabel: backupTag(requestID)}})
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://"+status+"/chunk/snapshot", bytes.NewReader(body))
if err != nil {
return snapshotAnswer{}, "", err
return snapshotAnswer{}, nil, err
}
req.Header.Set("Content-Type", "application/json")
client := *env.HTTP
client.Timeout = timeout + 5*time.Second
unknown := func(why string) (snapshotAnswer, string, error) {
return snapshotAnswer{}, "", fmt.Errorf("the snapshot's outcome is unknown (%s)", why)
unknown := func(why string) (snapshotAnswer, *snapshotRefusal, error) {
return snapshotAnswer{}, nil, fmt.Errorf("the snapshot's outcome is unknown (%s)", why)
}
resp, err := client.Do(req)
if err != nil {
@@ -154,7 +160,7 @@ func volumeSnapshot(ctx context.Context, env verbEnv, status, volume, name, requ
if json.Unmarshal(b, &eb) != nil || eb.Code == "" || eb.Code == "outcome_unknown" {
return unknown(fmt.Sprintf("http %d %s %s", resp.StatusCode, eb.Code, strings.TrimSpace(eb.Message)))
}
return snapshotAnswer{}, eb.Code + ": " + eb.Message, nil
return snapshotAnswer{}, &snapshotRefusal{Code: eb.Code, Message: eb.Message}, nil
}
var a snapshotAnswer
if err := json.Unmarshal(b, &a); err != nil || a.VolumeID != volume || a.Name != name {
@@ -164,9 +170,9 @@ func volumeSnapshot(ctx context.Context, env verbEnv, status, volume, name, requ
return unknown("the answer lacks the snapshot's head generation, cut LSN or time")
}
if got := a.Labels[backupLabel]; got != backupTag(requestID) {
return snapshotAnswer{}, fmt.Sprintf("snapshot_name_taken: %s already has a snapshot %s that this command did not take (its %s label is %q); choose another --name", volume, shQuote(name), backupLabel, got), nil
return snapshotAnswer{}, &snapshotRefusal{Code: "snapshot_name_taken", Message: fmt.Sprintf("%s already has a snapshot %s that this command did not take (its %s label is %q); choose another --name", volume, shQuote(name), backupLabel, got)}, nil
}
return a, "", nil
return a, nil, nil
}
type restoreOpts struct {
@@ -256,6 +256,12 @@ func (p *DurableProvider) chunkAuthorityWait(volumeID string, h *volHandle, gen
}
continue
}
// A first grant wins on the bound's tick. Later attempts must not
// start after expiry during the retry backoff or the next poll.
if attempts > 0 && gaveUp() {
expired, ended = true, "assignment allowance ended before attach retry"
return
}
if attempts++; attempts == 1 {
log.Printf("durable: chunk layer for volume=%s: master authority after %s (replica=%q epoch=%d endpoint=%d); first attach attempt",
volumeID, time.Since(start), proj.ReplicaID, proj.Epoch, proj.EndpointVersion)