Esempio n. 1
0
//
// 将Request中的SeqNum进行替换(修改Request部分的数据)
//
func (r *Request) ReplaceSeqId(newSeq int32) {
	if r.Request.Data != nil {
		//		log.Printf(Green("Replace SeqNum: %d --> %d"), r.Request.SeqId, newSeq)
		if r.Response.SeqId != 0 {
			log.Errorf("Unexpected Response SedId")
		}
		r.Response.SeqId = newSeq

		start := 0

		if r.ProxyRequest {
			start = len(r.Service)
		}
		if start > 0 {
			start += 1 // ":"
			//			log.Printf("Service: %s, Name: %s\n", r.Service, r.Request.Name)
		}
		transport := NewTMemoryBufferWithBuf(r.Request.Data[start:start])
		protocol := thrift.NewTBinaryProtocolTransport(transport)
		protocol.WriteMessageBegin(r.Request.Name, r.Request.TypeId, newSeq)

		if start > 0 {
			r.Request.DataOrig = r.Request.Data
		}
		// 将service从name中剥离出去
		r.Request.Data = r.Request.Data[start:len(r.Request.Data)]

	} else {
		log.Errorf("ReplaceSeqId called on processed Data")
	}
}
Esempio n. 2
0
// 配对 Request, resp, err
// PARAM: resp []byte 为一帧完整的thrift数据包
func (bc *BackendConnLB) setResponse(r *Request, data []byte, err error) error {
	//	log.Printf("#setResponse:  data: %v", data)
	// 表示出现错误了
	if data == nil {
		log.Printf("No Data From Server, error: %v\n", err)
		r.Response.Err = err
	} else {
		// 从resp中读取基本的信息
		typeId, method, seqId, err := DecodeThriftTypIdSeqId(data)

		// 解码错误,直接报错
		if err != nil {
			log.ErrorErrorf(err, "Decode SeqId Error: %v", err)
			return err
		}

		if typeId == MESSAGE_TYPE_STOP {
			// 不再接受新的输入
			// 直接来自后端的服务(不遵循: Request/Reply模型)
			bc.MarkConnActiveFalse()
			return nil
		}

		// 找到对应的Request

		req := bc.seqNumRequestMap.Pop(seqId)

		// 如果是心跳,则OK
		if typeId == MESSAGE_TYPE_HEART_BEAT {
			bc.hbLastTime.Set(time.Now().Unix())
			return nil
		}

		if req == nil {
			log.Errorf("#setResponse not found, seqId: %d", seqId)
			return nil
		} else {

			if req.Response.SeqId != seqId {
				log.Errorf("Data From Server, SeqId not match, Ex: %d, Ret: %d", req.Request.SeqId, seqId)
			}
			r = req
			r.Response.TypeId = typeId
			if req.Request.Name != method {
				data = nil
				err = req.NewInvalidResponseError(method, "conn_lb")
			}
		}
	}

	r.Response.Data, r.Response.Err = data, err
	// 还原SeqId
	if data != nil {
		r.RestoreSeqId()
	}

	r.Wait.Done()
	return err
}
Esempio n. 3
0
//
// 添加新的key, value
//
func (c *RequestMap) Add(key int32, value *Request) bool {
	c.lock.Lock()

	// 如果key存在,则覆盖之前的元素;并添加Warning
	if ent, ok := c.items[key]; ok {
		c.evictList.MoveToFront(ent)
		ent.Value.(*Entry).value = value
		log.Errorf(Red("Duplicated Key Found in RequestOrderedMap: %d"), key)

		c.lock.Unlock()
		return false
	}

	// Add new item
	ent := &Entry{key, value}
	entry := c.evictList.PushFront(ent)
	c.items[key] = entry

	// 如果超过指定的大小,则清除元素
	evict := c.evictList.Len() > c.size
	if evict {
		c.removeOldest()
	}
	c.lock.Unlock()
	return evict
}
func (p *TBufferedFramedTransport) Write(buf []byte) (int, error) {
	// 直接写入Buffer
	n, err := p.Buffer.Write(buf)
	if n != len(buf) {
		log.Errorf("TBufferedFramedTransport#Write Error")
	}
	return n, thrift.NewTTransportExceptionFromError(err)
}
Esempio n. 5
0
// 配对 Request, resp, err
// PARAM: resp []byte 为一帧完整的thrift数据包
func (bc *BackendConn) setResponse(r *Request, data []byte, err error) error {
	// 表示出现错误了
	if data == nil {
		log.Debugf("[%s] SeqId: %d, No Data From Server, error: %v", r.Service, r.Response.SeqId, err)
		r.Response.Err = err
	} else {
		// 从resp中读取基本的信息
		typeId, method, seqId, err := DecodeThriftTypIdSeqId(data)
		//		if err != nil {
		//			log.Debugf("SeqId: %d, Decoded, error: %v", seqId, err)
		//		} else {
		//			log.Debugf("SeqId: %d, Decoded", seqId)
		//		}
		// 解码错误,直接报错
		if err != nil {
			log.Debugf("SeqId: %d, Decoded, error: %v", seqId, err)
			return err
		}

		// 找到对应的Request
		req := bc.seqNumRequestMap.Pop(seqId)

		// 如果是心跳,则OK
		if typeId == MESSAGE_TYPE_HEART_BEAT {
			bc.hbLastTime.Set(time.Now().Unix())
			//			if req != nil {
			//				log.Printf("HB RT: %.3fms", float64(microseconds()-req.Start)*0.001)
			//			}
			return nil
		}

		if req == nil {
			// return errors.New("Invalid Response")
			// 由于是异步返回,因此回来找不到也正常
			log.Errorf("#setResponse not found, seqId: %d", seqId)
			return nil
		} else {

			if req.Response.SeqId != seqId {
				log.Errorf("Data From Server, SeqId not match, Ex: %d, Ret: %d", req.Request.SeqId, seqId)
			}
			r = req
			r.Response.TypeId = typeId
			if req.Request.Name != method {
				data = nil
				err = req.NewInvalidResponseError(method, "conn_proxy")
			}
		}
	}

	// 正常返回数据,或者报错
	r.Response.Data, r.Response.Err = data, err

	// 还原SeqId
	if data != nil {
		r.RestoreSeqId()
	}

	// 设置几个控制用的channel
	r.Wait.Done()

	return err
}