Exemple #1
0
func (r *nodeRPC) handleConn(conn net.Conn) {
	defer conn.Close()
	var req rpcRequest
	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() {
		rpcProxyRequest(r.r.Leader(), &req, conn, r.r.tlsConfig)
		return
	}

	var res rpcResponse
	if err := h(&req); err != nil {
		res.Err = err.Error()
	}
	api.Encode(&res, conn)
}
Exemple #2
0
func (s *store) apply(ax *api.Request) error {
	if !s.r.IsLeader() {
		return s.forwardToLeader(ax)
	}
	buf := bytes.NewBuffer(nil)
	if err := api.Encode(ax, buf); err != nil {
		return err
	}
	return s.r.Apply(buf.Bytes())
}
Exemple #3
0
// rpc is a helper function for performing RPC requests between nodes
func rpc(addr string, msg *rpcRequest, tlsConfig *tls.Config) (*rpcResponse, error) {
	conn, err := api.Dial(addr, api.RPCMessage, defaultTimeout, tlsConfig, true)
	if err != nil {
		return nil, err
	}

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

	var res rpcResponse
	if err := api.Decode(&res, conn); err != nil {
		return nil, err
	}
	return &res, nil
}
Exemple #4
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 != "" {
		return nil, fmt.Errorf("store error: %s", res.Err)
	}
	return &res, nil
}
Exemple #5
0
func (r *clientRPC) handleConn(conn net.Conn) {
	defer conn.Close()
	var req api.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.Action, req.Key)

	var h clientRPCHandlerFn

	switch req.Action {
	case api.Get:
		h = r.Get
	case api.Put:
		h = r.Put
	}

	api.Encode(h(&req), conn)
}
Exemple #6
0
// rpcProxyRequest is a helper function to proxy an rpc request to another node
func rpcProxyRequest(addr string, msg *rpcRequest, from io.ReadWriter, tlsConfig *tls.Config) error {
	conn, err := api.Dial(addr, api.RPCMessage, defaultTimeout, tlsConfig, true)
	if err != nil {
		return err
	}
	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 #7
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)
}