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 }
// CodecEncodeSelf implements codec.Selfer func (c *EdgeMetadatas) CodecEncodeSelf(encoder *codec.Encoder) { if c.psMap != nil { encoder.Encode(c.toIntermediate()) } else { encoder.Encode(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 *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 (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 }
// CodecEncodeSelf implements codec.Selfer func (m *LatestMap) CodecEncodeSelf(encoder *codec.Encoder) { if m.Map != nil { encoder.Encode(m.toIntermediate()) } else { encoder.Encode(nil) } }
// CodecEncodeSelf implements codec.Selfer func (n *PluginSpecs) CodecEncodeSelf(encoder *codec.Encoder) { if n.psMap != nil { encoder.Encode(n.toIntermediate()) } else { encoder.Encode(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 }
// CodecEncodeSelf implements codec.Selfer func (s *Sets) CodecEncodeSelf(encoder *codec.Encoder) { if s.psMap != nil { encoder.Encode(s.toIntermediate()) } else { encoder.Encode(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 encodeRecordSet(encoder *codec.Encoder, recordSet FluentRecordSet) error { v := []interface{}{recordSet.Tag, recordSet.Records} err := encoder.Encode(v) if err != nil { return err } return err }
// CodecEncodeSelf marshals this ControlInstance. It takes the basic Metric // rendering, then adds some row-specific fields. func (c *ControlInstance) CodecEncodeSelf(encoder *codec.Encoder) { encoder.Encode(wiredControlInstance{ ProbeID: c.ProbeID, NodeID: c.NodeID, ID: c.Control.ID, Human: c.Control.Human, Icon: c.Control.Icon, Rank: c.Control.Rank, }) }
func encodeRecords(encoder *codec.Encoder, records []TinyFluentRecord) error { for _, record := range records { e := map[string]interface{}{"time": record.Timestamp} for k, v := range record.Data { e[k] = v } err := encoder.Encode(e) if 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) 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 }
func main() { log.Println("MsgPack test begin==>") bookNode := Book{"web-founding", 36.5} for i := 0; i < 200; i++ { bookList = append(bookList, bookNode) } log.Println("json test=>begin") jbook, err := json.Marshal(bookList) if err != nil { log.Println("json failed=>", err) return } log.Println("length[", len(jbook), "]") fh, _ := os.OpenFile("json.txt", os.O_CREATE|os.O_WRONLY, 0666) nlen, _ := fh.Write(jbook) log.Println("acive write byte length[", nlen, "]") fh.Close() log.Println("json test=>end") log.Println("==================================") log.Println("msgpack test=>begin") log.Println("msgpack encoding...") var h codec.Handle = new(codec.MsgpackHandle) var buf []byte var enc *codec.Encoder = codec.NewEncoderBytes(&buf, h) err = enc.Encode(bookList) if err != nil { log.Println("msgpack encode err:", err) return } // hlen := hex.Encode(dst, buf) log.Println("buf:=>", len(hex.EncodeToString(buf))) fmsg, _ := os.OpenFile("msg.txt", os.O_CREATE|os.O_WRONLY, 0666) nlen, _ = fmsg.Write(buf) log.Println("acive write byte length[", nlen, "]") fmsg.Close() log.Println("msgpack decoding...") var dec *codec.Decoder = codec.NewDecoderBytes(buf, h) v2 := new([]Book) log.Println("src data len=>", len(buf)) err = dec.Decode(v2) if err != nil { log.Println("msgpack decode err:", err) return } // log.Println("decode=>", *v2) log.Println("msgpack test=>end") log.Println("MsgPack test end==>") }
// CodecEncodeSelf marshals this MetricRow. It takes the basic Metric // rendering, then adds some row-specific fields. func (m *MetricRow) CodecEncodeSelf(encoder *codec.Encoder) { in := m.Metric.ToIntermediate() encoder.Encode(wiredMetricRow{ ID: m.ID, Label: m.Label, Format: m.Format, Group: m.Group, Value: m.Value, Priority: m.Priority, Samples: in.Samples, Min: in.Min, Max: in.Max, First: in.First, Last: in.Last, }) }
/* * New creates a new message from a message type and a generic payload */ func New(t Type, p interface{}) *Message { var mh codec.MsgpackHandle msg := new(Message) msg.Type = t // Encode payload to msgpack bb := new(bytes.Buffer) var enc *codec.Encoder = codec.NewEncoder(bb, &mh) err := enc.Encode(p) if err != nil { log.WithError(err).Error("Error encoding payload") return nil } // Copy the payload buffer msg.Payload = bb.Bytes() // Length is the buffer length msg.Length = uint32(len(msg.Payload)) return msg }
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 }
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 }
// CodecEncodeSelf is part of the codec.Selfer interface. func (vs *Verbs) CodecEncodeSelf(encoder *codec.Encoder) { encoder.Encode(vs) }
// CodecEncodeSelf implements codec.Selfer func (c *Counters) CodecEncodeSelf(encoder *codec.Encoder) { encoder.Encode(c.toIntermediate()) }
// CodecEncodeSelf implements codec.Selfer func (m *Metric) CodecEncodeSelf(encoder *codec.Encoder) { in := m.ToIntermediate() encoder.Encode(in) }
func (l *ListLink) Encode(output *[]byte, ctx *Context) error { var enc *codec.Encoder = codec.NewEncoderBytes(output, ctx.mh) err := enc.Encode(l) return err }
func (r Record) ToJSON(w io.Writer) error { var enc *codec.Encoder = codec.NewEncoder(w, codecJSON) return enc.Encode(r) }
func (e *framedMsgpackEncoder) encodeToBytes(enc *codec.Encoder, i interface{}) (v []byte, err error) { enc.ResetBytes(&v) err = enc.Encode(i) return v, err }
// CodecEncodeSelf implements codec.Selfer func (nc *NodeControls) CodecEncodeSelf(encoder *codec.Encoder) { encoder.Encode(WireNodeControls{ Timestamp: renderTime(nc.Timestamp), Controls: nc.Controls, }) }
// handleCommand is used to decode and dispatch a single command func (n *NetworkTransport) handleCommand(r *bufio.Reader, dec *codec.Decoder, enc *codec.Encoder) error { // Get the rpc type rpcType, err := r.ReadByte() if err != nil { return err } // Create the RPC object respCh := make(chan RPCResponse, 1) rpc := RPC{ RespChan: respCh, } // Decode the command switch rpcType { case rpcAppendEntries: var req AppendEntriesRequest if err := dec.Decode(&req); err != nil { return err } rpc.Command = &req case rpcRequestVote: var req RequestVoteRequest if err := dec.Decode(&req); err != nil { return err } rpc.Command = &req case rpcInstallSnapshot: var req InstallSnapshotRequest if err := dec.Decode(&req); err != nil { return err } rpc.Command = &req rpc.Reader = io.LimitReader(r, req.Size) default: return fmt.Errorf("unknown rpc type %d", rpcType) } // Dispatch the RPC select { case n.consumeCh <- rpc: case <-n.shutdownCh: return ErrTransportShutdown } // Wait for response select { case resp := <-respCh: // Send the error first respErr := "" if resp.Error != nil { respErr = resp.Error.Error() } if err := enc.Encode(respErr); err != nil { return err } // Send the response if err := enc.Encode(resp.Response); err != nil { return err } case <-n.shutdownCh: return ErrTransportShutdown } return nil }