mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
Merge pull request #726 from seaweedfs/feat/head-write-logs
feat, chunkstore: log HEAD writes with census causes and sessions
This commit is contained in:
commit
afb9e86a11
10 files changed
+328
-3
No files matched your search
@@ -72,3 +72,6 @@ TestCutoverWriteSnapshotKillRestartRestore
|
||||
TestBlockCSI_BinaryStartsAndServesIdentity # package=blockcsi
|
||||
TestG15a_BlockCSIControllerPublishUsesMasterFrontendFact # package=blockcsi
|
||||
TestG15c_BlockCSICreateVolumeWritesMasterLifecycleIntent # package=blockcsi
|
||||
|
||||
# Typed delete stdout remains separate from HEAD diagnostics.
|
||||
TestDeleteNoRevokeKeepsTheTypedStdoutSeparate
|
||||
@@ -0,0 +1,45 @@
|
||||
//go:build linux && subprocess && chunkstore_integration
|
||||
|
||||
package main_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDeleteNoRevokeKeepsTheTypedStdoutSeparate(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name, stdout, stderr string
|
||||
code int
|
||||
completed bool
|
||||
}{
|
||||
{"completed-with-diagnostics", `{"volume":"v3-12345678","state":"deleted"}`, "layout: HEAD write level=INFO", 0, true},
|
||||
{"completed-retry-with-diagnostics", `{"volume":"v3-12345678","state":"deleted","already_deleted_roots":["old"]}`, "layout: HEAD write level=INFO", 0, true},
|
||||
{"malformed-success-is-not-completed", `not JSON`, "layout: HEAD write level=INFO", 0, false},
|
||||
{"refusal-retains-diagnostics", ``, "not_admitted: live until 2026-11-29T05:12:42Z", 1, false},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
cli := filepath.Join(dir, "answer-cli")
|
||||
script := fmt.Sprintf("#!/bin/sh\nprintf '%%s\\n' '%s'\nprintf '%%s\\n' '%s' >&2\nexit %d\n", tc.stdout, tc.stderr, tc.code)
|
||||
if err := os.WriteFile(cli, []byte(script), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
env := &handoffEnv{h: &plannedHandoffHarness{dir: dir, cli: cli, t: t}, cluster: &w4WeedCluster{}}
|
||||
out, code := env.deleteNoRevoke(context.Background(), "v3", "del-v3")
|
||||
if code != tc.code || handoffDeleteCompleted(out) != tc.completed {
|
||||
t.Fatalf("exit%d completed%v; want exit%d completed%v: %s", code, handoffDeleteCompleted(out), tc.code, tc.completed, out)
|
||||
}
|
||||
if code == 0 && strings.TrimSpace(out) != tc.stdout {
|
||||
t.Fatalf("successful typed answer includes diagnostics: %q", out)
|
||||
}
|
||||
if code != 0 && !strings.Contains(out, tc.stderr) {
|
||||
t.Fatalf("refusal lost its reason: %q", out)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -140,6 +140,11 @@ func (env *handoffEnv) deleteNoRevoke(ctx context.Context, v, request string) (s
|
||||
if err != nil {
|
||||
code = -1
|
||||
}
|
||||
// runCLI records both streams. Only stdout is the typed success answer;
|
||||
// stderr may contain acknowledged HEAD-write diagnostics on success too.
|
||||
if code == 0 {
|
||||
return out, code
|
||||
}
|
||||
return out + errOut, code
|
||||
}
|
||||
|
||||
|
||||
@@ -532,6 +532,9 @@ func runTransition(ctx context.Context, st HeadStore, cause HeadWriteCause, t fu
|
||||
if err := next.check(); err != nil {
|
||||
return Head{}, err
|
||||
}
|
||||
// Preserve the observed session for logging when the transition closes it.
|
||||
// This metadata is never used to authorize or persist a transition.
|
||||
ctx = context.WithValue(ctx, headWriteObservationKey{}, headWriteObservation{h.Gen, h.Activity.Session})
|
||||
if err := st.UpdateHead(ctx, h.Gen, next, cause); err != nil {
|
||||
return Head{}, err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package layout
|
||||
|
||||
import "context"
|
||||
|
||||
type headWriteObservationKey struct{}
|
||||
type headWriteObservation struct {
|
||||
gen uint64
|
||||
session string
|
||||
}
|
||||
|
||||
// HeadWriteSession returns observational log metadata, including the session a
|
||||
// successful transition just closed. It performs no read and grants no authority.
|
||||
func HeadWriteSession(ctx context.Context, expectedGen uint64, next Head) string {
|
||||
if next.Activity.Session != "" {
|
||||
return next.Activity.Session
|
||||
}
|
||||
if before, ok := ctx.Value(headWriteObservationKey{}).(headWriteObservation); ok && before.gen == expectedGen {
|
||||
return before.session
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package layout
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHeadWriteSessionDoesNotReuseAnotherGeneration(t *testing.T) {
|
||||
ctx := context.WithValue(context.Background(), headWriteObservationKey{}, headWriteObservation{gen: 7, session: "closed"})
|
||||
for _, tc := range []struct {
|
||||
gen uint64
|
||||
next string
|
||||
want string
|
||||
}{{7, "", "closed"}, {8, "", ""}, {7, "new", "new"}} {
|
||||
h := Head{}
|
||||
h.Activity.Session = tc.next
|
||||
if got := HeadWriteSession(ctx, tc.gen, h); got != tc.want {
|
||||
t.Fatalf("generation %d next %q: got %q, want %q", tc.gen, tc.next, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/layout"
|
||||
)
|
||||
@@ -21,7 +22,7 @@ func (s *LayoutStore) CreateHead(ctx context.Context, h layout.Head, cause layou
|
||||
if h.State != layout.StateLive || h.Holder != "" {
|
||||
return errors.New("chunkstore: only an unclaimed live HEAD is created")
|
||||
}
|
||||
return s.createHead(ctx, h)
|
||||
return s.createHead(ctx, h, cause)
|
||||
}
|
||||
|
||||
// CreateDeletedHead writes a deleted HEAD (a tombstone) exclusively;
|
||||
@@ -33,10 +34,11 @@ func (s *LayoutStore) CreateDeletedHead(ctx context.Context, h layout.Head, caus
|
||||
if h.State != layout.StateDeleted {
|
||||
return errors.New("chunkstore: only a deleted HEAD is created as a tombstone")
|
||||
}
|
||||
return s.createHead(ctx, h)
|
||||
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
|
||||
}
|
||||
@@ -47,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
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package chunkstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
"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 := ""
|
||||
if h.Current != nil {
|
||||
current = h.Current.String()
|
||||
}
|
||||
dependents := 0
|
||||
for _, snapshot := range h.Snapshots {
|
||||
dependents += len(snapshot.Dependents)
|
||||
}
|
||||
log.Printf("layout: HEAD write level=INFO volume=%q volume_id=%s gen=%d->%d head_write=%s session=%q epoch=%d holder=%q claim=%q activity=%s state=%s current=%q position=%d snapshots=%d dependents=%d took_ms=%.3f",
|
||||
path.Base(s.root), h.VolumeID, gen, gen+1, cause, layout.HeadWriteSession(ctx, gen, h), h.Epoch, h.Holder, h.ClaimID, h.Activity.Kind, h.State, current, h.Position, len(h.Snapshots), dependents, float64(time.Since(started))/float64(time.Millisecond))
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
package chunkstore
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"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
|
||||
old, flags := log.Writer(), log.Flags()
|
||||
log.SetOutput(&out)
|
||||
log.SetFlags(0)
|
||||
t.Cleanup(func() { log.SetOutput(old); log.SetFlags(flags) })
|
||||
return &out
|
||||
}
|
||||
|
||||
func TestEverySuccessfulStoreHeadWriteLogsItsCensusCause(t *testing.T) {
|
||||
out := captureHeadWrites(t)
|
||||
ctx := context.Background()
|
||||
for _, cause := range layout.HeadWriteCauses {
|
||||
s, _ := causeStore(t)
|
||||
h := layout.Unclaimed(s.id, nil)
|
||||
var err error
|
||||
switch cause {
|
||||
case layout.CauseCreate:
|
||||
err = s.CreateHead(ctx, h, cause)
|
||||
case layout.CauseTombstone:
|
||||
h.State = layout.StateDeleted
|
||||
err = s.CreateDeletedHead(ctx, h, cause)
|
||||
default:
|
||||
if err := s.CreateHead(ctx, h, layout.CauseCreate); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out.Reset()
|
||||
err = s.UpdateHead(ctx, 1, h, cause)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("%s: %v", cause, err)
|
||||
}
|
||||
line := out.String()
|
||||
if strings.Count(line, "HEAD write") != 1 || !strings.Contains(line, "level=INFO") || !strings.Contains(line, "head_write="+string(cause)) || !strings.Contains(line, "took_ms=") {
|
||||
t.Fatalf("%s: expected one INFO census line, got %q", cause, line)
|
||||
}
|
||||
out.Reset()
|
||||
}
|
||||
}
|
||||
|
||||
func TestHeadWriteLogDoesNotClaimARefusedWrite(t *testing.T) {
|
||||
out := captureHeadWrites(t)
|
||||
s, _ := causeStore(t)
|
||||
ctx := context.Background()
|
||||
h := layout.Unclaimed(s.id, nil)
|
||||
if err := s.CreateHead(ctx, h, layout.CauseCreate); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out.Reset()
|
||||
if err := s.CreateHead(ctx, h, layout.CauseCreate); !errors.Is(err, layout.ErrExists) {
|
||||
t.Fatalf("create conflict: %v", err)
|
||||
}
|
||||
if err := s.UpdateHead(ctx, 99, h, layout.CauseCommit); err == nil {
|
||||
t.Fatal("stale update accepted")
|
||||
}
|
||||
if out.Len() != 0 {
|
||||
t.Fatalf("refused writes claimed success: %s", out.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHeadSessionBeginAndCommitLogsKeepTheSameSession(t *testing.T) {
|
||||
out := captureHeadWrites(t)
|
||||
s, _, _ := layoutStoreFixture(t)
|
||||
ctx := context.Background()
|
||||
if err := s.CreateHead(ctx, layout.Unclaimed(s.id, nil), layout.CauseCreate); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
me := layout.HolderID{Epoch: 1, Holder: "host/replica", ClaimID: "claim-a"}
|
||||
if _, err := layout.RunClaim(ctx, s, okClaimLease{}, me); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out.Reset()
|
||||
if _, err := layout.RunBegin(ctx, s, me, "session-a"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
root, _, err := layout.Build(map[uint64]layout.Ref{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
name, raw, err := layout.NameRoot(root)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := layout.EnsureObject(ctx, s, name.Path(), raw, "session-a"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := layout.RunCommit(ctx, s, s, s.root, me, layout.CommitRequest{Session: "session-a", Next: map[uint64]layout.Ref{}, State: layout.StateLive}, layout.CauseCommit); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
lines := strings.Split(strings.TrimSpace(out.String()), "\n")
|
||||
if len(lines) != 2 {
|
||||
t.Fatalf("want begin and commit lines: %q", out.String())
|
||||
}
|
||||
for i, cause := range []string{"session-begin", "commit"} {
|
||||
if !strings.Contains(lines[i], "head_write="+cause) || !strings.Contains(lines[i], `session="session-a"`) {
|
||||
t.Fatalf("lost session correlation: %q", lines[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -97,6 +97,7 @@ func DecodeLayoutHeadEntry(e *filerpb.Entry, id layout.VolumeID) (layout.Head, e
|
||||
}
|
||||
|
||||
func (s *LayoutStore) UpdateHead(ctx context.Context, expectedGen uint64, next layout.Head, cause layout.HeadWriteCause) error {
|
||||
started := time.Now()
|
||||
if !cause.Known() {
|
||||
return fmt.Errorf("chunkstore: HEAD update with cause %q, not a census row", cause)
|
||||
}
|
||||
@@ -117,6 +118,11 @@ func (s *LayoutStore) UpdateHead(ctx context.Context, expectedGen uint64, next l
|
||||
} else {
|
||||
err = s.f.UpdateEntry(ctx, s.root, e, map[string][]byte{layoutGen: []byte(strconv.FormatUint(expectedGen, 10)), layoutVolume: []byte(s.id.String())})
|
||||
}
|
||||
if err == nil {
|
||||
s.logHeadWrite(ctx, expectedGen, next, cause, started)
|
||||
} else {
|
||||
s.logUnknownHeadWrite(ctx, expectedGen, next, cause, started, err)
|
||||
}
|
||||
return layoutError(err)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user