pbacsko commented on code in PR #533:
URL: https://github.com/apache/yunikorn-core/pull/533#discussion_r1412100072


##########
pkg/events/event_streaming.go:
##########
@@ -0,0 +1,179 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements.  See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership.  The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License.  You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+package events
+
+import (
+       "sync"
+       "time"
+
+       "go.uber.org/zap"
+
+       "github.com/apache/yunikorn-core/pkg/log"
+       "github.com/apache/yunikorn-scheduler-interface/lib/go/si"
+)
+
+const defaultChannelBufSize = 1000
+
+// EventStreaming implements the event streaming logic.
+// New events are immediately forwarded to all active consumers.
+type EventStreaming struct {
+       buffer       *eventRingBuffer
+       stopCh       chan struct{}
+       eventStreams map[*EventStream]eventConsumerDetails
+       sync.Mutex
+}
+
+type eventConsumerDetails struct {
+       local     chan *si.EventRecord
+       consumer  chan<- *si.EventRecord
+       stopCh    chan struct{}
+       name      string
+       createdAt time.Time
+}
+
+// EventStream handle type returned to the client that wants to capture the 
stream of events.
+type EventStream struct {
+       Events <-chan *si.EventRecord
+}
+
+// PublishEvent publishes an event to all event stream consumers.
+//
+// The streaming logic uses bridging to ensure proper ordering of existing and 
new events.
+// Events are sent to the "local" channel from where it is forwarded to the 
"consumer" channel.
+//
+// If "local" is full, it means that the consumer side has not processed the 
events at an appropriate pace.
+// Such a consumer is removed and the related channels are closed.
+func (e *EventStreaming) PublishEvent(event *si.EventRecord) {
+       e.Lock()
+       defer e.Unlock()
+
+       for consumer, details := range e.eventStreams {
+               if len(details.local) == defaultChannelBufSize {
+                       log.Log(log.Events).Warn("Listener buffer full due to 
potentially slow consumer, removing it")
+                       e.removeEventStream(consumer)
+                       continue
+               }
+
+               details.local <- event
+       }
+}
+
+// CreateEventStream sets up event streaming for a consumer. The returned 
EventStream object
+// contains a channel that can be used for reading.
+//
+// When a consumer is finished, it must call RemoveEventStream to free up 
resources.
+//
+// Consumers have an arbitrary name for logging purposes. The "count" 
parameter defines the number
+// of maximum historical events from the ring buffer. "0" is a valid value and 
means no past events.
+func (e *EventStreaming) CreateEventStream(name string, count uint64) 
*EventStream {

Review Comment:
   We'll see, let's talk about in a different JIRA. Although it's possible even 
now, because you just have to use math.MaxUint64 and that does the job. Numbers 
that are too high isn't an issue.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to