Skip to content

Commit

Permalink
Merge pull request #155 from LiilyZhang/zhangl/Issue154
Browse files Browse the repository at this point in the history
Issue #154 - Bug: Failed to create index when it exists
  • Loading branch information
LiilyZhang committed Sep 9, 2024
2 parents 1dbcb25 + ea543b7 commit dfc2ff0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
24 changes: 9 additions & 15 deletions core/storage/mongoStorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type leaderDocument struct {
}

type isMasterResult struct {
IsMaster bool `bson:"isMaster"`
IsMaster bool `bson:"ismaster"`
LocalTime time.Time `bson:"localTime"`
OK bool `bson:"ok"`
}
Expand Down Expand Up @@ -104,15 +104,6 @@ type aclObject struct {
LastUpdate time.Time `bson:"last-update"`
}

type dataInfoObject struct {
ID string `bson:"_id"`
ChunkSize int32 `bson:"chunkSize"`
UploadDate time.Time `bson:"uploadDate"`
Length int32 `bson:"length"`
MD5 string `bson:"md5"`
Filename string `bson:"filename"`
}

const maxUpdateTries = 5

var sleepInMS int
Expand All @@ -138,7 +129,7 @@ func (store *MongoStorage) Init() common.SyncServiceError {
var mongoClient *mongo.Client
var err error
if trace.IsLogging(logger.INFO) {
trace.Info("CConnecting to mongo...")
trace.Info("Connecting to mongo...")
}
// Set up MongoDB client options
clientOptions := options.Client().ApplyURI(common.Configuration.MongoAddressCsv)
Expand Down Expand Up @@ -271,12 +262,15 @@ func (store *MongoStorage) Init() common.SyncServiceError {
{"metadata.destination-policy.services.service-name", -1},
},
Options: options.Index().SetName("syncObjects-destination-policy.services.service-id").SetSparse(true),
// need to set index name???
})

if err != nil {
message := fmt.Sprintf("Failed to create an index on %s. Error: %s", objects, err)
log.Error(message)
return &Error{message}
// check if error is IndexKeySpecsConflict
if casterr := err.(mongo.CommandError); casterr.Code != 86 {
message := fmt.Sprintf("Failed to create an index on %s. Error: %s", objects, err)
log.Error(message)
return &Error{message}
}
}

_, err = objectsCollection.Indexes().CreateOne(
Expand Down
2 changes: 1 addition & 1 deletion core/storage/mongoStorageHelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ func (store *MongoStorage) createFile(id string, data io.Reader) common.SyncServ

func (store *MongoStorage) run(cmd interface{}, result interface{}) common.SyncServiceError {
function := func(db *mongo.Database) error {
return db.RunCommand(context.TODO(), cmd).Decode(&result)
return db.RunCommand(context.TODO(), cmd).Decode(result)
}

retry, err := store.withDBHelper(function, true)
Expand Down

0 comments on commit dfc2ff0

Please sign in to comment.