Beispiel #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)
}
Beispiel #2
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
}
Beispiel #3
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
}
Beispiel #4
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)
}
Beispiel #5
0
// TODO: handle watches
func (s *storeFSM) Apply(l *raft.Log) interface{} {
	var ax api.Request
	if err := api.Decode(&ax, bytes.NewBuffer(l.Data)); err != nil {
		return err
	}

	switch ax.Action {
	case api.Delete:
		delete(s.data.KV, ax.Key)
		delete(s.data.TTLs, ax.Key)
	case api.Put:
		s.data.KV[ax.Key] = &libkvstore.KVPair{Key: ax.Key, Value: ax.Value, LastIndex: l.Index}
		if ax.TTL != 0 {
			s.ttlLock.Lock()
			s.data.TTLs[ax.Key] = &ttl{CreateTime: time.Now(), TTL: ax.TTL, CreateIndex: l.Index}
			s.ttlLock.Unlock()
		}
	case api.DeleteTree:
		for k := range s.data.KV {
			if !strings.HasPrefix(k, ax.Key) {
				continue
			}
			delete(s.data.KV, k)
			delete(s.data.TTLs, k)
		}
	case reapKeys:
		s.mu.Lock()
		for _, k := range ax.Args {
			delete(s.data.KV, k)
			delete(s.data.TTLs, k)
		}
		s.mu.Unlock()
	default:
		return fmt.Errorf("unknown api.Request")
	}
	return nil
}
Beispiel #6
0
func (s *storeFSM) Restore(r io.ReadCloser) error {
	defer r.Close()
	s.data = newDB()
	return api.Decode(s.data, r)
}