Exemple #1
0
func (r *clientRPC) Get(conn io.Writer, req *clientRequest) {
	var res api.Response
	kv, err := r.s.Get(req.Key)
	if err != nil {
		setError(&res, err)
		api.Encode(&res, conn)
		return
	}
	res.KV = libkvToKV(kv)
	api.Encode(&res, conn)
}
Exemple #2
0
// ProxyRequest is a helper function to proxy an rpc request to another node
func (l *StreamLayer) ProxyRequest(addr string, msg *Request, from io.ReadWriter) error {
	conn, err := l.DialWithRetry(addr, time.Duration(retryTimeout)*time.Second, true)
	if err != nil {
		return err
	}
	defer conn.Close()
	if err := api.Encode(msg, conn); err != nil {
		return err
	}

	chErr := make(chan error, 2)
	go func() {
		_, err := io.Copy(from, conn)
		chErr <- err
	}()
	go func() {
		_, err := io.Copy(conn, from)
		chErr <- err
	}()

	if err := <-chErr; err != nil {
		return err
	}
	return <-chErr
}
Exemple #3
0
func (r *nodeRPC) handleConn(conn net.Conn) {
	defer conn.Close()
	var req rpc.Request
	if err := api.Decode(&req, conn); err != nil {
		logrus.Errorf("error handling K/V RPC connection: %v", err)
		return
	}

	logrus.Debugf("Got: %s(%s)", req.Method, req.Args)

	var h rpcHandlerFunc

	switch req.Method {
	case addNode:
		h = r.addNode
	case removeNode:
		h = r.removeNode
	}

	if !r.r.IsLeader() {
		r.ProxyRequest(r.r.GetLeader(), &req, conn)
		return
	}

	var res rpc.Response
	if err := h(&req); err != nil {
		res.Err = err.Error()
	}
	api.Encode(&res, conn)
}
Exemple #4
0
func (s *store) apply(ax *api.Request) error {
	buf := bytes.NewBuffer(nil)
	if err := api.Encode(ax, buf); err != nil {
		return err
	}
	return s.r.Apply(buf.Bytes())
}
Exemple #5
0
func (r *clientRPC) Put(conn io.Writer, req *clientRequest) {
	var res api.Response
	err := r.s.Put(req.Key, req.Value, &libkvstore.WriteOptions{TTL: req.TTL})
	if err != nil {
		setError(&res, err)
	}
	api.Encode(res, conn)
}
Exemple #6
0
func (c *Client) stream(req *api.Request) (io.ReadCloser, error) {
	conn, err := c.dial()
	if err != nil {
		return nil, err
	}
	if err := api.Encode(&req, conn); err != nil {
		return nil, err
	}
	return conn, nil
}
Exemple #7
0
func (r *clientRPC) Watch(conn io.Writer, req *clientRequest) {
	chStop := make(chan struct{})
	go waitClose(req.body, chStop)
	chKv, err := r.s.Watch(req.Key, chStop)
	if err != nil {
		return
	}
	for kv := range chKv {
		api.Encode(libkvToKV(kv), conn)
	}
}
Exemple #8
0
// RPC is a helper function for performing RPC requests between nodes
func (l *StreamLayer) RPC(addr string, msg *Request) (*Response, error) {
	conn, err := l.DialWithRetry(addr, time.Duration(retryTimeout)*time.Second, true)
	if err != nil {
		return nil, err
	}
	if err := api.Encode(msg, conn); err != nil {
		return nil, err
	}

	var res Response
	if err := api.Decode(&res, conn); err != nil {
		return nil, err
	}
	return &res, nil
}
Exemple #9
0
func (r *clientRPC) WatchTree(conn io.Writer, req *clientRequest) {
	chStop := make(chan struct{})
	go waitClose(req.body, chStop)
	chKv, err := r.s.WatchTree(req.Key, chStop)
	if err != nil {
		return
	}
	var apiKVList []*api.KVPair
	for kvList := range chKv {
		for _, kv := range kvList {
			apiKVList = append(apiKVList, libkvToKV(kv))
		}
		api.Encode(apiKVList, conn)
		apiKVList = nil
	}
}
Exemple #10
0
func (c *Client) do(req *api.Request) (*api.Response, error) {
	conn, err := c.dial()
	if err != nil {
		return nil, err
	}
	defer conn.Close()

	if err := api.Encode(&req, conn); err != nil {
		return nil, err
	}

	var res api.Response
	if err := api.Decode(&res, conn); err != nil {
		return nil, fmt.Errorf("error decoding response: %v", err)
	}
	if res.Err != nil {
		return nil, getErr(res.Err)
	}
	return &res, nil
}
Exemple #11
0
func (s *storeFSM) Persist(sink raft.SnapshotSink) error {
	defer sink.Close()
	s.mu.Lock()
	defer s.mu.Unlock()
	return api.Encode(s.data, sink)
}