handle create index duplicate error
@@ -131,10 +131,7 @@ func EnsureService() {
serviceIndexs = append(serviceIndexs, serviceIDIndex, serviceIndex)
err = client.GetMongoClient().CreateIndexes(context.Background(), CollectionService, serviceIndexs)
- if err != nil {
- log.Fatal("failed to create service collection indexs", err)
- return
- }
+ wrapCreateIndexesError(err)
}
func EnsureInstance() {
@@ -150,10 +147,7 @@ func EnsureInstance() {
instanceIndexs = append(instanceIndexs, instanceIndex, instanceServiceIndex)
err = client.GetMongoClient().CreateIndexes(context.Background(), CollectionInstance, instanceIndexs)
- if err != nil {
- log.Fatal("failed to create instance collection indexs", err)
- return
- }
+ wrapCreateIndexesError(err)
}
func EnsureSchema() {
@@ -169,10 +163,7 @@ func EnsureSchema() {
schemaIndexs = append(schemaIndexs, schemaServiceIndex)
err = client.GetMongoClient().CreateIndexes(context.Background(), CollectionSchema, schemaIndexs)
- if err != nil {
- log.Fatal("failed to create schema collection indexs", err)
- return
- }
+ wrapCreateIndexesError(err)
}
func EnsureRule() {
@@ -188,10 +179,7 @@ func EnsureRule() {
ruleIndexs = append(ruleIndexs, ruleServiceIndex)
err = client.GetMongoClient().CreateIndexes(context.Background(), CollectionRule, ruleIndexs)
- if err != nil {
- log.Fatal("failed to create rule collection indexs", err)
- return
- }
+ wrapCreateIndexesError(err)
}
func EnsureDep() {
@@ -224,6 +212,17 @@ func wrapCreateCollectionError(err error) {
}
}
+func wrapCreateIndexesError(err error) {
+ if err != nil {
+ // commandError can be returned by any operation
+ cmdErr, ok := err.(mongo.CommandError)
+ if ok && cmdErr.Code == client.DuplicateKey {
+ return
+ }
+ log.Fatal("failed to create indexes ", err)
+ }
+}
+
func (ds *DataSource) initStore() {
if !config.GetRegistry().EnableCache {
log.Debug("cache is disabled")
