Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add index to parsedVaa collection #616

Merged
merged 2 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,14 @@ func main() {

app.Use(requestid.New())
app.Use(logger.New(logger.Config{
Format: "level=info timestamp=${time} method=${method} path=${path} status${status} request_id=${locals:requestid}\n",
Format: "level=info timestamp=${time} method=${method} path=${path} latency=${latency} status${status} request_id=${locals:requestid}\n",
Next: func(c *fiber.Ctx) bool {
path := c.Path()
if path == "/api/v1/health" || path == "/api/v1/ready" {
return true
}
return false
},
}))
if cfg.PprofEnabled {
app.Use(pprof.New())
Expand Down
2 changes: 1 addition & 1 deletion deploy/api/env/production-mainnet.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ ENVIRONMENT=production-mainnet
NAMESPACE=wormscan
NAME=wormscan-api
PORT=8000
REPLICAS=2
REPLICAS=4
IMAGE_NAME=
RESOURCES_LIMITS_MEMORY=256Mi
RESOURCES_LIMITS_CPU=500m
Expand Down
7 changes: 7 additions & 0 deletions parser/cmd/service/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
parserAlert "github.com/wormhole-foundation/wormhole-explorer/parser/internal/alert"
"github.com/wormhole-foundation/wormhole-explorer/parser/internal/metrics"
"github.com/wormhole-foundation/wormhole-explorer/parser/internal/sqs"
"github.com/wormhole-foundation/wormhole-explorer/parser/migration"
"github.com/wormhole-foundation/wormhole-explorer/parser/parser"
"github.com/wormhole-foundation/wormhole-explorer/parser/processor"
"github.com/wormhole-foundation/wormhole-explorer/parser/queue"
Expand Down Expand Up @@ -58,6 +59,12 @@ func Run() {
logger.Fatal("failed to connect MongoDB", zap.Error(err))
}

// run the database migration.
err = migration.Run(db.Database)
if err != nil {
logger.Fatal("error running migration", zap.Error(err))
}

// get alert client.
alertClient, err := newAlertClient(config)
if err != nil {
Expand Down
37 changes: 37 additions & 0 deletions parser/migration/migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package migration

import (
"context"
"errors"

"github.com/wormhole-foundation/wormhole-explorer/parser/parser"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)

// TODO: move this to migration tool that support mongodb.
func Run(db *mongo.Database) error {
// Created parsedVaa collection.
err := db.CreateCollection(context.TODO(), parser.ParsedVAACollection)
if err != nil && isNotAlreadyExistsError(err) {
return err
}

// create index in observations collection by indexedAt.
indexToAddress := mongo.IndexModel{Keys: bson.D{{Key: "standardizedProperties.toAddress", Value: 1}}}
_, err = db.Collection(parser.ParsedVAACollection).Indexes().CreateOne(context.TODO(), indexToAddress)
if err != nil && isNotAlreadyExistsError(err) {
return err
}

return nil
}

func isNotAlreadyExistsError(err error) bool {
target := &mongo.CommandError{}
isCommandError := errors.As(err, target)
if !isCommandError || err.(mongo.CommandError).Code != 48 {
return true
}
return false
}
4 changes: 3 additions & 1 deletion parser/parser/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
// repository errors
var ErrDocNotFound = errors.New("NOT FOUND")

const ParsedVAACollection = "parsedVaa"

// Repository definitions.
type Repository struct {
db *mongo.Database
Expand All @@ -28,7 +30,7 @@ func NewRepository(db *mongo.Database, log *zap.Logger) *Repository {
return &Repository{db, log, struct {
parsedVaa *mongo.Collection
}{
parsedVaa: db.Collection("parsedVaa"),
parsedVaa: db.Collection(ParsedVAACollection),
}}
}

Expand Down
Loading