本文整理汇总了Golang中github.com/hashicorp/raft.SnapshotSink.Close方法的典型用法代码示例。如果您正苦于以下问题:Golang SnapshotSink.Close方法的具体用法?Golang SnapshotSink.Close怎么用?Golang SnapshotSink.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/hashicorp/raft.SnapshotSink
的用法示例。
在下文中一共展示了SnapshotSink.Close方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Persist
func (s *storeFSMSnapshot) Persist(sink raft.SnapshotSink) error {
err := func() error {
// Encode data.
p, err := s.Data.MarshalBinary()
if err != nil {
return err
}
// Write data to sink.
if _, err := sink.Write(p); err != nil {
return err
}
// Close the sink.
if err := sink.Close(); err != nil {
return err
}
return nil
}()
if err != nil {
sink.Cancel()
return err
}
return nil
}
示例2: Persist
// Persist writes a snapshot to a file. We just serialize all active entries.
func (s *Snapshot) Persist(sink raft.SnapshotSink) error {
_, err := sink.Write([]byte{snapSchemaVersion})
if err != nil {
sink.Cancel()
return err
}
for i, e := range s.entries {
_, err = sink.Write(s.uuids[i])
if err != nil {
sink.Cancel()
return err
}
b, err := e.ToBytes()
if err != nil {
sink.Cancel()
return err
}
_, err = sink.Write(uint32ToBytes(uint32(len(b))))
if err != nil {
sink.Cancel()
return err
}
_, err = sink.Write(b)
if err != nil {
sink.Cancel()
return err
}
}
return sink.Close()
}
示例3: Persist
func (f *fsmSnapshot) Persist(sink raft.SnapshotSink) error {
err := func() error {
// Encode data.
b, err := json.Marshal(f.store)
if err != nil {
return err
}
// Write data to sink.
if _, err := sink.Write(b); err != nil {
return err
}
// Close the sink.
if err := sink.Close(); err != nil {
return err
}
return nil
}()
if err != nil {
sink.Cancel()
return err
}
return nil
}
示例4: Persist
// See raft.SnapshotSink.
func (m *MockSnapshot) Persist(sink raft.SnapshotSink) error {
hd := codec.MsgpackHandle{}
enc := codec.NewEncoder(sink, &hd)
if err := enc.Encode(m.logs[:m.maxIndex]); err != nil {
sink.Cancel()
return err
}
sink.Close()
return nil
}
示例5: Persist
// Persist writes the snapshot to the sink.
func (ss *raftSnapshot) Persist(sink raft.SnapshotSink) error {
// Write data to sink.
if _, err := sink.Write(ss.data); err != nil {
sink.Cancel()
return err
}
// Close and exit.
return sink.Close()
}
示例6: Persist
// First, walk all kvs, write temp leveldb.
// Second, make tar.gz for temp leveldb dir
func (f *fsmSnapshot) Persist(sink raft.SnapshotSink) error {
// Create a temporary path for the state store
tmpPath, err := ioutil.TempDir(os.TempDir(), "state")
if err != nil {
return err
}
defer os.RemoveAll(tmpPath)
db, err := leveldb.OpenFile(tmpPath, nil)
if err != nil {
return err
}
iter := f.snapshot.NewIterator(nil, nil)
for iter.Next() {
err = db.Put(iter.Key(), iter.Value(), nil)
if err != nil {
db.Close()
sink.Cancel()
return err
}
}
iter.Release()
db.Close()
// make tar.gz
w := gzip.NewWriter(sink)
err = Tar(tmpPath, w)
if err != nil {
sink.Cancel()
return err
}
err = w.Close()
if err != nil {
sink.Cancel()
return err
}
sink.Close()
return nil
}
示例7: Persist
// Persist writes the snapshot to the give sink.
func (f *fsmSnapshot) Persist(sink raft.SnapshotSink) error {
err := func() error {
// Write data to sink.
if _, err := sink.Write(f.data); err != nil {
return err
}
// Close the sink.
if err := sink.Close(); err != nil {
return err
}
return nil
}()
if err != nil {
sink.Cancel()
return err
}
return nil
}
示例8: Persist
// Persist writes the snapshot to the given sink.
func (f *fsmSnapshot) Persist(sink raft.SnapshotSink) error {
err := func() error {
// Start by writing size of database.
b := new(bytes.Buffer)
sz := uint64(len(f.database))
err := binary.Write(b, binary.LittleEndian, sz)
if err != nil {
return err
}
if _, err := sink.Write(b.Bytes()); err != nil {
return err
}
// Next write database to sink.
if _, err := sink.Write(f.database); err != nil {
return err
}
// Finally write the meta.
if _, err := sink.Write(f.meta); err != nil {
return err
}
// Close the sink.
if err := sink.Close(); err != nil {
return err
}
return nil
}()
if err != nil {
sink.Cancel()
return err
}
return nil
}
示例9: Persist
func (s *storeFSM) Persist(sink raft.SnapshotSink) error {
defer sink.Close()
s.mu.Lock()
defer s.mu.Unlock()
return api.Encode(s.data, sink)
}
示例10: Persist
//.........这里部分代码省略.........
// TODO: would it be okay to just mark them for deletion? i.e., do all messages that modify state return a result?
if len(vmsgs) == 0 {
s.sliced[parsed.Session.Id] = append(s.sliced[parsed.Session.Id], &parsed)
}
}
}
log.Printf("got %d sessions\n", len(sessions))
for session := range sessions {
log.Printf("session 0x%x has %d messages\n", session.Id, len(s.sliced[session.Id]))
}
// We repeatedly compact, since the result of one compaction can affect the
// result of other compactions (see compaction_test.go for examples).
changed := true
pass := 0
for changed {
log.Printf("Compaction pass %d\n", pass)
pass++
changed = false
for i := s.firstIndex; i <= s.lastIndex; i++ {
if i%1000 == 0 {
log.Printf("message %d of %d (%.0f%%)\n",
i, s.lastIndex, (float64(i)/float64(s.lastIndex))*100.0)
}
if s.del[i] {
continue
}
msg, ok := s.parsed[i]
if !ok {
continue
}
if compactionStart.Sub(time.Unix(0, msg.Id.Id)) < 7*24*time.Hour {
// If we ran outside the window, we don’t even need to look at
// any newer messages anymore.
break
}
session := msg.Session
if msg.Type == types.RobustCreateSession {
session = msg.Id
}
canCompact, slicedIdx, err := s.canCompact(session, msg, i)
if err != nil {
sink.Cancel()
return err
}
if canCompact {
s.del[i] = true
if slicedIdx != -1 {
s.sliced[session.Id][slicedIdx] = nil
}
changed = true
}
}
}
encoder := json.NewEncoder(sink)
for i := s.firstIndex; i <= s.lastIndex; i++ {
if s.del[i] {
continue
}
var elog raft.Log
if err := s.store.GetLog(i, &elog); err != nil {
continue
}
if err := encoder.Encode(elog); err != nil {
sink.Cancel()
return err
}
}
sink.Close()
for idx, del := range s.del {
if !del {
continue
}
nmsg, ok := s.parsed[idx]
// If the message was not found in parsed, then there was no message
// with this index, hence there is nothing to delete.
if !ok {
continue
}
// TODO: Since outputstream uses a LevelDB database, we could be more
// efficient and use batch deletions.
if err := ircServer.Delete(nmsg.Id); err != nil {
log.Panicf("Could not delete outputstream message: %v\n", err)
}
s.store.DeleteRange(idx, idx)
}
return nil
}