mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 18:47:20 +01:00
* util/http, operation, topology: give the DELETE helpers a deadline Delete and DeleteProxied made their request without a context and read the whole answer. Only the shared client's 30 s response header timeout bounded them, and nothing bounded an answer whose body never ends. - The cleanup after a failed chunk upload (deleteChunkFromHolders, used by PutObject and every chunked upload) asks the holder that just failed the write, often the one that stalls. A holder that did not answer held the upload for 30 s per holder and attempt, with the chunk's buffer. One that sent its headers and stalled held it for good, and the holders after it were never asked. Each delete is now bounded by cleanupDeleteTimeout (10 s), as the copy path's cleanup is by copyCleanupTimeout (#13297). A delete that runs out is logged and leaves the needle, as a failed delete did before. - A delete forwarded to a replica that does not answer held the primary's DELETE for 30 s, while a forwarded write fails at replicaRequestTimeout. The delete forward now has the same bound, as the Rust volume server's replica_request already gives deletes. Its timeout is counted as a timeout, through replicaDeadlineError, which the write forward uses as well. Delete and DeleteProxied take a context. benchmark and the unmaintained repeated_vacuum pass context.Background(). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * topology: let a replica's failure leave the other replica deletes running DistributedOperation cancels its context when the first replica fails, and the delete deadline was derived from it, so a sibling's failure could cut the deletes to the other replicas short. The old context-free Delete kept them going, and they must: the needle is gone on this server already, so a replica that is not asked keeps its copy. Each delete now takes its deadline from context.Background(). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * topology: read the replica delete bound once, before the fan-out A replica delete that outlives ReplicatedDelete read the package bound again after its answer, racing a later caller that changes it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
857 lines
30 KiB
Go
857 lines
30 KiB
Go
package topology
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/prometheus/client_golang/prometheus/testutil"
|
|
dto "github.com/prometheus/client_model/go"
|
|
"github.com/seaweedfs/seaweedfs/weed/operation"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/security"
|
|
"github.com/seaweedfs/seaweedfs/weed/stats"
|
|
"github.com/seaweedfs/seaweedfs/weed/storage"
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/types"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
)
|
|
|
|
// TestDistributedOperationCancelsSiblingsOnFirstError verifies that once one
|
|
// replica fails, an outstanding replica still stalled in a dial timeout is
|
|
// cancelled rather than gating the caller until it times out.
|
|
func TestDistributedOperationCancelsSiblingsOnFirstError(t *testing.T) {
|
|
locations := []operation.Location{{Url: "fast"}, {Url: "slow"}}
|
|
cancelled := make(chan struct{}, 1)
|
|
|
|
start := time.Now()
|
|
err := DistributedOperation(context.Background(), locations, func(ctx context.Context, location operation.Location) error {
|
|
if location.Url == "fast" {
|
|
return errors.New("connection refused")
|
|
}
|
|
// slow: a replica stalled in a dial timeout
|
|
select {
|
|
case <-ctx.Done():
|
|
cancelled <- struct{}{}
|
|
return ctx.Err()
|
|
case <-time.After(10 * time.Second):
|
|
return nil
|
|
}
|
|
})
|
|
|
|
if err == nil {
|
|
t.Fatal("expected an error from the fast-failing replica")
|
|
}
|
|
if elapsed := time.Since(start); elapsed > 2*time.Second {
|
|
t.Fatalf("did not fail fast: took %v", elapsed)
|
|
}
|
|
select {
|
|
case <-cancelled:
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("slow replica was not cancelled after the first error")
|
|
}
|
|
}
|
|
|
|
func TestDistributedOperationEmpty(t *testing.T) {
|
|
err := DistributedOperation(context.Background(), nil, func(ctx context.Context, location operation.Location) error {
|
|
t.Fatal("op should not be called when there are no locations")
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("expected nil for no locations, got %v", err)
|
|
}
|
|
}
|
|
|
|
type mockMasterServer struct {
|
|
master_pb.UnimplementedSeaweedServer
|
|
mu sync.Mutex
|
|
calls int
|
|
locations []*master_pb.Location
|
|
}
|
|
|
|
func (m *mockMasterServer) LookupVolume(ctx context.Context, req *master_pb.LookupVolumeRequest) (*master_pb.LookupVolumeResponse, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
m.calls++
|
|
var vls []*master_pb.LookupVolumeResponse_VolumeIdLocation
|
|
for _, vid := range req.VolumeOrFileIds {
|
|
vl := &master_pb.LookupVolumeResponse_VolumeIdLocation{
|
|
VolumeOrFileId: vid,
|
|
Locations: m.locations,
|
|
}
|
|
if len(m.locations) == 0 {
|
|
vl.Error = fmt.Sprintf("volume id %s not found", vid)
|
|
}
|
|
vls = append(vls, vl)
|
|
}
|
|
return &master_pb.LookupVolumeResponse{VolumeIdLocations: vls}, nil
|
|
}
|
|
|
|
func startMockMasterServer(t *testing.T, master *mockMasterServer) (operation.GetMasterFn, grpc.DialOption) {
|
|
t.Helper()
|
|
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
grpcServer := grpc.NewServer()
|
|
master_pb.RegisterSeaweedServer(grpcServer, master)
|
|
serveErr := make(chan error, 1)
|
|
go func() { serveErr <- grpcServer.Serve(lis) }()
|
|
t.Cleanup(func() {
|
|
grpcServer.Stop()
|
|
if err := <-serveErr; err != nil && !errors.Is(err, grpc.ErrServerStopped) {
|
|
t.Errorf("mock master serve: %v", err)
|
|
}
|
|
})
|
|
|
|
grpcPort := lis.Addr().(*net.TCPAddr).Port
|
|
return func(_ context.Context) pb.ServerAddress {
|
|
return pb.NewServerAddressWithGrpcPort(fmt.Sprintf("127.0.0.1:%d", grpcPort), grpcPort)
|
|
}, grpc.WithTransportCredentials(insecure.NewCredentials())
|
|
}
|
|
|
|
func TestGetWritableRemoteReplicationsRefreshesReadOnlyReplicas(t *testing.T) {
|
|
master := &mockMasterServer{locations: []*master_pb.Location{
|
|
{Url: "127.0.0.1:8080"},
|
|
{Url: "127.0.0.2:8080"},
|
|
}}
|
|
masterFn, dialOption := startMockMasterServer(t, master)
|
|
store := &storage.Store{Ip: "127.0.0.1", Port: 8080}
|
|
volumeId := needle.VolumeId(1)
|
|
operation.InvalidateVolumeIdLocationCache(volumeId.String())
|
|
|
|
locations, err := GetWritableRemoteReplications(store, dialOption, volumeId, masterFn)
|
|
if err != nil {
|
|
t.Fatalf("first lookup: %v", err)
|
|
}
|
|
if len(locations) != 1 || locations[0].Url != "127.0.0.2:8080" {
|
|
t.Fatalf("first lookup locations = %v", locations)
|
|
}
|
|
|
|
master.mu.Lock()
|
|
master.locations = []*master_pb.Location{
|
|
{Url: "127.0.0.1:8080"},
|
|
{Url: "127.0.0.2:8080", ReadOnly: true, ReadOnlyCanDelete: true},
|
|
}
|
|
master.mu.Unlock()
|
|
|
|
locations, err = GetWritableRemoteReplications(store, dialOption, volumeId, masterFn)
|
|
if err != nil {
|
|
t.Fatalf("second lookup: %v", err)
|
|
}
|
|
if len(locations) != 0 {
|
|
t.Fatalf("read-only replica remained a write target: %v", locations)
|
|
}
|
|
master.mu.Lock()
|
|
calls := master.calls
|
|
master.mu.Unlock()
|
|
if calls != 2 {
|
|
t.Fatalf("master lookup calls = %d, want 2", calls)
|
|
}
|
|
|
|
locations, err = GetRemoteReplications(store, dialOption, volumeId, masterFn)
|
|
if err != nil {
|
|
t.Fatalf("delete lookup: %v", err)
|
|
}
|
|
if len(locations) != 1 || !locations[0].ReadOnly || !locations[0].ReadOnlyCanDelete {
|
|
t.Fatalf("read-only-can-delete target was dropped: %v", locations)
|
|
}
|
|
}
|
|
|
|
// TestReplicatedWriteForwardsFsyncToReplicas verifies that the fsync=true
|
|
// request parameter is forwarded to replica volume servers in the fan-out
|
|
// request, so a durable write means every replica has flushed to disk.
|
|
func TestReplicatedWriteForwardsFsyncToReplicas(t *testing.T) {
|
|
replicaQueries := make(chan url.Values, 4)
|
|
replica := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
replicaQueries <- r.URL.Query()
|
|
w.WriteHeader(http.StatusCreated)
|
|
if _, err := w.Write([]byte(`{"size":1}`)); err != nil {
|
|
t.Errorf("replica write response: %v", err)
|
|
}
|
|
}))
|
|
defer replica.Close()
|
|
replicaHost := strings.TrimPrefix(replica.URL, "http://")
|
|
|
|
masterFn, dialOption := startMockMasterServer(t, &mockMasterServer{
|
|
locations: []*master_pb.Location{{Url: replicaHost}},
|
|
})
|
|
|
|
store := &storage.Store{}
|
|
volumeId := needle.VolumeId(1)
|
|
|
|
for _, tc := range []struct {
|
|
name string
|
|
fsync string
|
|
wantFsync bool
|
|
}{
|
|
{name: "fsync requested", fsync: "true", wantFsync: true},
|
|
{name: "no fsync requested", fsync: "", wantFsync: false},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
operation.InvalidateVolumeIdLocationCache(volumeId.String())
|
|
|
|
path := "http://127.0.0.1:8080/1,01637037d6"
|
|
if tc.fsync != "" {
|
|
path += "?fsync=" + tc.fsync
|
|
}
|
|
r := httptest.NewRequest(http.MethodPost, path, nil)
|
|
if err := r.ParseForm(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
n := &needle.Needle{
|
|
Id: 1,
|
|
Data: []byte("test data"),
|
|
Ttl: needle.EMPTY_TTL,
|
|
}
|
|
if _, err := ReplicatedWrite(context.Background(), masterFn, dialOption, store, volumeId, n, r, nil, "", ""); err != nil {
|
|
t.Fatalf("ReplicatedWrite: %v", err)
|
|
}
|
|
|
|
select {
|
|
case q := <-replicaQueries:
|
|
got := q.Get("fsync")
|
|
if tc.wantFsync && got != "true" {
|
|
t.Errorf("expected fsync=true in replica query, got %q (query: %v)", got, q)
|
|
}
|
|
if !tc.wantFsync && got != "" {
|
|
t.Errorf("expected no fsync in replica query, got %q (query: %v)", got, q)
|
|
}
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("replica never received the fan-out request")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestReplicatedWriteForwardsReplicaJwt verifies that each forward carries a
|
|
// JWT minted after the master lookup, right before the replica request, not
|
|
// the one on the incoming request.
|
|
func TestReplicatedWriteForwardsReplicaJwt(t *testing.T) {
|
|
replicaAuth := make(chan string, 1)
|
|
replica := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
replicaAuth <- r.Header.Get("Authorization")
|
|
w.WriteHeader(http.StatusCreated)
|
|
if _, err := w.Write([]byte(`{"size":1}`)); err != nil {
|
|
t.Errorf("replica write response: %v", err)
|
|
}
|
|
}))
|
|
defer replica.Close()
|
|
|
|
master := &mockMasterServer{
|
|
locations: []*master_pb.Location{{Url: strings.TrimPrefix(replica.URL, "http://")}},
|
|
}
|
|
masterFn, dialOption := startMockMasterServer(t, master)
|
|
volumeId := needle.VolumeId(1)
|
|
operation.InvalidateVolumeIdLocationCache(volumeId.String())
|
|
|
|
r := httptest.NewRequest(http.MethodPost, "http://127.0.0.1:8080/1,01637037d6", nil)
|
|
r.Header.Set("Authorization", "Bearer incoming-jwt")
|
|
if err := r.ParseForm(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var mints, lookupsAtMint int
|
|
replicaJwt := func() security.EncodedJwt {
|
|
master.mu.Lock()
|
|
lookupsAtMint = master.calls
|
|
master.mu.Unlock()
|
|
mints++
|
|
return "replica-jwt"
|
|
}
|
|
n := &needle.Needle{Id: 1, Data: []byte("test data"), Ttl: needle.EMPTY_TTL}
|
|
if _, err := ReplicatedWrite(context.Background(), masterFn, dialOption, &storage.Store{}, volumeId, n, r, replicaJwt, "", ""); err != nil {
|
|
t.Fatalf("ReplicatedWrite: %v", err)
|
|
}
|
|
|
|
select {
|
|
case got := <-replicaAuth:
|
|
if got != "Bearer replica-jwt" {
|
|
t.Errorf("replica Authorization = %q, want the replica JWT", got)
|
|
}
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("replica never received the fan-out request")
|
|
}
|
|
if mints != 1 {
|
|
t.Errorf("replica JWT minted %d times, want once per replica", mints)
|
|
}
|
|
if lookupsAtMint == 0 {
|
|
t.Error("replica JWT minted before the master lookup")
|
|
}
|
|
}
|
|
|
|
// newReplicateTestStore builds a single-disk store in dir, draining the notify
|
|
// channels so volume mounts never block on a heartbeat reader.
|
|
func newReplicateTestStore(t *testing.T, dir string) *storage.Store {
|
|
t.Helper()
|
|
store := storage.NewStore(nil, "127.0.0.1", 8080, 18080, "http://127.0.0.1:8080", "store-id",
|
|
[]string{dir}, []int32{100}, []util.MinFreeSpace{{}}, "",
|
|
storage.NeedleMapInMemory, []types.DiskType{types.HardDriveType}, nil, 3,
|
|
stats.DefaultDiskIOProbeConfig())
|
|
done := make(chan struct{})
|
|
go func() {
|
|
for {
|
|
select {
|
|
case <-store.NewVolumesChan:
|
|
case <-store.DeletedVolumesChan:
|
|
case <-store.NewEcShardsChan:
|
|
case <-store.DeletedEcShardsChan:
|
|
case <-store.StateUpdateChan:
|
|
case <-done:
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
t.Cleanup(func() {
|
|
close(done)
|
|
store.Close()
|
|
})
|
|
return store
|
|
}
|
|
|
|
// TestReplicatedWriteOverlapsLocalAndReplicaWrites runs the write path with a
|
|
// real local volume and a replica that stalls mid-request: the fan-out starts
|
|
// while the local write is still free to run, the local write lands before the
|
|
// replica unblocks, and the acknowledged write waits for both legs.
|
|
func TestReplicatedWriteOverlapsLocalAndReplicaWrites(t *testing.T) {
|
|
replicaStarted := make(chan url.Values, 1)
|
|
releaseReplica := make(chan struct{})
|
|
var releaseOnce sync.Once
|
|
release := func() { releaseOnce.Do(func() { close(releaseReplica) }) }
|
|
replica := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
replicaStarted <- r.URL.Query()
|
|
<-releaseReplica
|
|
w.WriteHeader(http.StatusCreated)
|
|
if _, err := w.Write([]byte(`{"size":21}`)); err != nil {
|
|
t.Errorf("replica write response: %v", err)
|
|
}
|
|
}))
|
|
defer replica.Close()
|
|
defer release()
|
|
replicaHost := strings.TrimPrefix(replica.URL, "http://")
|
|
|
|
masterFn, dialOption := startMockMasterServer(t, &mockMasterServer{
|
|
locations: []*master_pb.Location{{Url: "127.0.0.1:8080"}, {Url: replicaHost}},
|
|
})
|
|
|
|
store := newReplicateTestStore(t, t.TempDir())
|
|
volumeId := needle.VolumeId(7)
|
|
operation.InvalidateVolumeIdLocationCache(volumeId.String())
|
|
if err := store.AddVolume(volumeId, "", storage.NeedleMapInMemory, "001", "15d", 0, needle.GetCurrentVersion(), 0, types.HardDriveType, 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
data := []byte("overlapped write data")
|
|
n := &needle.Needle{
|
|
Id: 1,
|
|
Data: data,
|
|
Checksum: needle.NewCRC(data),
|
|
Ttl: needle.EMPTY_TTL,
|
|
}
|
|
r := httptest.NewRequest(http.MethodPost, "http://127.0.0.1:8080/7,01637037d6", nil)
|
|
if err := r.ParseForm(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
done := make(chan error, 1)
|
|
go func() {
|
|
_, err := ReplicatedWrite(context.Background(), masterFn, dialOption, store, volumeId, n, r, nil, "", "")
|
|
done <- err
|
|
}()
|
|
|
|
var q url.Values
|
|
select {
|
|
case q = <-replicaStarted:
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("replica never received the fan-out request")
|
|
}
|
|
if got := q.Get("type"); got != "replicate" {
|
|
t.Errorf("replica type = %q, want replicate", got)
|
|
}
|
|
if got := q.Get("ttl"); got != "15d" {
|
|
t.Errorf("replica ttl = %q, want the inherited volume ttl 15d", got)
|
|
}
|
|
|
|
select {
|
|
case err := <-done:
|
|
t.Fatalf("write returned before the replica responded: %v", err)
|
|
case <-time.After(50 * time.Millisecond):
|
|
}
|
|
|
|
deadline := time.Now().Add(5 * time.Second)
|
|
for {
|
|
readBack := &needle.Needle{Id: n.Id}
|
|
if _, err := store.ReadVolumeNeedle(volumeId, readBack, nil, nil); err == nil && string(readBack.Data) == string(data) {
|
|
break
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatal("local write did not land while the replica was still blocked")
|
|
}
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
|
|
release()
|
|
if err := <-done; err != nil {
|
|
t.Fatalf("ReplicatedWrite: %v", err)
|
|
}
|
|
}
|
|
|
|
// TestReplicatedWriteFailsWhenEitherLegFails checks that a failed local write
|
|
// or a failed replica fan-out fails the whole request even when the other leg
|
|
// succeeded.
|
|
func TestReplicatedWriteFailsWhenEitherLegFails(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
volumeId needle.VolumeId
|
|
readonlyLocal bool
|
|
replicaStatus int
|
|
}{
|
|
{name: "local write fails", volumeId: 8, readonlyLocal: true, replicaStatus: http.StatusCreated},
|
|
{name: "replica write fails", volumeId: 9, replicaStatus: http.StatusInternalServerError},
|
|
{name: "replica read only", volumeId: 10, replicaStatus: http.StatusConflict},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
replica := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(tc.replicaStatus)
|
|
}))
|
|
defer replica.Close()
|
|
replicaHost := strings.TrimPrefix(replica.URL, "http://")
|
|
|
|
masterFn, dialOption := startMockMasterServer(t, &mockMasterServer{
|
|
locations: []*master_pb.Location{{Url: "127.0.0.1:8080"}, {Url: replicaHost}},
|
|
})
|
|
|
|
store := newReplicateTestStore(t, t.TempDir())
|
|
operation.InvalidateVolumeIdLocationCache(tc.volumeId.String())
|
|
if err := store.AddVolume(tc.volumeId, "", storage.NeedleMapInMemory, "001", "", 0, needle.GetCurrentVersion(), 0, types.HardDriveType, 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if tc.readonlyLocal {
|
|
if err := store.MarkVolumeReadonly(tc.volumeId, false, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
data := []byte("write data")
|
|
n := &needle.Needle{
|
|
Id: 1,
|
|
Data: data,
|
|
Checksum: needle.NewCRC(data),
|
|
Ttl: needle.EMPTY_TTL,
|
|
}
|
|
r := httptest.NewRequest(http.MethodPost, fmt.Sprintf("http://127.0.0.1:8080/%d,01637037d6", tc.volumeId), nil)
|
|
if err := r.ParseForm(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
_, err := ReplicatedWrite(context.Background(), masterFn, dialOption, store, tc.volumeId, n, r, nil, "", "")
|
|
if err == nil {
|
|
t.Fatal("ReplicatedWrite succeeded despite a failed leg")
|
|
}
|
|
if errors.Is(err, ErrorWriteRefused) {
|
|
t.Fatalf("replicated write incorrectly proves no append: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestReplicatedWriteRejectsWriteWithNoTarget verifies that a write this server
|
|
// cannot store and has no replica to forward to fails instead of being
|
|
// acknowledged with nothing written.
|
|
func TestReplicatedWriteRejectsWriteWithNoTarget(t *testing.T) {
|
|
master := &mockMasterServer{}
|
|
masterFn, dialOption := startMockMasterServer(t, master)
|
|
store := &storage.Store{Ip: "127.0.0.1", Port: 8080}
|
|
volumeId := needle.VolumeId(31234)
|
|
|
|
for _, tc := range []struct {
|
|
name string
|
|
query string
|
|
locations []*master_pb.Location
|
|
}{
|
|
{name: "volume unknown to the master"},
|
|
{name: "master lists only this server", locations: []*master_pb.Location{{Url: "127.0.0.1:8080"}}},
|
|
{name: "replica write for a volume not on this server", query: "?type=replicate"},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
master.mu.Lock()
|
|
master.locations = tc.locations
|
|
master.mu.Unlock()
|
|
|
|
r := httptest.NewRequest(http.MethodPost, "http://127.0.0.1:8080/31234,01637037d6"+tc.query, nil)
|
|
if err := r.ParseForm(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
n := &needle.Needle{
|
|
Id: 1,
|
|
Data: []byte("test data"),
|
|
Ttl: needle.EMPTY_TTL,
|
|
}
|
|
_, err := ReplicatedWrite(context.Background(), masterFn, dialOption, store, volumeId, n, r, nil, "", "")
|
|
if err == nil {
|
|
t.Fatal("ReplicatedWrite acknowledged a write that no volume stored")
|
|
}
|
|
if !strings.Contains(err.Error(), "not found") {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestReplicatedWriteFailsAtTheBoundWhenAReplicaNeverAnswers: a replica that
|
|
// takes the forward and never answers fails the write at the forward bound,
|
|
// not at the transport's 30 s response header timeout.
|
|
func TestReplicatedWriteFailsAtTheBoundWhenAReplicaNeverAnswers(t *testing.T) {
|
|
received := make(chan struct{}, 1)
|
|
release := make(chan struct{})
|
|
replica := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
received <- struct{}{}
|
|
<-release
|
|
}))
|
|
defer replica.Close()
|
|
defer close(release)
|
|
|
|
defer func(d time.Duration) { replicaRequestTimeout = d }(replicaRequestTimeout)
|
|
replicaRequestTimeout = 300 * time.Millisecond
|
|
|
|
masterFn, dialOption := startMockMasterServer(t, &mockMasterServer{
|
|
locations: []*master_pb.Location{{Url: strings.TrimPrefix(replica.URL, "http://")}},
|
|
})
|
|
volumeId := needle.VolumeId(1)
|
|
operation.InvalidateVolumeIdLocationCache(volumeId.String())
|
|
r := httptest.NewRequest(http.MethodPost, "http://127.0.0.1:8080/1,01637037d6", nil)
|
|
if err := r.ParseForm(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
n := &needle.Needle{Id: 1, Data: []byte("test data"), Ttl: needle.EMPTY_TTL}
|
|
|
|
timeouts := stats.VolumeServerReplicationFailures.WithLabelValues(stats.ReplicationOpWrite, stats.FailureTimeout)
|
|
timeoutsBefore := testutil.ToFloat64(timeouts)
|
|
start := time.Now()
|
|
_, err := ReplicatedWrite(context.Background(), masterFn, dialOption, &storage.Store{}, volumeId, n, r, nil, "", "")
|
|
elapsed := time.Since(start)
|
|
select {
|
|
case <-received:
|
|
default:
|
|
t.Fatal("the replica never received the forward")
|
|
}
|
|
if err == nil {
|
|
t.Fatal("a replica that never answers cannot ack the write")
|
|
}
|
|
if elapsed < replicaRequestTimeout || elapsed > 5*time.Second {
|
|
t.Fatalf("write failed after %v, want about the %v forward bound: %v", elapsed, replicaRequestTimeout, err)
|
|
}
|
|
if got := testutil.ToFloat64(timeouts) - timeoutsBefore; got != 1 {
|
|
t.Fatalf("replication timeout failures went up by %v, want 1: %v", got, err)
|
|
}
|
|
}
|
|
|
|
// TestReplicatedDeleteFailsAtTheBoundWhenAReplicaNeverAnswers: a replica that
|
|
// takes the forwarded delete and never answers fails the delete at the
|
|
// forward bound, as a write does, not at the transport's 30 s response header
|
|
// timeout.
|
|
func TestReplicatedDeleteFailsAtTheBoundWhenAReplicaNeverAnswers(t *testing.T) {
|
|
received := make(chan struct{}, 1)
|
|
release := make(chan struct{})
|
|
replica := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
received <- struct{}{}
|
|
<-release
|
|
}))
|
|
defer replica.Close()
|
|
defer close(release)
|
|
|
|
defer func(d time.Duration) { replicaRequestTimeout = d }(replicaRequestTimeout)
|
|
replicaRequestTimeout = 300 * time.Millisecond
|
|
|
|
masterFn, dialOption := startMockMasterServer(t, &mockMasterServer{
|
|
locations: []*master_pb.Location{{Url: strings.TrimPrefix(replica.URL, "http://")}},
|
|
})
|
|
volumeId := needle.VolumeId(1)
|
|
operation.InvalidateVolumeIdLocationCache(volumeId.String())
|
|
r := httptest.NewRequest(http.MethodDelete, "http://127.0.0.1:8080/1,01637037d6", nil)
|
|
if err := r.ParseForm(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
n := &needle.Needle{Id: 1}
|
|
|
|
timeouts := stats.VolumeServerReplicationFailures.WithLabelValues(stats.ReplicationOpDelete, stats.FailureTimeout)
|
|
timeoutsBefore := testutil.ToFloat64(timeouts)
|
|
start := time.Now()
|
|
_, err := ReplicatedDelete(masterFn, dialOption, &storage.Store{}, volumeId, n, r, true)
|
|
elapsed := time.Since(start)
|
|
select {
|
|
case <-received:
|
|
default:
|
|
t.Fatal("the replica never received the forwarded delete")
|
|
}
|
|
if err == nil {
|
|
t.Fatal("a replica that never answers cannot ack the delete")
|
|
}
|
|
if elapsed < replicaRequestTimeout || elapsed > 5*time.Second {
|
|
t.Fatalf("delete failed after %v, want about the %v forward bound: %v", elapsed, replicaRequestTimeout, err)
|
|
}
|
|
if got := testutil.ToFloat64(timeouts) - timeoutsBefore; got != 1 {
|
|
t.Fatalf("replication timeout failures went up by %v, want 1: %v", got, err)
|
|
}
|
|
}
|
|
|
|
// TestReplicatedDeleteStillReachesAReplicaAfterAnotherFails: the needle is
|
|
// gone here already, so when one replica fails the delete, the delete to
|
|
// another replica goes on instead of being cancelled with it.
|
|
func TestReplicatedDeleteStillReachesAReplicaAfterAnotherFails(t *testing.T) {
|
|
failing := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
http.Error(w, "disk failure", http.StatusInternalServerError)
|
|
}))
|
|
defer failing.Close()
|
|
received := make(chan struct{}, 1)
|
|
proceed := make(chan struct{})
|
|
release := sync.OnceFunc(func() { close(proceed) })
|
|
gaveUp := make(chan error, 1)
|
|
other := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
received <- struct{}{}
|
|
<-proceed
|
|
// A forwarder that gives up on the delete closes its connection.
|
|
select {
|
|
case <-r.Context().Done():
|
|
gaveUp <- r.Context().Err()
|
|
case <-time.After(200 * time.Millisecond):
|
|
gaveUp <- nil
|
|
}
|
|
w.WriteHeader(http.StatusAccepted)
|
|
}))
|
|
defer other.Close()
|
|
defer release()
|
|
|
|
masterFn, dialOption := startMockMasterServer(t, &mockMasterServer{
|
|
locations: []*master_pb.Location{
|
|
{Url: strings.TrimPrefix(failing.URL, "http://")},
|
|
{Url: strings.TrimPrefix(other.URL, "http://")},
|
|
},
|
|
})
|
|
volumeId := needle.VolumeId(1)
|
|
operation.InvalidateVolumeIdLocationCache(volumeId.String())
|
|
r := httptest.NewRequest(http.MethodDelete, "http://127.0.0.1:8080/1,01637037d6", nil)
|
|
if err := r.ParseForm(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if _, err := ReplicatedDelete(masterFn, dialOption, &storage.Store{}, volumeId, &needle.Needle{Id: 1}, r, true); err == nil {
|
|
t.Fatal("a replica that failed the delete cannot ack it")
|
|
}
|
|
select {
|
|
case <-received:
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("the other replica never got the delete")
|
|
}
|
|
release()
|
|
if err := <-gaveUp; err != nil {
|
|
t.Fatalf("the delete to the other replica was given up when the first replica failed: %v", err)
|
|
}
|
|
}
|
|
|
|
// TestReplicatedWriteGivesALargerBodyMoreTime: a replica that answers after
|
|
// the bodiless bound, but within the bound for its body, still acks the write.
|
|
func TestReplicatedWriteGivesALargerBodyMoreTime(t *testing.T) {
|
|
defer func(d time.Duration) { replicaRequestTimeout = d }(replicaRequestTimeout)
|
|
replicaRequestTimeout = 300 * time.Millisecond
|
|
data := make([]byte, replicaMinBytesPerSec)
|
|
answerAfter := (replicaRequestTimeout + replicaRequestBound(len(data))) / 2
|
|
|
|
received := make(chan int, 1)
|
|
replica := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
mr, err := r.MultipartReader()
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
part, err := mr.NextPart()
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
body, _ := io.ReadAll(part)
|
|
received <- len(body)
|
|
time.Sleep(answerAfter)
|
|
w.WriteHeader(http.StatusCreated)
|
|
fmt.Fprint(w, "{}")
|
|
}))
|
|
defer replica.Close()
|
|
|
|
masterFn, dialOption := startMockMasterServer(t, &mockMasterServer{
|
|
locations: []*master_pb.Location{{Url: strings.TrimPrefix(replica.URL, "http://")}},
|
|
})
|
|
volumeId := needle.VolumeId(1)
|
|
operation.InvalidateVolumeIdLocationCache(volumeId.String())
|
|
r := httptest.NewRequest(http.MethodPost, "http://127.0.0.1:8080/1,01637037d6", nil)
|
|
if err := r.ParseForm(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
n := &needle.Needle{Id: 1, Data: data, Ttl: needle.EMPTY_TTL}
|
|
|
|
if _, err := ReplicatedWrite(context.Background(), masterFn, dialOption, &storage.Store{}, volumeId, n, r, nil, "", ""); err != nil {
|
|
t.Fatalf("a replica answering within the %v bound for a %d-byte body failed the write: %v", replicaRequestBound(len(data)), len(data), err)
|
|
}
|
|
select {
|
|
case got := <-received:
|
|
if got != len(data) {
|
|
t.Fatalf("the replica received %d bytes, want %d", got, len(data))
|
|
}
|
|
default:
|
|
t.Fatal("the replica never received the forward")
|
|
}
|
|
}
|
|
|
|
func TestReplicaRequestBoundGrowsWithTheBody(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
size int
|
|
want time.Duration
|
|
}{
|
|
{0, replicaRequestTimeout},
|
|
{replicaMinBytesPerSec / 2, replicaRequestTimeout + 500*time.Millisecond},
|
|
{256 * replicaMinBytesPerSec, replicaRequestTimeout + 256*time.Second},
|
|
} {
|
|
if got := replicaRequestBound(tc.size); got != tc.want {
|
|
t.Errorf("replicaRequestBound(%d) = %v, want %v", tc.size, got, tc.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestReplicatedWriteRefusesBeforeFanOutWhenLocalVolumeIsReadOnly: a local
|
|
// volume that already refuses writes must fail the request before any replica
|
|
// is contacted. The fan-out runs concurrently with the local write, so without
|
|
// the up-front check the replicas stored a needle that nothing references.
|
|
func TestReplicatedWriteRefusesBeforeFanOutWhenLocalVolumeIsReadOnly(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
volumeId needle.VolumeId
|
|
canDelete bool
|
|
replicate bool
|
|
withReplicas bool
|
|
fsync bool
|
|
wantProof bool
|
|
}{
|
|
{name: "no write or delete", volumeId: 21, withReplicas: true},
|
|
{name: "no write, can delete", volumeId: 22, canDelete: true, withReplicas: true},
|
|
{name: "fsync", volumeId: 23, withReplicas: true, fsync: true},
|
|
{name: "no replica keeps the refusal proof", volumeId: 24, wantProof: true},
|
|
{name: "replicate request is not pre-checked", volumeId: 25, replicate: true},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
var replicaWrites atomic.Int32
|
|
replica := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
replicaWrites.Add(1)
|
|
w.WriteHeader(http.StatusCreated)
|
|
_, _ = w.Write([]byte(`{"size":1}`))
|
|
}))
|
|
defer replica.Close()
|
|
|
|
locations := []*master_pb.Location{{Url: "127.0.0.1:8080"}}
|
|
replication := "000"
|
|
if tc.withReplicas {
|
|
replication = "001"
|
|
locations = append(locations, &master_pb.Location{Url: strings.TrimPrefix(replica.URL, "http://")})
|
|
}
|
|
masterFn, dialOption := startMockMasterServer(t, &mockMasterServer{locations: locations})
|
|
|
|
store := newReplicateTestStore(t, t.TempDir())
|
|
operation.InvalidateVolumeIdLocationCache(tc.volumeId.String())
|
|
if err := store.AddVolume(tc.volumeId, "", storage.NeedleMapInMemory, replication, "", 0, needle.GetCurrentVersion(), 0, types.HardDriveType, 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.MarkVolumeReadonly(tc.volumeId, tc.canDelete, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
data := []byte("write data")
|
|
n := &needle.Needle{Id: 1, Data: data, Checksum: needle.NewCRC(data), Ttl: needle.EMPTY_TTL}
|
|
query := ""
|
|
if tc.replicate {
|
|
query = "?type=replicate"
|
|
} else if tc.fsync {
|
|
query = "?fsync=true"
|
|
}
|
|
r := httptest.NewRequest(http.MethodPost, fmt.Sprintf("http://127.0.0.1:8080/%d,01637037d6%s", tc.volumeId, query), nil)
|
|
if err := r.ParseForm(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
observed := localWriteObservations(t)
|
|
_, err := ReplicatedWrite(context.Background(), masterFn, dialOption, store, tc.volumeId, n, r, nil, "", "")
|
|
if !errors.Is(err, storage.ErrorReadOnly) {
|
|
t.Fatalf("ReplicatedWrite = %v, want a read-only error", err)
|
|
}
|
|
// Refused up front or by the write, a refusal is observed once.
|
|
if got := localWriteObservations(t) - observed; got != 1 {
|
|
t.Errorf("local write histogram observed %d writes, want 1", got)
|
|
}
|
|
if want := fmt.Sprintf("volume %d is read only", tc.volumeId); !strings.Contains(err.Error(), "failed to write to local disk: ") || !strings.Contains(err.Error(), want) {
|
|
t.Errorf("error = %q, want the local write failure for %q", err, want)
|
|
}
|
|
if got := errors.Is(err, ErrorWriteRefused); got != tc.wantProof {
|
|
t.Errorf("errors.Is(err, ErrorWriteRefused) = %v, want %v: %v", got, tc.wantProof, err)
|
|
}
|
|
if got := replicaWrites.Load(); got != 0 {
|
|
t.Errorf("replica received %d writes for a write the local volume refused", got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// localWriteObservations is the number of local disk writes the request
|
|
// histogram has observed.
|
|
func localWriteObservations(t *testing.T) uint64 {
|
|
t.Helper()
|
|
var m dto.Metric
|
|
if err := stats.VolumeServerRequestHistogram.WithLabelValues(stats.WriteToLocalDisk).(prometheus.Histogram).Write(&m); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return m.GetHistogram().GetSampleCount()
|
|
}
|
|
|
|
// TestReplicatedWriteStillFansOutToWritableLocalVolume pins the other side of
|
|
// the up-front check: a writable volume is written and forwarded as before.
|
|
func TestReplicatedWriteStillFansOutToWritableLocalVolume(t *testing.T) {
|
|
var replicaWrites atomic.Int32
|
|
replica := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
replicaWrites.Add(1)
|
|
w.WriteHeader(http.StatusCreated)
|
|
_, _ = w.Write([]byte(`{"size":1}`))
|
|
}))
|
|
defer replica.Close()
|
|
masterFn, dialOption := startMockMasterServer(t, &mockMasterServer{
|
|
locations: []*master_pb.Location{{Url: "127.0.0.1:8080"}, {Url: strings.TrimPrefix(replica.URL, "http://")}},
|
|
})
|
|
store := newReplicateTestStore(t, t.TempDir())
|
|
vid := needle.VolumeId(26)
|
|
operation.InvalidateVolumeIdLocationCache(vid.String())
|
|
if err := store.AddVolume(vid, "", storage.NeedleMapInMemory, "001", "", 0, needle.GetCurrentVersion(), 0, types.HardDriveType, 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
data := []byte("write data")
|
|
n := &needle.Needle{Id: 1, Data: data, Checksum: needle.NewCRC(data), Ttl: needle.EMPTY_TTL}
|
|
r := httptest.NewRequest(http.MethodPost, fmt.Sprintf("http://127.0.0.1:8080/%d,01637037d6", vid), nil)
|
|
if err := r.ParseForm(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := ReplicatedWrite(context.Background(), masterFn, dialOption, store, vid, n, r, nil, "", ""); err != nil {
|
|
t.Fatalf("ReplicatedWrite: %v", err)
|
|
}
|
|
if got := replicaWrites.Load(); got != 1 {
|
|
t.Errorf("replica received %d writes, want 1", got)
|
|
}
|
|
}
|