compactor.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. checkCompactionInterval = 5 * time.Minute
  27. ModePeriodic = "periodic"
  28. ModeRevision = "revision"
  29. )
  30. // Compactor purges old log from the storage periodically.
  31. type Compactor interface {
  32. // Run starts the main loop of the compactor in background.
  33. // Use Stop() to halt the loop and release the resource.
  34. Run()
  35. // Stop halts the main loop of the compactor.
  36. Stop()
  37. // Pause temporally suspend the compactor not to run compaction. Resume() to unpose.
  38. Pause()
  39. // Resume restarts the compactor suspended by Pause().
  40. Resume()
  41. }
  42. type Compactable interface {
  43. Compact(ctx context.Context, r *pb.CompactionRequest) (*pb.CompactionResponse, error)
  44. }
  45. type RevGetter interface {
  46. Rev() int64
  47. }
  48. func New(mode string, retention time.Duration, rg RevGetter, c Compactable) (Compactor, error) {
  49. switch mode {
  50. case ModePeriodic:
  51. return NewPeriodic(retention, rg, c), nil
  52. case ModeRevision:
  53. return NewRevision(int64(retention), rg, c), nil
  54. default:
  55. return nil, fmt.Errorf("unsupported compaction mode %s", mode)
  56. }
  57. }