quota.go 3.3 KB

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