...

Source file src/go.etcd.io/etcd/client/v3/snapshot/v3_snapshot.go

Documentation: go.etcd.io/etcd/client/v3/snapshot

     1  // Copyright 2018 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 snapshot
    16  
    17  import (
    18  	"context"
    19  	"crypto/sha256"
    20  	"fmt"
    21  	"io"
    22  	"os"
    23  	"time"
    24  
    25  	"github.com/dustin/go-humanize"
    26  	"go.etcd.io/etcd/client/pkg/v3/fileutil"
    27  	"go.etcd.io/etcd/client/v3"
    28  	"go.uber.org/zap"
    29  )
    30  
    31  // hasChecksum returns "true" if the file size "n"
    32  // has appended sha256 hash digest.
    33  func hasChecksum(n int64) bool {
    34  	// 512 is chosen because it's a minimum disk sector size
    35  	// smaller than (and multiplies to) OS page size in most systems
    36  	return (n % 512) == sha256.Size
    37  }
    38  
    39  // Save fetches snapshot from remote etcd server and saves data
    40  // to target path. If the context "ctx" is canceled or timed out,
    41  // snapshot save stream will error out (e.g. context.Canceled,
    42  // context.DeadlineExceeded). Make sure to specify only one endpoint
    43  // in client configuration. Snapshot API must be requested to a
    44  // selected node, and saved snapshot is the point-in-time state of
    45  // the selected node.
    46  func Save(ctx context.Context, lg *zap.Logger, cfg clientv3.Config, dbPath string) error {
    47  	cfg.Logger = lg.Named("client")
    48  	if len(cfg.Endpoints) != 1 {
    49  		return fmt.Errorf("snapshot must be requested to one selected node, not multiple %v", cfg.Endpoints)
    50  	}
    51  	cli, err := clientv3.New(cfg)
    52  	if err != nil {
    53  		return err
    54  	}
    55  	defer cli.Close()
    56  
    57  	partpath := dbPath + ".part"
    58  	defer os.RemoveAll(partpath)
    59  
    60  	var f *os.File
    61  	f, err = os.OpenFile(partpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, fileutil.PrivateFileMode)
    62  	if err != nil {
    63  		return fmt.Errorf("could not open %s (%v)", partpath, err)
    64  	}
    65  	lg.Info("created temporary db file", zap.String("path", partpath))
    66  
    67  	now := time.Now()
    68  	var rd io.ReadCloser
    69  	rd, err = cli.Snapshot(ctx)
    70  	if err != nil {
    71  		return err
    72  	}
    73  	lg.Info("fetching snapshot", zap.String("endpoint", cfg.Endpoints[0]))
    74  	var size int64
    75  	size, err = io.Copy(f, rd)
    76  	if err != nil {
    77  		return err
    78  	}
    79  	if !hasChecksum(size) {
    80  		return fmt.Errorf("sha256 checksum not found [bytes: %d]", size)
    81  	}
    82  	if err = fileutil.Fsync(f); err != nil {
    83  		return err
    84  	}
    85  	if err = f.Close(); err != nil {
    86  		return err
    87  	}
    88  	lg.Info("fetched snapshot",
    89  		zap.String("endpoint", cfg.Endpoints[0]),
    90  		zap.String("size", humanize.Bytes(uint64(size))),
    91  		zap.String("took", humanize.Time(now)),
    92  	)
    93  
    94  	if err = os.Rename(partpath, dbPath); err != nil {
    95  		return fmt.Errorf("could not rename %s to %s (%v)", partpath, dbPath, err)
    96  	}
    97  	lg.Info("saved", zap.String("path", dbPath))
    98  	return nil
    99  }
   100  

View as plain text