quota.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package etcdserver
  15. import (
  16. "sync"
  17. pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
  18. humanize "github.com/dustin/go-humanize"
  19. "go.uber.org/zap"
  20. )
  21. const (
  22. // DefaultQuotaBytes is the number of bytes the backend Size may
  23. // consume before exceeding the space quota.
  24. DefaultQuotaBytes = int64(2 * 1024 * 1024 * 1024) // 2GB
  25. // MaxQuotaBytes is the maximum number of bytes suggested for a backend
  26. // quota. A larger quota may lead to degraded performance.
  27. MaxQuotaBytes = int64(8 * 1024 * 1024 * 1024) // 8GB
  28. )
  29. // Quota represents an arbitrary quota against arbitrary requests. Each request
  30. // costs some charge; if there is not enough remaining charge, then there are
  31. // too few resources available within the quota to apply the request.
  32. type Quota interface {
  33. // Available judges whether the given request fits within the quota.
  34. Available(req interface{}) bool
  35. // Cost computes the charge against the quota for a given request.
  36. Cost(req interface{}) int
  37. // Remaining is the amount of charge left for the quota.
  38. Remaining() int64
  39. }
  40. type passthroughQuota struct{}
  41. func (*passthroughQuota) Available(interface{}) bool { return true }
  42. func (*passthroughQuota) Cost(interface{}) int { return 0 }
  43. func (*passthroughQuota) Remaining() int64 { return 1 }
  44. type backendQuota struct {
  45. s *EtcdServer
  46. maxBackendBytes int64
  47. }
  48. const (
  49. // leaseOverhead is an estimate for the cost of storing a lease
  50. leaseOverhead = 64
  51. // kvOverhead is an estimate for the cost of storing a key's metadata
  52. kvOverhead = 256
  53. )
  54. var (
  55. // only log once
  56. quotaLogOnce sync.Once
  57. DefaultQuotaSize = humanize.Bytes(uint64(DefaultQuotaBytes))
  58. maxQuotaSize = humanize.Bytes(uint64(MaxQuotaBytes))
  59. )
  60. // NewBackendQuota creates a quota layer with the given storage limit.
  61. func NewBackendQuota(s *EtcdServer, name string) Quota {
  62. lg := s.getLogger()
  63. quotaBackendBytes.Set(float64(s.Cfg.QuotaBackendBytes))
  64. if s.Cfg.QuotaBackendBytes < 0 {
  65. // disable quotas if negative
  66. quotaLogOnce.Do(func() {
  67. if lg != nil {
  68. lg.Info(
  69. "disabled backend quota",
  70. zap.String("quota-name", name),
  71. zap.Int64("quota-size-bytes", s.Cfg.QuotaBackendBytes),
  72. )
  73. } else {
  74. plog.Warningf("disabling backend quota")
  75. }
  76. })
  77. return &passthroughQuota{}
  78. }
  79. if s.Cfg.QuotaBackendBytes == 0 {
  80. // use default size if no quota size given
  81. quotaLogOnce.Do(func() {
  82. if lg != nil {
  83. lg.Info(
  84. "enabled backend quota with default value",
  85. zap.String("quota-name", name),
  86. zap.Int64("quota-size-bytes", DefaultQuotaBytes),
  87. zap.String("quota-size", DefaultQuotaSize),
  88. )
  89. }
  90. })
  91. quotaBackendBytes.Set(float64(DefaultQuotaBytes))
  92. return &backendQuota{s, DefaultQuotaBytes}
  93. }
  94. quotaLogOnce.Do(func() {
  95. if s.Cfg.QuotaBackendBytes > MaxQuotaBytes {
  96. if lg != nil {
  97. lg.Warn(
  98. "quota exceeds the maximum value",
  99. zap.String("quota-name", name),
  100. zap.Int64("quota-size-bytes", s.Cfg.QuotaBackendBytes),
  101. zap.String("quota-size", humanize.Bytes(uint64(s.Cfg.QuotaBackendBytes))),
  102. zap.Int64("quota-maximum-size-bytes", MaxQuotaBytes),
  103. zap.String("quota-maximum-size", maxQuotaSize),
  104. )
  105. } else {
  106. plog.Warningf("backend quota %v exceeds maximum recommended quota %v", s.Cfg.QuotaBackendBytes, MaxQuotaBytes)
  107. }
  108. }
  109. if lg != nil {
  110. lg.Info(
  111. "enabled backend quota",
  112. zap.String("quota-name", name),
  113. zap.Int64("quota-size-bytes", s.Cfg.QuotaBackendBytes),
  114. zap.String("quota-size", humanize.Bytes(uint64(s.Cfg.QuotaBackendBytes))),
  115. )
  116. }
  117. })
  118. return &backendQuota{s, s.Cfg.QuotaBackendBytes}
  119. }
  120. func (b *backendQuota) Available(v interface{}) bool {
  121. // TODO: maybe optimize backend.Size()
  122. return b.s.Backend().Size()+int64(b.Cost(v)) < b.maxBackendBytes
  123. }
  124. func (b *backendQuota) Cost(v interface{}) int {
  125. switch r := v.(type) {
  126. case *pb.PutRequest:
  127. return costPut(r)
  128. case *pb.TxnRequest:
  129. return costTxn(r)
  130. case *pb.LeaseGrantRequest:
  131. return leaseOverhead
  132. default:
  133. panic("unexpected cost")
  134. }
  135. }
  136. func costPut(r *pb.PutRequest) int { return kvOverhead + len(r.Key) + len(r.Value) }
  137. func costTxnReq(u *pb.RequestOp) int {
  138. r := u.GetRequestPut()
  139. if r == nil {
  140. return 0
  141. }
  142. return costPut(r)
  143. }
  144. func costTxn(r *pb.TxnRequest) int {
  145. sizeSuccess := 0
  146. for _, u := range r.Success {
  147. sizeSuccess += costTxnReq(u)
  148. }
  149. sizeFailure := 0
  150. for _, u := range r.Failure {
  151. sizeFailure += costTxnReq(u)
  152. }
  153. if sizeFailure > sizeSuccess {
  154. return sizeFailure
  155. }
  156. return sizeSuccess
  157. }
  158. func (b *backendQuota) Remaining() int64 {
  159. return b.maxBackendBytes - b.s.Backend().Size()
  160. }