VOL-3734 optimise rpc events to be send to queue and then to kafka from the queue

Change-Id: I5e068722412b6d9526760900d9173aaf51e00946
diff --git a/vendor/github.com/opencord/voltha-lib-go/v4/pkg/events/events_proxy.go b/vendor/github.com/opencord/voltha-lib-go/v4/pkg/events/events_proxy.go
index 2301f43..910fec3 100644
--- a/vendor/github.com/opencord/voltha-lib-go/v4/pkg/events/events_proxy.go
+++ b/vendor/github.com/opencord/voltha-lib-go/v4/pkg/events/events_proxy.go
@@ -17,11 +17,13 @@
 package events
 
 import (
+	"container/ring"
 	"context"
 	"errors"
 	"fmt"
 	"strconv"
 	"strings"
+	"sync"
 	"time"
 
 	"github.com/golang/protobuf/ptypes"
@@ -31,9 +33,17 @@
 	"github.com/opencord/voltha-protos/v4/go/voltha"
 )
 
+// TODO: Make configurable through helm chart
+const EVENT_THRESHOLD = 1000
+
+type lastEvent struct{}
+
 type EventProxy struct {
-	kafkaClient kafka.Client
-	eventTopic  kafka.Topic
+	kafkaClient    kafka.Client
+	eventTopic     kafka.Topic
+	eventQueue     *EventQueue
+	queueCtx       context.Context
+	queueCancelCtx context.CancelFunc
 }
 
 func NewEventProxy(opts ...EventProxyOption) *EventProxy {
@@ -41,6 +51,8 @@
 	for _, option := range opts {
 		option(&proxy)
 	}
+	proxy.eventQueue = newEventQueue()
+	proxy.queueCtx, proxy.queueCancelCtx = context.WithCancel(context.Background())
 	return &proxy
 }
 
@@ -84,8 +96,8 @@
 	header.Type = eventType
 	header.TypeVersion = eventif.EventTypeVersion
 
-	// raisedTs is in nanoseconds
-	timestamp, err := ptypes.TimestampProto(time.Unix(0, raisedTs))
+	// raisedTs is in seconds
+	timestamp, err := ptypes.TimestampProto(time.Unix(raisedTs, 0))
 	if err != nil {
 		return nil, err
 	}
@@ -112,15 +124,7 @@
 		return err
 	}
 	event.EventType = &voltha.Event_RpcEvent{RpcEvent: rpcEvent}
-	if err := ep.sendEvent(ctx, &event); err != nil {
-		logger.Errorw(ctx, "Failed to send rpc event to KAFKA bus", log.Fields{"rpc-event": rpcEvent})
-		return err
-	}
-	logger.Debugw(ctx, "Successfully sent RPC event to KAFKA bus", 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": rpcEvent.ResourceId, "Context": rpcEvent.Context,
-		"RPCEventName": id})
-
+	ep.eventQueue.push(&event)
 	return nil
 
 }
@@ -195,3 +199,155 @@
 func (ep *EventProxy) SendLiveness(ctx context.Context) error {
 	return ep.kafkaClient.SendLiveness(ctx)
 }
