Skip to content

Commit

Permalink
Merge branch 'fix/#5669/fix_migrations' into 'develop'
Browse files Browse the repository at this point in the history
Issue[#5669](develop): fix some migrations and set mongo client timeout to 0 in canopsis-reconfigure

See merge request canopsis/canopsis-pro!4286
  • Loading branch information
mmourcia committed Oct 30, 2024
2 parents 9015e1a + 62b82a0 commit 1a1b8d8
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ func main() {
logger.Fatal().Err(err).Msg("failed to initialize rabbitmq")
}

client, err := mongo.NewClient(ctx, 0, 0, logger)
// remove timeout to not limit long migrations
client, err := mongo.NewClientWithOptions(ctx, 0, 0, mongo.DefaultServerSelectionTimeout, 0, logger)
if err != nil {
logger.Fatal().Err(err).Msg("failed to connect to mongo")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (w *cleanPeriodicalWorker) Work(ctx context.Context) {
return
}

mongoClient, err := mongo.NewClientWithOptions(ctx, 0, 0, 0,
mongoClient, err := mongo.NewClientWithOptions(ctx, 0, 0, mongo.DefaultServerSelectionTimeout,
w.DataStorageConfigProvider.Get().MongoClientTimeout, w.Logger)
if err != nil {
w.Logger.Err(err).Msg("cannot connect to mongo")
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
db.createCollection("message_rate_statistic_minute", {capped: true, size: 100000, max: 1440});
if (!db.getCollectionNames().includes('message_rate_statistic_minute')) {
db.createCollection("message_rate_statistic_minute", {capped: true, size: 100000, max: 1440});
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ if (db.entity_service_counters.countDocuments() === 0) {
db.default_entities.find({
"type": "service"
}).forEach(function (doc) {
outputTemplate = doc.output_template
var outputTemplate = doc.output_template
if (outputTemplate === undefined) {
outputTemplate = ""
}
db.entity_service_counters.insert({
db.entity_service_counters.insertOne({
_id: doc._id,
output_template: outputTemplate
});
Expand Down
2 changes: 1 addition & 1 deletion community/go-engines-community/lib/api/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func Default(
logger.Info().Msg("Non-unique names for services ENABLED")
}

dbExportClient, err := mongo.NewClientWithOptions(ctx, 0, 0, 0,
dbExportClient, err := mongo.NewClientWithOptions(ctx, 0, 0, mongo.DefaultServerSelectionTimeout,
p.ApiConfigProvider.Get().ExportMongoClientTimeout, logger)
if err != nil {
return nil, nil, fmt.Errorf("cannot connect to mongodb: %w", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (w *worker) processTask(ctx context.Context, task CleanTask) {
}

func (w *worker) doTask(ctx context.Context, task CleanTask) {
dbClient, err := mongo.NewClientWithOptions(ctx, 0, 0, 0,
dbClient, err := mongo.NewClientWithOptions(ctx, 0, 0, mongo.DefaultServerSelectionTimeout,
w.dataStorageConfigProvider.Get().MongoClientTimeout, w.logger)
if err != nil {
w.logger.Err(err).Msg("cannot connect to mongo")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (w *cleanExternalTagPeriodicalWorker) Work(ctx context.Context) {
return
}

mongoClient, err := mongo.NewClientWithOptions(ctx, 0, 0, 0, dataStorageConf.MongoClientTimeout, w.Logger)
mongoClient, err := mongo.NewClientWithOptions(ctx, 0, 0, mongo.DefaultServerSelectionTimeout, dataStorageConf.MongoClientTimeout, w.Logger)
if err != nil {
w.Logger.Err(err).Msg("cannot connect to mongo")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (w *resolvedArchiverWorker) Work(ctx context.Context) {
return
}

mongoClient, err := mongo.NewClientWithOptions(ctx, 0, 0, 0,
mongoClient, err := mongo.NewClientWithOptions(ctx, 0, 0, mongo.DefaultServerSelectionTimeout,
w.DataStorageConfigProvider.Get().MongoClientTimeout, w.Logger)
if err != nil {
w.Logger.Err(err).Msg("cannot connect to mongo")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (w *cleanPeriodicalWorker) Work(ctx context.Context) {
return
}

mongoClient, err := mongo.NewClientWithOptions(ctx, 0, 0, 0,
mongoClient, err := mongo.NewClientWithOptions(ctx, 0, 0, mongo.DefaultServerSelectionTimeout,
w.DataStorageConfigProvider.Get().MongoClientTimeout, w.Logger)
if err != nil {
w.Logger.Err(err).Msg("cannot connect to mongo")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (w *deleteOutdatedRatesWorker) Work(ctx context.Context) {

d := conf.Config.HealthCheck.DeleteAfter
if datetime.IsDurationEnabledAndValid(d) {
mongoClient, err := mongo.NewClientWithOptions(ctx, 0, 0, 0,
mongoClient, err := mongo.NewClientWithOptions(ctx, 0, 0, mongo.DefaultServerSelectionTimeout,
w.DataStorageConfigProvider.Get().MongoClientTimeout, w.Logger)
if err != nil {
w.Logger.Err(err).Msg("cannot connect to mongo")
Expand Down
23 changes: 13 additions & 10 deletions community/go-engines-community/lib/mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import (
)

const (
defaultClientTimeout = 15 * time.Second
disableRetries contextKey = "disable_retries"
DefaultClientTimeout = 15 * time.Second
DefaultServerSelectionTimeout = 30 * time.Second

disableRetries contextKey = "disable_retries"

topologyCheckTimeout = 1 * time.Second

Expand Down Expand Up @@ -371,7 +373,7 @@ func NewClient(ctx context.Context, retryCount int, minRetryTimeout time.Duratio

clientOptions := options.Client().ApplyURI(mongoURL)
if clientOptions.Timeout == nil {
clientOptions.SetTimeout(defaultClientTimeout)
clientOptions.SetTimeout(DefaultClientTimeout)
}
client, err := mongo.Connect(ctx, clientOptions)
if err != nil {
Expand Down Expand Up @@ -415,15 +417,16 @@ func NewClientWithOptions(
}

clientOptions := options.Client().ApplyURI(mongoURL)
if serverSelectionTimeout > 0 {
clientOptions.SetServerSelectionTimeout(serverSelectionTimeout)
}
if clientTimeout > 0 {
clientOptions.SetTimeout(clientTimeout)
if serverSelectionTimeout < 0 {
serverSelectionTimeout = DefaultServerSelectionTimeout
}
if clientOptions.Timeout == nil {
clientOptions.SetTimeout(defaultClientTimeout)
if clientTimeout < 0 {
clientTimeout = DefaultClientTimeout
}

clientOptions.SetServerSelectionTimeout(serverSelectionTimeout)
clientOptions.SetTimeout(clientTimeout)

client, err := mongo.Connect(ctx, clientOptions)
if err != nil {
return nil, err
Expand Down

0 comments on commit 1a1b8d8

Please sign in to comment.