quota.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  16. const (
  17. // DefaultQuotaBytes is the number of bytes the backend Size may
  18. // consume before exceeding the space quota.
  19. DefaultQuotaBytes = int64(2 * 1024 * 1024 * 1024) // 2GB
  20. // MaxQuotaBytes is the maximum number of bytes suggested for a backend
  21. // quota. A larger quota may lead to degraded performance.
  22. MaxQuotaBytes = int64(8 * 1024 * 1024 * 1024) // 8GB
  23. )
  24. // Quota represents an arbitrary quota against arbitrary requests. Each request
  25. // costs some charge; if there is not enough remaining charge, then there are
  26. // too few resources available within the quota to apply the request.
  27. type Quota interface {
  28. // Available judges whether the given request fits within the quota.
  29. Available(req interface{}) bool
  30. // Cost computes the charge against the quota for a given request.
  31. Cost(req interface{}) int
  32. // Remaining is the amount of charge left for the quota.
  33. Remaining() int64
  34. }
  35. type passthroughQuota struct{}
  36. func (*passthroughQuota) Available(interface{}) bool { return true }
  37. func (*passthroughQuota) Cost(interface{}) int { return 0 }
  38. func (*passthroughQuota) Remaining() int64 { return 1 }
  39. type backendQuota struct {
  40. s *EtcdServer
  41. maxBackendBytes int64
  42. }
  43. const (
  44. // leaseOverhead is an estimate for the cost of storing a lease
  45. leaseOverhead = 64
  46. // kvOverhead is an estimate for the cost of storing a key's metadata
  47. kvOverhead = 256
  48. )
  49. func NewBackendQuota(s *EtcdServer) Quota {
  50. quotaBackendBytes.Set(float64(s.Cfg.QuotaBackendBytes))
  51. if s.Cfg.QuotaBackendBytes < 0 {
  52. // disable quotas if negative
  53. plog.Warningf("disabling backend quota")
  54. return &passthroughQuota{}
  55. }
  56. if s.Cfg.QuotaBackendBytes == 0 {
  57. // use default size if no quota size given
  58. quotaBackendBytes.Set(float64(DefaultQuotaBytes))
  59. return &backendQuota{s, DefaultQuotaBytes}
  60. }
  61. if s.Cfg.QuotaBackendBytes > MaxQuotaBytes {
  62. plog.Warningf("backend quota %v exceeds maximum recommended quota %v", s.Cfg.QuotaBackendBytes, MaxQuotaBytes)
  63. }
  64. return &backendQuota{s, s.Cfg.QuotaBackendBytes}
  65. }
  66. func (b *backendQuota) Available(v interface{}) bool {
  67. // TODO: maybe optimize backend.Size()
  68. return b.s.Backend().Size()+int64(b.Cost(v)) < b.maxBackendBytes
  69. }
  70. func (b *backendQuota) Cost(v interface{}) int {
  71. switch r := v.(type) {
  72. case *pb.PutRequest:
  73. return costPut(r)
  74. case *pb.TxnRequest:
  75. return costTxn(r)
  76. case *pb.LeaseGrantRequest:
  77. return leaseOverhead
  78. default:
  79. panic("unexpected cost")
  80. }
  81. }
  82. func costPut(r *pb.PutRequest) int { return kvOverhead + len(r.Key) + len(r.Value) }
  83. func costTxnReq(u *pb.RequestOp) int {
  84. r := u.GetRequestPut()
  85. if r == nil {
  86. return 0
  87. }
  88. return costPut(r)
  89. }
  90. func costTxn(r *pb.TxnRequest) int {
  91. sizeSuccess := 0
  92. for _, u := range r.Success {
  93. sizeSuccess += costTxnReq(u)
  94. }
  95. sizeFailure := 0
  96. for _, u := range r.Failure {
  97. sizeFailure += costTxnReq(u)
  98. }
  99. if sizeFailure > sizeSuccess {
  100. return sizeFailure
  101. }
  102. return sizeSuccess
  103. }
  104. func (b *backendQuota) Remaining() int64 {
  105. return b.maxBackendBytes - b.s.Backend().Size()
  106. }