Skip to content

Commit

Permalink
feat: added Hazelcast.CustomLoggerEnabled, ForeceDelete and IncreaseR…
Browse files Browse the repository at this point in the history
…epublishingCount
  • Loading branch information
Schnix84 committed Jun 28, 2024
1 parent f2e1956 commit f91ac8f
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 34 deletions.
31 changes: 1 addition & 30 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ require (
github.com/go-co-op/gocron v1.37.0
github.com/gofiber/contrib/otelfiber/v2 v2.1.0
github.com/gofiber/fiber/v2 v2.52.4
github.com/google/uuid v1.6.0
github.com/hazelcast/hazelcast-go-client v1.4.1
github.com/prometheus/client_golang v1.19.1
github.com/rs/zerolog v1.33.0
Expand All @@ -24,9 +23,6 @@ require (
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.27.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.27.0
go.opentelemetry.io/otel/sdk v1.27.0
k8s.io/api v0.30.2
k8s.io/apimachinery v0.30.2
k8s.io/client-go v0.30.2
)

require (
Expand All @@ -39,44 +35,30 @@ require (
github.com/eapache/go-resiliency v1.6.0 // indirect
github.com/eapache/go-xerial-snappy v0.0.0-20230731223053-c322873962e3 // indirect
github.com/eapache/queue v1.1.0 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jcmturner/aescts/v2 v2.0.0 // indirect
github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect
github.com/jcmturner/gofork v1.7.6 // indirect
github.com/jcmturner/gokrb5/v8 v8.4.4 // indirect
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.8 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
Expand Down Expand Up @@ -111,24 +93,13 @@ require (
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/oauth2 v0.20.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240520151616-dc85e6b867a5 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240515191416-fc5f0ca64291 // indirect
google.golang.org/grpc v1.64.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
5 changes: 4 additions & 1 deletion internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ func createNewHazelcastConfig() hazelcast.Config {

cacheConfig.Cluster.Name = config.Current.Hazelcast.ClusterName
cacheConfig.Cluster.Network.SetAddresses(config.Current.Hazelcast.ServiceDNS)
cacheConfig.Logger.CustomLogger = new(util.HazelcastZerologLogger)

if config.Current.Hazelcast.CustomLoggerEnabled {
cacheConfig.Logger.CustomLogger = new(util.HazelcastZerologLogger)
}

return cacheConfig
}
Expand Down
30 changes: 30 additions & 0 deletions internal/circuit_breaker/circuit_breaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,39 @@ import (
"golaris/internal/config"
)

// TODO lock/unlock the circuit breaker entry?
// TODO set the status of the circuit breaker to closed instead of deleting the entry
func CloseCircuitBreaker(subscriptionId string) {
if err := cache.CircuitBreakers.Delete(config.Current.Hazelcast.Caches.CircuitBreakerCache, subscriptionId); err != nil {
log.Error().Err(err).Msgf("Error: %v while closing circuit breaker for subscription %s", err, subscriptionId)
return
}
}

// TODO lock/unlock the circuit breaker entry?
// IncreaseRepublishingCount increments the republishing count for a given subscription.
// The function first retrieves the CircuitBreaker message associated with the subscriptionId from the cache.
// If the CircuitBreaker message is successfully retrieved, the republishing count of the message is incremented.
// The updated CircuitBreaker message is then put back into the cache.
// If any error occurs during these operations, it is logged and the function returns immediately.
//
// Parameters:
// subscriptionId: The ID of the subscription for which the republishing count is to be incremented.
//
// Returns:
// This function does not return a value.
func IncreaseRepublishingCount(subscriptionId string) {
cbMessage, err := cache.CircuitBreakers.Get(config.Current.Hazelcast.Caches.CircuitBreakerCache, subscriptionId)
if err != nil {
log.Error().Err(err).Msgf("Error while getting CircuitBreaker message for subscription %s", subscriptionId)
return
}

cbMessage.RepublishingCount++
if err := cache.CircuitBreakers.Put(config.Current.Hazelcast.Caches.CircuitBreakerCache, subscriptionId, *cbMessage); err != nil {
log.Error().Err(err).Msgf("Error while updating CircuitBreaker message for subscription %s", subscriptionId)
return
}

log.Debug().Msgf("Successfully increased RepublishingCount to %d for subscription %s", cbMessage.RepublishingCount, subscriptionId)
}
7 changes: 4 additions & 3 deletions internal/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ type Kubernetes struct {
}

type Hazelcast struct {
ServiceDNS string `mapstructure:"serviceDNS"`
ClusterName string `mapstructure:"clusterName"`
Caches Caches `mapstructure:"caches"`
ServiceDNS string `mapstructure:"serviceDNS"`
ClusterName string `mapstructure:"clusterName"`
Caches Caches `mapstructure:"caches"`
CustomLoggerEnabled bool `mapstructure:"customLoggerEnabled"`
}

type Polling struct {
Expand Down
8 changes: 8 additions & 0 deletions internal/health_check/health_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ func PerformHealthCheck(cbMessage message.CircuitBreakerMessage, subscription *r
log.Debug().Msgf("Successfully unlocked key %s", healthCheckKey)
}()

// ForceDelete eventual existing RepublishingCache entry for the subscriptionId
if republish.ForceDelete(cbMessage.SubscriptionId, ctx) == false {
return
}

// Increase the republishing count for the subscription by 1
circuit_breaker.IncreaseRepublishingCount(cbMessage.SubscriptionId)

//Check if there is already a HealthCheck entry for the HealthCheckKey
if shouldSkipHealthCheck(ctx, healthCheckKey) {
return
Expand Down
56 changes: 56 additions & 0 deletions internal/republish/republish.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
package republish

import (
"context"
"github.com/rs/zerolog/log"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
"golaris/internal/cache"
"golaris/internal/config"
"golaris/internal/kafka"
"golaris/internal/mongo"
Expand Down Expand Up @@ -71,3 +73,57 @@ func RepublishPendingEvents(subscriptionId string) {
page++
}
}

// ForceDelete attempts to delete an entry from the RepublishingCache for a given subscriptionId.
// The function first tries to retrieve the entry from the cache. If the entry does not exist, the function returns true.
// If the entry exists, the function checks if it is locked. If it is locked, the function attempts to unlock it.
// After ensuring the entry is not locked, the function attempts to delete the entry from the cache.
// If any of the operations (getting, checking lock, unlocking, deleting) fail, the function logs the error and returns false.
// If the entry is successfully deleted, the function logs a success message and returns true.
//
// Parameters:
// - subscriptionId: The ID of the subscription for which the cache entry should be deleted.
// - ctx: The context within which the function should operate. This is typically used for timeout and cancellation signals.
//
// Returns:
// - bool: Returns true if the operation is successful (either the entry did not exist or it was successfully deleted). Returns false if any operation fails.
func ForceDelete(subscriptionId string, ctx context.Context) bool {
// Attempt to get the entry and check if it's locked
entry, err := cache.RepublishingCache.Get(ctx, subscriptionId)
if err != nil {
log.Error().Err(err).Msgf("Error getting entry from RepublishingCache for subscriptionId %s", subscriptionId)
return false
}

// If there is no entry, nothing to delete
if entry == nil {
log.Debug().Msgf("No RepublishingCache entry found for subscriptionId %s", subscriptionId)
return true
}

// Check if the entry is locked
isLocked, err := cache.RepublishingCache.IsLocked(ctx, subscriptionId)
if err != nil {
log.Error().Err(err).Msgf("Error checking if RepublishingCache entry is locked for subscriptionId %s", subscriptionId)
return false
}

// If locked, unlock it
if isLocked {
err = cache.RepublishingCache.ForceUnlock(ctx, subscriptionId)
if err != nil {
log.Error().Err(err).Msgf("Error unlocking RepublishingCache entry for subscriptionId %s", subscriptionId)
return false
}
}

// Delete the entry
err = cache.RepublishingCache.Delete(ctx, subscriptionId)
if err != nil {
log.Error().Err(err).Msgf("Error deleting RepublishingCache entry for subscriptionId %s", subscriptionId)
return false
}

log.Info().Msgf("Successfully deleted RepublishingCache entry for subscriptionId %s", subscriptionId)
return true
}
3 changes: 3 additions & 0 deletions internal/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func StartScheduler() {
scheduler.StartAsync()
}

// TODO why public?
func CheckCircuitBreakersByStatus(status enum.CircuitBreakerStatus) {
statusQuery := predicate.Equal("status", string(status))
cbEntries, err := cache.CircuitBreakers.GetQuery(config.Current.Hazelcast.Caches.CircuitBreakerCache, statusQuery)
Expand All @@ -51,9 +52,11 @@ func CheckCircuitBreakersByStatus(status enum.CircuitBreakerStatus) {

// ToDo: Check whether the subscription has changed
go health_check.PerformHealthCheck(entry, subscription)

}
}

// TODO why public?
func GetSubscriptionForCbMessage(subscriptionId string) *resource.SubscriptionResource {
subscription, err := cache.Subscriptions.Get(config.Current.Hazelcast.Caches.SubscriptionCache, subscriptionId)
if err != nil {
Expand Down

0 comments on commit f91ac8f

Please sign in to comment.