diff --git a/seaweed-block/core/frontend/chunkstore/layout_create.go b/seaweed-block/core/frontend/chunkstore/layout_create.go index bbfb88a12c..c0e5753ea6 100644 --- a/seaweed-block/core/frontend/chunkstore/layout_create.go +++ b/seaweed-block/core/frontend/chunkstore/layout_create.go @@ -16,38 +16,29 @@ var _ layout.TargetStore = (*LayoutStore)(nil) // CreateHead writes the volume's first HEAD (unclaimed) exclusively; // layout.ErrExists if a HEAD is already there. func (s *LayoutStore) CreateHead(ctx context.Context, h layout.Head, cause layout.HeadWriteCause) error { - started := time.Now() if cause != layout.CauseCreate { return fmt.Errorf("chunkstore: a first HEAD is written as %q, not %q", layout.CauseCreate, cause) } if h.State != layout.StateLive || h.Holder != "" { return errors.New("chunkstore: only an unclaimed live HEAD is created") } - err := s.createHead(ctx, h) - if err == nil { - s.logHeadWrite(ctx, 0, h, cause, started) - } - return err + return s.createHead(ctx, h, cause) } // CreateDeletedHead writes a deleted HEAD (a tombstone) exclusively; // layout.ErrExists if a HEAD is already there. func (s *LayoutStore) CreateDeletedHead(ctx context.Context, h layout.Head, cause layout.HeadWriteCause) error { - started := time.Now() if cause != layout.CauseTombstone { return fmt.Errorf("chunkstore: a tombstone HEAD is written as %q, not %q", layout.CauseTombstone, cause) } if h.State != layout.StateDeleted { return errors.New("chunkstore: only a deleted HEAD is created as a tombstone") } - err := s.createHead(ctx, h) - if err == nil { - s.logHeadWrite(ctx, 0, h, cause, started) - } - return err + return s.createHead(ctx, h, cause) } -func (s *LayoutStore) createHead(ctx context.Context, h layout.Head) error { +func (s *LayoutStore) createHead(ctx context.Context, h layout.Head, cause layout.HeadWriteCause) error { + started := time.Now() if h.VolumeID != s.id { return layout.ErrIdentity } @@ -58,6 +49,11 @@ func (s *LayoutStore) createHead(ctx context.Context, h layout.Head) error { e := layoutEntry(layout.HeadName, raw) e.Extended = map[string][]byte{layoutKind: []byte("head"), layoutGen: []byte(strconv.FormatUint(1, 10)), layoutVolume: []byte(s.id.String())} err = s.f.CreateEntry(ctx, s.root, e, true) + if err == nil { + s.logHeadWrite(ctx, 0, h, cause, started) + } else { + s.logUnknownHeadWrite(ctx, 0, h, cause, started, err) + } if errors.Is(err, ErrExists) { return layout.ErrExists } diff --git a/seaweed-block/core/frontend/chunkstore/layout_head_log.go b/seaweed-block/core/frontend/chunkstore/layout_head_log.go index d89b5b2a7b..4c4a0a0816 100644 --- a/seaweed-block/core/frontend/chunkstore/layout_head_log.go +++ b/seaweed-block/core/frontend/chunkstore/layout_head_log.go @@ -2,6 +2,7 @@ package chunkstore import ( "context" + "errors" "log" "path" "time" @@ -9,6 +10,14 @@ import ( "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/layout" ) +func (s *LayoutStore) logUnknownHeadWrite(ctx context.Context, gen uint64, h layout.Head, cause layout.HeadWriteCause, started time.Time, err error) { + if errors.Is(err, ErrPrecondition) || errors.Is(err, ErrExists) || errors.Is(err, ErrNotFound) { + return // A definite store refusal is not an unknown outcome. + } + log.Printf("layout: HEAD write level=WARN outcome=unknown volume=%q volume_id=%s expected_gen=%d head_write=%s session=%q took_ms=%.3f err=%q", + path.Base(s.root), h.VolumeID, gen, cause, layout.HeadWriteSession(ctx, gen, h), float64(time.Since(started))/float64(time.Millisecond), err.Error()) +} + // Log only acknowledged writes: an error or a lost answer is not proof of a write. func (s *LayoutStore) logHeadWrite(ctx context.Context, gen uint64, h layout.Head, cause layout.HeadWriteCause, started time.Time) { current := "" diff --git a/seaweed-block/core/frontend/chunkstore/layout_head_log_test.go b/seaweed-block/core/frontend/chunkstore/layout_head_log_test.go index 24584b9771..1b6a8964a5 100644 --- a/seaweed-block/core/frontend/chunkstore/layout_head_log_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout_head_log_test.go @@ -8,9 +8,77 @@ import ( "strings" "testing" + "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/filerpb" "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/layout" ) +type lostHeadReplyFiler struct { + Filer + applied bool + fault error +} + +func (f lostHeadReplyFiler) CreateEntry(ctx context.Context, dir string, e *filerpb.Entry, exclusive bool) error { + if f.applied { + if err := f.Filer.CreateEntry(ctx, dir, e, exclusive); err != nil { + return err + } + } + return f.fault +} + +func (f lostHeadReplyFiler) UpdateEntry(ctx context.Context, dir string, e *filerpb.Entry, expected map[string][]byte) error { + if f.applied { + if err := f.Filer.UpdateEntry(ctx, dir, e, expected); err != nil { + return err + } + } + return f.fault +} + +func TestHeadWriteLostAnswerWarnsWithoutClaimingSuccess(t *testing.T) { + for _, create := range []bool{true, false} { + for _, applied := range []bool{true, false} { + out := captureHeadWrites(t) + s, _ := causeStore(t) + ctx := context.Background() + h := layout.Unclaimed(s.id, nil) + if !create { + if err := s.CreateHead(ctx, h, layout.CauseCreate); err != nil { + t.Fatal(err) + } + } + out.Reset() + fault := errors.New("HEAD answer lost") + s.f = lostHeadReplyFiler{Filer: s.f, applied: applied, fault: fault} + var err error + if create { + err = s.CreateHead(ctx, h, layout.CauseCreate) + } else { + err = s.UpdateHead(ctx, 1, h, layout.CauseCommit) + } + if !errors.Is(err, fault) { + t.Fatalf("error changed: %v", err) + } + line := out.String() + if strings.Count(line, "outcome=unknown") != 1 || !strings.Contains(line, "level=WARN") || !strings.Contains(line, "expected_gen=") || !strings.Contains(line, "head_write=") || !strings.Contains(line, fault.Error()) || strings.Contains(line, "level=INFO") { + t.Fatalf("create=%t applied=%t: %q", create, applied, line) + } + got, found, err := s.ReadHead(ctx) + if err != nil || (create && found != applied) || (!create && got.Gen != 1+boolUint(applied)) { + t.Fatalf("stored outcome: found=%t gen=%d err=%v", found, got.Gen, err) + } + } + } +} + +func boolUint(v bool) uint64 { + if v { + return 1 + } + return 0 +} + func captureHeadWrites(t *testing.T) *bytes.Buffer { t.Helper() var out bytes.Buffer diff --git a/seaweed-block/core/frontend/chunkstore/layout_store.go b/seaweed-block/core/frontend/chunkstore/layout_store.go index c25629daba..0096941f9f 100644 --- a/seaweed-block/core/frontend/chunkstore/layout_store.go +++ b/seaweed-block/core/frontend/chunkstore/layout_store.go @@ -120,6 +120,8 @@ func (s *LayoutStore) UpdateHead(ctx context.Context, expectedGen uint64, next l } if err == nil { s.logHeadWrite(ctx, expectedGen, next, cause, started) + } else { + s.logUnknownHeadWrite(ctx, expectedGen, next, cause, started, err) } return layoutError(err) }