import "C"
import (
+ "fmt"
"github.com/spf13/viper"
"strconv"
"strings"
{Name: "ReceiveError", Help: "The total number of RMR receive errors"},
}
+var RMRErrors = map[int]string{
+ C.RMR_OK: "state is good",
+ C.RMR_ERR_BADARG: "argument passed to function was unusable",
+ C.RMR_ERR_NOENDPT: "send/call could not find an endpoint based on msg type",
+ C.RMR_ERR_EMPTY: "msg received had no payload; attempt to send an empty message",
+ C.RMR_ERR_NOHDR: "message didn't contain a valid header",
+ C.RMR_ERR_SENDFAILED: "send failed; errno has nano reason",
+ C.RMR_ERR_CALLFAILED: "unable to send call() message",
+ C.RMR_ERR_NOWHOPEN: "no wormholes are open",
+ C.RMR_ERR_WHID: "wormhole id was invalid",
+ C.RMR_ERR_OVERFLOW: "operation would have busted through a buffer/field size",
+ C.RMR_ERR_RETRY: "request (send/call/rts) failed, but caller should retry (EAGAIN for wrappers)",
+ C.RMR_ERR_RCVFAILED: "receive failed (hard error)",
+ C.RMR_ERR_TIMEOUT: "message processing call timed out",
+ C.RMR_ERR_UNSET: "the message hasn't been populated with a transport buffer",
+ C.RMR_ERR_TRUNC: "received message likely truncated",
+ C.RMR_ERR_INITFAILED: "initialization of something (probably message) failed",
+ C.RMR_ERR_NOTSUPP: "the request is not supported, or RMr was not initialized for the request",
+}
+
type RMRParams struct {
Mtype int
Payload []byte
Mbuf *C.rmr_mbuf_t
}
-func NewRMRClient() *RMRClient {
- p := C.CString(viper.GetString("rmr.protPort"))
- m := C.int(viper.GetInt("rmr.maxSize"))
+func NewRMRClientWithParams(protPort string, maxSize int, numWorkers int, statDesc string) *RMRClient {
+ p := C.CString(protPort)
+ m := C.int(maxSize)
defer C.free(unsafe.Pointer(p))
ctx := C.rmr_init(p, m, C.int(0))
}
return &RMRClient{
- context: ctx,
- consumers: make([]MessageConsumer, 0),
- stat: Metric.RegisterCounterGroup(RMRCounterOpts, "RMR"),
+ protPort: protPort,
+ numWorkers: numWorkers,
+ context: ctx,
+ consumers: make([]MessageConsumer, 0),
+ stat: Metric.RegisterCounterGroup(RMRCounterOpts, statDesc),
}
}
+func NewRMRClient() *RMRClient {
+ return NewRMRClientWithParams(viper.GetString("rmr.protPort"), viper.GetInt("rmr.maxSize"), viper.GetInt("rmr.numWorkers"), "RMR")
+}
+
func (m *RMRClient) Start(c MessageConsumer) {
if c != nil {
m.consumers = append(m.consumers, c)
}
+ var counter int = 0
for {
- Logger.Info("rmrClient: Waiting for RMR to be ready ...")
-
if m.ready = int(C.rmr_ready(m.context)); m.ready == 1 {
+ Logger.Info("rmrClient: RMR is ready after %d seconds waiting...", counter)
break
}
- time.Sleep(10 * time.Second)
+ if counter%10 == 0 {
+ Logger.Info("rmrClient: Waiting for RMR to be ready ...")
+ }
+ time.Sleep(1 * time.Second)
+ counter++
}
- m.wg.Add(viper.GetInt("rmr.numWorkers"))
+ m.wg.Add(m.numWorkers)
if m.readyCb != nil {
go m.readyCb(m.readyCbParams)
}
- for w := 0; w < viper.GetInt("rmr.numWorkers"); w++ {
+ for w := 0; w < m.numWorkers; w++ {
go m.Worker("worker-"+strconv.Itoa(w), 0)
}
m.Wait()
}
func (m *RMRClient) Worker(taskName string, msgSize int) {
- p := viper.GetString("rmr.protPort")
- Logger.Info("rmrClient: '%s': receiving messages on [%s]", taskName, p)
+ Logger.Info("rmrClient: '%s': receiving messages on [%s]", taskName, m.protPort)
defer m.wg.Done()
for {
rxBuffer := C.rmr_rcv_msg(m.context, nil)
if rxBuffer == nil {
+ m.LogMBufError("RecvMsg failed", rxBuffer)
m.UpdateStatCounter("ReceiveError")
continue
}
if buf == nil {
Logger.Error("rmrClient: Allocating message buffer failed!")
}
-
return buf
}
+func (m *RMRClient) Free(mbuf *C.rmr_mbuf_t) {
+ if mbuf == nil {
+ return
+ }
+ C.rmr_free_msg(mbuf)
+}
+
func (m *RMRClient) SendMsg(params *RMRParams) bool {
return m.Send(params, false)
}
}
func (m *RMRClient) Send(params *RMRParams, isRts bool) bool {
- buf := params.Mbuf
- if buf == nil {
- buf = m.Allocate()
+ txBuffer := params.Mbuf
+ if txBuffer == nil {
+ txBuffer = m.Allocate()
+ if txBuffer == nil {
+ return false
+ }
}
- buf.mtype = C.int(params.Mtype)
- buf.sub_id = C.int(params.SubId)
- buf.len = C.int(len(params.Payload))
+ txBuffer.mtype = C.int(params.Mtype)
+ txBuffer.sub_id = C.int(params.SubId)
+ txBuffer.len = C.int(len(params.Payload))
+ if params.PayloadLen != 0 {
+ txBuffer.len = C.int(params.PayloadLen)
+ }
datap := C.CBytes(params.Payload)
defer C.free(datap)
if params.Meid != nil {
b := make([]byte, int(C.RMR_MAX_MEID))
copy(b, []byte(params.Meid.RanName))
- C.rmr_bytes2meid(buf, (*C.uchar)(unsafe.Pointer(&b[0])), C.int(len(b)))
+ C.rmr_bytes2meid(txBuffer, (*C.uchar)(unsafe.Pointer(&b[0])), C.int(len(b)))
}
xidLen := len(params.Xid)
if xidLen > 0 && xidLen <= C.RMR_MAX_XID {
- b := make([]byte, int(C.RMR_MAX_MEID))
+ b := make([]byte, int(C.RMR_MAX_XID))
copy(b, []byte(params.Xid))
- C.rmr_bytes2xact(buf, (*C.uchar)(unsafe.Pointer(&b[0])), C.int(len(b)))
+ C.rmr_bytes2xact(txBuffer, (*C.uchar)(unsafe.Pointer(&b[0])), C.int(len(b)))
}
}
- C.write_bytes_array(buf.payload, datap, buf.len)
+ C.write_bytes_array(txBuffer.payload, datap, txBuffer.len)
- return m.SendBuf(buf, isRts)
+ return m.SendBuf(txBuffer, isRts)
}
func (m *RMRClient) SendBuf(txBuffer *C.rmr_mbuf_t, isRts bool) bool {
- for i := 0; i < 10; i++ {
- txBuffer.state = 0
+ var (
+ currBuffer *C.rmr_mbuf_t
+ state bool = true
+ counterName string = "Transmitted"
+ )
+
+ txBuffer.state = 0
+ if isRts {
+ currBuffer = C.rmr_rts_msg(m.context, txBuffer)
+ } else {
+ currBuffer = C.rmr_send_msg(m.context, txBuffer)
+ }
+
+ if currBuffer == nil {
+ m.UpdateStatCounter("TransmitError")
+ return m.LogMBufError("SendBuf failed", txBuffer)
+ }
+
+ // Just quick retry seems to help for K8s issue
+ for j := 0; j < 3 && currBuffer != nil && currBuffer.state == C.RMR_ERR_RETRY; j++ {
if isRts {
- txBuffer = C.rmr_rts_msg(m.context, txBuffer)
+ currBuffer = C.rmr_rts_msg(m.context, currBuffer)
} else {
- txBuffer = C.rmr_send_msg(m.context, txBuffer)
- }
-
- if txBuffer == nil {
- break
- } else if txBuffer.state != C.RMR_OK {
- if txBuffer.state != C.RMR_ERR_RETRY {
- time.Sleep(100 * time.Microsecond)
- m.UpdateStatCounter("TransmitError")
- }
- for j := 0; j < 100 && txBuffer.state == C.RMR_ERR_RETRY; j++ {
- txBuffer = C.rmr_send_msg(m.context, txBuffer)
- }
+ currBuffer = C.rmr_send_msg(m.context, currBuffer)
}
+ }
- if txBuffer.state == C.RMR_OK {
- m.UpdateStatCounter("Transmitted")
- return true
- }
+ if currBuffer.state != C.RMR_OK {
+ counterName = "TransmitError"
+ state = m.LogMBufError("SendBuf failed", currBuffer)
}
- m.UpdateStatCounter("TransmitError")
- return false
+
+ m.UpdateStatCounter(counterName)
+ m.Free(currBuffer)
+ return state
}
func (m *RMRClient) UpdateStatCounter(name string) {
return
}
+func (m *RMRClient) LogMBufError(text string, mbuf *C.rmr_mbuf_t) bool {
+ Logger.Debug(fmt.Sprintf("rmrClient: %s -> [tp=%v] %v - %s", text, mbuf.tp_state, mbuf.state, RMRErrors[int(mbuf.state)]))
+ return false
+}
+
// To be removed ...
func (m *RMRClient) GetStat() (r RMRStatistics) {
return