+
+// Start the event proxy
+func (ep *EventProxy) Start() {
+	eq := ep.eventQueue
+	go eq.start(ep.queueCtx)
+	logger.Debugw(context.Background(), "event-proxy-starting...", log.Fields{"events-threashold": EVENT_THRESHOLD})
+	for {
+		// Notify the queue I am ready
+		eq.readyToSendToKafkaCh <- struct{}{}
+		// Wait for an event
+		elem, ok := <-eq.eventChannel
+		if !ok {
+			logger.Debug(context.Background(), "event-channel-closed-exiting")
+			break
+		}
+		// Check for last event
+		if _, ok := elem.(*lastEvent); ok {
+			// close the queuing loop
+			logger.Info(context.Background(), "received-last-event")
+			ep.queueCancelCtx()
+			break
+		}
+		ctx := context.Background()
+		event, ok := elem.(*voltha.Event)
+		if !ok {
+			logger.Warnw(ctx, "invalid-event", log.Fields{"element": elem})
+			continue
+		}
+		if err := ep.sendEvent(ctx, event); err != nil {
+			logger.Errorw(ctx, "failed-to-send-event-to-kafka-bus", log.Fields{"event": event})
+		} else {
+			logger.Debugw(ctx, "successfully-sent-rpc-event-to-kafka-bus", log.Fields{"id": event.Header.Id, "category": event.Header.Category,
+				"sub-category": event.Header.SubCategory, "type": event.Header.Type, "type-version": event.Header.TypeVersion,
+				"reported-ts": event.Header.ReportedTs, "event-type": event.EventType})
+		}
+	}
+}
+
+func (ep *EventProxy) Stop() {
+	ep.eventQueue.stop()
+}
+
+type EventQueue struct {
+	mutex                sync.RWMutex
+	eventChannel         chan interface{}
+	insertPosition       *ring.Ring
+	popPosition          *ring.Ring
+	dataToSendAvailable  chan struct{}
+	readyToSendToKafkaCh chan struct{}
+	eventQueueStopped    chan struct{}
+}
+
+func newEventQueue() *EventQueue {
+	ev := &EventQueue{
+		eventChannel:         make(chan interface{}),
+		insertPosition:       ring.New(EVENT_THRESHOLD),
+		dataToSendAvailable:  make(chan struct{}),
+		readyToSendToKafkaCh: make(chan struct{}),
+		eventQueueStopped:    make(chan struct{}),
+	}
+	ev.popPosition = ev.insertPosition
+	return ev
+}
+
+// push is invoked to push an event at the back of a queue
+func (eq *EventQueue) push(event interface{}) {
+	eq.mutex.Lock()
+
+	if eq.insertPosition != nil {
+		// Handle Queue is full.
+		// TODO: Current default is to overwrite old data if queue is full. Is there a need to
+		// block caller if max threshold is reached?
+		if eq.insertPosition.Value != nil && eq.insertPosition == eq.popPosition {
+			eq.popPosition = eq.popPosition.Next()
+		}
+
+		// Insert data and move pointer to next empty position
+		eq.insertPosition.Value = event
+		eq.insertPosition = eq.insertPosition.Next()
+
+		// Check for last event
+		if _, ok := event.(*lastEvent); ok {
+			eq.insertPosition = nil
+		}
+		eq.mutex.Unlock()
+		// Notify waiting thread of data availability
+		eq.dataToSendAvailable <- struct{}{}
+
+	} else {
+		logger.Debug(context.Background(), "event-queue-is-closed-as-insert-position-is-cleared")
+		eq.mutex.Unlock()
+	}
+}
+
+// start starts the routine that extracts an element from the event queue and
+// send it to the kafka sending routine to process.
+func (eq *EventQueue) start(ctx context.Context) {
+	logger.Info(ctx, "starting-event-queue")
+loop:
+	for {
+		select {
+		case <-eq.dataToSendAvailable:
+		//	Do nothing - use to prevent caller pushing data to block
+		case <-eq.readyToSendToKafkaCh:
+			{
+				// Kafka sending routine is ready to process an event
+				eq.mutex.Lock()
+				element := eq.popPosition.Value
+				if element == nil {
+					// No events to send. Wait
+					eq.mutex.Unlock()
+					select {
+					case _, ok := <-eq.dataToSendAvailable:
+						if !ok {
+							// channel closed
+							eq.eventQueueStopped <- struct{}{}
+							return
+						}
+					case <-ctx.Done():
+						logger.Info(ctx, "event-queue-context-done")
+						eq.eventQueueStopped <- struct{}{}
+						return
+					}
+					eq.mutex.Lock()
+					element = eq.popPosition.Value
+				}
+				eq.popPosition.Value = nil
+				eq.popPosition = eq.popPosition.Next()
+				eq.mutex.Unlock()
+				eq.eventChannel <- element
+			}
+		case <-ctx.Done():
+			logger.Info(ctx, "event-queue-context-done")
+			eq.eventQueueStopped <- struct{}{}
+			break loop
+		}
+	}
+	logger.Info(ctx, "event-queue-stopped")
+
+}
+
+func (eq *EventQueue) stop() {
+	// Flush all
+	eq.push(&lastEvent{})
+	<-eq.eventQueueStopped
+	eq.mutex.Lock()
+	close(eq.readyToSendToKafkaCh)
+	close(eq.dataToSendAvailable)
+	close(eq.eventChannel)
+	eq.mutex.Unlock()
+
+}
diff --git a/vendor/github.com/opencord/voltha-lib-go/v4/pkg/mocks/kafka/kafka_client.go b/vendor/github.com/opencord/voltha-lib-go/v4/pkg/mocks/kafka/kafka_client.go
index 268c571..97bb135 100644
--- a/vendor/github.com/opencord/voltha-lib-go/v4/pkg/mocks/kafka/kafka_client.go
+++ b/vendor/github.com/opencord/voltha-lib-go/v4/pkg/mocks/kafka/kafka_client.go
@@ -18,12 +18,15 @@
 import (
 	"context"
 	"fmt"
+	"github.com/golang/protobuf/ptypes"
 	"sync"
 	"time"
 
+	"github.com/golang/protobuf/proto"
 	"github.com/opencord/voltha-lib-go/v4/pkg/kafka"
 	"github.com/opencord/voltha-lib-go/v4/pkg/log"
 	ic "github.com/opencord/voltha-protos/v4/go/inter_container"
+	"github.com/opencord/voltha-protos/v4/go/voltha"
 	"google.golang.org/grpc/codes"
 	"google.golang.org/grpc/status"
 )
