[VOL-2991] Remove incorrect locks from backend.go
This commit consists of the following:
1) Remove the locks in the backend.go that were forcing all
ETCD requests to be serialized.
2) Minor change to the log to check whether a log level is
set before outputting message with serialized content.
Change-Id: I093eae006d9090fbeec75916ad82c329f1c61723
diff --git a/pkg/db/backend.go b/pkg/db/backend.go
index 55fda64..4d29195 100644
--- a/pkg/db/backend.go
+++ b/pkg/db/backend.go
@@ -21,7 +21,6 @@
"errors"
"fmt"
"strconv"
- "sync"
"time"
"github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
@@ -37,7 +36,6 @@
// Backend structure holds details for accessing the kv store
type Backend struct {
- sync.RWMutex
Client kvstore.Client
StoreType string
Host string
@@ -188,9 +186,6 @@
// List retrieves one or more items that match the specified key
func (b *Backend) List(ctx context.Context, key string) (map[string]*kvstore.KVPair, error) {
- b.Lock()
- defer b.Unlock()
-
formattedPath := b.makePath(key)
logger.Debugw("listing-key", log.Fields{"key": key, "path": formattedPath})
@@ -203,9 +198,6 @@
// Get retrieves an item that matches the specified key
func (b *Backend) Get(ctx context.Context, key string) (*kvstore.KVPair, error) {
- b.Lock()
- defer b.Unlock()
-
formattedPath := b.makePath(key)
logger.Debugw("getting-key", log.Fields{"key": key, "path": formattedPath})
@@ -218,9 +210,6 @@
// Put stores an item value under the specifed key
func (b *Backend) Put(ctx context.Context, key string, value interface{}) error {
- b.Lock()
- defer b.Unlock()
-
formattedPath := b.makePath(key)
logger.Debugw("putting-key", log.Fields{"key": key, "path": formattedPath})
@@ -233,9 +222,6 @@
// Delete removes an item under the specified key
func (b *Backend) Delete(ctx context.Context, key string) error {
- b.Lock()
- defer b.Unlock()
-
formattedPath := b.makePath(key)
logger.Debugw("deleting-key", log.Fields{"key": key, "path": formattedPath})
@@ -248,9 +234,6 @@
// CreateWatch starts watching events for the specified key
func (b *Backend) CreateWatch(ctx context.Context, key string, withPrefix bool) chan *kvstore.Event {
- b.Lock()
- defer b.Unlock()
-
formattedPath := b.makePath(key)
logger.Debugw("creating-key-watch", log.Fields{"key": key, "path": formattedPath})
@@ -259,9 +242,6 @@
// DeleteWatch stops watching events for the specified key
func (b *Backend) DeleteWatch(key string, ch chan *kvstore.Event) {
- b.Lock()
- defer b.Unlock()
-
formattedPath := b.makePath(key)
logger.Debugw("deleting-key-watch", log.Fields{"key": key, "path": formattedPath})
diff --git a/pkg/log/log.go b/pkg/log/log.go
index 6b7087f..9e0a0b5 100644
--- a/pkg/log/log.go
+++ b/pkg/log/log.go
@@ -552,7 +552,9 @@
// Debugw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func (l logger) Debugw(msg string, keysAndValues Fields) {
- l.log.Debugw(msg, serializeMap(keysAndValues)...)
+ if l.V(DebugLevel) {
+ l.log.Debugw(msg, serializeMap(keysAndValues)...)
+ }
}
// Info logs a message at level Info on the standard logger.
@@ -575,7 +577,9 @@
// Infow logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func (l logger) Infow(msg string, keysAndValues Fields) {
- l.log.Infow(msg, serializeMap(keysAndValues)...)
+ if l.V(InfoLevel) {
+ l.log.Infow(msg, serializeMap(keysAndValues)...)
+ }
}
// Warn logs a message at level Warn on the standard logger.
@@ -596,7 +600,9 @@
// Warnw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func (l logger) Warnw(msg string, keysAndValues Fields) {
- l.log.Warnw(msg, serializeMap(keysAndValues)...)
+ if l.V(WarnLevel) {
+ l.log.Warnw(msg, serializeMap(keysAndValues)...)
+ }
}
// Error logs a message at level Error on the standard logger.
@@ -617,7 +623,9 @@
// Errorw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func (l logger) Errorw(msg string, keysAndValues Fields) {
- l.log.Errorw(msg, serializeMap(keysAndValues)...)
+ if l.V(ErrorLevel) {
+ l.log.Errorw(msg, serializeMap(keysAndValues)...)
+ }
}
// Fatal logs a message at level Fatal on the standard logger.
@@ -638,7 +646,9 @@
// Fatalw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func (l logger) Fatalw(msg string, keysAndValues Fields) {
- l.log.Fatalw(msg, serializeMap(keysAndValues)...)
+ if l.V(FatalLevel) {
+ l.log.Fatalw(msg, serializeMap(keysAndValues)...)
+ }
}
// Warning logs a message at level Warn on the standard logger.