...

Source file src/go.etcd.io/etcd/server/v3/wal/util.go

Documentation: go.etcd.io/etcd/server/v3/wal

     1  // Copyright 2015 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 wal
    16  
    17  import (
    18  	"errors"
    19  	"fmt"
    20  	"strings"
    21  
    22  	"go.etcd.io/etcd/client/pkg/v3/fileutil"
    23  
    24  	"go.uber.org/zap"
    25  )
    26  
    27  var errBadWALName = errors.New("bad wal name")
    28  
    29  // Exist returns true if there are any files in a given directory.
    30  func Exist(dir string) bool {
    31  	names, err := fileutil.ReadDir(dir, fileutil.WithExt(".wal"))
    32  	if err != nil {
    33  		return false
    34  	}
    35  	return len(names) != 0
    36  }
    37  
    38  // searchIndex returns the last array index of names whose raft index section is
    39  // equal to or smaller than the given index.
    40  // The given names MUST be sorted.
    41  func searchIndex(lg *zap.Logger, names []string, index uint64) (int, bool) {
    42  	for i := len(names) - 1; i >= 0; i-- {
    43  		name := names[i]
    44  		_, curIndex, err := parseWALName(name)
    45  		if err != nil {
    46  			lg.Panic("failed to parse WAL file name", zap.String("path", name), zap.Error(err))
    47  		}
    48  		if index >= curIndex {
    49  			return i, true
    50  		}
    51  	}
    52  	return -1, false
    53  }
    54  
    55  // names should have been sorted based on sequence number.
    56  // isValidSeq checks whether seq increases continuously.
    57  func isValidSeq(lg *zap.Logger, names []string) bool {
    58  	var lastSeq uint64
    59  	for _, name := range names {
    60  		curSeq, _, err := parseWALName(name)
    61  		if err != nil {
    62  			lg.Panic("failed to parse WAL file name", zap.String("path", name), zap.Error(err))
    63  		}
    64  		if lastSeq != 0 && lastSeq != curSeq-1 {
    65  			return false
    66  		}
    67  		lastSeq = curSeq
    68  	}
    69  	return true
    70  }
    71  
    72  func readWALNames(lg *zap.Logger, dirpath string) ([]string, error) {
    73  	names, err := fileutil.ReadDir(dirpath)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  	wnames := checkWalNames(lg, names)
    78  	if len(wnames) == 0 {
    79  		return nil, ErrFileNotFound
    80  	}
    81  	return wnames, nil
    82  }
    83  
    84  func checkWalNames(lg *zap.Logger, names []string) []string {
    85  	wnames := make([]string, 0)
    86  	for _, name := range names {
    87  		if _, _, err := parseWALName(name); err != nil {
    88  			// don't complain about left over tmp files
    89  			if !strings.HasSuffix(name, ".tmp") {
    90  				lg.Warn(
    91  					"ignored file in WAL directory",
    92  					zap.String("path", name),
    93  				)
    94  			}
    95  			continue
    96  		}
    97  		wnames = append(wnames, name)
    98  	}
    99  	return wnames
   100  }
   101  
   102  func parseWALName(str string) (seq, index uint64, err error) {
   103  	if !strings.HasSuffix(str, ".wal") {
   104  		return 0, 0, errBadWALName
   105  	}
   106  	_, err = fmt.Sscanf(str, "%016x-%016x.wal", &seq, &index)
   107  	return seq, index, err
   108  }
   109  
   110  func walName(seq, index uint64) string {
   111  	return fmt.Sprintf("%016x-%016x.wal", seq, index)
   112  }
   113  

View as plain text