...

Source file src/go.etcd.io/etcd/server/v3/etcdserver/api/v3compactor/compactor.go

Documentation: go.etcd.io/etcd/server/v3/etcdserver/api/v3compactor

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

View as plain text