Concurrent backups through two Alphas get the same backupNum, and the series can no longer be restored

Aperta
#9,900 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 10 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
backend-api-design, databases, go
Ambito
databases

Direzione di ricerca

Start with worker/backup.go around ProcessBackupRequest, backupLock and CompleteBackup, then worker/backup_manifest.go verifyManifests. Run the reproduction test the reporter supplied in worker/ to see the duplicate backupNum. Done means concurrent backups through two Alphas no longer produce duplicate numbers, or the second completion is refused and the chain stays restorable. A cluster-wide fix needs domain knowledge of Zero, so this is not a first task.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Summary

Two backup requests sent at the same time through two different Alphas both complete with the same backupNum. After that, restoring the latest series fails with found a manifest with backup number N but expected N+1 until manifest.json is edited by hand, and every later incremental extends the broken series.

Checked against main at e95fc26526adf3929315beba174d35f6a101003c and tag v25.5.0. The line numbers below are from main.

Mechanism (from reading the code)

  • ProcessBackupRequest serialises backups with backupLock (worker/backup.go:229, 245), a process-local sync.Mutex. The code comment explains why: "Multiple requests could lead to multiple backups with the same backupNum in their manifest."
  • It reads the latest manifest before doing any work, and computes BackupNum = latest.BackupNum + 1.
  • BackupGroup backs up the coordinator's own group locally. The per-node task guard (worker/draft.go:159, "another backup operation is already running") only refuses a second backup on the same node.
  • CompleteBackup (worker/backup.go:709) re-reads manifest.json and appends its own entry, without checking whether the series moved on while the backup ran.

So two requests that reach two Alphas (for example two replicas behind a load-balanced service, or two schedulers) both pass every guard. They write two data directories and append two manifests with the same number. verifyManifests (worker/backup_manifest.go:44) then refuses the series on restore.

If both completions read manifest.json before either one writes it, one entry is lost instead, and that backup cannot be reached even though it reported success. We inferred this second case from the code and did not test it.

Reproduction (unit test, no cluster needed)

The test plays the interleaving one step at a time by calling CompleteBackup directly. Drop it into worker/ on main:

package worker

import (
	"context"
	"fmt"
	"os"
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/dgraph-io/dgraph/v25/protos/pb"
)

// ProcessBackupRequest serialises
// backups with backupLock, a per-process mutex, and reads the chain's latest
// manifest before it writes anything. Two backup requests sent through two
// alphas therefore both read the same latest manifest (backupNum N) and both
// complete with backupNum N+1. This test plays that interleaving one step at a
// time (no cluster needed: CompleteBackup is the step that publishes the
// manifest) and states the required outcome: the second completion is refused
// and the chain stays restorable.
func TestCompleteBackup_SecondCompletionOfTheSameNumberIsRefused(t *testing.T) {
	dir := t.TempDir()
	h, uri := testFileHandlerForDir(t, dir)

	// writeData creates the group file a manifest names, as WriteBackup would.
	writeData := func(path string, readTs uint64) {
		require.NoError(t, os.MkdirAll(filepath.Join(dir, path), 0o755))
		require.NoError(t, os.WriteFile(filepath.Join(dir, path, backupName(readTs, 1)), []byte("x"), 0o644))
	}
	complete := func(m Manifest) error {
		bp := NewBackupProcessor(nil, &pb.BackupRequest{Destination: dir})
		defer bp.Close()
		return bp.CompleteBackup(context.Background(), &m)
	}
	manifest := func(typ string, num, readTs uint64, path string) Manifest {
		writeData(path, readTs)
		return Manifest{
			ManifestBase: ManifestBase{Type: typ, BackupId: "chain", BackupNum: num, ReadTs: readTs,
				Version: 2105, Path: path, Compression: "snappy"},
			Groups: map[uint32][]string{1: {"0-name"}},
		}
	}

	require.NoError(t, complete(manifest("full", 1, 100, "dgraph.full")))

	// Two coordinators read the latest manifest (num 1) before either completes.
	latest, err := GetLatestManifest(h, uri)
	require.NoError(t, err)
	next := latest.BackupNum + 1
	a := manifest("incremental", next, 200, "dgraph.incA")
	b := manifest("incremental", next, 210, "dgraph.incB")

	require.NoError(t, complete(a), "the first completion")
	errB := complete(b)

	master, err := GetManifestNoUpgrade(h, uri)
	require.NoError(t, err)
	var nums []string
	for _, m := range master.Manifests {
		nums = append(nums, fmt.Sprintf("%s#%d@%d", m.Type, m.BackupNum, m.ReadTs))
	}
	_, restoreErr := getManifestsToRestore(h, uri, &pb.RestoreRequest{})

	require.Error(t, errB, "a second backup completed with backupNum %d while the chain already held it; "+
		"manifest now %v; restore of the latest series: %v", next, nums, restoreErr)
	require.NoError(t, restoreErr, "the chain must stay restorable; manifest %v", nums)
}

Output, run on our fork (its worker/ backup code is upstream v25.5.0 unchanged; we did not run it on main, whose backupLock, CompleteBackup and verifyManifests read the same):

--- FAIL: TestCompleteBackup_SecondCompletionOfTheSameNumberIsRefused
    Error: An error is expected but got nil.
    Messages: a second backup completed with backupNum 2 while the chain already held it;
      manifest now [full#1@100 incremental#2@200 incremental#2@210];
      restore of the latest series: found a manifest with backup number 2 but expected 3

A narrow fix we applied in our fork

In CompleteBackup, after GetManifestNoUpgrade, we refuse to append when the series has moved on:

  • an incremental must be latest.BackupNum + 1 and in the same BackupId as the latest manifest;
  • a full backup must not reuse an existing BackupId.

The backup task then reports Failed, and the series stays restorable. The test above passes with the check and fails again without it.

This narrows the race but does not close it: the window of the manifest.json read-modify-write itself remains, and the rename is not atomic on S3. A real fix would need a cluster-wide lock, for example one held through Zero, or routing every backup to a single coordinator (as export routes to the group leader). We tell our operators to send backups through one Alpha at a time.

We can open a PR with the narrow check and the test if that would help.

Lingua principale
Go
Stelle
21.8k
Fork
1.6k
Merge medio
11g 7h
PR unite (30g)
5

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di dgraph-io/dgraph

Tutte le issue di dgraph-io/dgraph

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.