X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=pkg%2Fxapp%2Frmr.go;h=3a52c97cce50b391714f73473ddbc945b121281e;hb=96be5095a97c975d6644cdae6244366a9d2e59fe;hp=c7b0ff45ad5ff245d0cdd736efa57f84210d0194;hpb=6f0b3180de2528924b801855c5a7b29b013ad561;p=ric-plt%2Fxapp-frame.git diff --git a/pkg/xapp/rmr.go b/pkg/xapp/rmr.go index c7b0ff4..3a52c97 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,45 +94,55 @@ 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) } + 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 } @@ -176,37 +207,20 @@ func (m *RMRClient) Free(mbuf *C.rmr_mbuf_t) { } func (m *RMRClient) SendMsg(params *RMRParams) bool { - return m.SendBuffer(params, false) + return m.Send(params, false) } func (m *RMRClient) SendRts(params *RMRParams) bool { - return m.SendBuffer(params, true) + return m.Send(params, true) } -func (m *RMRClient) SendBuffer(params *RMRParams, isRts bool) bool { - for i := 0; i < 10; i++ { - errCode := m.Send(params, isRts) - if errCode == C.RMR_OK { - m.Free(params.Mbuf) - m.UpdateStatCounter("Transmitted") - return true - } - if errCode != C.RMR_ERR_RETRY { - Logger.Error("rmrClient: rmr_send returned hard error - %d", errCode) - break - } - - } - - m.Free(params.Mbuf) - m.UpdateStatCounter("TransmitError") - return false -} - -func (m *RMRClient) Send(params *RMRParams, isRts bool) C.int { +func (m *RMRClient) Send(params *RMRParams, isRts bool) bool { txBuffer := params.Mbuf if txBuffer == nil { txBuffer = m.Allocate() + if txBuffer == nil { + return false + } } txBuffer.mtype = C.int(params.Mtype) @@ -226,25 +240,52 @@ func (m *RMRClient) Send(params *RMRParams, isRts bool) C.int { } 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(txBuffer, (*C.uchar)(unsafe.Pointer(&b[0])), C.int(len(b))) } } C.write_bytes_array(txBuffer.payload, datap, txBuffer.len) + return m.SendBuf(txBuffer, isRts) +} + +func (m *RMRClient) SendBuf(txBuffer *C.rmr_mbuf_t, isRts bool) bool { + var ( + currBuffer *C.rmr_mbuf_t + state bool = true + counterName string = "Transmitted" + ) + txBuffer.state = 0 - currBuffer := txBuffer if isRts { currBuffer = C.rmr_rts_msg(m.context, txBuffer) } else { currBuffer = C.rmr_send_msg(m.context, txBuffer) } - if currBuffer != nil { - return currBuffer.state + if currBuffer == nil { + m.UpdateStatCounter("TransmitError") + return m.LogMBufError("SendBuf failed", txBuffer) } - return -1 + + // 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 { + currBuffer = C.rmr_rts_msg(m.context, currBuffer) + } else { + currBuffer = C.rmr_send_msg(m.context, currBuffer) + } + } + + if currBuffer.state != C.RMR_OK { + counterName = "TransmitError" + state = m.LogMBufError("SendBuf failed", currBuffer) + } + + m.UpdateStatCounter(counterName) + m.Free(currBuffer) + return state } func (m *RMRClient) UpdateStatCounter(name string) { @@ -284,6 +325,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