blob: 73a96842d1c2b904e12cff1bbd085f28d4d8308c [file] [log] [blame]
khenaidooab1f7bd2019-11-14 14:00:27 -05001// 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 v3compactor
16
17import (
18 "context"
19 "fmt"
20 "time"
21
22 pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
23
24 "github.com/coreos/pkg/capnslog"
25 "github.com/jonboulle/clockwork"
26 "go.uber.org/zap"
27)
28
29var (
30 plog = capnslog.NewPackageLogger("go.etcd.io/etcd", "compactor")
31)
32
33const (
34 ModePeriodic = "periodic"
35 ModeRevision = "revision"
36)
37
38// Compactor purges old log from the storage periodically.
39type Compactor interface {
40 // Run starts the main loop of the compactor in background.
41 // Use Stop() to halt the loop and release the resource.
42 Run()
43 // Stop halts the main loop of the compactor.
44 Stop()
45 // Pause temporally suspend the compactor not to run compaction. Resume() to unpose.
46 Pause()
47 // Resume restarts the compactor suspended by Pause().
48 Resume()
49}
50
51type Compactable interface {
52 Compact(ctx context.Context, r *pb.CompactionRequest) (*pb.CompactionResponse, error)
53}
54
55type RevGetter interface {
56 Rev() int64
57}
58
59// New returns a new Compactor based on given "mode".
60func New(
61 lg *zap.Logger,
62 mode string,
63 retention time.Duration,
64 rg RevGetter,
65 c Compactable,
66) (Compactor, error) {
67 switch mode {
68 case ModePeriodic:
69 return newPeriodic(lg, clockwork.NewRealClock(), retention, rg, c), nil
70 case ModeRevision:
71 return newRevision(lg, clockwork.NewRealClock(), int64(retention), rg, c), nil
72 default:
73 return nil, fmt.Errorf("unsupported compaction mode %s", mode)
74 }
75}