blob: 082a33f0e5d01dbb9a01bc2c7544476ea18ae87c [file] [log] [blame]
khenaidood948f772021-08-11 17:49:24 -04001// Copyright 2015 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
15package mvcc
16
17import (
18 "encoding/binary"
19 "time"
20)
21
22func (s *store) scheduleCompaction(compactMainRev int64, keep map[revision]struct{}) bool {
23 totalStart := time.Now()
24 defer func() { dbCompactionTotalDurations.Observe(float64(time.Since(totalStart) / time.Millisecond)) }()
25 keyCompactions := 0
26 defer func() { dbCompactionKeysCounter.Add(float64(keyCompactions)) }()
27
28 end := make([]byte, 8)
29 binary.BigEndian.PutUint64(end, uint64(compactMainRev+1))
30
31 batchsize := int64(10000)
32 last := make([]byte, 8+1+8)
33 for {
34 var rev revision
35
36 start := time.Now()
37 tx := s.b.BatchTx()
38 tx.Lock()
39
40 keys, _ := tx.UnsafeRange(keyBucketName, last, end, batchsize)
41 for _, key := range keys {
42 rev = bytesToRev(key)
43 if _, ok := keep[rev]; !ok {
44 tx.UnsafeDelete(keyBucketName, key)
45 keyCompactions++
46 }
47 }
48
49 if len(keys) < int(batchsize) {
50 rbytes := make([]byte, 8+1+8)
51 revToBytes(revision{main: compactMainRev}, rbytes)
52 tx.UnsafePut(metaBucketName, finishedCompactKeyName, rbytes)
53 tx.Unlock()
54 plog.Printf("finished scheduled compaction at %d (took %v)", compactMainRev, time.Since(totalStart))
55 return true
56 }
57
58 // update last
59 revToBytes(revision{main: rev.main, sub: rev.sub + 1}, last)
60 tx.Unlock()
61 dbCompactionPauseDurations.Observe(float64(time.Since(start) / time.Millisecond))
62
63 select {
64 case <-time.After(100 * time.Millisecond):
65 case <-s.stopc:
66 return false
67 }
68 }
69}