compactor.go 1.9 KB

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