diff --git a/seaweed-block/core/host/volume/chunk_control.go b/seaweed-block/core/host/volume/chunk_control.go index afb8cd99cb..ebd313c0b5 100644 --- a/seaweed-block/core/host/volume/chunk_control.go +++ b/seaweed-block/core/host/volume/chunk_control.go @@ -45,8 +45,55 @@ type ChunkCutResponse struct { Holder string `json:"holder"` Noop bool `json:"noop"` FullHash bool `json:"fullHash"` - // The cut statistics (§3.1) are added when W3 fills them; until then - // the body omits them rather than report zeros (B9 W2 appendix). + // Stats are the statistics the chunk layer measured for this cut + // (§3.1). A zero field is "not measured" or "none" (a Noop cut + // uploads nothing). + Stats ChunkCutStatsBody `json:"stats"` +} + +// ChunkCutStatsBody is one cut's statistics in the /chunk/cut body. It +// mirrors storage.ChunkCutStats field for field (a test keeps the two in +// step); every duration is in nanoseconds. +type ChunkCutStatsBody struct { + BarrierPauseNs int64 `json:"barrierPauseNs"` + SyncNs int64 `json:"syncNs"` + CopyNs int64 `json:"copyNs"` + CandidateChunks int `json:"candidateChunks"` + UploadedChunks int `json:"uploadedChunks"` + UploadedBytes int64 `json:"uploadedBytes"` + ZeroedChunks int `json:"zeroedChunks"` + UnchangedChunks int `json:"unchangedChunks"` + UploadNs int64 `json:"uploadNs"` + CASNs int64 `json:"casNs"` + CASResult string `json:"casResult"` + TotalNs int64 `json:"totalNs"` + StageRounds int `json:"stageRounds"` + StagedChunks int `json:"stagedChunks"` + StagedChunksUsed int `json:"stagedChunksUsed"` + BarrierCopyChunks int `json:"barrierCopyChunks"` + BarrierOverBound bool `json:"barrierOverBound"` +} + +func cutStatsBody(s storage.ChunkCutStats) ChunkCutStatsBody { + return ChunkCutStatsBody{ + BarrierPauseNs: s.BarrierPauseNs, + SyncNs: s.SyncNs, + CopyNs: s.CopyNs, + CandidateChunks: s.CandidateChunks, + UploadedChunks: s.UploadedChunks, + UploadedBytes: s.UploadedBytes, + ZeroedChunks: s.ZeroedChunks, + UnchangedChunks: s.UnchangedChunks, + UploadNs: s.UploadNs, + CASNs: s.CASNs, + CASResult: s.CASResult, + TotalNs: s.TotalNs, + StageRounds: s.StageRounds, + StagedChunks: s.StagedChunks, + StagedChunksUsed: s.StagedChunksUsed, + BarrierCopyChunks: s.BarrierCopyChunks, + BarrierOverBound: s.BarrierOverBound, + } } // ChunkSnapshotRequest is POST /chunk/snapshot's request (§3.2), without @@ -455,7 +502,7 @@ func (c *chunkControl) Cut(ctx context.Context, volumeID string, minLSN uint64) return ChunkCutResponse{}, err } return ChunkCutResponse{VolumeID: volumeID, CutLSN: r.H, HeadGen: r.Head.Gen, HeadEpoch: r.Head.Epoch, Holder: r.Head.Holder, - Noop: r.Noop, FullHash: r.FullHash}, nil + Noop: r.Noop, FullHash: r.FullHash, Stats: cutStatsBody(r.Stats)}, nil } // Snapshot resolves the request against the catalog first (§3.2 step 1): diff --git a/seaweed-block/core/host/volume/chunk_control_adapter_test.go b/seaweed-block/core/host/volume/chunk_control_adapter_test.go index 7649b6cea1..cac46d325a 100644 --- a/seaweed-block/core/host/volume/chunk_control_adapter_test.go +++ b/seaweed-block/core/host/volume/chunk_control_adapter_test.go @@ -3,6 +3,7 @@ package volume import ( "context" "encoding/json" + "reflect" "testing" "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore" @@ -112,6 +113,43 @@ func TestChunkControl_CutReturnsTheLayerStats(t *testing.T) { } } +// ChunkCutStatsBody mirrors storage.ChunkCutStats: a field added to the +// layer's statistics must also reach the body. +func TestChunkCutStatsBody_MirrorsTheLayerStats(t *testing.T) { + layer, body := reflect.TypeOf(storage.ChunkCutStats{}), reflect.TypeOf(ChunkCutStatsBody{}) + if layer.NumField() != body.NumField() { + t.Fatalf("storage.ChunkCutStats has %d fields, ChunkCutStatsBody %d", layer.NumField(), body.NumField()) + } + for i := 0; i < layer.NumField(); i++ { + lf, ok := body.FieldByName(layer.Field(i).Name) + if !ok || lf.Type != layer.Field(i).Type { + t.Fatalf("ChunkCutStatsBody lacks %s %s", layer.Field(i).Name, layer.Field(i).Type) + } + } + // Every field is copied: set each one to a distinct value and compare. + var s storage.ChunkCutStats + v := reflect.ValueOf(&s).Elem() + for i := 0; i < v.NumField(); i++ { + switch f := v.Field(i); f.Kind() { + case reflect.Int, reflect.Int64: + f.SetInt(int64(i + 1)) + case reflect.String: + f.SetString("s") + case reflect.Bool: + f.SetBool(true) + default: + t.Fatalf("field %s: unhandled kind %s", v.Type().Field(i).Name, f.Kind()) + } + } + got := reflect.ValueOf(cutStatsBody(s)) + for i := 0; i < v.NumField(); i++ { + name := v.Type().Field(i).Name + if !reflect.DeepEqual(got.FieldByName(name).Interface(), v.Field(i).Interface()) { + t.Fatalf("cutStatsBody drops %s: got %v, want %v", name, got.FieldByName(name).Interface(), v.Field(i).Interface()) + } + } +} + func TestChunkControl_CutMinLSNAndDetach(t *testing.T) { ctx := context.Background() fp, ctl := adapterFixture(t)