mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 11:07:31 +01:00
filer: count the store's KV requests in the store metrics (#13302)
* filer: count the store's KV requests in the store metrics FilerStoreWrapper counts and times InsertEntry, UpdateEntry, FindEntry, DeleteEntry and the listings, but passed KvPut, KvGet, KvBatchGet and KvDelete straight to the store. Hard link records, the deletion ledger and the per-path commit records are all kept in KV, so that part of what a filer asks of its store did not show in SeaweedFS_filerStore_request_total or in the request duration histogram. The four KV calls are now counted and timed like the others, as kvPut, kvGet, kvBatchGet and kvDelete. * filer: shorten the KV metrics helper comment Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: MapleWhiteZZZ <18653045+MapleWhiteZZZ@users.noreply.github.com> Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2 files changed
+93
-4
No files matched your search
@@ -585,27 +585,45 @@ func (fsw *FilerStoreWrapper) Shutdown() {
|
||||
fsw.getDefaultStore().Shutdown()
|
||||
}
|
||||
|
||||
// kvRequest counts a KV request to the default store; done records its duration.
|
||||
func (fsw *FilerStoreWrapper) kvRequest(requestType string) (store FilerStore, done func()) {
|
||||
store = fsw.getDefaultStore()
|
||||
stats.FilerStoreCounter.WithLabelValues(store.GetName(), requestType).Inc()
|
||||
start := time.Now()
|
||||
return store, func() {
|
||||
stats.FilerStoreHistogram.WithLabelValues(store.GetName(), requestType).Observe(time.Since(start).Seconds())
|
||||
}
|
||||
}
|
||||
|
||||
func (fsw *FilerStoreWrapper) KvPut(ctx context.Context, key []byte, value []byte) (err error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
ctx = context.WithoutCancel(ctx)
|
||||
return fsw.getDefaultStore().KvPut(ctx, key, value)
|
||||
store, done := fsw.kvRequest("kvPut")
|
||||
defer done()
|
||||
return store.KvPut(ctx, key, value)
|
||||
}
|
||||
func (fsw *FilerStoreWrapper) KvGet(ctx context.Context, key []byte) (value []byte, err error) {
|
||||
ctx = context.WithoutCancel(ctx)
|
||||
return fsw.getDefaultStore().KvGet(ctx, key)
|
||||
store, done := fsw.kvRequest("kvGet")
|
||||
defer done()
|
||||
return store.KvGet(ctx, key)
|
||||
}
|
||||
func (fsw *FilerStoreWrapper) KvBatchGet(ctx context.Context, keys [][]byte) (values map[string][]byte, err error) {
|
||||
ctx = context.WithoutCancel(ctx)
|
||||
return fsw.getDefaultStore().KvBatchGet(ctx, keys)
|
||||
store, done := fsw.kvRequest("kvBatchGet")
|
||||
defer done()
|
||||
return store.KvBatchGet(ctx, keys)
|
||||
}
|
||||
func (fsw *FilerStoreWrapper) KvDelete(ctx context.Context, key []byte) (err error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
ctx = context.WithoutCancel(ctx)
|
||||
return fsw.getDefaultStore().KvDelete(ctx, key)
|
||||
store, done := fsw.kvRequest("kvDelete")
|
||||
defer done()
|
||||
return store.KvDelete(ctx, key)
|
||||
}
|
||||
|
||||
func (fsw *FilerStoreWrapper) Debug(writer io.Writer) {
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package filer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"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/stats"
|
||||
)
|
||||
|
||||
// KV requests go to the store like entry requests do, so they are counted and
|
||||
// timed like them.
|
||||
func TestFilerStoreWrapperCountsKvRequests(t *testing.T) {
|
||||
store := newStubFilerStore()
|
||||
wrapper := NewFilerStoreWrapper(store)
|
||||
ctx := context.Background()
|
||||
|
||||
requests := func(requestType string) float64 {
|
||||
return testutil.ToFloat64(stats.FilerStoreCounter.WithLabelValues(store.GetName(), requestType))
|
||||
}
|
||||
timed := func(requestType string) uint64 {
|
||||
var m dto.Metric
|
||||
if err := stats.FilerStoreHistogram.WithLabelValues(store.GetName(), requestType).(prometheus.Histogram).Write(&m); err != nil {
|
||||
t.Fatalf("read the %s duration histogram: %v", requestType, err)
|
||||
}
|
||||
return m.GetHistogram().GetSampleCount()
|
||||
}
|
||||
before, timedBefore := make(map[string]float64), make(map[string]uint64)
|
||||
for _, requestType := range []string{"kvPut", "kvGet", "kvBatchGet", "kvDelete"} {
|
||||
before[requestType], timedBefore[requestType] = requests(requestType), timed(requestType)
|
||||
}
|
||||
|
||||
if err := wrapper.KvPut(ctx, []byte("k"), []byte("v")); err != nil {
|
||||
t.Fatalf("KvPut: %v", err)
|
||||
}
|
||||
if _, err := wrapper.KvGet(ctx, []byte("k")); err != nil {
|
||||
t.Fatalf("KvGet: %v", err)
|
||||
}
|
||||
// a miss is a request to the store too
|
||||
if _, err := wrapper.KvGet(ctx, []byte("absent")); err != ErrKvNotFound {
|
||||
t.Fatalf("KvGet of an absent key: %v, want ErrKvNotFound", err)
|
||||
}
|
||||
if _, err := wrapper.KvBatchGet(ctx, [][]byte{[]byte("k"), []byte("absent")}); err != nil {
|
||||
t.Fatalf("KvBatchGet: %v", err)
|
||||
}
|
||||
if err := wrapper.KvDelete(ctx, []byte("k")); err != nil {
|
||||
t.Fatalf("KvDelete: %v", err)
|
||||
}
|
||||
|
||||
for requestType, want := range map[string]float64{"kvPut": 1, "kvGet": 2, "kvBatchGet": 1, "kvDelete": 1} {
|
||||
if got := requests(requestType) - before[requestType]; got != want {
|
||||
t.Errorf("%s requests counted: %v, want %v", requestType, got, want)
|
||||
}
|
||||
if got := timed(requestType) - timedBefore[requestType]; got != uint64(want) {
|
||||
t.Errorf("%s requests timed: %v, want %v", requestType, got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// a request refused for a cancelled context never reaches the store
|
||||
cancelled, cancel := context.WithCancel(ctx)
|
||||
cancel()
|
||||
if err := wrapper.KvPut(cancelled, []byte("k"), []byte("v")); err == nil {
|
||||
t.Fatal("KvPut with a cancelled context succeeded")
|
||||
}
|
||||
if got := requests("kvPut") - before["kvPut"]; got != 1 {
|
||||
t.Errorf("kvPut requests after a refused one: %v, want 1", got)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user