mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
What would you like to be added?
Add new compactor based revision count, instead of fixed interval time.
In order to make it happen, the mvcc store needs to export
`CompactNotify` function to notify the compactor that configured number of
write transactions have occured since previsious compaction. The
new compactor can get the revision change and delete out-of-date data in time,
instead of waiting with fixed interval time. The underly bbolt db can
reuse the free pages as soon as possible.
Why is this needed?
In the kubernetes cluster, for instance, argo workflow, there will be batch
requests to create pods , and then there are also a lot of pod status's PATCH
requests, especially when the pod has more than 3 containers. If the burst
requests increase the db size in short time, it will be easy to exceed the max
quota size. And then the cluster admin get involved to defrag, which may casue
long downtime. So, we hope the ETCD can delete the out-of-date data as
soon as possible and slow down the grow of total db size.
Currently, both revision and periodic are based on time. It's not easy
to use fixed interval time to face the unexpected burst update requests.
The new compactor based on revision count can make the admin life easier.
For instance, let's say that average of object size is 50 KiB. The new
compactor will compact based on 10,000 revisions. It's like that ETCD can
compact after new 500 MiB data in, no matter how long ETCD takes to get
new 10,000 revisions. It can handle the burst update requests well.
There are some test results:
* Fixed value size: 10 KiB, Update Rate: 100/s, Total key space: 3,000
```
enchmark put --rate=100 --total=300000 --compact-interval=0 \
--key-space-size=3000 --key-size=256 --val-size=10240
```
| Compactor | DB Total Size | DB InUse Size |
| -- | -- | -- |
| Revision(5min,retension:10000) | 570 MiB | 208 MiB |
| Periodic(1m) | 232 MiB | 165 MiB |
| Periodic(30s) | 151 MiB | 127 MiB |
| NewRevision(retension:10000) | 195 MiB | 187 MiB |
* Random value size: [9 KiB, 11 KiB], Update Rate: 150/s, Total key space: 3,000
```
bnchmark put --rate=150 --total=300000 --compact-interval=0 \
--key-space-size=3000 --key-size=256 --val-size=10240 \
--delta-val-size=1024
```
| Compactor | DB Total Size | DB InUse Size |
| -- | -- | -- |
| Revision(5min,retension:10000) | 718 MiB | 554 MiB |
| Periodic(1m) | 297 MiB | 246 MiB |
| Periodic(30s) | 185 MiB | 146 MiB |
| NewRevision(retension:10000) | 186 MiB | 178 MiB |
* Random value size: [6 KiB, 14 KiB], Update Rate: 200/s, Total key space: 3,000
```
bnchmark put --rate=200 --total=300000 --compact-interval=0 \
--key-space-size=3000 --key-size=256 --val-size=10240 \
--delta-val-size=4096
```
| Compactor | DB Total Size | DB InUse Size |
| -- | -- | -- |
| Revision(5min,retension:10000) | 874 MiB | 221 MiB |
| Periodic(1m) | 357 MiB | 260 MiB |
| Periodic(30s) | 215 MiB | 151 MiB |
| NewRevision(retension:10000) | 182 MiB | 176 MiB |
For the burst requests, we needs to use short periodic interval.
Otherwise, the total size will be large. I think the new compactor can
handle it well.
Additional Change:
Currently, the quota system only checks DB total size. However, there
could be a lot of free pages which can be reused to upcoming requests.
Based on this proposal, I also want to extend current quota system with DB's
InUse size.
If the InUse size is less than max quota size, we should allow requests to
update. Since the bbolt might be resized if there is no available
continuous pages, we should setup a hard limit for the overflow, like 1
GiB.
```diff
// Quota represents an arbitrary quota against arbitrary requests. Each request
@@ -130,7 +134,17 @@ func (b *BackendQuota) Available(v interface{}) bool {
return true
}
// TODO: maybe optimize Backend.Size()
- return b.be.Size()+int64(cost) < b.maxBackendBytes
+
+ // Since the compact comes with allocatable pages, we should check the
+ // SizeInUse first. If there is no continuous pages for key/value and
+ // the boltdb continues to resize, it should not increase more than 1
+ // GiB. It's hard limitation.
+ //
+ // TODO: It should be enabled by flag.
+ if b.be.Size()+int64(cost)-b.maxBackendBytes >= maxAllowedOverflowBytes(b.maxBackendBytes) {
+ return false
+ }
+ return b.be.SizeInUse()+int64(cost) < b.maxBackendBytes
}
```
And it's likely to disable NOSPACE alarm if the compact can get much
more free pages. It can reduce downtime.
Signed-off-by: Wei Fu <fuweid89@gmail.com>
199 lines
5.7 KiB
Go
199 lines
5.7 KiB
Go
// Copyright 2016 The etcd Authors
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package storage
|
|
|
|
import (
|
|
"sync"
|
|
|
|
pb "go.etcd.io/etcd/api/v3/etcdserverpb"
|
|
"go.etcd.io/etcd/server/v3/storage/backend"
|
|
|
|
humanize "github.com/dustin/go-humanize"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
const (
|
|
// DefaultQuotaBytes is the number of bytes the backend Size may
|
|
// consume before exceeding the space quota.
|
|
DefaultQuotaBytes = int64(2 * 1024 * 1024 * 1024) // 2GB
|
|
// MaxQuotaBytes is the maximum number of bytes suggested for a backend
|
|
// quota. A larger quota may lead to degraded performance.
|
|
MaxQuotaBytes = int64(8 * 1024 * 1024 * 1024) // 8GB
|
|
|
|
// MaxAllowedOverflowQuotaBytes is the number of bytes the backend size
|
|
// can be overflow after exceeding the space quota.
|
|
MaxAllowedOverflowQuotaBytes = int64(1024 * 1024 * 1024) // 1GB
|
|
)
|
|
|
|
// Quota represents an arbitrary quota against arbitrary requests. Each request
|
|
// costs some charge; if there is not enough remaining charge, then there are
|
|
// too few resources available within the quota to apply the request.
|
|
type Quota interface {
|
|
// Available judges whether the given request fits within the quota.
|
|
Available(req interface{}) bool
|
|
// Cost computes the charge against the quota for a given request.
|
|
Cost(req interface{}) int
|
|
// Remaining is the amount of charge left for the quota.
|
|
Remaining() int64
|
|
}
|
|
|
|
type passthroughQuota struct{}
|
|
|
|
func (*passthroughQuota) Available(interface{}) bool { return true }
|
|
func (*passthroughQuota) Cost(interface{}) int { return 0 }
|
|
func (*passthroughQuota) Remaining() int64 { return 1 }
|
|
|
|
type BackendQuota struct {
|
|
be backend.Backend
|
|
maxBackendBytes int64
|
|
}
|
|
|
|
const (
|
|
// leaseOverhead is an estimate for the cost of storing a lease
|
|
leaseOverhead = 64
|
|
// kvOverhead is an estimate for the cost of storing a key's Metadata
|
|
kvOverhead = 256
|
|
)
|
|
|
|
var (
|
|
// only log once
|
|
quotaLogOnce sync.Once
|
|
|
|
DefaultQuotaSize = humanize.Bytes(uint64(DefaultQuotaBytes))
|
|
maxQuotaSize = humanize.Bytes(uint64(MaxQuotaBytes))
|
|
)
|
|
|
|
// NewBackendQuota creates a quota layer with the given storage limit.
|
|
func NewBackendQuota(lg *zap.Logger, quotaBackendBytesCfg int64, be backend.Backend, name string) Quota {
|
|
quotaBackendBytes.Set(float64(quotaBackendBytesCfg))
|
|
if quotaBackendBytesCfg < 0 {
|
|
// disable quotas if negative
|
|
quotaLogOnce.Do(func() {
|
|
lg.Info(
|
|
"disabled backend quota",
|
|
zap.String("quota-name", name),
|
|
zap.Int64("quota-size-bytes", quotaBackendBytesCfg),
|
|
)
|
|
})
|
|
return &passthroughQuota{}
|
|
}
|
|
|
|
if quotaBackendBytesCfg == 0 {
|
|
// use default size if no quota size given
|
|
quotaLogOnce.Do(func() {
|
|
if lg != nil {
|
|
lg.Info(
|
|
"enabled backend quota with default value",
|
|
zap.String("quota-name", name),
|
|
zap.Int64("quota-size-bytes", DefaultQuotaBytes),
|
|
zap.String("quota-size", DefaultQuotaSize),
|
|
)
|
|
}
|
|
})
|
|
quotaBackendBytes.Set(float64(DefaultQuotaBytes))
|
|
return &BackendQuota{be, DefaultQuotaBytes}
|
|
}
|
|
|
|
quotaLogOnce.Do(func() {
|
|
if quotaBackendBytesCfg > MaxQuotaBytes {
|
|
lg.Warn(
|
|
"quota exceeds the maximum value",
|
|
zap.String("quota-name", name),
|
|
zap.Int64("quota-size-bytes", quotaBackendBytesCfg),
|
|
zap.String("quota-size", humanize.Bytes(uint64(quotaBackendBytesCfg))),
|
|
zap.Int64("quota-maximum-size-bytes", MaxQuotaBytes),
|
|
zap.String("quota-maximum-size", maxQuotaSize),
|
|
)
|
|
}
|
|
lg.Info(
|
|
"enabled backend quota",
|
|
zap.String("quota-name", name),
|
|
zap.Int64("quota-size-bytes", quotaBackendBytesCfg),
|
|
zap.String("quota-size", humanize.Bytes(uint64(quotaBackendBytesCfg))),
|
|
)
|
|
})
|
|
return &BackendQuota{be, quotaBackendBytesCfg}
|
|
}
|
|
|
|
func (b *BackendQuota) Available(v interface{}) bool {
|
|
cost := b.Cost(v)
|
|
// if there are no mutating requests, it's safe to pass through
|
|
if cost == 0 {
|
|
return true
|
|
}
|
|
// TODO: maybe optimize Backend.Size()
|
|
|
|
// Since the compact comes with allocatable pages, we should check the
|
|
// SizeInUse first. If there is no continuous pages for key/value and
|
|
// the boltdb continues to resize, it should not increase more than 1
|
|
// GiB. It's hard limitation.
|
|
//
|
|
// TODO: It should be enabled by flag.
|
|
if b.be.Size()+int64(cost)-b.maxBackendBytes >= maxAllowedOverflowBytes(b.maxBackendBytes) {
|
|
return false
|
|
}
|
|
return b.be.SizeInUse()+int64(cost) < b.maxBackendBytes
|
|
}
|
|
|
|
func (b *BackendQuota) Cost(v interface{}) int {
|
|
switch r := v.(type) {
|
|
case *pb.PutRequest:
|
|
return costPut(r)
|
|
case *pb.TxnRequest:
|
|
return costTxn(r)
|
|
case *pb.LeaseGrantRequest:
|
|
return leaseOverhead
|
|
default:
|
|
panic("unexpected cost")
|
|
}
|
|
}
|
|
|
|
func costPut(r *pb.PutRequest) int { return kvOverhead + len(r.Key) + len(r.Value) }
|
|
|
|
func costTxnReq(u *pb.RequestOp) int {
|
|
r := u.GetRequestPut()
|
|
if r == nil {
|
|
return 0
|
|
}
|
|
return costPut(r)
|
|
}
|
|
|
|
func costTxn(r *pb.TxnRequest) int {
|
|
sizeSuccess := 0
|
|
for _, u := range r.Success {
|
|
sizeSuccess += costTxnReq(u)
|
|
}
|
|
sizeFailure := 0
|
|
for _, u := range r.Failure {
|
|
sizeFailure += costTxnReq(u)
|
|
}
|
|
if sizeFailure > sizeSuccess {
|
|
return sizeFailure
|
|
}
|
|
return sizeSuccess
|
|
}
|
|
|
|
func (b *BackendQuota) Remaining() int64 {
|
|
return b.maxBackendBytes - b.be.Size()
|
|
}
|
|
|
|
func maxAllowedOverflowBytes(maxBackendBytes int64) int64 {
|
|
allow := maxBackendBytes * 10 / 100
|
|
if allow > MaxAllowedOverflowQuotaBytes {
|
|
allow = MaxAllowedOverflowQuotaBytes
|
|
}
|
|
return allow
|
|
}
|