etcdserver/api/snap/db.go
func (s *Snapshotter) SaveDBFrom(r io.Reader, id uint64) (int64, error) {
	start := time.Now()

	f, err := ioutil.TempFile(s.dir, "tmp")
	if err != nil {
		return 0, err
	}
	var n int64
	n, err = io.Copy(f, r)
	if err == nil {
		fsyncStart := time.Now()
		err = fileutil.Fsync(f)
		snapDBFsyncSec.Observe(time.Since(fsyncStart).Seconds())
	}
	f.Close()
	if err != nil {
		os.Remove(f.Name())
		return n, err
	}
	fn := s.dbFilePath(id)
	if fileutil.Exist(fn) {
		os.Remove(f.Name())
		return n, nil
	}
	err = os.Rename(f.Name(), fn)
	if err != nil {
		os.Remove(f.Name())
		return n, err
	}

	if s.lg != nil {
		s.lg.Info(
			"saved database snapshot to disk",
			zap.String("path", fn),
			zap.Int64("bytes", n),
			zap.String("size", humanize.Bytes(uint64(n))),
		)
	} else {
		plog.Infof("saved database snapshot to disk [total bytes: %d]", n)
	}

	snapDBSaveSec.Observe(time.Since(start).Seconds())
	return n, nil
}
func (s *Snapshotter) SaveDBFrom(r io.Reader, id uint64) (int64, error) {
	start := time.Now()

	f, err := ioutil.TempFile(s.dir, "tmp")
	if err != nil {
		return 0, err
	}
	var n int64
	n, err = io.Copy(f, r)
	if err == nil {
		fsyncStart := time.Now()
		err = fileutil.Fsync(f)
		snapDBFsyncSec.Observe(time.Since(fsyncStart).Seconds())
	}
	f.Close()
	if err != nil {
		os.Remove(f.Name())
		return n, err
	}
	fn := s.dbFilePath(id)
	if fileutil.Exist(fn) {
		os.Remove(f.Name())
		return n, nil
	}
	err = os.Rename(f.Name(), fn)
	if err != nil {
		os.Remove(f.Name())
		return n, err
	}

	if s.lg != nil {
		s.lg.Info(
			"saved database snapshot to disk",
			zap.String("path", fn),
			zap.Int64("bytes", n),
			zap.String("size", humanize.Bytes(uint64(n))),
		)
	} else {
		plog.Infof("saved database snapshot to disk [total bytes: %d]", n)
	}

	snapDBSaveSec.Observe(time.Since(start).Seconds())
	return n, nil
}
func ( s * Snapshotter ) SaveDBFrom ( r io . Reader , id uint64 ) ( int64 , error ) { start := time . Now ( ) f , err := ioutil . TempFile ( s . dir , " " ) if err != nil { return 0 , err } var n int64 n , err = io . Copy ( f , r ) if err == nil { fsyncStart := time . Now ( ) err = fileutil . Fsync ( f ) snapDBFsyncSec . Observe ( time . Since ( fsyncStart ) . Seconds ( ) ) } f . Close ( ) if err != nil { os . Remove ( f . Name ( ) ) return n , err } fn := s . dbFilePath ( id ) if fileutil . Exist ( fn ) { os . Remove ( f . Name ( ) ) return n , nil } err = os . Rename ( f . Name ( ) , fn ) if err != nil { os . Remove ( f . Name ( ) ) return n , err } if s . lg != nil { s . lg . Info ( " " , zap . String ( " " , fn ) , zap . Int64 ( " " , n ) , zap . String ( " " , humanize . Bytes ( uint64 ( n ) ) ) , ) } else { plog . Infof ( " " , n ) } snapDBSaveSec . Observe ( time . Since ( start ) . Seconds ( ) ) return n , nil }
// SaveDBFrom saves snapshot of the database from the given reader. It
// guarantees the save operation is atomic.
SaveDBFrom saves snapshot of the database from the given reader . It guarantees the save operation is atomic .