blob: 8100b69385adb542ad4a37a36a47922c61716a0a [file] [log] [blame]
khenaidood948f772021-08-11 17:49:24 -04001// Copyright 2016 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 compactor
16
17import (
18 "context"
19 "fmt"
20 "time"
21
22 pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
23
24 "github.com/coreos/pkg/capnslog"
25)
26
27var (
28 plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "compactor")
29)
30
31const (
32 ModePeriodic = "periodic"
33 ModeRevision = "revision"
34)
35
36// Compactor purges old log from the storage periodically.
37type Compactor interface {
38 // Run starts the main loop of the compactor in background.
39 // Use Stop() to halt the loop and release the resource.
40 Run()
41 // Stop halts the main loop of the compactor.
42 Stop()
43 // Pause temporally suspend the compactor not to run compaction. Resume() to unpose.
44 Pause()
45 // Resume restarts the compactor suspended by Pause().
46 Resume()
47}
48
49type Compactable interface {
50 Compact(ctx context.Context, r *pb.CompactionRequest) (*pb.CompactionResponse, error)
51}
52
53type RevGetter interface {
54 Rev() int64
55}
56
57func New(mode string, retention time.Duration, rg RevGetter, c Compactable) (Compactor, error) {
58 switch mode {
59 case ModePeriodic:
60 return NewPeriodic(retention, rg, c), nil
61 case ModeRevision:
62 return NewRevision(int64(retention), rg, c), nil
63 default:
64 return nil, fmt.Errorf("unsupported compaction mode %s", mode)
65 }
66}