mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
server: Remove Quota direct dependency on EtcdServer
This commit is contained in:
parent
44b8ae145b
commit
23b742cfd3
@ -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()},
|
||||
}
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user