Skip to content

Commit

Permalink
fix(): cache issue fixes (#28)
Browse files Browse the repository at this point in the history
* fix(): cache fixes

Signed-off-by: Puran Adhikari <[email protected]>

* fix(): debug logs added

Signed-off-by: Puran Adhikari <[email protected]>

* fix(): cache added to the copy function

Signed-off-by: Puran Adhikari <[email protected]>

* fix(): cache issue fixes

Signed-off-by: Puran Adhikari <[email protected]>

* fix(): log changes to debug

Signed-off-by: Puran Adhikari <[email protected]>

* fix(): log changes

Signed-off-by: Puran Adhikari <[email protected]>

---------

Signed-off-by: Puran Adhikari <[email protected]>
  • Loading branch information
PuranAdhikari authored May 5, 2023
1 parent edd2613 commit 940b2ec
Showing 1 changed file with 35 additions and 25 deletions.
60 changes: 35 additions & 25 deletions pkg/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package events

import (
b64 "encoding/base64"
"fmt"
"strings"
"sync"
Expand Down Expand Up @@ -56,11 +57,12 @@ func NewEventRecorder(c client.Writer, s *runtime.Scheme, em map[EventName]*Even
EventsMap: em,
Options: o,
Logger: log,
cache: lru.New(4096),
cache: lru.New(4096),
}
}

var _ EventRecorder = (*eventRecorder)(nil)

type EventRecorderOptions struct {
// Version is the version of the component
Version string
Expand Down Expand Up @@ -89,24 +91,28 @@ type Event struct {

// getEventKey builds unique event key based on source, involvedObject, reason, message
func GetEventKey(event *corev1.Event) string {
return strings.Join([]string{
event.Source.Component,
event.Source.Host,
event.InvolvedObject.Kind,
event.InvolvedObject.Namespace,
event.InvolvedObject.Name,
event.InvolvedObject.FieldPath,
string(event.InvolvedObject.UID),
event.InvolvedObject.APIVersion,
event.Type,
event.Reason,
event.Message,
event.Labels["sliceName"],
event.Labels["sliceCluster"],
event.Labels["sliceProject"],
event.Labels["eventTitle"],
},
"")
return b64.StdEncoding.EncodeToString(
[]byte(
strings.Join([]string{
event.Source.Component,
event.Source.Host,
event.InvolvedObject.Kind,
event.InvolvedObject.Namespace,
event.InvolvedObject.Name,
event.InvolvedObject.FieldPath,
string(event.InvolvedObject.UID),
event.InvolvedObject.APIVersion,
event.Type,
event.Reason,
event.Message,
event.Labels["sliceName"],
event.Labels["sliceCluster"],
event.Labels["sliceProject"],
event.Labels["eventTitle"],
},
""),
),
)
}

type eventRecorder struct {
Expand All @@ -115,8 +121,8 @@ type eventRecorder struct {
Scheme *runtime.Scheme
EventsMap map[EventName]*EventSchema
Options EventRecorderOptions
cache *lru.Cache // cache of last seen events
cacheLock sync.RWMutex // mutex to synchronize access to cache
cache *lru.Cache // cache of last seen events
cacheLock sync.RWMutex // mutex to synchronize access to cache
}

func (er *eventRecorder) Copy() *eventRecorder {
Expand All @@ -125,6 +131,7 @@ func (er *eventRecorder) Copy() *eventRecorder {
Logger: er.Logger,
Scheme: er.Scheme,
EventsMap: er.EventsMap,
cache: er.cache,
Options: EventRecorderOptions{
Version: er.Options.Version,
Cluster: er.Options.Cluster,
Expand Down Expand Up @@ -229,17 +236,19 @@ func (er *eventRecorder) RecordEvent(ctx context.Context, e *Event) error {
er.cache = lru.New(4096)
}
key := GetEventKey(ev)
er.Logger.Debugf("event key %s", key)
er.cacheLock.Lock()
defer er.cacheLock.Unlock()
lastSeenEvent,ok := er.cache.Get(key)
if !ok{
lastSeenEvent, ok := er.cache.Get(key)
if !ok {
ev.FirstTimestamp = t
if err := er.Client.Create(ctx, ev); err != nil {
er.Logger.With("error", err, "event", ev).Error("Unable to create event")
return err
} else {
er.cache.Add(key,ev)
er.cache.Add(key, ev)
}
er.Logger.Infof("event has been created %v", ev)
} else {
// event already present in cache
e := lastSeenEvent.(*corev1.Event)
Expand All @@ -250,7 +259,8 @@ func (er *eventRecorder) RecordEvent(ctx context.Context, e *Event) error {
return err
}
// update the cache
er.cache.Add(key,e)
er.cache.Add(key, e)
er.Logger.Infof("event has been updated %v", ev)
}
return nil
}

0 comments on commit 940b2ec

Please sign in to comment.