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"
|
2018-04-16 13:58:15 +03:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
2015-05-31 18:59:31 +03:00
|
|
|
)
|
|
|
|
|
2016-04-01 20:59:28 +03:00
|
|
|
func (s *store) scheduleCompaction(compactMainRev int64, keep map[revision]struct{}) bool {
|
2015-08-28 20:35:30 +03:00
|
|
|
totalStart := time.Now()
|
2019-04-15 09:44:55 +03:00
|
|
|
defer func() { dbCompactionTotalMs.Observe(float64(time.Since(totalStart) / time.Millisecond)) }()
|
2017-07-19 20:00:57 +03:00
|
|
|
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))
|
|
|
|
|
|
|
|
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()
|
2019-08-14 11:56:42 +03:00
|
|
|
|
2015-05-31 18:59:31 +03:00
|
|
|
tx := s.b.BatchTx()
|
|
|
|
tx.Lock()
|
2019-08-14 11:56:42 +03:00
|
|
|
keys, _ := tx.UnsafeRange(keyBucketName, last, end, int64(s.cfg.CompactionBatchLimit))
|
2015-05-31 18:59:31 +03:00
|
|
|
for _, key := range keys {
|
|
|
|
rev = bytesToRev(key)
|
|
|
|
if _, ok := keep[rev]; !ok {
|
|
|
|
tx.UnsafeDelete(keyBucketName, key)
|
2019-11-27 12:55:42 +03:00
|
|
|
keyCompactions++
|
2015-05-31 18:59:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-14 11:56:42 +03:00
|
|
|
if len(keys) < s.cfg.CompactionBatchLimit {
|
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)
|
2015-06-17 23:52:17 +03:00
|
|
|
tx.UnsafePut(metaBucketName, finishedCompactKeyName, rbytes)
|
2015-05-31 18:59:31 +03:00
|
|
|
tx.Unlock()
|
2020-02-05 19:18:15 +03:00
|
|
|
s.lg.Info(
|
|
|
|
"finished scheduled compaction",
|
|
|
|
zap.Int64("compact-revision", compactMainRev),
|
|
|
|
zap.Duration("took", time.Since(totalStart)),
|
|
|
|
)
|
2016-04-01 20:59:28 +03:00
|
|
|
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()
|
2019-08-14 11:56:42 +03:00
|
|
|
// Immediately commit the compaction deletes instead of letting them accumulate in the write buffer
|
2019-08-14 10:09:47 +03:00
|
|
|
s.b.ForceCommit()
|
2018-05-23 23:47:52 +03:00
|
|
|
dbCompactionPauseMs.Observe(float64(time.Since(start) / time.Millisecond))
|
2015-05-31 18:59:31 +03:00
|
|
|
|
2015-06-18 21:05:31 +03:00
|
|
|
select {
|
2019-08-14 10:09:47 +03:00
|
|
|
case <-time.After(10 * time.Millisecond):
|
2015-06-18 21:05:31 +03:00
|
|
|
case <-s.stopc:
|
2016-04-01 20:59:28 +03:00
|
|
|
return false
|
2015-06-18 21:05:31 +03:00
|
|
|
}
|
2015-05-31 18:59:31 +03:00
|
|
|
}
|
|
|
|
}
|