@@ -117,10 +120,35 @@
 	logger.Debug(ctx, "SubscribeForMetadata - unimplemented")
 }
 
+func toIntercontainerMessage(event *voltha.Event) *ic.InterContainerMessage {
+	msg := &ic.InterContainerMessage{
+		Header: &ic.Header{
+			Id:        event.Header.Id,
+			Type:      ic.MessageType_REQUEST,
+			Timestamp: event.Header.RaisedTs,
+		},
+	}
+	// Marshal event
+	if eventBody, err := ptypes.MarshalAny(event); err == nil {
+		msg.Body = eventBody
+	}
+	return msg
+}
+
 func (kc *KafkaClient) Send(ctx context.Context, msg interface{}, topic *kafka.Topic, keys ...string) error {
+	// Assert message is a proto message
+	// ascertain the value interface type is a proto.Message
+	if _, ok := msg.(proto.Message); !ok {
+		logger.Warnw(ctx, "message-not-a-proto-message", log.Fields{"msg": msg})
+		return status.Error(codes.InvalidArgument, "msg-not-a-proto-msg")
+	}
 	req, ok := msg.(*ic.InterContainerMessage)
 	if !ok {
-		return status.Error(codes.InvalidArgument, "msg-not-InterContainerMessage-type")
+		event, ok := msg.(*voltha.Event) //This is required as event message will be of type voltha.Event
+		if !ok {
+			return status.Error(codes.InvalidArgument, "unexpected-message-type")
+		}
+		req = toIntercontainerMessage(event)
 	}
 	if req == nil {
 		return status.Error(codes.InvalidArgument, "msg-nil")
diff --git a/vendor/github.com/opencord/voltha-protos/v4/go/voltha/core.pb.go b/vendor/github.com/opencord/voltha-protos/v4/go/voltha/core.pb.go
index 4c92095..64d42d0 100644
--- a/vendor/github.com/opencord/voltha-protos/v4/go/voltha/core.pb.go
+++ b/vendor/github.com/opencord/voltha-protos/v4/go/voltha/core.pb.go
@@ -37,6 +37,8 @@
 	DeviceTransientState_DELETING_POST_ADAPTER_RESPONSE DeviceTransientState_Types = 4
 	// State to represent that the device deletion is failed
 	DeviceTransientState_DELETE_FAILED DeviceTransientState_Types = 5
+	// State to represent that reconcile is in progress
+	DeviceTransientState_RECONCILE_IN_PROGRESS DeviceTransientState_Types = 6
 )
 
 var DeviceTransientState_Types_name = map[int32]string{
@@ -46,6 +48,7 @@
 	3: "DELETING_FROM_ADAPTER",
 	4: "DELETING_POST_ADAPTER_RESPONSE",
 	5: "DELETE_FAILED",
+	6: "RECONCILE_IN_PROGRESS",
 }
 
 var DeviceTransientState_Types_value = map[string]int32{
@@ -55,6 +58,7 @@
 	"DELETING_FROM_ADAPTER":          3,
 	"DELETING_POST_ADAPTER_RESPONSE": 4,
 	"DELETE_FAILED":                  5,
+	"RECONCILE_IN_PROGRESS":          6,
 }
 
 func (x DeviceTransientState_Types) String() string {
@@ -112,22 +116,23 @@
 func init() { proto.RegisterFile("voltha_protos/core.proto", fileDescriptor_39634f15fb8a505e) }
 
 var fileDescriptor_39634f15fb8a505e = []byte{
-	// 264 bytes of a gzipped FileDescriptorProto
-	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x90, 0x4d, 0x4b, 0xc4, 0x30,
-	0x10, 0x86, 0xad, 0xfb, 0xa1, 0x0c, 0x58, 0x63, 0x54, 0x58, 0x2f, 0x22, 0x3d, 0x79, 0x31, 0x05,
-	0xf5, 0x0f, 0x54, 0x3b, 0x95, 0xc5, 0xb5, 0x2d, 0x6d, 0x2e, 0x7a, 0x09, 0xdd, 0x1a, 0xba, 0x05,
-	0x6d, 0x4a, 0x1a, 0x0b, 0xde, 0xfc, 0xb5, 0xfe, 0x0e, 0xb1, 0x1f, 0x82, 0xb0, 0xb7, 0x99, 0xe7,
-	0x99, 0xf7, 0x30, 0x2f, 0x2c, 0x5a, 0xf5, 0x66, 0x36, 0x99, 0xa8, 0xb5, 0x32, 0xaa, 0x71, 0x73,
-	0xa5, 0x25, 0xeb, 0x66, 0x3a, 0xef, 0x8d, 0xf3, 0x6d, 0xc1, 0x89, 0x2f, 0xdb, 0x32, 0x97, 0x5c,
-	0x67, 0x55, 0x53, 0xca, 0xca, 0xa4, 0x26, 0x33, 0x92, 0x3e, 0xc2, 0xa1, 0x19, 0x89, 0x68, 0x7e,
-	0xd1, 0xc2, 0xba, 0xb0, 0x2e, 0xed, 0x6b, 0x87, 0xf5, 0x51, 0xb6, 0x2d, 0xc6, 0xf8, 0x67, 0x2d,
-	0x9b, 0xc4, 0x36, 0xff, 0xa8, 0xf3, 0x65, 0xc1, 0xac, 0x33, 0x74, 0x1f, 0xa6, 0x61, 0x14, 0x22,
-	0xd9, 0xa1, 0x7b, 0x30, 0xf1, 0xc2, 0x67, 0x62, 0x51, 0x0a, 0x76, 0x10, 0x25, 0xf7, 0x28, 0x7c,
-	0x5c, 0x21, 0x5f, 0x86, 0x0f, 0x64, 0x97, 0x9e, 0xc1, 0xe9, 0xb8, 0x89, 0x20, 0x89, 0x9e, 0x84,
-	0xe7, 0x7b, 0x31, 0xc7, 0x84, 0x4c, 0xa8, 0x03, 0xe7, 0x7f, 0x2a, 0x8e, 0x52, 0x3e, 0x2a, 0x91,
-	0x60, 0x1a, 0x47, 0x61, 0x8a, 0x64, 0x4a, 0x8f, 0xe0, 0xa0, 0xbb, 0x41, 0x11, 0x78, 0xcb, 0x15,
-	0xfa, 0x64, 0x76, 0x87, 0x70, 0xac, 0x74, 0xc1, 0x54, 0x2d, 0xab, 0x5c, 0xe9, 0xd7, 0xe1, 0x89,
-	0x17, 0x56, 0x94, 0x66, 0xf3, 0xb1, 0x66, 0xb9, 0x7a, 0x77, 0x47, 0xe7, 0xf6, 0xee, 0x6a, 0x68,
-	0xad, 0xbd, 0x75, 0x0b, 0x35, 0xb0, 0xf5, 0xbc, 0x83, 0x37, 0x3f, 0x01, 0x00, 0x00, 0xff, 0xff,
-	0xe3, 0x06, 0xbc, 0xa1, 0x5a, 0x01, 0x00, 0x00,
+	// 284 bytes of a gzipped FileDescriptorProto
+	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x90, 0x4d, 0x4b, 0xf4, 0x30,
+	0x10, 0xc7, 0x9f, 0xee, 0xdb, 0x23, 0x01, 0xd7, 0x18, 0x15, 0xd6, 0x8b, 0x48, 0x4f, 0x5e, 0x4c,
+	0x41, 0xfd, 0x02, 0x75, 0x3b, 0xbb, 0x14, 0xd7, 0xa4, 0x24, 0xbd, 0xe8, 0x25, 0x74, 0x6b, 0xe8,
+	0x16, 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