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

feat: move access-api delegation bytes out of d1 and into r2 #578

Merged
merged 27 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
1e83d67
start delegations store
gobengo Mar 20, 2023
198e32d
test a bit
gobengo Mar 20, 2023
963a0d4
start delegations v3 table
gobengo Mar 21, 2023
e957d94
DbDelegationsStorageWithR2 reads/writes to r2
gobengo Mar 22, 2023
83c9c49
add comment explaining v2-only test
gobengo Mar 22, 2023
e65dd19
add todo
gobengo Mar 22, 2023
b491acb
todo
gobengo Mar 22, 2023
000c704
fix issues from conflicting wrangler, miniflare, @miniflare/r2 versio…
gobengo Mar 22, 2023
044f76b
access-api adds @miniflare/r2 as devDependency, fixing tsc
gobengo Mar 22, 2023
eeab7fd
rm unused files
gobengo Mar 22, 2023
a39b780
fix depcheck for access-api
gobengo Mar 22, 2023
bd42ee4
Merge branch 'main' into 571-delegation-bytes-out-d1
gobengo Mar 22, 2023
568d095
chore: loosen wrangler/miniflare semvers to range (#597)
gobengo Mar 22, 2023
0186d6a
models only depend on table types that they need
gobengo Mar 22, 2023
9fce623
chore: upgrade kysely-d1 and clean up db-related things due to d1 lib…
gobengo Mar 22, 2023
51e2835
feat: access-api env.models.delegations uses DbDelegationsStorageWith…
gobengo Mar 22, 2023
8acaa8d
fix: DbDelegationsStorageWithR2 saving to desired r2 key (#600)
gobengo Mar 22, 2023
ecbe77d
chore: rename ACCESS_API_R2 binding to DELEGATIONS, and configure wra…
gobengo Mar 23, 2023
c77d0cd
Update packages/access-api/src/models/delegations.js
gobengo Mar 23, 2023
37b05c1
Update packages/access-api/src/models/delegations.js
gobengo Mar 23, 2023
7f616d8
Update packages/access-api/test/delegations-storage.test.js
gobengo Mar 23, 2023
6fa5390
prettier
gobengo Mar 23, 2023
fe26c70
remove delegations_v2 schema and DbDelegationsStorage (keep newer DbD…
gobengo Mar 23, 2023
e379699
assert cid string kind in d1 cid column and r2 key
gobengo Mar 23, 2023
7d017e7
improve car parsing/validating in DbDelegationsStorageWithR2 rowToDel…
gobengo Mar 23, 2023
e2da517
env.DELEGATIONS -> env.DELEGATIONS_BUCKET
gobengo Mar 23, 2023
3261215
rename r2 buckets to w3up-delegations-{env}-{i}
gobengo Mar 23, 2023
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
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
"lint-staged": "^13.2.0",
"prettier": "2.8.3",
"typedoc-plugin-markdown": "^3.14.0",
"typescript": "4.9.5",
"wrangler": "^2.12.3"
"typescript": "4.9.5"
},
"prettier": {
"trailingComma": "es5",
Expand All @@ -36,7 +35,7 @@
},
"dependencies": {
"depcheck": "^1.4.3",
"typedoc": "^0.23.26",
"typedoc": "^0.23.28",
"typedoc-plugin-missing-exports": "^1.0.0"
},
"packageManager": "pnpm@7.24.3",
Expand Down
19 changes: 19 additions & 0 deletions packages/access-api/migrations/0007_add_delegations_v3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- Migration number: 0007 2023-03-20T23:48:40.469Z

/*
goal: add a new table to store delegations in
which doesn't have a 'bytes' column.

context: we're going to start storing bytes outside of the database (e.g. in R2)
*/

CREATE TABLE
IF NOT EXISTS delegations_v3 (
cid TEXT NOT NULL PRIMARY KEY,
gobengo marked this conversation as resolved.
Show resolved Hide resolved
audience TEXT NOT NULL,
issuer TEXT NOT NULL,
expiration TEXT,
inserted_at TEXT NOT NULL DEFAULT (strftime ('%Y-%m-%dT%H:%M:%fZ', 'now')),
updated_at TEXT NOT NULL DEFAULT (strftime ('%Y-%m-%dT%H:%M:%fZ', 'now')),
UNIQUE (cid)
);
9 changes: 5 additions & 4 deletions packages/access-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@web3-storage/capabilities": "workspace:^",
"@web3-storage/worker-utils": "0.4.3-dev",
"kysely": "^0.23.4",
"kysely-d1": "^0.1.0",
"kysely-d1": "^0.3.0",
"multiformats": "^11.0.2",
"p-retry": "^5.1.2",
"preact": "^10.11.3",
Expand All @@ -38,9 +38,10 @@
"toucan-js": "^2.7.0"
},
"devDependencies": {
"@cloudflare/workers-types": "^3.18.0",
"@cloudflare/workers-types": "^3.19.0",
"@databases/split-sql-query": "^1.0.3",
"@databases/sql": "^3.2.0",
"@miniflare/r2": "^2.12.1",
"@sentry/cli": "2.7.0",
"@types/assert": "^1.5.6",
"@types/git-rev-sync": "^2.0.0",
Expand All @@ -55,14 +56,14 @@
"git-rev-sync": "^3.0.2",
"hd-scripts": "^4.0.0",
"is-subset": "^0.1.1",
"miniflare": "^2.11.0",
"miniflare": "^2.12.1",
"mocha": "^10.2.0",
"p-wait-for": "^5.0.0",
"process": "^0.11.10",
"readable-stream": "^4.2.0",
"sade": "^1.8.1",
"typescript": "4.9.5",
"wrangler": "^2.8.0"
"wrangler": "^2.13.0"
},
"eslintConfig": {
"extends": [
Expand Down
17 changes: 6 additions & 11 deletions packages/access-api/src/bindings.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import type { Logging } from '@web3-storage/worker-utils/logging'
import type {
AccountTable,
DelegationTable,
SpaceTable,
} from '@web3-storage/access/types'
import type { Handler as _Handler } from '@web3-storage/worker-utils/router'
import { Spaces } from './models/spaces.js'
import { Validations } from './models/validations.js'
Expand All @@ -12,6 +7,7 @@ import { ConnectionView, Signer as EdSigner } from '@ucanto/principal/ed25519'
import { Accounts } from './models/accounts.js'
import { DelegationsStorage as Delegations } from './types/delegations.js'
import { ProvisionsStorage } from './types/provisions.js'
import { R2Bucket } from '@miniflare/r2'

export {}

Expand Down Expand Up @@ -53,6 +49,11 @@ export interface Env {
SPACES: KVNamespace
VALIDATIONS: KVNamespace
W3ACCESS_METRICS: AnalyticsEngine
/**
* primary r2 bucket used by access-api.
* will be used for storing env.models.delegations CARs
*/
ACCESS_API_R2: R2Bucket
Copy link
Member

Choose a reason for hiding this comment

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

Just have a bucket specific to the things it's storing? We can create many of these - we don't have to have only 1 for everything we store.

Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: I think @alanshaw suggesting this should be more like DELEGATIONS_BUCKET which I'd find to be more intuitive as well. I would also suggest not baking product names like R2 so it's not tied to CF product and could work e.g if we move to S3.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

// eslint-disable-next-line @typescript-eslint/naming-convention
__D1_BETA__: D1Database
}
Expand Down Expand Up @@ -108,9 +109,3 @@ export interface ModuleWorker {
export interface D1ErrorRaw extends Error {
cause: Error & { code: string }
}

export interface D1Schema {
spaces: SpaceTable
accounts: AccountTable
delegations: DelegationTable
}
7 changes: 7 additions & 0 deletions packages/access-api/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ export function loadConfig(env) {
}
}

if (typeof env.ACCESS_API_R2 !== 'object') {
throw new TypeError(
`expected env.ACCESS_API_R2 to be an R2Bucket object, but got ${typeof env.ACCESS_API_R2}`
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Would be nice if error said wrangler magic one supposed to do to fix this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it shouldn't happen under normal circumstances, i.e. there is no wrangler magic to recommend. the wranger.toml will set this up in a good way for known envs. This would catch it if you somehow opted out of that calling this function.

)
}

return {
DEBUG: boolValue(vars.DEBUG),
ENV: parseRuntimeEnv(vars.ENV),
Expand Down Expand Up @@ -67,6 +73,7 @@ export function loadConfig(env) {
SPACES: env.SPACES,
VALIDATIONS: env.VALIDATIONS,
DB: /** @type {D1Database} */ (env.__D1_BETA__),
ACCESS_API_R2: env.ACCESS_API_R2,
}
}

Expand Down
20 changes: 7 additions & 13 deletions packages/access-api/src/models/accounts.js
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you avoid changing this unless needed as it would create conflict with my PR also changing this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Original file line number Diff line number Diff line change
@@ -1,35 +1,29 @@
// eslint-disable-next-line no-unused-vars
import * as Ucanto from '@ucanto/interface'
import { Kysely } from 'kysely'
import { D1Dialect } from 'kysely-d1'
import { GenericPlugin } from '../utils/d1.js'

/**
* @typedef {import('@web3-storage/access/src/types.js').DelegationRecord} DelegationRecord
*/

/**
* Accounts
*/
export class Accounts {
/**
*
* @param {D1Database} d1
*/
constructor(d1) {
/** @type {GenericPlugin<DelegationRecord>} */
const objectPlugin = new GenericPlugin({
// eslint-disable-next-line unicorn/no-null
expires_at: (v) => (typeof v === 'string' ? new Date(v) : null),
inserted_at: (v) => new Date(v),
updated_at: (v) => new Date(v),
})
this.d1 = /** @type {Kysely<import('../bindings').D1Schema>} */ (
new Kysely({
dialect: new D1Dialect({ database: d1 }),
plugins: [objectPlugin],
})
)
this.d1 =
/** @type {Kysely<{ accounts: import('@web3-storage/access/src/types.js').AccountTable }>} */ (
new Kysely({
dialect: new D1Dialect({ database: d1 }),
plugins: [objectPlugin],
})
)
}

/**
Expand Down
Loading