Ejemplo n.º 1
0
// SendCopReq sends a Request to co-processor and receives Response.
func (c *rpcClient) SendCopReq(req *coprocessor.Request) (*coprocessor.Response, error) {
	conn, err := c.pool.Get()
	if err != nil {
		return nil, errors.Trace(err)
	}
	defer conn.Close()
	msg := msgpb.Message{
		MsgType: msgpb.MessageType_CopReq.Enum(),
		CopReq:  req,
	}
	err = c.doSend(conn, &msg)
	if err == nil {
		if msg.GetMsgType() != msgpb.MessageType_CopResp || msg.GetCopResp() == nil {
			err = errors.Trace(errInvalidResponse)
		}
	}
	if err != nil {
		// This connection is not valid any more, so close its underlying conn.
		if poolConn, ok := conn.(*pool.PoolConn); ok {
			poolConn.MarkUnusable()
		}
		return nil, errors.Trace(err)
	}
	return msg.GetCopResp(), nil
}
Ejemplo n.º 2
0
// SendKVReq sends a Request to kv server and receives Response.
func (c *rpcClient) SendKVReq(addr string, req *kvrpcpb.Request, timeout time.Duration) (*kvrpcpb.Response, error) {
	sendReqCounter.WithLabelValues("kv").Inc()
	start := time.Now()
	defer func() { sendReqHistogram.WithLabelValues("kv").Observe(time.Since(start).Seconds()) }()

	conn, err := c.p.GetConn(addr)
	if err != nil {
		return nil, errors.Trace(err)
	}
	defer c.p.PutConn(conn)
	msg := msgpb.Message{
		MsgType: msgpb.MessageType_KvReq,
		KvReq:   req,
	}
	err = c.doSend(conn, &msg, writeTimeout, timeout)
	if err != nil {
		conn.Close()
		return nil, errors.Trace(err)
	}
	if msg.GetMsgType() != msgpb.MessageType_KvResp || msg.GetKvResp() == nil {
		conn.Close()
		return nil, errors.Trace(errInvalidResponse)
	}
	return msg.GetKvResp(), nil
}
Ejemplo n.º 3
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
}
Ejemplo n.º 4
0
// SendKVReq sends a Request to kv server and receives Response.
func (c *rpcClient) SendKVReq(addr string, req *kvrpcpb.Request) (*kvrpcpb.Response, error) {
	conn, err := c.p.GetConn(addr)
	if err != nil {
		return nil, errors.Trace(err)
	}
	defer c.p.PutConn(conn)
	msg := msgpb.Message{
		MsgType: msgpb.MessageType_KvReq.Enum(),
		KvReq:   req,
	}
	err = c.doSend(conn, &msg)
	if err != nil {
		return nil, errors.Trace(err)
	}
	if msg.GetMsgType() != msgpb.MessageType_KvResp || msg.GetKvResp() == nil {
		return nil, errors.Trace(errInvalidResponse)
	}
	return msg.GetKvResp(), nil
}
Ejemplo n.º 5
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)
}
Ejemplo n.º 6
0
// SendCopReq sends a Request to co-processor and receives Response.
func (c *rpcClient) SendCopReq(addr string, req *coprocessor.Request) (*coprocessor.Response, error) {
	conn, err := c.p.GetConn(addr)
	if err != nil {
		return nil, errors.Trace(err)
	}
	defer c.p.PutConn(conn)
	msg := msgpb.Message{
		MsgType: msgpb.MessageType_CopReq,
		CopReq:  req,
	}
	err = c.doSend(conn, &msg)
	if err != nil {
		conn.Close()
		return nil, errors.Trace(err)
	}
	if msg.GetMsgType() != msgpb.MessageType_CopResp || msg.GetCopResp() == nil {
		conn.Close()
		return nil, errors.Trace(errInvalidResponse)
	}
	return msg.GetCopResp(), nil
}
Ejemplo n.º 7
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
}
Ejemplo n.º 8
0
func (c *rpcClient) doSend(conn *Conn, msg *msgpb.Message, writeTimeout time.Duration, readTimeout time.Duration) error {
	curMsgID := atomic.AddUint64(&c.msgID, 1)
	log.Debugf("Send request msgID[%d] type[%v]", curMsgID, msg.GetMsgType())
	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)
	}
	log.Debugf("Receive response msgID[%d] type[%v]", msgID, msg.GetMsgType())
	return nil
}