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

update schema.graphql with denormalized schema #132

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions db/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const ormconfig = {

require('typeorm').createConnection(ormconfig).then(async con => {
try {
await con.runMigrations({transaction: 'all'})
await con.runMigrations({ transaction: 'all' })
} finally {
await con.close().catch(err => null)
await con.destroy().catch(err => null)
}
}).then(
() => process.exit(),
Expand Down
65 changes: 65 additions & 0 deletions db/migrations/1694605589133-denormalize_data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
module.exports = class denormalize_data1694605589133 {
name = 'denormalize_data1694605589133'

async up(db) {
await db.query(`CREATE TABLE "contract" ("id" character varying NOT NULL, "grid_version" integer NOT NULL, "contract_id" numeric NOT NULL, "twin_id" integer NOT NULL, "node_id" integer, "deployment_data" text, "deployment_hash" text, "number_of_public_i_ps" integer, "state" character varying(11) NOT NULL, "created_at" numeric NOT NULL, "solution_provider_id" integer, "used_cru" numeric, "used_mru" numeric, "used_sru" numeric, "used_hru" numeric, "name" text, "type" character varying(4) NOT NULL, CONSTRAINT "PK_17c3a89f58a2997276084e706e8" PRIMARY KEY ("id"))`)
await db.query(`CREATE INDEX "IDX_2f25fae55a3bd80337501b310e" ON "contract" ("contract_id") `)
await db.query(`CREATE INDEX "IDX_e9713e5dd661bc93d698d725e8" ON "contract" ("state") `)
await db.query(`CREATE INDEX "IDX_59d5a7aca69d7ea58c101a44c6" ON "contract" ("type") `)
await db.query(`ALTER TABLE "farm" ADD "total_ips" integer NOT NULL`)
await db.query(`ALTER TABLE "farm" ADD "free_ips" integer NOT NULL`)
await db.query(`ALTER TABLE "node" ADD "total_cru" numeric NOT NULL`)
await db.query(`ALTER TABLE "node" ADD "total_hru" numeric NOT NULL`)
await db.query(`ALTER TABLE "node" ADD "total_sru" numeric NOT NULL`)
await db.query(`ALTER TABLE "node" ADD "total_mru" numeric NOT NULL`)
await db.query(`ALTER TABLE "node" ADD "free_mru" numeric NOT NULL`)
await db.query(`ALTER TABLE "node" ADD "free_hru" numeric NOT NULL`)
await db.query(`ALTER TABLE "node" ADD "free_sru" numeric NOT NULL`)
await db.query(`ALTER TABLE "node" ADD "rented_by" integer`)
await db.query(`ALTER TABLE "public_ip" ALTER COLUMN "contract_id" DROP NOT NULL`)
await db.query(`CREATE INDEX "IDX_a162f043823849732649d40627" ON "twin" ("twin_id") `)
await db.query(`CREATE INDEX "IDX_5f29012fe3403c0cf194457ecd" ON "public_ip" ("contract_id") `)
await db.query(`CREATE INDEX "IDX_a09d726d0ff006b47ff4d25428" ON "farm" ("farm_id") `)
await db.query(`CREATE INDEX "IDX_e6523793483b172e422dc225ad" ON "node" ("node_id") `)
await db.query(`CREATE INDEX "IDX_4d4375648edaa07add3df9d876" ON "node" ("free_mru") `)
await db.query(`CREATE INDEX "IDX_b6ff61e1f0b66b70374d1168d4" ON "node" ("free_hru") `)
await db.query(`CREATE INDEX "IDX_1d93b5617168669d707d52077d" ON "node" ("free_sru") `)
await db.query(`CREATE INDEX "IDX_23c232066e25d61f3a703b1c08" ON "contract_bill_report" ("contract_id") `)
await db.query(`DROP TABLE "rent_contract"`)
await db.query(`DROP TABLE "name_contract"`)
await db.query(`DROP TABLE "contract_resources" cascade`)
await db.query(`DROP TABLE "node_contract" cascade`)
}

async down(db) {
await db.query(`DROP TABLE "contract"`)
await db.query(`DROP INDEX "public"."IDX_2f25fae55a3bd80337501b310e"`)
await db.query(`DROP INDEX "public"."IDX_e9713e5dd661bc93d698d725e8"`)
await db.query(`DROP INDEX "public"."IDX_59d5a7aca69d7ea58c101a44c6"`)
await db.query(`ALTER TABLE "farm" DROP COLUMN "total_ips"`)
await db.query(`ALTER TABLE "farm" DROP COLUMN "free_ips"`)
await db.query(`ALTER TABLE "node" DROP COLUMN "total_cru"`)
await db.query(`ALTER TABLE "node" DROP COLUMN "total_hru"`)
await db.query(`ALTER TABLE "node" DROP COLUMN "total_sru"`)
await db.query(`ALTER TABLE "node" DROP COLUMN "total_mru"`)
await db.query(`ALTER TABLE "node" DROP COLUMN "free_mru"`)
await db.query(`ALTER TABLE "node" DROP COLUMN "free_hru"`)
await db.query(`ALTER TABLE "node" DROP COLUMN "free_sru"`)
await db.query(`ALTER TABLE "node" DROP COLUMN "rented_by"`)
await db.query(`ALTER TABLE "public_ip" ALTER COLUMN "contract_id" SET NOT NULL`)
await db.query(`DROP INDEX "public"."IDX_a162f043823849732649d40627"`)
await db.query(`DROP INDEX "public"."IDX_5f29012fe3403c0cf194457ecd"`)
await db.query(`DROP INDEX "public"."IDX_a09d726d0ff006b47ff4d25428"`)
await db.query(`DROP INDEX "public"."IDX_e6523793483b172e422dc225ad"`)
await db.query(`DROP INDEX "public"."IDX_4d4375648edaa07add3df9d876"`)
await db.query(`DROP INDEX "public"."IDX_b6ff61e1f0b66b70374d1168d4"`)
await db.query(`DROP INDEX "public"."IDX_1d93b5617168669d707d52077d"`)
await db.query(`DROP INDEX "public"."IDX_23c232066e25d61f3a703b1c08"`)
await db.query(`CREATE TABLE "node_contract" ("id" character varying NOT NULL, "grid_version" integer NOT NULL, "contract_id" numeric NOT NULL, "twin_id" integer NOT NULL, "node_id" integer NOT NULL, "deployment_data" text NOT NULL, "deployment_hash" text NOT NULL, "number_of_public_i_ps" integer NOT NULL, "state" character varying(11) NOT NULL, "created_at" numeric NOT NULL, "resources_used_id" character varying, "solution_provider_id" integer, CONSTRAINT "PK_a5f90b17f504ffcd79d1f66574a" PRIMARY KEY ("id"))`)
await db.query(`ALTER TABLE "node_contract" ADD CONSTRAINT "FK_f294cfb50bb7c7b976d86c08fda" FOREIGN KEY ("resources_used_id") REFERENCES "contract_resources"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`)
await db.query(`CREATE TABLE "rent_contract" ("id" character varying NOT NULL, "grid_version" integer NOT NULL, "contract_id" numeric NOT NULL, "twin_id" integer NOT NULL, "node_id" integer NOT NULL, "state" character varying(11) NOT NULL, "created_at" numeric NOT NULL, "solution_provider_id" integer, CONSTRAINT "PK_3c99766b627604d5950d704e33a" PRIMARY KEY ("id"))`)
await db.query(`CREATE TABLE "name_contract" ("id" character varying NOT NULL, "grid_version" integer NOT NULL, "contract_id" numeric NOT NULL, "twin_id" integer NOT NULL, "name" text NOT NULL, "state" character varying(11) NOT NULL, "created_at" numeric NOT NULL, "solution_provider_id" integer, CONSTRAINT "PK_7b4cd056bbb83602d211996360f" PRIMARY KEY ("id"))`)
await db.query(`CREATE TABLE "contract_resources" ("id" character varying NOT NULL, "hru" numeric NOT NULL, "sru" numeric NOT NULL, "cru" numeric NOT NULL, "mru" numeric NOT NULL, "contract_id" character varying, CONSTRAINT "PK_557de19994fcca90916e8c6582f" PRIMARY KEY ("id"))`)
await db.query(`ALTER TABLE "contract_resources" ADD CONSTRAINT "FK_621238dffde9099b2233650235d" FOREIGN KEY ("contract_id") REFERENCES "node_contract"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`)
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Mario, for all the good work!

  • Since this PR is about performance, have we conducted any tests or benchmarks to demonstrate the effectiveness of the performance improvements made here?
    I would like to see some numbers before and after the changes for the queries that were used in production. This will help us understand the impact of the changes and whether they are worth implementing.

  • Also, I was wondering if there are any other parts of the system that use this database directly besides gridproxy. If so, it might be helpful to include a note in the PR description about what will be affected by this change.

84 changes: 34 additions & 50 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Entity @entity {

type Twin @entity {
gridVersion: Int!
twinID: Int!
twinID: Int! @index
accountID: String!
relay: String
publicKey: String
Expand All @@ -23,12 +23,14 @@ type EntityProof @entity {

type Farm @entity {
gridVersion: Int!
farmID: Int!
farmID: Int! @index
name: String!
twinID: Int!
pricingPolicyID: Int!
certification: FarmCertification
publicIPs: [PublicIp!] @derivedFrom(field: "farm")
totalIps: Int!
freeIps: Int!
stellarAddress: String
dedicatedFarm: Boolean
}
Expand All @@ -37,19 +39,18 @@ type PublicIp @entity {
farm: Farm!
gateway: String!
ip: String!
contractId: BigInt!
contractId: BigInt @index
}

type Node @entity {
gridVersion: Int!
nodeID: Int!
nodeID: Int! @index
farmID: Int!
twinID: Int!
location: Location!
country: String
city: String
publicConfig: PublicConfig @derivedFrom(field: "node")
resourcesTotal: NodeResourcesTotal @derivedFrom(field: "node")
uptime: BigInt
created: Int!
farmingPolicyId: Int!
Expand All @@ -64,6 +65,14 @@ type Node @entity {
power: NodePower
dedicated: Boolean!
extraFee: BigInt
totalCRU: BigInt!
totalHRU: BigInt!
totalSRU: BigInt!
totalMRU: BigInt!
freeMRU: BigInt! @index
freeHRU: BigInt! @index
freeSRU: BigInt! @index
rentedBy: Int
}

type NodePower {
Expand All @@ -81,14 +90,6 @@ enum Power {
Down
}

type NodeResourcesTotal @entity {
node: Node! @unique
hru: BigInt!
sru: BigInt!
cru: BigInt!
mru: BigInt!
}

type Interfaces @entity {
node: Node!
name: String!
Expand Down Expand Up @@ -154,46 +155,23 @@ type City @entity {
name: String!
}

type NodeContract @entity {
gridVersion: Int!
contractID: BigInt!
twinID: Int!
nodeID: Int!
deploymentData: String!
deploymentHash: String!
numberOfPublicIPs: Int!
state: ContractState!
resourcesUsed: ContractResources
createdAt: BigInt!
solutionProviderID: Int
}

type ContractResources @entity {
contract: NodeContract!
hru: BigInt!
sru: BigInt!
cru: BigInt!
mru: BigInt!
}

type NameContract @entity {
gridVersion: Int!
contractID: BigInt!
twinID: Int!
name: String!
state: ContractState!
createdAt: BigInt!
solutionProviderID: Int
}

type RentContract @entity {
type Contract @entity{
gridVersion: Int!
contractID: BigInt!
contractID: BigInt! @index
twinID: Int!
nodeID: Int!
state: ContractState!
nodeID: Int
deploymentData: String
deploymentHash: String
numberOfPublicIPs: Int
state: ContractState! @index
createdAt: BigInt!
solutionProviderID: Int
usedCRU: BigInt
usedMRU: BigInt
usedSRU: BigInt
usedHRU: BigInt
name: String
type: ContractType! @index
}

type SolutionProvider @entity {
Expand All @@ -216,6 +194,12 @@ enum ContractState {
GracePeriod
}

enum ContractType {
Node
Name
Rent
}

type NruConsumption @entity {
contractID: BigInt!
timestamp: BigInt!
Expand All @@ -224,7 +208,7 @@ type NruConsumption @entity {
}

type ContractBillReport @entity {
contractID: BigInt!
contractID: BigInt! @index
discountReceived: DiscountLevel!
amountBilled: BigInt!
timestamp: BigInt!
Expand Down
Loading