From 23b742cfd327ee9e20f8cad49fad12eec1b61a98 Mon Sep 17 00:00:00 2001 From: Marek Siarkowicz Date: Wed, 21 Jul 2021 16:58:42 +0200 Subject: [PATCH] server: Remove Quota direct dependency on EtcdServer --- server/etcdserver/api/v3rpc/quota.go | 4 ++-- server/etcdserver/apply.go | 2 +- server/etcdserver/quota.go | 34 +++++++++++++++------------- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/server/etcdserver/api/v3rpc/quota.go b/server/etcdserver/api/v3rpc/quota.go index 7f53bd966..c660df529 100644 --- a/server/etcdserver/api/v3rpc/quota.go +++ b/server/etcdserver/api/v3rpc/quota.go @@ -52,7 +52,7 @@ func (qa *quotaAlarmer) check(ctx context.Context, r interface{}) error { func NewQuotaKVServer(s *etcdserver.EtcdServer) pb.KVServer { return "aKVServer{ NewKVServer(s), - quotaAlarmer{etcdserver.NewBackendQuota(s, "kv"), s, s.ID()}, + quotaAlarmer{etcdserver.NewBackendQuota(s.Cfg, s.Backend(), "kv"), s, s.ID()}, } } @@ -85,6 +85,6 @@ func (s *quotaLeaseServer) LeaseGrant(ctx context.Context, cr *pb.LeaseGrantRequ func NewQuotaLeaseServer(s *etcdserver.EtcdServer) pb.LeaseServer { return "aLeaseServer{ NewLeaseServer(s), - quotaAlarmer{etcdserver.NewBackendQuota(s, "lease"), s, s.ID()}, + quotaAlarmer{etcdserver.NewBackendQuota(s.Cfg, s.Backend(), "lease"), s, s.ID()}, } } diff --git a/server/etcdserver/apply.go b/server/etcdserver/apply.go index 5235d61cd..69cd8c8b3 100644 --- a/server/etcdserver/apply.go +++ b/server/etcdserver/apply.go @@ -953,7 +953,7 @@ type quotaApplierV3 struct { } func newQuotaApplierV3(s *EtcdServer, app applierV3) applierV3 { - return "aApplierV3{app, NewBackendQuota(s, "v3-applier")} + return "aApplierV3{app, NewBackendQuota(s.Cfg, s.Backend(), "v3-applier")} } func (a *quotaApplierV3) Put(ctx context.Context, txn mvcc.TxnWrite, p *pb.PutRequest) (*pb.PutResponse, *traceutil.Trace, error) { diff --git a/server/etcdserver/quota.go b/server/etcdserver/quota.go index 33c06e619..e999abb59 100644 --- a/server/etcdserver/quota.go +++ b/server/etcdserver/quota.go @@ -18,6 +18,8 @@ import ( "sync" pb "go.etcd.io/etcd/api/v3/etcdserverpb" + "go.etcd.io/etcd/server/v3/config" + "go.etcd.io/etcd/server/v3/storage/backend" humanize "github.com/dustin/go-humanize" "go.uber.org/zap" @@ -51,7 +53,7 @@ func (*passthroughQuota) Cost(interface{}) int { return 0 } func (*passthroughQuota) Remaining() int64 { return 1 } type backendQuota struct { - s *EtcdServer + be backend.Backend maxBackendBytes int64 } @@ -71,23 +73,23 @@ var ( ) // NewBackendQuota creates a quota layer with the given storage limit. -func NewBackendQuota(s *EtcdServer, name string) Quota { - lg := s.Logger() - quotaBackendBytes.Set(float64(s.Cfg.QuotaBackendBytes)) +func NewBackendQuota(cfg config.ServerConfig, be backend.Backend, name string) Quota { + lg := cfg.Logger + quotaBackendBytes.Set(float64(cfg.QuotaBackendBytes)) - if s.Cfg.QuotaBackendBytes < 0 { + if cfg.QuotaBackendBytes < 0 { // disable quotas if negative quotaLogOnce.Do(func() { lg.Info( "disabled backend quota", zap.String("quota-name", name), - zap.Int64("quota-size-bytes", s.Cfg.QuotaBackendBytes), + zap.Int64("quota-size-bytes", cfg.QuotaBackendBytes), ) }) return &passthroughQuota{} } - if s.Cfg.QuotaBackendBytes == 0 { + if cfg.QuotaBackendBytes == 0 { // use default size if no quota size given quotaLogOnce.Do(func() { if lg != nil { @@ -100,16 +102,16 @@ func NewBackendQuota(s *EtcdServer, name string) Quota { } }) quotaBackendBytes.Set(float64(DefaultQuotaBytes)) - return &backendQuota{s, DefaultQuotaBytes} + return &backendQuota{be, DefaultQuotaBytes} } quotaLogOnce.Do(func() { - if s.Cfg.QuotaBackendBytes > MaxQuotaBytes { + if cfg.QuotaBackendBytes > MaxQuotaBytes { lg.Warn( "quota exceeds the maximum value", zap.String("quota-name", name), - zap.Int64("quota-size-bytes", s.Cfg.QuotaBackendBytes), - zap.String("quota-size", humanize.Bytes(uint64(s.Cfg.QuotaBackendBytes))), + zap.Int64("quota-size-bytes", cfg.QuotaBackendBytes), + zap.String("quota-size", humanize.Bytes(uint64(cfg.QuotaBackendBytes))), zap.Int64("quota-maximum-size-bytes", MaxQuotaBytes), zap.String("quota-maximum-size", maxQuotaSize), ) @@ -117,16 +119,16 @@ func NewBackendQuota(s *EtcdServer, name string) Quota { lg.Info( "enabled backend quota", zap.String("quota-name", name), - zap.Int64("quota-size-bytes", s.Cfg.QuotaBackendBytes), - zap.String("quota-size", humanize.Bytes(uint64(s.Cfg.QuotaBackendBytes))), + zap.Int64("quota-size-bytes", cfg.QuotaBackendBytes), + zap.String("quota-size", humanize.Bytes(uint64(cfg.QuotaBackendBytes))), ) }) - return &backendQuota{s, s.Cfg.QuotaBackendBytes} + return &backendQuota{be, cfg.QuotaBackendBytes} } func (b *backendQuota) Available(v interface{}) bool { // TODO: maybe optimize backend.Size() - return b.s.Backend().Size()+int64(b.Cost(v)) < b.maxBackendBytes + return b.be.Size()+int64(b.Cost(v)) < b.maxBackendBytes } func (b *backendQuota) Cost(v interface{}) int { @@ -168,5 +170,5 @@ func costTxn(r *pb.TxnRequest) int { } func (b *backendQuota) Remaining() int64 { - return b.maxBackendBytes - b.s.Backend().Size() + return b.maxBackendBytes - b.be.Size() }