etcd/mvcc/kvstore_compaction.go

80 lines
2.2 KiB
Go
Raw Normal View History

2016-05-13 06:50:33 +03:00
// Copyright 2015 The etcd Authors
2015-09-15 23:54:11 +03:00
//
// 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.
2016-04-25 22:32:58 +03:00
package mvcc
2015-05-31 18:59:31 +03:00
import (
"encoding/binary"
"time"
"go.uber.org/zap"
2015-05-31 18:59:31 +03:00
)
func (s *store) scheduleCompaction(compactMainRev int64, keep map[revision]struct{}) bool {
2015-08-28 20:35:30 +03:00
totalStart := time.Now()
defer dbCompactionTotalMs.Observe(float64(time.Since(totalStart) / time.Millisecond))
keyCompactions := 0
defer func() { dbCompactionKeysCounter.Add(float64(keyCompactions)) }()
2015-08-28 20:35:30 +03:00
2015-05-31 18:59:31 +03:00
end := make([]byte, 8)
binary.BigEndian.PutUint64(end, uint64(compactMainRev+1))
batchsize := int64(10000)
last := make([]byte, 8+1+8)
for {
2015-08-20 18:39:07 +03:00
var rev revision
2015-05-31 18:59:31 +03:00
2015-08-28 20:35:30 +03:00
start := time.Now()
2015-05-31 18:59:31 +03:00
tx := s.b.BatchTx()
tx.Lock()
keys, _ := tx.UnsafeRange(keyBucketName, last, end, batchsize)
for _, key := range keys {
rev = bytesToRev(key)
if _, ok := keep[rev]; !ok {
tx.UnsafeDelete(keyBucketName, key)
keyCompactions++
2015-05-31 18:59:31 +03:00
}
}
if len(keys) < int(batchsize) {
2015-05-31 18:59:31 +03:00
rbytes := make([]byte, 8+1+8)
2015-08-20 18:39:07 +03:00
revToBytes(revision{main: compactMainRev}, rbytes)
tx.UnsafePut(metaBucketName, finishedCompactKeyName, rbytes)
2015-05-31 18:59:31 +03:00
tx.Unlock()
if s.lg != nil {
s.lg.Info(
"finished scheduled compaction",
zap.Int64("compact-revision", compactMainRev),
zap.Duration("took", time.Since(totalStart)),
)
} else {
plog.Printf("finished scheduled compaction at %d (took %v)", compactMainRev, time.Since(totalStart))
}
return true
2015-05-31 18:59:31 +03:00
}
// update last
2015-08-20 18:39:07 +03:00
revToBytes(revision{main: rev.main, sub: rev.sub + 1}, last)
2015-05-31 18:59:31 +03:00
tx.Unlock()
dbCompactionPauseMs.Observe(float64(time.Since(start) / time.Millisecond))
2015-05-31 18:59:31 +03:00
select {
case <-time.After(100 * time.Millisecond):
case <-s.stopc:
return false
}
2015-05-31 18:59:31 +03:00
}
}