Example #1
0
func (w *rpcWorker) callRPC(conn *bufio.ReadWriter, req *pdpb.Request) (resp *pdpb.Response, err error) {
	// Record some metrics.
	start := time.Now()
	label := metrics.GetCmdLabel(req)
	defer func() {
		if err == nil {
			cmdCounter.WithLabelValues(label).Inc()
			cmdDuration.WithLabelValues(label).Observe(time.Since(start).Seconds())
		} else {
			cmdFailedCounter.WithLabelValues(label).Inc()
			cmdFailedDuration.WithLabelValues(label).Observe(time.Since(start).Seconds())
		}
	}()

	msg := &msgpb.Message{
		MsgType: msgpb.MessageType_PdReq,
		PdReq:   req,
	}
	if err = util.WriteMessage(conn, newMsgID(), msg); err != nil {
		return nil, errors.Errorf("[pd] rpc failed: %v", err)
	}
	conn.Flush()
	if _, err = util.ReadMessage(conn, msg); err != nil {
		return nil, errors.Errorf("[pd] rpc failed: %v", err)
	}
	if msg.GetMsgType() != msgpb.MessageType_PdResp {
		return nil, errors.Trace(errInvalidResponse)
	}
	resp = msg.GetPdResp()
	if err = w.checkResponse(resp); err != nil {
		return nil, errors.Trace(err)
	}

	return resp, nil
}
Example #2
0
func (s *testClientSuite) TestWrongMessageID(c *C) {
	l := startServer(":61237", c, func(conn net.Conn, c *C) {
		var msg msgpb.Message
		msgID, err := util.ReadMessage(conn, &msg)
		c.Assert(err, IsNil)
		resp := msgpb.Message{
			MsgType: msgpb.MessageType_KvResp,
			KvResp: &pb.Response{
				Type: msg.GetKvReq().GetType(),
			},
		}
		// Send the request back to client, set wrong msgID for the 1st
		// request.
		if msgID == 1 {
			err = util.WriteMessage(conn, msgID+100, &resp)
		} else {
			err = util.WriteMessage(conn, msgID, &resp)
		}
		c.Assert(err, IsNil)
	})
	defer l.Close()
	cli := newRPCClient()
	req := &pb.Request{
		Type: pb.MessageType_CmdGet,
	}
	// Wrong ID for the first request, correct for the rests.
	_, err := cli.SendKVReq(":61237", req, readTimeoutShort)
	c.Assert(err, NotNil)
	resp, err := cli.SendKVReq(":61237", req, readTimeoutShort)
	c.Assert(err, IsNil)
	c.Assert(resp.GetType(), Equals, req.GetType())
}
Example #3
0
func readThenCloseRequest(conn net.Conn, c *C) {
	c.Assert(conn, NotNil)
	defer conn.Close()
	var msg msgpb.Message
	msgID, err := util.ReadMessage(conn, &msg)
	c.Assert(err, IsNil)
	c.Assert(msg.GetKvReq(), NotNil)
	c.Assert(msgID, Greater, uint64(0))
}
Example #4
0
// Call sends the request to conn and wait for the response.
func Call(conn net.Conn, reqID uint64, request *pdpb.Request) (*pdpb.Response, error) {
	req := &msgpb.Message{
		MsgType: msgpb.MessageType_PdReq,
		PdReq:   request,
	}
	if err := util.WriteMessage(conn, reqID, req); err != nil {
		return nil, errors.Trace(err)
	}
	resp := &msgpb.Message{}
	respID, err := util.ReadMessage(conn, resp)
	if err != nil {
		return nil, errors.Trace(err)
	}
	if respID != reqID {
		return nil, errors.Errorf("message id mismatch: reqID %d respID %d", reqID, respID)
	}
	return resp.GetPdResp(), nil
}
Example #5
0
func (c *rpcClient) doSend(conn *Conn, msg *msgpb.Message) error {
	curMsgID := atomic.AddUint64(&c.msgID, 1)
	log.Debugf("Send request msgID[%d] type[%v]", curMsgID, msg.GetMsgType())
	if err := util.WriteMessage(conn, curMsgID, msg); err != nil {
		return errors.Trace(err)
	}
	if err := conn.Flush(); err != nil {
		return errors.Trace(err)
	}
	msgID, err := util.ReadMessage(conn.BufioReader(), msg)
	if err != nil {
		return errors.Trace(err)
	}
	if curMsgID != msgID {
		log.Errorf("Sent msgID[%d] mismatches recv msgID[%d]", curMsgID, msgID)
	}
	log.Debugf("Receive response msgID[%d] type[%v]", msgID, msg.GetMsgType())
	return nil
}
Example #6
0
// handleRequest receive Request then send empty Response back fill with same Type.
func handleRequest(conn net.Conn, c *C) {
	c.Assert(conn, NotNil)
	defer conn.Close()
	var msg msgpb.Message
	msgID, err := util.ReadMessage(conn, &msg)
	c.Assert(err, IsNil)
	c.Assert(msgID, Greater, uint64(0))
	c.Assert(msg.GetMsgType(), Equals, msgpb.MessageType_KvReq)

	req := msg.GetKvReq()
	c.Assert(req, NotNil)
	var resp pb.Response
	resp.Type = req.Type
	msg = msgpb.Message{
		MsgType: msgpb.MessageType_KvResp,
		KvResp:  &resp,
	}
	err = util.WriteMessage(conn, msgID, &msg)
	c.Assert(err, IsNil)
}
Example #7
0
func (c *rpcClient) doSend(conn *Conn, msg *msgpb.Message, writeTimeout time.Duration, readTimeout time.Duration) error {
	curMsgID := atomic.AddUint64(&c.msgID, 1)
	conn.SetWriteDeadline(time.Now().Add(writeTimeout))
	if err := util.WriteMessage(conn, curMsgID, msg); err != nil {
		return errors.Trace(err)
	}
	if err := conn.Flush(); err != nil {
		return errors.Trace(err)
	}
	conn.SetReadDeadline(time.Now().Add(readTimeout))
	msgID, err := util.ReadMessage(conn.BufioReader(), msg)
	if err != nil {
		return errors.Trace(err)
	}
	if curMsgID != msgID {
		log.Errorf("Sent msgID[%d] mismatches recv msgID[%d]", curMsgID, msgID)
		return errors.Trace(errInvalidResponse)
	}
	return nil
}
Example #8
0
func (w *rpcWorker) callRPC(conn *bufio.ReadWriter, req *pdpb.Request) (*pdpb.Response, error) {
	msg := &msgpb.Message{
		MsgType: msgpb.MessageType_PdReq.Enum(),
		PdReq:   req,
	}
	if err := util.WriteMessage(conn, newMsgID(), msg); err != nil {
		return nil, errors.Errorf("[pd] rpc failed: %v", err)
	}
	conn.Flush()
	if _, err := util.ReadMessage(conn, msg); err != nil {
		return nil, errors.Errorf("[pd] rpc failed: %v", err)
	}
	if msg.GetMsgType() != msgpb.MessageType_PdResp {
		return nil, errors.Trace(errInvalidResponse)
	}
	resp := msg.GetPdResp()
	if err := w.checkResponse(resp); err != nil {
		return nil, errors.Trace(err)
	}

	return resp, nil
}
Example #9
0
func (c *rpcClient) doSend(conn net.Conn, msg *msgpb.Message) error {
	curMsgID := atomic.AddUint64(&c.msgID, 1)
	log.Debugf("Send request msgID[%d] type[%v]", curMsgID, msg.GetMsgType())
	if err := conn.SetWriteDeadline(time.Now().Add(writeTimeout)); err != nil {
		log.Warn("Set write deadline failed, it may be blocked.")
	}
	if err := util.WriteMessage(conn, curMsgID, msg); err != nil {
		return errors.Trace(err)
	}
	if err := conn.SetReadDeadline(time.Now().Add(readTimeout)); err != nil {
		log.Warn("Set read deadline failed, it may be blocked.")
	}
	msgID, err := util.ReadMessage(conn, msg)
	if err != nil {
		return errors.Trace(err)
	}
	if curMsgID != msgID {
		log.Errorf("Sent msgID[%d] mismatches recv msgID[%d]", curMsgID, msgID)
	}
	log.Debugf("Receive response msgID[%d] type[%v]", msgID, msg.GetMsgType())
	return nil
}