Skip to content

Commit f6e193a

Browse files
authored
fix: stats for single shard (#992)
1 parent 7cb07b5 commit f6e193a

4 files changed

Lines changed: 61 additions & 51 deletions

File tree

src/internal/sharding/sharder.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ResourceKind, ShardRow, ShardStatus } from './store'
1+
import { ResourceKind, ShardRow, ShardStats, ShardStatus } from './store'
22

33
export interface ShardResource {
44
kind: ResourceKind
@@ -7,6 +7,14 @@ export interface ShardResource {
77
logicalName: string
88
}
99

10+
export interface ReservationResult {
11+
reservationId: string
12+
shardId: string
13+
shardKey: string
14+
slotNo: number
15+
leaseExpiresAt: string
16+
}
17+
1018
export interface Sharder {
1119
createShard(opts: {
1220
kind: ResourceKind
@@ -17,26 +25,13 @@ export interface Sharder {
1725

1826
setShardStatus(shardId: string | number, status: ShardStatus): Promise<void>
1927

20-
reserve(
21-
opts: ShardResource & {
22-
kind: ResourceKind
23-
tenantId: string
24-
bucketName: string
25-
logicalName: string
26-
}
27-
): Promise<{
28-
reservationId: string
29-
shardId: string
30-
shardKey: string
31-
slotNo: number
32-
leaseExpiresAt: string
33-
}>
28+
reserve(opts: ShardResource): Promise<ReservationResult>
3429
confirm(reservationId: string, resource: ShardResource): Promise<void>
3530
cancel(reservationId: string): Promise<void>
3631
expireLeases(): Promise<number>
3732
freeByLocation(shardId: string | number, slotNo: number): Promise<void>
3833
freeByResource(shardId: string | number, resource: ShardResource): Promise<void>
39-
shardStats(kind?: ResourceKind): Promise<any>
34+
shardStats(kind?: ResourceKind): Promise<ShardStats>
4035
findShardByResourceId(param: ShardResource): Promise<ShardRow | null>
4136
listShardByKind(icebergTables: ResourceKind): Promise<ShardRow[]>
4237

src/internal/sharding/store.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ export type ReservationRow = {
2525
created_at: string
2626
}
2727

28+
export type ShardStats = Array<{
29+
shardId: string
30+
shardKey: string
31+
capacity: number
32+
used: number
33+
free: number
34+
}>
35+
2836
/** Factory that opens a transaction and passes a store bound to that tx */
2937
export interface ShardStoreFactory<Tnx = unknown> {
3038
withTransaction<T>(fn: (store: ShardStore) => Promise<T>): Promise<T>
@@ -101,11 +109,7 @@ export interface ShardStore {
101109
findShardByResourceId(tenantId: string, resourceId: string): Promise<ShardRow | null>
102110

103111
// Stats
104-
shardStats(
105-
kind?: ResourceKind
106-
): Promise<
107-
Array<{ shardId: string; shardKey: string; capacity: number; used: number; free: number }>
108-
>
112+
shardStats(kind?: ResourceKind): Promise<ShardStats>
109113

110114
findShardById(shardId: number): Promise<ShardRow | null>
111115
}

src/internal/sharding/strategy/single-shard.ts

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { ResourceKind, ShardRow, ShardStatus } from '@internal/sharding/store'
2-
import { Sharder, ShardResource } from '../sharder'
1+
import { ResourceKind, ShardRow, ShardStats, ShardStatus } from '@internal/sharding/store'
2+
import { ReservationResult, Sharder, ShardResource } from '../sharder'
33

44
export class SingleShard implements Sharder {
55
constructor(
@@ -9,7 +9,7 @@ export class SingleShard implements Sharder {
99
}
1010
) {}
1111

12-
listShardByKind(): Promise<ShardRow[]> {
12+
listShardByKind(_kind: ResourceKind): Promise<ShardRow[]> {
1313
return Promise.resolve([
1414
{
1515
id: 1,
@@ -23,32 +23,34 @@ export class SingleShard implements Sharder {
2323
])
2424
}
2525

26-
shardStats(): Promise<any> {
27-
return Promise.resolve({
28-
shardId: 1,
29-
shardKey: this.singleShard.shardKey,
30-
capacity: this.singleShard.capacity,
31-
used: -1,
32-
free: -1,
33-
})
26+
shardStats(_kind?: ResourceKind): Promise<ShardStats> {
27+
return Promise.resolve([
28+
{
29+
shardId: '1',
30+
shardKey: this.singleShard.shardKey,
31+
capacity: this.singleShard.capacity,
32+
used: -1,
33+
free: -1,
34+
},
35+
])
3436
}
3537

36-
withTnx(): Sharder {
38+
withTnx(_tnx: unknown): Sharder {
3739
return new SingleShard({
3840
shardKey: this.singleShard.shardKey,
3941
capacity: this.singleShard.capacity,
4042
})
4143
}
4244

43-
freeByResource(): Promise<void> {
45+
freeByResource(_shardId: string | number, _resource: ShardResource): Promise<void> {
4446
return Promise.resolve()
4547
}
4648

47-
cancel(): Promise<void> {
49+
cancel(_reservationId: string): Promise<void> {
4850
return Promise.resolve(undefined)
4951
}
5052

51-
confirm(): Promise<void> {
53+
confirm(_reservationId: string, _resource: ShardResource): Promise<void> {
5254
return Promise.resolve(undefined)
5355
}
5456

@@ -85,31 +87,21 @@ export class SingleShard implements Sharder {
8587
})
8688
}
8789

88-
freeByLocation(): Promise<void> {
90+
freeByLocation(_shardId: string | number, _slotNo: number): Promise<void> {
8991
return Promise.resolve(undefined)
9092
}
9193

92-
reserve(): Promise<{
93-
reservationId: string
94-
shardId: string
95-
shardKey: string
96-
slotNo: number
97-
leaseExpiresAt: string
98-
}> {
94+
reserve(_opts: ShardResource): Promise<ReservationResult> {
9995
return Promise.resolve({
10096
leaseExpiresAt: '',
10197
reservationId: '',
102-
shardId: this.singleShard.shardKey,
98+
shardId: '1',
10399
shardKey: this.singleShard.shardKey,
104100
slotNo: 0,
105101
})
106102
}
107103

108-
setShardStatus(): Promise<void> {
109-
return Promise.resolve(undefined)
110-
}
111-
112-
shardStatsByKind(): Promise<any> {
104+
setShardStatus(_shardId: string | number, _status: ShardStatus): Promise<void> {
113105
return Promise.resolve(undefined)
114106
}
115107
}

src/test/sharding.test.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { multitenantKnex } from '@internal/database'
44
import { runMultitenantMigrations } from '@internal/database/migrations'
5-
import { KnexShardStoreFactory, ShardCatalog } from '@internal/sharding'
5+
import { KnexShardStoreFactory, ShardCatalog, SingleShard } from '@internal/sharding'
66
import {
77
ExpiredReservationError,
88
NoActiveShardError,
@@ -798,3 +798,22 @@ describe('Sharding System', () => {
798798
})
799799
})
800800
})
801+
802+
describe('SingleShard', () => {
803+
it('returns shard stats in the canonical array shape', async () => {
804+
const sharder = new SingleShard({
805+
shardKey: 'single-shard-key',
806+
capacity: 25,
807+
})
808+
809+
await expect(sharder.shardStats()).resolves.toEqual([
810+
{
811+
shardId: '1',
812+
shardKey: 'single-shard-key',
813+
capacity: 25,
814+
used: -1,
815+
free: -1,
816+
},
817+
])
818+
})
819+
})

0 commit comments

Comments
 (0)