Lock tuning and registry release fix. 86/1986/1
authorJuha Hyttinen <juha.hyttinen@nokia.com>
Tue, 10 Dec 2019 10:14:24 +0000 (12:14 +0200)
committerJuha Hyttinen <juha.hyttinen@nokia.com>
Tue, 10 Dec 2019 10:14:57 +0000 (12:14 +0200)
Change-Id: Id075e6f1e2adff994017398f79b4555ac00dfcec
Signed-off-by: Juha Hyttinen <juha.hyttinen@nokia.com>
pkg/control/control.go
pkg/control/registry.go
pkg/control/tracker.go

index 7e2d673..d27844e 100644 (file)
@@ -111,9 +111,10 @@ func (c *Control) Consume(rp *xapp.RMRParams) (err error) {
 func (c *Control) rmrSend(params *xapp.RMRParams) (err error) {
        status := false
        i := 1
-       rmrSendMutex.Lock()
        for ; i <= 10 && status == false; i++ { 
+               rmrSendMutex.Lock()
                status = xapp.Rmr.Send(params, false)
+               rmrSendMutex.Unlock()
                if status == false {
                        xapp.Logger.Info("rmr.Send() failed. Retry count %v, Mtype: %v, SubId: %v, Xid %s",i, params.Mtype, params.SubId, params.Xid)
                        time.Sleep(500 * time.Millisecond)
@@ -123,7 +124,6 @@ func (c *Control) rmrSend(params *xapp.RMRParams) (err error) {
                err = errors.New("rmr.Send() failed")
                xapp.Rmr.Free(params.Mbuf)
        }
-       rmrSendMutex.Unlock()
        
        /*
        if !xapp.Rmr.Send(params, false) {
index 98aa97e..03e90ae 100644 (file)
@@ -24,11 +24,11 @@ import (
        "sync"
 )
 
-var registryMutex = &sync.Mutex{}
 
 type Registry struct {
        register map[uint16]bool
        counter  uint16
+       mutex sync.Mutex
 }
 
 // This method should run as a constructor
@@ -40,8 +40,8 @@ func (r *Registry) Initialize(seedsn uint16) {
 // Reserves and returns the next free sequence number
 func (r *Registry) ReserveSequenceNumber() (uint16, bool) {
        // Check is current SequenceNumber valid
-       registryMutex.Lock()
-       defer registryMutex.Unlock()
+       r.mutex.Lock()
+       defer r.mutex.Unlock()
        sequenceNumber := r.counter
        if _, ok := r.register[sequenceNumber]; ok {
                xapp.Logger.Error("Invalid SeqenceNumber sequenceNumber: %v",sequenceNumber)
@@ -60,8 +60,8 @@ func (r *Registry) ReserveSequenceNumber() (uint16, bool) {
 
 // This function checks the validity of the given subscription id
 func (r *Registry) IsValidSequenceNumber(sn uint16) bool {
-       registryMutex.Lock()
-       defer registryMutex.Unlock()
+       r.mutex.Lock()
+       defer r.mutex.Unlock()
        xapp.Logger.Debug("Registry map: %v", r.register)
        if _, ok := r.register[sn]; ok {
                return true
@@ -71,26 +71,26 @@ func (r *Registry) IsValidSequenceNumber(sn uint16) bool {
 
 // This function sets the give id as confirmed in the register
 func (r *Registry) setSubscriptionToConfirmed(sn uint16) {
-       registryMutex.Lock()
-       defer registryMutex.Unlock()
+       r.mutex.Lock()
+       defer r.mutex.Unlock()
        r.register[sn] = true
 }
 
 //This function sets the given id as unused in the register
 func (r *Registry) deleteSubscription(sn uint16) {
-       registryMutex.Lock()
-       defer registryMutex.Unlock()
+       r.mutex.Lock()
+       defer r.mutex.Unlock()
        r.register[sn] = false
 }
 
 //This function releases the given id as unused in the register
 func (r *Registry) releaseSequenceNumber(sn uint16) bool {
-       registryMutex.Lock()
-       defer registryMutex.Unlock()
-       if r.register[sn] {
-               return false
-               } else {
+       r.mutex.Lock()
+       defer r.mutex.Unlock()
+       if _, ok := r.register[sn]; ok {
                delete(r.register, sn)
                return true
+       } else {
+               return false
        }
 }
index dfab96e..9b984a3 100644 (file)
@@ -24,13 +24,12 @@ import (
        "sync"
 )
 
-var trackerMutex = &sync.Mutex{}
-
 /*
 Implements a record of ongoing transactions and helper functions to CRUD the records.
 */
 type Tracker struct {
        transactionTable map[TransactionKey]Transaction
+       mutex sync.Mutex
 }
 
 func (t *Tracker) Init() {
@@ -42,8 +41,8 @@ Checks if a tranascation with similar type has been ongoing. If not then creates
 Returns error if there is similar transatcion ongoing.
 */
 func (t *Tracker) TrackTransaction(key TransactionKey, xact Transaction) error {
-       trackerMutex.Lock()
-       defer trackerMutex.Unlock()
+       t.mutex.Lock()
+       defer t.mutex.Unlock()
        if _, ok := t.transactionTable[key]; ok {
                // TODO: Implement merge related check here. If the key is same but the value is different.
                err := fmt.Errorf("transaction tracker: Similar transaction with sub id %d and type %s is ongoing", key.SubID, key.transType)
@@ -59,8 +58,8 @@ Returns error in case the transaction cannot be found.
 */
 func (t *Tracker) UpdateTransaction(SubID uint16, transType Action, xact Transaction) error {
        key := TransactionKey{SubID, transType}
-       trackerMutex.Lock()
-       defer trackerMutex.Unlock()
+       t.mutex.Lock()
+       defer t.mutex.Unlock()
        if _, ok := t.transactionTable[key]; ok {
                // TODO: Implement merge related check here. If the key is same but the value is different.
                err := fmt.Errorf("transaction tracker: Similar transaction with sub id %d and type %v is ongoing", key.SubID, key.transType)
@@ -76,8 +75,8 @@ Returns error in case the transaction cannot be found.
 */
 func (t *Tracker) RetriveTransaction(subID uint16, act Action) (Transaction, error) {
        key := TransactionKey{subID, act}
-       trackerMutex.Lock()
-       defer trackerMutex.Unlock()
+       t.mutex.Lock()
+       defer t.mutex.Unlock()
        var xact Transaction
        if xact, ok := t.transactionTable[key]; ok {
                return xact, nil
@@ -93,8 +92,8 @@ Returns error in case the transaction cannot be found.
 func (t *Tracker) completeTransaction(subID uint16, act Action) (Transaction, error) {
        key := TransactionKey{subID, act}
        var emptyTransaction Transaction
-       trackerMutex.Lock()
-       defer trackerMutex.Unlock()
+       t.mutex.Lock()
+       defer t.mutex.Unlock()
        if xact, ok := t.transactionTable[key]; ok {
                delete(t.transactionTable, key)
                return xact, nil