server: Remove Quota direct dependency on EtcdServer

dependabot/go_modules/go.uber.org/atomic-1.10.0
Marek Siarkowicz 2021-07-21 16:58:42 +02:00
parent 44b8ae145b
commit 23b742cfd3
3 changed files with 21 additions and 19 deletions

View File

@ -52,7 +52,7 @@ func (qa *quotaAlarmer) check(ctx context.Context, r interface{}) error {
func NewQuotaKVServer(s *etcdserver.EtcdServer) pb.KVServer { func NewQuotaKVServer(s *etcdserver.EtcdServer) pb.KVServer {
return &quotaKVServer{ return &quotaKVServer{
NewKVServer(s), 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 { func NewQuotaLeaseServer(s *etcdserver.EtcdServer) pb.LeaseServer {
return &quotaLeaseServer{ return &quotaLeaseServer{
NewLeaseServer(s), NewLeaseServer(s),
quotaAlarmer{etcdserver.NewBackendQuota(s, "lease"), s, s.ID()}, quotaAlarmer{etcdserver.NewBackendQuota(s.Cfg, s.Backend(), "lease"), s, s.ID()},
} }
} }

View File

@ -953,7 +953,7 @@ type quotaApplierV3 struct {
} }
func newQuotaApplierV3(s *EtcdServer, app applierV3) applierV3 { func newQuotaApplierV3(s *EtcdServer, app applierV3) applierV3 {
return &quotaApplierV3{app, NewBackendQuota(s, "v3-applier")} return &quotaApplierV3{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) { func (a *quotaApplierV3) Put(ctx context.Context, txn mvcc.TxnWrite, p *pb.PutRequest) (*pb.PutResponse, *traceutil.Trace, error) {

View File

@ -18,6 +18,8 @@ import (
"sync" "sync"
pb "go.etcd.io/etcd/api/v3/etcdserverpb" 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" humanize "github.com/dustin/go-humanize"
"go.uber.org/zap" "go.uber.org/zap"
@ -51,7 +53,7 @@ func (*passthroughQuota) Cost(interface{}) int { return 0 }
func (*passthroughQuota) Remaining() int64 { return 1 } func (*passthroughQuota) Remaining() int64 { return 1 }
type backendQuota struct { type backendQuota struct {
s *EtcdServer be backend.Backend
maxBackendBytes int64 maxBackendBytes int64
} }
@ -71,23 +73,23 @@ var (
) )
// NewBackendQuota creates a quota layer with the given storage limit. // NewBackendQuota creates a quota layer with the given storage limit.
func NewBackendQuota(s *EtcdServer, name string) Quota { func NewBackendQuota(cfg config.ServerConfig, be backend.Backend, name string) Quota {
lg := s.Logger() lg := cfg.Logger
quotaBackendBytes.Set(float64(s.Cfg.QuotaBackendBytes)) quotaBackendBytes.Set(float64(cfg.QuotaBackendBytes))
if s.Cfg.QuotaBackendBytes < 0 { if cfg.QuotaBackendBytes < 0 {
// disable quotas if negative // disable quotas if negative
quotaLogOnce.Do(func() { quotaLogOnce.Do(func() {
lg.Info( lg.Info(
"disabled backend quota", "disabled backend quota",
zap.String("quota-name", name), zap.String("quota-name", name),
zap.Int64("quota-size-bytes", s.Cfg.QuotaBackendBytes), zap.Int64("quota-size-bytes", cfg.QuotaBackendBytes),
) )
}) })
return &passthroughQuota{} return &passthroughQuota{}
} }
if s.Cfg.QuotaBackendBytes == 0 { if cfg.QuotaBackendBytes == 0 {
// use default size if no quota size given // use default size if no quota size given
quotaLogOnce.Do(func() { quotaLogOnce.Do(func() {
if lg != nil { if lg != nil {
@ -100,16 +102,16 @@ func NewBackendQuota(s *EtcdServer, name string) Quota {
} }
}) })
quotaBackendBytes.Set(float64(DefaultQuotaBytes)) quotaBackendBytes.Set(float64(DefaultQuotaBytes))
return &backendQuota{s, DefaultQuotaBytes} return &backendQuota{be, DefaultQuotaBytes}
} }
quotaLogOnce.Do(func() { quotaLogOnce.Do(func() {
if s.Cfg.QuotaBackendBytes > MaxQuotaBytes { if cfg.QuotaBackendBytes > MaxQuotaBytes {
lg.Warn( lg.Warn(
"quota exceeds the maximum value", "quota exceeds the maximum value",
zap.String("quota-name", name), zap.String("quota-name", name),
zap.Int64("quota-size-bytes", s.Cfg.QuotaBackendBytes), zap.Int64("quota-size-bytes", cfg.QuotaBackendBytes),
zap.String("quota-size", humanize.Bytes(uint64(s.Cfg.QuotaBackendBytes))), zap.String("quota-size", humanize.Bytes(uint64(cfg.QuotaBackendBytes))),
zap.Int64("quota-maximum-size-bytes", MaxQuotaBytes), zap.Int64("quota-maximum-size-bytes", MaxQuotaBytes),
zap.String("quota-maximum-size", maxQuotaSize), zap.String("quota-maximum-size", maxQuotaSize),
) )
@ -117,16 +119,16 @@ func NewBackendQuota(s *EtcdServer, name string) Quota {
lg.Info( lg.Info(
"enabled backend quota", "enabled backend quota",
zap.String("quota-name", name), zap.String("quota-name", name),
zap.Int64("quota-size-bytes", s.Cfg.QuotaBackendBytes), zap.Int64("quota-size-bytes", cfg.QuotaBackendBytes),
zap.String("quota-size", humanize.Bytes(uint64(s.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 { func (b *backendQuota) Available(v interface{}) bool {
// TODO: maybe optimize backend.Size() // 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 { func (b *backendQuota) Cost(v interface{}) int {
@ -168,5 +170,5 @@ func costTxn(r *pb.TxnRequest) int {
} }
func (b *backendQuota) Remaining() int64 { func (b *backendQuota) Remaining() int64 {
return b.maxBackendBytes - b.s.Backend().Size() return b.maxBackendBytes - b.be.Size()
} }