quota.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  17. humanize "github.com/dustin/go-humanize"
  18. "go.uber.org/zap"
  19. )
  20. const (
  21. // DefaultQuotaBytes is the number of bytes the backend Size may
  22. // consume before exceeding the space quota.
  23. DefaultQuotaBytes = int64(2 * 1024 * 1024 * 1024) // 2GB
  24. // MaxQuotaBytes is the maximum number of bytes suggested for a backend
  25. // quota. A larger quota may lead to degraded performance.
  26. MaxQuotaBytes = int64(8 * 1024 * 1024 * 1024) // 8GB
  27. )
  28. // Quota represents an arbitrary quota against arbitrary requests. Each request
  29. // costs some charge; if there is not enough remaining charge, then there are
  30. // too few resources available within the quota to apply the request.
  31. type Quota interface {
  32. // Available judges whether the given request fits within the quota.
  33. Available(req interface{}) bool
  34. // Cost computes the charge against the quota for a given request.
  35. Cost(req interface{}) int
  36. // Remaining is the amount of charge left for the quota.
  37. Remaining() int64
  38. }
  39. type passthroughQuota struct{}
  40. func (*passthroughQuota) Available(interface{}) bool { return true }
  41. func (*passthroughQuota) Cost(interface{}) int { return 0 }
  42. func (*passthroughQuota) Remaining() int64 { return 1 }
  43. type backendQuota struct {
  44. s *EtcdServer
  45. maxBackendBytes int64
  46. }
  47. const (
  48. // leaseOverhead is an estimate for the cost of storing a lease
  49. leaseOverhead = 64
  50. // kvOverhead is an estimate for the cost of storing a key's metadata
  51. kvOverhead = 256
  52. )
  53. func NewBackendQuota(s *EtcdServer, name string) Quota {
  54. lg := s.getLogger()
  55. if s.Cfg.QuotaBackendBytes < 0 {
  56. // disable quotas if negative
  57. if lg != nil {
  58. lg.Info(
  59. "disabled backend quota",
  60. zap.String("quota-name", name),
  61. zap.Int64("quota-size-bytes", s.Cfg.QuotaBackendBytes),
  62. )
  63. } else {
  64. plog.Warningf("disabling backend quota")
  65. }
  66. return &passthroughQuota{}
  67. }
  68. if s.Cfg.QuotaBackendBytes == 0 {
  69. // use default size if no quota size given
  70. if lg != nil {
  71. lg.Info(
  72. "enabled backend quota with default value",
  73. zap.String("quota-name", name),
  74. zap.Int64("quota-size-bytes", DefaultQuotaBytes),
  75. zap.String("quota-size", humanize.Bytes(uint64(DefaultQuotaBytes))),
  76. )
  77. }
  78. return &backendQuota{s, DefaultQuotaBytes}
  79. }
  80. if s.Cfg.QuotaBackendBytes > MaxQuotaBytes {
  81. if lg != nil {
  82. lg.Warn(
  83. "quota exceeds the maximum value",
  84. zap.String("quota-name", name),
  85. zap.Int64("quota-size-bytes", s.Cfg.QuotaBackendBytes),
  86. zap.String("quota-size", humanize.Bytes(uint64(s.Cfg.QuotaBackendBytes))),
  87. zap.Int64("quota-maximum-size-bytes", MaxQuotaBytes),
  88. zap.String("quota-maximum-size", humanize.Bytes(uint64(MaxQuotaBytes))),
  89. )
  90. } else {
  91. plog.Warningf("backend quota %v exceeds maximum recommended quota %v", s.Cfg.QuotaBackendBytes, MaxQuotaBytes)
  92. }
  93. }
  94. if lg != nil {
  95. lg.Info(
  96. "enabled backend quota",
  97. zap.String("quota-name", name),
  98. zap.Int64("quota-size-bytes", s.Cfg.QuotaBackendBytes),
  99. zap.String("quota-size", humanize.Bytes(uint64(s.Cfg.QuotaBackendBytes))),
  100. )
  101. }
  102. return &backendQuota{s, s.Cfg.QuotaBackendBytes}
  103. }
  104. func (b *backendQuota) Available(v interface{}) bool {
  105. // TODO: maybe optimize backend.Size()
  106. return b.s.Backend().Size()+int64(b.Cost(v)) < b.maxBackendBytes
  107. }
  108. func (b *backendQuota) Cost(v interface{}) int {
  109. switch r := v.(type) {
  110. case *pb.PutRequest:
  111. return costPut(r)
  112. case *pb.TxnRequest:
  113. return costTxn(r)
  114. case *pb.LeaseGrantRequest:
  115. return leaseOverhead
  116. default:
  117. panic("unexpected cost")
  118. }
  119. }
  120. func costPut(r *pb.PutRequest) int { return kvOverhead + len(r.Key) + len(r.Value) }
  121. func costTxnReq(u *pb.RequestOp) int {
  122. r := u.GetRequestPut()
  123. if r == nil {
  124. return 0
  125. }
  126. return costPut(r)
  127. }
  128. func costTxn(r *pb.TxnRequest) int {
  129. sizeSuccess := 0
  130. for _, u := range r.Success {
  131. sizeSuccess += costTxnReq(u)
  132. }
  133. sizeFailure := 0
  134. for _, u := range r.Failure {
  135. sizeFailure += costTxnReq(u)
  136. }
  137. if sizeFailure > sizeSuccess {
  138. return sizeFailure
  139. }
  140. return sizeSuccess
  141. }
  142. func (b *backendQuota) Remaining() int64 {
  143. return b.maxBackendBytes - b.s.Backend().Size()
  144. }