From 2554a0c9282a7f41ff9c8f7802f3cede6d4890e3 Mon Sep 17 00:00:00 2001 From: Julian Spierefka <Julian.Spierefka@telekom.de> Date: Wed, 19 Jun 2024 14:32:26 +0200 Subject: [PATCH] feat: update healthCheckCache --- golaris/circuit_breaker.go | 5 +++-- golaris/health_check.go | 23 +++++++++++++---------- {cache => health}/cache.go | 19 +++---------------- mock/circuit_breaker.go | 2 +- service/service.go | 23 +++++++++++++++-------- 5 files changed, 35 insertions(+), 37 deletions(-) rename {cache => health}/cache.go (63%) diff --git a/golaris/circuit_breaker.go b/golaris/circuit_breaker.go index 80230f2..a24f27a 100644 --- a/golaris/circuit_breaker.go +++ b/golaris/circuit_breaker.go @@ -4,11 +4,12 @@ import ( "eni.telekom.de/horizon2go/pkg/cache" "eni.telekom.de/horizon2go/pkg/message" "eni.telekom.de/horizon2go/pkg/resource" + "github.com/hazelcast/hazelcast-go-client" "github.com/rs/zerolog/log" "golaris/config" ) -func checkSubscriptionForCbMessage(subCache *cache.Cache[resource.SubscriptionResource], cbMessage message.CircuitBreakerMessage) { +func checkSubscriptionForCbMessage(subCache *cache.Cache[resource.SubscriptionResource], healthCache *hazelcast.Map, cbMessage message.CircuitBreakerMessage) { subscriptionId := cbMessage.SubscriptionId subscription, err := subCache.Get(config.Current.Hazelcast.Caches.SubscriptionCache, subscriptionId) @@ -21,5 +22,5 @@ func checkSubscriptionForCbMessage(subCache *cache.Cache[resource.SubscriptionRe // ToDo: Check whether the subscription has changed - go performHealthCheck(subscription) + go performHealthCheck(subscription, healthCache) } diff --git a/golaris/health_check.go b/golaris/health_check.go index 13985a8..047a798 100644 --- a/golaris/health_check.go +++ b/golaris/health_check.go @@ -4,12 +4,13 @@ import ( "context" "eni.telekom.de/horizon2go/pkg/resource" "fmt" + "github.com/hazelcast/hazelcast-go-client" "github.com/rs/zerolog/log" - "golaris/cache" + "golaris/health" "time" ) -func performHealthCheck(subscription *resource.SubscriptionResource) { +func performHealthCheck(subscription *resource.SubscriptionResource, healthCache *hazelcast.Map) { // Specify HTTP method based on the subscription configuration httpMethod := "HEAD" if subscription.Spec.Subscription.EnforceGetHealthCheck == true { @@ -20,31 +21,30 @@ func performHealthCheck(subscription *resource.SubscriptionResource) { log.Info().Msgf("Created healthCheckKey is: %s", healthCheckKey) // Attempt to acquire a lock for the health check key - ctx := cache.HealthCheckMap.NewLockContext(context.Background()) - if acquired, _ := cache.HealthCheckMap.TryLockWithTimeout(ctx, healthCheckKey, 10*time.Millisecond); !acquired { + ctx := healthCache.NewLockContext(context.Background()) + if acquired, _ := healthCache.TryLockWithTimeout(ctx, healthCheckKey, 10*time.Millisecond); !acquired { log.Info().Msgf("Could not acquire lock for key %s, skipping health check", healthCheckKey) return } // Ensure that the lock is released when the function is ended defer func() { - if err := cache.HealthCheckMap.Unlock(context.Background(), healthCheckKey); err != nil { + if err := healthCache.Unlock(context.Background(), healthCheckKey); err != nil { log.Error().Err(err).Msgf("Error unlocking key %s", healthCheckKey) } }() //Check if there is already a HealthCheck entry for the HealthCheckKey - existingHealthCheck, err := cache.HealthCheckMap.Get(context.Background(), healthCheckKey) + existingHealthCheck, err := healthCache.Get(context.Background(), healthCheckKey) if err != nil { log.Error().Err(err).Msgf("Failed to retrieve health check for key %s", healthCheckKey) } else if existingHealthCheck != nil { //ToDo: What do we want to do in this case? log.Info().Msgf("Health check for key %s already exists, skipping health check", healthCheckKey) - return } // Prepare the health check object - healthCheck := cache.HealthCheck{ + healthCheckData := health.HealthCheck{ Environment: subscription.Spec.Environment, Method: httpMethod, CallbackUrl: subscription.Spec.Subscription.Callback, @@ -53,12 +53,15 @@ func performHealthCheck(subscription *resource.SubscriptionResource) { LastedCheckedStatus: 0, } - // Update the health check in the cache - err = cache.HealthCheckMap.Set(context.Background(), healthCheckKey, healthCheck) + // Update the health check in the health + err = healthCache.Set(context.Background(), healthCheckKey, healthCheckData) if err != nil { log.Error().Err(err).Msgf("Failed to update health check for key %s", healthCheckKey) return } + existingHealthCheck2, _ := healthCache.Get(context.Background(), healthCheckKey) + log.Info().Msgf("Health check for key %s is: %v", healthCheckKey, existingHealthCheck2) + log.Info().Msgf("Successfully updated health check for key %s", healthCheckKey) } diff --git a/cache/cache.go b/health/cache.go similarity index 63% rename from cache/cache.go rename to health/cache.go index 5c7527a..4f9b309 100644 --- a/cache/cache.go +++ b/health/cache.go @@ -1,9 +1,8 @@ -package cache +package health import ( "context" "github.com/hazelcast/hazelcast-go-client" - "github.com/rs/zerolog/log" "time" ) @@ -16,18 +15,6 @@ type HealthCheck struct { LastedCheckedStatus int `json:"lastCheckedStatus"` } -var HealthCheckMap *hazelcast.Map - -func init() { - cacheConfig := hazelcast.Config{} - - var err error - HealthCheckMap, err = NewHealthCheckCache(cacheConfig) - if err != nil { - log.Error().Msgf("Failed to initialize health check cache: %v", err) - } -} - func NewHealthCheckCache(config hazelcast.Config) (*hazelcast.Map, error) { hazelcastClient, err := hazelcast.StartNewClientWithConfig(context.Background(), config) if err != nil { @@ -35,10 +22,10 @@ func NewHealthCheckCache(config hazelcast.Config) (*hazelcast.Map, error) { } mapName := "healthCheckEntries" - HealthCheckMap, err = hazelcastClient.GetMap(context.Background(), mapName) + HealthCheckInstance, err := hazelcastClient.GetMap(context.Background(), mapName) if err != nil { return nil, err } - return HealthCheckMap, nil + return HealthCheckInstance, nil } diff --git a/mock/circuit_breaker.go b/mock/circuit_breaker.go index e182a0a..a8c4a58 100644 --- a/mock/circuit_breaker.go +++ b/mock/circuit_breaker.go @@ -14,7 +14,7 @@ func CreateMockedCircuitBreakerMessages(cbCache *cache.Cache[message.CircuitBrea for i := 1; i <= numberMessages; i++ { - subscriptionId := "subscriptionId" + subscriptionId := "" circuitBreakerMessage := message.CircuitBreakerMessage{ SubscriptionId: subscriptionId, diff --git a/service/service.go b/service/service.go index c7aabef..a60fa7b 100644 --- a/service/service.go +++ b/service/service.go @@ -13,6 +13,7 @@ import ( "github.com/rs/zerolog/log" "golaris/config" "golaris/golaris" + "golaris/health" "golaris/kafka" "golaris/metrics" "golaris/mock" @@ -21,11 +22,12 @@ import ( ) var ( - SubCacheInstance *cache.Cache[resource.SubscriptionResource] - CbCacheInstance *cache.Cache[message.CircuitBreakerMessage] - kafkaHandler *kafka.Handler - app *fiber.App - mongoConnection *mongo.Connection + SubCacheInstance *cache.Cache[resource.SubscriptionResource] + CbCacheInstance *cache.Cache[message.CircuitBreakerMessage] + HealthCheckInstance *hazelcast.Map + kafkaHandler *kafka.Handler + app *fiber.App + mongoConnection *mongo.Connection ) func InitializeService() { @@ -42,12 +44,17 @@ func InitializeService() { cacheConfig := configureHazelcast() SubCacheInstance, err = cache.NewCache[resource.SubscriptionResource](cacheConfig) if err != nil { - log.Panic().Err(err).Msg("Error while initializing Hazelcast subscription cache") + log.Panic().Err(err).Msg("Error while initializing Hazelcast subscription health") } CbCacheInstance, err = cache.NewCache[message.CircuitBreakerMessage](cacheConfig) if err != nil { - log.Panic().Err(err).Msg("Error while initializing CircuitBreaker cache") + log.Panic().Err(err).Msg("Error while initializing CircuitBreaker health") + } + + HealthCheckInstance, err = health.NewHealthCheckCache(cacheConfig) + if err != nil { + log.Panic().Err(err).Msg("Error while initializing HealthCheck cache") } mongoConnection, err = mongo.NewMongoConnection(&config.Current.Mongo) @@ -60,7 +67,7 @@ func InitializeService() { log.Panic().Err(err).Msg("Error while initializing Kafka Picker") } - golaris.InitializeScheduler(CbCacheInstance, SubCacheInstance) + golaris.InitializeScheduler(CbCacheInstance, SubCacheInstance, HealthCheckInstance) // TODO Mock cb-messages until comet is adapted mock.CreateMockedCircuitBreakerMessages(CbCacheInstance, 1)