Example #1
0
func (fs *FragmentStorage) CreateSnapshot(i uint64, cs *pb.ConfState, data []byte) (pb.Snapshot, error) {
	fs.Lock()
	defer fs.Unlock()
	snapshot := pb.Snapshot{}
	preSnapshot, err := fs.Snapshot()
	if err != nil {
		return snapshot, err
	}
	if i <= preSnapshot.Metadata.Index {
		return pb.Snapshot{}, ErrSnapOutOfDate
	}
	lastIndex, err := fs.LastIndex()
	if err != nil {
		return snapshot, err
	}
	if i > lastIndex {
		fmt.Errorf("snapshot %d is out of bound lastindex(%d)", i, lastIndex)
	}
	snapshot.Metadata.Index = i
	if en, err := fs.Entry(i); err != nil {
		return snapshot, err
	} else {
		snapshot.Metadata.Term = en.Term
	}
	if cs != nil {
		snapshot.Metadata.ConfState = *cs
	}
	snapshot.Data = data
	return snapshot, nil

}
Example #2
0
func (fs *FragmentStorage) Snapshot() (pb.Snapshot, error) {
	snapshot := pb.Snapshot{}
	r := bufio.NewReader(fs.fsnapshot)
	data, err := r.ReadSlice('\n')
	if err != nil {
		return snapshot, err
	}
	data = data[:len(data)-1]
	return snapshot, snapshot.Unmarshal(data)
}
Example #3
0
// ApplySnapshot overwrites the contents of this Storage object with
// those of the given snapshot.
func (fs *FragmentStorage) ApplySnapshot(snap pb.Snapshot) error {
	fs.Lock()
	defer fs.Unlock()
	data, err := snap.Marshal()
	if err != nil {
		return err
	}
	fs.fsnapshot.Seek(0, 0)
	_, err = fmt.Fprintln(fs.fsnapshot, data)
	if err != nil {
		return err
	}
	entries := []pb.Entry{{Term: snap.Metadata.Term, Index: snap.Metadata.Index}}
	return fs.Append(entries)
}