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

enable sqlite wal #472

Merged
merged 4 commits into from
Oct 26, 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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ dist
*.sqlite-journal
*.wasm
*.db
*.sqlite-shm
*.sqlite-wal

.DS_store

Expand All @@ -135,4 +137,4 @@ lib
docs-src

# vitepress cache
.vitepress/cache
.vitepress/cache
23 changes: 0 additions & 23 deletions packages/db/src/base-sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,6 @@ import { DataSource } from 'typeorm'
import { HexString } from '@polkadot/util/types'

import { BlockEntity, KeyValuePair } from './db/entities'
import { retry } from './retry'

function Retryable<T>(
_target: any,
_propertyKey: string,
descriptor: TypedPropertyDescriptor<(...args: any[]) => Promise<T>>,
) {
const originalMethod = descriptor.value

descriptor.value = async function (...args: any[]): Promise<T> {
return retry(() => originalMethod!.apply(this, args))
}

return descriptor
}

export abstract class BaseSqlDatabase implements Database {
abstract datasource: Promise<DataSource>
Expand All @@ -27,7 +12,6 @@ export abstract class BaseSqlDatabase implements Database {
await db.destroy()
}

@Retryable
async saveBlock(block: BlockEntry) {
const db = await this.datasource

Expand All @@ -49,37 +33,31 @@ export abstract class BaseSqlDatabase implements Database {
})
}

@Retryable
async queryBlock(hash: HexString): Promise<BlockEntry | null> {
const db = await this.datasource
return db.getRepository(BlockEntity).findOne({ where: { hash } })
}

@Retryable
async queryBlockByNumber(number: number): Promise<BlockEntry | null> {
const db = await this.datasource
return db.getRepository(BlockEntity).findOne({ where: { number }, order: { number: 'desc' } })
}

@Retryable
async queryHighestBlock(): Promise<BlockEntry | null> {
const db = await this.datasource
return db.getRepository(BlockEntity).findOne({ where: {}, order: { number: 'desc' } })
}

@Retryable
async deleteBlock(hash: HexString) {
const db = await this.datasource
await db.getRepository(BlockEntity).delete({ hash })
}

@Retryable
async blocksCount(): Promise<number> {
const db = await this.datasource
return db.getRepository(BlockEntity).count()
}

@Retryable
async saveStorage(blockHash: HexString, key: HexString, value: HexString | null) {
const db = await this.datasource
await db.getRepository(KeyValuePair).upsert(
Expand All @@ -92,7 +70,6 @@ export abstract class BaseSqlDatabase implements Database {
)
}

@Retryable
async queryStorage(blockHash: HexString, key: HexString): Promise<KeyValueEntry | null> {
const db = await this.datasource
return db.getRepository(KeyValuePair).findOne({ where: { blockHash, key } })
Expand Down
6 changes: 4 additions & 2 deletions packages/db/src/db/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { DataSource } from 'typeorm'

import * as entities from './entities'
import { retry } from '../retry'

export const openDb = async (dbPath: string): Promise<DataSource> => {
const source = new DataSource({
Expand All @@ -10,9 +9,12 @@ export const openDb = async (dbPath: string): Promise<DataSource> => {
entities: Object.values(entities),
synchronize: true,
logging: false,
enableWAL: true, // improve performance and concurrency
busyErrorRetry: 1000, // typeorm retry timeout
busyTimeout: 5000, // retry for 5 seconds, sqlite PRAGMA busy_timeout
})

await retry(() => source.initialize(), 3, 1000)
await source.initialize()

return source
}
26 changes: 0 additions & 26 deletions packages/db/src/retry.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/e2e/scripts/sidecar-chopsticks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
set -x

# run chopsticks node
yarn dev:acala --port 8011 & ACALA_PID=$!
yarn dev:karura --port 8012 & KARURA_PID=$!
yarn script:start -c acala --port 8011 & ACALA_PID=$!
yarn script:start -c karura --port 8012 & KARURA_PID=$!

printf "Waiting for chains to be ready"
attempts=30 # 5 minutes
Expand Down
2 changes: 0 additions & 2 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
"resolveJsonModule": true,
"baseUrl": ".",
"composite": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"paths": {
"@acala-network/chopsticks": ["packages/chopsticks/src"],
"@acala-network/chopsticks/*": ["packages/chopsticks/src/*"],
Expand Down