mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 02:57:37 +01:00
* filer: delete the chunks a failed copy wrote A copy through the filer (POST dst?cp.from=src) writes the destination's chunks first and its entry last. When it ended before the entry was written (the client gave up, a chunk failed to copy, the entry write failed), the chunks already on the volume servers stayed there, with no entry to reference them and nothing to delete them. The fan-out now waits for the copies it started, starts no further one after a failure or once the request has ended, and deletes what the completed ones wrote. A chunk copy that fails deletes the file id it was assigned, as a failed write can still have stored the needle. A failed entry write deletes only the chunks the destination does not hold, since CreateEntry can fail after storing the entry. #13217 did the same for the S3 copy paths. * filer: keep the copied chunks when the entry write fails CreateEntry can fail after it stored the entry: the entry stays when only its parent folders could not be created. Reading the destination afterwards does not settle whether the chunks are live, since an entry that was stored may have been renamed by then, and deleting on a miss would take its data. So a failed entry write keeps the chunks, as it did before. Only the failures that come before the entry write delete what the copy wrote. * filer: fail a copy whose request ended after its last chunk A request cancelled once every chunk copy had returned still handed the chunks on, to an entry write that the cancelled context then fails, and that write keeps them. The end of the request now fails the fan-out whether or not a copy was left to start, so the chunks are deleted there. --------- Co-authored-by: MapleWhiteZZZ <18653045+MapleWhiteZZZ@users.noreply.github.com>
127 lines
3.9 KiB
Go
127 lines
3.9 KiB
Go
package weed_server
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"sort"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
)
|
|
|
|
func copiedChunk(i int) *filer_pb.FileChunk {
|
|
return &filer_pb.FileChunk{FileId: fmt.Sprintf("7,%02x637037d6", i+1), Size: 4}
|
|
}
|
|
|
|
// A copy that fails has no entry to hold the chunks it already wrote, so they
|
|
// are deleted: the ones whose copy completed, and no other.
|
|
func TestCopyChunksInParallelDeletesWrittenChunksOnFailure(t *testing.T) {
|
|
fs := &FilerServer{filer: newRenameTestFiler(t, newRenameTestStore())}
|
|
|
|
const n, failing, cutShort = 3 * maxConcurrentChunkCopies, 5, 6
|
|
copyErr := errors.New("source returned status 404")
|
|
firstWritten := make(chan struct{})
|
|
var once sync.Once
|
|
var mu sync.Mutex
|
|
var written []string
|
|
|
|
newChunks, err := fs.copyChunksInParallel(context.Background(), n, func(ctx context.Context, i int) (*filer_pb.FileChunk, error) {
|
|
switch i {
|
|
case failing:
|
|
// Fails once another chunk is on the volume servers.
|
|
<-firstWritten
|
|
return nil, copyErr
|
|
case cutShort:
|
|
// A transfer the failure cancels stores nothing.
|
|
<-ctx.Done()
|
|
return nil, ctx.Err()
|
|
}
|
|
chunk := copiedChunk(i)
|
|
mu.Lock()
|
|
written = append(written, chunk.FileId)
|
|
mu.Unlock()
|
|
once.Do(func() { close(firstWritten) })
|
|
return chunk, nil
|
|
})
|
|
|
|
if !errors.Is(err, copyErr) {
|
|
t.Fatalf("error = %v, want the first copy failure", err)
|
|
}
|
|
if newChunks != nil {
|
|
t.Fatalf("a failed copy returned %d chunks", len(newChunks))
|
|
}
|
|
if len(written) == 0 || len(written) > n-2 {
|
|
t.Fatalf("%d chunks written, want between 1 and %d", len(written), n-2)
|
|
}
|
|
sort.Strings(written)
|
|
checkDeletions(t, fs.filer, written...)
|
|
}
|
|
|
|
func TestCopyChunksInParallelKeepsTheChunksOfACompleteCopy(t *testing.T) {
|
|
fs := &FilerServer{filer: newRenameTestFiler(t, newRenameTestStore())}
|
|
|
|
const n = 3 * maxConcurrentChunkCopies
|
|
newChunks, err := fs.copyChunksInParallel(context.Background(), n, func(ctx context.Context, i int) (*filer_pb.FileChunk, error) {
|
|
return copiedChunk(i), nil
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("copy: %v", err)
|
|
}
|
|
if len(newChunks) != n {
|
|
t.Fatalf("%d chunks returned, want %d", len(newChunks), n)
|
|
}
|
|
for i, chunk := range newChunks {
|
|
if want := copiedChunk(i).FileId; chunk.GetFileId() != want {
|
|
t.Fatalf("chunk %d is %s, want %s: the order of the source is lost", i, chunk.GetFileId(), want)
|
|
}
|
|
}
|
|
checkDeletions(t, fs.filer)
|
|
}
|
|
|
|
// The client going away ends the copy too. Every copy in flight may well
|
|
// complete: the ones never started must not pass for copied, and a copy whose
|
|
// last chunk completes after the client left has no entry coming either.
|
|
func TestCopyChunksInParallelDeletesWrittenChunksWhenTheRequestEnds(t *testing.T) {
|
|
for _, n := range []int{3 * maxConcurrentChunkCopies, maxConcurrentChunkCopies} {
|
|
t.Run(fmt.Sprintf("%d chunks", n), func(t *testing.T) {
|
|
fs := &FilerServer{filer: newRenameTestFiler(t, newRenameTestStore())}
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
var mu sync.Mutex
|
|
var inFlight int
|
|
var written []string
|
|
|
|
newChunks, err := fs.copyChunksInParallel(ctx, n, func(ctx context.Context, i int) (*filer_pb.FileChunk, error) {
|
|
mu.Lock()
|
|
inFlight++
|
|
if inFlight == maxConcurrentChunkCopies {
|
|
cancel()
|
|
}
|
|
mu.Unlock()
|
|
// The volume server stores the needle all the same.
|
|
<-ctx.Done()
|
|
chunk := copiedChunk(i)
|
|
mu.Lock()
|
|
written = append(written, chunk.FileId)
|
|
mu.Unlock()
|
|
return chunk, nil
|
|
})
|
|
|
|
if !errors.Is(err, context.Canceled) {
|
|
t.Fatalf("error = %v, want the request's cancellation", err)
|
|
}
|
|
if newChunks != nil {
|
|
t.Fatalf("a copy cut short returned %d chunks", len(newChunks))
|
|
}
|
|
if len(written) != maxConcurrentChunkCopies {
|
|
t.Fatalf("%d chunks written, want the %d in flight when the request ended", len(written), maxConcurrentChunkCopies)
|
|
}
|
|
sort.Strings(written)
|
|
checkDeletions(t, fs.filer, written...)
|
|
})
|
|
}
|
|
}
|