compactor.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. "fmt"
  17. "time"
  18. "github.com/coreos/pkg/capnslog"
  19. "golang.org/x/net/context"
  20. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  21. )
  22. var (
  23. plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "compactor")
  24. )
  25. const (
  26. checkCompactionInterval = 5 * time.Minute
  27. executeCompactionInterval = time.Hour
  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. func New(mode string, retention int, rg RevGetter, c Compactable) (Compactor, error) {
  50. switch mode {
  51. case ModePeriodic:
  52. return NewPeriodic(retention, rg, c), nil
  53. case ModeRevision:
  54. return NewRevision(int64(retention), rg, c), nil
  55. default:
  56. return nil, fmt.Errorf("unsupported compaction mode %s", mode)
  57. }
  58. }