X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=pkg%2Fxapp%2Frmr.go;h=3cb4f8481866919390fc2fbfa757f48c5a348a0b;hb=refs%2Fchanges%2F76%2F1776%2F2;hp=8bbae7b3ee820ce4b249986a9ddfa916935cd4da;hpb=19461e199260b58e24f03c992b27477bb859d691;p=ric-plt%2Fxapp-frame.git diff --git a/pkg/xapp/rmr.go b/pkg/xapp/rmr.go index 8bbae7b..3cb4f84 100755 --- a/pkg/xapp/rmr.go +++ b/pkg/xapp/rmr.go @@ -37,6 +37,7 @@ void write_bytes_array(unsigned char *dst, void *data, int len) { import "C" import ( + "fmt" "github.com/spf13/viper" "strconv" "strings" @@ -51,6 +52,26 @@ var RMRCounterOpts = []CounterOpts{ {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 @@ -62,9 +83,9 @@ type RMRParams struct { 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)) @@ -73,12 +94,18 @@ func NewRMRClient() *RMRClient { } 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) @@ -92,26 +119,26 @@ func (m *RMRClient) Start(c MessageConsumer) { } time.Sleep(10 * time.Second) } - 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 } @@ -165,10 +192,16 @@ func (m *RMRClient) Allocate() *C.rmr_mbuf_t { 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) } @@ -178,14 +211,20 @@ func (m *RMRClient) SendRts(params *RMRParams) bool { } 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) @@ -193,48 +232,56 @@ func (m *RMRClient) Send(params *RMRParams, isRts bool) bool { 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) { @@ -274,6 +321,11 @@ func (m *RMRClient) GetRicMessageName(id int) (s 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