[VOL-3069]Pass Context in methods which are performing logging and need the context
Change-Id: I3d9e1c3eff95d60dde46d44d16bed4805f7447f5
diff --git a/VERSION b/VERSION
index e6291b9..3797f3f 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-3.1.16
+3.1.17
diff --git a/pkg/adapters/adapterif/events_proxy_if.go b/pkg/adapters/adapterif/events_proxy_if.go
index c144935..dbd8140 100644
--- a/pkg/adapters/adapterif/events_proxy_if.go
+++ b/pkg/adapters/adapterif/events_proxy_if.go
@@ -17,14 +17,15 @@
package adapterif
import (
+ "context"
"github.com/opencord/voltha-protos/v3/go/voltha"
)
// EventProxy interface for eventproxy
type EventProxy interface {
- SendDeviceEvent(deviceEvent *voltha.DeviceEvent, category EventCategory,
+ SendDeviceEvent(ctx context.Context, deviceEvent *voltha.DeviceEvent, category EventCategory,
subCategory EventSubCategory, raisedTs int64) error
- SendKpiEvent(id string, deviceEvent *voltha.KpiEvent2, category EventCategory,
+ SendKpiEvent(ctx context.Context, id string, deviceEvent *voltha.KpiEvent2, category EventCategory,
subCategory EventSubCategory, raisedTs int64) error
}
diff --git a/pkg/adapters/common/adapter_proxy.go b/pkg/adapters/common/adapter_proxy.go
index cd5750f..ca44d0d 100644
--- a/pkg/adapters/common/adapter_proxy.go
+++ b/pkg/adapters/common/adapter_proxy.go
@@ -35,14 +35,14 @@
endpointMgr kafka.EndpointManager
}
-func NewAdapterProxy(kafkaProxy kafka.InterContainerProxy, adapterTopic string, coreTopic string, backend *db.Backend) *AdapterProxy {
+func NewAdapterProxy(ctx context.Context, kafkaProxy kafka.InterContainerProxy, adapterTopic string, coreTopic string, backend *db.Backend) *AdapterProxy {
proxy := AdapterProxy{
kafkaICProxy: kafkaProxy,
adapterTopic: adapterTopic,
coreTopic: coreTopic,
endpointMgr: kafka.NewEndpointManager(backend),
}
- logger.Debugw("topics", log.Fields{"core": proxy.coreTopic, "adapter": proxy.adapterTopic})
+ logger.Debugw(ctx, "topics", log.Fields{"core": proxy.coreTopic, "adapter": proxy.adapterTopic})
return &proxy
}
@@ -54,14 +54,14 @@
toDeviceId string,
proxyDeviceId string,
messageId string) error {
- logger.Debugw("sending-inter-adapter-message", log.Fields{"type": msgType, "from": fromAdapter,
+ logger.Debugw(ctx, "sending-inter-adapter-message", log.Fields{"type": msgType, "from": fromAdapter,
"to": toAdapter, "toDevice": toDeviceId, "proxyDevice": proxyDeviceId})
//Marshal the message
var marshalledMsg *any.Any
var err error
if marshalledMsg, err = ptypes.MarshalAny(msg); err != nil {
- logger.Warnw("cannot-marshal-msg", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-marshal-msg", log.Fields{"error": err})
return err
}
@@ -90,7 +90,7 @@
}
// Set up the required rpc arguments
- endpoint, err := ap.endpointMgr.GetEndpoint(toDeviceId, toAdapter)
+ endpoint, err := ap.endpointMgr.GetEndpoint(ctx, toDeviceId, toAdapter)
if err != nil {
return err
}
@@ -99,6 +99,6 @@
rpc := "process_inter_adapter_message"
success, result := ap.kafkaICProxy.InvokeRPC(ctx, rpc, &topic, &replyToTopic, true, proxyDeviceId, args...)
- logger.Debugw("inter-adapter-msg-response", log.Fields{"replyTopic": replyToTopic, "success": success})
- return unPackResponse(rpc, "", success, result)
+ logger.Debugw(ctx, "inter-adapter-msg-response", log.Fields{"replyTopic": replyToTopic, "success": success})
+ return unPackResponse(ctx, rpc, "", success, result)
}
diff --git a/pkg/adapters/common/adapter_proxy_test.go b/pkg/adapters/common/adapter_proxy_test.go
index 3ba8290..01a6603 100644
--- a/pkg/adapters/common/adapter_proxy_test.go
+++ b/pkg/adapters/common/adapter_proxy_test.go
@@ -39,10 +39,11 @@
func init() {
+ ctx := context.Background()
var err error
embedEtcdServerPort, err = freeport.GetFreePort()
if err != nil {
- logger.Fatal("Cannot get freeport for KvClient")
+ logger.Fatal(ctx, "Cannot get freeport for KvClient")
}
}
@@ -54,8 +55,8 @@
Response: &voltha.Device{Id: "testDeviceId"},
},
}
- backend := db.NewBackend("etcd", embedEtcdServerHost+":"+strconv.Itoa(embedEtcdServerPort), defaultTimeout, defaultPathPrefix)
- adapter := NewAdapterProxy(mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic", backend)
+ backend := db.NewBackend(context.Background(), "etcd", embedEtcdServerHost+":"+strconv.Itoa(embedEtcdServerPort), defaultTimeout, defaultPathPrefix)
+ adapter := NewAdapterProxy(context.Background(), mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic", backend)
assert.NotNil(t, adapter)
}
@@ -68,9 +69,9 @@
Response: &voltha.Device{Id: "testDeviceId"},
},
}
- backend := db.NewBackend("etcd", embedEtcdServerHost+":"+strconv.Itoa(embedEtcdServerPort), defaultTimeout, defaultPathPrefix)
+ backend := db.NewBackend(context.Background(), "etcd", embedEtcdServerHost+":"+strconv.Itoa(embedEtcdServerPort), defaultTimeout, defaultPathPrefix)
- adapter := NewAdapterProxy(mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic", backend)
+ adapter := NewAdapterProxy(context.Background(), mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic", backend)
adapter.endpointMgr = mocks.NewEndpointManager()
@@ -112,9 +113,9 @@
Response: &voltha.Device{Id: "testDeviceId"},
},
}
- backend := db.NewBackend("etcd", embedEtcdServerHost+":"+strconv.Itoa(embedEtcdServerPort), defaultTimeout, defaultPathPrefix)
+ backend := db.NewBackend(context.Background(), "etcd", embedEtcdServerHost+":"+strconv.Itoa(embedEtcdServerPort), defaultTimeout, defaultPathPrefix)
- adapter := NewAdapterProxy(mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic", backend)
+ adapter := NewAdapterProxy(context.Background(), mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic", backend)
adapter.endpointMgr = mocks.NewEndpointManager()
@@ -140,9 +141,9 @@
Response: &voltha.Device{Id: "testDeviceId"},
},
}
- backend := db.NewBackend("etcd", embedEtcdServerHost+":"+strconv.Itoa(embedEtcdServerPort), defaultTimeout, defaultPathPrefix)
+ backend := db.NewBackend(context.Background(), "etcd", embedEtcdServerHost+":"+strconv.Itoa(embedEtcdServerPort), defaultTimeout, defaultPathPrefix)
- adapter := NewAdapterProxy(mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic", backend)
+ adapter := NewAdapterProxy(context.Background(), mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic", backend)
adapter.endpointMgr = mocks.NewEndpointManager()
diff --git a/pkg/adapters/common/common.go b/pkg/adapters/common/common.go
index 95a036d..ad8b11b 100644
--- a/pkg/adapters/common/common.go
+++ b/pkg/adapters/common/common.go
@@ -19,12 +19,12 @@
"github.com/opencord/voltha-lib-go/v3/pkg/log"
)
-var logger log.Logger
+var logger log.CLogger
func init() {
// Setup this package so that it's log level can be modified at run time
var err error
- logger, err = log.AddPackage(log.JSON, log.ErrorLevel, log.Fields{"pkg": "common"})
+ logger, err = log.RegisterPackage(log.JSON, log.ErrorLevel, log.Fields{"pkg": "common"})
if err != nil {
panic(err)
}
diff --git a/pkg/adapters/common/core_proxy.go b/pkg/adapters/common/core_proxy.go
index 20e1a52..28b532f 100644
--- a/pkg/adapters/common/core_proxy.go
+++ b/pkg/adapters/common/core_proxy.go
@@ -37,28 +37,28 @@
lockDeviceIdCoreMap sync.RWMutex
}
-func NewCoreProxy(kafkaProxy kafka.InterContainerProxy, adapterTopic string, coreTopic string) *CoreProxy {
+func NewCoreProxy(ctx context.Context, kafkaProxy kafka.InterContainerProxy, adapterTopic string, coreTopic string) *CoreProxy {
var proxy CoreProxy
proxy.kafkaICProxy = kafkaProxy
proxy.adapterTopic = adapterTopic
proxy.coreTopic = coreTopic
proxy.deviceIdCoreMap = make(map[string]string)
proxy.lockDeviceIdCoreMap = sync.RWMutex{}
- logger.Debugw("TOPICS", log.Fields{"core": proxy.coreTopic, "adapter": proxy.adapterTopic})
+ logger.Debugw(ctx, "TOPICS", log.Fields{"core": proxy.coreTopic, "adapter": proxy.adapterTopic})
return &proxy
}
-func unPackResponse(rpc string, deviceId string, success bool, response *a.Any) error {
+func unPackResponse(ctx context.Context, rpc string, deviceId string, success bool, response *a.Any) error {
if success {
return nil
} else {
unpackResult := &ic.Error{}
var err error
if err = ptypes.UnmarshalAny(response, unpackResult); err != nil {
- logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-response", log.Fields{"error": err})
}
- logger.Debugw("response", log.Fields{"rpc": rpc, "deviceId": deviceId, "success": success, "error": err})
+ logger.Debugw(ctx, "response", log.Fields{"rpc": rpc, "deviceId": deviceId, "success": success, "error": err})
// TODO: Need to get the real error code
return status.Errorf(codes.Canceled, "%s", unpackResult.Reason)
}
@@ -94,18 +94,18 @@
}
func (ap *CoreProxy) RegisterAdapter(ctx context.Context, adapter *voltha.Adapter, deviceTypes *voltha.DeviceTypes) error {
- logger.Debugw("registering-adapter", log.Fields{"coreTopic": ap.coreTopic, "adapterTopic": ap.adapterTopic})
+ logger.Debugw(ctx, "registering-adapter", log.Fields{"coreTopic": ap.coreTopic, "adapterTopic": ap.adapterTopic})
rpc := "Register"
topic := kafka.Topic{Name: ap.coreTopic}
replyToTopic := ap.getAdapterTopic()
args := make([]*kafka.KVArg, 2)
if adapter.TotalReplicas == 0 && adapter.CurrentReplica != 0 {
- log.Fatal("totalReplicas can't be 0, since you're here you have at least one")
+ logger.Fatal(ctx, "totalReplicas can't be 0, since you're here you have at least one")
}
if adapter.CurrentReplica == 0 && adapter.TotalReplicas != 0 {
- log.Fatal("currentReplica can't be 0, it has to start from 1")
+ logger.Fatal(ctx, "currentReplica can't be 0, it has to start from 1")
}
if adapter.CurrentReplica == 0 && adapter.TotalReplicas == 0 {
@@ -117,7 +117,7 @@
}
if adapter.CurrentReplica > adapter.TotalReplicas {
- log.Fatalf("CurrentReplica (%d) can't be greater than TotalReplicas (%d)",
+ logger.Fatalf(ctx, "CurrentReplica (%d) can't be greater than TotalReplicas (%d)",
adapter.CurrentReplica, adapter.TotalReplicas)
}
@@ -131,12 +131,12 @@
}
success, result := ap.kafkaICProxy.InvokeRPC(ctx, rpc, &topic, &replyToTopic, true, "", args...)
- logger.Debugw("Register-Adapter-response", log.Fields{"replyTopic": replyToTopic, "success": success})
- return unPackResponse(rpc, "", success, result)
+ logger.Debugw(ctx, "Register-Adapter-response", log.Fields{"replyTopic": replyToTopic, "success": success})
+ return unPackResponse(ctx, rpc, "", success, result)
}
func (ap *CoreProxy) DeviceUpdate(ctx context.Context, device *voltha.Device) error {
- logger.Debugw("DeviceUpdate", log.Fields{"deviceId": device.Id})
+ logger.Debugw(ctx, "DeviceUpdate", log.Fields{"deviceId": device.Id})
rpc := "DeviceUpdate"
toTopic := ap.getCoreTopic(device.Id)
args := make([]*kafka.KVArg, 1)
@@ -147,12 +147,12 @@
// Use a device specific topic as we are the only adaptercore handling requests for this device
replyToTopic := ap.getAdapterTopic()
success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, device.Id, args...)
- logger.Debugw("DeviceUpdate-response", log.Fields{"deviceId": device.Id, "success": success})
- return unPackResponse(rpc, device.Id, success, result)
+ logger.Debugw(ctx, "DeviceUpdate-response", log.Fields{"deviceId": device.Id, "success": success})
+ return unPackResponse(ctx, rpc, device.Id, success, result)
}
func (ap *CoreProxy) PortCreated(ctx context.Context, deviceId string, port *voltha.Port) error {
- logger.Debugw("PortCreated", log.Fields{"portNo": port.PortNo})
+ logger.Debugw(ctx, "PortCreated", log.Fields{"portNo": port.PortNo})
rpc := "PortCreated"
// Use a device specific topic to send the request. The adapter handling the device creates a device
// specific topic
@@ -171,12 +171,12 @@
// Use a device specific topic as we are the only adaptercore handling requests for this device
replyToTopic := ap.getAdapterTopic()
success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, deviceId, args...)
- logger.Debugw("PortCreated-response", log.Fields{"deviceId": deviceId, "success": success})
- return unPackResponse(rpc, deviceId, success, result)
+ logger.Debugw(ctx, "PortCreated-response", log.Fields{"deviceId": deviceId, "success": success})
+ return unPackResponse(ctx, rpc, deviceId, success, result)
}
func (ap *CoreProxy) PortsStateUpdate(ctx context.Context, deviceId string, operStatus voltha.OperStatus_Types) error {
- logger.Debugw("PortsStateUpdate", log.Fields{"deviceId": deviceId})
+ logger.Debugw(ctx, "PortsStateUpdate", log.Fields{"deviceId": deviceId})
rpc := "PortsStateUpdate"
// Use a device specific topic to send the request. The adapter handling the device creates a device
// specific topic
@@ -197,12 +197,12 @@
// Use a device specific topic as we are the only adaptercore handling requests for this device
replyToTopic := ap.getAdapterTopic()
success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, deviceId, args...)
- logger.Debugw("PortsStateUpdate-response", log.Fields{"deviceId": deviceId, "success": success})
- return unPackResponse(rpc, deviceId, success, result)
+ logger.Debugw(ctx, "PortsStateUpdate-response", log.Fields{"deviceId": deviceId, "success": success})
+ return unPackResponse(ctx, rpc, deviceId, success, result)
}
func (ap *CoreProxy) DeleteAllPorts(ctx context.Context, deviceId string) error {
- logger.Debugw("DeleteAllPorts", log.Fields{"deviceId": deviceId})
+ logger.Debugw(ctx, "DeleteAllPorts", log.Fields{"deviceId": deviceId})
rpc := "DeleteAllPorts"
// Use a device specific topic to send the request. The adapter handling the device creates a device
// specific topic
@@ -218,13 +218,13 @@
// Use a device specific topic as we are the only adaptercore handling requests for this device
replyToTopic := ap.getAdapterTopic()
success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, deviceId, args...)
- logger.Debugw("DeleteAllPorts-response", log.Fields{"deviceId": deviceId, "success": success})
- return unPackResponse(rpc, deviceId, success, result)
+ logger.Debugw(ctx, "DeleteAllPorts-response", log.Fields{"deviceId": deviceId, "success": success})
+ return unPackResponse(ctx, rpc, deviceId, success, result)
}
func (ap *CoreProxy) DeviceStateUpdate(ctx context.Context, deviceId string,
connStatus voltha.ConnectStatus_Types, operStatus voltha.OperStatus_Types) error {
- logger.Debugw("DeviceStateUpdate", log.Fields{"deviceId": deviceId})
+ logger.Debugw(ctx, "DeviceStateUpdate", log.Fields{"deviceId": deviceId})
rpc := "DeviceStateUpdate"
// Use a device specific topic to send the request. The adapter handling the device creates a device
// specific topic
@@ -249,13 +249,13 @@
// Use a device specific topic as we are the only adaptercore handling requests for this device
replyToTopic := ap.getAdapterTopic()
success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, deviceId, args...)
- logger.Debugw("DeviceStateUpdate-response", log.Fields{"deviceId": deviceId, "success": success})
- return unPackResponse(rpc, deviceId, success, result)
+ logger.Debugw(ctx, "DeviceStateUpdate-response", log.Fields{"deviceId": deviceId, "success": success})
+ return unPackResponse(ctx, rpc, deviceId, success, result)
}
func (ap *CoreProxy) ChildDeviceDetected(ctx context.Context, parentDeviceId string, parentPortNo int,
childDeviceType string, channelId int, vendorId string, serialNumber string, onuId int64) (*voltha.Device, error) {
- logger.Debugw("ChildDeviceDetected", log.Fields{"pDeviceId": parentDeviceId, "channelId": channelId})
+ logger.Debugw(ctx, "ChildDeviceDetected", log.Fields{"pDeviceId": parentDeviceId, "channelId": channelId})
rpc := "ChildDeviceDetected"
// Use a device specific topic to send the request. The adapter handling the device creates a device
// specific topic
@@ -300,12 +300,12 @@
}
success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, parentDeviceId, args...)
- logger.Debugw("ChildDeviceDetected-response", log.Fields{"pDeviceId": parentDeviceId, "success": success})
+ logger.Debugw(ctx, "ChildDeviceDetected-response", log.Fields{"pDeviceId": parentDeviceId, "success": success})
if success {
volthaDevice := &voltha.Device{}
if err := ptypes.UnmarshalAny(result, volthaDevice); err != nil {
- logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-response", log.Fields{"error": err})
return nil, status.Error(codes.InvalidArgument, err.Error())
}
return volthaDevice, nil
@@ -313,17 +313,17 @@
unpackResult := &ic.Error{}
var err error
if err = ptypes.UnmarshalAny(result, unpackResult); err != nil {
- logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-response", log.Fields{"error": err})
}
- logger.Debugw("ChildDeviceDetected-return", log.Fields{"deviceid": parentDeviceId, "success": success, "error": err})
+ logger.Debugw(ctx, "ChildDeviceDetected-return", log.Fields{"deviceid": parentDeviceId, "success": success, "error": err})
- return nil, status.Error(ICProxyErrorCodeToGrpcErrorCode(unpackResult.Code), unpackResult.Reason)
+ return nil, status.Error(ICProxyErrorCodeToGrpcErrorCode(ctx, unpackResult.Code), unpackResult.Reason)
}
}
func (ap *CoreProxy) ChildDevicesLost(ctx context.Context, parentDeviceId string) error {
- logger.Debugw("ChildDevicesLost", log.Fields{"pDeviceId": parentDeviceId})
+ logger.Debugw(ctx, "ChildDevicesLost", log.Fields{"pDeviceId": parentDeviceId})
rpc := "ChildDevicesLost"
// Use a device specific topic to send the request. The adapter handling the device creates a device
// specific topic
@@ -338,12 +338,12 @@
}
success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, parentDeviceId, args...)
- logger.Debugw("ChildDevicesLost-response", log.Fields{"pDeviceId": parentDeviceId, "success": success})
- return unPackResponse(rpc, parentDeviceId, success, result)
+ logger.Debugw(ctx, "ChildDevicesLost-response", log.Fields{"pDeviceId": parentDeviceId, "success": success})
+ return unPackResponse(ctx, rpc, parentDeviceId, success, result)
}
func (ap *CoreProxy) ChildDevicesDetected(ctx context.Context, parentDeviceId string) error {
- logger.Debugw("ChildDevicesDetected", log.Fields{"pDeviceId": parentDeviceId})
+ logger.Debugw(ctx, "ChildDevicesDetected", log.Fields{"pDeviceId": parentDeviceId})
rpc := "ChildDevicesDetected"
// Use a device specific topic to send the request. The adapter handling the device creates a device
// specific topic
@@ -358,12 +358,12 @@
}
success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, parentDeviceId, args...)
- logger.Debugw("ChildDevicesDetected-response", log.Fields{"pDeviceId": parentDeviceId, "success": success})
- return unPackResponse(rpc, parentDeviceId, success, result)
+ logger.Debugw(ctx, "ChildDevicesDetected-response", log.Fields{"pDeviceId": parentDeviceId, "success": success})
+ return unPackResponse(ctx, rpc, parentDeviceId, success, result)
}
func (ap *CoreProxy) GetDevice(ctx context.Context, parentDeviceId string, deviceId string) (*voltha.Device, error) {
- logger.Debugw("GetDevice", log.Fields{"deviceId": deviceId})
+ logger.Debugw(ctx, "GetDevice", log.Fields{"deviceId": deviceId})
rpc := "GetDevice"
toTopic := ap.getCoreTopic(parentDeviceId)
@@ -377,12 +377,12 @@
}
success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, parentDeviceId, args...)
- logger.Debugw("GetDevice-response", log.Fields{"pDeviceId": parentDeviceId, "success": success})
+ logger.Debugw(ctx, "GetDevice-response", log.Fields{"pDeviceId": parentDeviceId, "success": success})
if success {
volthaDevice := &voltha.Device{}
if err := ptypes.UnmarshalAny(result, volthaDevice); err != nil {
- logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-response", log.Fields{"error": err})
return nil, status.Error(codes.InvalidArgument, err.Error())
}
return volthaDevice, nil
@@ -390,16 +390,16 @@
unpackResult := &ic.Error{}
var err error
if err = ptypes.UnmarshalAny(result, unpackResult); err != nil {
- logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-response", log.Fields{"error": err})
}
- logger.Debugw("GetDevice-return", log.Fields{"deviceid": parentDeviceId, "success": success, "error": err})
+ logger.Debugw(ctx, "GetDevice-return", log.Fields{"deviceid": parentDeviceId, "success": success, "error": err})
// TODO: Need to get the real error code
- return nil, status.Error(ICProxyErrorCodeToGrpcErrorCode(unpackResult.Code), unpackResult.Reason)
+ return nil, status.Error(ICProxyErrorCodeToGrpcErrorCode(ctx, unpackResult.Code), unpackResult.Reason)
}
}
func (ap *CoreProxy) GetChildDevice(ctx context.Context, parentDeviceId string, kwargs map[string]interface{}) (*voltha.Device, error) {
- logger.Debugw("GetChildDevice", log.Fields{"parentDeviceId": parentDeviceId, "kwargs": kwargs})
+ logger.Debugw(ctx, "GetChildDevice", log.Fields{"parentDeviceId": parentDeviceId, "kwargs": kwargs})
rpc := "GetChildDevice"
toTopic := ap.getCoreTopic(parentDeviceId)
@@ -437,12 +437,12 @@
}
success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, parentDeviceId, args...)
- logger.Debugw("GetChildDevice-response", log.Fields{"pDeviceId": parentDeviceId, "success": success})
+ logger.Debugw(ctx, "GetChildDevice-response", log.Fields{"pDeviceId": parentDeviceId, "success": success})
if success {
volthaDevice := &voltha.Device{}
if err := ptypes.UnmarshalAny(result, volthaDevice); err != nil {
- logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-response", log.Fields{"error": err})
return nil, status.Error(codes.InvalidArgument, err.Error())
}
return volthaDevice, nil
@@ -450,16 +450,16 @@
unpackResult := &ic.Error{}
var err error
if err = ptypes.UnmarshalAny(result, unpackResult); err != nil {
- logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-response", log.Fields{"error": err})
}
- logger.Debugw("GetChildDevice-return", log.Fields{"deviceid": parentDeviceId, "success": success, "error": err})
+ logger.Debugw(ctx, "GetChildDevice-return", log.Fields{"deviceid": parentDeviceId, "success": success, "error": err})
- return nil, status.Error(ICProxyErrorCodeToGrpcErrorCode(unpackResult.Code), unpackResult.Reason)
+ return nil, status.Error(ICProxyErrorCodeToGrpcErrorCode(ctx, unpackResult.Code), unpackResult.Reason)
}
}
func (ap *CoreProxy) GetChildDevices(ctx context.Context, parentDeviceId string) (*voltha.Devices, error) {
- logger.Debugw("GetChildDevices", log.Fields{"parentDeviceId": parentDeviceId})
+ logger.Debugw(ctx, "GetChildDevices", log.Fields{"parentDeviceId": parentDeviceId})
rpc := "GetChildDevices"
toTopic := ap.getCoreTopic(parentDeviceId)
@@ -473,12 +473,12 @@
}
success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, parentDeviceId, args...)
- logger.Debugw("GetChildDevices-response", log.Fields{"pDeviceId": parentDeviceId, "success": success})
+ logger.Debugw(ctx, "GetChildDevices-response", log.Fields{"pDeviceId": parentDeviceId, "success": success})
if success {
volthaDevices := &voltha.Devices{}
if err := ptypes.UnmarshalAny(result, volthaDevices); err != nil {
- logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-response", log.Fields{"error": err})
return nil, status.Error(codes.InvalidArgument, err.Error())
}
return volthaDevices, nil
@@ -486,16 +486,16 @@
unpackResult := &ic.Error{}
var err error
if err = ptypes.UnmarshalAny(result, unpackResult); err != nil {
- logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-response", log.Fields{"error": err})
}
- logger.Debugw("GetChildDevices-return", log.Fields{"deviceid": parentDeviceId, "success": success, "error": err})
+ logger.Debugw(ctx, "GetChildDevices-return", log.Fields{"deviceid": parentDeviceId, "success": success, "error": err})
- return nil, status.Error(ICProxyErrorCodeToGrpcErrorCode(unpackResult.Code), unpackResult.Reason)
+ return nil, status.Error(ICProxyErrorCodeToGrpcErrorCode(ctx, unpackResult.Code), unpackResult.Reason)
}
}
func (ap *CoreProxy) SendPacketIn(ctx context.Context, deviceId string, port uint32, pktPayload []byte) error {
- logger.Debugw("SendPacketIn", log.Fields{"deviceId": deviceId, "port": port, "pktPayload": pktPayload})
+ logger.Debugw(ctx, "SendPacketIn", log.Fields{"deviceId": deviceId, "port": port, "pktPayload": pktPayload})
rpc := "PacketIn"
// Use a device specific topic to send the request. The adapter handling the device creates a device
// specific topic
@@ -519,12 +519,12 @@
Value: pkt,
}
success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, deviceId, args...)
- logger.Debugw("SendPacketIn-response", log.Fields{"pDeviceId": deviceId, "success": success})
- return unPackResponse(rpc, deviceId, success, result)
+ logger.Debugw(ctx, "SendPacketIn-response", log.Fields{"pDeviceId": deviceId, "success": success})
+ return unPackResponse(ctx, rpc, deviceId, success, result)
}
func (ap *CoreProxy) DeviceReasonUpdate(ctx context.Context, deviceId string, deviceReason string) error {
- logger.Debugw("DeviceReasonUpdate", log.Fields{"deviceId": deviceId, "deviceReason": deviceReason})
+ logger.Debugw(ctx, "DeviceReasonUpdate", log.Fields{"deviceId": deviceId, "deviceReason": deviceReason})
rpc := "DeviceReasonUpdate"
// Use a device specific topic to send the request. The adapter handling the device creates a device
// specific topic
@@ -543,12 +543,12 @@
Value: reason,
}
success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, deviceId, args...)
- logger.Debugw("DeviceReason-response", log.Fields{"pDeviceId": deviceId, "success": success})
- return unPackResponse(rpc, deviceId, success, result)
+ logger.Debugw(ctx, "DeviceReason-response", log.Fields{"pDeviceId": deviceId, "success": success})
+ return unPackResponse(ctx, rpc, deviceId, success, result)
}
func (ap *CoreProxy) DevicePMConfigUpdate(ctx context.Context, pmConfigs *voltha.PmConfigs) error {
- logger.Debugw("DevicePMConfigUpdate", log.Fields{"pmConfigs": pmConfigs})
+ logger.Debugw(ctx, "DevicePMConfigUpdate", log.Fields{"pmConfigs": pmConfigs})
rpc := "DevicePMConfigUpdate"
// Use a device specific topic to send the request. The adapter handling the device creates a device
// specific topic
@@ -561,12 +561,12 @@
Value: pmConfigs,
}
success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, pmConfigs.Id, args...)
- logger.Debugw("DevicePMConfigUpdate-response", log.Fields{"pDeviceId": pmConfigs.Id, "success": success})
- return unPackResponse(rpc, pmConfigs.Id, success, result)
+ logger.Debugw(ctx, "DevicePMConfigUpdate-response", log.Fields{"pDeviceId": pmConfigs.Id, "success": success})
+ return unPackResponse(ctx, rpc, pmConfigs.Id, success, result)
}
func (ap *CoreProxy) ReconcileChildDevices(ctx context.Context, parentDeviceId string) error {
- logger.Debugw("ReconcileChildDevices", log.Fields{"parentDeviceId": parentDeviceId})
+ logger.Debugw(ctx, "ReconcileChildDevices", log.Fields{"parentDeviceId": parentDeviceId})
rpc := "ReconcileChildDevices"
// Use a device specific topic to send the request. The adapter handling the device creates a device
// specific topic
@@ -578,13 +578,13 @@
}
success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, parentDeviceId, args...)
- logger.Debugw("ReconcileChildDevices-response", log.Fields{"pDeviceId": parentDeviceId, "success": success})
- return unPackResponse(rpc, parentDeviceId, success, result)
+ logger.Debugw(ctx, "ReconcileChildDevices-response", log.Fields{"pDeviceId": parentDeviceId, "success": success})
+ return unPackResponse(ctx, rpc, parentDeviceId, success, result)
}
func (ap *CoreProxy) PortStateUpdate(ctx context.Context, deviceId string, pType voltha.Port_PortType, portNum uint32,
operStatus voltha.OperStatus_Types) error {
- logger.Debugw("PortStateUpdate", log.Fields{"deviceId": deviceId, "portType": pType, "portNo": portNum, "operation_status": operStatus})
+ logger.Debugw(ctx, "PortStateUpdate", log.Fields{"deviceId": deviceId, "portType": pType, "portNo": portNum, "operation_status": operStatus})
rpc := "PortStateUpdate"
// Use a device specific topic to send the request. The adapter handling the device creates a device
// specific topic
@@ -615,6 +615,6 @@
// Use a device specific topic as we are the only adaptercore handling requests for this device
replyToTopic := ap.getAdapterTopic()
success, result := ap.kafkaICProxy.InvokeRPC(context.Background(), rpc, &toTopic, &replyToTopic, true, deviceId, args...)
- logger.Debugw("PortStateUpdate-response", log.Fields{"deviceId": deviceId, "success": success})
- return unPackResponse(rpc, deviceId, success, result)
+ logger.Debugw(ctx, "PortStateUpdate-response", log.Fields{"deviceId": deviceId, "success": success})
+ return unPackResponse(ctx, rpc, deviceId, success, result)
}
diff --git a/pkg/adapters/common/core_proxy_test.go b/pkg/adapters/common/core_proxy_test.go
index a1b4290..2fb4df7 100644
--- a/pkg/adapters/common/core_proxy_test.go
+++ b/pkg/adapters/common/core_proxy_test.go
@@ -45,7 +45,7 @@
},
}
- proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
+ proxy := NewCoreProxy(context.Background(), &mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
adapter := &voltha.Adapter{
Id: "testAdapter",
@@ -88,7 +88,7 @@
},
}
- proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
+ proxy := NewCoreProxy(context.Background(), &mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
adapter := &voltha.Adapter{
Id: "testAdapter",
@@ -128,7 +128,7 @@
},
}
- proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
+ proxy := NewCoreProxy(context.Background(), &mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
kwargs := make(map[string]interface{})
kwargs["serial_number"] = "TEST00000000001"
@@ -159,7 +159,7 @@
},
}
- proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
+ proxy := NewCoreProxy(context.Background(), &mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
kwargs := make(map[string]interface{})
kwargs["onu_id"] = uint32(1234)
@@ -190,7 +190,7 @@
},
}
- proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
+ proxy := NewCoreProxy(context.Background(), &mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
kwargs := make(map[string]interface{})
kwargs["onu_id"] = uint32(1234)
@@ -213,7 +213,7 @@
},
}
- proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
+ proxy := NewCoreProxy(context.Background(), &mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
kwargs := make(map[string]interface{})
kwargs["onu_id"] = uint32(1234)
@@ -241,7 +241,7 @@
},
}
- proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
+ proxy := NewCoreProxy(context.Background(), &mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
parentDeviceId := "aabbcc"
devices, error := proxy.GetChildDevices(context.TODO(), parentDeviceId)
@@ -268,7 +268,7 @@
},
}
- proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
+ proxy := NewCoreProxy(context.Background(), &mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
parentDeviceId := "aabbcc"
devices, error := proxy.GetChildDevices(context.TODO(), parentDeviceId)
@@ -288,7 +288,7 @@
},
}
- proxy := NewCoreProxy(&mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
+ proxy := NewCoreProxy(context.Background(), &mockKafkaIcProxy, "testAdapterTopic", "testCoreTopic")
parentDeviceId := "aabbcc"
devices, error := proxy.GetChildDevices(context.TODO(), parentDeviceId)
diff --git a/pkg/adapters/common/events_proxy.go b/pkg/adapters/common/events_proxy.go
index da9c9eb..b79bafe 100644
--- a/pkg/adapters/common/events_proxy.go
+++ b/pkg/adapters/common/events_proxy.go
@@ -17,6 +17,7 @@
package common
import (
+ "context"
"errors"
"fmt"
"strconv"
@@ -96,9 +97,9 @@
}
/* Send out device events*/
-func (ep *EventProxy) SendDeviceEvent(deviceEvent *voltha.DeviceEvent, category adapterif.EventCategory, subCategory adapterif.EventSubCategory, raisedTs int64) error {
+func (ep *EventProxy) SendDeviceEvent(ctx context.Context, deviceEvent *voltha.DeviceEvent, category adapterif.EventCategory, subCategory adapterif.EventSubCategory, raisedTs int64) error {
if deviceEvent == nil {
- logger.Error("Recieved empty device event")
+ logger.Error(ctx, "Recieved empty device event")
return errors.New("Device event nil")
}
var event voltha.Event
@@ -109,11 +110,11 @@
return err
}
event.EventType = &de
- if err := ep.sendEvent(&event); err != nil {
- logger.Errorw("Failed to send device event to KAFKA bus", log.Fields{"device-event": deviceEvent})
+ if err := ep.sendEvent(ctx, &event); err != nil {
+ logger.Errorw(ctx, "Failed to send device event to KAFKA bus", log.Fields{"device-event": deviceEvent})
return err
}
- logger.Infow("Successfully sent device event KAFKA", log.Fields{"Id": event.Header.Id, "Category": event.Header.Category,
+ logger.Infow(ctx, "Successfully sent device event KAFKA", log.Fields{"Id": event.Header.Id, "Category": event.Header.Category,
"SubCategory": event.Header.SubCategory, "Type": event.Header.Type, "TypeVersion": event.Header.TypeVersion,
"ReportedTs": event.Header.ReportedTs, "ResourceId": deviceEvent.ResourceId, "Context": deviceEvent.Context,
"DeviceEventName": deviceEvent.DeviceEventName})
@@ -123,9 +124,9 @@
}
// SendKpiEvent is to send kpi events to voltha.event topic
-func (ep *EventProxy) SendKpiEvent(id string, kpiEvent *voltha.KpiEvent2, category adapterif.EventCategory, subCategory adapterif.EventSubCategory, raisedTs int64) error {
+func (ep *EventProxy) SendKpiEvent(ctx context.Context, id string, kpiEvent *voltha.KpiEvent2, category adapterif.EventCategory, subCategory adapterif.EventSubCategory, raisedTs int64) error {
if kpiEvent == nil {
- logger.Error("Recieved empty kpi event")
+ logger.Error(ctx, "Recieved empty kpi event")
return errors.New("KPI event nil")
}
var event voltha.Event
@@ -136,11 +137,11 @@
return err
}
event.EventType = &de
- if err := ep.sendEvent(&event); err != nil {
- logger.Errorw("Failed to send kpi event to KAFKA bus", log.Fields{"device-event": kpiEvent})
+ if err := ep.sendEvent(ctx, &event); err != nil {
+ logger.Errorw(ctx, "Failed to send kpi event to KAFKA bus", log.Fields{"device-event": kpiEvent})
return err
}
- logger.Infow("Successfully sent kpi event to KAFKA", log.Fields{"Id": event.Header.Id, "Category": event.Header.Category,
+ logger.Infow(ctx, "Successfully sent kpi event to KAFKA", log.Fields{"Id": event.Header.Id, "Category": event.Header.Category,
"SubCategory": event.Header.SubCategory, "Type": event.Header.Type, "TypeVersion": event.Header.TypeVersion,
"ReportedTs": event.Header.ReportedTs, "KpiEventName": "STATS_EVENT"})
@@ -150,11 +151,11 @@
/* TODO: Send out KPI events*/
-func (ep *EventProxy) sendEvent(event *voltha.Event) error {
- if err := ep.kafkaClient.Send(event, &ep.eventTopic); err != nil {
+func (ep *EventProxy) sendEvent(ctx context.Context, event *voltha.Event) error {
+ if err := ep.kafkaClient.Send(ctx, event, &ep.eventTopic); err != nil {
return err
}
- logger.Debugw("Sent event to kafka", log.Fields{"event": event})
+ logger.Debugw(ctx, "Sent event to kafka", log.Fields{"event": event})
return nil
}
diff --git a/pkg/adapters/common/request_handler.go b/pkg/adapters/common/request_handler.go
index 62d8cdd..a92ed51 100644
--- a/pkg/adapters/common/request_handler.go
+++ b/pkg/adapters/common/request_handler.go
@@ -16,6 +16,7 @@
package common
import (
+ "context"
"errors"
"github.com/golang/protobuf/ptypes"
@@ -58,9 +59,9 @@
return nil, nil
}
-func (rhp *RequestHandlerProxy) Adopt_device(args []*ic.Argument) (*empty.Empty, error) {
+func (rhp *RequestHandlerProxy) Adopt_device(ctx context.Context, args []*ic.Argument) (*empty.Empty, error) {
if len(args) < 3 {
- logger.Warn("invalid-number-of-args", log.Fields{"args": args})
+ logger.Warn(ctx, "invalid-number-of-args", log.Fields{"args": args})
err := errors.New("invalid-number-of-args")
return nil, err
}
@@ -71,38 +72,38 @@
switch arg.Key {
case "device":
if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
- logger.Warnw("cannot-unmarshal-device", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-device", log.Fields{"error": err})
return nil, err
}
case kafka.TransactionKey:
if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
- logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-transaction-ID", log.Fields{"error": err})
return nil, err
}
case kafka.FromTopic:
if err := ptypes.UnmarshalAny(arg.Value, fromTopic); err != nil {
- logger.Warnw("cannot-unmarshal-from-topic", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-from-topic", log.Fields{"error": err})
return nil, err
}
}
}
- logger.Debugw("Adopt_device", log.Fields{"deviceId": device.Id})
+ logger.Debugw(ctx, "Adopt_device", log.Fields{"deviceId": device.Id})
//Update the core reference for that device
rhp.coreProxy.UpdateCoreReference(device.Id, fromTopic.Val)
//Invoke the adopt device on the adapter
- if err := rhp.adapter.Adopt_device(device); err != nil {
+ if err := rhp.adapter.Adopt_device(ctx, device); err != nil {
return nil, status.Errorf(codes.NotFound, "%s", err.Error())
}
return new(empty.Empty), nil
}
-func (rhp *RequestHandlerProxy) Reconcile_device(args []*ic.Argument) (*empty.Empty, error) {
+func (rhp *RequestHandlerProxy) Reconcile_device(ctx context.Context, args []*ic.Argument) (*empty.Empty, error) {
if len(args) < 3 {
- logger.Warn("invalid-number-of-args", log.Fields{"args": args})
+ logger.Warn(ctx, "invalid-number-of-args", log.Fields{"args": args})
err := errors.New("invalid-number-of-args")
return nil, err
}
@@ -114,17 +115,17 @@
switch arg.Key {
case "device":
if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
- logger.Warnw("cannot-unmarshal-device", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-device", log.Fields{"error": err})
return nil, err
}
case kafka.TransactionKey:
if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
- logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-transaction-ID", log.Fields{"error": err})
return nil, err
}
case kafka.FromTopic:
if err := ptypes.UnmarshalAny(arg.Value, fromTopic); err != nil {
- logger.Warnw("cannot-unmarshal-from-topic", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-from-topic", log.Fields{"error": err})
return nil, err
}
}
@@ -133,7 +134,7 @@
rhp.coreProxy.UpdateCoreReference(device.Id, fromTopic.Val)
//Invoke the reconcile device API on the adapter
- if err := rhp.adapter.Reconcile_device(device); err != nil {
+ if err := rhp.adapter.Reconcile_device(ctx, device); err != nil {
return nil, status.Errorf(codes.NotFound, "%s", err.Error())
}
return new(empty.Empty), nil
@@ -143,9 +144,9 @@
return new(empty.Empty), nil
}
-func (rhp *RequestHandlerProxy) Disable_device(args []*ic.Argument) (*empty.Empty, error) {
+func (rhp *RequestHandlerProxy) Disable_device(ctx context.Context, args []*ic.Argument) (*empty.Empty, error) {
if len(args) < 3 {
- logger.Warn("invalid-number-of-args", log.Fields{"args": args})
+ logger.Warn(ctx, "invalid-number-of-args", log.Fields{"args": args})
err := errors.New("invalid-number-of-args")
return nil, err
}
@@ -157,17 +158,17 @@
switch arg.Key {
case "device":
if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
- logger.Warnw("cannot-unmarshal-device", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-device", log.Fields{"error": err})
return nil, err
}
case kafka.TransactionKey:
if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
- logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-transaction-ID", log.Fields{"error": err})
return nil, err
}
case kafka.FromTopic:
if err := ptypes.UnmarshalAny(arg.Value, fromTopic); err != nil {
- logger.Warnw("cannot-unmarshal-from-topic", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-from-topic", log.Fields{"error": err})
return nil, err
}
}
@@ -175,15 +176,15 @@
//Update the core reference for that device
rhp.coreProxy.UpdateCoreReference(device.Id, fromTopic.Val)
//Invoke the Disable_device API on the adapter
- if err := rhp.adapter.Disable_device(device); err != nil {
+ if err := rhp.adapter.Disable_device(ctx, device); err != nil {
return nil, status.Errorf(codes.NotFound, "%s", err.Error())
}
return new(empty.Empty), nil
}
-func (rhp *RequestHandlerProxy) Reenable_device(args []*ic.Argument) (*empty.Empty, error) {
+func (rhp *RequestHandlerProxy) Reenable_device(ctx context.Context, args []*ic.Argument) (*empty.Empty, error) {
if len(args) < 3 {
- logger.Warn("invalid-number-of-args", log.Fields{"args": args})
+ logger.Warn(ctx, "invalid-number-of-args", log.Fields{"args": args})
err := errors.New("invalid-number-of-args")
return nil, err
}
@@ -195,17 +196,17 @@
switch arg.Key {
case "device":
if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
- logger.Warnw("cannot-unmarshal-device", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-device", log.Fields{"error": err})
return nil, err
}
case kafka.TransactionKey:
if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
- logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-transaction-ID", log.Fields{"error": err})
return nil, err
}
case kafka.FromTopic:
if err := ptypes.UnmarshalAny(arg.Value, fromTopic); err != nil {
- logger.Warnw("cannot-unmarshal-from-topic", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-from-topic", log.Fields{"error": err})
return nil, err
}
}
@@ -213,15 +214,15 @@
//Update the core reference for that device
rhp.coreProxy.UpdateCoreReference(device.Id, fromTopic.Val)
//Invoke the Reenable_device API on the adapter
- if err := rhp.adapter.Reenable_device(device); err != nil {
+ if err := rhp.adapter.Reenable_device(ctx, device); err != nil {
return nil, status.Errorf(codes.NotFound, "%s", err.Error())
}
return new(empty.Empty), nil
}
-func (rhp *RequestHandlerProxy) Reboot_device(args []*ic.Argument) (*empty.Empty, error) {
+func (rhp *RequestHandlerProxy) Reboot_device(ctx context.Context, args []*ic.Argument) (*empty.Empty, error) {
if len(args) < 3 {
- logger.Warn("invalid-number-of-args", log.Fields{"args": args})
+ logger.Warn(ctx, "invalid-number-of-args", log.Fields{"args": args})
err := errors.New("invalid-number-of-args")
return nil, err
}
@@ -233,17 +234,17 @@
switch arg.Key {
case "device":
if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
- logger.Warnw("cannot-unmarshal-device", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-device", log.Fields{"error": err})
return nil, err
}
case kafka.TransactionKey:
if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
- logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-transaction-ID", log.Fields{"error": err})
return nil, err
}
case kafka.FromTopic:
if err := ptypes.UnmarshalAny(arg.Value, fromTopic); err != nil {
- logger.Warnw("cannot-unmarshal-from-topic", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-from-topic", log.Fields{"error": err})
return nil, err
}
}
@@ -251,7 +252,7 @@
//Update the core reference for that device
rhp.coreProxy.UpdateCoreReference(device.Id, fromTopic.Val)
//Invoke the Reboot_device API on the adapter
- if err := rhp.adapter.Reboot_device(device); err != nil {
+ if err := rhp.adapter.Reboot_device(ctx, device); err != nil {
return nil, status.Errorf(codes.NotFound, "%s", err.Error())
}
return new(empty.Empty), nil
@@ -262,9 +263,9 @@
return new(empty.Empty), nil
}
-func (rhp *RequestHandlerProxy) Delete_device(args []*ic.Argument) (*empty.Empty, error) {
+func (rhp *RequestHandlerProxy) Delete_device(ctx context.Context, args []*ic.Argument) (*empty.Empty, error) {
if len(args) < 3 {
- logger.Warn("invalid-number-of-args", log.Fields{"args": args})
+ logger.Warn(ctx, "invalid-number-of-args", log.Fields{"args": args})
err := errors.New("invalid-number-of-args")
return nil, err
}
@@ -276,17 +277,17 @@
switch arg.Key {
case "device":
if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
- logger.Warnw("cannot-unmarshal-device", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-device", log.Fields{"error": err})
return nil, err
}
case kafka.TransactionKey:
if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
- logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-transaction-ID", log.Fields{"error": err})
return nil, err
}
case kafka.FromTopic:
if err := ptypes.UnmarshalAny(arg.Value, fromTopic); err != nil {
- logger.Warnw("cannot-unmarshal-from-topic", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-from-topic", log.Fields{"error": err})
return nil, err
}
}
@@ -294,7 +295,7 @@
//Update the core reference for that device
rhp.coreProxy.UpdateCoreReference(device.Id, fromTopic.Val)
//Invoke the delete_device API on the adapter
- if err := rhp.adapter.Delete_device(device); err != nil {
+ if err := rhp.adapter.Delete_device(ctx, device); err != nil {
return nil, status.Errorf(codes.NotFound, "%s", err.Error())
}
return new(empty.Empty), nil
@@ -304,10 +305,10 @@
return new(empty.Empty), nil
}
-func (rhp *RequestHandlerProxy) Update_flows_bulk(args []*ic.Argument) (*empty.Empty, error) {
- logger.Debug("Update_flows_bulk")
+func (rhp *RequestHandlerProxy) Update_flows_bulk(ctx context.Context, args []*ic.Argument) (*empty.Empty, error) {
+ logger.Debug(ctx, "Update_flows_bulk")
if len(args) < 5 {
- logger.Warn("Update_flows_bulk-invalid-number-of-args", log.Fields{"args": args})
+ logger.Warn(ctx, "Update_flows_bulk-invalid-number-of-args", log.Fields{"args": args})
err := errors.New("invalid-number-of-args")
return nil, err
}
@@ -320,43 +321,43 @@
switch arg.Key {
case "device":
if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
- logger.Warnw("cannot-unmarshal-device", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-device", log.Fields{"error": err})
return nil, err
}
case "flows":
if err := ptypes.UnmarshalAny(arg.Value, flows); err != nil {
- logger.Warnw("cannot-unmarshal-flows", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-flows", log.Fields{"error": err})
return nil, err
}
case "groups":
if err := ptypes.UnmarshalAny(arg.Value, groups); err != nil {
- logger.Warnw("cannot-unmarshal-groups", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-groups", log.Fields{"error": err})
return nil, err
}
case "flow_metadata":
if err := ptypes.UnmarshalAny(arg.Value, flowMetadata); err != nil {
- logger.Warnw("cannot-unmarshal-metadata", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-metadata", log.Fields{"error": err})
return nil, err
}
case kafka.TransactionKey:
if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
- logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-transaction-ID", log.Fields{"error": err})
return nil, err
}
}
}
- logger.Debugw("Update_flows_bulk", log.Fields{"flows": flows, "groups": groups})
+ logger.Debugw(ctx, "Update_flows_bulk", log.Fields{"flows": flows, "groups": groups})
//Invoke the bulk flow update API of the adapter
- if err := rhp.adapter.Update_flows_bulk(device, flows, groups, flowMetadata); err != nil {
+ if err := rhp.adapter.Update_flows_bulk(ctx, device, flows, groups, flowMetadata); err != nil {
return nil, status.Errorf(codes.NotFound, "%s", err.Error())
}
return new(empty.Empty), nil
}
-func (rhp *RequestHandlerProxy) Update_flows_incrementally(args []*ic.Argument) (*empty.Empty, error) {
- logger.Debug("Update_flows_incrementally")
+func (rhp *RequestHandlerProxy) Update_flows_incrementally(ctx context.Context, args []*ic.Argument) (*empty.Empty, error) {
+ logger.Debug(ctx, "Update_flows_incrementally")
if len(args) < 5 {
- logger.Warn("Update_flows_incrementally-invalid-number-of-args", log.Fields{"args": args})
+ logger.Warn(ctx, "Update_flows_incrementally-invalid-number-of-args", log.Fields{"args": args})
err := errors.New("invalid-number-of-args")
return nil, err
}
@@ -369,43 +370,43 @@
switch arg.Key {
case "device":
if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
- logger.Warnw("cannot-unmarshal-device", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-device", log.Fields{"error": err})
return nil, err
}
case "flow_changes":
if err := ptypes.UnmarshalAny(arg.Value, flows); err != nil {
- logger.Warnw("cannot-unmarshal-flows", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-flows", log.Fields{"error": err})
return nil, err
}
case "group_changes":
if err := ptypes.UnmarshalAny(arg.Value, groups); err != nil {
- logger.Warnw("cannot-unmarshal-groups", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-groups", log.Fields{"error": err})
return nil, err
}
case "flow_metadata":
if err := ptypes.UnmarshalAny(arg.Value, flowMetadata); err != nil {
- logger.Warnw("cannot-unmarshal-metadata", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-metadata", log.Fields{"error": err})
return nil, err
}
case kafka.TransactionKey:
if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
- logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-transaction-ID", log.Fields{"error": err})
return nil, err
}
}
}
- logger.Debugw("Update_flows_incrementally", log.Fields{"flows": flows, "groups": groups})
+ logger.Debugw(ctx, "Update_flows_incrementally", log.Fields{"flows": flows, "groups": groups})
//Invoke the incremental flow update API of the adapter
- if err := rhp.adapter.Update_flows_incrementally(device, flows, groups, flowMetadata); err != nil {
+ if err := rhp.adapter.Update_flows_incrementally(ctx, device, flows, groups, flowMetadata); err != nil {
return nil, status.Errorf(codes.NotFound, "%s", err.Error())
}
return new(empty.Empty), nil
}
-func (rhp *RequestHandlerProxy) Update_pm_config(args []*ic.Argument) (*empty.Empty, error) {
- logger.Debug("Update_pm_config")
+func (rhp *RequestHandlerProxy) Update_pm_config(ctx context.Context, args []*ic.Argument) (*empty.Empty, error) {
+ logger.Debug(ctx, "Update_pm_config")
if len(args) < 2 {
- logger.Warn("Update_pm_config-invalid-number-of-args", log.Fields{"args": args})
+ logger.Warn(ctx, "Update_pm_config-invalid-number-of-args", log.Fields{"args": args})
err := errors.New("invalid-number-of-args")
return nil, err
}
@@ -416,33 +417,33 @@
switch arg.Key {
case "device":
if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
- logger.Warnw("cannot-unmarshal-device", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-device", log.Fields{"error": err})
return nil, err
}
case "pm_configs":
if err := ptypes.UnmarshalAny(arg.Value, pmConfigs); err != nil {
- logger.Warnw("cannot-unmarshal-pm-configs", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-pm-configs", log.Fields{"error": err})
return nil, err
}
case kafka.TransactionKey:
if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
- logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-transaction-ID", log.Fields{"error": err})
return nil, err
}
}
}
- logger.Debugw("Update_pm_config", log.Fields{"deviceId": device.Id, "pmConfigs": pmConfigs})
+ logger.Debugw(ctx, "Update_pm_config", log.Fields{"deviceId": device.Id, "pmConfigs": pmConfigs})
//Invoke the pm config update API of the adapter
- if err := rhp.adapter.Update_pm_config(device, pmConfigs); err != nil {
+ if err := rhp.adapter.Update_pm_config(ctx, device, pmConfigs); err != nil {
return nil, status.Errorf(codes.NotFound, "%s", err.Error())
}
return new(empty.Empty), nil
}
-func (rhp *RequestHandlerProxy) Receive_packet_out(args []*ic.Argument) (*empty.Empty, error) {
- logger.Debugw("Receive_packet_out", log.Fields{"args": args})
+func (rhp *RequestHandlerProxy) Receive_packet_out(ctx context.Context, args []*ic.Argument) (*empty.Empty, error) {
+ logger.Debugw(ctx, "Receive_packet_out", log.Fields{"args": args})
if len(args) < 3 {
- logger.Warn("Receive_packet_out-invalid-number-of-args", log.Fields{"args": args})
+ logger.Warn(ctx, "Receive_packet_out-invalid-number-of-args", log.Fields{"args": args})
err := errors.New("invalid-number-of-args")
return nil, err
}
@@ -454,29 +455,29 @@
switch arg.Key {
case "deviceId":
if err := ptypes.UnmarshalAny(arg.Value, deviceId); err != nil {
- logger.Warnw("cannot-unmarshal-deviceId", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-deviceId", log.Fields{"error": err})
return nil, err
}
case "outPort":
if err := ptypes.UnmarshalAny(arg.Value, egressPort); err != nil {
- logger.Warnw("cannot-unmarshal-egressPort", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-egressPort", log.Fields{"error": err})
return nil, err
}
case "packet":
if err := ptypes.UnmarshalAny(arg.Value, packet); err != nil {
- logger.Warnw("cannot-unmarshal-packet", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-packet", log.Fields{"error": err})
return nil, err
}
case kafka.TransactionKey:
if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
- logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-transaction-ID", log.Fields{"error": err})
return nil, err
}
}
}
- logger.Debugw("Receive_packet_out", log.Fields{"deviceId": deviceId.Val, "outPort": egressPort, "packet": packet})
+ logger.Debugw(ctx, "Receive_packet_out", log.Fields{"deviceId": deviceId.Val, "outPort": egressPort, "packet": packet})
//Invoke the adopt device on the adapter
- if err := rhp.adapter.Receive_packet_out(deviceId.Val, int(egressPort.Val), packet); err != nil {
+ if err := rhp.adapter.Receive_packet_out(ctx, deviceId.Val, int(egressPort.Val), packet); err != nil {
return nil, status.Errorf(codes.NotFound, "%s", err.Error())
}
return new(empty.Empty), nil
@@ -490,9 +491,9 @@
return new(empty.Empty), nil
}
-func (rhp *RequestHandlerProxy) Get_ofp_device_info(args []*ic.Argument) (*ic.SwitchCapability, error) {
+func (rhp *RequestHandlerProxy) Get_ofp_device_info(ctx context.Context, args []*ic.Argument) (*ic.SwitchCapability, error) {
if len(args) < 2 {
- logger.Warn("invalid-number-of-args", log.Fields{"args": args})
+ logger.Warn(ctx, "invalid-number-of-args", log.Fields{"args": args})
err := errors.New("invalid-number-of-args")
return nil, err
}
@@ -502,31 +503,31 @@
switch arg.Key {
case "device":
if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
- logger.Warnw("cannot-unmarshal-device", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-device", log.Fields{"error": err})
return nil, err
}
case kafka.TransactionKey:
if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
- logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-transaction-ID", log.Fields{"error": err})
return nil, err
}
}
}
- logger.Debugw("Get_ofp_device_info", log.Fields{"deviceId": device.Id})
+ logger.Debugw(ctx, "Get_ofp_device_info", log.Fields{"deviceId": device.Id})
var cap *ic.SwitchCapability
var err error
- if cap, err = rhp.adapter.Get_ofp_device_info(device); err != nil {
+ if cap, err = rhp.adapter.Get_ofp_device_info(ctx, device); err != nil {
return nil, status.Errorf(codes.NotFound, "%s", err.Error())
}
- logger.Debugw("Get_ofp_device_info", log.Fields{"cap": cap})
+ logger.Debugw(ctx, "Get_ofp_device_info", log.Fields{"cap": cap})
return cap, nil
}
-func (rhp *RequestHandlerProxy) Process_inter_adapter_message(args []*ic.Argument) (*empty.Empty, error) {
+func (rhp *RequestHandlerProxy) Process_inter_adapter_message(ctx context.Context, args []*ic.Argument) (*empty.Empty, error) {
if len(args) < 2 {
- logger.Warn("invalid-number-of-args", log.Fields{"args": args})
+ logger.Warn(ctx, "invalid-number-of-args", log.Fields{"args": args})
err := errors.New("invalid-number-of-args")
return nil, err
}
@@ -536,21 +537,21 @@
switch arg.Key {
case "msg":
if err := ptypes.UnmarshalAny(arg.Value, iaMsg); err != nil {
- logger.Warnw("cannot-unmarshal-device", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-device", log.Fields{"error": err})
return nil, err
}
case kafka.TransactionKey:
if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
- logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-transaction-ID", log.Fields{"error": err})
return nil, err
}
}
}
- logger.Debugw("Process_inter_adapter_message", log.Fields{"msgId": iaMsg.Header.Id})
+ logger.Debugw(ctx, "Process_inter_adapter_message", log.Fields{"msgId": iaMsg.Header.Id})
//Invoke the inter adapter API on the handler
- if err := rhp.adapter.Process_inter_adapter_message(iaMsg); err != nil {
+ if err := rhp.adapter.Process_inter_adapter_message(ctx, iaMsg); err != nil {
return nil, status.Errorf(codes.NotFound, "%s", err.Error())
}
@@ -577,30 +578,30 @@
return &voltha.ImageDownload{}, nil
}
-func (rhp *RequestHandlerProxy) Enable_port(args []*ic.Argument) error {
- logger.Debugw("enable_port", log.Fields{"args": args})
- deviceId, port, err := rhp.getEnableDisableParams(args)
+func (rhp *RequestHandlerProxy) Enable_port(ctx context.Context, args []*ic.Argument) error {
+ logger.Debugw(ctx, "enable_port", log.Fields{"args": args})
+ deviceId, port, err := rhp.getEnableDisableParams(ctx, args)
if err != nil {
- logger.Warnw("enable_port", log.Fields{"args": args, "deviceId": deviceId, "port": port})
+ logger.Warnw(ctx, "enable_port", log.Fields{"args": args, "deviceId": deviceId, "port": port})
return err
}
- return rhp.adapter.Enable_port(deviceId, port)
+ return rhp.adapter.Enable_port(ctx, deviceId, port)
}
-func (rhp *RequestHandlerProxy) Disable_port(args []*ic.Argument) error {
- logger.Debugw("disable_port", log.Fields{"args": args})
- deviceId, port, err := rhp.getEnableDisableParams(args)
+func (rhp *RequestHandlerProxy) Disable_port(ctx context.Context, args []*ic.Argument) error {
+ logger.Debugw(ctx, "disable_port", log.Fields{"args": args})
+ deviceId, port, err := rhp.getEnableDisableParams(ctx, args)
if err != nil {
- logger.Warnw("disable_port", log.Fields{"args": args, "deviceId": deviceId, "port": port})
+ logger.Warnw(ctx, "disable_port", log.Fields{"args": args, "deviceId": deviceId, "port": port})
return err
}
- return rhp.adapter.Disable_port(deviceId, port)
+ return rhp.adapter.Disable_port(ctx, deviceId, port)
}
-func (rhp *RequestHandlerProxy) getEnableDisableParams(args []*ic.Argument) (string, *voltha.Port, error) {
- logger.Debugw("getEnableDisableParams", log.Fields{"args": args})
+func (rhp *RequestHandlerProxy) getEnableDisableParams(ctx context.Context, args []*ic.Argument) (string, *voltha.Port, error) {
+ logger.Debugw(ctx, "getEnableDisableParams", log.Fields{"args": args})
if len(args) < 3 {
- logger.Warn("invalid-number-of-args", log.Fields{"args": args})
+ logger.Warn(ctx, "invalid-number-of-args", log.Fields{"args": args})
return "", nil, errors.New("invalid-number-of-args")
}
deviceId := &ic.StrType{}
@@ -609,12 +610,12 @@
switch arg.Key {
case "deviceId":
if err := ptypes.UnmarshalAny(arg.Value, deviceId); err != nil {
- logger.Warnw("cannot-unmarshal-device", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-device", log.Fields{"error": err})
return "", nil, err
}
case "port":
if err := ptypes.UnmarshalAny(arg.Value, port); err != nil {
- logger.Warnw("cannot-unmarshal-port", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-port", log.Fields{"error": err})
return "", nil, err
}
}
@@ -622,9 +623,9 @@
return deviceId.Val, port, nil
}
-func (rhp *RequestHandlerProxy) Child_device_lost(args []*ic.Argument) error {
+func (rhp *RequestHandlerProxy) Child_device_lost(ctx context.Context, args []*ic.Argument) error {
if len(args) < 4 {
- logger.Warn("invalid-number-of-args", log.Fields{"args": args})
+ logger.Warn(ctx, "invalid-number-of-args", log.Fields{"args": args})
return errors.New("invalid-number-of-args")
}
@@ -636,22 +637,22 @@
switch arg.Key {
case "pDeviceId":
if err := ptypes.UnmarshalAny(arg.Value, pDeviceId); err != nil {
- logger.Warnw("cannot-unmarshal-parent-deviceId", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-parent-deviceId", log.Fields{"error": err})
return err
}
case "pPortNo":
if err := ptypes.UnmarshalAny(arg.Value, pPortNo); err != nil {
- logger.Warnw("cannot-unmarshal-port", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-port", log.Fields{"error": err})
return err
}
case "onuID":
if err := ptypes.UnmarshalAny(arg.Value, onuID); err != nil {
- logger.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-transaction-ID", log.Fields{"error": err})
return err
}
case kafka.FromTopic:
if err := ptypes.UnmarshalAny(arg.Value, fromTopic); err != nil {
- logger.Warnw("cannot-unmarshal-from-topic", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-from-topic", log.Fields{"error": err})
return err
}
}
@@ -659,15 +660,15 @@
//Update the core reference for that device
rhp.coreProxy.UpdateCoreReference(pDeviceId.Val, fromTopic.Val)
//Invoke the Child_device_lost API on the adapter
- if err := rhp.adapter.Child_device_lost(pDeviceId.Val, uint32(pPortNo.Val), uint32(onuID.Val)); err != nil {
+ if err := rhp.adapter.Child_device_lost(ctx, pDeviceId.Val, uint32(pPortNo.Val), uint32(onuID.Val)); err != nil {
return status.Errorf(codes.NotFound, "%s", err.Error())
}
return nil
}
-func (rhp *RequestHandlerProxy) Start_omci_test(args []*ic.Argument) (*ic.TestResponse, error) {
+func (rhp *RequestHandlerProxy) Start_omci_test(ctx context.Context, args []*ic.Argument) (*ic.TestResponse, error) {
if len(args) < 2 {
- logger.Warn("invalid-number-of-args", log.Fields{"args": args})
+ logger.Warn(ctx, "invalid-number-of-args", log.Fields{"args": args})
err := errors.New("invalid-number-of-args")
return nil, err
}
@@ -681,26 +682,26 @@
switch arg.Key {
case "device":
if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
- logger.Warnw("cannot-unmarshal-device", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-device", log.Fields{"error": err})
return nil, err
}
case "omcitestrequest":
if err := ptypes.UnmarshalAny(arg.Value, request); err != nil {
- logger.Warnw("cannot-unmarshal-omcitestrequest", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-omcitestrequest", log.Fields{"error": err})
return nil, err
}
}
}
- logger.Debugw("Start_omci_test", log.Fields{"device-id": device.Id, "req": request})
- result, err := rhp.adapter.Start_omci_test(device, request)
+ logger.Debugw(ctx, "Start_omci_test", log.Fields{"device-id": device.Id, "req": request})
+ result, err := rhp.adapter.Start_omci_test(ctx, device, request)
if err != nil {
return nil, status.Errorf(codes.NotFound, "%s", err.Error())
}
return result, nil
}
-func (rhp *RequestHandlerProxy) Get_ext_value(args []*ic.Argument) (*voltha.ReturnValues, error) {
+func (rhp *RequestHandlerProxy) Get_ext_value(ctx context.Context, args []*ic.Argument) (*voltha.ReturnValues, error) {
if len(args) < 3 {
- logger.Warn("invalid-number-of-args", log.Fields{"args": args})
+ logger.Warn(ctx, "invalid-number-of-args", log.Fields{"args": args})
return nil, errors.New("invalid-number-of-args")
}
@@ -711,24 +712,24 @@
switch arg.Key {
case "device":
if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
- logger.Warnw("cannot-unmarshal-device", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-device", log.Fields{"error": err})
return nil, err
}
case "pDeviceId":
if err := ptypes.UnmarshalAny(arg.Value, pDeviceId); err != nil {
- logger.Warnw("cannot-unmarshal-parent-deviceId", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-parent-deviceId", log.Fields{"error": err})
return nil, err
}
case "valuetype":
if err := ptypes.UnmarshalAny(arg.Value, valuetype); err != nil {
- logger.Warnw("cannot-unmarshal-valuetype", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-valuetype", log.Fields{"error": err})
return nil, err
}
default:
- logger.Warnw("key-not-found", log.Fields{"arg.Key": arg.Key})
+ logger.Warnw(ctx, "key-not-found", log.Fields{"arg.Key": arg.Key})
}
}
//Invoke the Get_value API on the adapter
- return rhp.adapter.Get_ext_value(pDeviceId.Val, device, voltha.ValueType_Type(valuetype.Val))
+ return rhp.adapter.Get_ext_value(ctx, pDeviceId.Val, device, voltha.ValueType_Type(valuetype.Val))
}
diff --git a/pkg/adapters/common/utils.go b/pkg/adapters/common/utils.go
index 94e8bd6..3d91119 100644
--- a/pkg/adapters/common/utils.go
+++ b/pkg/adapters/common/utils.go
@@ -16,6 +16,7 @@
package common
import (
+ "context"
"fmt"
"github.com/opencord/voltha-lib-go/v3/pkg/log"
ic "github.com/opencord/voltha-protos/v3/go/inter_container"
@@ -75,7 +76,7 @@
return string(b)
}
-func ICProxyErrorCodeToGrpcErrorCode(icErr ic.ErrorCodeCodes) codes.Code {
+func ICProxyErrorCodeToGrpcErrorCode(ctx context.Context, icErr ic.ErrorCodeCodes) codes.Code {
switch icErr {
case ic.ErrorCode_INVALID_PARAMETERS:
return codes.InvalidArgument
@@ -84,7 +85,7 @@
case ic.ErrorCode_DEADLINE_EXCEEDED:
return codes.DeadlineExceeded
default:
- logger.Warnw("cannnot-map-ic-error-code-to-grpc-error-code", log.Fields{"err": icErr})
+ logger.Warnw(ctx, "cannnot-map-ic-error-code-to-grpc-error-code", log.Fields{"err": icErr})
return codes.Internal
}
}
diff --git a/pkg/adapters/common/utils_test.go b/pkg/adapters/common/utils_test.go
index 87594fd..a93dd2f 100644
--- a/pkg/adapters/common/utils_test.go
+++ b/pkg/adapters/common/utils_test.go
@@ -16,6 +16,7 @@
package common
import (
+ "context"
ic "github.com/opencord/voltha-protos/v3/go/inter_container"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/codes"
@@ -72,12 +73,12 @@
}
func TestICProxyErrorCodeToGrpcErrorCode(t *testing.T) {
- unsupported := ICProxyErrorCodeToGrpcErrorCode(ic.ErrorCode_UNSUPPORTED_REQUEST)
+ unsupported := ICProxyErrorCodeToGrpcErrorCode(context.Background(), ic.ErrorCode_UNSUPPORTED_REQUEST)
assert.Equal(t, unsupported, codes.Unavailable)
- invalid := ICProxyErrorCodeToGrpcErrorCode(ic.ErrorCode_INVALID_PARAMETERS)
+ invalid := ICProxyErrorCodeToGrpcErrorCode(context.Background(), ic.ErrorCode_INVALID_PARAMETERS)
assert.Equal(t, invalid, codes.InvalidArgument)
- timeout := ICProxyErrorCodeToGrpcErrorCode(ic.ErrorCode_DEADLINE_EXCEEDED)
+ timeout := ICProxyErrorCodeToGrpcErrorCode(context.Background(), ic.ErrorCode_DEADLINE_EXCEEDED)
assert.Equal(t, timeout, codes.DeadlineExceeded)
}
diff --git a/pkg/adapters/iAdapter.go b/pkg/adapters/iAdapter.go
index 1e81890..ce0b791 100644
--- a/pkg/adapters/iAdapter.go
+++ b/pkg/adapters/iAdapter.go
@@ -16,6 +16,7 @@
package adapters
import (
+ "context"
ic "github.com/opencord/voltha-protos/v3/go/inter_container"
"github.com/opencord/voltha-protos/v3/go/openflow_13"
"github.com/opencord/voltha-protos/v3/go/voltha"
@@ -23,34 +24,34 @@
//IAdapter represents the set of APIs a voltha adapter has to support.
type IAdapter interface {
- Adapter_descriptor() error
- Device_types() (*voltha.DeviceTypes, error)
- Health() (*voltha.HealthStatus, error)
- Adopt_device(device *voltha.Device) error
- Reconcile_device(device *voltha.Device) error
- Abandon_device(device *voltha.Device) error
- Disable_device(device *voltha.Device) error
- Reenable_device(device *voltha.Device) error
- Reboot_device(device *voltha.Device) error
- Self_test_device(device *voltha.Device) error
- Delete_device(device *voltha.Device) error
- Get_device_details(device *voltha.Device) error
- Update_flows_bulk(device *voltha.Device, flows *voltha.Flows, groups *voltha.FlowGroups, flowMetadata *voltha.FlowMetadata) error
- Update_flows_incrementally(device *voltha.Device, flows *openflow_13.FlowChanges, groups *openflow_13.FlowGroupChanges, flowMetadata *voltha.FlowMetadata) error
- Update_pm_config(device *voltha.Device, pm_configs *voltha.PmConfigs) error
- Receive_packet_out(deviceId string, egress_port_no int, msg *openflow_13.OfpPacketOut) error
- Suppress_event(filter *voltha.EventFilter) error
- Unsuppress_event(filter *voltha.EventFilter) error
- Get_ofp_device_info(device *voltha.Device) (*ic.SwitchCapability, error)
- Process_inter_adapter_message(msg *ic.InterAdapterMessage) error
- Download_image(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error)
- Get_image_download_status(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error)
- Cancel_image_download(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error)
- Activate_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error)
- Revert_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error)
- Enable_port(deviceId string, port *voltha.Port) error
- Disable_port(deviceId string, port *voltha.Port) error
- Child_device_lost(parentDeviceId string, parentPortNo uint32, onuID uint32) error
- Start_omci_test(device *voltha.Device, request *voltha.OmciTestRequest) (*voltha.TestResponse, error)
- Get_ext_value(deviceId string, device *voltha.Device, valueflag voltha.ValueType_Type) (*voltha.ReturnValues, error)
+ Adapter_descriptor(ctx context.Context) error
+ Device_types(ctx context.Context) (*voltha.DeviceTypes, error)
+ Health(ctx context.Context) (*voltha.HealthStatus, error)
+ Adopt_device(ctx context.Context, device *voltha.Device) error
+ Reconcile_device(ctx context.Context, device *voltha.Device) error
+ Abandon_device(ctx context.Context, device *voltha.Device) error
+ Disable_device(ctx context.Context, device *voltha.Device) error
+ Reenable_device(ctx context.Context, device *voltha.Device) error
+ Reboot_device(ctx context.Context, device *voltha.Device) error
+ Self_test_device(ctx context.Context, device *voltha.Device) error
+ Delete_device(ctx context.Context, device *voltha.Device) error
+ Get_device_details(ctx context.Context, device *voltha.Device) error
+ Update_flows_bulk(ctx context.Context, device *voltha.Device, flows *voltha.Flows, groups *voltha.FlowGroups, flowMetadata *voltha.FlowMetadata) error
+ Update_flows_incrementally(ctx context.Context, device *voltha.Device, flows *openflow_13.FlowChanges, groups *openflow_13.FlowGroupChanges, flowMetadata *voltha.FlowMetadata) error
+ Update_pm_config(ctx context.Context, device *voltha.Device, pm_configs *voltha.PmConfigs) error
+ Receive_packet_out(ctx context.Context, deviceId string, egress_port_no int, msg *openflow_13.OfpPacketOut) error
+ Suppress_event(ctx context.Context, filter *voltha.EventFilter) error
+ Unsuppress_event(ctx context.Context, filter *voltha.EventFilter) error
+ Get_ofp_device_info(ctx context.Context, device *voltha.Device) (*ic.SwitchCapability, error)
+ Process_inter_adapter_message(ctx context.Context, msg *ic.InterAdapterMessage) error
+ Download_image(ctx context.Context, device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error)
+ Get_image_download_status(ctx context.Context, device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error)
+ Cancel_image_download(ctx context.Context, device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error)
+ Activate_image_update(ctx context.Context, device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error)
+ Revert_image_update(ctx context.Context, device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error)
+ Enable_port(ctx context.Context, deviceId string, port *voltha.Port) error
+ Disable_port(ctx context.Context, deviceId string, port *voltha.Port) error
+ Child_device_lost(ctx context.Context, parentDeviceId string, parentPortNo uint32, onuID uint32) error
+ Start_omci_test(ctx context.Context, device *voltha.Device, request *voltha.OmciTestRequest) (*voltha.TestResponse, error)
+ Get_ext_value(ctx context.Context, deviceId string, device *voltha.Device, valueflag voltha.ValueType_Type) (*voltha.ReturnValues, error)
}
diff --git a/pkg/config/common.go b/pkg/config/common.go
index 37e05fd..06b8b3c 100644
--- a/pkg/config/common.go
+++ b/pkg/config/common.go
@@ -19,12 +19,12 @@
"github.com/opencord/voltha-lib-go/v3/pkg/log"
)
-var logger log.Logger
+var logger log.CLogger
func init() {
// Setup this package so that it's log level can be modified at run time
var err error
- logger, err = log.AddPackage(log.JSON, log.ErrorLevel, log.Fields{"pkg": "config"})
+ logger, err = log.RegisterPackage(log.JSON, log.ErrorLevel, log.Fields{"pkg": "config"})
if err != nil {
panic(err)
}
diff --git a/pkg/config/configmanager.go b/pkg/config/configmanager.go
index 24988be..11aa8e6 100644
--- a/pkg/config/configmanager.go
+++ b/pkg/config/configmanager.go
@@ -96,14 +96,14 @@
kvStoreEventChan chan *kvstore.Event
}
-func NewConfigManager(kvClient kvstore.Client, kvStoreType, kvStoreAddress string, kvStoreTimeout time.Duration) *ConfigManager {
+func NewConfigManager(ctx context.Context, kvClient kvstore.Client, kvStoreType, kvStoreAddress string, kvStoreTimeout time.Duration) *ConfigManager {
var kvStorePrefix string
if prefix, present := os.LookupEnv("KV_STORE_DATAPATH_PREFIX"); present {
kvStorePrefix = prefix
- logger.Infow("KV_STORE_DATAPATH_PREFIX env variable is set, ", log.Fields{"kvStoreDataPathPrefix": kvStorePrefix})
+ logger.Infow(ctx, "KV_STORE_DATAPATH_PREFIX env variable is set, ", log.Fields{"kvStoreDataPathPrefix": kvStorePrefix})
} else {
kvStorePrefix = defaultkvStoreDataPathPrefix
- logger.Infow("KV_STORE_DATAPATH_PREFIX env variable is not set, using default", log.Fields{"kvStoreDataPathPrefix": defaultkvStoreDataPathPrefix})
+ logger.Infow(ctx, "KV_STORE_DATAPATH_PREFIX env variable is not set, using default", log.Fields{"kvStoreDataPathPrefix": defaultkvStoreDataPathPrefix})
}
return &ConfigManager{
KVStoreConfigPrefix: defaultkvStoreConfigPath,
@@ -176,31 +176,31 @@
func (c *ComponentConfig) MonitorForConfigChange(ctx context.Context) chan *ConfigChangeEvent {
key := c.makeConfigPath()
- logger.Debugw("monitoring-for-config-change", log.Fields{"key": key})
+ logger.Debugw(ctx, "monitoring-for-config-change", log.Fields{"key": key})
c.changeEventChan = make(chan *ConfigChangeEvent, 1)
c.kvStoreEventChan = c.cManager.Backend.CreateWatch(ctx, key, true)
- go c.processKVStoreWatchEvents()
+ go c.processKVStoreWatchEvents(ctx)
return c.changeEventChan
}
// processKVStoreWatchEvents process event channel recieved from the Backend for any ChangeType
// It checks for the EventType is valid or not.For the valid EventTypes creates ConfigChangeEvent and send it on channel
-func (c *ComponentConfig) processKVStoreWatchEvents() {
+func (c *ComponentConfig) processKVStoreWatchEvents(ctx context.Context) {
ccKeyPrefix := c.makeConfigPath()
- logger.Debugw("processing-kvstore-event-change", log.Fields{"key-prefix": ccKeyPrefix})
+ logger.Debugw(ctx, "processing-kvstore-event-change", log.Fields{"key-prefix": ccKeyPrefix})
ccPathPrefix := c.cManager.Backend.PathPrefix + ccKeyPrefix + kvStorePathSeparator
for watchResp := range c.kvStoreEventChan {
if watchResp.EventType == kvstore.CONNECTIONDOWN || watchResp.EventType == kvstore.UNKNOWN {
- logger.Warnw("received-invalid-change-type-in-watch-channel-from-kvstore", log.Fields{"change-type": watchResp.EventType})
+ logger.Warnw(ctx, "received-invalid-change-type-in-watch-channel-from-kvstore", log.Fields{"change-type": watchResp.EventType})
continue
}
@@ -220,7 +220,7 @@
func (c *ComponentConfig) Retrieve(ctx context.Context, configKey string) (string, error) {
key := c.makeConfigPath() + "/" + configKey
- logger.Debugw("retrieving-config", log.Fields{"key": key})
+ logger.Debugw(ctx, "retrieving-config", log.Fields{"key": key})
if kvpair, err := c.cManager.Backend.Get(ctx, key); err != nil {
return "", err
@@ -230,7 +230,7 @@
}
value := strings.Trim(fmt.Sprintf("%s", kvpair.Value), "\"")
- logger.Debugw("retrieved-config", log.Fields{"key": key, "value": value})
+ logger.Debugw(ctx, "retrieved-config", log.Fields{"key": key, "value": value})
return value, nil
}
}
@@ -238,7 +238,7 @@
func (c *ComponentConfig) RetrieveAll(ctx context.Context) (map[string]string, error) {
key := c.makeConfigPath()
- logger.Debugw("retreiving-list", log.Fields{"key": key})
+ logger.Debugw(ctx, "retreiving-list", log.Fields{"key": key})
data, err := c.cManager.Backend.List(ctx, key)
if err != nil {
@@ -261,7 +261,7 @@
func (c *ComponentConfig) Save(ctx context.Context, configKey string, configValue string) error {
key := c.makeConfigPath() + "/" + configKey
- logger.Debugw("saving-config", log.Fields{"key": key, "value": configValue})
+ logger.Debugw(ctx, "saving-config", log.Fields{"key": key, "value": configValue})
//save the data for update config
if err := c.cManager.Backend.Put(ctx, key, configValue); err != nil {
@@ -274,7 +274,7 @@
//construct key using makeConfigPath
key := c.makeConfigPath() + "/" + configKey
- logger.Debugw("deleting-config", log.Fields{"key": key})
+ logger.Debugw(ctx, "deleting-config", log.Fields{"key": key})
//delete the config
if err := c.cManager.Backend.Delete(ctx, key); err != nil {
return err
diff --git a/pkg/config/logcontroller.go b/pkg/config/logcontroller.go
index b00569f..f83e383 100644
--- a/pkg/config/logcontroller.go
+++ b/pkg/config/logcontroller.go
@@ -51,9 +51,8 @@
initialLogLevel string // Initial default log level set by helm chart
}
-func NewComponentLogController(cm *ConfigManager) (*ComponentLogController, error) {
-
- logger.Debug("creating-new-component-log-controller")
+func NewComponentLogController(ctx context.Context, cm *ConfigManager) (*ComponentLogController, error) {
+ logger.Debug(ctx, "creating-new-component-log-controller")
componentName := os.Getenv("COMPONENT_NAME")
if componentName == "" {
return nil, errors.New("Unable to retrieve PoD Component Name from Runtime env")
@@ -80,17 +79,17 @@
// Then, it persists initial default Loglevels into Config Store before
// starting the loading and processing of all Log Configuration
func StartLogLevelConfigProcessing(cm *ConfigManager, ctx context.Context) {
- cc, err := NewComponentLogController(cm)
+ cc, err := NewComponentLogController(ctx, cm)
if err != nil {
- logger.Errorw("unable-to-construct-component-log-controller-instance-for-log-config-monitoring", log.Fields{"error": err})
+ logger.Errorw(ctx, "unable-to-construct-component-log-controller-instance-for-log-config-monitoring", log.Fields{"error": err})
return
}
cc.GlobalConfig = cm.InitComponentConfig(globalConfigRootNode, ConfigTypeLogLevel)
- logger.Debugw("global-log-config", log.Fields{"cc-global-config": cc.GlobalConfig})
+ logger.Debugw(ctx, "global-log-config", log.Fields{"cc-global-config": cc.GlobalConfig})
cc.componentNameConfig = cm.InitComponentConfig(cc.ComponentName, ConfigTypeLogLevel)
- logger.Debugw("component-log-config", log.Fields{"cc-component-name-config": cc.componentNameConfig})
+ logger.Debugw(ctx, "component-log-config", log.Fields{"cc-component-name-config": cc.componentNameConfig})
cc.persistInitialDefaultLogConfigs(ctx)
@@ -105,21 +104,21 @@
_, err := c.GlobalConfig.Retrieve(ctx, defaultLogLevelKey)
if err != nil {
- logger.Debugw("failed-to-retrieve-global-default-log-config-at-startup", log.Fields{"error": err})
+ logger.Debugw(ctx, "failed-to-retrieve-global-default-log-config-at-startup", log.Fields{"error": err})
err = c.GlobalConfig.Save(ctx, defaultLogLevelKey, initialGlobalDefaultLogLevelValue)
if err != nil {
- logger.Errorw("failed-to-persist-global-default-log-config-at-startup", log.Fields{"error": err, "loglevel": initialGlobalDefaultLogLevelValue})
+ logger.Errorw(ctx, "failed-to-persist-global-default-log-config-at-startup", log.Fields{"error": err, "loglevel": initialGlobalDefaultLogLevelValue})
}
}
_, err = c.componentNameConfig.Retrieve(ctx, defaultLogLevelKey)
if err != nil {
- logger.Debugw("failed-to-retrieve-component-default-log-config-at-startup", log.Fields{"error": err})
+ logger.Debugw(ctx, "failed-to-retrieve-component-default-log-config-at-startup", log.Fields{"error": err})
err = c.componentNameConfig.Save(ctx, defaultLogLevelKey, c.initialLogLevel)
if err != nil {
- logger.Errorw("failed-to-persist-component-default-log-config-at-startup", log.Fields{"error": err, "loglevel": c.initialLogLevel})
+ logger.Errorw(ctx, "failed-to-persist-component-default-log-config-at-startup", log.Fields{"error": err, "loglevel": c.initialLogLevel})
}
}
}
@@ -129,7 +128,7 @@
func (c *ComponentLogController) persistRegisteredLogPackageList(ctx context.Context) {
componentMetadataConfig := c.configManager.InitComponentConfig(c.ComponentName, ConfigTypeMetadata)
- logger.Debugw("component-metadata-config", log.Fields{"component-metadata-config": componentMetadataConfig})
+ logger.Debugw(ctx, "component-metadata-config", log.Fields{"component-metadata-config": componentMetadataConfig})
packageList := log.GetPackageNames()
packageList = append(packageList, defaultLogLevelKey)
@@ -137,12 +136,12 @@
packageNames, err := json.Marshal(packageList)
if err != nil {
- logger.Errorw("failed-to-marshal-log-package-list-for-storage", log.Fields{"error": err, "packageList": packageList})
+ logger.Errorw(ctx, "failed-to-marshal-log-package-list-for-storage", log.Fields{"error": err, "packageList": packageList})
return
}
if err := componentMetadataConfig.Save(ctx, logPackagesListKey, string(packageNames)); err != nil {
- logger.Errorw("failed-to-persist-component-registered-log-package-list-at-startup", log.Fields{"error": err, "packageNames": packageNames})
+ logger.Errorw(ctx, "failed-to-persist-component-registered-log-package-list-at-startup", log.Fields{"error": err, "packageNames": packageNames})
}
}
@@ -155,10 +154,10 @@
// Load and apply Log Config for first time
initialLogConfig, err := c.buildUpdatedLogConfig(ctx)
if err != nil {
- logger.Warnw("unable-to-load-log-config-at-startup", log.Fields{"error": err})
+ logger.Warnw(ctx, "unable-to-load-log-config-at-startup", log.Fields{"error": err})
} else {
- if err := c.loadAndApplyLogConfig(initialLogConfig); err != nil {
- logger.Warnw("unable-to-apply-log-config-at-startup", log.Fields{"error": err})
+ if err := c.loadAndApplyLogConfig(ctx, initialLogConfig); err != nil {
+ logger.Warnw(ctx, "unable-to-apply-log-config-at-startup", log.Fields{"error": err})
}
}
@@ -174,25 +173,25 @@
case configEvent = <-componentConfigEventChan:
}
- logger.Debugw("processing-log-config-change", log.Fields{"ChangeType": configEvent.ChangeType, "Package": configEvent.ConfigAttribute})
+ logger.Debugw(ctx, "processing-log-config-change", log.Fields{"ChangeType": configEvent.ChangeType, "Package": configEvent.ConfigAttribute})
updatedLogConfig, err := c.buildUpdatedLogConfig(ctx)
if err != nil {
- logger.Warnw("unable-to-fetch-updated-log-config", log.Fields{"error": err})
+ logger.Warnw(ctx, "unable-to-fetch-updated-log-config", log.Fields{"error": err})
continue
}
- logger.Debugw("applying-updated-log-config", log.Fields{"updated-log-config": updatedLogConfig})
+ logger.Debugw(ctx, "applying-updated-log-config", log.Fields{"updated-log-config": updatedLogConfig})
- if err := c.loadAndApplyLogConfig(updatedLogConfig); err != nil {
- logger.Warnw("unable-to-load-and-apply-log-config", log.Fields{"error": err})
+ if err := c.loadAndApplyLogConfig(ctx, updatedLogConfig); err != nil {
+ logger.Warnw(ctx, "unable-to-load-and-apply-log-config", log.Fields{"error": err})
}
}
}
// get active loglevel from the zap logger
-func getActiveLogLevels() map[string]string {
+func getActiveLogLevels(ctx context.Context) map[string]string {
loglevels := make(map[string]string)
// now do the default log level
@@ -204,7 +203,7 @@
for _, packageName := range log.GetPackageNames() {
level, err := log.GetPackageLogLevel(packageName)
if err != nil {
- logger.Warnw("unable-to-fetch-current-active-loglevel-for-package-name", log.Fields{"package-name": packageName, "error": err})
+ logger.Warnw(ctx, "unable-to-fetch-current-active-loglevel-for-package-name", log.Fields{"package-name": packageName, "error": err})
continue
}
@@ -213,7 +212,7 @@
}
}
- logger.Debugw("retreived-log-levels-from-zap-logger", log.Fields{"loglevels": loglevels})
+ logger.Debugw(ctx, "retreived-log-levels-from-zap-logger", log.Fields{"loglevels": loglevels})
return loglevels
}
@@ -228,16 +227,16 @@
// Handle edge cases when global default loglevel is deleted directly from etcd or set to a invalid value
// We should use hard-coded initial default value in such cases
if globalDefaultLogLevel == "" {
- logger.Warn("global-default-loglevel-not-found-in-config-store")
+ logger.Warn(ctx, "global-default-loglevel-not-found-in-config-store")
globalDefaultLogLevel = initialGlobalDefaultLogLevelValue
}
if _, err := log.StringToLogLevel(globalDefaultLogLevel); err != nil {
- logger.Warnw("unsupported-loglevel-config-defined-at-global-default", log.Fields{"log-level": globalDefaultLogLevel})
+ logger.Warnw(ctx, "unsupported-loglevel-config-defined-at-global-default", log.Fields{"log-level": globalDefaultLogLevel})
globalDefaultLogLevel = initialGlobalDefaultLogLevelValue
}
- logger.Debugw("retrieved-global-default-loglevel", log.Fields{"level": globalDefaultLogLevel})
+ logger.Debugw(ctx, "retrieved-global-default-loglevel", log.Fields{"level": globalDefaultLogLevel})
return globalDefaultLogLevel, nil
}
@@ -251,7 +250,7 @@
effectiveDefaultLogLevel := ""
for logConfigKey, logConfigValue := range componentLogConfig {
if _, err := log.StringToLogLevel(logConfigValue); err != nil || logConfigKey == "" {
- logger.Warnw("unsupported-loglevel-config-defined-at-component-context", log.Fields{"package-name": logConfigKey, "log-level": logConfigValue})
+ logger.Warnw(ctx, "unsupported-loglevel-config-defined-at-component-context", log.Fields{"package-name": logConfigKey, "log-level": logConfigValue})
delete(componentLogConfig, logConfigKey)
} else {
if logConfigKey == defaultLogLevelKey {
@@ -268,7 +267,7 @@
componentLogConfig[defaultLogLevelKey] = effectiveDefaultLogLevel
- logger.Debugw("retrieved-component-log-config", log.Fields{"component-log-level": componentLogConfig})
+ logger.Debugw(ctx, "retrieved-component-log-config", log.Fields{"component-log-level": componentLogConfig})
return componentLogConfig, nil
}
@@ -282,7 +281,7 @@
func (c *ComponentLogController) buildUpdatedLogConfig(ctx context.Context) (map[string]string, error) {
globalLogLevel, err := c.getGlobalLogConfig(ctx)
if err != nil {
- logger.Errorw("unable-to-retrieve-global-log-config", log.Fields{"err": err})
+ logger.Errorw(ctx, "unable-to-retrieve-global-log-config", log.Fields{"err": err})
}
componentLogConfig, err := c.getComponentLogConfig(ctx, globalLogLevel)
@@ -302,17 +301,17 @@
// create hash of loaded configuration using GenerateLogConfigHash
// if there is previous hash stored, compare the hash to stored hash
// if there is any change will call UpdateLogLevels
-func (c *ComponentLogController) loadAndApplyLogConfig(logConfig map[string]string) error {
+func (c *ComponentLogController) loadAndApplyLogConfig(ctx context.Context, logConfig map[string]string) error {
currentLogHash, err := GenerateLogConfigHash(logConfig)
if err != nil {
return err
}
if c.logHash != currentLogHash {
- UpdateLogLevels(logConfig)
+ updateLogLevels(ctx, logConfig)
c.logHash = currentLogHash
} else {
- logger.Debug("effective-loglevel-config-same-as-currently-active")
+ logger.Debug(ctx, "effective-loglevel-config-same-as-currently-active")
}
return nil
@@ -322,7 +321,7 @@
// to identify and create map of modified Log Levels of 2 types:
// - Packages for which log level has been changed
// - Packages for which log level config has been cleared - set to default log level
-func createModifiedLogLevels(activeLogLevels, updatedLogLevels map[string]string) map[string]string {
+func createModifiedLogLevels(ctx context.Context, activeLogLevels, updatedLogLevels map[string]string) map[string]string {
defaultLevel := updatedLogLevels[defaultLogLevelKey]
modifiedLogLevels := make(map[string]string)
@@ -339,7 +338,7 @@
// Log warnings for all invalid packages for which log config has been set
for key, value := range updatedLogLevels {
if _, exist := activeLogLevels[key]; !exist {
- logger.Warnw("ignoring-loglevel-set-for-invalid-package", log.Fields{"package": key, "log-level": value})
+ logger.Warnw(ctx, "ignoring-loglevel-set-for-invalid-package", log.Fields{"package": key, "log-level": value})
}
}
@@ -349,18 +348,18 @@
// updateLogLevels update the loglevels for the component
// retrieve active confguration from logger
// compare with entries one by one and apply
-func UpdateLogLevels(updatedLogConfig map[string]string) {
+func updateLogLevels(ctx context.Context, updatedLogConfig map[string]string) {
- activeLogLevels := getActiveLogLevels()
- changedLogLevels := createModifiedLogLevels(activeLogLevels, updatedLogConfig)
+ activeLogLevels := getActiveLogLevels(ctx)
+ changedLogLevels := createModifiedLogLevels(ctx, activeLogLevels, updatedLogConfig)
// If no changed log levels are found, just return. It may happen on configuration of a invalid package
if len(changedLogLevels) == 0 {
- logger.Debug("no-change-in-effective-loglevel-config")
+ logger.Debug(ctx, "no-change-in-effective-loglevel-config")
return
}
- logger.Debugw("applying-log-level-for-modified-packages", log.Fields{"changed-log-levels": changedLogLevels})
+ logger.Debugw(ctx, "applying-log-level-for-modified-packages", log.Fields{"changed-log-levels": changedLogLevels})
for key, level := range changedLogLevels {
if key == defaultLogLevelKey {
if l, err := log.StringToLogLevel(level); err == nil {
diff --git a/pkg/db/backend.go b/pkg/db/backend.go
index 1e23a0f..f595dc1 100644
--- a/pkg/db/backend.go
+++ b/pkg/db/backend.go
@@ -47,7 +47,7 @@
}
// NewBackend creates a new instance of a Backend structure
-func NewBackend(storeType string, address string, timeout time.Duration, pathPrefix string) *Backend {
+func NewBackend(ctx context.Context, storeType string, address string, timeout time.Duration, pathPrefix string) *Backend {
var err error
b := &Backend{
@@ -59,8 +59,8 @@
alive: false, // connection considered down at start
}
- if b.Client, err = b.newClient(address, timeout); err != nil {
- logger.Errorw("failed-to-create-kv-client",
+ if b.Client, err = b.newClient(ctx, address, timeout); err != nil {
+ logger.Errorw(ctx, "failed-to-create-kv-client",
log.Fields{
"type": storeType, "address": address,
"timeout": timeout, "prefix": pathPrefix,
@@ -71,22 +71,22 @@
return b
}
-func (b *Backend) newClient(address string, timeout time.Duration) (kvstore.Client, error) {
+func (b *Backend) newClient(ctx context.Context, address string, timeout time.Duration) (kvstore.Client, error) {
switch b.StoreType {
case "consul":
- return kvstore.NewConsulClient(address, timeout)
+ return kvstore.NewConsulClient(ctx, address, timeout)
case "etcd":
- return kvstore.NewEtcdClient(address, timeout, log.WarnLevel)
+ return kvstore.NewEtcdClient(ctx, address, timeout, log.WarnLevel)
}
return nil, errors.New("unsupported-kv-store")
}
-func (b *Backend) makePath(key string) string {
+func (b *Backend) makePath(ctx context.Context, key string) string {
path := fmt.Sprintf("%s/%s", b.PathPrefix, key)
return path
}
-func (b *Backend) updateLiveness(alive bool) {
+func (b *Backend) updateLiveness(ctx context.Context, alive bool) {
// Periodically push stream of liveness data to the channel,
// so that in a live state, the core does not timeout and
// send a forced liveness message. Push alive state if the
@@ -94,11 +94,11 @@
if b.liveness != nil {
if b.alive != alive {
- logger.Debug("update-liveness-channel-reason-change")
+ logger.Debug(ctx, "update-liveness-channel-reason-change")
b.liveness <- alive
b.lastLivenessTime = time.Now()
} else if time.Since(b.lastLivenessTime) > b.LivenessChannelInterval {
- logger.Debug("update-liveness-channel-reason-interval")
+ logger.Debug(ctx, "update-liveness-channel-reason-interval")
b.liveness <- alive
b.lastLivenessTime = time.Now()
}
@@ -106,7 +106,7 @@
// Emit log message only for alive state change
if b.alive != alive {
- logger.Debugw("change-kvstore-alive-status", log.Fields{"alive": alive})
+ logger.Debugw(ctx, "change-kvstore-alive-status", log.Fields{"alive": alive})
b.alive = alive
}
}
@@ -115,9 +115,9 @@
// post on Liveness channel
func (b *Backend) PerformLivenessCheck(ctx context.Context) bool {
alive := b.Client.IsConnectionUp(ctx)
- logger.Debugw("kvstore-liveness-check-result", log.Fields{"alive": alive})
+ logger.Debugw(ctx, "kvstore-liveness-check-result", log.Fields{"alive": alive})
- b.updateLiveness(alive)
+ b.updateLiveness(ctx, alive)
return alive
}
@@ -126,11 +126,11 @@
// or not the connection is still Live. This channel is then picked up
// by the service (i.e. rw_core / ro_core) to update readiness status
// and/or take other actions.
-func (b *Backend) EnableLivenessChannel() chan bool {
- logger.Debug("enable-kvstore-liveness-channel")
+func (b *Backend) EnableLivenessChannel(ctx context.Context) chan bool {
+ logger.Debug(ctx, "enable-kvstore-liveness-channel")
if b.liveness == nil {
- logger.Debug("create-kvstore-liveness-channel")
+ logger.Debug(ctx, "create-kvstore-liveness-channel")
// Channel size of 10 to avoid any possibility of blocking in Load conditions
b.liveness = make(chan bool, 10)
@@ -144,7 +144,7 @@
}
// Extract Alive status of Kvstore based on type of error
-func (b *Backend) isErrorIndicatingAliveKvstore(err error) bool {
+func (b *Backend) isErrorIndicatingAliveKvstore(ctx context.Context, err error) bool {
// Alive unless observed an error indicating so
alive := true
@@ -182,64 +182,64 @@
// 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) {
- formattedPath := b.makePath(key)
- logger.Debugw("listing-key", log.Fields{"key": key, "path": formattedPath})
+ formattedPath := b.makePath(ctx, key)
+ logger.Debugw(ctx, "listing-key", log.Fields{"key": key, "path": formattedPath})
pair, err := b.Client.List(ctx, formattedPath)
- b.updateLiveness(b.isErrorIndicatingAliveKvstore(err))
+ b.updateLiveness(ctx, b.isErrorIndicatingAliveKvstore(ctx, err))
return pair, err
}
// Get retrieves an item that matches the specified key
func (b *Backend) Get(ctx context.Context, key string) (*kvstore.KVPair, error) {
- formattedPath := b.makePath(key)
- logger.Debugw("getting-key", log.Fields{"key": key, "path": formattedPath})
+ formattedPath := b.makePath(ctx, key)
+ logger.Debugw(ctx, "getting-key", log.Fields{"key": key, "path": formattedPath})
pair, err := b.Client.Get(ctx, formattedPath)
- b.updateLiveness(b.isErrorIndicatingAliveKvstore(err))
+ b.updateLiveness(ctx, b.isErrorIndicatingAliveKvstore(ctx, err))
return pair, err
}
// Put stores an item value under the specifed key
func (b *Backend) Put(ctx context.Context, key string, value interface{}) error {
- formattedPath := b.makePath(key)
- logger.Debugw("putting-key", log.Fields{"key": key, "path": formattedPath})
+ formattedPath := b.makePath(ctx, key)
+ logger.Debugw(ctx, "putting-key", log.Fields{"key": key, "path": formattedPath})
err := b.Client.Put(ctx, formattedPath, value)
- b.updateLiveness(b.isErrorIndicatingAliveKvstore(err))
+ b.updateLiveness(ctx, b.isErrorIndicatingAliveKvstore(ctx, err))
return err
}
// Delete removes an item under the specified key
func (b *Backend) Delete(ctx context.Context, key string) error {
- formattedPath := b.makePath(key)
- logger.Debugw("deleting-key", log.Fields{"key": key, "path": formattedPath})
+ formattedPath := b.makePath(ctx, key)
+ logger.Debugw(ctx, "deleting-key", log.Fields{"key": key, "path": formattedPath})
err := b.Client.Delete(ctx, formattedPath)
- b.updateLiveness(b.isErrorIndicatingAliveKvstore(err))
+ b.updateLiveness(ctx, b.isErrorIndicatingAliveKvstore(ctx, err))
return err
}
// CreateWatch starts watching events for the specified key
func (b *Backend) CreateWatch(ctx context.Context, key string, withPrefix bool) chan *kvstore.Event {
- formattedPath := b.makePath(key)
- logger.Debugw("creating-key-watch", log.Fields{"key": key, "path": formattedPath})
+ formattedPath := b.makePath(ctx, key)
+ logger.Debugw(ctx, "creating-key-watch", log.Fields{"key": key, "path": formattedPath})
return b.Client.Watch(ctx, formattedPath, withPrefix)
}
// DeleteWatch stops watching events for the specified key
-func (b *Backend) DeleteWatch(key string, ch chan *kvstore.Event) {
- formattedPath := b.makePath(key)
- logger.Debugw("deleting-key-watch", log.Fields{"key": key, "path": formattedPath})
+func (b *Backend) DeleteWatch(ctx context.Context, key string, ch chan *kvstore.Event) {
+ formattedPath := b.makePath(ctx, key)
+ logger.Debugw(ctx, "deleting-key-watch", log.Fields{"key": key, "path": formattedPath})
- b.Client.CloseWatch(formattedPath, ch)
+ b.Client.CloseWatch(ctx, formattedPath, ch)
}
diff --git a/pkg/db/backend_test.go b/pkg/db/backend_test.go
index b8c66bd..98b72a0 100644
--- a/pkg/db/backend_test.go
+++ b/pkg/db/backend_test.go
@@ -41,35 +41,37 @@
)
func TestMain(m *testing.M) {
+ ctx := context.Background()
var err error
embedEtcdServerPort, err = freeport.GetFreePort()
if err != nil {
- logger.Fatal(err)
+ logger.Fatal(ctx, err)
}
dummyEtcdServerPort, err = freeport.GetFreePort()
if err != nil {
- logger.Fatal(err)
+ logger.Fatal(ctx, err)
}
peerPort, err := freeport.GetFreePort()
if err != nil {
- logger.Fatal(err)
+ logger.Fatal(ctx, err)
}
- etcdServer := mocks.StartEtcdServer(mocks.MKConfig("voltha.db.test", embedEtcdServerPort, peerPort, "voltha.lib.db", "error"))
+ etcdServer := mocks.StartEtcdServer(ctx, mocks.MKConfig(ctx, "voltha.db.test", embedEtcdServerPort, peerPort, "voltha.lib.db", "error"))
res := m.Run()
- etcdServer.Stop()
+ etcdServer.Stop(ctx)
os.Exit(res)
}
func provisionBackendWithEmbeddedEtcdServer(t *testing.T) *Backend {
- backend := NewBackend("etcd", embedEtcdServerHost+":"+strconv.Itoa(embedEtcdServerPort), defaultTimeout, defaultPathPrefix)
+ ctx := context.Background()
+ backend := NewBackend(ctx, "etcd", embedEtcdServerHost+":"+strconv.Itoa(embedEtcdServerPort), defaultTimeout, defaultPathPrefix)
assert.NotNil(t, backend)
assert.NotNil(t, backend.Client)
return backend
}
func provisionBackendWithDummyEtcdServer(t *testing.T) *Backend {
- backend := NewBackend("etcd", embedEtcdServerHost+":"+strconv.Itoa(dummyEtcdServerPort), defaultTimeout, defaultPathPrefix)
+ backend := NewBackend(context.Background(), "etcd", embedEtcdServerHost+":"+strconv.Itoa(dummyEtcdServerPort), defaultTimeout, defaultPathPrefix)
assert.NotNil(t, backend)
assert.NotNil(t, backend.Client)
return backend
@@ -78,7 +80,7 @@
// Create instance using Etcd Kvstore
func TestNewBackend_EtcdKvStore(t *testing.T) {
address := embedEtcdServerHost + ":" + strconv.Itoa(embedEtcdServerPort)
- backend := NewBackend("etcd", address, defaultTimeout, defaultPathPrefix)
+ backend := NewBackend(context.Background(), "etcd", address, defaultTimeout, defaultPathPrefix)
// Verify all attributes of backend have got set correctly
assert.NotNil(t, backend)
@@ -94,7 +96,7 @@
// Create instance using Consul Kvstore
func TestNewBackend_ConsulKvStore(t *testing.T) {
- backend := NewBackend("consul", embedEtcdServerHost+":"+strconv.Itoa(embedEtcdServerPort), defaultTimeout, defaultPathPrefix)
+ backend := NewBackend(context.Background(), "consul", embedEtcdServerHost+":"+strconv.Itoa(embedEtcdServerPort), defaultTimeout, defaultPathPrefix)
// Verify kvstore type attribute of backend has got set correctly
assert.NotNil(t, backend)
@@ -104,7 +106,7 @@
// Create instance using Invalid Kvstore; instance creation should fail
func TestNewBackend_InvalidKvstore(t *testing.T) {
- backend := NewBackend("unknown", embedEtcdServerHost+":"+strconv.Itoa(embedEtcdServerPort), defaultTimeout, defaultPathPrefix)
+ backend := NewBackend(context.Background(), "unknown", embedEtcdServerHost+":"+strconv.Itoa(embedEtcdServerPort), defaultTimeout, defaultPathPrefix)
assert.NotNil(t, backend)
assert.Nil(t, backend.Client)
@@ -112,7 +114,7 @@
func TestMakePath(t *testing.T) {
backend := provisionBackendWithEmbeddedEtcdServer(t)
- path := backend.makePath("Suffix")
+ path := backend.makePath(context.Background(), "Suffix")
assert.Equal(t, defaultPathPrefix+"/Suffix", path)
}
@@ -138,7 +140,7 @@
func TestEnableLivenessChannel_EmbeddedEtcdServer_BeforeLivenessCheck(t *testing.T) {
backend := provisionBackendWithEmbeddedEtcdServer(t)
- alive := backend.EnableLivenessChannel()
+ alive := backend.EnableLivenessChannel(context.Background())
assert.NotNil(t, alive)
assert.Equal(t, 1, len(alive))
assert.Equal(t, false, <-alive)
@@ -152,7 +154,7 @@
defer cancel()
backend.PerformLivenessCheck(ctx)
- alive := backend.EnableLivenessChannel()
+ alive := backend.EnableLivenessChannel(ctx)
assert.NotNil(t, alive)
assert.Equal(t, 1, len(alive))
assert.Equal(t, true, <-alive)
@@ -163,14 +165,14 @@
func TestUpdateLiveness_AliveStatusChange(t *testing.T) {
backend := provisionBackendWithEmbeddedEtcdServer(t)
// Enable Liveness Channel and verify initial state is not-alive
- aliveState := backend.EnableLivenessChannel()
+ aliveState := backend.EnableLivenessChannel(context.Background())
assert.NotNil(t, aliveState)
assert.Equal(t, 1, len(backend.liveness))
assert.Equal(t, false, <-backend.liveness)
lastUpdateTime := backend.lastLivenessTime
// Update with changed alive state. Verify alive state push & liveness time update
- backend.updateLiveness(true)
+ backend.updateLiveness(context.Background(), true)
assert.Equal(t, 1, len(backend.liveness))
assert.Equal(t, true, <-backend.liveness)
assert.NotEqual(t, lastUpdateTime, backend.lastLivenessTime)
@@ -180,13 +182,13 @@
func TestUpdateLiveness_AliveStatusUnchanged(t *testing.T) {
backend := provisionBackendWithEmbeddedEtcdServer(t)
// Enable Liveness Channel and verify initial state is not-alive
- aliveState := backend.EnableLivenessChannel()
+ aliveState := backend.EnableLivenessChannel(context.Background())
assert.NotNil(t, aliveState)
assert.Equal(t, false, <-backend.liveness)
lastUpdateTime := backend.lastLivenessTime
// Update with same alive state. Verify no further alive state push
- backend.updateLiveness(false)
+ backend.updateLiveness(context.Background(), false)
assert.Equal(t, 0, len(backend.liveness))
assert.Equal(t, lastUpdateTime, backend.lastLivenessTime)
@@ -195,7 +197,7 @@
backend.lastLivenessTime = time.Now().Add(-tenMinDuration)
lastUpdateTime = backend.lastLivenessTime
- backend.updateLiveness(false)
+ backend.updateLiveness(context.Background(), false)
assert.Equal(t, 1, len(backend.liveness))
assert.Equal(t, false, <-backend.liveness)
assert.NotEqual(t, lastUpdateTime, backend.lastLivenessTime)
@@ -223,7 +225,7 @@
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- if backend.isErrorIndicatingAliveKvstore(tt.arg) != tt.want {
+ if backend.isErrorIndicatingAliveKvstore(context.Background(), tt.arg) != tt.want {
t.Errorf("isErrorIndicatingAliveKvstore failed for %s: expected %t but got %t", tt.name, tt.want, !tt.want)
}
})
@@ -411,7 +413,7 @@
time.Sleep(time.Millisecond * 100)
assert.Equal(t, 1, len(eventChan))
- backend.DeleteWatch("key5", eventChan)
+ backend.DeleteWatch(context.Background(), "key5", eventChan)
}
// Test Create and Delete Watch with prefix for Embedded Etcd Server
@@ -432,5 +434,5 @@
time.Sleep(time.Millisecond * 100)
assert.Equal(t, 1, len(eventChan))
- backend.DeleteWatch("key6", eventChan)
+ backend.DeleteWatch(context.Background(), "key6", eventChan)
}
diff --git a/pkg/db/common.go b/pkg/db/common.go
index 1cf2e1c..fe84b46 100644
--- a/pkg/db/common.go
+++ b/pkg/db/common.go
@@ -19,12 +19,12 @@
"github.com/opencord/voltha-lib-go/v3/pkg/log"
)
-var logger log.Logger
+var logger log.CLogger
func init() {
// Setup this package so that it's log level can be modified at run time
var err error
- logger, err = log.AddPackage(log.JSON, log.ErrorLevel, log.Fields{"pkg": "db"})
+ logger, err = log.RegisterPackage(log.JSON, log.ErrorLevel, log.Fields{"pkg": "db"})
if err != nil {
panic(err)
}
diff --git a/pkg/db/kvstore/client.go b/pkg/db/kvstore/client.go
index 158e626..480d476 100644
--- a/pkg/db/kvstore/client.go
+++ b/pkg/db/kvstore/client.go
@@ -88,6 +88,6 @@
AcquireLock(ctx context.Context, lockName string, timeout time.Duration) error
ReleaseLock(lockName string) error
IsConnectionUp(ctx context.Context) bool // timeout in second
- CloseWatch(key string, ch chan *Event)
- Close()
+ CloseWatch(ctx context.Context, key string, ch chan *Event)
+ Close(ctx context.Context)
}
diff --git a/pkg/db/kvstore/common.go b/pkg/db/kvstore/common.go
index aa7aeb0..0de395f 100644
--- a/pkg/db/kvstore/common.go
+++ b/pkg/db/kvstore/common.go
@@ -19,12 +19,12 @@
"github.com/opencord/voltha-lib-go/v3/pkg/log"
)
-var logger log.Logger
+var logger log.CLogger
func init() {
// Setup this package so that it's log level can be modified at run time
var err error
- logger, err = log.AddPackage(log.JSON, log.ErrorLevel, log.Fields{"pkg": "kvstore"})
+ logger, err = log.RegisterPackage(log.JSON, log.ErrorLevel, log.Fields{"pkg": "kvstore"})
if err != nil {
panic(err)
}
diff --git a/pkg/db/kvstore/consulclient.go b/pkg/db/kvstore/consulclient.go
index d2544dd..c2cd841 100644
--- a/pkg/db/kvstore/consulclient.go
+++ b/pkg/db/kvstore/consulclient.go
@@ -44,14 +44,13 @@
}
// NewConsulClient returns a new client for the Consul KV store
-func NewConsulClient(addr string, timeout time.Duration) (*ConsulClient, error) {
-
+func NewConsulClient(ctx context.Context, addr string, timeout time.Duration) (*ConsulClient, error) {
config := consulapi.DefaultConfig()
config.Address = addr
config.WaitTime = timeout
consul, err := consulapi.NewClient(config)
if err != nil {
- logger.Error(err)
+ logger.Error(ctx, err)
return nil, err
}
@@ -63,7 +62,7 @@
// IsConnectionUp returns whether the connection to the Consul KV store is up
func (c *ConsulClient) IsConnectionUp(ctx context.Context) bool {
- logger.Error("Unimplemented function")
+ logger.Error(ctx, "Unimplemented function")
return false
}
@@ -80,7 +79,7 @@
// For now we ignore meta data
kvps, _, err := kv.List(key, &queryOptions)
if err != nil {
- logger.Error(err)
+ logger.Error(ctx, err)
return nil, err
}
m := make(map[string]*KVPair)
@@ -103,7 +102,7 @@
// For now we ignore meta data
kvp, _, err := kv.Get(key, &queryOptions)
if err != nil {
- logger.Error(err)
+ logger.Error(ctx, err)
return nil, err
}
if kvp != nil {
@@ -122,7 +121,7 @@
var val []byte
var er error
if val, er = ToByte(value); er != nil {
- logger.Error(er)
+ logger.Error(ctx, er)
return er
}
@@ -134,7 +133,7 @@
defer c.writeLock.Unlock()
_, err := kv.Put(&kvp, &writeOptions)
if err != nil {
- logger.Error(err)
+ logger.Error(ctx, err)
return err
}
return nil
@@ -149,26 +148,26 @@
defer c.writeLock.Unlock()
_, err := kv.Delete(key, &writeOptions)
if err != nil {
- logger.Error(err)
+ logger.Error(ctx, err)
return err
}
return nil
}
-func (c *ConsulClient) deleteSession() {
+func (c *ConsulClient) deleteSession(ctx context.Context) {
if c.sessionID != "" {
- logger.Debug("cleaning-up-session")
+ logger.Debug(ctx, "cleaning-up-session")
session := c.consul.Session()
_, err := session.Destroy(c.sessionID, nil)
if err != nil {
- logger.Errorw("error-cleaning-session", log.Fields{"session": c.sessionID, "error": err})
+ logger.Errorw(ctx, "error-cleaning-session", log.Fields{"session": c.sessionID, "error": err})
}
}
c.sessionID = ""
c.session = nil
}
-func (c *ConsulClient) createSession(ttl time.Duration, retries int) (*consulapi.Session, string, error) {
+func (c *ConsulClient) createSession(ctx context.Context, ttl time.Duration, retries int) (*consulapi.Session, string, error) {
session := c.consul.Session()
entry := &consulapi.SessionEntry{
Behavior: consulapi.SessionBehaviorDelete,
@@ -178,17 +177,17 @@
for {
id, meta, err := session.Create(entry, nil)
if err != nil {
- logger.Errorw("create-session-error", log.Fields{"error": err})
+ logger.Errorw(ctx, "create-session-error", log.Fields{"error": err})
if retries == 0 {
return nil, "", err
}
} else if meta.RequestTime == 0 {
- logger.Errorw("create-session-bad-meta-data", log.Fields{"meta-data": meta})
+ logger.Errorw(ctx, "create-session-bad-meta-data", log.Fields{"meta-data": meta})
if retries == 0 {
return nil, "", errors.New("bad-meta-data")
}
} else if id == "" {
- logger.Error("create-session-nil-id")
+ logger.Error(ctx, "create-session-nil-id")
if retries == 0 {
return nil, "", errors.New("ID-nil")
}
@@ -199,7 +198,7 @@
if retries > 0 {
retries--
}
- logger.Debug("retrying-session-create-after-a-second-delay")
+ logger.Debug(ctx, "retrying-session-create-after-a-second-delay")
time.Sleep(time.Duration(1) * time.Second)
}
}
@@ -226,30 +225,30 @@
var val []byte
var er error
if val, er = ToByte(value); er != nil {
- logger.Error(er)
+ logger.Error(ctx, er)
return nil, er
}
// Cleanup any existing session and recreate new ones. A key is reserved against a session
if c.sessionID != "" {
- c.deleteSession()
+ c.deleteSession(ctx)
}
// Clear session if reservation is not successful
reservationSuccessful := false
defer func() {
if !reservationSuccessful {
- logger.Debug("deleting-session")
- c.deleteSession()
+ logger.Debug(ctx, "deleting-session")
+ c.deleteSession(ctx)
}
}()
- session, sessionID, err := c.createSession(ttl, -1)
+ session, sessionID, err := c.createSession(ctx, ttl, -1)
if err != nil {
- logger.Errorw("no-session-created", log.Fields{"error": err})
+ logger.Errorw(ctx, "no-session-created", log.Fields{"error": err})
return "", errors.New("no-session-created")
}
- logger.Debugw("session-created", log.Fields{"session-id": sessionID})
+ logger.Debugw(ctx, "session-created", log.Fields{"session-id": sessionID})
c.sessionID = sessionID
c.session = session
@@ -258,11 +257,11 @@
kvp := consulapi.KVPair{Key: key, Value: val, Session: c.sessionID}
result, _, err := kv.Acquire(&kvp, nil)
if err != nil {
- logger.Errorw("error-acquiring-keys", log.Fields{"error": err})
+ logger.Errorw(ctx, "error-acquiring-keys", log.Fields{"error": err})
return nil, err
}
- logger.Debugw("key-acquired", log.Fields{"key": key, "status": result})
+ logger.Debugw(ctx, "key-acquired", log.Fields{"key": key, "status": result})
// Irrespective whether we were successful in acquiring the key, let's read it back and see if it's us.
m, err := c.Get(ctx, key)
@@ -270,7 +269,7 @@
return nil, err
}
if m != nil {
- logger.Debugw("response-received", log.Fields{"key": m.Key, "m.value": string(m.Value.([]byte)), "value": value})
+ logger.Debugw(ctx, "response-received", log.Fields{"key": m.Key, "m.value": string(m.Value.([]byte)), "value": value})
if m.Key == key && isEqual(m.Value, value) {
// My reservation is successful - register it. For now, support is only for 1 reservation per key
// per session.
@@ -300,11 +299,11 @@
kvp = consulapi.KVPair{Key: key, Value: value.([]byte), Session: c.sessionID}
result, _, err = kv.Release(&kvp, nil)
if err != nil {
- logger.Errorw("cannot-release-reservation", log.Fields{"key": key, "error": err})
+ logger.Errorw(ctx, "cannot-release-reservation", log.Fields{"key": key, "error": err})
return err
}
if !result {
- logger.Errorw("cannot-release-reservation", log.Fields{"key": key})
+ logger.Errorw(ctx, "cannot-release-reservation", log.Fields{"key": key})
}
delete(c.keyReservations, key)
}
@@ -384,21 +383,21 @@
// CloseWatch closes a specific watch. Both the key and the channel are required when closing a watch as there
// may be multiple listeners on the same key. The previously created channel serves as a key
-func (c *ConsulClient) CloseWatch(key string, ch chan *Event) {
+func (c *ConsulClient) CloseWatch(ctx context.Context, key string, ch chan *Event) {
// First close the context
var ok bool
var watchedChannelsContexts []*channelContextMap
c.writeLock.Lock()
defer c.writeLock.Unlock()
if watchedChannelsContexts, ok = c.watchedChannelsContext[key]; !ok {
- logger.Errorw("key-has-no-watched-context-or-channel", log.Fields{"key": key})
+ logger.Errorw(ctx, "key-has-no-watched-context-or-channel", log.Fields{"key": key})
return
}
// Look for the channels
var pos = -1
for i, chCtxMap := range watchedChannelsContexts {
if chCtxMap.channel == ch {
- logger.Debug("channel-found")
+ logger.Debug(ctx, "channel-found")
chCtxMap.cancel()
//close the channel
close(ch)
@@ -410,7 +409,7 @@
if pos >= 0 {
c.watchedChannelsContext[key] = append(c.watchedChannelsContext[key][:pos], c.watchedChannelsContext[key][pos+1:]...)
}
- logger.Debugw("watched-channel-exiting", log.Fields{"key": key, "channel": c.watchedChannelsContext[key]})
+ logger.Debugw(ctx, "watched-channel-exiting", log.Fields{"key": key, "channel": c.watchedChannelsContext[key]})
}
func (c *ConsulClient) isKVEqual(kv1 *consulapi.KVPair, kv2 *consulapi.KVPair) bool {
@@ -430,10 +429,10 @@
return true
}
-func (c *ConsulClient) listenForKeyChange(watchContext context.Context, key string, ch chan *Event) {
- logger.Debugw("start-watching-channel", log.Fields{"key": key, "channel": ch})
+func (c *ConsulClient) listenForKeyChange(ctx context.Context, key string, ch chan *Event) {
+ logger.Debugw(ctx, "start-watching-channel", log.Fields{"key": key, "channel": ch})
- defer c.CloseWatch(key, ch)
+ defer c.CloseWatch(ctx, key, ch)
kv := c.consul.KV()
var queryOptions consulapi.QueryOptions
queryOptions.WaitTime = defaultKVGetTimeout
@@ -441,7 +440,7 @@
// Get the existing value, if any
previousKVPair, meta, err := kv.Get(key, &queryOptions)
if err != nil {
- logger.Debug(err)
+ logger.Debug(ctx, err)
}
lastIndex := meta.LastIndex
@@ -449,37 +448,37 @@
//var waitOptions consulapi.QueryOptions
var pair *consulapi.KVPair
//watchContext, _ := context.WithCancel(context.Background())
- waitOptions := queryOptions.WithContext(watchContext)
+ waitOptions := queryOptions.WithContext(ctx)
for {
//waitOptions = consulapi.QueryOptions{WaitIndex: lastIndex}
waitOptions.WaitIndex = lastIndex
pair, meta, err = kv.Get(key, waitOptions)
select {
- case <-watchContext.Done():
- logger.Debug("done-event-received-exiting")
+ case <-ctx.Done():
+ logger.Debug(ctx, "done-event-received-exiting")
return
default:
if err != nil {
- logger.Warnw("error-from-watch", log.Fields{"error": err})
+ logger.Warnw(ctx, "error-from-watch", log.Fields{"error": err})
ch <- NewEvent(CONNECTIONDOWN, key, []byte(""), -1)
} else {
- logger.Debugw("index-state", log.Fields{"lastindex": lastIndex, "newindex": meta.LastIndex, "key": key})
+ logger.Debugw(ctx, "index-state", log.Fields{"lastindex": lastIndex, "newindex": meta.LastIndex, "key": key})
}
}
if err != nil {
- logger.Debug(err)
+ logger.Debug(ctx, err)
// On error, block for 10 milliseconds to prevent endless loop
time.Sleep(10 * time.Millisecond)
} else if meta.LastIndex <= lastIndex {
- logger.Info("no-index-change-or-negative")
+ logger.Info(ctx, "no-index-change-or-negative")
} else {
- logger.Debugw("update-received", log.Fields{"pair": pair})
+ logger.Debugw(ctx, "update-received", log.Fields{"pair": pair})
if pair == nil {
ch <- NewEvent(DELETE, key, []byte(""), -1)
} else if !c.isKVEqual(pair, previousKVPair) {
// Push the change onto the channel if the data has changed
// For now just assume it's a PUT change
- logger.Debugw("pair-details", log.Fields{"session": pair.Session, "key": pair.Key, "value": pair.Value})
+ logger.Debugw(ctx, "pair-details", log.Fields{"session": pair.Session, "key": pair.Key, "value": pair.Value})
ch <- NewEvent(PUT, pair.Key, pair.Value, -1)
}
previousKVPair = pair
@@ -489,7 +488,7 @@
}
// Close closes the KV store client
-func (c *ConsulClient) Close() {
+func (c *ConsulClient) Close(ctx context.Context) {
var writeOptions consulapi.WriteOptions
// Inform any goroutine it's time to say goodbye.
c.writeLock.Lock()
@@ -500,7 +499,7 @@
// Clear the sessionID
if _, err := c.consul.Session().Destroy(c.sessionID, &writeOptions); err != nil {
- logger.Errorw("error-closing-client", log.Fields{"error": err})
+ logger.Errorw(ctx, "error-closing-client", log.Fields{"error": err})
}
}
diff --git a/pkg/db/kvstore/etcdclient.go b/pkg/db/kvstore/etcdclient.go
index 8d4a462..0165e18 100644
--- a/pkg/db/kvstore/etcdclient.go
+++ b/pkg/db/kvstore/etcdclient.go
@@ -40,7 +40,7 @@
}
// NewEtcdClient returns a new client for the Etcd KV store
-func NewEtcdClient(addr string, timeout time.Duration, level log.LogLevel) (*EtcdClient, error) {
+func NewEtcdClient(ctx context.Context, addr string, timeout time.Duration, level log.LogLevel) (*EtcdClient, error) {
logconfig := log.ConstructZapConfig(log.JSON, level, log.Fields{})
c, err := v3Client.New(v3Client.Config{
@@ -49,7 +49,7 @@
LogConfig: &logconfig,
})
if err != nil {
- logger.Error(err)
+ logger.Error(ctx, err)
return nil, err
}
@@ -77,7 +77,7 @@
func (c *EtcdClient) List(ctx context.Context, key string) (map[string]*KVPair, error) {
resp, err := c.ectdAPI.Get(ctx, key, v3Client.WithPrefix())
if err != nil {
- logger.Error(err)
+ logger.Error(ctx, err)
return nil, err
}
m := make(map[string]*KVPair)
@@ -94,7 +94,7 @@
resp, err := c.ectdAPI.Get(ctx, key)
if err != nil {
- logger.Error(err)
+ logger.Error(ctx, err)
return nil, err
}
for _, ev := range resp.Kvs {
@@ -131,13 +131,13 @@
if err != nil {
switch err {
case context.Canceled:
- logger.Warnw("context-cancelled", log.Fields{"error": err})
+ logger.Warnw(ctx, "context-cancelled", log.Fields{"error": err})
case context.DeadlineExceeded:
- logger.Warnw("context-deadline-exceeded", log.Fields{"error": err})
+ logger.Warnw(ctx, "context-deadline-exceeded", log.Fields{"error": err})
case v3rpcTypes.ErrEmptyKey:
- logger.Warnw("etcd-client-error", log.Fields{"error": err})
+ logger.Warnw(ctx, "etcd-client-error", log.Fields{"error": err})
default:
- logger.Warnw("bad-endpoints", log.Fields{"error": err})
+ logger.Warnw(ctx, "bad-endpoints", log.Fields{"error": err})
}
return err
}
@@ -150,10 +150,10 @@
// delete the key
if _, err := c.ectdAPI.Delete(ctx, key); err != nil {
- logger.Errorw("failed-to-delete-key", log.Fields{"key": key, "error": err})
+ logger.Errorw(ctx, "failed-to-delete-key", log.Fields{"key": key, "error": err})
return err
}
- logger.Debugw("key(s)-deleted", log.Fields{"key": key})
+ logger.Debugw(ctx, "key(s)-deleted", log.Fields{"key": key})
return nil
}
@@ -172,7 +172,7 @@
resp, err := c.ectdAPI.Grant(ctx, int64(ttl.Seconds()))
if err != nil {
- logger.Error(err)
+ logger.Error(ctx, err)
return nil, err
}
// Register the lease id
@@ -185,7 +185,7 @@
defer func() {
if !reservationSuccessful {
if err = c.ReleaseReservation(context.Background(), key); err != nil {
- logger.Error("cannot-release-lease")
+ logger.Error(ctx, "cannot-release-lease")
}
}
}()
@@ -240,7 +240,7 @@
for key, leaseID := range c.keyReservations {
_, err := c.ectdAPI.Revoke(ctx, *leaseID)
if err != nil {
- logger.Errorw("cannot-release-reservation", log.Fields{"key": key, "error": err})
+ logger.Errorw(ctx, "cannot-release-reservation", log.Fields{"key": key, "error": err})
return err
}
delete(c.keyReservations, key)
@@ -251,7 +251,7 @@
// ReleaseReservation releases reservation for a specific key.
func (c *EtcdClient) ReleaseReservation(ctx context.Context, key string) error {
// Get the leaseid using the key
- logger.Debugw("Release-reservation", log.Fields{"key": key})
+ logger.Debugw(ctx, "Release-reservation", log.Fields{"key": key})
var ok bool
var leaseID *v3Client.LeaseID
c.keyReservationsLock.Lock()
@@ -263,7 +263,7 @@
if leaseID != nil {
_, err := c.ectdAPI.Revoke(ctx, *leaseID)
if err != nil {
- logger.Error(err)
+ logger.Error(ctx, err)
return err
}
delete(c.keyReservations, key)
@@ -288,7 +288,7 @@
if leaseID != nil {
_, err := c.ectdAPI.KeepAliveOnce(ctx, *leaseID)
if err != nil {
- logger.Errorw("lease-may-have-expired", log.Fields{"error": err})
+ logger.Errorw(ctx, "lease-may-have-expired", log.Fields{"error": err})
return err
}
} else {
@@ -320,9 +320,9 @@
// Changing the log field (from channelMaps) as the underlying logger cannot format the map of channels into a
// json format.
- logger.Debugw("watched-channels", log.Fields{"len": len(channelMaps)})
+ logger.Debugw(ctx, "watched-channels", log.Fields{"len": len(channelMaps)})
// Launch a go routine to listen for updates
- go c.listenForKeyChange(channel, ch, cancel)
+ go c.listenForKeyChange(ctx, channel, ch, cancel)
return ch
@@ -369,23 +369,23 @@
// CloseWatch closes a specific watch. Both the key and the channel are required when closing a watch as there
// may be multiple listeners on the same key. The previously created channel serves as a key
-func (c *EtcdClient) CloseWatch(key string, ch chan *Event) {
+func (c *EtcdClient) CloseWatch(ctx context.Context, key string, ch chan *Event) {
// Get the array of channels mapping
var watchedChannels []map[chan *Event]v3Client.Watcher
var ok bool
if watchedChannels, ok = c.getChannelMaps(key); !ok {
- logger.Warnw("key-has-no-watched-channels", log.Fields{"key": key})
+ logger.Warnw(ctx, "key-has-no-watched-channels", log.Fields{"key": key})
return
}
// Look for the channels
var pos = -1
for i, chMap := range watchedChannels {
if t, ok := chMap[ch]; ok {
- logger.Debug("channel-found")
+ logger.Debug(ctx, "channel-found")
// Close the etcd watcher before the client channel. This should close the etcd channel as well
if err := t.Close(); err != nil {
- logger.Errorw("watcher-cannot-be-closed", log.Fields{"key": key, "error": err})
+ logger.Errorw(ctx, "watcher-cannot-be-closed", log.Fields{"key": key, "error": err})
}
pos = i
break
@@ -397,11 +397,11 @@
if pos >= 0 {
channelMaps = c.removeChannelMap(key, pos)
}
- logger.Infow("watcher-channel-exiting", log.Fields{"key": key, "channel": channelMaps})
+ logger.Infow(ctx, "watcher-channel-exiting", log.Fields{"key": key, "channel": channelMaps})
}
-func (c *EtcdClient) listenForKeyChange(channel v3Client.WatchChan, ch chan<- *Event, cancel context.CancelFunc) {
- logger.Debug("start-listening-on-channel ...")
+func (c *EtcdClient) listenForKeyChange(ctx context.Context, channel v3Client.WatchChan, ch chan<- *Event, cancel context.CancelFunc) {
+ logger.Debug(ctx, "start-listening-on-channel ...")
defer cancel()
defer close(ch)
for resp := range channel {
@@ -409,7 +409,7 @@
ch <- NewEvent(getEventType(ev), ev.Kv.Key, ev.Kv.Value, ev.Kv.Version)
}
}
- logger.Debug("stop-listening-on-channel ...")
+ logger.Debug(ctx, "stop-listening-on-channel ...")
}
func getEventType(event *v3Client.Event) int {
@@ -423,9 +423,9 @@
}
// Close closes the KV store client
-func (c *EtcdClient) Close() {
+func (c *EtcdClient) Close(ctx context.Context) {
if err := c.ectdAPI.Close(); err != nil {
- logger.Errorw("error-closing-client", log.Fields{"error": err})
+ logger.Errorw(ctx, "error-closing-client", log.Fields{"error": err})
}
}
diff --git a/pkg/flows/common.go b/pkg/flows/common.go
index 557de3f..0328d72 100644
--- a/pkg/flows/common.go
+++ b/pkg/flows/common.go
@@ -19,12 +19,12 @@
"github.com/opencord/voltha-lib-go/v3/pkg/log"
)
-var logger log.Logger
+var logger log.CLogger
func init() {
// Setup this package so that it's log level can be modified at run time
var err error
- logger, err = log.AddPackage(log.JSON, log.ErrorLevel, log.Fields{"pkg": "flowsUtils"})
+ logger, err = log.RegisterPackage(log.JSON, log.ErrorLevel, log.Fields{"pkg": "flowsUtils"})
if err != nil {
panic(err)
}
diff --git a/pkg/flows/flow_utils.go b/pkg/flows/flow_utils.go
index 3139144..66e719c 100644
--- a/pkg/flows/flow_utils.go
+++ b/pkg/flows/flow_utils.go
@@ -17,6 +17,7 @@
import (
"bytes"
+ "context"
"crypto/md5"
"encoding/binary"
"fmt"
@@ -503,7 +504,7 @@
}
//GetMetaData - legacy get method (only want lower 32 bits)
-func GetMetaData(flow *ofp.OfpFlowStats) uint32 {
+func GetMetaData(ctx context.Context, flow *ofp.OfpFlowStats) uint32 {
if flow == nil {
return 0
}
@@ -512,11 +513,11 @@
return uint32(field.GetTableMetadata() & 0xFFFFFFFF)
}
}
- logger.Debug("No-metadata-present")
+ logger.Debug(ctx, "No-metadata-present")
return 0
}
-func GetMetaData64Bit(flow *ofp.OfpFlowStats) uint64 {
+func GetMetaData64Bit(ctx context.Context, flow *ofp.OfpFlowStats) uint64 {
if flow == nil {
return 0
}
@@ -525,12 +526,12 @@
return field.GetTableMetadata()
}
}
- logger.Debug("No-metadata-present")
+ logger.Debug(ctx, "No-metadata-present")
return 0
}
// function returns write metadata value from write_metadata action field
-func GetMetadataFromWriteMetadataAction(flow *ofp.OfpFlowStats) uint64 {
+func GetMetadataFromWriteMetadataAction(ctx context.Context, flow *ofp.OfpFlowStats) uint64 {
if flow != nil {
for _, instruction := range flow.Instructions {
if instruction.Type == uint32(WRITE_METADATA) {
@@ -540,11 +541,11 @@
}
}
}
- logger.Debugw("No-write-metadata-present", log.Fields{"flow": flow})
+ logger.Debugw(ctx, "No-write-metadata-present", log.Fields{"flow": flow})
return 0
}
-func GetTechProfileIDFromWriteMetaData(metadata uint64) uint16 {
+func GetTechProfileIDFromWriteMetaData(ctx context.Context, metadata uint64) uint16 {
/*
Write metadata instruction value (metadata) is 8 bytes:
MS 2 bytes: C Tag
@@ -554,15 +555,15 @@
This is set in the ONOS OltPipeline as a write metadata instruction
*/
var tpId uint16 = 0
- logger.Debugw("Write metadata value for Techprofile ID", log.Fields{"metadata": metadata})
+ logger.Debugw(ctx, "Write metadata value for Techprofile ID", log.Fields{"metadata": metadata})
if metadata != 0 {
tpId = uint16((metadata >> 32) & 0xFFFF)
- logger.Debugw("Found techprofile ID from write metadata action", log.Fields{"tpid": tpId})
+ logger.Debugw(ctx, "Found techprofile ID from write metadata action", log.Fields{"tpid": tpId})
}
return tpId
}
-func GetEgressPortNumberFromWriteMetadata(flow *ofp.OfpFlowStats) uint32 {
+func GetEgressPortNumberFromWriteMetadata(ctx context.Context, flow *ofp.OfpFlowStats) uint32 {
/*
Write metadata instruction value (metadata) is 8 bytes:
MS 2 bytes: C Tag
@@ -571,17 +572,17 @@
This is set in the ONOS OltPipeline as a write metadata instruction
*/
var uniPort uint32 = 0
- md := GetMetadataFromWriteMetadataAction(flow)
- logger.Debugw("Metadata found for egress/uni port ", log.Fields{"metadata": md})
+ md := GetMetadataFromWriteMetadataAction(ctx, flow)
+ logger.Debugw(ctx, "Metadata found for egress/uni port ", log.Fields{"metadata": md})
if md != 0 {
uniPort = uint32(md & 0xFFFFFFFF)
- logger.Debugw("Found EgressPort from write metadata action", log.Fields{"egress_port": uniPort})
+ logger.Debugw(ctx, "Found EgressPort from write metadata action", log.Fields{"egress_port": uniPort})
}
return uniPort
}
-func GetInnerTagFromMetaData(flow *ofp.OfpFlowStats) uint16 {
+func GetInnerTagFromMetaData(ctx context.Context, flow *ofp.OfpFlowStats) uint16 {
/*
Write metadata instruction value (metadata) is 8 bytes:
MS 2 bytes: C Tag
@@ -590,10 +591,10 @@
This is set in the ONOS OltPipeline as a write metadata instruction
*/
var innerTag uint16 = 0
- md := GetMetadataFromWriteMetadataAction(flow)
+ md := GetMetadataFromWriteMetadataAction(ctx, flow)
if md != 0 {
innerTag = uint16((md >> 48) & 0xFFFF)
- logger.Debugw("Found CVLAN from write metadate action", log.Fields{"c_vlan": innerTag})
+ logger.Debugw(ctx, "Found CVLAN from write metadate action", log.Fields{"c_vlan": innerTag})
}
return innerTag
}
@@ -607,7 +608,7 @@
return 0
}
if md <= 0xffffffff {
- logger.Debugw("onos-upgrade-suggested", logger.Fields{"Metadata_ofp": md, "message": "Legacy MetaData detected form OltPipeline"})
+ logger.Debugw(ctx, "onos-upgrade-suggested", logger.Fields{"Metadata_ofp": md, "message": "Legacy MetaData detected form OltPipeline"})
return md
}
return (md >> 32) & 0xffffffff
@@ -937,12 +938,12 @@
}
// flowStatsEntryFromFlowModMessage maps an ofp_flow_mod message to an ofp_flow_stats message
-func MeterEntryFromMeterMod(meterMod *ofp.OfpMeterMod) *ofp.OfpMeterEntry {
+func MeterEntryFromMeterMod(ctx context.Context, meterMod *ofp.OfpMeterMod) *ofp.OfpMeterEntry {
bandStats := make([]*ofp.OfpMeterBandStats, 0)
meter := &ofp.OfpMeterEntry{Config: &ofp.OfpMeterConfig{},
Stats: &ofp.OfpMeterStats{BandStats: bandStats}}
if meterMod == nil {
- logger.Error("Invalid meter mod command")
+ logger.Error(ctx, "Invalid meter mod command")
return meter
}
// config init
@@ -964,7 +965,7 @@
bandStats = append(bandStats, band)
}
meter.Stats.BandStats = bandStats
- logger.Debugw("Allocated meter entry", log.Fields{"meter": *meter})
+ logger.Debugw(ctx, "Allocated meter entry", log.Fields{"meter": *meter})
return meter
}
diff --git a/pkg/grpc/common.go b/pkg/grpc/common.go
index 17eeeaf..fa53542 100644
--- a/pkg/grpc/common.go
+++ b/pkg/grpc/common.go
@@ -19,12 +19,12 @@
"github.com/opencord/voltha-lib-go/v3/pkg/log"
)
-var logger log.Logger
+var logger log.CLogger
func init() {
// Setup this package so that it's log level can be modified at run time
var err error
- logger, err = log.AddPackage(log.JSON, log.ErrorLevel, log.Fields{"pkg": "grpc"})
+ logger, err = log.RegisterPackage(log.JSON, log.ErrorLevel, log.Fields{"pkg": "grpc"})
if err != nil {
panic(err)
}
diff --git a/pkg/grpc/server.go b/pkg/grpc/server.go
index fa5c521..2bf7696 100644
--- a/pkg/grpc/server.go
+++ b/pkg/grpc/server.go
@@ -93,19 +93,19 @@
lis, err := net.Listen("tcp", s.address)
if err != nil {
- logger.Fatalf("failed to listen: %v", err)
+ logger.Fatalf(ctx, "failed to listen: %v", err)
}
if s.secure && s.GrpcSecurity != nil {
creds, err := credentials.NewServerTLSFromFile(s.CertFile, s.KeyFile)
if err != nil {
- logger.Fatalf("could not load TLS keys: %s", err)
+ logger.Fatalf(ctx, "could not load TLS keys: %s", err)
}
s.gs = grpc.NewServer(grpc.Creds(creds),
withServerUnaryInterceptor(s))
} else {
- logger.Info("starting-insecure-grpc-server")
+ logger.Info(ctx, "starting-insecure-grpc-server")
s.gs = grpc.NewServer(withServerUnaryInterceptor(s))
}
@@ -115,7 +115,7 @@
}
if err := s.gs.Serve(lis); err != nil {
- logger.Fatalf("failed to serve: %v\n", err)
+ logger.Fatalf(ctx, "failed to serve: %v\n", err)
}
}
@@ -138,7 +138,7 @@
handler grpc.UnaryHandler) (interface{}, error) {
if (s.probe != nil) && (!s.probe.IsReady()) {
- logger.Warnf("Grpc request received while not ready %v", req)
+ logger.Warnf(ctx, "Grpc request received while not ready %v", req)
return nil, status.Error(codes.Unavailable, "system is not ready")
}
diff --git a/pkg/kafka/client.go b/pkg/kafka/client.go
index 0d9e3a5..d977e38 100755
--- a/pkg/kafka/client.go
+++ b/pkg/kafka/client.go
@@ -16,9 +16,9 @@
package kafka
import (
- "time"
-
+ "context"
ca "github.com/opencord/voltha-protos/v3/go/inter_container"
+ "time"
)
const (
@@ -61,15 +61,15 @@
// MsgClient represents the set of APIs a Kafka MsgClient must implement
type Client interface {
- Start() error
- Stop()
- CreateTopic(topic *Topic, numPartition int, repFactor int) error
- DeleteTopic(topic *Topic) error
- Subscribe(topic *Topic, kvArgs ...*KVArg) (<-chan *ca.InterContainerMessage, error)
- UnSubscribe(topic *Topic, ch <-chan *ca.InterContainerMessage) error
- SubscribeForMetadata(func(fromTopic string, timestamp time.Time))
- Send(msg interface{}, topic *Topic, keys ...string) error
- SendLiveness() error
- EnableLivenessChannel(enable bool) chan bool
- EnableHealthinessChannel(enable bool) chan bool
+ Start(ctx context.Context) error
+ Stop(ctx context.Context)
+ CreateTopic(ctx context.Context, topic *Topic, numPartition int, repFactor int) error
+ DeleteTopic(ctx context.Context, topic *Topic) error
+ Subscribe(ctx context.Context, topic *Topic, kvArgs ...*KVArg) (<-chan *ca.InterContainerMessage, error)
+ UnSubscribe(ctx context.Context, topic *Topic, ch <-chan *ca.InterContainerMessage) error
+ SubscribeForMetadata(context.Context, func(fromTopic string, timestamp time.Time))
+ Send(ctx context.Context, msg interface{}, topic *Topic, keys ...string) error
+ SendLiveness(ctx context.Context) error
+ EnableLivenessChannel(ctx context.Context, enable bool) chan bool
+ EnableHealthinessChannel(ctx context.Context, enable bool) chan bool
}
diff --git a/pkg/kafka/common.go b/pkg/kafka/common.go
index 149c150..99b4cdf 100644
--- a/pkg/kafka/common.go
+++ b/pkg/kafka/common.go
@@ -19,12 +19,12 @@
"github.com/opencord/voltha-lib-go/v3/pkg/log"
)
-var logger log.Logger
+var logger log.CLogger
func init() {
// Setup this package so that it's log level can be modified at run time
var err error
- logger, err = log.AddPackage(log.JSON, log.ErrorLevel, log.Fields{"pkg": "kafka"})
+ logger, err = log.RegisterPackage(log.JSON, log.ErrorLevel, log.Fields{"pkg": "kafka"})
if err != nil {
panic(err)
}
diff --git a/pkg/kafka/endpoint_manager.go b/pkg/kafka/endpoint_manager.go
index 1258382..a876c09 100644
--- a/pkg/kafka/endpoint_manager.go
+++ b/pkg/kafka/endpoint_manager.go
@@ -50,15 +50,15 @@
// GetEndpoint is called to get the endpoint to communicate with for a specific device and service type. For
// now this will return the topic name
- GetEndpoint(deviceID string, serviceType string) (Endpoint, error)
+ GetEndpoint(ctx context.Context, deviceID string, serviceType string) (Endpoint, error)
// IsDeviceOwnedByService is invoked when a specific service (service type + replicaNumber) is restarted and
// devices owned by that service need to be reconciled
- IsDeviceOwnedByService(deviceID string, serviceType string, replicaNumber int32) (bool, error)
+ IsDeviceOwnedByService(ctx context.Context, deviceID string, serviceType string, replicaNumber int32) (bool, error)
// GetReplicaAssignment returns the replica number of the service that owns the deviceID. This is used by the
// test only
- GetReplicaAssignment(deviceID string, serviceType string) (ReplicaID, error)
+ GetReplicaAssignment(ctx context.Context, deviceID string, serviceType string) (ReplicaID, error)
}
type service struct {
@@ -119,9 +119,9 @@
return newEndpointManager(backend, opts...)
}
-func (ep *endpointManager) GetEndpoint(deviceID string, serviceType string) (Endpoint, error) {
- logger.Debugw("getting-endpoint", log.Fields{"device-id": deviceID, "service": serviceType})
- owner, err := ep.getOwner(deviceID, serviceType)
+func (ep *endpointManager) GetEndpoint(ctx context.Context, deviceID string, serviceType string) (Endpoint, error) {
+ logger.Debugw(ctx, "getting-endpoint", log.Fields{"device-id": deviceID, "service": serviceType})
+ owner, err := ep.getOwner(ctx, deviceID, serviceType)
if err != nil {
return "", err
}
@@ -133,13 +133,13 @@
if endpoint == "" {
return "", status.Errorf(codes.Unavailable, "endpoint-not-set-%s", serviceType)
}
- logger.Debugw("returning-endpoint", log.Fields{"device-id": deviceID, "service": serviceType, "endpoint": endpoint})
+ logger.Debugw(ctx, "returning-endpoint", log.Fields{"device-id": deviceID, "service": serviceType, "endpoint": endpoint})
return endpoint, nil
}
-func (ep *endpointManager) IsDeviceOwnedByService(deviceID string, serviceType string, replicaNumber int32) (bool, error) {
- logger.Debugw("device-ownership", log.Fields{"device-id": deviceID, "service": serviceType, "replica-number": replicaNumber})
- owner, err := ep.getOwner(deviceID, serviceType)
+func (ep *endpointManager) IsDeviceOwnedByService(ctx context.Context, deviceID string, serviceType string, replicaNumber int32) (bool, error) {
+ logger.Debugw(ctx, "device-ownership", log.Fields{"device-id": deviceID, "service": serviceType, "replica-number": replicaNumber})
+ owner, err := ep.getOwner(ctx, deviceID, serviceType)
if err != nil {
return false, nil
}
@@ -150,8 +150,8 @@
return m.getReplica() == ReplicaID(replicaNumber), nil
}
-func (ep *endpointManager) GetReplicaAssignment(deviceID string, serviceType string) (ReplicaID, error) {
- owner, err := ep.getOwner(deviceID, serviceType)
+func (ep *endpointManager) GetReplicaAssignment(ctx context.Context, deviceID string, serviceType string) (ReplicaID, error) {
+ owner, err := ep.getOwner(ctx, deviceID, serviceType)
if err != nil {
return 0, nil
}
@@ -162,8 +162,8 @@
return m.getReplica(), nil
}
-func (ep *endpointManager) getOwner(deviceID string, serviceType string) (consistent.Member, error) {
- serv, dType, err := ep.getServiceAndDeviceType(serviceType)
+func (ep *endpointManager) getOwner(ctx context.Context, deviceID string, serviceType string) (consistent.Member, error) {
+ serv, dType, err := ep.getServiceAndDeviceType(ctx, serviceType)
if err != nil {
return nil, err
}
@@ -171,7 +171,7 @@
return serv.consistentRing.LocateKey(key), nil
}
-func (ep *endpointManager) getServiceAndDeviceType(serviceType string) (*service, string, error) {
+func (ep *endpointManager) getServiceAndDeviceType(ctx context.Context, serviceType string) (*service, string, error) {
// Check whether service exist
ep.servicesLock.RLock()
serv, serviceExist := ep.services[serviceType]
@@ -179,7 +179,7 @@
// Load the service and device types if needed
if !serviceExist || serv == nil || int(serv.totalReplicas) != len(serv.consistentRing.GetMembers()) {
- if err := ep.loadServices(); err != nil {
+ if err := ep.loadServices(ctx); err != nil {
return nil, "", err
}
@@ -214,7 +214,7 @@
// loadServices loads the services (adapters) and device types in memory. Because of the small size of the data and
// the data format in the dB being binary protobuf then it is better to load all the data if inconsistency is detected,
// instead of watching for updates in the dB and acting on it.
-func (ep *endpointManager) loadServices() error {
+func (ep *endpointManager) loadServices(ctx context.Context) error {
ep.servicesLock.Lock()
defer ep.servicesLock.Unlock()
ep.deviceTypeServiceMapLock.Lock()
@@ -276,13 +276,13 @@
if logger.V(log.DebugLevel) {
for key, val := range ep.services {
members := val.consistentRing.GetMembers()
- logger.Debugw("service", log.Fields{"service": key, "expected-replica": val.totalReplicas, "replicas": len(val.consistentRing.GetMembers())})
+ logger.Debugw(ctx, "service", log.Fields{"service": key, "expected-replica": val.totalReplicas, "replicas": len(val.consistentRing.GetMembers())})
for _, m := range members {
n := m.(Member)
- logger.Debugw("service-loaded", log.Fields{"serviceId": n.getID(), "serviceType": n.getServiceType(), "replica": n.getReplica(), "endpoint": n.getEndPoint()})
+ logger.Debugw(ctx, "service-loaded", log.Fields{"serviceId": n.getID(), "serviceType": n.getServiceType(), "replica": n.getReplica(), "endpoint": n.getEndPoint()})
}
}
- logger.Debugw("device-types-loaded", log.Fields{"device-types": ep.deviceTypeServiceMap})
+ logger.Debugw(ctx, "device-types-loaded", log.Fields{"device-types": ep.deviceTypeServiceMap})
}
return nil
}
diff --git a/pkg/kafka/endpoint_manager_test.go b/pkg/kafka/endpoint_manager_test.go
index 3790221..a42cc2e 100644
--- a/pkg/kafka/endpoint_manager_test.go
+++ b/pkg/kafka/endpoint_manager_test.go
@@ -42,6 +42,7 @@
}
func newEPTest(minReplicas, maxReplicas int) *EPTest {
+ ctx := context.Background()
test := &EPTest{
minReplicas: minReplicas,
maxReplicas: maxReplicas,
@@ -49,17 +50,18 @@
// Create backend
if err := test.initBackend(); err != nil {
- logger.Fatalw("setting-backend-failed", log.Fields{"error": err})
+ logger.Fatalw(ctx, "setting-backend-failed", log.Fields{"error": err})
}
// Populate backend with data
if err := test.populateBackend(); err != nil {
- logger.Fatalw("populating-db-failed", log.Fields{"error": err})
+ logger.Fatalw(ctx, "populating-db-failed", log.Fields{"error": err})
}
return test
}
func (ep *EPTest) initBackend() error {
+ ctx := context.Background()
configName := "voltha-lib.kafka.ep.test"
storageDir := "voltha-lib.kafka.ep.etcd"
logLevel := "error"
@@ -73,18 +75,18 @@
if err != nil {
return err
}
- ep.etcdServer = etcd.StartEtcdServer(etcd.MKConfig(configName, kvClientPort, peerPort, storageDir, logLevel))
+ ep.etcdServer = etcd.StartEtcdServer(ctx, etcd.MKConfig(ctx, configName, kvClientPort, peerPort, storageDir, logLevel))
if ep.etcdServer == nil {
return status.Error(codes.Internal, "Embedded server failed to start")
}
- ep.backend = db.NewBackend("etcd", "127.0.0.1"+":"+strconv.Itoa(kvClientPort), timeout, "service/voltha")
+ ep.backend = db.NewBackend(ctx, "etcd", "127.0.0.1"+":"+strconv.Itoa(kvClientPort), timeout, "service/voltha")
return nil
}
func (ep *EPTest) stopAll() {
if ep.etcdServer != nil {
- ep.etcdServer.Stop()
+ ep.etcdServer.Stop(context.Background())
}
}
@@ -185,20 +187,21 @@
}
func (ep *EPTest) testEndpointManagerAPIs(t *testing.T, tm EndpointManager, serviceType string, deviceType string, replicas int) {
+ ctx := context.Background()
// Map of device ids to topic
deviceIDs := make(map[string]Endpoint)
numDevices := 1000
total := make([]int, replicas)
for i := 0; i < numDevices; i++ {
deviceID := uuid.New().String()
- endpoint, err := tm.GetEndpoint(deviceID, serviceType)
+ endpoint, err := tm.GetEndpoint(ctx, deviceID, serviceType)
if err != nil {
- logger.Fatalw("error-getting-endpoint", log.Fields{"error": err})
+ logger.Fatalw(ctx, "error-getting-endpoint", log.Fields{"error": err})
}
deviceIDs[deviceID] = endpoint
- replicaID, err := tm.GetReplicaAssignment(deviceID, serviceType)
+ replicaID, err := tm.GetReplicaAssignment(ctx, deviceID, serviceType)
if err != nil {
- logger.Fatalw("error-getting-endpoint", log.Fields{"error": err})
+ logger.Fatalw(ctx, "error-getting-endpoint", log.Fields{"error": err})
}
total[replicaID] += 1
}
@@ -210,9 +213,9 @@
numIterations := 10
for i := 0; i < numIterations; i++ {
for deviceID, expectedEndpoint := range deviceIDs {
- endpointByServiceType, err := tm.GetEndpoint(deviceID, serviceType)
+ endpointByServiceType, err := tm.GetEndpoint(ctx, deviceID, serviceType)
if err != nil {
- logger.Fatalw("error-getting-endpoint", log.Fields{"error": err})
+ logger.Fatalw(ctx, "error-getting-endpoint", log.Fields{"error": err})
}
assert.Equal(t, expectedEndpoint, endpointByServiceType)
}
@@ -220,14 +223,14 @@
// Verify that a device belong to the correct node
for deviceID := range deviceIDs {
- replicaID, err := tm.GetReplicaAssignment(deviceID, serviceType)
+ replicaID, err := tm.GetReplicaAssignment(ctx, deviceID, serviceType)
if err != nil {
- logger.Fatalw("error-getting-topic", log.Fields{"error": err})
+ logger.Fatalw(ctx, "error-getting-topic", log.Fields{"error": err})
}
for k := 0; k < replicas; k++ {
- owned, err := tm.IsDeviceOwnedByService(deviceID, serviceType, int32(k))
+ owned, err := tm.IsDeviceOwnedByService(ctx, deviceID, serviceType, int32(k))
if err != nil {
- logger.Fatalw("error-verifying-device-ownership", log.Fields{"error": err})
+ logger.Fatalw(ctx, "error-verifying-device-ownership", log.Fields{"error": err})
}
assert.Equal(t, ReplicaID(k) == replicaID, owned)
}
diff --git a/pkg/kafka/kafka_inter_container_library.go b/pkg/kafka/kafka_inter_container_library.go
index 9f9fbfc..c14c54b 100644
--- a/pkg/kafka/kafka_inter_container_library.go
+++ b/pkg/kafka/kafka_inter_container_library.go
@@ -63,18 +63,18 @@
}
type InterContainerProxy interface {
- Start() error
- Stop()
+ Start(ctx context.Context) error
+ Stop(ctx context.Context)
GetDefaultTopic() *Topic
- DeviceDiscovered(deviceId string, deviceType string, parentId string, publisher string) error
+ DeviceDiscovered(ctx context.Context, deviceId string, deviceType string, parentId string, publisher string) error
InvokeRPC(ctx context.Context, rpc string, toTopic *Topic, replyToTopic *Topic, waitForResponse bool, key string, kvArgs ...*KVArg) (bool, *any.Any)
InvokeAsyncRPC(ctx context.Context, rpc string, toTopic *Topic, replyToTopic *Topic, waitForResponse bool, key string, kvArgs ...*KVArg) chan *RpcResponse
- SubscribeWithRequestHandlerInterface(topic Topic, handler interface{}) error
- SubscribeWithDefaultRequestHandler(topic Topic, initialOffset int64) error
- UnSubscribeFromRequestHandler(topic Topic) error
- DeleteTopic(topic Topic) error
- EnableLivenessChannel(enable bool) chan bool
- SendLiveness() error
+ SubscribeWithRequestHandlerInterface(ctx context.Context, topic Topic, handler interface{}) error
+ SubscribeWithDefaultRequestHandler(ctx context.Context, topic Topic, initialOffset int64) error
+ UnSubscribeFromRequestHandler(ctx context.Context, topic Topic) error
+ DeleteTopic(ctx context.Context, topic Topic) error
+ EnableLivenessChannel(ctx context.Context, enable bool) chan bool
+ SendLiveness(ctx context.Context) error
}
// interContainerProxy represents the messaging proxy
@@ -153,17 +153,17 @@
return newInterContainerProxy(opts...)
}
-func (kp *interContainerProxy) Start() error {
- logger.Info("Starting-Proxy")
+func (kp *interContainerProxy) Start(ctx context.Context) error {
+ logger.Info(ctx, "Starting-Proxy")
// Kafka MsgClient should already have been created. If not, output fatal error
if kp.kafkaClient == nil {
- logger.Fatal("kafka-client-not-set")
+ logger.Fatal(ctx, "kafka-client-not-set")
}
// Start the kafka client
- if err := kp.kafkaClient.Start(); err != nil {
- logger.Errorw("Cannot-create-kafka-proxy", log.Fields{"error": err})
+ if err := kp.kafkaClient.Start(ctx); err != nil {
+ logger.Errorw(ctx, "Cannot-create-kafka-proxy", log.Fields{"error": err})
return err
}
@@ -179,20 +179,20 @@
return nil
}
-func (kp *interContainerProxy) Stop() {
- logger.Info("stopping-intercontainer-proxy")
+func (kp *interContainerProxy) Stop(ctx context.Context) {
+ logger.Info(ctx, "stopping-intercontainer-proxy")
kp.doneOnce.Do(func() { close(kp.doneCh) })
// TODO : Perform cleanup
- kp.kafkaClient.Stop()
- err := kp.deleteAllTopicRequestHandlerChannelMap()
+ kp.kafkaClient.Stop(ctx)
+ err := kp.deleteAllTopicRequestHandlerChannelMap(ctx)
if err != nil {
- logger.Errorw("failed-delete-all-topic-request-handler-channel-map", log.Fields{"error": err})
+ logger.Errorw(ctx, "failed-delete-all-topic-request-handler-channel-map", log.Fields{"error": err})
}
- err = kp.deleteAllTopicResponseChannelMap()
+ err = kp.deleteAllTopicResponseChannelMap(ctx)
if err != nil {
- logger.Errorw("failed-delete-all-topic-response-channel-map", log.Fields{"error": err})
+ logger.Errorw(ctx, "failed-delete-all-topic-response-channel-map", log.Fields{"error": err})
}
- kp.deleteAllTransactionIdToChannelMap()
+ kp.deleteAllTransactionIdToChannelMap(ctx)
}
func (kp *interContainerProxy) GetDefaultTopic() *Topic {
@@ -200,11 +200,11 @@
}
// DeviceDiscovered publish the discovered device onto the kafka messaging bus
-func (kp *interContainerProxy) DeviceDiscovered(deviceId string, deviceType string, parentId string, publisher string) error {
- logger.Debugw("sending-device-discovery-msg", log.Fields{"deviceId": deviceId})
+func (kp *interContainerProxy) DeviceDiscovered(ctx context.Context, deviceId string, deviceType string, parentId string, publisher string) error {
+ logger.Debugw(ctx, "sending-device-discovery-msg", log.Fields{"deviceId": deviceId})
// Simple validation
if deviceId == "" || deviceType == "" {
- logger.Errorw("invalid-parameters", log.Fields{"id": deviceId, "type": deviceType})
+ logger.Errorw(ctx, "invalid-parameters", log.Fields{"id": deviceId, "type": deviceType})
return errors.New("invalid-parameters")
}
// Create the device discovery message
@@ -225,7 +225,7 @@
var marshalledData *any.Any
var err error
if marshalledData, err = ptypes.MarshalAny(body); err != nil {
- logger.Errorw("cannot-marshal-request", log.Fields{"error": err})
+ logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"error": err})
return err
}
msg := &ic.InterContainerMessage{
@@ -234,8 +234,8 @@
}
// Send the message
- if err := kp.kafkaClient.Send(msg, kp.deviceDiscoveryTopic); err != nil {
- logger.Errorw("cannot-send-device-discovery-message", log.Fields{"error": err})
+ if err := kp.kafkaClient.Send(ctx, msg, kp.deviceDiscoveryTopic); err != nil {
+ logger.Errorw(ctx, "cannot-send-device-discovery-message", log.Fields{"error": err})
return err
}
return nil
@@ -245,7 +245,7 @@
func (kp *interContainerProxy) InvokeAsyncRPC(ctx context.Context, rpc string, toTopic *Topic, replyToTopic *Topic,
waitForResponse bool, key string, kvArgs ...*KVArg) chan *RpcResponse {
- logger.Debugw("InvokeAsyncRPC", log.Fields{"rpc": rpc, "key": key})
+ logger.Debugw(ctx, "InvokeAsyncRPC", log.Fields{"rpc": rpc, "key": key})
// If a replyToTopic is provided then we use it, otherwise just use the default toTopic. The replyToTopic is
// typically the device ID.
responseTopic := replyToTopic
@@ -265,17 +265,17 @@
var protoRequest *ic.InterContainerMessage
// Encode the request
- protoRequest, err = encodeRequest(rpc, toTopic, responseTopic, key, kvArgs...)
+ protoRequest, err = encodeRequest(ctx, rpc, toTopic, responseTopic, key, kvArgs...)
if err != nil {
- logger.Warnw("cannot-format-request", log.Fields{"rpc": rpc, "error": err})
+ logger.Warnw(ctx, "cannot-format-request", log.Fields{"rpc": rpc, "error": err})
chnl <- NewResponse(RpcFormattingError, err, nil)
return
}
// Subscribe for response, if needed, before sending request
var ch <-chan *ic.InterContainerMessage
- if ch, err = kp.subscribeForResponse(*responseTopic, protoRequest.Header.Id); err != nil {
- logger.Errorw("failed-to-subscribe-for-response", log.Fields{"error": err, "toTopic": toTopic.Name})
+ if ch, err = kp.subscribeForResponse(ctx, *responseTopic, protoRequest.Header.Id); err != nil {
+ logger.Errorw(ctx, "failed-to-subscribe-for-response", log.Fields{"error": err, "toTopic": toTopic.Name})
chnl <- NewResponse(RpcTransportError, err, nil)
return
}
@@ -283,10 +283,10 @@
// Send request - if the topic is formatted with a device Id then we will send the request using a
// specific key, hence ensuring a single partition is used to publish the request. This ensures that the
// subscriber on that topic will receive the request in the order it was sent. The key used is the deviceId.
- logger.Debugw("sending-msg", log.Fields{"rpc": rpc, "toTopic": toTopic, "replyTopic": responseTopic, "key": key, "xId": protoRequest.Header.Id})
+ logger.Debugw(ctx, "sending-msg", log.Fields{"rpc": rpc, "toTopic": toTopic, "replyTopic": responseTopic, "key": key, "xId": protoRequest.Header.Id})
// if the message is not sent on kafka publish an event an close the channel
- if err = kp.kafkaClient.Send(protoRequest, toTopic, key); err != nil {
+ if err = kp.kafkaClient.Send(ctx, protoRequest, toTopic, key); err != nil {
chnl <- NewResponse(RpcTransportError, err, nil)
return
}
@@ -299,8 +299,8 @@
defer func() {
// Remove the subscription for a response on return
- if err := kp.unSubscribeForResponse(protoRequest.Header.Id); err != nil {
- logger.Warnw("invoke-async-rpc-unsubscriber-for-response-failed", log.Fields{"err": err})
+ if err := kp.unSubscribeForResponse(ctx, protoRequest.Header.Id); err != nil {
+ logger.Warnw(ctx, "invoke-async-rpc-unsubscriber-for-response-failed", log.Fields{"err": err})
}
}()
@@ -308,11 +308,11 @@
select {
case msg, ok := <-ch:
if !ok {
- logger.Warnw("channel-closed", log.Fields{"rpc": rpc, "replyTopic": replyToTopic.Name})
+ logger.Warnw(ctx, "channel-closed", log.Fields{"rpc": rpc, "replyTopic": replyToTopic.Name})
chnl <- NewResponse(RpcTransportError, status.Error(codes.Aborted, "channel closed"), nil)
}
- logger.Debugw("received-response", log.Fields{"rpc": rpc, "msgHeader": msg.Header})
- if responseBody, err := decodeResponse(msg); err != nil {
+ logger.Debugw(ctx, "received-response", log.Fields{"rpc": rpc, "msgHeader": msg.Header})
+ if responseBody, err := decodeResponse(ctx, msg); err != nil {
chnl <- NewResponse(RpcReply, err, nil)
} else {
if responseBody.Success {
@@ -328,12 +328,12 @@
}
}
case <-ctx.Done():
- logger.Errorw("context-cancelled", log.Fields{"rpc": rpc, "ctx": ctx.Err()})
+ logger.Errorw(ctx, "context-cancelled", log.Fields{"rpc": rpc, "ctx": ctx.Err()})
err := status.Error(codes.DeadlineExceeded, ctx.Err().Error())
chnl <- NewResponse(RpcTimeout, err, nil)
case <-kp.doneCh:
chnl <- NewResponse(RpcSystemClosing, nil, nil)
- logger.Warnw("received-exit-signal", log.Fields{"toTopic": toTopic.Name, "rpc": rpc})
+ logger.Warnw(ctx, "received-exit-signal", log.Fields{"toTopic": toTopic.Name, "rpc": rpc})
}
}()
return chnl
@@ -351,9 +351,9 @@
}
// Encode the request
- protoRequest, err := encodeRequest(rpc, toTopic, responseTopic, key, kvArgs...)
+ protoRequest, err := encodeRequest(ctx, rpc, toTopic, responseTopic, key, kvArgs...)
if err != nil {
- logger.Warnw("cannot-format-request", log.Fields{"rpc": rpc, "error": err})
+ logger.Warnw(ctx, "cannot-format-request", log.Fields{"rpc": rpc, "error": err})
return false, nil
}
@@ -361,8 +361,8 @@
var ch <-chan *ic.InterContainerMessage
if waitForResponse {
var err error
- if ch, err = kp.subscribeForResponse(*responseTopic, protoRequest.Header.Id); err != nil {
- logger.Errorw("failed-to-subscribe-for-response", log.Fields{"error": err, "toTopic": toTopic.Name})
+ if ch, err = kp.subscribeForResponse(ctx, *responseTopic, protoRequest.Header.Id); err != nil {
+ logger.Errorw(ctx, "failed-to-subscribe-for-response", log.Fields{"error": err, "toTopic": toTopic.Name})
}
}
@@ -370,10 +370,10 @@
// specific key, hence ensuring a single partition is used to publish the request. This ensures that the
// subscriber on that topic will receive the request in the order it was sent. The key used is the deviceId.
//key := GetDeviceIdFromTopic(*toTopic)
- logger.Debugw("sending-msg", log.Fields{"rpc": rpc, "toTopic": toTopic, "replyTopic": responseTopic, "key": key, "xId": protoRequest.Header.Id})
+ logger.Debugw(ctx, "sending-msg", log.Fields{"rpc": rpc, "toTopic": toTopic, "replyTopic": responseTopic, "key": key, "xId": protoRequest.Header.Id})
go func() {
- if err := kp.kafkaClient.Send(protoRequest, toTopic, key); err != nil {
- logger.Errorw("send-failed", log.Fields{
+ if err := kp.kafkaClient.Send(ctx, protoRequest, toTopic, key); err != nil {
+ logger.Errorw(ctx, "send-failed", log.Fields{
"topic": toTopic,
"key": key,
"error": err})
@@ -394,8 +394,8 @@
// Wait for response as well as timeout or cancellation
// Remove the subscription for a response on return
defer func() {
- if err := kp.unSubscribeForResponse(protoRequest.Header.Id); err != nil {
- logger.Errorw("response-unsubscribe-failed", log.Fields{
+ if err := kp.unSubscribeForResponse(ctx, protoRequest.Header.Id); err != nil {
+ logger.Errorw(ctx, "response-unsubscribe-failed", log.Fields{
"id": protoRequest.Header.Id,
"error": err})
}
@@ -403,7 +403,7 @@
select {
case msg, ok := <-ch:
if !ok {
- logger.Warnw("channel-closed", log.Fields{"rpc": rpc, "replyTopic": replyToTopic.Name})
+ logger.Warnw(ctx, "channel-closed", log.Fields{"rpc": rpc, "replyTopic": replyToTopic.Name})
protoError := &ic.Error{Reason: "channel-closed"}
var marshalledArg *any.Any
if marshalledArg, err = ptypes.MarshalAny(protoError); err != nil {
@@ -411,16 +411,16 @@
}
return false, marshalledArg
}
- logger.Debugw("received-response", log.Fields{"rpc": rpc, "msgHeader": msg.Header})
+ logger.Debugw(ctx, "received-response", log.Fields{"rpc": rpc, "msgHeader": msg.Header})
var responseBody *ic.InterContainerResponseBody
var err error
- if responseBody, err = decodeResponse(msg); err != nil {
- logger.Errorw("decode-response-error", log.Fields{"error": err})
+ if responseBody, err = decodeResponse(ctx, msg); err != nil {
+ logger.Errorw(ctx, "decode-response-error", log.Fields{"error": err})
// FIXME we should return something
}
return responseBody.Success, responseBody.Result
case <-ctx.Done():
- logger.Debugw("context-cancelled", log.Fields{"rpc": rpc, "ctx": ctx.Err()})
+ logger.Debugw(ctx, "context-cancelled", log.Fields{"rpc": rpc, "ctx": ctx.Err()})
// pack the error as proto any type
protoError := &ic.Error{Reason: ctx.Err().Error(), Code: ic.ErrorCode_DEADLINE_EXCEEDED}
@@ -430,7 +430,7 @@
}
return false, marshalledArg
case <-childCtx.Done():
- logger.Debugw("context-cancelled", log.Fields{"rpc": rpc, "ctx": childCtx.Err()})
+ logger.Debugw(ctx, "context-cancelled", log.Fields{"rpc": rpc, "ctx": childCtx.Err()})
// pack the error as proto any type
protoError := &ic.Error{Reason: childCtx.Err().Error(), Code: ic.ErrorCode_DEADLINE_EXCEEDED}
@@ -440,7 +440,7 @@
}
return false, marshalledArg
case <-kp.doneCh:
- logger.Infow("received-exit-signal", log.Fields{"toTopic": toTopic.Name, "rpc": rpc})
+ logger.Infow(ctx, "received-exit-signal", log.Fields{"toTopic": toTopic.Name, "rpc": rpc})
return true, nil
}
}
@@ -449,55 +449,55 @@
// SubscribeWithRequestHandlerInterface allows a caller to assign a target object to be invoked automatically
// when a message is received on a given topic
-func (kp *interContainerProxy) SubscribeWithRequestHandlerInterface(topic Topic, handler interface{}) error {
+func (kp *interContainerProxy) SubscribeWithRequestHandlerInterface(ctx context.Context, topic Topic, handler interface{}) error {
// Subscribe to receive messages for that topic
var ch <-chan *ic.InterContainerMessage
var err error
- if ch, err = kp.kafkaClient.Subscribe(&topic); err != nil {
+ if ch, err = kp.kafkaClient.Subscribe(ctx, &topic); err != nil {
//if ch, err = kp.Subscribe(topic); err != nil {
- logger.Errorw("failed-to-subscribe", log.Fields{"error": err, "topic": topic.Name})
+ logger.Errorw(ctx, "failed-to-subscribe", log.Fields{"error": err, "topic": topic.Name})
return err
}
kp.defaultRequestHandlerInterface = handler
kp.addToTopicRequestHandlerChannelMap(topic.Name, &requestHandlerChannel{requesthandlerInterface: handler, ch: ch})
// Launch a go routine to receive and process kafka messages
- go kp.waitForMessages(ch, topic, handler)
+ go kp.waitForMessages(ctx, ch, topic, handler)
return nil
}
// SubscribeWithDefaultRequestHandler allows a caller to add a topic to an existing target object to be invoked automatically
// when a message is received on a given topic. So far there is only 1 target registered per microservice
-func (kp *interContainerProxy) SubscribeWithDefaultRequestHandler(topic Topic, initialOffset int64) error {
+func (kp *interContainerProxy) SubscribeWithDefaultRequestHandler(ctx context.Context, topic Topic, initialOffset int64) error {
// Subscribe to receive messages for that topic
var ch <-chan *ic.InterContainerMessage
var err error
- if ch, err = kp.kafkaClient.Subscribe(&topic, &KVArg{Key: Offset, Value: initialOffset}); err != nil {
- logger.Errorw("failed-to-subscribe", log.Fields{"error": err, "topic": topic.Name})
+ if ch, err = kp.kafkaClient.Subscribe(ctx, &topic, &KVArg{Key: Offset, Value: initialOffset}); err != nil {
+ logger.Errorw(ctx, "failed-to-subscribe", log.Fields{"error": err, "topic": topic.Name})
return err
}
kp.addToTopicRequestHandlerChannelMap(topic.Name, &requestHandlerChannel{requesthandlerInterface: kp.defaultRequestHandlerInterface, ch: ch})
// Launch a go routine to receive and process kafka messages
- go kp.waitForMessages(ch, topic, kp.defaultRequestHandlerInterface)
+ go kp.waitForMessages(ctx, ch, topic, kp.defaultRequestHandlerInterface)
return nil
}
-func (kp *interContainerProxy) UnSubscribeFromRequestHandler(topic Topic) error {
- return kp.deleteFromTopicRequestHandlerChannelMap(topic.Name)
+func (kp *interContainerProxy) UnSubscribeFromRequestHandler(ctx context.Context, topic Topic) error {
+ return kp.deleteFromTopicRequestHandlerChannelMap(ctx, topic.Name)
}
-func (kp *interContainerProxy) deleteFromTopicResponseChannelMap(topic string) error {
+func (kp *interContainerProxy) deleteFromTopicResponseChannelMap(ctx context.Context, topic string) error {
kp.lockTopicResponseChannelMap.Lock()
defer kp.lockTopicResponseChannelMap.Unlock()
if _, exist := kp.topicToResponseChannelMap[topic]; exist {
// Unsubscribe to this topic first - this will close the subscribed channel
var err error
- if err = kp.kafkaClient.UnSubscribe(&Topic{Name: topic}, kp.topicToResponseChannelMap[topic]); err != nil {
- logger.Errorw("unsubscribing-error", log.Fields{"topic": topic})
+ if err = kp.kafkaClient.UnSubscribe(ctx, &Topic{Name: topic}, kp.topicToResponseChannelMap[topic]); err != nil {
+ logger.Errorw(ctx, "unsubscribing-error", log.Fields{"topic": topic})
}
delete(kp.topicToResponseChannelMap, topic)
return err
@@ -507,16 +507,16 @@
}
// nolint: unused
-func (kp *interContainerProxy) deleteAllTopicResponseChannelMap() error {
- logger.Debug("delete-all-topic-response-channel")
+func (kp *interContainerProxy) deleteAllTopicResponseChannelMap(ctx context.Context) error {
+ logger.Debug(ctx, "delete-all-topic-response-channel")
kp.lockTopicResponseChannelMap.Lock()
defer kp.lockTopicResponseChannelMap.Unlock()
var unsubscribeFailTopics []string
for topic := range kp.topicToResponseChannelMap {
// Unsubscribe to this topic first - this will close the subscribed channel
- if err := kp.kafkaClient.UnSubscribe(&Topic{Name: topic}, kp.topicToResponseChannelMap[topic]); err != nil {
+ if err := kp.kafkaClient.UnSubscribe(ctx, &Topic{Name: topic}, kp.topicToResponseChannelMap[topic]); err != nil {
unsubscribeFailTopics = append(unsubscribeFailTopics, topic)
- logger.Errorw("unsubscribing-error", log.Fields{"topic": topic, "error": err})
+ logger.Errorw(ctx, "unsubscribing-error", log.Fields{"topic": topic, "error": err})
// Do not return. Continue to try to unsubscribe to other topics.
} else {
// Only delete from channel map if successfully unsubscribed.
@@ -537,12 +537,12 @@
}
}
-func (kp *interContainerProxy) deleteFromTopicRequestHandlerChannelMap(topic string) error {
+func (kp *interContainerProxy) deleteFromTopicRequestHandlerChannelMap(ctx context.Context, topic string) error {
kp.lockTopicRequestHandlerChannelMap.Lock()
defer kp.lockTopicRequestHandlerChannelMap.Unlock()
if _, exist := kp.topicToRequestHandlerChannelMap[topic]; exist {
// Close the kafka client client first by unsubscribing to this topic
- if err := kp.kafkaClient.UnSubscribe(&Topic{Name: topic}, kp.topicToRequestHandlerChannelMap[topic].ch); err != nil {
+ if err := kp.kafkaClient.UnSubscribe(ctx, &Topic{Name: topic}, kp.topicToRequestHandlerChannelMap[topic].ch); err != nil {
return err
}
delete(kp.topicToRequestHandlerChannelMap, topic)
@@ -553,16 +553,16 @@
}
// nolint: unused
-func (kp *interContainerProxy) deleteAllTopicRequestHandlerChannelMap() error {
- logger.Debug("delete-all-topic-request-channel")
+func (kp *interContainerProxy) deleteAllTopicRequestHandlerChannelMap(ctx context.Context) error {
+ logger.Debug(ctx, "delete-all-topic-request-channel")
kp.lockTopicRequestHandlerChannelMap.Lock()
defer kp.lockTopicRequestHandlerChannelMap.Unlock()
var unsubscribeFailTopics []string
for topic := range kp.topicToRequestHandlerChannelMap {
// Close the kafka client client first by unsubscribing to this topic
- if err := kp.kafkaClient.UnSubscribe(&Topic{Name: topic}, kp.topicToRequestHandlerChannelMap[topic].ch); err != nil {
+ if err := kp.kafkaClient.UnSubscribe(ctx, &Topic{Name: topic}, kp.topicToRequestHandlerChannelMap[topic].ch); err != nil {
unsubscribeFailTopics = append(unsubscribeFailTopics, topic)
- logger.Errorw("unsubscribing-error", log.Fields{"topic": topic, "error": err})
+ logger.Errorw(ctx, "unsubscribing-error", log.Fields{"topic": topic, "error": err})
// Do not return. Continue to try to unsubscribe to other topics.
} else {
// Only delete from channel map if successfully unsubscribed.
@@ -605,8 +605,8 @@
}
// nolint: unused
-func (kp *interContainerProxy) deleteAllTransactionIdToChannelMap() {
- logger.Debug("delete-all-transaction-id-channel-map")
+func (kp *interContainerProxy) deleteAllTransactionIdToChannelMap(ctx context.Context) {
+ logger.Debug(ctx, "delete-all-transaction-id-channel-map")
kp.lockTransactionIdToChannelMap.Lock()
defer kp.lockTransactionIdToChannelMap.Unlock()
for key, value := range kp.transactionIdToChannelMap {
@@ -615,27 +615,27 @@
}
}
-func (kp *interContainerProxy) DeleteTopic(topic Topic) error {
+func (kp *interContainerProxy) DeleteTopic(ctx context.Context, topic Topic) error {
// If we have any consumers on that topic we need to close them
- if err := kp.deleteFromTopicResponseChannelMap(topic.Name); err != nil {
- logger.Errorw("delete-from-topic-responsechannelmap-failed", log.Fields{"error": err})
+ if err := kp.deleteFromTopicResponseChannelMap(ctx, topic.Name); err != nil {
+ logger.Errorw(ctx, "delete-from-topic-responsechannelmap-failed", log.Fields{"error": err})
}
- if err := kp.deleteFromTopicRequestHandlerChannelMap(topic.Name); err != nil {
- logger.Errorw("delete-from-topic-requesthandlerchannelmap-failed", log.Fields{"error": err})
+ if err := kp.deleteFromTopicRequestHandlerChannelMap(ctx, topic.Name); err != nil {
+ logger.Errorw(ctx, "delete-from-topic-requesthandlerchannelmap-failed", log.Fields{"error": err})
}
kp.deleteTopicTransactionIdToChannelMap(topic.Name)
- return kp.kafkaClient.DeleteTopic(&topic)
+ return kp.kafkaClient.DeleteTopic(ctx, &topic)
}
-func encodeReturnedValue(returnedVal interface{}) (*any.Any, error) {
+func encodeReturnedValue(ctx context.Context, returnedVal interface{}) (*any.Any, error) {
// Encode the response argument - needs to be a proto message
if returnedVal == nil {
return nil, nil
}
protoValue, ok := returnedVal.(proto.Message)
if !ok {
- logger.Warnw("response-value-not-proto-message", log.Fields{"error": ok, "returnVal": returnedVal})
+ logger.Warnw(ctx, "response-value-not-proto-message", log.Fields{"error": ok, "returnVal": returnedVal})
err := errors.New("response-value-not-proto-message")
return nil, err
}
@@ -644,13 +644,13 @@
var marshalledReturnedVal *any.Any
var err error
if marshalledReturnedVal, err = ptypes.MarshalAny(protoValue); err != nil {
- logger.Warnw("cannot-marshal-returned-val", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-marshal-returned-val", log.Fields{"error": err})
return nil, err
}
return marshalledReturnedVal, nil
}
-func encodeDefaultFailedResponse(request *ic.InterContainerMessage) *ic.InterContainerMessage {
+func encodeDefaultFailedResponse(ctx context.Context, request *ic.InterContainerMessage) *ic.InterContainerMessage {
responseHeader := &ic.Header{
Id: request.Header.Id,
Type: ic.MessageType_RESPONSE,
@@ -666,7 +666,7 @@
var err error
// Error should never happen here
if marshalledResponseBody, err = ptypes.MarshalAny(responseBody); err != nil {
- logger.Warnw("cannot-marshal-failed-response-body", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-marshal-failed-response-body", log.Fields{"error": err})
}
return &ic.InterContainerMessage{
@@ -678,8 +678,8 @@
//formatRequest formats a request to send over kafka and returns an InterContainerMessage message on success
//or an error on failure
-func encodeResponse(request *ic.InterContainerMessage, success bool, returnedValues ...interface{}) (*ic.InterContainerMessage, error) {
- //logger.Debugw("encodeResponse", log.Fields{"success": success, "returnedValues": returnedValues})
+func encodeResponse(ctx context.Context, request *ic.InterContainerMessage, success bool, returnedValues ...interface{}) (*ic.InterContainerMessage, error) {
+ //logger.Debugw(ctx, "encodeResponse", log.Fields{"success": success, "returnedValues": returnedValues})
responseHeader := &ic.Header{
Id: request.Header.Id,
Type: ic.MessageType_RESPONSE,
@@ -695,8 +695,8 @@
// for now we support only 1 returned value - (excluding the error)
if len(returnedValues) > 0 {
- if marshalledReturnedVal, err = encodeReturnedValue(returnedValues[0]); err != nil {
- logger.Warnw("cannot-marshal-response-body", log.Fields{"error": err})
+ if marshalledReturnedVal, err = encodeReturnedValue(ctx, returnedValues[0]); err != nil {
+ logger.Warnw(ctx, "cannot-marshal-response-body", log.Fields{"error": err})
}
}
@@ -708,7 +708,7 @@
// Marshal the response body
var marshalledResponseBody *any.Any
if marshalledResponseBody, err = ptypes.MarshalAny(responseBody); err != nil {
- logger.Warnw("cannot-marshal-response-body", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-marshal-response-body", log.Fields{"error": err})
return nil, err
}
@@ -718,7 +718,7 @@
}, nil
}
-func CallFuncByName(myClass interface{}, funcName string, params ...interface{}) (out []reflect.Value, err error) {
+func CallFuncByName(ctx context.Context, myClass interface{}, funcName string, params ...interface{}) (out []reflect.Value, err error) {
myClassValue := reflect.ValueOf(myClass)
// Capitalize the first letter in the funcName to workaround the first capital letters required to
// invoke a function from a different package
@@ -727,15 +727,16 @@
if !m.IsValid() {
return make([]reflect.Value, 0), fmt.Errorf("method-not-found \"%s\"", funcName)
}
- in := make([]reflect.Value, len(params))
+ in := make([]reflect.Value, len(params)+1)
+ in[0] = reflect.ValueOf(ctx)
for i, param := range params {
- in[i] = reflect.ValueOf(param)
+ in[i+1] = reflect.ValueOf(param)
}
out = m.Call(in)
return
}
-func (kp *interContainerProxy) addTransactionId(transactionId string, currentArgs []*ic.Argument) []*ic.Argument {
+func (kp *interContainerProxy) addTransactionId(ctx context.Context, transactionId string, currentArgs []*ic.Argument) []*ic.Argument {
arg := &KVArg{
Key: TransactionKey,
Value: &ic.StrType{Val: transactionId},
@@ -744,7 +745,7 @@
var marshalledArg *any.Any
var err error
if marshalledArg, err = ptypes.MarshalAny(&ic.StrType{Val: transactionId}); err != nil {
- logger.Warnw("cannot-add-transactionId", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-add-transactionId", log.Fields{"error": err})
return currentArgs
}
protoArg := &ic.Argument{
@@ -754,11 +755,11 @@
return append(currentArgs, protoArg)
}
-func (kp *interContainerProxy) addFromTopic(fromTopic string, currentArgs []*ic.Argument) []*ic.Argument {
+func (kp *interContainerProxy) addFromTopic(ctx context.Context, fromTopic string, currentArgs []*ic.Argument) []*ic.Argument {
var marshalledArg *any.Any
var err error
if marshalledArg, err = ptypes.MarshalAny(&ic.StrType{Val: fromTopic}); err != nil {
- logger.Warnw("cannot-add-transactionId", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-add-transactionId", log.Fields{"error": err})
return currentArgs
}
protoArg := &ic.Argument{
@@ -768,7 +769,7 @@
return append(currentArgs, protoArg)
}
-func (kp *interContainerProxy) handleMessage(msg *ic.InterContainerMessage, targetInterface interface{}) {
+func (kp *interContainerProxy) handleMessage(ctx context.Context, msg *ic.InterContainerMessage, targetInterface interface{}) {
// First extract the header to know whether this is a request - responses are handled by a different handler
if msg.Header.Type == ic.MessageType_REQUEST {
@@ -778,21 +779,21 @@
// Get the request body
requestBody := &ic.InterContainerRequestBody{}
if err = ptypes.UnmarshalAny(msg.Body, requestBody); err != nil {
- logger.Warnw("cannot-unmarshal-request", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-request", log.Fields{"error": err})
} else {
- logger.Debugw("received-request", log.Fields{"rpc": requestBody.Rpc, "header": msg.Header})
+ logger.Debugw(ctx, "received-request", log.Fields{"rpc": requestBody.Rpc, "header": msg.Header})
// let the callee unpack the arguments as its the only one that knows the real proto type
// Augment the requestBody with the message Id as it will be used in scenarios where cores
// are set in pairs and competing
- requestBody.Args = kp.addTransactionId(msg.Header.Id, requestBody.Args)
+ requestBody.Args = kp.addTransactionId(ctx, msg.Header.Id, requestBody.Args)
// Augment the requestBody with the From topic name as it will be used in scenarios where a container
// needs to send an unsollicited message to the currently requested container
- requestBody.Args = kp.addFromTopic(msg.Header.FromTopic, requestBody.Args)
+ requestBody.Args = kp.addFromTopic(ctx, msg.Header.FromTopic, requestBody.Args)
- out, err = CallFuncByName(targetInterface, requestBody.Rpc, requestBody.Args)
+ out, err = CallFuncByName(ctx, targetInterface, requestBody.Rpc, requestBody.Args)
if err != nil {
- logger.Warn(err)
+ logger.Warn(ctx, err)
}
}
// Response required?
@@ -812,7 +813,7 @@
if out[lastIndex].Interface() != nil { // Error
if retError, ok := out[lastIndex].Interface().(error); ok {
if retError.Error() == ErrorTransactionNotAcquired.Error() {
- logger.Debugw("Ignoring request", log.Fields{"error": retError, "txId": msg.Header.Id})
+ logger.Debugw(ctx, "Ignoring request", log.Fields{"error": retError, "txId": msg.Header.Id})
return // Ignore - process is in competing mode and ignored transaction
}
returnError = &ic.Error{Reason: retError.Error()}
@@ -822,12 +823,12 @@
returnedValues = append(returnedValues, returnError)
}
} else if len(out) == 2 && reflect.ValueOf(out[0].Interface()).IsValid() && reflect.ValueOf(out[0].Interface()).IsNil() {
- logger.Warnw("Unexpected response of (nil,nil)", log.Fields{"txId": msg.Header.Id})
+ logger.Warnw(ctx, "Unexpected response of (nil,nil)", log.Fields{"txId": msg.Header.Id})
return // Ignore - should not happen
} else { // Non-error case
success = true
for idx, val := range out {
- //logger.Debugw("returned-api-response-loop", log.Fields{"idx": idx, "val": val.Interface()})
+ //logger.Debugw(ctx, "returned-api-response-loop", log.Fields{"idx": idx, "val": val.Interface()})
if idx != lastIndex {
returnedValues = append(returnedValues, val.Interface())
}
@@ -836,9 +837,9 @@
}
var icm *ic.InterContainerMessage
- if icm, err = encodeResponse(msg, success, returnedValues...); err != nil {
- logger.Warnw("error-encoding-response-returning-failure-result", log.Fields{"error": err})
- icm = encodeDefaultFailedResponse(msg)
+ if icm, err = encodeResponse(ctx, msg, success, returnedValues...); err != nil {
+ logger.Warnw(ctx, "error-encoding-response-returning-failure-result", log.Fields{"error": err})
+ icm = encodeDefaultFailedResponse(ctx, msg)
}
// To preserve ordering of messages, all messages to a given topic are sent to the same partition
// by providing a message key. The key is encoded in the topic name. If the deviceId is not
@@ -846,11 +847,11 @@
// partitions.
replyTopic := &Topic{Name: msg.Header.FromTopic}
key := msg.Header.KeyTopic
- logger.Debugw("sending-response-to-kafka", log.Fields{"rpc": requestBody.Rpc, "header": icm.Header, "key": key})
+ logger.Debugw(ctx, "sending-response-to-kafka", log.Fields{"rpc": requestBody.Rpc, "header": icm.Header, "key": key})
// TODO: handle error response.
go func() {
- if err := kp.kafkaClient.Send(icm, replyTopic, key); err != nil {
- logger.Errorw("send-reply-failed", log.Fields{
+ if err := kp.kafkaClient.Send(ctx, icm, replyTopic, key); err != nil {
+ logger.Errorw(ctx, "send-reply-failed", log.Fields{
"topic": replyTopic,
"key": key,
"error": err})
@@ -858,26 +859,26 @@
}()
}
} else if msg.Header.Type == ic.MessageType_RESPONSE {
- logger.Debugw("response-received", log.Fields{"msg-header": msg.Header})
- go kp.dispatchResponse(msg)
+ logger.Debugw(ctx, "response-received", log.Fields{"msg-header": msg.Header})
+ go kp.dispatchResponse(ctx, msg)
} else {
- logger.Warnw("unsupported-message-received", log.Fields{"msg-header": msg.Header})
+ logger.Warnw(ctx, "unsupported-message-received", log.Fields{"msg-header": msg.Header})
}
}
-func (kp *interContainerProxy) waitForMessages(ch <-chan *ic.InterContainerMessage, topic Topic, targetInterface interface{}) {
+func (kp *interContainerProxy) waitForMessages(ctx context.Context, ch <-chan *ic.InterContainerMessage, topic Topic, targetInterface interface{}) {
// Wait for messages
for msg := range ch {
- //logger.Debugw("request-received", log.Fields{"msg": msg, "topic": topic.Name, "target": targetInterface})
- go kp.handleMessage(msg, targetInterface)
+ //logger.Debugw(ctx, "request-received", log.Fields{"msg": msg, "topic": topic.Name, "target": targetInterface})
+ go kp.handleMessage(ctx, msg, targetInterface)
}
}
-func (kp *interContainerProxy) dispatchResponse(msg *ic.InterContainerMessage) {
+func (kp *interContainerProxy) dispatchResponse(ctx context.Context, msg *ic.InterContainerMessage) {
kp.lockTransactionIdToChannelMap.RLock()
defer kp.lockTransactionIdToChannelMap.RUnlock()
if _, exist := kp.transactionIdToChannelMap[msg.Header.Id]; !exist {
- logger.Debugw("no-waiting-channel", log.Fields{"transaction": msg.Header.Id})
+ logger.Debugw(ctx, "no-waiting-channel", log.Fields{"transaction": msg.Header.Id})
return
}
kp.transactionIdToChannelMap[msg.Header.Id].ch <- msg
@@ -887,8 +888,8 @@
// This method is built to prevent all subscribers to receive all messages as is the case of the Subscribe
// API. There is one response channel waiting for kafka messages before dispatching the message to the
// corresponding waiting channel
-func (kp *interContainerProxy) subscribeForResponse(topic Topic, trnsId string) (chan *ic.InterContainerMessage, error) {
- logger.Debugw("subscribeForResponse", log.Fields{"topic": topic.Name, "trnsid": trnsId})
+func (kp *interContainerProxy) subscribeForResponse(ctx context.Context, topic Topic, trnsId string) (chan *ic.InterContainerMessage, error) {
+ logger.Debugw(ctx, "subscribeForResponse", log.Fields{"topic": topic.Name, "trnsid": trnsId})
// Create a specific channel for this consumers. We cannot use the channel from the kafkaclient as it will
// broadcast any message for this topic to all channels waiting on it.
@@ -899,27 +900,27 @@
return ch, nil
}
-func (kp *interContainerProxy) unSubscribeForResponse(trnsId string) error {
- logger.Debugw("unsubscribe-for-response", log.Fields{"trnsId": trnsId})
+func (kp *interContainerProxy) unSubscribeForResponse(ctx context.Context, trnsId string) error {
+ logger.Debugw(ctx, "unsubscribe-for-response", log.Fields{"trnsId": trnsId})
kp.deleteFromTransactionIdToChannelMap(trnsId)
return nil
}
-func (kp *interContainerProxy) EnableLivenessChannel(enable bool) chan bool {
- return kp.kafkaClient.EnableLivenessChannel(enable)
+func (kp *interContainerProxy) EnableLivenessChannel(ctx context.Context, enable bool) chan bool {
+ return kp.kafkaClient.EnableLivenessChannel(ctx, enable)
}
-func (kp *interContainerProxy) EnableHealthinessChannel(enable bool) chan bool {
- return kp.kafkaClient.EnableHealthinessChannel(enable)
+func (kp *interContainerProxy) EnableHealthinessChannel(ctx context.Context, enable bool) chan bool {
+ return kp.kafkaClient.EnableHealthinessChannel(ctx, enable)
}
-func (kp *interContainerProxy) SendLiveness() error {
- return kp.kafkaClient.SendLiveness()
+func (kp *interContainerProxy) SendLiveness(ctx context.Context) error {
+ return kp.kafkaClient.SendLiveness(ctx)
}
//formatRequest formats a request to send over kafka and returns an InterContainerMessage message on success
//or an error on failure
-func encodeRequest(rpc string, toTopic *Topic, replyTopic *Topic, key string, kvArgs ...*KVArg) (*ic.InterContainerMessage, error) {
+func encodeRequest(ctx context.Context, rpc string, toTopic *Topic, replyTopic *Topic, key string, kvArgs ...*KVArg) (*ic.InterContainerMessage, error) {
requestHeader := &ic.Header{
Id: uuid.New().String(),
Type: ic.MessageType_REQUEST,
@@ -944,12 +945,12 @@
// ascertain the value interface type is a proto.Message
protoValue, ok := arg.Value.(proto.Message)
if !ok {
- logger.Warnw("argument-value-not-proto-message", log.Fields{"error": ok, "Value": arg.Value})
+ logger.Warnw(ctx, "argument-value-not-proto-message", log.Fields{"error": ok, "Value": arg.Value})
err := errors.New("argument-value-not-proto-message")
return nil, err
}
if marshalledArg, err = ptypes.MarshalAny(protoValue); err != nil {
- logger.Warnw("cannot-marshal-request", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-marshal-request", log.Fields{"error": err})
return nil, err
}
protoArg := &ic.Argument{
@@ -962,7 +963,7 @@
var marshalledData *any.Any
var err error
if marshalledData, err = ptypes.MarshalAny(requestBody); err != nil {
- logger.Warnw("cannot-marshal-request", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-marshal-request", log.Fields{"error": err})
return nil, err
}
request := &ic.InterContainerMessage{
@@ -972,14 +973,14 @@
return request, nil
}
-func decodeResponse(response *ic.InterContainerMessage) (*ic.InterContainerResponseBody, error) {
+func decodeResponse(ctx context.Context, response *ic.InterContainerMessage) (*ic.InterContainerResponseBody, error) {
// Extract the message body
responseBody := ic.InterContainerResponseBody{}
if err := ptypes.UnmarshalAny(response.Body, &responseBody); err != nil {
- logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err})
+ logger.Warnw(ctx, "cannot-unmarshal-response", log.Fields{"error": err})
return nil, err
}
- //logger.Debugw("response-decoded-successfully", log.Fields{"response-status": &responseBody.Success})
+ //logger.Debugw(ctx, "response-decoded-successfully", log.Fields{"response-status": &responseBody.Success})
return &responseBody, nil
diff --git a/pkg/kafka/kafka_inter_container_library_test.go b/pkg/kafka/kafka_inter_container_library_test.go
index 09286ad..8c88750 100644
--- a/pkg/kafka/kafka_inter_container_library_test.go
+++ b/pkg/kafka/kafka_inter_container_library_test.go
@@ -16,6 +16,7 @@
package kafka
import (
+ "context"
"github.com/stretchr/testify/assert"
"testing"
)
@@ -73,7 +74,7 @@
MsgClient(client),
)
- ch := probe.EnableLivenessChannel(true)
+ ch := probe.EnableLivenessChannel(context.Background(), true)
// The channel should have one "true" message on it
assert.NotEmpty(t, ch)
diff --git a/pkg/kafka/sarama_client.go b/pkg/kafka/sarama_client.go
index 581cf49..87c7ce4 100755
--- a/pkg/kafka/sarama_client.go
+++ b/pkg/kafka/sarama_client.go
@@ -231,8 +231,8 @@
return client
}
-func (sc *SaramaClient) Start() error {
- logger.Info("Starting-kafka-sarama-client")
+func (sc *SaramaClient) Start(ctx context.Context) error {
+ logger.Info(ctx, "Starting-kafka-sarama-client")
// Create the Done channel
sc.doneCh = make(chan int, 1)
@@ -242,26 +242,26 @@
// Add a cleanup in case of failure to startup
defer func() {
if err != nil {
- sc.Stop()
+ sc.Stop(ctx)
}
}()
// Create the Cluster Admin
- if err = sc.createClusterAdmin(); err != nil {
- logger.Errorw("Cannot-create-cluster-admin", log.Fields{"error": err})
+ if err = sc.createClusterAdmin(ctx); err != nil {
+ logger.Errorw(ctx, "Cannot-create-cluster-admin", log.Fields{"error": err})
return err
}
// Create the Publisher
- if err := sc.createPublisher(); err != nil {
- logger.Errorw("Cannot-create-kafka-publisher", log.Fields{"error": err})
+ if err := sc.createPublisher(ctx); err != nil {
+ logger.Errorw(ctx, "Cannot-create-kafka-publisher", log.Fields{"error": err})
return err
}
if sc.consumerType == DefaultConsumerType {
// Create the master consumers
- if err := sc.createConsumer(); err != nil {
- logger.Errorw("Cannot-create-kafka-consumers", log.Fields{"error": err})
+ if err := sc.createConsumer(ctx); err != nil {
+ logger.Errorw(ctx, "Cannot-create-kafka-consumers", log.Fields{"error": err})
return err
}
}
@@ -269,15 +269,15 @@
// Create the topic to consumers/channel map
sc.topicToConsumerChannelMap = make(map[string]*consumerChannels)
- logger.Info("kafka-sarama-client-started")
+ logger.Info(ctx, "kafka-sarama-client-started")
sc.started = true
return nil
}
-func (sc *SaramaClient) Stop() {
- logger.Info("stopping-sarama-client")
+func (sc *SaramaClient) Stop(ctx context.Context) {
+ logger.Info(ctx, "stopping-sarama-client")
sc.started = false
@@ -286,38 +286,38 @@
if sc.producer != nil {
if err := sc.producer.Close(); err != nil {
- logger.Errorw("closing-producer-failed", log.Fields{"error": err})
+ logger.Errorw(ctx, "closing-producer-failed", log.Fields{"error": err})
}
}
if sc.consumer != nil {
if err := sc.consumer.Close(); err != nil {
- logger.Errorw("closing-partition-consumer-failed", log.Fields{"error": err})
+ logger.Errorw(ctx, "closing-partition-consumer-failed", log.Fields{"error": err})
}
}
for key, val := range sc.groupConsumers {
- logger.Debugw("closing-group-consumer", log.Fields{"topic": key})
+ logger.Debugw(ctx, "closing-group-consumer", log.Fields{"topic": key})
if err := val.Close(); err != nil {
- logger.Errorw("closing-group-consumer-failed", log.Fields{"error": err, "topic": key})
+ logger.Errorw(ctx, "closing-group-consumer-failed", log.Fields{"error": err, "topic": key})
}
}
if sc.cAdmin != nil {
if err := sc.cAdmin.Close(); err != nil {
- logger.Errorw("closing-cluster-admin-failed", log.Fields{"error": err})
+ logger.Errorw(ctx, "closing-cluster-admin-failed", log.Fields{"error": err})
}
}
//TODO: Clear the consumers map
//sc.clearConsumerChannelMap()
- logger.Info("sarama-client-stopped")
+ logger.Info(ctx, "sarama-client-stopped")
}
//createTopic is an internal function to create a topic on the Kafka Broker. No locking is required as
// the invoking function must hold the lock
-func (sc *SaramaClient) createTopic(topic *Topic, numPartition int, repFactor int) error {
+func (sc *SaramaClient) createTopic(ctx context.Context, topic *Topic, numPartition int, repFactor int) error {
// Set the topic details
topicDetail := &sarama.TopicDetail{}
topicDetail.NumPartitions = int32(numPartition)
@@ -329,29 +329,29 @@
if err := sc.cAdmin.CreateTopic(topic.Name, topicDetail, false); err != nil {
if err == sarama.ErrTopicAlreadyExists {
// Not an error
- logger.Debugw("topic-already-exist", log.Fields{"topic": topic.Name})
+ logger.Debugw(ctx, "topic-already-exist", log.Fields{"topic": topic.Name})
return nil
}
- logger.Errorw("create-topic-failure", log.Fields{"error": err})
+ logger.Errorw(ctx, "create-topic-failure", log.Fields{"error": err})
return err
}
// TODO: Wait until the topic has been created. No API is available in the Sarama clusterAdmin to
// do so.
- logger.Debugw("topic-created", log.Fields{"topic": topic, "numPartition": numPartition, "replicationFactor": repFactor})
+ logger.Debugw(ctx, "topic-created", log.Fields{"topic": topic, "numPartition": numPartition, "replicationFactor": repFactor})
return nil
}
//CreateTopic is a public API to create a topic on the Kafka Broker. It uses a lock on a specific topic to
// ensure no two go routines are performing operations on the same topic
-func (sc *SaramaClient) CreateTopic(topic *Topic, numPartition int, repFactor int) error {
+func (sc *SaramaClient) CreateTopic(ctx context.Context, topic *Topic, numPartition int, repFactor int) error {
sc.lockTopic(topic)
defer sc.unLockTopic(topic)
- return sc.createTopic(topic, numPartition, repFactor)
+ return sc.createTopic(ctx, topic, numPartition, repFactor)
}
//DeleteTopic removes a topic from the kafka Broker
-func (sc *SaramaClient) DeleteTopic(topic *Topic) error {
+func (sc *SaramaClient) DeleteTopic(ctx context.Context, topic *Topic) error {
sc.lockTopic(topic)
defer sc.unLockTopic(topic)
@@ -359,16 +359,16 @@
if err := sc.cAdmin.DeleteTopic(topic.Name); err != nil {
if err == sarama.ErrUnknownTopicOrPartition {
// Not an error as does not exist
- logger.Debugw("topic-not-exist", log.Fields{"topic": topic.Name})
+ logger.Debugw(ctx, "topic-not-exist", log.Fields{"topic": topic.Name})
return nil
}
- logger.Errorw("delete-topic-failed", log.Fields{"topic": topic, "error": err})
+ logger.Errorw(ctx, "delete-topic-failed", log.Fields{"topic": topic, "error": err})
return err
}
// Clear the topic from the consumer channel. This will also close any consumers listening on that topic.
- if err := sc.clearTopicFromConsumerChannelMap(*topic); err != nil {
- logger.Errorw("failure-clearing-channels", log.Fields{"topic": topic, "error": err})
+ if err := sc.clearTopicFromConsumerChannelMap(ctx, *topic); err != nil {
+ logger.Errorw(ctx, "failure-clearing-channels", log.Fields{"topic": topic, "error": err})
return err
}
return nil
@@ -376,18 +376,18 @@
// Subscribe registers a caller to a topic. It returns a channel that the caller can use to receive
// messages from that topic
-func (sc *SaramaClient) Subscribe(topic *Topic, kvArgs ...*KVArg) (<-chan *ic.InterContainerMessage, error) {
+func (sc *SaramaClient) Subscribe(ctx context.Context, topic *Topic, kvArgs ...*KVArg) (<-chan *ic.InterContainerMessage, error) {
sc.lockTopic(topic)
defer sc.unLockTopic(topic)
- logger.Debugw("subscribe", log.Fields{"topic": topic.Name})
+ logger.Debugw(ctx, "subscribe", log.Fields{"topic": topic.Name})
// If a consumers already exist for that topic then resuse it
if consumerCh := sc.getConsumerChannel(topic); consumerCh != nil {
- logger.Debugw("topic-already-subscribed", log.Fields{"topic": topic.Name})
+ logger.Debugw(ctx, "topic-already-subscribed", log.Fields{"topic": topic.Name})
// Create a channel specific for that consumers and add it to the consumers channel map
ch := make(chan *ic.InterContainerMessage)
- sc.addChannelToConsumerChannelMap(topic, ch)
+ sc.addChannelToConsumerChannelMap(ctx, topic, ch)
return ch, nil
}
@@ -398,13 +398,13 @@
// Use the consumerType option to figure out the type of consumer to launch
if sc.consumerType == PartitionConsumer {
if sc.autoCreateTopic {
- if err = sc.createTopic(topic, sc.numPartitions, sc.numReplicas); err != nil {
- logger.Errorw("create-topic-failure", log.Fields{"error": err, "topic": topic.Name})
+ if err = sc.createTopic(ctx, topic, sc.numPartitions, sc.numReplicas); err != nil {
+ logger.Errorw(ctx, "create-topic-failure", log.Fields{"error": err, "topic": topic.Name})
return nil, err
}
}
- if consumerListeningChannel, err = sc.setupPartitionConsumerChannel(topic, getOffset(kvArgs...)); err != nil {
- logger.Warnw("create-consumers-channel-failure", log.Fields{"error": err, "topic": topic.Name})
+ if consumerListeningChannel, err = sc.setupPartitionConsumerChannel(ctx, topic, getOffset(kvArgs...)); err != nil {
+ logger.Warnw(ctx, "create-consumers-channel-failure", log.Fields{"error": err, "topic": topic.Name})
return nil, err
}
} else if sc.consumerType == GroupCustomer {
@@ -412,7 +412,7 @@
// does not consume from a precreated topic in some scenarios
//if sc.autoCreateTopic {
// if err = sc.createTopic(topic, sc.numPartitions, sc.numReplicas); err != nil {
- // logger.Errorw("create-topic-failure", logger.Fields{"error": err, "topic": topic.Name})
+ // logger.Errorw(ctx, "create-topic-failure", logger.Fields{"error": err, "topic": topic.Name})
// return nil, err
// }
//}
@@ -425,13 +425,13 @@
// Need to use a unique group Id per topic
groupId = sc.consumerGroupPrefix + topic.Name
}
- if consumerListeningChannel, err = sc.setupGroupConsumerChannel(topic, groupId, getOffset(kvArgs...)); err != nil {
- logger.Warnw("create-consumers-channel-failure", log.Fields{"error": err, "topic": topic.Name, "groupId": groupId})
+ if consumerListeningChannel, err = sc.setupGroupConsumerChannel(ctx, topic, groupId, getOffset(kvArgs...)); err != nil {
+ logger.Warnw(ctx, "create-consumers-channel-failure", log.Fields{"error": err, "topic": topic.Name, "groupId": groupId})
return nil, err
}
} else {
- logger.Warnw("unknown-consumer-type", log.Fields{"consumer-type": sc.consumerType})
+ logger.Warnw(ctx, "unknown-consumer-type", log.Fields{"consumer-type": sc.consumerType})
return nil, errors.New("unknown-consumer-type")
}
@@ -439,37 +439,37 @@
}
//UnSubscribe unsubscribe a consumer from a given topic
-func (sc *SaramaClient) UnSubscribe(topic *Topic, ch <-chan *ic.InterContainerMessage) error {
+func (sc *SaramaClient) UnSubscribe(ctx context.Context, topic *Topic, ch <-chan *ic.InterContainerMessage) error {
sc.lockTopic(topic)
defer sc.unLockTopic(topic)
- logger.Debugw("unsubscribing-channel-from-topic", log.Fields{"topic": topic.Name})
+ logger.Debugw(ctx, "unsubscribing-channel-from-topic", log.Fields{"topic": topic.Name})
var err error
- if err = sc.removeChannelFromConsumerChannelMap(*topic, ch); err != nil {
- logger.Errorw("failed-removing-channel", log.Fields{"error": err})
+ if err = sc.removeChannelFromConsumerChannelMap(ctx, *topic, ch); err != nil {
+ logger.Errorw(ctx, "failed-removing-channel", log.Fields{"error": err})
}
- if err = sc.deleteFromGroupConsumers(topic.Name); err != nil {
- logger.Errorw("failed-deleting-group-consumer", log.Fields{"error": err})
+ if err = sc.deleteFromGroupConsumers(ctx, topic.Name); err != nil {
+ logger.Errorw(ctx, "failed-deleting-group-consumer", log.Fields{"error": err})
}
return err
}
-func (sc *SaramaClient) SubscribeForMetadata(callback func(fromTopic string, timestamp time.Time)) {
+func (sc *SaramaClient) SubscribeForMetadata(ctx context.Context, callback func(fromTopic string, timestamp time.Time)) {
sc.metadataCallback = callback
}
-func (sc *SaramaClient) updateLiveness(alive bool) {
+func (sc *SaramaClient) updateLiveness(ctx context.Context, alive bool) {
// Post a consistent stream of liveness data to the channel,
// so that in a live state, the core does not timeout and
// send a forced liveness message. Production of liveness
// events to the channel is rate-limited by livenessChannelInterval.
if sc.liveness != nil {
if sc.alive != alive {
- logger.Info("update-liveness-channel-because-change")
+ logger.Info(ctx, "update-liveness-channel-because-change")
sc.liveness <- alive
sc.lastLivenessTime = time.Now()
} else if time.Since(sc.lastLivenessTime) > sc.livenessChannelInterval {
- logger.Info("update-liveness-channel-because-interval")
+ logger.Info(ctx, "update-liveness-channel-because-interval")
sc.liveness <- alive
sc.lastLivenessTime = time.Now()
}
@@ -477,21 +477,21 @@
// Only emit a log message when the state changes
if sc.alive != alive {
- logger.Info("set-client-alive", log.Fields{"alive": alive})
+ logger.Info(ctx, "set-client-alive", log.Fields{"alive": alive})
sc.alive = alive
}
}
// Once unhealthy, we never go back
-func (sc *SaramaClient) setUnhealthy() {
+func (sc *SaramaClient) setUnhealthy(ctx context.Context) {
sc.healthy = false
if sc.healthiness != nil {
- logger.Infow("set-client-unhealthy", log.Fields{"healthy": sc.healthy})
+ logger.Infow(ctx, "set-client-unhealthy", log.Fields{"healthy": sc.healthy})
sc.healthiness <- sc.healthy
}
}
-func (sc *SaramaClient) isLivenessError(err error) bool {
+func (sc *SaramaClient) isLivenessError(ctx context.Context, err error) bool {
// Sarama producers and consumers encapsulate the error inside
// a ProducerError or ConsumerError struct.
if prodError, ok := err.(*sarama.ProducerError); ok {
@@ -506,48 +506,48 @@
switch err.Error() {
case context.DeadlineExceeded.Error():
- logger.Info("is-liveness-error-timeout")
+ logger.Info(ctx, "is-liveness-error-timeout")
return true
case sarama.ErrOutOfBrokers.Error(): // "Kafka: client has run out of available brokers"
- logger.Info("is-liveness-error-no-brokers")
+ logger.Info(ctx, "is-liveness-error-no-brokers")
return true
case sarama.ErrShuttingDown.Error(): // "Kafka: message received by producer in process of shutting down"
- logger.Info("is-liveness-error-shutting-down")
+ logger.Info(ctx, "is-liveness-error-shutting-down")
return true
case sarama.ErrControllerNotAvailable.Error(): // "Kafka: controller is not available"
- logger.Info("is-liveness-error-not-available")
+ logger.Info(ctx, "is-liveness-error-not-available")
return true
case breaker.ErrBreakerOpen.Error(): // "circuit breaker is open"
- logger.Info("is-liveness-error-circuit-breaker-open")
+ logger.Info(ctx, "is-liveness-error-circuit-breaker-open")
return true
}
if strings.HasSuffix(err.Error(), "connection refused") { // "dial tcp 10.244.1.176:9092: connect: connection refused"
- logger.Info("is-liveness-error-connection-refused")
+ logger.Info(ctx, "is-liveness-error-connection-refused")
return true
}
if strings.HasSuffix(err.Error(), "i/o timeout") { // "dial tcp 10.244.1.176:9092: i/o timeout"
- logger.Info("is-liveness-error-io-timeout")
+ logger.Info(ctx, "is-liveness-error-io-timeout")
return true
}
// Other errors shouldn't trigger a loss of liveness
- logger.Infow("is-liveness-error-ignored", log.Fields{"err": err})
+ logger.Infow(ctx, "is-liveness-error-ignored", log.Fields{"err": err})
return false
}
// send formats and sends the request onto the kafka messaging bus.
-func (sc *SaramaClient) Send(msg interface{}, topic *Topic, keys ...string) error {
+func (sc *SaramaClient) Send(ctx context.Context, msg interface{}, topic *Topic, keys ...string) error {
// Assert message is a proto message
var protoMsg proto.Message
var ok bool
// ascertain the value interface type is a proto.Message
if protoMsg, ok = msg.(proto.Message); !ok {
- logger.Warnw("message-not-proto-message", log.Fields{"msg": msg})
+ logger.Warnw(ctx, "message-not-proto-message", log.Fields{"msg": msg})
return fmt.Errorf("not-a-proto-msg-%s", msg)
}
@@ -555,7 +555,7 @@
var err error
// Create the Sarama producer message
if marshalled, err = proto.Marshal(protoMsg); err != nil {
- logger.Errorw("marshalling-failed", log.Fields{"msg": protoMsg, "error": err})
+ logger.Errorw(ctx, "marshalling-failed", log.Fields{"msg": protoMsg, "error": err})
return err
}
key := ""
@@ -574,12 +574,12 @@
// TODO: Use a lock or a different mechanism to ensure the response received corresponds to the message sent.
select {
case ok := <-sc.producer.Successes():
- logger.Debugw("message-sent", log.Fields{"status": ok.Topic})
- sc.updateLiveness(true)
+ logger.Debugw(ctx, "message-sent", log.Fields{"status": ok.Topic})
+ sc.updateLiveness(ctx, true)
case notOk := <-sc.producer.Errors():
- logger.Debugw("error-sending", log.Fields{"status": notOk})
- if sc.isLivenessError(notOk) {
- sc.updateLiveness(false)
+ logger.Debugw(ctx, "error-sending", log.Fields{"status": notOk})
+ if sc.isLivenessError(ctx, notOk) {
+ sc.updateLiveness(ctx, false)
}
return notOk
}
@@ -591,11 +591,11 @@
// or not the channel is still live. This channel is then picked up
// by the service (i.e. rw_core / ro_core) to update readiness status
// and/or take other actions.
-func (sc *SaramaClient) EnableLivenessChannel(enable bool) chan bool {
- logger.Infow("kafka-enable-liveness-channel", log.Fields{"enable": enable})
+func (sc *SaramaClient) EnableLivenessChannel(ctx context.Context, enable bool) chan bool {
+ logger.Infow(ctx, "kafka-enable-liveness-channel", log.Fields{"enable": enable})
if enable {
if sc.liveness == nil {
- logger.Info("kafka-create-liveness-channel")
+ logger.Info(ctx, "kafka-create-liveness-channel")
// At least 1, so we can immediately post to it without blocking
// Setting a bigger number (10) allows the monitor to fall behind
// without blocking others. The monitor shouldn't really fall
@@ -615,11 +615,11 @@
// Enable the Healthiness monitor channel. This channel will report "false"
// if the kafka consumers die, or some other problem occurs which is
// catastrophic that would require re-creating the client.
-func (sc *SaramaClient) EnableHealthinessChannel(enable bool) chan bool {
- logger.Infow("kafka-enable-healthiness-channel", log.Fields{"enable": enable})
+func (sc *SaramaClient) EnableHealthinessChannel(ctx context.Context, enable bool) chan bool {
+ logger.Infow(ctx, "kafka-enable-healthiness-channel", log.Fields{"enable": enable})
if enable {
if sc.healthiness == nil {
- logger.Info("kafka-create-healthiness-channel")
+ logger.Info(ctx, "kafka-create-healthiness-channel")
// At least 1, so we can immediately post to it without blocking
// Setting a bigger number (10) allows the monitor to fall behind
// without blocking others. The monitor shouldn't really fall
@@ -638,7 +638,7 @@
// send an empty message on the liveness channel to check whether connectivity has
// been restored.
-func (sc *SaramaClient) SendLiveness() error {
+func (sc *SaramaClient) SendLiveness(ctx context.Context) error {
if !sc.started {
return fmt.Errorf("SendLiveness() called while not started")
}
@@ -654,12 +654,12 @@
// TODO: Use a lock or a different mechanism to ensure the response received corresponds to the message sent.
select {
case ok := <-sc.producer.Successes():
- logger.Debugw("liveness-message-sent", log.Fields{"status": ok.Topic})
- sc.updateLiveness(true)
+ logger.Debugw(ctx, "liveness-message-sent", log.Fields{"status": ok.Topic})
+ sc.updateLiveness(ctx, true)
case notOk := <-sc.producer.Errors():
- logger.Debugw("liveness-error-sending", log.Fields{"status": notOk})
- if sc.isLivenessError(notOk) {
- sc.updateLiveness(false)
+ logger.Debugw(ctx, "liveness-error-sending", log.Fields{"status": notOk})
+ if sc.isLivenessError(ctx, notOk) {
+ sc.updateLiveness(ctx, false)
}
return notOk
}
@@ -686,7 +686,7 @@
return sarama.OffsetNewest
}
-func (sc *SaramaClient) createClusterAdmin() error {
+func (sc *SaramaClient) createClusterAdmin(ctx context.Context) error {
config := sarama.NewConfig()
config.Version = sarama.V1_0_0_0
@@ -694,7 +694,7 @@
var cAdmin sarama.ClusterAdmin
var err error
if cAdmin, err = sarama.NewClusterAdmin([]string{sc.KafkaAddress}, config); err != nil {
- logger.Errorw("cluster-admin-failure", log.Fields{"error": err, "broker-address": sc.KafkaAddress})
+ logger.Errorw(ctx, "cluster-admin-failure", log.Fields{"error": err, "broker-address": sc.KafkaAddress})
return err
}
sc.cAdmin = cAdmin
@@ -739,24 +739,24 @@
return nil
}
-func (sc *SaramaClient) addChannelToConsumerChannelMap(topic *Topic, ch chan *ic.InterContainerMessage) {
+func (sc *SaramaClient) addChannelToConsumerChannelMap(ctx context.Context, topic *Topic, ch chan *ic.InterContainerMessage) {
sc.lockTopicToConsumerChannelMap.Lock()
defer sc.lockTopicToConsumerChannelMap.Unlock()
if consumerCh, exist := sc.topicToConsumerChannelMap[topic.Name]; exist {
consumerCh.channels = append(consumerCh.channels, ch)
return
}
- logger.Warnw("consumers-channel-not-exist", log.Fields{"topic": topic.Name})
+ logger.Warnw(ctx, "consumers-channel-not-exist", log.Fields{"topic": topic.Name})
}
//closeConsumers closes a list of sarama consumers. The consumers can either be a partition consumers or a group consumers
-func closeConsumers(consumers []interface{}) error {
+func closeConsumers(ctx context.Context, consumers []interface{}) error {
var err error
for _, consumer := range consumers {
// Is it a partition consumers?
if partionConsumer, ok := consumer.(sarama.PartitionConsumer); ok {
if errTemp := partionConsumer.Close(); errTemp != nil {
- logger.Debugw("partition!!!", log.Fields{"err": errTemp})
+ logger.Debugw(ctx, "partition!!!", log.Fields{"err": errTemp})
if strings.Compare(errTemp.Error(), sarama.ErrUnknownTopicOrPartition.Error()) == 0 {
// This can occur on race condition
err = nil
@@ -778,35 +778,35 @@
return err
}
-func (sc *SaramaClient) removeChannelFromConsumerChannelMap(topic Topic, ch <-chan *ic.InterContainerMessage) error {
+func (sc *SaramaClient) removeChannelFromConsumerChannelMap(ctx context.Context, topic Topic, ch <-chan *ic.InterContainerMessage) error {
sc.lockTopicToConsumerChannelMap.Lock()
defer sc.lockTopicToConsumerChannelMap.Unlock()
if consumerCh, exist := sc.topicToConsumerChannelMap[topic.Name]; exist {
// Channel will be closed in the removeChannel method
- consumerCh.channels = removeChannel(consumerCh.channels, ch)
+ consumerCh.channels = removeChannel(ctx, consumerCh.channels, ch)
// If there are no more channels then we can close the consumers itself
if len(consumerCh.channels) == 0 {
- logger.Debugw("closing-consumers", log.Fields{"topic": topic})
- err := closeConsumers(consumerCh.consumers)
+ logger.Debugw(ctx, "closing-consumers", log.Fields{"topic": topic})
+ err := closeConsumers(ctx, consumerCh.consumers)
//err := consumerCh.consumers.Close()
delete(sc.topicToConsumerChannelMap, topic.Name)
return err
}
return nil
}
- logger.Warnw("topic-does-not-exist", log.Fields{"topic": topic.Name})
+ logger.Warnw(ctx, "topic-does-not-exist", log.Fields{"topic": topic.Name})
return errors.New("topic-does-not-exist")
}
-func (sc *SaramaClient) clearTopicFromConsumerChannelMap(topic Topic) error {
+func (sc *SaramaClient) clearTopicFromConsumerChannelMap(ctx context.Context, topic Topic) error {
sc.lockTopicToConsumerChannelMap.Lock()
defer sc.lockTopicToConsumerChannelMap.Unlock()
if consumerCh, exist := sc.topicToConsumerChannelMap[topic.Name]; exist {
for _, ch := range consumerCh.channels {
// Channel will be closed in the removeChannel method
- removeChannel(consumerCh.channels, ch)
+ removeChannel(ctx, consumerCh.channels, ch)
}
- err := closeConsumers(consumerCh.consumers)
+ err := closeConsumers(ctx, consumerCh.consumers)
//if err == sarama.ErrUnknownTopicOrPartition {
// // Not an error
// err = nil
@@ -815,12 +815,12 @@
delete(sc.topicToConsumerChannelMap, topic.Name)
return err
}
- logger.Debugw("topic-does-not-exist", log.Fields{"topic": topic.Name})
+ logger.Debugw(ctx, "topic-does-not-exist", log.Fields{"topic": topic.Name})
return nil
}
//createPublisher creates the publisher which is used to send a message onto kafka
-func (sc *SaramaClient) createPublisher() error {
+func (sc *SaramaClient) createPublisher(ctx context.Context) error {
// This Creates the publisher
config := sarama.NewConfig()
config.Producer.Partitioner = sarama.NewRandomPartitioner
@@ -835,16 +835,16 @@
brokers := []string{sc.KafkaAddress}
if producer, err := sarama.NewAsyncProducer(brokers, config); err != nil {
- logger.Errorw("error-starting-publisher", log.Fields{"error": err})
+ logger.Errorw(ctx, "error-starting-publisher", log.Fields{"error": err})
return err
} else {
sc.producer = producer
}
- logger.Info("Kafka-publisher-created")
+ logger.Info(ctx, "Kafka-publisher-created")
return nil
}
-func (sc *SaramaClient) createConsumer() error {
+func (sc *SaramaClient) createConsumer(ctx context.Context) error {
config := sarama.NewConfig()
config.Consumer.Return.Errors = true
config.Consumer.Fetch.Min = 1
@@ -855,17 +855,17 @@
brokers := []string{sc.KafkaAddress}
if consumer, err := sarama.NewConsumer(brokers, config); err != nil {
- logger.Errorw("error-starting-consumers", log.Fields{"error": err})
+ logger.Errorw(ctx, "error-starting-consumers", log.Fields{"error": err})
return err
} else {
sc.consumer = consumer
}
- logger.Info("Kafka-consumers-created")
+ logger.Info(ctx, "Kafka-consumers-created")
return nil
}
// createGroupConsumer creates a consumers group
-func (sc *SaramaClient) createGroupConsumer(topic *Topic, groupId string, initialOffset int64, retries int) (*scc.Consumer, error) {
+func (sc *SaramaClient) createGroupConsumer(ctx context.Context, topic *Topic, groupId string, initialOffset int64, retries int) (*scc.Consumer, error) {
config := scc.NewConfig()
config.ClientID = uuid.New().String()
config.Group.Mode = scc.ConsumerModeMultiplex
@@ -883,10 +883,10 @@
var err error
if consumer, err = scc.NewConsumer(brokers, groupId, topics, config); err != nil {
- logger.Errorw("create-group-consumers-failure", log.Fields{"error": err, "topic": topic.Name, "groupId": groupId})
+ logger.Errorw(ctx, "create-group-consumers-failure", log.Fields{"error": err, "topic": topic.Name, "groupId": groupId})
return nil, err
}
- logger.Debugw("create-group-consumers-success", log.Fields{"topic": topic.Name, "groupId": groupId})
+ logger.Debugw(ctx, "create-group-consumers-success", log.Fields{"topic": topic.Name, "groupId": groupId})
//sc.groupConsumers[topic.Name] = consumer
sc.addToGroupConsumers(topic.Name, consumer)
@@ -911,104 +911,104 @@
}
}
-func (sc *SaramaClient) consumeFromAPartition(topic *Topic, consumer sarama.PartitionConsumer, consumerChnls *consumerChannels) {
- logger.Debugw("starting-partition-consumption-loop", log.Fields{"topic": topic.Name})
+func (sc *SaramaClient) consumeFromAPartition(ctx context.Context, topic *Topic, consumer sarama.PartitionConsumer, consumerChnls *consumerChannels) {
+ logger.Debugw(ctx, "starting-partition-consumption-loop", log.Fields{"topic": topic.Name})
startloop:
for {
select {
case err, ok := <-consumer.Errors():
if ok {
- if sc.isLivenessError(err) {
- sc.updateLiveness(false)
- logger.Warnw("partition-consumers-error", log.Fields{"error": err})
+ if sc.isLivenessError(ctx, err) {
+ sc.updateLiveness(ctx, false)
+ logger.Warnw(ctx, "partition-consumers-error", log.Fields{"error": err})
}
} else {
// Channel is closed
break startloop
}
case msg, ok := <-consumer.Messages():
- //logger.Debugw("message-received", logger.Fields{"msg": msg, "receivedTopic": msg.Topic})
+ //logger.Debugw(ctx, "message-received", logger.Fields{"msg": msg, "receivedTopic": msg.Topic})
if !ok {
// channel is closed
break startloop
}
msgBody := msg.Value
- sc.updateLiveness(true)
- logger.Debugw("message-received", log.Fields{"timestamp": msg.Timestamp, "receivedTopic": msg.Topic})
+ sc.updateLiveness(ctx, true)
+ logger.Debugw(ctx, "message-received", log.Fields{"timestamp": msg.Timestamp, "receivedTopic": msg.Topic})
icm := &ic.InterContainerMessage{}
if err := proto.Unmarshal(msgBody, icm); err != nil {
- logger.Warnw("partition-invalid-message", log.Fields{"error": err})
+ logger.Warnw(ctx, "partition-invalid-message", log.Fields{"error": err})
continue
}
go sc.dispatchToConsumers(consumerChnls, icm)
case <-sc.doneCh:
- logger.Infow("partition-received-exit-signal", log.Fields{"topic": topic.Name})
+ logger.Infow(ctx, "partition-received-exit-signal", log.Fields{"topic": topic.Name})
break startloop
}
}
- logger.Infow("partition-consumer-stopped", log.Fields{"topic": topic.Name})
- sc.setUnhealthy()
+ logger.Infow(ctx, "partition-consumer-stopped", log.Fields{"topic": topic.Name})
+ sc.setUnhealthy(ctx)
}
-func (sc *SaramaClient) consumeGroupMessages(topic *Topic, consumer *scc.Consumer, consumerChnls *consumerChannels) {
- logger.Debugw("starting-group-consumption-loop", log.Fields{"topic": topic.Name})
+func (sc *SaramaClient) consumeGroupMessages(ctx context.Context, topic *Topic, consumer *scc.Consumer, consumerChnls *consumerChannels) {
+ logger.Debugw(ctx, "starting-group-consumption-loop", log.Fields{"topic": topic.Name})
startloop:
for {
select {
case err, ok := <-consumer.Errors():
if ok {
- if sc.isLivenessError(err) {
- sc.updateLiveness(false)
+ if sc.isLivenessError(ctx, err) {
+ sc.updateLiveness(ctx, false)
}
- logger.Warnw("group-consumers-error", log.Fields{"topic": topic.Name, "error": err})
+ logger.Warnw(ctx, "group-consumers-error", log.Fields{"topic": topic.Name, "error": err})
} else {
- logger.Warnw("group-consumers-closed-err", log.Fields{"topic": topic.Name})
+ logger.Warnw(ctx, "group-consumers-closed-err", log.Fields{"topic": topic.Name})
// channel is closed
break startloop
}
case msg, ok := <-consumer.Messages():
if !ok {
- logger.Warnw("group-consumers-closed-msg", log.Fields{"topic": topic.Name})
+ logger.Warnw(ctx, "group-consumers-closed-msg", log.Fields{"topic": topic.Name})
// Channel closed
break startloop
}
- sc.updateLiveness(true)
- logger.Debugw("message-received", log.Fields{"timestamp": msg.Timestamp, "receivedTopic": msg.Topic})
+ sc.updateLiveness(ctx, true)
+ logger.Debugw(ctx, "message-received", log.Fields{"timestamp": msg.Timestamp, "receivedTopic": msg.Topic})
msgBody := msg.Value
icm := &ic.InterContainerMessage{}
if err := proto.Unmarshal(msgBody, icm); err != nil {
- logger.Warnw("invalid-message", log.Fields{"error": err})
+ logger.Warnw(ctx, "invalid-message", log.Fields{"error": err})
continue
}
go sc.dispatchToConsumers(consumerChnls, icm)
consumer.MarkOffset(msg, "")
case ntf := <-consumer.Notifications():
- logger.Debugw("group-received-notification", log.Fields{"notification": ntf})
+ logger.Debugw(ctx, "group-received-notification", log.Fields{"notification": ntf})
case <-sc.doneCh:
- logger.Infow("group-received-exit-signal", log.Fields{"topic": topic.Name})
+ logger.Infow(ctx, "group-received-exit-signal", log.Fields{"topic": topic.Name})
break startloop
}
}
- logger.Infow("group-consumer-stopped", log.Fields{"topic": topic.Name})
- sc.setUnhealthy()
+ logger.Infow(ctx, "group-consumer-stopped", log.Fields{"topic": topic.Name})
+ sc.setUnhealthy(ctx)
}
-func (sc *SaramaClient) startConsumers(topic *Topic) error {
- logger.Debugw("starting-consumers", log.Fields{"topic": topic.Name})
+func (sc *SaramaClient) startConsumers(ctx context.Context, topic *Topic) error {
+ logger.Debugw(ctx, "starting-consumers", log.Fields{"topic": topic.Name})
var consumerCh *consumerChannels
if consumerCh = sc.getConsumerChannel(topic); consumerCh == nil {
- logger.Errorw("consumers-not-exist", log.Fields{"topic": topic.Name})
+ logger.Errorw(ctx, "consumers-not-exist", log.Fields{"topic": topic.Name})
return errors.New("consumers-not-exist")
}
// For each consumer listening for that topic, start a consumption loop
for _, consumer := range consumerCh.consumers {
if pConsumer, ok := consumer.(sarama.PartitionConsumer); ok {
- go sc.consumeFromAPartition(topic, pConsumer, consumerCh)
+ go sc.consumeFromAPartition(ctx, topic, pConsumer, consumerCh)
} else if gConsumer, ok := consumer.(*scc.Consumer); ok {
- go sc.consumeGroupMessages(topic, gConsumer, consumerCh)
+ go sc.consumeGroupMessages(ctx, topic, gConsumer, consumerCh)
} else {
- logger.Errorw("invalid-consumer", log.Fields{"topic": topic})
+ logger.Errorw(ctx, "invalid-consumer", log.Fields{"topic": topic})
return errors.New("invalid-consumer")
}
}
@@ -1017,12 +1017,12 @@
//// setupConsumerChannel creates a consumerChannels object for that topic and add it to the consumerChannels map
//// for that topic. It also starts the routine that listens for messages on that topic.
-func (sc *SaramaClient) setupPartitionConsumerChannel(topic *Topic, initialOffset int64) (chan *ic.InterContainerMessage, error) {
+func (sc *SaramaClient) setupPartitionConsumerChannel(ctx context.Context, topic *Topic, initialOffset int64) (chan *ic.InterContainerMessage, error) {
var pConsumers []sarama.PartitionConsumer
var err error
- if pConsumers, err = sc.createPartitionConsumers(topic, initialOffset); err != nil {
- logger.Errorw("creating-partition-consumers-failure", log.Fields{"error": err, "topic": topic.Name})
+ if pConsumers, err = sc.createPartitionConsumers(ctx, topic, initialOffset); err != nil {
+ logger.Errorw(ctx, "creating-partition-consumers-failure", log.Fields{"error": err, "topic": topic.Name})
return nil, err
}
@@ -1044,8 +1044,8 @@
//Start a consumers to listen on that specific topic
go func() {
- if err := sc.startConsumers(topic); err != nil {
- logger.Errorw("start-consumers-failed", log.Fields{
+ if err := sc.startConsumers(ctx, topic); err != nil {
+ logger.Errorw(ctx, "start-consumers-failed", log.Fields{
"topic": topic,
"error": err})
}
@@ -1056,12 +1056,12 @@
// setupConsumerChannel creates a consumerChannels object for that topic and add it to the consumerChannels map
// for that topic. It also starts the routine that listens for messages on that topic.
-func (sc *SaramaClient) setupGroupConsumerChannel(topic *Topic, groupId string, initialOffset int64) (chan *ic.InterContainerMessage, error) {
+func (sc *SaramaClient) setupGroupConsumerChannel(ctx context.Context, topic *Topic, groupId string, initialOffset int64) (chan *ic.InterContainerMessage, error) {
// TODO: Replace this development partition consumers with a group consumers
var pConsumer *scc.Consumer
var err error
- if pConsumer, err = sc.createGroupConsumer(topic, groupId, initialOffset, DefaultMaxRetries); err != nil {
- logger.Errorw("creating-partition-consumers-failure", log.Fields{"error": err, "topic": topic.Name})
+ if pConsumer, err = sc.createGroupConsumer(ctx, topic, groupId, initialOffset, DefaultMaxRetries); err != nil {
+ logger.Errorw(ctx, "creating-partition-consumers-failure", log.Fields{"error": err, "topic": topic.Name})
return nil, err
}
// Create the consumers/channel structure and set the consumers and create a channel on that topic - for now
@@ -1077,8 +1077,8 @@
//Start a consumers to listen on that specific topic
go func() {
- if err := sc.startConsumers(topic); err != nil {
- logger.Errorw("start-consumers-failed", log.Fields{
+ if err := sc.startConsumers(ctx, topic); err != nil {
+ logger.Errorw(ctx, "start-consumers-failed", log.Fields{
"topic": topic,
"error": err})
}
@@ -1087,11 +1087,11 @@
return consumerListeningChannel, nil
}
-func (sc *SaramaClient) createPartitionConsumers(topic *Topic, initialOffset int64) ([]sarama.PartitionConsumer, error) {
- logger.Debugw("creating-partition-consumers", log.Fields{"topic": topic.Name})
+func (sc *SaramaClient) createPartitionConsumers(ctx context.Context, topic *Topic, initialOffset int64) ([]sarama.PartitionConsumer, error) {
+ logger.Debugw(ctx, "creating-partition-consumers", log.Fields{"topic": topic.Name})
partitionList, err := sc.consumer.Partitions(topic.Name)
if err != nil {
- logger.Warnw("get-partition-failure", log.Fields{"error": err, "topic": topic.Name})
+ logger.Warnw(ctx, "get-partition-failure", log.Fields{"error": err, "topic": topic.Name})
return nil, err
}
@@ -1099,7 +1099,7 @@
for _, partition := range partitionList {
var pConsumer sarama.PartitionConsumer
if pConsumer, err = sc.consumer.ConsumePartition(topic.Name, partition, initialOffset); err != nil {
- logger.Warnw("consumers-partition-failure", log.Fields{"error": err, "topic": topic.Name})
+ logger.Warnw(ctx, "consumers-partition-failure", log.Fields{"error": err, "topic": topic.Name})
return nil, err
}
pConsumers = append(pConsumers, pConsumer)
@@ -1107,14 +1107,14 @@
return pConsumers, nil
}
-func removeChannel(channels []chan *ic.InterContainerMessage, ch <-chan *ic.InterContainerMessage) []chan *ic.InterContainerMessage {
+func removeChannel(ctx context.Context, channels []chan *ic.InterContainerMessage, ch <-chan *ic.InterContainerMessage) []chan *ic.InterContainerMessage {
var i int
var channel chan *ic.InterContainerMessage
for i, channel = range channels {
if channel == ch {
channels[len(channels)-1], channels[i] = channels[i], channels[len(channels)-1]
close(channel)
- logger.Debug("channel-closed")
+ logger.Debug(ctx, "channel-closed")
return channels[:len(channels)-1]
}
}
@@ -1129,14 +1129,14 @@
}
}
-func (sc *SaramaClient) deleteFromGroupConsumers(topic string) error {
+func (sc *SaramaClient) deleteFromGroupConsumers(ctx context.Context, topic string) error {
sc.lockOfGroupConsumers.Lock()
defer sc.lockOfGroupConsumers.Unlock()
if _, exist := sc.groupConsumers[topic]; exist {
consumer := sc.groupConsumers[topic]
delete(sc.groupConsumers, topic)
if err := consumer.Close(); err != nil {
- logger.Errorw("failure-closing-consumer", log.Fields{"error": err})
+ logger.Errorw(ctx, "failure-closing-consumer", log.Fields{"error": err})
return err
}
}
diff --git a/pkg/kafka/sarama_client_test.go b/pkg/kafka/sarama_client_test.go
index 6dd9fd8..cbbfe7e 100644
--- a/pkg/kafka/sarama_client_test.go
+++ b/pkg/kafka/sarama_client_test.go
@@ -16,6 +16,7 @@
package kafka
import (
+ "context"
"github.com/stretchr/testify/assert"
"testing"
)
@@ -24,7 +25,7 @@
// Note: This doesn't actually start the client
client := NewSaramaClient()
- ch := client.EnableLivenessChannel(true)
+ ch := client.EnableLivenessChannel(context.Background(), true)
// The channel should have one "true" message on it
assert.NotEmpty(t, ch)
diff --git a/pkg/mocks/etcd/common.go b/pkg/mocks/etcd/common.go
index a45b4b2..63d4ab0 100644
--- a/pkg/mocks/etcd/common.go
+++ b/pkg/mocks/etcd/common.go
@@ -19,12 +19,12 @@
"github.com/opencord/voltha-lib-go/v3/pkg/log"
)
-var logger log.Logger
+var logger log.CLogger
func init() {
// Setup this package so that it's log level can be modified at run time
var err error
- logger, err = log.AddPackage(log.JSON, log.ErrorLevel, log.Fields{"pkg": "mocks"})
+ logger, err = log.RegisterPackage(log.JSON, log.ErrorLevel, log.Fields{"pkg": "mocks"})
if err != nil {
panic(err)
}
diff --git a/pkg/mocks/etcd/etcd_server.go b/pkg/mocks/etcd/etcd_server.go
index b4e201d..6113b3a 100644
--- a/pkg/mocks/etcd/etcd_server.go
+++ b/pkg/mocks/etcd/etcd_server.go
@@ -16,6 +16,7 @@
package etcd
import (
+ "context"
"fmt"
"go.etcd.io/etcd/embed"
"net/url"
@@ -51,25 +52,25 @@
* :param localPersistentStorageDir: The name of a local directory which will hold the Etcd server data
* :param logLevel: One of debug, info, warn, error, panic, or fatal. Default 'info'.
*/
-func MKConfig(configName string, clientPort, peerPort int, localPersistentStorageDir string, logLevel string) *embed.Config {
+func MKConfig(ctx context.Context, configName string, clientPort, peerPort int, localPersistentStorageDir string, logLevel string) *embed.Config {
cfg := embed.NewConfig()
cfg.Name = configName
cfg.Dir = localPersistentStorageDir
cfg.Logger = "zap"
if !islogLevelValid(logLevel) {
- logger.Fatalf("Invalid log level -%s", logLevel)
+ logger.Fatalf(ctx, "Invalid log level -%s", logLevel)
}
cfg.LogLevel = logLevel
acurl, err := url.Parse(fmt.Sprintf("http://localhost:%d", clientPort))
if err != nil {
- logger.Fatalf("Invalid client port -%d", clientPort)
+ logger.Fatalf(ctx, "Invalid client port -%d", clientPort)
}
cfg.ACUrls = []url.URL{*acurl}
cfg.LCUrls = []url.URL{*acurl}
apurl, err := url.Parse(fmt.Sprintf("http://localhost:%d", peerPort))
if err != nil {
- logger.Fatalf("Invalid peer port -%d", peerPort)
+ logger.Fatalf(ctx, "Invalid peer port -%d", peerPort)
}
cfg.LPUrls = []url.URL{*apurl}
cfg.APUrls = []url.URL{*apurl}
@@ -91,7 +92,7 @@
//StartEtcdServer creates and starts an embedded Etcd server. A local directory to store data is created for the
//embedded server lifetime (for the duration of a unit test. The server runs at localhost:2379.
-func StartEtcdServer(cfg *embed.Config) *EtcdServer {
+func StartEtcdServer(ctx context.Context, cfg *embed.Config) *EtcdServer {
// If the server is already running, just return
if cfg == nil {
cfg = getDefaultCfg()
@@ -99,35 +100,35 @@
// Remove the local directory as
// a safeguard for the case where a prior test failed
if err := os.RemoveAll(cfg.Dir); err != nil {
- logger.Fatalf("Failure removing local directory %s", cfg.Dir)
+ logger.Fatalf(ctx, "Failure removing local directory %s", cfg.Dir)
}
e, err := embed.StartEtcd(cfg)
if err != nil {
- logger.Fatal(err)
+ logger.Fatal(ctx, err)
}
select {
case <-e.Server.ReadyNotify():
- logger.Debug("Embedded Etcd server is ready!")
+ logger.Debug(ctx, "Embedded Etcd server is ready!")
case <-time.After(serverStartUpTimeout):
e.Server.HardStop() // trigger a shutdown
e.Close()
- logger.Fatal("Embedded Etcd server took too long to start!")
+ logger.Fatal(ctx, "Embedded Etcd server took too long to start!")
case err := <-e.Err():
e.Server.HardStop() // trigger a shutdown
e.Close()
- logger.Fatalf("Embedded Etcd server errored out - %s", err)
+ logger.Fatalf(ctx, "Embedded Etcd server errored out - %s", err)
}
return &EtcdServer{server: e}
}
//Stop closes the embedded Etcd server and removes the local data directory as well
-func (es *EtcdServer) Stop() {
+func (es *EtcdServer) Stop(ctx context.Context) {
if es != nil {
storage := es.server.Config().Dir
es.server.Server.HardStop()
es.server.Close()
if err := os.RemoveAll(storage); err != nil {
- logger.Fatalf("Failure removing local directory %s", es.server.Config().Dir)
+ logger.Fatalf(ctx, "Failure removing local directory %s", es.server.Config().Dir)
}
}
}
diff --git a/pkg/mocks/etcd/etcd_server_test.go b/pkg/mocks/etcd/etcd_server_test.go
index 43c7a42..b26b262 100644
--- a/pkg/mocks/etcd/etcd_server_test.go
+++ b/pkg/mocks/etcd/etcd_server_test.go
@@ -32,23 +32,24 @@
var client *kvstore.EtcdClient
func setup() {
+ ctx := context.Background()
clientPort, err := freeport.GetFreePort()
if err != nil {
- logger.Fatal(err)
+ logger.Fatal(ctx, err)
}
peerPort, err := freeport.GetFreePort()
if err != nil {
- logger.Fatal(err)
+ logger.Fatal(ctx, err)
}
- etcdServer = StartEtcdServer(MKConfig("voltha.mock.test", clientPort, peerPort, "voltha.lib.mocks.etcd", "error"))
+ etcdServer = StartEtcdServer(ctx, MKConfig(ctx, "voltha.mock.test", clientPort, peerPort, "voltha.lib.mocks.etcd", "error"))
if etcdServer == nil {
- logger.Fatal("Embedded server failed to start")
+ logger.Fatal(ctx, "Embedded server failed to start")
}
clientAddr := fmt.Sprintf("localhost:%d", clientPort)
- client, err = kvstore.NewEtcdClient(clientAddr, 10*time.Second, log.WarnLevel)
+ client, err = kvstore.NewEtcdClient(ctx, clientAddr, 10*time.Second, log.WarnLevel)
if err != nil || client == nil {
- etcdServer.Stop()
- logger.Fatal("Failed to create an Etcd client")
+ etcdServer.Stop(ctx)
+ logger.Fatal(ctx, "Failed to create an Etcd client")
}
}
@@ -77,10 +78,10 @@
func shutdown() {
if client != nil {
- client.Close()
+ client.Close(context.Background())
}
if etcdServer != nil {
- etcdServer.Stop()
+ etcdServer.Stop(context.Background())
}
}
diff --git a/pkg/mocks/kafka/common.go b/pkg/mocks/kafka/common.go
index 05bc5f9..e980b05 100644
--- a/pkg/mocks/kafka/common.go
+++ b/pkg/mocks/kafka/common.go
@@ -19,12 +19,12 @@
"github.com/opencord/voltha-lib-go/v3/pkg/log"
)
-var logger log.Logger
+var logger log.CLogger
func init() {
// Setup this package so that it's log level can be modified at run time
var err error
- logger, err = log.AddPackage(log.JSON, log.ErrorLevel, log.Fields{"pkg": "mocks"})
+ logger, err = log.RegisterPackage(log.JSON, log.ErrorLevel, log.Fields{"pkg": "mocks"})
if err != nil {
panic(err)
}
diff --git a/pkg/mocks/kafka/endpoint_manager.go b/pkg/mocks/kafka/endpoint_manager.go
index fedbebf..8b8e7f6 100644
--- a/pkg/mocks/kafka/endpoint_manager.go
+++ b/pkg/mocks/kafka/endpoint_manager.go
@@ -17,6 +17,7 @@
package kafka
import (
+ "context"
"github.com/opencord/voltha-lib-go/v3/pkg/kafka"
)
@@ -27,16 +28,16 @@
return mock
}
-func (em *EndpointManager) GetEndpoint(deviceID string, serviceType string) (kafka.Endpoint, error) {
+func (em *EndpointManager) GetEndpoint(ctx context.Context, deviceID string, serviceType string) (kafka.Endpoint, error) {
// TODO add mocks call and args
return kafka.Endpoint(serviceType), nil
}
-func (em *EndpointManager) IsDeviceOwnedByService(deviceID string, serviceType string, replicaNumber int32) (bool, error) {
+func (em *EndpointManager) IsDeviceOwnedByService(ctx context.Context, deviceID string, serviceType string, replicaNumber int32) (bool, error) {
// TODO add mocks call and args
return true, nil
}
-func (em *EndpointManager) GetReplicaAssignment(deviceID string, serviceType string) (kafka.ReplicaID, error) {
+func (em *EndpointManager) GetReplicaAssignment(ctx context.Context, deviceID string, serviceType string) (kafka.ReplicaID, error) {
return kafka.ReplicaID(1), nil
}
diff --git a/pkg/mocks/kafka/kafka_client.go b/pkg/mocks/kafka/kafka_client.go
index 7c5508b..9d6f50c 100644
--- a/pkg/mocks/kafka/kafka_client.go
+++ b/pkg/mocks/kafka/kafka_client.go
@@ -16,6 +16,7 @@
package kafka
import (
+ "context"
"fmt"
"sync"
"time"
@@ -42,12 +43,12 @@
}
}
-func (kc *KafkaClient) Start() error {
- logger.Debug("kafka-client-started")
+func (kc *KafkaClient) Start(ctx context.Context) error {
+ logger.Debug(ctx, "kafka-client-started")
return nil
}
-func (kc *KafkaClient) Stop() {
+func (kc *KafkaClient) Stop(ctx context.Context) {
kc.lock.Lock()
defer kc.lock.Unlock()
for topic, chnls := range kc.topicsChannelMap {
@@ -56,11 +57,11 @@
}
delete(kc.topicsChannelMap, topic)
}
- logger.Debug("kafka-client-stopped")
+ logger.Debug(ctx, "kafka-client-stopped")
}
-func (kc *KafkaClient) CreateTopic(topic *kafka.Topic, numPartition int, repFactor int) error {
- logger.Debugw("CreatingTopic", log.Fields{"topic": topic.Name, "numPartition": numPartition, "replicationFactor": repFactor})
+func (kc *KafkaClient) CreateTopic(ctx context.Context, topic *kafka.Topic, numPartition int, repFactor int) error {
+ logger.Debugw(ctx, "CreatingTopic", log.Fields{"topic": topic.Name, "numPartition": numPartition, "replicationFactor": repFactor})
kc.lock.Lock()
defer kc.lock.Unlock()
if _, ok := kc.topicsChannelMap[topic.Name]; ok {
@@ -71,16 +72,16 @@
return nil
}
-func (kc *KafkaClient) DeleteTopic(topic *kafka.Topic) error {
- logger.Debugw("DeleteTopic", log.Fields{"topic": topic.Name})
+func (kc *KafkaClient) DeleteTopic(ctx context.Context, topic *kafka.Topic) error {
+ logger.Debugw(ctx, "DeleteTopic", log.Fields{"topic": topic.Name})
kc.lock.Lock()
defer kc.lock.Unlock()
delete(kc.topicsChannelMap, topic.Name)
return nil
}
-func (kc *KafkaClient) Subscribe(topic *kafka.Topic, kvArgs ...*kafka.KVArg) (<-chan *ic.InterContainerMessage, error) {
- logger.Debugw("Subscribe", log.Fields{"topic": topic.Name, "args": kvArgs})
+func (kc *KafkaClient) Subscribe(ctx context.Context, topic *kafka.Topic, kvArgs ...*kafka.KVArg) (<-chan *ic.InterContainerMessage, error) {
+ logger.Debugw(ctx, "Subscribe", log.Fields{"topic": topic.Name, "args": kvArgs})
kc.lock.Lock()
defer kc.lock.Unlock()
ch := make(chan *ic.InterContainerMessage)
@@ -93,8 +94,8 @@
return s[:len(s)-1]
}
-func (kc *KafkaClient) UnSubscribe(topic *kafka.Topic, ch <-chan *ic.InterContainerMessage) error {
- logger.Debugw("UnSubscribe", log.Fields{"topic": topic.Name})
+func (kc *KafkaClient) UnSubscribe(ctx context.Context, topic *kafka.Topic, ch <-chan *ic.InterContainerMessage) error {
+ logger.Debugw(ctx, "UnSubscribe", log.Fields{"topic": topic.Name})
kc.lock.Lock()
defer kc.lock.Unlock()
if chnls, ok := kc.topicsChannelMap[topic.Name]; ok {
@@ -112,11 +113,11 @@
return nil
}
-func (kc *KafkaClient) SubscribeForMetadata(_ func(fromTopic string, timestamp time.Time)) {
- logger.Debug("SubscribeForMetadata - unimplemented")
+func (kc *KafkaClient) SubscribeForMetadata(ctx context.Context, _ func(fromTopic string, timestamp time.Time)) {
+ logger.Debug(ctx, "SubscribeForMetadata - unimplemented")
}
-func (kc *KafkaClient) Send(msg interface{}, topic *kafka.Topic, keys ...string) error {
+func (kc *KafkaClient) Send(ctx context.Context, msg interface{}, topic *kafka.Topic, keys ...string) error {
req, ok := msg.(*ic.InterContainerMessage)
if !ok {
return status.Error(codes.InvalidArgument, "msg-not-InterContainerMessage-type")
@@ -127,22 +128,22 @@
kc.lock.RLock()
defer kc.lock.RUnlock()
for _, ch := range kc.topicsChannelMap[topic.Name] {
- logger.Debugw("Publishing", log.Fields{"fromTopic": req.Header.FromTopic, "toTopic": topic.Name, "id": req.Header.Id})
+ logger.Debugw(ctx, "Publishing", log.Fields{"fromTopic": req.Header.FromTopic, "toTopic": topic.Name, "id": req.Header.Id})
ch <- req
}
return nil
}
-func (kc *KafkaClient) SendLiveness() error {
+func (kc *KafkaClient) SendLiveness(ctx context.Context) error {
return status.Error(codes.Unimplemented, "SendLiveness")
}
-func (kc *KafkaClient) EnableLivenessChannel(enable bool) chan bool {
- logger.Debug("EnableLivenessChannel - unimplemented")
+func (kc *KafkaClient) EnableLivenessChannel(ctx context.Context, enable bool) chan bool {
+ logger.Debug(ctx, "EnableLivenessChannel - unimplemented")
return nil
}
-func (kc *KafkaClient) EnableHealthinessChannel(enable bool) chan bool {
- logger.Debug("EnableHealthinessChannel - unimplemented")
+func (kc *KafkaClient) EnableHealthinessChannel(ctx context.Context, enable bool) chan bool {
+ logger.Debug(ctx, "EnableHealthinessChannel - unimplemented")
return nil
}
diff --git a/pkg/mocks/kafka/kafka_client_test.go b/pkg/mocks/kafka/kafka_client_test.go
index 0e35ec1..7753d66 100644
--- a/pkg/mocks/kafka/kafka_client_test.go
+++ b/pkg/mocks/kafka/kafka_client_test.go
@@ -17,34 +17,35 @@
package kafka
import (
- "testing"
- "time"
-
+ "context"
"github.com/opencord/voltha-lib-go/v3/pkg/kafka"
ic "github.com/opencord/voltha-protos/v3/go/inter_container"
"github.com/stretchr/testify/assert"
+ "testing"
+ "time"
)
func TestKafkaClientCreateTopic(t *testing.T) {
+ ctx := context.Background()
cTkc := NewKafkaClient()
topic := kafka.Topic{Name: "myTopic"}
- err := cTkc.CreateTopic(&topic, 1, 1)
+ err := cTkc.CreateTopic(ctx, &topic, 1, 1)
assert.Nil(t, err)
- err = cTkc.CreateTopic(&topic, 1, 1)
+ err = cTkc.CreateTopic(ctx, &topic, 1, 1)
assert.NotNil(t, err)
}
func TestKafkaClientDeleteTopic(t *testing.T) {
cTkc := NewKafkaClient()
topic := kafka.Topic{Name: "myTopic"}
- err := cTkc.DeleteTopic(&topic)
+ err := cTkc.DeleteTopic(context.Background(), &topic)
assert.Nil(t, err)
}
func TestKafkaClientSubscribeSend(t *testing.T) {
cTkc := NewKafkaClient()
topic := kafka.Topic{Name: "myTopic"}
- ch, err := cTkc.Subscribe(&topic)
+ ch, err := cTkc.Subscribe(context.Background(), &topic)
assert.Nil(t, err)
assert.NotNil(t, ch)
testCh := make(chan bool)
@@ -65,7 +66,7 @@
testCh <- false
}
}()
- err = cTkc.Send(msg, &topic)
+ err = cTkc.Send(context.Background(), msg, &topic)
assert.Nil(t, err)
res := <-testCh
assert.True(t, res)
@@ -74,20 +75,20 @@
func TestKafkaClientUnSubscribe(t *testing.T) {
cTkc := NewKafkaClient()
topic := kafka.Topic{Name: "myTopic"}
- ch, err := cTkc.Subscribe(&topic)
+ ch, err := cTkc.Subscribe(context.Background(), &topic)
assert.Nil(t, err)
assert.NotNil(t, ch)
- err = cTkc.UnSubscribe(&topic, ch)
+ err = cTkc.UnSubscribe(context.Background(), &topic, ch)
assert.Nil(t, err)
}
func TestKafkaClientStop(t *testing.T) {
cTkc := NewKafkaClient()
topic := kafka.Topic{Name: "myTopic"}
- ch, err := cTkc.Subscribe(&topic)
+ ch, err := cTkc.Subscribe(context.Background(), &topic)
assert.Nil(t, err)
assert.NotNil(t, ch)
- err = cTkc.UnSubscribe(&topic, ch)
+ err = cTkc.UnSubscribe(context.Background(), &topic, ch)
assert.Nil(t, err)
- cTkc.Stop()
+ cTkc.Stop(context.Background())
}
diff --git a/pkg/mocks/kafka/kafka_inter_container_proxy.go b/pkg/mocks/kafka/kafka_inter_container_proxy.go
index 34aec95..2a1b5a1 100644
--- a/pkg/mocks/kafka/kafka_inter_container_proxy.go
+++ b/pkg/mocks/kafka/kafka_inter_container_proxy.go
@@ -53,18 +53,18 @@
InvokeRpcSpy InvokeRpcSpy
}
-func (s *MockKafkaICProxy) Start() error { return nil }
+func (s *MockKafkaICProxy) Start(ctx context.Context) error { return nil }
func (s *MockKafkaICProxy) GetDefaultTopic() *kafka.Topic {
t := kafka.Topic{
Name: "test-topic",
}
return &t
}
-func (s *MockKafkaICProxy) DeleteTopic(topic kafka.Topic) error { return nil }
-func (s *MockKafkaICProxy) DeviceDiscovered(deviceId string, deviceType string, parentId string, publisher string) error {
+func (s *MockKafkaICProxy) DeleteTopic(ctx context.Context, topic kafka.Topic) error { return nil }
+func (s *MockKafkaICProxy) DeviceDiscovered(ctx context.Context, deviceId string, deviceType string, parentId string, publisher string) error {
return nil
}
-func (s *MockKafkaICProxy) Stop() {}
+func (s *MockKafkaICProxy) Stop(ctx context.Context) {}
func (s *MockKafkaICProxy) InvokeAsyncRPC(ctx context.Context, rpc string, toTopic *kafka.Topic, replyToTopic *kafka.Topic,
waitForResponse bool, key string, kvArgs ...*kafka.KVArg) chan *kafka.RpcResponse {
@@ -122,12 +122,16 @@
return success, &response
}
-func (s *MockKafkaICProxy) SubscribeWithRequestHandlerInterface(topic kafka.Topic, handler interface{}) error {
+func (s *MockKafkaICProxy) SubscribeWithRequestHandlerInterface(ctx context.Context, topic kafka.Topic, handler interface{}) error {
return nil
}
-func (s *MockKafkaICProxy) SubscribeWithDefaultRequestHandler(topic kafka.Topic, initialOffset int64) error {
+func (s *MockKafkaICProxy) SubscribeWithDefaultRequestHandler(ctx context.Context, topic kafka.Topic, initialOffset int64) error {
return nil
}
-func (s *MockKafkaICProxy) UnSubscribeFromRequestHandler(topic kafka.Topic) error { return nil }
-func (s *MockKafkaICProxy) EnableLivenessChannel(enable bool) chan bool { return nil }
-func (s *MockKafkaICProxy) SendLiveness() error { return nil }
+func (s *MockKafkaICProxy) UnSubscribeFromRequestHandler(ctx context.Context, topic kafka.Topic) error {
+ return nil
+}
+func (s *MockKafkaICProxy) EnableLivenessChannel(ctx context.Context, enable bool) chan bool {
+ return nil
+}
+func (s *MockKafkaICProxy) SendLiveness(ctx context.Context) error { return nil }
diff --git a/pkg/ponresourcemanager/common.go b/pkg/ponresourcemanager/common.go
index 0f4339e..113b39c 100644
--- a/pkg/ponresourcemanager/common.go
+++ b/pkg/ponresourcemanager/common.go
@@ -19,12 +19,12 @@
"github.com/opencord/voltha-lib-go/v3/pkg/log"
)
-var logger log.Logger
+var logger log.CLogger
func init() {
// Setup this package so that it's log level can be modified at run time
var err error
- logger, err = log.AddPackage(log.JSON, log.ErrorLevel, log.Fields{"pkg": "ponresourcemanager"})
+ logger, err = log.RegisterPackage(log.JSON, log.ErrorLevel, log.Fields{"pkg": "ponresourcemanager"})
if err != nil {
panic(err)
}
diff --git a/pkg/ponresourcemanager/ponresourcemanager.go b/pkg/ponresourcemanager/ponresourcemanager.go
index 5c10b5e..baff575 100755
--- a/pkg/ponresourcemanager/ponresourcemanager.go
+++ b/pkg/ponresourcemanager/ponresourcemanager.go
@@ -154,23 +154,23 @@
Globalorlocal string
}
-func newKVClient(storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
- logger.Infow("kv-store-type", log.Fields{"store": storeType})
+func newKVClient(ctx context.Context, storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
+ logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType})
switch storeType {
case "consul":
- return kvstore.NewConsulClient(address, timeout)
+ return kvstore.NewConsulClient(ctx, address, timeout)
case "etcd":
- return kvstore.NewEtcdClient(address, timeout, log.WarnLevel)
+ return kvstore.NewEtcdClient(ctx, address, timeout, log.WarnLevel)
}
return nil, errors.New("unsupported-kv-store")
}
-func SetKVClient(Technology string, Backend string, Addr string, configClient bool) *db.Backend {
+func SetKVClient(ctx context.Context, Technology string, Backend string, Addr string, configClient bool) *db.Backend {
// TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
// issue between kv store and backend , core is not calling NewBackend directly
- kvClient, err := newKVClient(Backend, Addr, KVSTORE_RETRY_TIMEOUT)
+ kvClient, err := newKVClient(ctx, Backend, Addr, KVSTORE_RETRY_TIMEOUT)
if err != nil {
- logger.Fatalw("Failed to init KV client\n", log.Fields{"err": err})
+ logger.Fatalw(ctx, "Failed to init KV client\n", log.Fields{"err": err})
return nil
}
@@ -192,27 +192,27 @@
}
// NewPONResourceManager creates a new PON resource manager.
-func NewPONResourceManager(Technology string, DeviceType string, DeviceID string, Backend string, Address string) (*PONResourceManager, error) {
+func NewPONResourceManager(ctx context.Context, Technology string, DeviceType string, DeviceID string, Backend string, Address string) (*PONResourceManager, error) {
var PONMgr PONResourceManager
PONMgr.Technology = Technology
PONMgr.DeviceType = DeviceType
PONMgr.DeviceID = DeviceID
PONMgr.Backend = Backend
PONMgr.Address = Address
- PONMgr.KVStore = SetKVClient(Technology, Backend, Address, false)
+ PONMgr.KVStore = SetKVClient(ctx, Technology, Backend, Address, false)
if PONMgr.KVStore == nil {
- logger.Error("KV Client initilization failed")
+ logger.Error(ctx, "KV Client initilization failed")
return nil, errors.New("Failed to init KV client")
}
// init kv client to read from the config path
- PONMgr.KVStoreForConfig = SetKVClient(Technology, Backend, Address, true)
+ PONMgr.KVStoreForConfig = SetKVClient(ctx, Technology, Backend, Address, true)
if PONMgr.KVStoreForConfig == nil {
- logger.Error("KV Config Client initilization failed")
+ logger.Error(ctx, "KV Config Client initilization failed")
return nil, errors.New("Failed to init KV Config client")
}
// Initialize techprofile for this technology
- if PONMgr.TechProfileMgr, _ = tp.NewTechProfile(&PONMgr, Backend, Address); PONMgr.TechProfileMgr == nil {
- logger.Error("Techprofile initialization failed")
+ if PONMgr.TechProfileMgr, _ = tp.NewTechProfile(ctx, &PONMgr, Backend, Address); PONMgr.TechProfileMgr == nil {
+ logger.Error(ctx, "Techprofile initialization failed")
return nil, errors.New("Failed to init tech profile")
}
PONMgr.PonResourceRanges = make(map[string]interface{})
@@ -240,36 +240,36 @@
// Try to initialize the PON Resource Ranges from KV store based on the
// OLT model key, if available
if PONRMgr.OLTModel == "" {
- logger.Error("Failed to get OLT model")
+ logger.Error(ctx, "Failed to get OLT model")
return false
}
Path := fmt.Sprintf(PON_RESOURCE_RANGE_CONFIG_PATH, PONRMgr.OLTModel)
//get resource from kv store
Result, err := PONRMgr.KVStore.Get(ctx, Path)
if err != nil {
- logger.Debugf("Error in fetching resource %s from KV strore", Path)
+ logger.Debugf(ctx, "Error in fetching resource %s from KV strore", Path)
return false
}
if Result == nil {
- logger.Debug("There may be no resources in the KV store in case of fresh bootup, return true")
+ logger.Debug(ctx, "There may be no resources in the KV store in case of fresh bootup, return true")
return false
}
//update internal ranges from kv ranges. If there are missing
// values in the KV profile, continue to use the defaults
Value, err := ToByte(Result.Value)
if err != nil {
- logger.Error("Failed to convert kvpair to byte string")
+ logger.Error(ctx, "Failed to convert kvpair to byte string")
return false
}
if err := json.Unmarshal(Value, &PONRMgr.PonResourceRanges); err != nil {
- logger.Error("Failed to Unmarshal json byte")
+ logger.Error(ctx, "Failed to Unmarshal json byte")
return false
}
- logger.Debug("Init resource ranges from kvstore success")
+ logger.Debug(ctx, "Init resource ranges from kvstore success")
return true
}
-func (PONRMgr *PONResourceManager) UpdateRanges(StartIDx string, StartID uint32, EndIDx string, EndID uint32,
+func (PONRMgr *PONResourceManager) UpdateRanges(ctx context.Context, StartIDx string, StartID uint32, EndIDx string, EndID uint32,
SharedIDx string, SharedPoolID uint32, RMgr *PONResourceManager) {
/*
Update the ranges for all reosurce type in the intermnal maps
@@ -281,7 +281,7 @@
param: shared pool id
param: global resource manager
*/
- logger.Debugf("update ranges for %s, %d", StartIDx, StartID)
+ logger.Debugf(ctx, "update ranges for %s, %d", StartIDx, StartID)
if StartID != 0 {
if (PONRMgr.PonResourceRanges[StartIDx] == nil) || (PONRMgr.PonResourceRanges[StartIDx].(uint32) < StartID) {
@@ -301,7 +301,8 @@
}
}
-func (PONRMgr *PONResourceManager) InitDefaultPONResourceRanges(ONUIDStart uint32,
+func (PONRMgr *PONResourceManager) InitDefaultPONResourceRanges(ctx context.Context,
+ ONUIDStart uint32,
ONUIDEnd uint32,
ONUIDSharedPoolID uint32,
AllocIDStart uint32,
@@ -335,12 +336,12 @@
:param num_of_pon_ports: number of PON ports
:param intf_ids: interfaces serviced by this manager
*/
- PONRMgr.UpdateRanges(ONU_ID_START_IDX, ONUIDStart, ONU_ID_END_IDX, ONUIDEnd, ONU_ID_SHARED_IDX, ONUIDSharedPoolID, nil)
- PONRMgr.UpdateRanges(ALLOC_ID_START_IDX, AllocIDStart, ALLOC_ID_END_IDX, AllocIDEnd, ALLOC_ID_SHARED_IDX, AllocIDSharedPoolID, nil)
- PONRMgr.UpdateRanges(GEMPORT_ID_START_IDX, GEMPortIDStart, GEMPORT_ID_END_IDX, GEMPortIDEnd, GEMPORT_ID_SHARED_IDX, GEMPortIDSharedPoolID, nil)
- PONRMgr.UpdateRanges(FLOW_ID_START_IDX, FlowIDStart, FLOW_ID_END_IDX, FlowIDEnd, FLOW_ID_SHARED_IDX, FlowIDSharedPoolID, nil)
- PONRMgr.UpdateRanges(UNI_ID_START_IDX, UNIIDStart, UNI_ID_END_IDX, UNIIDEnd, "", 0, nil)
- logger.Debug("Initialize default range values")
+ PONRMgr.UpdateRanges(ctx, ONU_ID_START_IDX, ONUIDStart, ONU_ID_END_IDX, ONUIDEnd, ONU_ID_SHARED_IDX, ONUIDSharedPoolID, nil)
+ PONRMgr.UpdateRanges(ctx, ALLOC_ID_START_IDX, AllocIDStart, ALLOC_ID_END_IDX, AllocIDEnd, ALLOC_ID_SHARED_IDX, AllocIDSharedPoolID, nil)
+ PONRMgr.UpdateRanges(ctx, GEMPORT_ID_START_IDX, GEMPortIDStart, GEMPORT_ID_END_IDX, GEMPortIDEnd, GEMPORT_ID_SHARED_IDX, GEMPortIDSharedPoolID, nil)
+ PONRMgr.UpdateRanges(ctx, FLOW_ID_START_IDX, FlowIDStart, FLOW_ID_END_IDX, FlowIDEnd, FLOW_ID_SHARED_IDX, FlowIDSharedPoolID, nil)
+ PONRMgr.UpdateRanges(ctx, UNI_ID_START_IDX, UNIIDStart, UNI_ID_END_IDX, UNIIDEnd, "", 0, nil)
+ logger.Debug(ctx, "Initialize default range values")
var i uint32
if IntfIDs == nil {
for i = 0; i < NoOfPONPorts; i++ {
@@ -356,7 +357,7 @@
//Initialize resource pool for all PON ports.
- logger.Debug("Init resource ranges")
+ logger.Debug(ctx, "Init resource ranges")
var err error
for _, Intf := range PONRMgr.IntfIDs {
@@ -367,7 +368,7 @@
if err = PONRMgr.InitResourceIDPool(ctx, Intf, ONU_ID,
PONRMgr.PonResourceRanges[ONU_ID_START_IDX].(uint32),
PONRMgr.PonResourceRanges[ONU_ID_END_IDX].(uint32)); err != nil {
- logger.Error("Failed to init ONU ID resource pool")
+ logger.Error(ctx, "Failed to init ONU ID resource pool")
return err
}
if SharedPoolID != 0 {
@@ -383,7 +384,7 @@
if err = PONRMgr.InitResourceIDPool(ctx, Intf, ALLOC_ID,
PONRMgr.PonResourceRanges[ALLOC_ID_START_IDX].(uint32),
PONRMgr.PonResourceRanges[ALLOC_ID_END_IDX].(uint32)); err != nil {
- logger.Error("Failed to init ALLOC ID resource pool ")
+ logger.Error(ctx, "Failed to init ALLOC ID resource pool ")
return err
}
if SharedPoolID != 0 {
@@ -398,7 +399,7 @@
if err = PONRMgr.InitResourceIDPool(ctx, Intf, GEMPORT_ID,
PONRMgr.PonResourceRanges[GEMPORT_ID_START_IDX].(uint32),
PONRMgr.PonResourceRanges[GEMPORT_ID_END_IDX].(uint32)); err != nil {
- logger.Error("Failed to init GEMPORT ID resource pool")
+ logger.Error(ctx, "Failed to init GEMPORT ID resource pool")
return err
}
if SharedPoolID != 0 {
@@ -414,7 +415,7 @@
if err = PONRMgr.InitResourceIDPool(ctx, Intf, FLOW_ID,
PONRMgr.PonResourceRanges[FLOW_ID_START_IDX].(uint32),
PONRMgr.PonResourceRanges[FLOW_ID_END_IDX].(uint32)); err != nil {
- logger.Error("Failed to init FLOW ID resource pool")
+ logger.Error(ctx, "Failed to init FLOW ID resource pool")
return err
}
if SharedPoolID != 0 {
@@ -428,7 +429,7 @@
//Clear resource pool for all PON ports.
- logger.Debug("Clear resource ranges")
+ logger.Debug(ctx, "Clear resource ranges")
for _, Intf := range PONRMgr.IntfIDs {
SharedPoolID := PONRMgr.PonResourceRanges[ONU_ID_SHARED_IDX].(uint32)
@@ -436,7 +437,7 @@
Intf = SharedPoolID
}
if status := PONRMgr.ClearResourceIDPool(ctx, Intf, ONU_ID); !status {
- logger.Error("Failed to clear ONU ID resource pool")
+ logger.Error(ctx, "Failed to clear ONU ID resource pool")
return errors.New("Failed to clear ONU ID resource pool")
}
if SharedPoolID != 0 {
@@ -450,7 +451,7 @@
Intf = SharedPoolID
}
if status := PONRMgr.ClearResourceIDPool(ctx, Intf, ALLOC_ID); !status {
- logger.Error("Failed to clear ALLOC ID resource pool ")
+ logger.Error(ctx, "Failed to clear ALLOC ID resource pool ")
return errors.New("Failed to clear ALLOC ID resource pool")
}
if SharedPoolID != 0 {
@@ -463,7 +464,7 @@
Intf = SharedPoolID
}
if status := PONRMgr.ClearResourceIDPool(ctx, Intf, GEMPORT_ID); !status {
- logger.Error("Failed to clear GEMPORT ID resource pool")
+ logger.Error(ctx, "Failed to clear GEMPORT ID resource pool")
return errors.New("Failed to clear GEMPORT ID resource pool")
}
if SharedPoolID != 0 {
@@ -477,7 +478,7 @@
Intf = SharedPoolID
}
if status := PONRMgr.ClearResourceIDPool(ctx, Intf, FLOW_ID); !status {
- logger.Error("Failed to clear FLOW ID resource pool")
+ logger.Error(ctx, "Failed to clear FLOW ID resource pool")
return errors.New("Failed to clear FLOW ID resource pool")
}
if SharedPoolID != 0 {
@@ -504,9 +505,9 @@
return SharedResourceMgr.InitResourceIDPool(ctx, Intf, ResourceType, StartID, EndID)
}
- Path := PONRMgr.GetPath(Intf, ResourceType)
+ Path := PONRMgr.GetPath(ctx, Intf, ResourceType)
if Path == "" {
- logger.Errorf("Failed to get path for resource type %s", ResourceType)
+ logger.Errorf(ctx, "Failed to get path for resource type %s", ResourceType)
return fmt.Errorf("Failed to get path for resource type %s", ResourceType)
}
@@ -514,7 +515,7 @@
//checked for its presence if not kv store update happens
Res, err := PONRMgr.GetResource(ctx, Path)
if (err == nil) && (Res != nil) {
- logger.Debugf("Resource %s already present in store ", Path)
+ logger.Debugf(ctx, "Resource %s already present in store ", Path)
return nil
} else {
var excluded []uint32
@@ -522,23 +523,23 @@
//get gem port ids defined in the KV store, if any, and exclude them from the gem port id pool
if reservedGemPortIds, defined := PONRMgr.getReservedGemPortIdsFromKVStore(ctx); defined {
excluded = reservedGemPortIds
- logger.Debugw("Excluding some ports from GEM port id pool", log.Fields{"excluded gem ports": excluded})
+ logger.Debugw(ctx, "Excluding some ports from GEM port id pool", log.Fields{"excluded gem ports": excluded})
}
}
- FormatResult, err := PONRMgr.FormatResource(Intf, StartID, EndID, excluded)
+ FormatResult, err := PONRMgr.FormatResource(ctx, Intf, StartID, EndID, excluded)
if err != nil {
- logger.Errorf("Failed to format resource")
+ logger.Errorf(ctx, "Failed to format resource")
return err
}
// Add resource as json in kv store.
err = PONRMgr.KVStore.Put(ctx, Path, FormatResult)
if err == nil {
- logger.Debug("Successfuly posted to kv store")
+ logger.Debug(ctx, "Successfuly posted to kv store")
return err
}
}
- logger.Debug("Error initializing pool")
+ logger.Debug(ctx, "Error initializing pool")
return err
}
@@ -548,7 +549,7 @@
// read reserved gem ports from the config path
KvPair, err := PONRMgr.KVStoreForConfig.Get(ctx, RESERVED_GEMPORT_IDS_PATH)
if err != nil {
- logger.Errorw("Unable to get reserved GEM port ids from the kv store", log.Fields{"err": err})
+ logger.Errorw(ctx, "Unable to get reserved GEM port ids from the kv store", log.Fields{"err": err})
return reservedGemPortIds, false
}
if KvPair == nil || KvPair.Value == nil {
@@ -557,17 +558,17 @@
}
Val, err := kvstore.ToByte(KvPair.Value)
if err != nil {
- logger.Errorw("Failed to convert reserved gem port ids into byte array", log.Fields{"err": err})
+ logger.Errorw(ctx, "Failed to convert reserved gem port ids into byte array", log.Fields{"err": err})
return reservedGemPortIds, false
}
if err = json.Unmarshal(Val, &reservedGemPortIds); err != nil {
- logger.Errorw("Failed to unmarshal reservedGemPortIds", log.Fields{"err": err})
+ logger.Errorw(ctx, "Failed to unmarshal reservedGemPortIds", log.Fields{"err": err})
return reservedGemPortIds, false
}
return reservedGemPortIds, true
}
-func (PONRMgr *PONResourceManager) FormatResource(IntfID uint32, StartIDx uint32, EndIDx uint32,
+func (PONRMgr *PONResourceManager) FormatResource(ctx context.Context, IntfID uint32, StartIDx uint32, EndIDx uint32,
Excluded []uint32) ([]byte, error) {
/*
Format resource as json.
@@ -589,22 +590,22 @@
*/
var TSData *bitmap.Threadsafe
if TSData = bitmap.NewTS(int(EndIDx)); TSData == nil {
- logger.Error("Failed to create a bitmap")
+ logger.Error(ctx, "Failed to create a bitmap")
return nil, errors.New("Failed to create bitmap")
}
for _, excludedID := range Excluded {
if excludedID < StartIDx || excludedID > EndIDx {
- logger.Warnf("Cannot reserve %d. It must be in the range of [%d, %d]", excludedID,
+ logger.Warnf(ctx, "Cannot reserve %d. It must be in the range of [%d, %d]", excludedID,
StartIDx, EndIDx)
continue
}
- PONRMgr.reserveID(TSData, StartIDx, excludedID)
+ PONRMgr.reserveID(ctx, TSData, StartIDx, excludedID)
}
Resource[POOL] = TSData.Data(false) //we pass false so as the TSData lib api does not do a copy of the data and return
Value, err := json.Marshal(Resource)
if err != nil {
- logger.Errorf("Failed to marshall resource")
+ logger.Errorf(ctx, "Failed to marshall resource")
return nil, err
}
return Value, err
@@ -624,7 +625,7 @@
Resource, err := PONRMgr.KVStore.Get(ctx, Path)
if (err != nil) || (Resource == nil) {
- logger.Debugf("Resource unavailable at %s", Path)
+ logger.Debugf(ctx, "Resource unavailable at %s", Path)
return nil, err
}
@@ -636,7 +637,7 @@
// decode resource fetched from backend store to dictionary
err = json.Unmarshal(Value, &Result)
if err != nil {
- logger.Error("Failed to decode resource")
+ logger.Error(ctx, "Failed to decode resource")
return Result, err
}
/*
@@ -646,20 +647,20 @@
*/
Str, err = ToString(Result[POOL])
if err != nil {
- logger.Error("Failed to conver to kv pair to string")
+ logger.Error(ctx, "Failed to conver to kv pair to string")
return Result, err
}
Decode64, _ := base64.StdEncoding.DecodeString(Str)
Result[POOL], err = ToByte(Decode64)
if err != nil {
- logger.Error("Failed to convert resource pool to byte")
+ logger.Error(ctx, "Failed to convert resource pool to byte")
return Result, err
}
return Result, err
}
-func (PONRMgr *PONResourceManager) GetPath(IntfID uint32, ResourceType string) string {
+func (PONRMgr *PONResourceManager) GetPath(ctx context.Context, IntfID uint32, ResourceType string) string {
/*
Get path for given resource type.
:param pon_intf_id: OLT PON interface id
@@ -685,7 +686,7 @@
} else if ResourceType == FLOW_ID {
Path = fmt.Sprintf(FLOW_ID_POOL_PATH, PONRMgr.DeviceID, IntfID)
} else {
- logger.Error("Invalid resource pool identifier")
+ logger.Error(ctx, "Invalid resource pool identifier")
}
return Path
}
@@ -700,7 +701,7 @@
alloc_id/gemport_id, onu_id or invalid type respectively
*/
if NumIDs < 1 {
- logger.Error("Invalid number of resources requested")
+ logger.Error(ctx, "Invalid number of resources requested")
return nil, fmt.Errorf("Invalid number of resources requested %d", NumIDs)
}
// delegate to the master instance if sharing enabled across instances
@@ -709,34 +710,34 @@
if SharedResourceMgr != nil && PONRMgr != SharedResourceMgr {
return SharedResourceMgr.GetResourceID(ctx, IntfID, ResourceType, NumIDs)
}
- logger.Debugf("Fetching resource from %s rsrc mgr for resource %s", PONRMgr.Globalorlocal, ResourceType)
+ logger.Debugf(ctx, "Fetching resource from %s rsrc mgr for resource %s", PONRMgr.Globalorlocal, ResourceType)
- Path := PONRMgr.GetPath(IntfID, ResourceType)
+ Path := PONRMgr.GetPath(ctx, IntfID, ResourceType)
if Path == "" {
- logger.Errorf("Failed to get path for resource type %s", ResourceType)
+ logger.Errorf(ctx, "Failed to get path for resource type %s", ResourceType)
return nil, fmt.Errorf("Failed to get path for resource type %s", ResourceType)
}
- logger.Debugf("Get resource for type %s on path %s", ResourceType, Path)
+ logger.Debugf(ctx, "Get resource for type %s on path %s", ResourceType, Path)
var Result []uint32
var NextID uint32
Resource, err := PONRMgr.GetResource(ctx, Path)
if (err == nil) && (ResourceType == ONU_ID) || (ResourceType == FLOW_ID) {
- if NextID, err = PONRMgr.GenerateNextID(Resource); err != nil {
- logger.Error("Failed to Generate ID")
+ if NextID, err = PONRMgr.GenerateNextID(ctx, Resource); err != nil {
+ logger.Error(ctx, "Failed to Generate ID")
return Result, err
}
Result = append(Result, NextID)
} else if (err == nil) && ((ResourceType == GEMPORT_ID) || (ResourceType == ALLOC_ID)) {
if NumIDs == 1 {
- if NextID, err = PONRMgr.GenerateNextID(Resource); err != nil {
- logger.Error("Failed to Generate ID")
+ if NextID, err = PONRMgr.GenerateNextID(ctx, Resource); err != nil {
+ logger.Error(ctx, "Failed to Generate ID")
return Result, err
}
Result = append(Result, NextID)
} else {
for NumIDs > 0 {
- if NextID, err = PONRMgr.GenerateNextID(Resource); err != nil {
- logger.Error("Failed to Generate ID")
+ if NextID, err = PONRMgr.GenerateNextID(ctx, Resource); err != nil {
+ logger.Error(ctx, "Failed to Generate ID")
return Result, err
}
Result = append(Result, NextID)
@@ -744,13 +745,13 @@
}
}
} else {
- logger.Error("get resource failed")
+ logger.Error(ctx, "get resource failed")
return Result, err
}
//Update resource in kv store
if PONRMgr.UpdateResource(ctx, Path, Resource) != nil {
- logger.Errorf("Failed to update resource %s", Path)
+ logger.Errorf(ctx, "Failed to update resource %s", Path)
return nil, fmt.Errorf("Failed to update resource %s", Path)
}
return Result, nil
@@ -776,11 +777,11 @@
:return boolean: True if all IDs in given release_content release else False
*/
if !checkValidResourceType(ResourceType) {
- logger.Error("Invalid resource type")
+ logger.Error(ctx, "Invalid resource type")
return false
}
if ReleaseContent == nil {
- logger.Debug("Nothing to release")
+ logger.Debug(ctx, "Nothing to release")
return true
}
// delegate to the master instance if sharing enabled across instances
@@ -788,21 +789,21 @@
if SharedResourceMgr != nil && PONRMgr != SharedResourceMgr {
return SharedResourceMgr.FreeResourceID(ctx, IntfID, ResourceType, ReleaseContent)
}
- Path := PONRMgr.GetPath(IntfID, ResourceType)
+ Path := PONRMgr.GetPath(ctx, IntfID, ResourceType)
if Path == "" {
- logger.Error("Failed to get path")
+ logger.Error(ctx, "Failed to get path")
return false
}
Resource, err := PONRMgr.GetResource(ctx, Path)
if err != nil {
- logger.Error("Failed to get resource")
+ logger.Error(ctx, "Failed to get resource")
return false
}
for _, Val := range ReleaseContent {
- PONRMgr.ReleaseID(Resource, Val)
+ PONRMgr.ReleaseID(ctx, Resource, Val)
}
if PONRMgr.UpdateResource(ctx, Path, Resource) != nil {
- logger.Errorf("Free resource for %s failed", Path)
+ logger.Errorf(ctx, "Free resource for %s failed", Path)
return false
}
return true
@@ -818,12 +819,12 @@
// TODO resource[POOL] = resource[POOL].bin
Value, err := json.Marshal(Resource)
if err != nil {
- logger.Error("failed to Marshal")
+ logger.Error(ctx, "failed to Marshal")
return err
}
err = PONRMgr.KVStore.Put(ctx, Path, Value)
if err != nil {
- logger.Error("failed to put data to kv store %s", Path)
+ logger.Error(ctx, "failed to put data to kv store %s", Path)
return err
}
return nil
@@ -840,17 +841,17 @@
if SharedResourceMgr != nil && PONRMgr != SharedResourceMgr {
return SharedResourceMgr.ClearResourceIDPool(ctx, contIntfID, ResourceType)
}
- Path := PONRMgr.GetPath(contIntfID, ResourceType)
+ Path := PONRMgr.GetPath(ctx, contIntfID, ResourceType)
if Path == "" {
- logger.Error("Failed to get path")
+ logger.Error(ctx, "Failed to get path")
return false
}
if err := PONRMgr.KVStore.Delete(ctx, Path); err != nil {
- logger.Errorf("Failed to delete resource %s", Path)
+ logger.Errorf(ctx, "Failed to delete resource %s", Path)
return false
}
- logger.Debugf("Cleared resource %s", Path)
+ logger.Debugf(ctx, "Cleared resource %s", Path)
return true
}
@@ -864,7 +865,7 @@
var AllocIDs []byte
Result := PONRMgr.KVStore.Put(ctx, AllocIDPath, AllocIDs)
if Result != nil {
- logger.Error("Failed to update the KV store")
+ logger.Error(ctx, "Failed to update the KV store")
return
}
// initialize pon_intf_onu_id tuple to gemport_ids map
@@ -872,7 +873,7 @@
var GEMPortIDs []byte
Result = PONRMgr.KVStore.Put(ctx, GEMPortIDPath, GEMPortIDs)
if Result != nil {
- logger.Error("Failed to update the KV store")
+ logger.Error(ctx, "Failed to update the KV store")
return
}
}
@@ -886,14 +887,14 @@
var err error
AllocIDPath := fmt.Sprintf(ALLOC_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, PONIntfONUID)
if err = PONRMgr.KVStore.Delete(ctx, AllocIDPath); err != nil {
- logger.Errorf("Failed to remove resource %s", AllocIDPath)
+ logger.Errorf(ctx, "Failed to remove resource %s", AllocIDPath)
return false
}
// remove pon_intf_onu_id tuple to gemport_ids map
GEMPortIDPath := fmt.Sprintf(GEMPORT_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, PONIntfONUID)
err = PONRMgr.KVStore.Delete(ctx, GEMPortIDPath)
if err != nil {
- logger.Errorf("Failed to remove resource %s", GEMPortIDPath)
+ logger.Errorf(ctx, "Failed to remove resource %s", GEMPortIDPath)
return false
}
@@ -902,14 +903,14 @@
for _, Flow := range FlowIDs {
FlowIDInfoPath := fmt.Sprintf(FLOW_ID_INFO_PATH, PONRMgr.DeviceID, PONIntfONUID, Flow.Value)
if err = PONRMgr.KVStore.Delete(ctx, FlowIDInfoPath); err != nil {
- logger.Errorf("Failed to remove resource %s", FlowIDInfoPath)
+ logger.Errorf(ctx, "Failed to remove resource %s", FlowIDInfoPath)
return false
}
}
}
if err = PONRMgr.KVStore.Delete(ctx, FlowIDPath); err != nil {
- logger.Errorf("Failed to remove resource %s", FlowIDPath)
+ logger.Errorf(ctx, "Failed to remove resource %s", FlowIDPath)
return false
}
@@ -930,11 +931,11 @@
if Value != nil {
Val, err := ToByte(Value.Value)
if err != nil {
- logger.Errorw("Failed to convert into byte array", log.Fields{"error": err})
+ logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"error": err})
return Data
}
if err = json.Unmarshal(Val, &Data); err != nil {
- logger.Error("Failed to unmarshal", log.Fields{"error": err})
+ logger.Error(ctx, "Failed to unmarshal", log.Fields{"error": err})
return Data
}
}
@@ -950,19 +951,19 @@
*/
Path := fmt.Sprintf(GEMPORT_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID)
- logger.Debugf("Getting current gemports for %s", Path)
+ logger.Debugf(ctx, "Getting current gemports for %s", Path)
var Data []uint32
Value, err := PONRMgr.KVStore.Get(ctx, Path)
if err == nil {
if Value != nil {
Val, _ := ToByte(Value.Value)
if err = json.Unmarshal(Val, &Data); err != nil {
- logger.Errorw("Failed to unmarshal", log.Fields{"error": err})
+ logger.Errorw(ctx, "Failed to unmarshal", log.Fields{"error": err})
return Data
}
}
} else {
- logger.Errorf("Failed to get data from kvstore for %s", Path)
+ logger.Errorf(ctx, "Failed to get data from kvstore for %s", Path)
}
return Data
}
@@ -982,7 +983,7 @@
if Value != nil {
Val, _ := ToByte(Value.Value)
if err = json.Unmarshal(Val, &Data); err != nil {
- logger.Error("Failed to unmarshal")
+ logger.Error(ctx, "Failed to unmarshal")
return Data
}
}
@@ -1006,11 +1007,11 @@
if Value != nil {
Val, err := ToByte(Value.Value)
if err != nil {
- logger.Errorw("Failed to convert flowinfo into byte array", log.Fields{"error": err})
+ logger.Errorw(ctx, "Failed to convert flowinfo into byte array", log.Fields{"error": err})
return err
}
if err = json.Unmarshal(Val, Data); err != nil {
- logger.Errorw("Failed to unmarshal", log.Fields{"error": err})
+ logger.Errorw(ctx, "Failed to unmarshal", log.Fields{"error": err})
return err
}
}
@@ -1027,7 +1028,7 @@
Path := fmt.Sprintf(FLOW_ID_INFO_PATH, PONRMgr.DeviceID, IntfONUID, FlowID)
if err := PONRMgr.KVStore.Delete(ctx, Path); err != nil {
- logger.Errorf("Falied to remove resource %s", Path)
+ logger.Errorf(ctx, "Falied to remove resource %s", Path)
return false
}
return true
@@ -1044,12 +1045,12 @@
Path := fmt.Sprintf(ALLOC_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID)
Value, err = json.Marshal(AllocIDs)
if err != nil {
- logger.Error("failed to Marshal")
+ logger.Error(ctx, "failed to Marshal")
return err
}
if err = PONRMgr.KVStore.Put(ctx, Path, Value); err != nil {
- logger.Errorf("Failed to update resource %s", Path)
+ logger.Errorf(ctx, "Failed to update resource %s", Path)
return err
}
return err
@@ -1065,15 +1066,15 @@
var Value []byte
var err error
Path := fmt.Sprintf(GEMPORT_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID)
- logger.Debugf("Updating gemport ids for %s", Path)
+ logger.Debugf(ctx, "Updating gemport ids for %s", Path)
Value, err = json.Marshal(GEMPortIDs)
if err != nil {
- logger.Error("failed to Marshal")
+ logger.Error(ctx, "failed to Marshal")
return err
}
if err = PONRMgr.KVStore.Put(ctx, Path, Value); err != nil {
- logger.Errorf("Failed to update resource %s", Path)
+ logger.Errorf(ctx, "Failed to update resource %s", Path)
return err
}
return err
@@ -1124,12 +1125,12 @@
}
Value, err = json.Marshal(FlowIDs)
if err != nil {
- logger.Error("Failed to Marshal")
+ logger.Error(ctx, "Failed to Marshal")
return err
}
if err = PONRMgr.KVStore.Put(ctx, Path, Value); err != nil {
- logger.Errorf("Failed to update resource %s", Path)
+ logger.Errorf(ctx, "Failed to update resource %s", Path)
return err
}
return err
@@ -1148,18 +1149,18 @@
Path := fmt.Sprintf(FLOW_ID_INFO_PATH, PONRMgr.DeviceID, IntfONUID, FlowID)
Value, err = json.Marshal(FlowData)
if err != nil {
- logger.Error("failed to Marshal")
+ logger.Error(ctx, "failed to Marshal")
return err
}
if err = PONRMgr.KVStore.Put(ctx, Path, Value); err != nil {
- logger.Errorf("Failed to update resource %s", Path)
+ logger.Errorf(ctx, "Failed to update resource %s", Path)
return err
}
return err
}
-func (PONRMgr *PONResourceManager) GenerateNextID(Resource map[string]interface{}) (uint32, error) {
+func (PONRMgr *PONResourceManager) GenerateNextID(ctx context.Context, Resource map[string]interface{}) (uint32, error) {
/*
Generate unique id having OFFSET as start
:param resource: resource used to generate ID
@@ -1167,12 +1168,12 @@
*/
ByteArray, err := ToByte(Resource[POOL])
if err != nil {
- logger.Error("Failed to convert resource to byte array")
+ logger.Error(ctx, "Failed to convert resource to byte array")
return 0, err
}
Data := bitmap.TSFromData(ByteArray, false)
if Data == nil {
- logger.Error("Failed to get data from byte array")
+ logger.Error(ctx, "Failed to get data from byte array")
return 0, errors.New("Failed to get data from byte array")
}
@@ -1186,11 +1187,11 @@
Data.Set(Idx, true)
res := uint32(Resource[START_IDX].(float64))
Resource[POOL] = Data.Data(false)
- logger.Debugf("Generated ID for %d", (uint32(Idx) + res))
+ logger.Debugf(ctx, "Generated ID for %d", (uint32(Idx) + res))
return (uint32(Idx) + res), err
}
-func (PONRMgr *PONResourceManager) ReleaseID(Resource map[string]interface{}, Id uint32) bool {
+func (PONRMgr *PONResourceManager) ReleaseID(ctx context.Context, Resource map[string]interface{}, Id uint32) bool {
/*
Release unique id having OFFSET as start index.
:param resource: resource used to release ID
@@ -1198,12 +1199,12 @@
*/
ByteArray, err := ToByte(Resource[POOL])
if err != nil {
- logger.Error("Failed to convert resource to byte array")
+ logger.Error(ctx, "Failed to convert resource to byte array")
return false
}
Data := bitmap.TSFromData(ByteArray, false)
if Data == nil {
- logger.Error("Failed to get resource pool")
+ logger.Error(ctx, "Failed to get resource pool")
return false
}
Idx := Id - uint32(Resource[START_IDX].(float64))
@@ -1217,10 +1218,10 @@
:param Resource: resource used to reserve ID
:param Id: ID to be reserved
*/
-func (PONRMgr *PONResourceManager) reserveID(TSData *bitmap.Threadsafe, StartIndex uint32, Id uint32) bool {
+func (PONRMgr *PONResourceManager) reserveID(ctx context.Context, TSData *bitmap.Threadsafe, StartIndex uint32, Id uint32) bool {
Data := bitmap.TSFromData(TSData.Data(false), false)
if Data == nil {
- logger.Error("Failed to get resource pool")
+ logger.Error(ctx, "Failed to get resource pool")
return false
}
Idx := Id - StartIndex
@@ -1277,12 +1278,12 @@
Path := fmt.Sprintf(ONU_GEM_INFO_PATH, PONRMgr.DeviceID, intfID)
Value, err = json.Marshal(onuGemData)
if err != nil {
- logger.Error("failed to Marshal")
+ logger.Error(ctx, "failed to Marshal")
return err
}
if err = PONRMgr.KVStore.Put(ctx, Path, Value); err != nil {
- logger.Errorf("Failed to update resource %s", Path)
+ logger.Errorf(ctx, "Failed to update resource %s", Path)
return err
}
return err
@@ -1299,22 +1300,22 @@
path := fmt.Sprintf(ONU_GEM_INFO_PATH, PONRMgr.DeviceID, IntfId)
value, err := PONRMgr.KVStore.Get(ctx, path)
if err != nil {
- logger.Errorw("Failed to get from kv store", log.Fields{"path": path})
+ logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
return err
} else if value == nil {
- logger.Debug("No onuinfo for path", log.Fields{"path": path})
+ logger.Debug(ctx, "No onuinfo for path", log.Fields{"path": path})
return nil // returning nil as this could happen if there are no onus for the interface yet
}
if Val, err = kvstore.ToByte(value.Value); err != nil {
- logger.Error("Failed to convert to byte array")
+ logger.Error(ctx, "Failed to convert to byte array")
return err
}
if err = json.Unmarshal(Val, &onuGemInfo); err != nil {
- logger.Error("Failed to unmarshall")
+ logger.Error(ctx, "Failed to unmarshall")
return err
}
- logger.Debugw("found onuinfo from path", log.Fields{"path": path, "onuinfo": onuGemInfo})
+ logger.Debugw(ctx, "found onuinfo from path", log.Fields{"path": path, "onuinfo": onuGemInfo})
return err
}
@@ -1326,7 +1327,7 @@
path := fmt.Sprintf(ONU_GEM_INFO_PATH, PONRMgr.DeviceID, intfId)
if err := PONRMgr.KVStore.Delete(ctx, path); err != nil {
- logger.Errorf("Falied to remove resource %s", path)
+ logger.Errorf(ctx, "Falied to remove resource %s", path)
return err
}
return nil
diff --git a/pkg/ponresourcemanager/ponresourcemanager_test.go b/pkg/ponresourcemanager/ponresourcemanager_test.go
index b91107c..0eab36e 100644
--- a/pkg/ponresourcemanager/ponresourcemanager_test.go
+++ b/pkg/ponresourcemanager/ponresourcemanager_test.go
@@ -39,7 +39,7 @@
resourceMap map[string]interface{}
}
-func newMockKvClient() *MockResKVClient {
+func newMockKvClient(ctx context.Context) *MockResKVClient {
var mockResKVClient MockResKVClient
mockResKVClient.resourceMap = make(map[string]interface{})
return &mockResKVClient
@@ -52,16 +52,16 @@
// Get mock function implementation for KVClient
func (kvclient *MockResKVClient) Get(ctx context.Context, key string) (*kvstore.KVPair, error) {
- logger.Debugw("Get of MockKVClient called", log.Fields{"key": key})
+ logger.Debugw(ctx, "Get of MockKVClient called", log.Fields{"key": key})
if key != "" {
if strings.Contains(key, RESERVED_GEMPORT_IDS_PATH) {
- logger.Debug("Getting Key:", RESERVED_GEMPORT_IDS_PATH)
+ logger.Debug(ctx, "Getting Key:", RESERVED_GEMPORT_IDS_PATH)
reservedGemPorts := []uint32{RESERVED_GEM_PORT_ID}
str, _ := json.Marshal(reservedGemPorts)
return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
}
if strings.Contains(key, GEM_POOL_PATH) {
- logger.Debug("Getting Key:", GEM_POOL_PATH)
+ logger.Debug(ctx, "Getting Key:", GEM_POOL_PATH)
resource := kvclient.resourceMap[key]
return kvstore.NewKVPair(key, resource, "mock", 3000, 1), nil
}
@@ -129,38 +129,38 @@
}
// CloseWatch mock function implementation for KVClient
-func (kvclient *MockResKVClient) CloseWatch(key string, ch chan *kvstore.Event) {
+func (kvclient *MockResKVClient) CloseWatch(ctx context.Context, key string, ch chan *kvstore.Event) {
}
// Close mock function implementation for KVClient
-func (kvclient *MockResKVClient) Close() {
+func (kvclient *MockResKVClient) Close(ctx context.Context) {
}
func TestExcludeReservedGemPortIdFromThePool(t *testing.T) {
- PONRMgr, err := NewPONResourceManager("gpon", "onu", "olt1",
+ ctx := context.Background()
+ PONRMgr, err := NewPONResourceManager(ctx, "gpon", "onu", "olt1",
"etcd", "1:1")
if err != nil {
return
}
PONRMgr.KVStore = &db.Backend{
- Client: newMockKvClient(),
+ Client: newMockKvClient(ctx),
}
PONRMgr.KVStoreForConfig = &db.Backend{
- Client: newMockKvClient(),
+ Client: newMockKvClient(ctx),
}
// create a pool in the range of [1,16]
// and exclude id 5 from this pool
StartIndex := uint32(1)
EndIndex := uint32(16)
- ctx := context.Background()
reservedGemPortIds, defined := PONRMgr.getReservedGemPortIdsFromKVStore(ctx)
if !defined {
return
}
- FormatResult, err := PONRMgr.FormatResource(1, StartIndex, EndIndex, reservedGemPortIds)
+ FormatResult, err := PONRMgr.FormatResource(ctx, 1, StartIndex, EndIndex, reservedGemPortIds)
if err != nil {
t.Error("Failed to format resource", err)
return
@@ -181,7 +181,7 @@
return
}
// get a gem port id from the pool
- nextID, err := PONRMgr.GenerateNextID(resource)
+ nextID, err := PONRMgr.GenerateNextID(ctx, resource)
if err != nil {
t.Error("Failed to get gem port id from the pool", err)
return
diff --git a/pkg/probe/common.go b/pkg/probe/common.go
index 211419d..14857ab 100644
--- a/pkg/probe/common.go
+++ b/pkg/probe/common.go
@@ -19,12 +19,12 @@
"github.com/opencord/voltha-lib-go/v3/pkg/log"
)
-var logger log.Logger
+var logger log.CLogger
func init() {
// Setup this package so that it's log level can be modified at run time
var err error
- logger, err = log.AddPackage(log.JSON, log.ErrorLevel, log.Fields{"pkg": "probe"})
+ logger, err = log.RegisterPackage(log.JSON, log.ErrorLevel, log.Fields{"pkg": "probe"})
if err != nil {
panic(err)
}
diff --git a/pkg/probe/probe.go b/pkg/probe/probe.go
index e89d5bc..732d6df 100644
--- a/pkg/probe/probe.go
+++ b/pkg/probe/probe.go
@@ -109,7 +109,7 @@
}
// RegisterService register one or more service names with the probe, status will be track against service name
-func (p *Probe) RegisterService(names ...string) {
+func (p *Probe) RegisterService(ctx context.Context, names ...string) {
p.mutex.Lock()
defer p.mutex.Unlock()
if p.status == nil {
@@ -118,7 +118,7 @@
for _, name := range names {
if _, ok := p.status[name]; !ok {
p.status[name] = ServiceStatusUnknown
- logger.Debugw("probe-service-registered", log.Fields{"service-name": name})
+ logger.Debugw(ctx, "probe-service-registered", log.Fields{"service-name": name})
}
}
@@ -136,7 +136,7 @@
}
// UpdateStatus utility function to send a service update to the probe
-func (p *Probe) UpdateStatus(name string, status ServiceStatus) {
+func (p *Probe) UpdateStatus(ctx context.Context, name string, status ServiceStatus) {
p.mutex.Lock()
defer p.mutex.Unlock()
if p.status == nil {
@@ -161,7 +161,7 @@
} else {
p.isHealthy = defaultHealthFunc(p.status)
}
- logger.Debugw("probe-service-status-updated",
+ logger.Debugw(ctx, "probe-service-status-updated",
log.Fields{
"service-name": name,
"status": status.String(),
@@ -204,7 +204,7 @@
func UpdateStatusFromContext(ctx context.Context, name string, status ServiceStatus) {
p := GetProbeFromContext(ctx)
if p != nil {
- p.UpdateStatus(name, status)
+ p.UpdateStatus(ctx, name, status)
}
}
@@ -228,25 +228,26 @@
}
}
func (p *Probe) detailzFunc(w http.ResponseWriter, req *http.Request) {
+ ctx := context.Background()
p.mutex.RLock()
defer p.mutex.RUnlock()
w.Header().Set("Content-Type", "application/json")
if _, err := w.Write([]byte("{")); err != nil {
- logger.Errorw("write-response", log.Fields{"error": err})
+ logger.Errorw(ctx, "write-response", log.Fields{"error": err})
w.WriteHeader(http.StatusInternalServerError)
return
}
comma := ""
for c, s := range p.status {
if _, err := w.Write([]byte(fmt.Sprintf("%s\"%s\": \"%s\"", comma, c, s.String()))); err != nil {
- logger.Errorw("write-response", log.Fields{"error": err})
+ logger.Errorw(ctx, "write-response", log.Fields{"error": err})
w.WriteHeader(http.StatusInternalServerError)
return
}
comma = ", "
}
if _, err := w.Write([]byte("}")); err != nil {
- logger.Errorw("write-response", log.Fields{"error": err})
+ logger.Errorw(ctx, "write-response", log.Fields{"error": err})
w.WriteHeader(http.StatusInternalServerError)
return
}
@@ -254,7 +255,7 @@
}
// ListenAndServe implements 3 HTTP endpoints on the given port for healthz, readz, and detailz. Returns only on error
-func (p *Probe) ListenAndServe(address string) {
+func (p *Probe) ListenAndServe(ctx context.Context, address string) {
mux := http.NewServeMux()
// Returns the result of the readyFunc calculation
@@ -269,7 +270,7 @@
Addr: address,
Handler: mux,
}
- logger.Fatal(s.ListenAndServe())
+ logger.Fatal(ctx, s.ListenAndServe())
}
func (p *Probe) IsReady() bool {
diff --git a/pkg/probe/probe_test.go b/pkg/probe/probe_test.go
index 77cd6a8..cae17e5 100644
--- a/pkg/probe/probe_test.go
+++ b/pkg/probe/probe_test.go
@@ -72,7 +72,7 @@
func TestRegisterOneService(t *testing.T) {
p := &Probe{}
- p.RegisterService("one")
+ p.RegisterService(context.Background(), "one")
assert.Equal(t, 1, len(p.status), "wrong number of services")
@@ -83,7 +83,7 @@
func TestRegisterMultipleServices(t *testing.T) {
p := &Probe{}
- p.RegisterService("one", "two", "three", "four")
+ p.RegisterService(context.Background(), "one", "two", "three", "four")
assert.Equal(t, 4, len(p.status), "wrong number of services")
@@ -99,10 +99,10 @@
func TestRegisterMultipleServicesIncremental(t *testing.T) {
p := &Probe{}
-
- p.RegisterService("one")
- p.RegisterService("two")
- p.RegisterService("three", "four")
+ ctx := context.Background()
+ p.RegisterService(ctx, "one")
+ p.RegisterService(ctx, "two")
+ p.RegisterService(ctx, "three", "four")
assert.Equal(t, 4, len(p.status), "wrong number of services")
@@ -119,7 +119,7 @@
func TestRegisterMultipleServicesDuplicates(t *testing.T) {
p := &Probe{}
- p.RegisterService("one", "one", "one", "two")
+ p.RegisterService(context.Background(), "one", "one", "one", "two")
assert.Equal(t, 2, len(p.status), "wrong number of services")
@@ -131,10 +131,10 @@
func TestRegisterMultipleServicesDuplicatesIncremental(t *testing.T) {
p := &Probe{}
-
- p.RegisterService("one")
- p.RegisterService("one")
- p.RegisterService("one", "two")
+ ctx := context.Background()
+ p.RegisterService(ctx, "one")
+ p.RegisterService(ctx, "one")
+ p.RegisterService(ctx, "one", "two")
assert.Equal(t, 2, len(p.status), "wrong number of services")
@@ -146,9 +146,9 @@
func TestUpdateStatus(t *testing.T) {
p := &Probe{}
-
- p.RegisterService("one", "two")
- p.UpdateStatus("one", ServiceStatusRunning)
+ ctx := context.Background()
+ p.RegisterService(ctx, "one", "two")
+ p.UpdateStatus(ctx, "one", ServiceStatusRunning)
assert.Equal(t, ServiceStatusRunning, p.status["one"], "status not set")
assert.Equal(t, ServiceStatusUnknown, p.status["two"], "status set")
@@ -156,14 +156,14 @@
func TestRegisterOverwriteStatus(t *testing.T) {
p := &Probe{}
-
- p.RegisterService("one", "two")
- p.UpdateStatus("one", ServiceStatusRunning)
+ ctx := context.Background()
+ p.RegisterService(ctx, "one", "two")
+ p.UpdateStatus(ctx, "one", ServiceStatusRunning)
assert.Equal(t, ServiceStatusRunning, p.status["one"], "status not set")
assert.Equal(t, ServiceStatusUnknown, p.status["two"], "status set")
- p.RegisterService("one", "three")
+ p.RegisterService(ctx, "one", "three")
assert.Equal(t, 3, len(p.status), "wrong number of services")
assert.Equal(t, ServiceStatusRunning, p.status["one"], "status overridden")
assert.Equal(t, ServiceStatusUnknown, p.status["two"], "status set")
@@ -172,7 +172,7 @@
func TestDetailzWithServies(t *testing.T) {
p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysTrue)
- p.RegisterService("one", "two")
+ p.RegisterService(context.Background(), "one", "two")
req := httptest.NewRequest("GET", "http://example.com/detailz", nil)
w := httptest.NewRecorder()
@@ -201,7 +201,7 @@
func TestReadzWithServicesWithTrue(t *testing.T) {
p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysTrue)
- p.RegisterService("one", "two")
+ p.RegisterService(context.Background(), "one", "two")
req := httptest.NewRequest("GET", "http://example.com/readz", nil)
w := httptest.NewRecorder()
@@ -212,7 +212,7 @@
func TestReadzWithServicesWithDefault(t *testing.T) {
p := &Probe{}
- p.RegisterService("one", "two")
+ p.RegisterService(context.Background(), "one", "two")
req := httptest.NewRequest("GET", "http://example.com/readz", nil)
w := httptest.NewRecorder()
@@ -233,9 +233,10 @@
func TestReadzWithServicesDefault(t *testing.T) {
p := &Probe{}
- p.RegisterService("one", "two")
- p.UpdateStatus("one", ServiceStatusRunning)
- p.UpdateStatus("two", ServiceStatusRunning)
+ ctx := context.Background()
+ p.RegisterService(ctx, "one", "two")
+ p.UpdateStatus(ctx, "one", ServiceStatusRunning)
+ p.UpdateStatus(ctx, "two", ServiceStatusRunning)
req := httptest.NewRequest("GET", "http://example.com/readz", nil)
w := httptest.NewRecorder()
@@ -246,8 +247,8 @@
func TestReadzWithServicesDefaultOne(t *testing.T) {
p := &Probe{}
- p.RegisterService("one", "two")
- p.UpdateStatus("one", ServiceStatusRunning)
+ p.RegisterService(context.Background(), "one", "two")
+ p.UpdateStatus(context.Background(), "one", ServiceStatusRunning)
req := httptest.NewRequest("GET", "http://example.com/readz", nil)
w := httptest.NewRecorder()
@@ -268,7 +269,7 @@
func TestHealthzWithServicesWithTrue(t *testing.T) {
p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysTrue)
- p.RegisterService("one", "two")
+ p.RegisterService(context.Background(), "one", "two")
req := httptest.NewRequest("GET", "http://example.com/healthz", nil)
w := httptest.NewRecorder()
@@ -279,7 +280,7 @@
func TestHealthzWithServicesWithDefault(t *testing.T) {
p := &Probe{}
- p.RegisterService("one", "two")
+ p.RegisterService(context.Background(), "one", "two")
req := httptest.NewRequest("GET", "http://example.com/healthz", nil)
w := httptest.NewRecorder()
@@ -300,9 +301,9 @@
func TestHealthzWithServicesDefault(t *testing.T) {
p := &Probe{}
- p.RegisterService("one", "two")
- p.UpdateStatus("one", ServiceStatusRunning)
- p.UpdateStatus("two", ServiceStatusRunning)
+ p.RegisterService(context.Background(), "one", "two")
+ p.UpdateStatus(context.Background(), "one", ServiceStatusRunning)
+ p.UpdateStatus(context.Background(), "two", ServiceStatusRunning)
req := httptest.NewRequest("GET", "http://example.com/healthz", nil)
w := httptest.NewRecorder()
@@ -313,8 +314,8 @@
func TestHealthzWithServicesDefaultFailed(t *testing.T) {
p := &Probe{}
- p.RegisterService("one", "two")
- p.UpdateStatus("one", ServiceStatusFailed)
+ p.RegisterService(context.Background(), "one", "two")
+ p.UpdateStatus(context.Background(), "one", ServiceStatusFailed)
req := httptest.NewRequest("GET", "http://example.com/healthz", nil)
w := httptest.NewRecorder()
@@ -332,7 +333,7 @@
func TestGetProbeFromContext(t *testing.T) {
p := &Probe{}
- p.RegisterService("one")
+ p.RegisterService(context.Background(), "one")
ctx := context.WithValue(context.Background(), ProbeContextKey, p)
pc := GetProbeFromContext(ctx)
assert.Equal(t, p, pc, "Probe from context was not identical to original probe")
@@ -347,7 +348,7 @@
func TestUpdateStatusFromContext(t *testing.T) {
p := &Probe{}
- p.RegisterService("one")
+ p.RegisterService(context.Background(), "one")
ctx := context.WithValue(context.Background(), ProbeContextKey, p)
UpdateStatusFromContext(ctx, "one", ServiceStatusRunning)
@@ -359,7 +360,7 @@
func TestUpdateStatusFromNilContext(t *testing.T) {
p := &Probe{}
- p.RegisterService("one")
+ p.RegisterService(context.Background(), "one")
// nolint: staticcheck
UpdateStatusFromContext(nil, "one", ServiceStatusRunning)
@@ -372,7 +373,7 @@
func TestUpdateStatusFromContextWithoutProbe(t *testing.T) {
p := &Probe{}
- p.RegisterService("one")
+ p.RegisterService(context.Background(), "one")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
UpdateStatusFromContext(ctx, "one", ServiceStatusRunning)
@@ -386,7 +387,7 @@
func TestUpdateStatusFromContextWrongType(t *testing.T) {
p := &Probe{}
- p.RegisterService("one")
+ p.RegisterService(context.Background(), "one")
ctx := context.WithValue(context.Background(), ProbeContextKey, "Teapot")
UpdateStatusFromContext(ctx, "one", ServiceStatusRunning)
@@ -399,7 +400,7 @@
func TestUpdateStatusNoRegistered(t *testing.T) {
p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysFalse)
- p.UpdateStatus("one", ServiceStatusRunning)
+ p.UpdateStatus(context.Background(), "one", ServiceStatusRunning)
assert.Equal(t, 1, len(p.status), "wrong number of services")
_, ok := p.status["one"]
assert.True(t, ok, "unable to find registered service")
@@ -409,7 +410,7 @@
func TestIsReadyTrue(t *testing.T) {
p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysFalse)
- p.RegisterService("SomeService")
+ p.RegisterService(context.Background(), "SomeService")
assert.True(t, p.IsReady(), "IsReady should have been true")
}
@@ -417,7 +418,7 @@
func TestIsReadyFalse(t *testing.T) {
p := (&Probe{}).WithReadyFunc(AlwaysFalse).WithHealthFunc(AlwaysFalse)
- p.RegisterService("SomeService")
+ p.RegisterService(context.Background(), "SomeService")
assert.False(t, p.IsReady(), "IsReady should have been false")
}
@@ -425,8 +426,8 @@
func TestGetStatus(t *testing.T) {
p := &Probe{}
- p.RegisterService("one", "two")
- p.UpdateStatus("one", ServiceStatusRunning)
+ p.RegisterService(context.Background(), "one", "two")
+ p.UpdateStatus(context.Background(), "one", ServiceStatusRunning)
ss := p.GetStatus("one")
assert.Equal(t, ServiceStatusRunning, ss, "Service status should have been ServiceStatusRunning")
@@ -435,7 +436,7 @@
func TestGetStatusMissingService(t *testing.T) {
p := &Probe{}
- p.RegisterService("one", "two")
+ p.RegisterService(context.Background(), "one", "two")
ss := p.GetStatus("three")
assert.Equal(t, ServiceStatusUnknown, ss, "Service status should have been ServiceStatusUnknown")
diff --git a/pkg/techprofile/common.go b/pkg/techprofile/common.go
index 42818f1..e7cd798 100644
--- a/pkg/techprofile/common.go
+++ b/pkg/techprofile/common.go
@@ -19,12 +19,12 @@
"github.com/opencord/voltha-lib-go/v3/pkg/log"
)
-var logger log.Logger
+var logger log.CLogger
func init() {
// Setup this package so that it's log level can be modified at run time
var err error
- logger, err = log.AddPackage(log.JSON, log.ErrorLevel, log.Fields{"pkg": "techprofile"})
+ logger, err = log.RegisterPackage(log.JSON, log.ErrorLevel, log.Fields{"pkg": "techprofile"})
if err != nil {
panic(err)
}
diff --git a/pkg/techprofile/tech_profile.go b/pkg/techprofile/tech_profile.go
index afe5c09..d876588 100644
--- a/pkg/techprofile/tech_profile.go
+++ b/pkg/techprofile/tech_profile.go
@@ -242,10 +242,10 @@
DownstreamGemPortAttributeList []iGemPortAttribute `json:"downstream_gem_port_attribute_list"`
}
-func (t *TechProfileMgr) SetKVClient() *db.Backend {
- kvClient, err := newKVClient(t.config.KVStoreType, t.config.KVStoreAddress, t.config.KVStoreTimeout)
+func (t *TechProfileMgr) SetKVClient(ctx context.Context) *db.Backend {
+ kvClient, err := newKVClient(ctx, t.config.KVStoreType, t.config.KVStoreAddress, t.config.KVStoreTimeout)
if err != nil {
- logger.Errorw("failed-to-create-kv-client",
+ logger.Errorw(ctx, "failed-to-create-kv-client",
log.Fields{
"type": t.config.KVStoreType, "address": t.config.KVStoreAddress,
"timeout": t.config.KVStoreTimeout, "prefix": t.config.TPKVPathPrefix,
@@ -267,34 +267,34 @@
*/
}
-func newKVClient(storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
+func newKVClient(ctx context.Context, storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
- logger.Infow("kv-store", log.Fields{"storeType": storeType, "address": address})
+ logger.Infow(ctx, "kv-store", log.Fields{"storeType": storeType, "address": address})
switch storeType {
case "consul":
- return kvstore.NewConsulClient(address, timeout)
+ return kvstore.NewConsulClient(ctx, address, timeout)
case "etcd":
- return kvstore.NewEtcdClient(address, timeout, log.WarnLevel)
+ return kvstore.NewEtcdClient(ctx, address, timeout, log.WarnLevel)
}
return nil, errors.New("unsupported-kv-store")
}
-func NewTechProfile(resourceMgr iPonResourceMgr, KVStoreType string, KVStoreAddress string) (*TechProfileMgr, error) {
+func NewTechProfile(ctx context.Context, resourceMgr iPonResourceMgr, KVStoreType string, KVStoreAddress string) (*TechProfileMgr, error) {
var techprofileObj TechProfileMgr
- logger.Debug("Initializing techprofile Manager")
+ logger.Debug(ctx, "Initializing techprofile Manager")
techprofileObj.config = NewTechProfileFlags(KVStoreType, KVStoreAddress)
- techprofileObj.config.KVBackend = techprofileObj.SetKVClient()
+ techprofileObj.config.KVBackend = techprofileObj.SetKVClient(ctx)
if techprofileObj.config.KVBackend == nil {
- logger.Error("Failed to initialize KV backend\n")
+ logger.Error(ctx, "Failed to initialize KV backend\n")
return nil, errors.New("KV backend init failed")
}
techprofileObj.resourceMgr = resourceMgr
- logger.Debug("Initializing techprofile object instance success")
+ logger.Debug(ctx, "Initializing techprofile object instance success")
return &techprofileObj, nil
}
-func (t *TechProfileMgr) GetTechProfileInstanceKVPath(techProfiletblID uint32, uniPortName string) string {
- logger.Debugw("get-tp-instance-kv-path", log.Fields{
+func (t *TechProfileMgr) GetTechProfileInstanceKVPath(ctx context.Context, techProfiletblID uint32, uniPortName string) string {
+ logger.Debugw(ctx, "get-tp-instance-kv-path", log.Fields{
"uniPortName": uniPortName,
"tpId": techProfiletblID,
})
@@ -307,16 +307,16 @@
var err error
var kvResult *kvstore.KVPair
- logger.Infow("get-tp-instance-form-kv-store", log.Fields{"path": path, "tpid": techProfiletblID})
+ logger.Infow(ctx, "get-tp-instance-form-kv-store", log.Fields{"path": path, "tpid": techProfiletblID})
kvResult, _ = t.config.KVBackend.Get(ctx, path)
if kvResult == nil {
- logger.Infow("tp-instance-not-found-on-kv", log.Fields{"key": path})
+ logger.Infow(ctx, "tp-instance-not-found-on-kv", log.Fields{"key": path})
return nil, nil
} else {
if value, err := kvstore.ToByte(kvResult.Value); err == nil {
if err = json.Unmarshal(value, resPtr); err != nil {
- logger.Errorw("error-unmarshal-kv-result", log.Fields{"key": path, "value": value})
+ logger.Errorw(ctx, "error-unmarshal-kv-result", log.Fields{"key": path, "value": value})
return nil, errors.New("error-unmarshal-kv-result")
} else {
return resPtr, nil
@@ -327,36 +327,36 @@
}
func (t *TechProfileMgr) addTechProfInstanceToKVStore(ctx context.Context, techProfiletblID uint32, uniPortName string, tpInstance *TechProfile) error {
- path := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
- logger.Debugw("Adding techprof instance to kvstore", log.Fields{"key": path, "tpinstance": tpInstance})
+ path := t.GetTechProfileInstanceKVPath(ctx, techProfiletblID, uniPortName)
+ logger.Debugw(ctx, "Adding techprof instance to kvstore", log.Fields{"key": path, "tpinstance": tpInstance})
tpInstanceJson, err := json.Marshal(*tpInstance)
if err == nil {
// Backend will convert JSON byte array into string format
- logger.Debugw("Storing tech profile instance to KV Store", log.Fields{"key": path, "val": tpInstanceJson})
+ logger.Debugw(ctx, "Storing tech profile instance to KV Store", log.Fields{"key": path, "val": tpInstanceJson})
err = t.config.KVBackend.Put(ctx, path, tpInstanceJson)
} else {
- logger.Errorw("Error in marshaling into Json format", log.Fields{"key": path, "tpinstance": tpInstance})
+ logger.Errorw(ctx, "Error in marshaling into Json format", log.Fields{"key": path, "tpinstance": tpInstance})
}
return err
}
func (t *TechProfileMgr) getTPFromKVStore(ctx context.Context, techProfiletblID uint32) *DefaultTechProfile {
var kvtechprofile DefaultTechProfile
key := fmt.Sprintf(t.config.TPFileKVPath, t.resourceMgr.GetTechnology(), techProfiletblID)
- logger.Debugw("Getting techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "Key": key})
+ logger.Debugw(ctx, "Getting techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "Key": key})
kvresult, err := t.config.KVBackend.Get(ctx, key)
if err != nil {
- logger.Errorw("Error while fetching value from KV store", log.Fields{"key": key})
+ logger.Errorw(ctx, "Error while fetching value from KV store", log.Fields{"key": key})
return nil
}
if kvresult != nil {
/* Backend will return Value in string format,needs to be converted to []byte before unmarshal*/
if value, err := kvstore.ToByte(kvresult.Value); err == nil {
if err = json.Unmarshal(value, &kvtechprofile); err != nil {
- logger.Errorw("Error unmarshaling techprofile fetched from KV store", log.Fields{"techProfiletblID": techProfiletblID, "error": err, "techprofile_json": value})
+ logger.Errorw(ctx, "Error unmarshaling techprofile fetched from KV store", log.Fields{"techProfiletblID": techProfiletblID, "error": err, "techprofile_json": value})
return nil
}
- logger.Debugw("Success fetched techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "value": kvtechprofile})
+ logger.Debugw(ctx, "Success fetched techprofile from KV store", log.Fields{"techProfiletblID": techProfiletblID, "value": kvtechprofile})
return &kvtechprofile
}
}
@@ -365,58 +365,58 @@
func (t *TechProfileMgr) CreateTechProfInstance(ctx context.Context, techProfiletblID uint32, uniPortName string, intfId uint32) (*TechProfile, error) {
var tpInstance *TechProfile
- logger.Infow("creating-tp-instance", log.Fields{"tableid": techProfiletblID, "uni": uniPortName, "intId": intfId})
+ logger.Infow(ctx, "creating-tp-instance", log.Fields{"tableid": techProfiletblID, "uni": uniPortName, "intId": intfId})
// Make sure the uniPortName is as per format pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}
if !uniPortNameFormat.Match([]byte(uniPortName)) {
- logger.Errorw("uni-port-name-not-confirming-to-format", log.Fields{"uniPortName": uniPortName})
+ logger.Errorw(ctx, "uni-port-name-not-confirming-to-format", log.Fields{"uniPortName": uniPortName})
return nil, errors.New("uni-port-name-not-confirming-to-format")
}
tp := t.getTPFromKVStore(ctx, techProfiletblID)
if tp != nil {
- if err := t.validateInstanceControlAttr(tp.InstanceCtrl); err != nil {
- logger.Error("invalid-instance-ctrl-attr--using-default-tp")
- tp = t.getDefaultTechProfile()
+ if err := t.validateInstanceControlAttr(ctx, tp.InstanceCtrl); err != nil {
+ logger.Error(ctx, "invalid-instance-ctrl-attr--using-default-tp")
+ tp = t.getDefaultTechProfile(ctx)
} else {
- logger.Infow("using-specified-tp-from-kv-store", log.Fields{"tpid": techProfiletblID})
+ logger.Infow(ctx, "using-specified-tp-from-kv-store", log.Fields{"tpid": techProfiletblID})
}
} else {
- logger.Info("tp-not-found-on-kv--creating-default-tp")
- tp = t.getDefaultTechProfile()
+ logger.Info(ctx, "tp-not-found-on-kv--creating-default-tp")
+ tp = t.getDefaultTechProfile(ctx)
}
- tpInstancePath := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
+ tpInstancePath := t.GetTechProfileInstanceKVPath(ctx, techProfiletblID, uniPortName)
if tpInstance = t.allocateTPInstance(ctx, uniPortName, tp, intfId, tpInstancePath); tpInstance == nil {
- logger.Error("tp-intance-allocation-failed")
+ logger.Error(ctx, "tp-intance-allocation-failed")
return nil, errors.New("tp-intance-allocation-failed")
}
if err := t.addTechProfInstanceToKVStore(ctx, techProfiletblID, uniPortName, tpInstance); err != nil {
- logger.Errorw("error-adding-tp-to-kv-store", log.Fields{"tableid": techProfiletblID, "uni": uniPortName})
+ logger.Errorw(ctx, "error-adding-tp-to-kv-store", log.Fields{"tableid": techProfiletblID, "uni": uniPortName})
return nil, errors.New("error-adding-tp-to-kv-store")
}
- logger.Infow("tp-added-to-kv-store-successfully",
+ logger.Infow(ctx, "tp-added-to-kv-store-successfully",
log.Fields{"tpid": techProfiletblID, "uni": uniPortName, "intfId": intfId})
return tpInstance, nil
}
func (t *TechProfileMgr) DeleteTechProfileInstance(ctx context.Context, techProfiletblID uint32, uniPortName string) error {
- path := t.GetTechProfileInstanceKVPath(techProfiletblID, uniPortName)
+ path := t.GetTechProfileInstanceKVPath(ctx, techProfiletblID, uniPortName)
return t.config.KVBackend.Delete(ctx, path)
}
-func (t *TechProfileMgr) validateInstanceControlAttr(instCtl InstanceControl) error {
+func (t *TechProfileMgr) validateInstanceControlAttr(ctx context.Context, instCtl InstanceControl) error {
if instCtl.Onu != "single-instance" && instCtl.Onu != "multi-instance" {
- logger.Errorw("invalid-onu-instance-control-attribute", log.Fields{"onu-inst": instCtl.Onu})
+ logger.Errorw(ctx, "invalid-onu-instance-control-attribute", log.Fields{"onu-inst": instCtl.Onu})
return errors.New("invalid-onu-instance-ctl-attr")
}
if instCtl.Uni != "single-instance" && instCtl.Uni != "multi-instance" {
- logger.Errorw("invalid-uni-instance-control-attribute", log.Fields{"uni-inst": instCtl.Uni})
+ logger.Errorw(ctx, "invalid-uni-instance-control-attribute", log.Fields{"uni-inst": instCtl.Uni})
return errors.New("invalid-uni-instance-ctl-attr")
}
if instCtl.Uni == "multi-instance" {
- logger.Error("uni-multi-instance-tp-not-supported")
+ logger.Error(ctx, "uni-multi-instance-tp-not-supported")
return errors.New("uni-multi-instance-tp-not-supported")
}
@@ -433,22 +433,22 @@
var gemPorts []uint32
var err error
- logger.Infow("Allocating TechProfileMgr instance from techprofile template", log.Fields{"uniPortName": uniPortName, "intfId": intfId, "numGem": tp.NumGemPorts})
+ logger.Infow(ctx, "Allocating TechProfileMgr instance from techprofile template", log.Fields{"uniPortName": uniPortName, "intfId": intfId, "numGem": tp.NumGemPorts})
if tp.InstanceCtrl.Onu == "multi-instance" {
if tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil {
- logger.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
+ logger.Errorw(ctx, "Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
return nil
}
} else { // "single-instance"
if tpInst, err := t.getSingleInstanceTp(ctx, tpInstPath); err != nil {
- logger.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
+ logger.Errorw(ctx, "Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
return nil
} else if tpInst == nil {
// No "single-instance" tp found on one any uni port for the given TP ID
// Allocate a new TcontID or AllocID
if tcontIDs, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeAllocID(), 1); err != nil {
- logger.Errorw("Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
+ logger.Errorw(ctx, "Error getting alloc id from rsrcrMgr", log.Fields{"intfId": intfId})
return nil
}
} else {
@@ -456,12 +456,12 @@
tcontIDs = append(tcontIDs, tpInst.UsScheduler.AllocID)
}
}
- logger.Debugw("Num GEM ports in TP:", log.Fields{"NumGemPorts": tp.NumGemPorts})
+ logger.Debugw(ctx, "Num GEM ports in TP:", log.Fields{"NumGemPorts": tp.NumGemPorts})
if gemPorts, err = t.resourceMgr.GetResourceID(ctx, intfId, t.resourceMgr.GetResourceTypeGemPortID(), tp.NumGemPorts); err != nil {
- logger.Errorw("Error getting gemport ids from rsrcrMgr", log.Fields{"intfId": intfId, "numGemports": tp.NumGemPorts})
+ logger.Errorw(ctx, "Error getting gemport ids from rsrcrMgr", log.Fields{"intfId": intfId, "numGemports": tp.NumGemPorts})
return nil
}
- logger.Infow("Allocated tconts and GEM ports successfully", log.Fields{"tconts": tcontIDs, "gemports": gemPorts})
+ logger.Infow(ctx, "Allocated tconts and GEM ports successfully", log.Fields{"tconts": tcontIDs, "gemports": gemPorts})
for index := 0; index < int(tp.NumGemPorts); index++ {
usGemPortAttributeList = append(usGemPortAttributeList,
iGemPortAttribute{GemportID: gemPorts[index],
@@ -475,7 +475,7 @@
DiscardConfig: tp.UpstreamGemPortAttributeList[index].DiscardConfig})
}
- logger.Info("length of DownstreamGemPortAttributeList", len(tp.DownstreamGemPortAttributeList))
+ logger.Info(ctx, "length of DownstreamGemPortAttributeList", len(tp.DownstreamGemPortAttributeList))
//put multicast and unicast downstream GEM port attributes in different lists first
for index := 0; index < int(len(tp.DownstreamGemPortAttributeList)); index++ {
if isMulticastGem(tp.DownstreamGemPortAttributeList[index].IsMulticast) {
@@ -564,10 +564,10 @@
for keyPath, kvPair := range kvPairs {
if value, err := kvstore.ToByte(kvPair.Value); err == nil {
if err = json.Unmarshal(value, &tpInst); err != nil {
- logger.Errorw("error-unmarshal-kv-pair", log.Fields{"keyPath": keyPath, "value": value})
+ logger.Errorw(ctx, "error-unmarshal-kv-pair", log.Fields{"keyPath": keyPath, "value": value})
return nil, errors.New("error-unmarshal-kv-pair")
} else {
- logger.Debugw("found-valid-tp-instance-on-another-uni", log.Fields{"keyPath": keyPath})
+ logger.Debugw(ctx, "found-valid-tp-instance-on-another-uni", log.Fields{"keyPath": keyPath})
return &tpInst, nil
}
}
@@ -575,13 +575,13 @@
return nil, nil
}
-func (t *TechProfileMgr) getDefaultTechProfile() *DefaultTechProfile {
+func (t *TechProfileMgr) getDefaultTechProfile(ctx context.Context) *DefaultTechProfile {
var usGemPortAttributeList []GemPortAttribute
var dsGemPortAttributeList []GemPortAttribute
for _, pbit := range t.config.DefaultPbits {
- logger.Debugw("Creating GEM port", log.Fields{"pbit": pbit})
+ logger.Debugw(ctx, "Creating GEM port", log.Fields{"pbit": pbit})
usGemPortAttributeList = append(usGemPortAttributeList,
GemPortAttribute{
MaxQueueSize: defaultMaxQueueSize,
@@ -638,7 +638,7 @@
DownstreamGemPortAttributeList: dsGemPortAttributeList}
}
-func (t *TechProfileMgr) GetprotoBufParamValue(paramType string, paramKey string) int32 {
+func (t *TechProfileMgr) GetprotoBufParamValue(ctx context.Context, paramType string, paramKey string) int32 {
var result int32 = -1
if paramType == "direction" {
@@ -656,7 +656,7 @@
} else if paramType == "sched_policy" {
for key, val := range tp_pb.SchedulingPolicy_value {
if key == paramKey {
- logger.Debugw("Got value in proto", log.Fields{"key": key, "value": val})
+ logger.Debugw(ctx, "Got value in proto", log.Fields{"key": key, "value": val})
result = val
}
}
@@ -667,29 +667,29 @@
}
}
} else {
- logger.Error("Could not find proto parameter", log.Fields{"paramType": paramType, "key": paramKey})
+ logger.Error(ctx, "Could not find proto parameter", log.Fields{"paramType": paramType, "key": paramKey})
return -1
}
- logger.Debugw("Got value in proto", log.Fields{"key": paramKey, "value": result})
+ logger.Debugw(ctx, "Got value in proto", log.Fields{"key": paramKey, "value": result})
return result
}
-func (t *TechProfileMgr) GetUsScheduler(tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
- dir := tp_pb.Direction(t.GetprotoBufParamValue("direction", tpInstance.UsScheduler.Direction))
+func (t *TechProfileMgr) GetUsScheduler(ctx context.Context, tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
+ dir := tp_pb.Direction(t.GetprotoBufParamValue(ctx, "direction", tpInstance.UsScheduler.Direction))
if dir == -1 {
- logger.Errorf("Error in getting proto id for direction %s for upstream scheduler", tpInstance.UsScheduler.Direction)
+ logger.Errorf(ctx, "Error in getting proto id for direction %s for upstream scheduler", tpInstance.UsScheduler.Direction)
return nil, fmt.Errorf("unable to get proto id for direction %s for upstream scheduler", tpInstance.UsScheduler.Direction)
}
- bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue("additional_bw", tpInstance.UsScheduler.AdditionalBw))
+ bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue(ctx, "additional_bw", tpInstance.UsScheduler.AdditionalBw))
if bw == -1 {
- logger.Errorf("Error in getting proto id for bandwidth %s for upstream scheduler", tpInstance.UsScheduler.AdditionalBw)
+ logger.Errorf(ctx, "Error in getting proto id for bandwidth %s for upstream scheduler", tpInstance.UsScheduler.AdditionalBw)
return nil, fmt.Errorf("unable to get proto id for bandwidth %s for upstream scheduler", tpInstance.UsScheduler.AdditionalBw)
}
- policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue("sched_policy", tpInstance.UsScheduler.QSchedPolicy))
+ policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue(ctx, "sched_policy", tpInstance.UsScheduler.QSchedPolicy))
if policy == -1 {
- logger.Errorf("Error in getting proto id for scheduling policy %s for upstream scheduler", tpInstance.UsScheduler.QSchedPolicy)
+ logger.Errorf(ctx, "Error in getting proto id for scheduling policy %s for upstream scheduler", tpInstance.UsScheduler.QSchedPolicy)
return nil, fmt.Errorf("unable to get proto id for scheduling policy %s for upstream scheduler", tpInstance.UsScheduler.QSchedPolicy)
}
@@ -701,23 +701,23 @@
SchedPolicy: policy}, nil
}
-func (t *TechProfileMgr) GetDsScheduler(tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
+func (t *TechProfileMgr) GetDsScheduler(ctx context.Context, tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error) {
- dir := tp_pb.Direction(t.GetprotoBufParamValue("direction", tpInstance.DsScheduler.Direction))
+ dir := tp_pb.Direction(t.GetprotoBufParamValue(ctx, "direction", tpInstance.DsScheduler.Direction))
if dir == -1 {
- logger.Errorf("Error in getting proto id for direction %s for downstream scheduler", tpInstance.DsScheduler.Direction)
+ logger.Errorf(ctx, "Error in getting proto id for direction %s for downstream scheduler", tpInstance.DsScheduler.Direction)
return nil, fmt.Errorf("unable to get proto id for direction %s for downstream scheduler", tpInstance.DsScheduler.Direction)
}
- bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue("additional_bw", tpInstance.DsScheduler.AdditionalBw))
+ bw := tp_pb.AdditionalBW(t.GetprotoBufParamValue(ctx, "additional_bw", tpInstance.DsScheduler.AdditionalBw))
if bw == -1 {
- logger.Errorf("Error in getting proto id for bandwidth %s for downstream scheduler", tpInstance.DsScheduler.AdditionalBw)
+ logger.Errorf(ctx, "Error in getting proto id for bandwidth %s for downstream scheduler", tpInstance.DsScheduler.AdditionalBw)
return nil, fmt.Errorf("unable to get proto id for bandwidth %s for downstream scheduler", tpInstance.DsScheduler.AdditionalBw)
}
- policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue("sched_policy", tpInstance.DsScheduler.QSchedPolicy))
+ policy := tp_pb.SchedulingPolicy(t.GetprotoBufParamValue(ctx, "sched_policy", tpInstance.DsScheduler.QSchedPolicy))
if policy == -1 {
- logger.Errorf("Error in getting proto id for scheduling policy %s for downstream scheduler", tpInstance.DsScheduler.QSchedPolicy)
+ logger.Errorf(ctx, "Error in getting proto id for scheduling policy %s for downstream scheduler", tpInstance.DsScheduler.QSchedPolicy)
return nil, fmt.Errorf("unable to get proto id for scheduling policy %s for downstream scheduler", tpInstance.DsScheduler.QSchedPolicy)
}
@@ -741,7 +741,7 @@
return tSched
}
-func (tpm *TechProfileMgr) GetTrafficQueues(tp *TechProfile, Dir tp_pb.Direction) ([]*tp_pb.TrafficQueue, error) {
+func (tpm *TechProfileMgr) GetTrafficQueues(ctx context.Context, tp *TechProfile, Dir tp_pb.Direction) ([]*tp_pb.TrafficQueue, error) {
var encryp bool
if Dir == tp_pb.Direction_UPSTREAM {
@@ -755,20 +755,20 @@
encryp = false
}
- schedPolicy := tpm.GetprotoBufParamValue("sched_policy", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
+ schedPolicy := tpm.GetprotoBufParamValue(ctx, "sched_policy", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
if schedPolicy == -1 {
- logger.Errorf("Error in getting Proto Id for scheduling policy %s for Upstream Gem Port %d", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy, Count)
+ logger.Errorf(ctx, "Error in getting Proto Id for scheduling policy %s for Upstream Gem Port %d", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy, Count)
return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.UpstreamGemPortAttributeList[Count].SchedulingPolicy)
}
- discardPolicy := tpm.GetprotoBufParamValue("discard_policy", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
+ discardPolicy := tpm.GetprotoBufParamValue(ctx, "discard_policy", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
if discardPolicy == -1 {
- logger.Errorf("Error in getting Proto Id for discard policy %s for Upstream Gem Port %d", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy, Count)
+ logger.Errorf(ctx, "Error in getting Proto Id for discard policy %s for Upstream Gem Port %d", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy, Count)
return nil, fmt.Errorf("upstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.UpstreamGemPortAttributeList[Count].DiscardPolicy)
}
GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
- Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.UsScheduler.Direction)),
+ Direction: tp_pb.Direction(tpm.GetprotoBufParamValue(ctx, "direction", tp.UsScheduler.Direction)),
GemportId: tp.UpstreamGemPortAttributeList[Count].GemportID,
PbitMap: tp.UpstreamGemPortAttributeList[Count].PbitMap,
AesEncryption: encryp,
@@ -778,7 +778,7 @@
DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy),
})
}
- logger.Debugw("Upstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
+ logger.Debugw(ctx, "Upstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
return GemPorts, nil
} else if Dir == tp_pb.Direction_DOWNSTREAM {
//downstream GEM ports
@@ -795,20 +795,20 @@
encryp = false
}
- schedPolicy := tpm.GetprotoBufParamValue("sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
+ schedPolicy := tpm.GetprotoBufParamValue(ctx, "sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
if schedPolicy == -1 {
- logger.Errorf("Error in getting Proto Id for scheduling policy %s for Downstream Gem Port %d", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy, Count)
+ logger.Errorf(ctx, "Error in getting Proto Id for scheduling policy %s for Downstream Gem Port %d", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy, Count)
return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized scheduling policy %s", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)
}
- discardPolicy := tpm.GetprotoBufParamValue("discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
+ discardPolicy := tpm.GetprotoBufParamValue(ctx, "discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
if discardPolicy == -1 {
- logger.Errorf("Error in getting Proto Id for discard policy %s for Downstream Gem Port %d", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy, Count)
+ logger.Errorf(ctx, "Error in getting Proto Id for discard policy %s for Downstream Gem Port %d", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy, Count)
return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unrecognized discard policy %s", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)
}
GemPorts = append(GemPorts, &tp_pb.TrafficQueue{
- Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.DsScheduler.Direction)),
+ Direction: tp_pb.Direction(tpm.GetprotoBufParamValue(ctx, "direction", tp.DsScheduler.Direction)),
GemportId: tp.DownstreamGemPortAttributeList[Count].GemportID,
PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
AesEncryption: encryp,
@@ -818,11 +818,11 @@
DiscardPolicy: tp_pb.DiscardPolicy(discardPolicy),
})
}
- logger.Debugw("Downstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
+ logger.Debugw(ctx, "Downstream Traffic queue list ", log.Fields{"queuelist": GemPorts})
return GemPorts, nil
}
- logger.Errorf("Unsupported direction %s used for generating Traffic Queue list", Dir)
+ logger.Errorf(ctx, "Unsupported direction %s used for generating Traffic Queue list", Dir)
return nil, fmt.Errorf("downstream gem port traffic queue creation failed due to unsupported direction %s", Dir)
}
@@ -832,7 +832,7 @@
(isMulticastAttrValue == "True" || isMulticastAttrValue == "true" || isMulticastAttrValue == "TRUE")
}
-func (tpm *TechProfileMgr) GetMulticastTrafficQueues(tp *TechProfile) []*tp_pb.TrafficQueue {
+func (tpm *TechProfileMgr) GetMulticastTrafficQueues(ctx context.Context, tp *TechProfile) []*tp_pb.TrafficQueue {
var encryp bool
NumGemPorts := len(tp.DownstreamGemPortAttributeList)
mcastTrafficQueues := make([]*tp_pb.TrafficQueue, 0)
@@ -846,29 +846,29 @@
encryp = false
}
mcastTrafficQueues = append(mcastTrafficQueues, &tp_pb.TrafficQueue{
- Direction: tp_pb.Direction(tpm.GetprotoBufParamValue("direction", tp.DsScheduler.Direction)),
+ Direction: tp_pb.Direction(tpm.GetprotoBufParamValue(ctx, "direction", tp.DsScheduler.Direction)),
GemportId: tp.DownstreamGemPortAttributeList[Count].McastGemID,
PbitMap: tp.DownstreamGemPortAttributeList[Count].PbitMap,
AesEncryption: encryp,
- SchedPolicy: tp_pb.SchedulingPolicy(tpm.GetprotoBufParamValue("sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)),
+ SchedPolicy: tp_pb.SchedulingPolicy(tpm.GetprotoBufParamValue(ctx, "sched_policy", tp.DownstreamGemPortAttributeList[Count].SchedulingPolicy)),
Priority: tp.DownstreamGemPortAttributeList[Count].PriorityQueue,
Weight: tp.DownstreamGemPortAttributeList[Count].Weight,
- DiscardPolicy: tp_pb.DiscardPolicy(tpm.GetprotoBufParamValue("discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)),
+ DiscardPolicy: tp_pb.DiscardPolicy(tpm.GetprotoBufParamValue(ctx, "discard_policy", tp.DownstreamGemPortAttributeList[Count].DiscardPolicy)),
})
}
- logger.Debugw("Downstream Multicast Traffic queue list ", log.Fields{"queuelist": mcastTrafficQueues})
+ logger.Debugw(ctx, "Downstream Multicast Traffic queue list ", log.Fields{"queuelist": mcastTrafficQueues})
return mcastTrafficQueues
}
-func (tpm *TechProfileMgr) GetUsTrafficScheduler(tp *TechProfile) *tp_pb.TrafficScheduler {
- UsScheduler, _ := tpm.GetUsScheduler(tp)
+func (tpm *TechProfileMgr) GetUsTrafficScheduler(ctx context.Context, tp *TechProfile) *tp_pb.TrafficScheduler {
+ UsScheduler, _ := tpm.GetUsScheduler(ctx, tp)
return &tp_pb.TrafficScheduler{Direction: UsScheduler.Direction,
AllocId: tp.UsScheduler.AllocID,
Scheduler: UsScheduler}
}
-func (t *TechProfileMgr) GetGemportIDForPbit(tp *TechProfile, dir tp_pb.Direction, pbit uint32) uint32 {
+func (t *TechProfileMgr) GetGemportIDForPbit(ctx context.Context, tp *TechProfile, dir tp_pb.Direction, pbit uint32) uint32 {
/*
Function to get the Gemport ID mapped to a pbit.
*/
@@ -882,7 +882,7 @@
// "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
if p, err := strconv.Atoi(string(tp.UpstreamGemPortAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
- logger.Debugw("Found-US-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.UpstreamGemPortAttributeList[gemCnt].GemportID})
+ logger.Debugw(ctx, "Found-US-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.UpstreamGemPortAttributeList[gemCnt].GemportID})
return tp.UpstreamGemPortAttributeList[gemCnt].GemportID
}
}
@@ -898,14 +898,14 @@
// "lenOfPbitMap - pbitMapIdx + 1" will give pbit-i th value from LSB position in the pbit map string
if p, err := strconv.Atoi(string(tp.DownstreamGemPortAttributeList[gemCnt].PbitMap[lenOfPbitMap-pbitMapIdx+1])); err == nil {
if uint32(pbitMapIdx-2) == pbit && p == 1 { // Check this p-bit is set
- logger.Debugw("Found-DS-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.DownstreamGemPortAttributeList[gemCnt].GemportID})
+ logger.Debugw(ctx, "Found-DS-GEMport-for-Pcp", log.Fields{"pbit": pbit, "GEMport": tp.DownstreamGemPortAttributeList[gemCnt].GemportID})
return tp.DownstreamGemPortAttributeList[gemCnt].GemportID
}
}
}
}
}
- logger.Errorw("No-GemportId-Found-For-Pcp", log.Fields{"pcpVlan": pbit})
+ logger.Errorw(ctx, "No-GemportId-Found-For-Pcp", log.Fields{"pcpVlan": pbit})
return 0
}
@@ -919,7 +919,7 @@
for kvPath, kvPair := range kvPairs {
if value, err := kvstore.ToByte(kvPair.Value); err == nil {
if err = json.Unmarshal(value, &tp); err != nil {
- logger.Errorw("error-unmarshal-kv-pair", log.Fields{"kvPath": kvPath, "value": value})
+ logger.Errorw(ctx, "error-unmarshal-kv-pair", log.Fields{"kvPath": kvPath, "value": value})
continue
} else {
tpInstances = append(tpInstances, tp)
diff --git a/pkg/techprofile/tech_profile_if.go b/pkg/techprofile/tech_profile_if.go
index e605d49..977dcdc 100644
--- a/pkg/techprofile/tech_profile_if.go
+++ b/pkg/techprofile/tech_profile_if.go
@@ -24,18 +24,18 @@
)
type TechProfileIf interface {
- SetKVClient() *db.Backend
- GetTechProfileInstanceKVPath(techProfiletblID uint32, uniPortName string) string
+ SetKVClient(ctx context.Context) *db.Backend
+ GetTechProfileInstanceKVPath(ctx context.Context, techProfiletblID uint32, uniPortName string) string
GetTPInstanceFromKVStore(ctx context.Context, techProfiletblID uint32, path string) (*TechProfile, error)
CreateTechProfInstance(ctx context.Context, techProfiletblID uint32, uniPortName string, intfId uint32) (*TechProfile, error)
DeleteTechProfileInstance(ctx context.Context, techProfiletblID uint32, uniPortName string) error
- GetprotoBufParamValue(paramType string, paramKey string) int32
- GetUsScheduler(tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error)
- GetDsScheduler(tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error)
+ GetprotoBufParamValue(ctx context.Context, paramType string, paramKey string) int32
+ GetUsScheduler(ctx context.Context, tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error)
+ GetDsScheduler(ctx context.Context, tpInstance *TechProfile) (*tp_pb.SchedulerConfig, error)
GetTrafficScheduler(tpInstance *TechProfile, SchedCfg *tp_pb.SchedulerConfig,
ShapingCfg *tp_pb.TrafficShapingInfo) *tp_pb.TrafficScheduler
- GetTrafficQueues(tp *TechProfile, Dir tp_pb.Direction) ([]*tp_pb.TrafficQueue, error)
- GetMulticastTrafficQueues(tp *TechProfile) []*tp_pb.TrafficQueue
- GetGemportIDForPbit(tp *TechProfile, Dir tp_pb.Direction, pbit uint32) uint32
+ GetTrafficQueues(ctx context.Context, tp *TechProfile, Dir tp_pb.Direction) ([]*tp_pb.TrafficQueue, error)
+ GetMulticastTrafficQueues(ctx context.Context, tp *TechProfile) []*tp_pb.TrafficQueue
+ GetGemportIDForPbit(ctx context.Context, tp *TechProfile, Dir tp_pb.Direction, pbit uint32) uint32
FindAllTpInstances(ctx context.Context, techProfiletblID uint32, ponIntf uint32, onuID uint32) []TechProfile
}