func (s *nomadSnapshot) persistIndexes(sink raft.SnapshotSink, encoder *codec.Encoder) error { // Get all the indexes iter, err := s.snap.Indexes() if err != nil { return err } for { // Get the next item raw := iter.Next() if raw == nil { break } // Prepare the request struct idx := raw.(*state.IndexEntry) // Write out a node registration sink.Write([]byte{byte(IndexSnapshot)}) if err := encoder.Encode(idx); err != nil { return err } } return nil }
func (s *nomadSnapshot) persistEvals(sink raft.SnapshotSink, encoder *codec.Encoder) error { // Get all the evaluations evals, err := s.snap.Evals() if err != nil { return err } for { // Get the next item raw := evals.Next() if raw == nil { break } // Prepare the request struct eval := raw.(*structs.Evaluation) // Write out the evaluation sink.Write([]byte{byte(EvalSnapshot)}) if err := encoder.Encode(eval); err != nil { return err } } return nil }
func (s *nomadSnapshot) persistJobs(sink raft.SnapshotSink, encoder *codec.Encoder) error { // Get all the jobs jobs, err := s.snap.Jobs() if err != nil { return err } for { // Get the next item raw := jobs.Next() if raw == nil { break } // Prepare the request struct job := raw.(*structs.Job) // Write out a job registration sink.Write([]byte{byte(JobSnapshot)}) if err := encoder.Encode(job); err != nil { return err } } return nil }
func (s *consulSnapshot) persistTombstones(sink raft.SnapshotSink, encoder *codec.Encoder) error { stones, err := s.state.Tombstones() if err != nil { return err } for stone := stones.Next(); stone != nil; stone = stones.Next() { sink.Write([]byte{byte(structs.TombstoneRequestType)}) // For historical reasons, these are serialized in the snapshots // as KV entries. We want to keep the snapshot format compatible // with pre-0.6 versions for now. s := stone.(*state.Tombstone) fake := &structs.DirEntry{ Key: s.Key, RaftIndex: structs.RaftIndex{ ModifyIndex: s.Index, }, } if err := encoder.Encode(fake); err != nil { return err } } return nil }
func (s *nomadSnapshot) persistPeriodicLaunches(sink raft.SnapshotSink, encoder *codec.Encoder) error { // Get all the jobs launches, err := s.snap.PeriodicLaunches() if err != nil { return err } for { // Get the next item raw := launches.Next() if raw == nil { break } // Prepare the request struct launch := raw.(*structs.PeriodicLaunch) // Write out a job registration sink.Write([]byte{byte(PeriodicLaunchSnapshot)}) if err := encoder.Encode(launch); err != nil { return err } } return nil }
func (s *consulSnapshot) Persist(sink raft.SnapshotSink) error { // Register the nodes encoder := codec.NewEncoder(sink, msgpackHandle) // Write the header header := snapshotHeader{ LastIndex: s.state.LastIndex(), } if err := encoder.Encode(&header); err != nil { sink.Cancel() return err } if err := s.persistNodes(sink, encoder); err != nil { sink.Cancel() return err } if err := s.persistSessions(sink, encoder); err != nil { sink.Cancel() return err } if err := s.persistACLs(sink, encoder); err != nil { sink.Cancel() return err } if err := s.persistKV(sink, encoder); err != nil { sink.Cancel() return err } return nil }
func (s *consulSnapshot) persistKV(sink raft.SnapshotSink, encoder *codec.Encoder) error { streamCh := make(chan interface{}, 256) errorCh := make(chan error) go func() { if err := s.state.KVSDump(streamCh); err != nil { errorCh <- err } }() for { select { case raw := <-streamCh: if raw == nil { return nil } sink.Write([]byte{byte(structs.KVSRequestType)}) if err := encoder.Encode(raw); err != nil { return err } case err := <-errorCh: return err } } return nil }
func (s *nomadSnapshot) persistNodes(sink raft.SnapshotSink, encoder *codec.Encoder) error { // Get all the nodes nodes, err := s.snap.Nodes() if err != nil { return err } for { // Get the next item raw := nodes.Next() if raw == nil { break } // Prepare the request struct node := raw.(*structs.Node) // Write out a node registration sink.Write([]byte{byte(NodeSnapshot)}) if err := encoder.Encode(node); err != nil { return err } } return nil }
func (s *nomadSnapshot) persistAllocs(sink raft.SnapshotSink, encoder *codec.Encoder) error { // Get all the allocations allocs, err := s.snap.Allocs() if err != nil { return err } for { // Get the next item raw := allocs.Next() if raw == nil { break } // Prepare the request struct alloc := raw.(*structs.Allocation) // Write out the evaluation sink.Write([]byte{byte(AllocSnapshot)}) if err := encoder.Encode(alloc); err != nil { return err } } return nil }
func (snap *masterSnapshot) Persist(sink raft.SnapshotSink) error { data, _ := json.Marshal(snap.data) _, err := sink.Write(data) if err != nil { sink.Cancel() } return err }
// 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 }
func (fsm *fsm) Persist(sink raft.SnapshotSink) error { fsm.Lock() defer fsm.Unlock() data, _ := json.Marshal(fsm) _, err := sink.Write(data) if err != nil { sink.Cancel() } return err }
func (mfsm *MyFsm) Persist(sink raft.SnapshotSink) error { mfsm.mutex.Lock() defer mfsm.mutex.Unlock() var buffer bytes.Buffer enc := gob.NewEncoder(&buffer) enc.Encode(mfsm.data) n, err := sink.Write(buffer.Bytes()) if err != nil { fmt.Printf("Error in snapshotting %s\n", err) return err } fmt.Printf("Snapshotted %d bytes", n) return nil }
func (s *consulSnapshot) persistPreparedQueries(sink raft.SnapshotSink, encoder *codec.Encoder) error { queries, err := s.state.PreparedQueries() if err != nil { return err } for _, query := range queries { sink.Write([]byte{byte(structs.PreparedQueryRequestType)}) if err := encoder.Encode(query); err != nil { return err } } return nil }
func (s *consulSnapshot) persistKVs(sink raft.SnapshotSink, encoder *codec.Encoder) error { entries, err := s.state.KVs() if err != nil { return err } for entry := entries.Next(); entry != nil; entry = entries.Next() { sink.Write([]byte{byte(structs.KVSRequestType)}) if err := encoder.Encode(entry.(*structs.DirEntry)); err != nil { return err } } return nil }
func (s *consulSnapshot) persistSessions(sink raft.SnapshotSink, encoder *codec.Encoder) error { sessions, err := s.state.SessionList() if err != nil { return err } for _, s := range sessions { sink.Write([]byte{byte(structs.SessionRequestType)}) if err := encoder.Encode(s); err != nil { return err } } return nil }
func (s *consulSnapshot) persistSessions(sink raft.SnapshotSink, encoder *codec.Encoder) error { sessions, err := s.state.Sessions() if err != nil { return err } for session := sessions.Next(); session != nil; session = sessions.Next() { sink.Write([]byte{byte(structs.SessionRequestType)}) if err := encoder.Encode(session.(*structs.Session)); err != nil { return err } } return nil }
func (s *consulSnapshot) persistACLs(sink raft.SnapshotSink, encoder *codec.Encoder) error { acls, err := s.state.ACLs() if err != nil { return err } for acl := acls.Next(); acl != nil; acl = acls.Next() { sink.Write([]byte{byte(structs.ACLRequestType)}) if err := encoder.Encode(acl.(*structs.ACL)); err != nil { return err } } return nil }
func (s *consulSnapshot) persistACLs(sink raft.SnapshotSink, encoder *codec.Encoder) error { acls, err := s.state.ACLList() if err != nil { return err } for _, s := range acls { sink.Write([]byte{byte(structs.ACLRequestType)}) if err := encoder.Encode(s); err != nil { return err } } return nil }
// Persist writes a robustSnapshot to disk, i.e. handles the // serialization details. func (s *robustSnapshot) Persist(sink raft.SnapshotSink) error { stateMsg := types.RobustMessage{ Type: types.RobustState, Data: base64.StdEncoding.EncodeToString(s.state), // TODO: find a more straight-forward way to encode this } stateMsgJson, err := json.Marshal(&stateMsg) if err != nil { return err } stateMsgRaftJson, err := json.Marshal(&raft.Log{ Type: raft.LogCommand, Index: 0, // never passed to raft. Data: stateMsgJson, }) if err != nil { return err } log.Printf("Copying non-deleted messages into snapshot\n") snapshotBytes, err := sink.Write(stateMsgRaftJson) if err != nil { return err } iterator := s.store.GetBulkIterator(s.firstIndex, s.lastIndex+1) defer iterator.Release() available := iterator.First() for available { if err := iterator.Error(); err != nil { return err } n, err := sink.Write(iterator.Value()) if err != nil { return err } snapshotBytes += n available = iterator.Next() } log.Printf("snapshot: wrote %d bytes", snapshotBytes) log.Printf("Snapshot done\n") return nil }
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 }
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 }
// 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 }
func (s *nomadSnapshot) persistVaultAccessors(sink raft.SnapshotSink, encoder *codec.Encoder) error { accessors, err := s.snap.VaultAccessors() if err != nil { return err } for { raw := accessors.Next() if raw == nil { break } accessor := raw.(*structs.VaultAccessor) sink.Write([]byte{byte(VaultAccessorSnapshot)}) if err := encoder.Encode(accessor); err != nil { return err } } return nil }
func (s *nomadSnapshot) persistJobSummaries(sink raft.SnapshotSink, encoder *codec.Encoder) error { summaries, err := s.snap.JobSummaries() if err != nil { return err } for { raw := summaries.Next() if raw == nil { break } jobSummary := raw.(structs.JobSummary) sink.Write([]byte{byte(JobSummarySnapshot)}) if err := encoder.Encode(jobSummary); err != nil { return err } } return nil }
// 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() }
func (s *consulSnapshot) persistNodes(sink raft.SnapshotSink, encoder *codec.Encoder) error { // Get all the nodes nodes, err := s.state.Nodes() if err != nil { return err } // Register each node for node := nodes.Next(); node != nil; node = nodes.Next() { n := node.(*structs.Node) req := structs.RegisterRequest{ Node: n.Node, Address: n.Address, } // Register the node itself sink.Write([]byte{byte(structs.RegisterRequestType)}) if err := encoder.Encode(&req); err != nil { return err } // Register each service this node has services, err := s.state.Services(n.Node) if err != nil { return err } for service := services.Next(); service != nil; service = services.Next() { sink.Write([]byte{byte(structs.RegisterRequestType)}) req.Service = service.(*structs.ServiceNode).ToNodeService() if err := encoder.Encode(&req); err != nil { return err } } // Register each check this node has req.Service = nil checks, err := s.state.Checks(n.Node) if err != nil { return err } for check := checks.Next(); check != nil; check = checks.Next() { sink.Write([]byte{byte(structs.RegisterRequestType)}) req.Check = check.(*structs.HealthCheck) if err := encoder.Encode(&req); err != nil { return err } } } return nil }
// 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 }
func (s *consulSnapshot) persistNodes(sink raft.SnapshotSink, encoder *codec.Encoder) error { // Get all the nodes nodes := s.state.Nodes() // Register each node var req structs.RegisterRequest for i := 0; i < len(nodes); i++ { req = structs.RegisterRequest{ Node: nodes[i].Node, Address: nodes[i].Address, } // Register the node itself sink.Write([]byte{byte(structs.RegisterRequestType)}) if err := encoder.Encode(&req); err != nil { return err } // Register each service this node has services := s.state.NodeServices(nodes[i].Node) for _, srv := range services.Services { req.Service = srv sink.Write([]byte{byte(structs.RegisterRequestType)}) if err := encoder.Encode(&req); err != nil { return err } } // Register each check this node has req.Service = nil checks := s.state.NodeChecks(nodes[i].Node) for _, check := range checks { req.Check = check sink.Write([]byte{byte(structs.RegisterRequestType)}) if err := encoder.Encode(&req); err != nil { return err } } } return nil }
// 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 }