Skip to content

Commit 260fc31

Browse files
committed
fix: stats for single shard
Signed-off-by: ferhat elmas <elmas.ferhat@gmail.com>
1 parent 7cb07b5 commit 260fc31

3 files changed

Lines changed: 40 additions & 19 deletions

File tree

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: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ResourceKind, ShardRow, ShardStatus } from '@internal/sharding/store'
1+
import { ResourceKind, ShardRow, ShardStats, ShardStatus } from '@internal/sharding/store'
22
import { Sharder, ShardResource } from '../sharder'
33

44
export class SingleShard implements Sharder {
@@ -23,14 +23,16 @@ 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(): 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

3638
withTnx(): Sharder {
@@ -108,8 +110,4 @@ export class SingleShard implements Sharder {
108110
setShardStatus(): Promise<void> {
109111
return Promise.resolve(undefined)
110112
}
111-
112-
shardStatsByKind(): Promise<any> {
113-
return Promise.resolve(undefined)
114-
}
115113
}

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)