Skip to content

GraphQL server fails to start in case of MongoDB server (using AWS DocumentDB) #5125

Open
@prateekch33

Description

@prateekch33

What happened:
Getting the error:

{"error":"a collection 'litmus.chaosExperiments' already exists","file":"/gql-server/pkg/database/mongodb/init.go:154","func":"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb.(*MongoClient).initAllCollection","level":"error","msg":"failed to create chaosExperiments collection","time":"2025-05-09T05:48:46Z"}
{"error":"a collection 'litmus.chaosExperimentRuns' already exists","file":"/gql-server/pkg/database/mongodb/init.go:178","func":"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb.(*MongoClient).initAllCollection","level":"error","msg":"failed to create chaosExperimentRuns collection","time":"2025-05-09T05:48:46Z"}
{"error":"a collection 'litmus.chaosHubs' already exists","file":"/gql-server/pkg/database/mongodb/init.go:196","func":"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb.(*MongoClient).initAllCollection","level":"error","msg":"failed to create chaosHubs collection","time":"2025-05-09T05:48:46Z"}
{"error":"a collection 'litmus.chaosProbes' already exists","file":"/gql-server/pkg/database/mongodb/init.go:275","func":"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb.(*MongoClient).initAllCollection","level":"error","msg":"failed to create chaosProbes collection","time":"2025-05-09T05:48:46Z"}

What you expected to happen:
It should start the server without having this issue

Where can this issue be corrected? (optional)

After seeing the code, I found that it should be checked whether the Collection already exists or not, , in the below parts of the code:

// Initialize chaos infra collection
err := m.Database.CreateCollection(context.TODO(), Collections[ChaosInfraCollection], nil)
if err != nil {
logrus.WithError(err).Error("failed to create chaosInfrastructures collection")
}

err = m.Database.CreateCollection(context.TODO(), Collections[ChaosExperimentCollection], nil)
if err != nil {
logrus.WithError(err).Error("failed to create chaosExperiments collection")
}

err = m.Database.CreateCollection(context.TODO(), Collections[ChaosExperimentRunsCollection], nil)
if err != nil {
logrus.WithError(err).Error("failed to create chaosExperimentRuns collection")
}

err = m.Database.CreateCollection(context.TODO(), Collections[ChaosHubCollection], nil)
if err != nil {
logrus.WithError(err).Error("failed to create chaosHubs collection")
}

How to reproduce it (as minimally and precisely as possible):

Start the graphql server, configuring it with a MongoDB server with retryWrites false

Anything else we need to know?:

As in the authentication server, while initiating the mongodb service, there is a check for existence of Collection, if its not present then only its creating the collection, can that be possible here also.

Reference:

func CreateCollection(collectionName string, db *mongo.Database) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err := db.CreateCollection(ctx, collectionName)
if err != nil {
if strings.Contains(err.Error(), "already exists") {
log.Info(collectionName + "'s collection already exists, continuing with the existing mongo collection")
return nil
} else {
return err
}
}
log.Info(collectionName + "'s mongo collection created")
return nil
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions