compactor.go 1.8 KB

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