quota.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2016 CoreOS, Inc.
  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/storage/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 backendQuota struct {
  31. s *EtcdServer
  32. maxBackendBytes int64
  33. }
  34. const (
  35. // leaseOverhead is an estimate for the cost of storing a lease
  36. leaseOverhead = 64
  37. // kvOverhead is an estimate for the cost of storing a key's metadata
  38. kvOverhead = 256
  39. )
  40. func NewBackendQuota(s *EtcdServer) Quota {
  41. return &backendQuota{s, backend.InitialMmapSize}
  42. }
  43. func (b *backendQuota) Available(v interface{}) bool {
  44. // TODO: maybe optimize backend.Size()
  45. return b.s.Backend().Size()+int64(b.Cost(v)) < b.maxBackendBytes
  46. }
  47. func (b *backendQuota) Cost(v interface{}) int {
  48. switch r := v.(type) {
  49. case *pb.PutRequest:
  50. return costPut(r)
  51. case *pb.TxnRequest:
  52. return costTxn(r)
  53. case *pb.LeaseCreateRequest:
  54. return leaseOverhead
  55. default:
  56. panic("unexpected cost")
  57. }
  58. }
  59. func costPut(r *pb.PutRequest) int { return kvOverhead + len(r.Key) + len(r.Value) }
  60. func costTxnReq(u *pb.RequestUnion) int {
  61. r := u.GetRequestPut()
  62. if r == nil {
  63. return 0
  64. }
  65. return costPut(r)
  66. }
  67. func costTxn(r *pb.TxnRequest) int {
  68. sizeSuccess := 0
  69. for _, u := range r.Success {
  70. sizeSuccess += costTxnReq(u)
  71. }
  72. sizeFailure := 0
  73. for _, u := range r.Failure {
  74. sizeFailure += costTxnReq(u)
  75. }
  76. if sizeFailure > sizeSuccess {
  77. return sizeFailure
  78. }
  79. return sizeSuccess
  80. }
  81. func (b *backendQuota) Remaining() int64 {
  82. return b.maxBackendBytes - b.s.Backend().Size()
  83. }