Skip to content

Commit

Permalink
Merge pull request #533 from threefoldtech/dev_proxy_optimization
Browse files Browse the repository at this point in the history
optimize database queries
  • Loading branch information
xmonader authored Dec 27, 2023
2 parents fd83ee0 + 595b5fa commit b9bd33c
Show file tree
Hide file tree
Showing 35 changed files with 2,868 additions and 1,796 deletions.
19 changes: 17 additions & 2 deletions grid-proxy/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.DEFAULT_GOAL := help
PQ_HOST = $(shell docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' postgres)
PQ_CONTAINER = postgres
count = 3

install-swag:
@go install github.com/swaggo/swag/cmd/[email protected];
Expand Down Expand Up @@ -41,7 +42,7 @@ db-stop: ## Stop the database container if running
@if [ ! "$(shell docker ps | grep '$(PQ_CONTAINER)' )" = "" ]; then \
docker stop postgres; \
fi

db-refill: db-stop db-start sleep db-fill
server-start: ## Start the proxy server (Args: `m=<MNEMONICS>`)
@go run cmds/proxy_server/main.go \
-no-cert \
Expand All @@ -61,22 +62,26 @@ sleep:
test-queries: ## Run all queries tests
@cd tests/queries/ &&\
go test -v \
-parallel 20 \
--seed 13 \
--postgres-host $(PQ_HOST) \
--postgres-db tfgrid-graphql \
--postgres-password postgres \
--postgres-user postgres \
--endpoint http://localhost:8080
--endpoint http://localhost:8080 \
-count $(count)

test-query: ## Run specific test query (Args: `t=TestName`).
@cd tests/queries/ &&\
go test -v \
-parallel 10 \
--seed 13 \
--postgres-host $(PQ_HOST) \
--postgres-db tfgrid-graphql \
--postgres-password postgres \
--postgres-user postgres \
--endpoint http://localhost:8080 \
-count $(count) \
-run $(t)

test-unit: ## Run only unit tests
Expand Down Expand Up @@ -124,3 +129,13 @@ spelling:
staticcheck:
@echo "Running $@"
@$(shell go env GOPATH)/bin/staticcheck -- ./...

bench:
@cd tests/queries/ &&\
go test -v -bench Bench -run notests -count 5\
--seed 13 \
--postgres-host $(PQ_HOST) \
--postgres-db tfgrid-graphql \
--postgres-password postgres \
--postgres-user postgres \
--endpoint http://localhost:8080
13 changes: 10 additions & 3 deletions grid-proxy/cmds/proxy_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
logging "github.com/threefoldtech/tfgrid-sdk-go/grid-proxy/pkg"
rmb "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go"
"github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer"
"gorm.io/gorm/logger"
)

const (
Expand All @@ -39,6 +40,7 @@ type flags struct {
postgresDB string
postgresUser string
postgresPassword string
sqlLogLevel int
address string
version bool
nocert bool
Expand All @@ -65,6 +67,7 @@ func main() {
flag.StringVar(&f.postgresDB, "postgres-db", "", "postgres database")
flag.StringVar(&f.postgresUser, "postgres-user", "", "postgres username")
flag.StringVar(&f.postgresPassword, "postgres-password", "", "postgres password")
flag.IntVar(&f.sqlLogLevel, "sql-log-level", 2, "sql logger level")
flag.BoolVar(&f.version, "v", false, "shows the package version")
flag.BoolVar(&f.nocert, "no-cert", false, "start the server without certificate")
flag.StringVar(&f.domain, "domain", "", "domain on which the server will be served")
Expand Down Expand Up @@ -108,18 +111,22 @@ func main() {
log.Fatal().Err(err).Msg("failed to create relay client")
}

db, err := db.NewPostgresDatabase(f.postgresHost, f.postgresPort, f.postgresUser, f.postgresPassword, f.postgresDB, f.maxPoolOpenConnections)
db, err := db.NewPostgresDatabase(f.postgresHost, f.postgresPort, f.postgresUser, f.postgresPassword, f.postgresDB, f.maxPoolOpenConnections, logger.LogLevel(f.sqlLogLevel))
if err != nil {
log.Fatal().Err(err).Msg("couldn't get postgres client")
}

dbClient := explorer.DBClient{DB: db}
if err := db.Initialize(); err != nil {
log.Fatal().Err(err).Msg("failed to initialize database")
}

dbClient := explorer.DBClient{DB: &db}

indexer, err := gpuindexer.NewNodeGPUIndexer(
ctx,
f.relayURL,
f.mnemonics,
subManager, db,
subManager, &db,
f.indexerCheckIntervalMins,
f.indexerBatchSize,
f.indexerResultWorkers,
Expand Down
8 changes: 4 additions & 4 deletions grid-proxy/internal/explorer/converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ func nodeFromDBNode(info db.Node) types.Node {
InDedicatedFarm: info.FarmDedicated,
CertificationType: info.Certification,
RentContractID: uint(info.RentContractID),
RentedByTwinID: uint(info.RentedByTwinID),
RentedByTwinID: uint(info.Renter),
SerialNumber: info.SerialNumber,
Power: types.NodePower(info.Power),
NumGPU: info.NumGPU,
ExtraFee: info.ExtraFee,
}
node.Status = nodestatus.DecideNodeStatus(node.Power, node.UpdatedAt)
node.Dedicated = info.FarmDedicated || !info.HasNodeContract || info.RentContractID != 0
node.Dedicated = info.FarmDedicated || info.NodeContractsCount == 0 || info.Renter != 0
return node
}

Expand Down Expand Up @@ -124,14 +124,14 @@ func nodeWithNestedCapacityFromDBNode(info db.Node) types.NodeWithNestedCapacity
InDedicatedFarm: info.FarmDedicated,
CertificationType: info.Certification,
RentContractID: uint(info.RentContractID),
RentedByTwinID: uint(info.RentedByTwinID),
RentedByTwinID: uint(info.Renter),
SerialNumber: info.SerialNumber,
Power: types.NodePower(info.Power),
NumGPU: info.NumGPU,
ExtraFee: info.ExtraFee,
}
node.Status = nodestatus.DecideNodeStatus(node.Power, node.UpdatedAt)
node.Dedicated = info.FarmDedicated || !info.HasNodeContract || info.RentContractID != 0
node.Dedicated = info.FarmDedicated || info.NodeContractsCount == 0 || info.Renter != 0
return node
}

Expand Down
Loading

0 comments on commit b9bd33c

Please sign in to comment.