fix: shard app usages properly by user (#2279)
Docker Image CI / build-and-push-image (push) Has been cancelled
Maintain Release Merge PR / update-release-pr (push) Has been cancelled
release-please / release-please (push) Has been cancelled
test / test-backend (24.x) (push) Has been cancelled
test / API tests (node env, api-test) (24.x) (push) Has been cancelled
test / puterjs (node env, vitest) (24.x) (push) Has been cancelled

This commit is contained in:
Daniel Salazar
2026-01-13 20:32:46 -08:00
committed by GitHub
parent 529a3d026b
commit 0b9863814c
@@ -51,14 +51,8 @@ export class MeteringService {
return key;
}
/**
* adds some randomized number from 0-99 to the usage key to help spread writes
* @param appId
* @param currentMonth
* @returns
*/
#generateAppUsageKey (appId: string, currentMonth: string) {
const hashOfApp = murmurhash.v3(`${appId}`) % MeteringService.APP_SHARD_COUNT;
#generateAppUsageKey (appId: string, userId: string, currentMonth: string) {
const hashOfApp = murmurhash.v3(`${appId}${userId}`) % MeteringService.APP_SHARD_COUNT;
const key = `${METRICS_PREFIX}:app:${appId}:${hashOfApp}:${currentMonth}`;
return key;
}
@@ -127,7 +121,7 @@ export class MeteringService {
});
if ( appId !== GLOBAL_APP_KEY ) {
const appUsageKey = this.#generateAppUsageKey(appId, currentMonth);
const appUsageKey = this.#generateAppUsageKey(appId, userId, currentMonth);
this.#kvStore.incr({
key: appUsageKey,
pathAndAmountMap,
@@ -283,7 +277,7 @@ export class MeteringService {
console.warn('Failed to increment aux usage data \'actorAppUsageKey\' with error: ', e);
});
const appUsageKey = this.#generateAppUsageKey(appId, currentMonth);
const appUsageKey = this.#generateAppUsageKey(appId, userId, currentMonth);
this.#kvStore.incr({
key: appUsageKey,
pathAndAmountMap: aggregatedPathAndAmountMap,