From c4af5214ef9f8857ae767ea7cc2f49a58f62aada Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 13 Dec 2023 15:08:23 +0000 Subject: [PATCH 1/4] Improving types around the writethrough cache, exposing the proper typing from the database and removing a log statement which is useless, errors are correctly propogated if they matter (and handled above this level with proper context) or in the 99% scenario it is not a real error (such as try once locks already being held) and a log is just spammy. --- .../src/cache/tests/writethrough.spec.ts | 13 +++++++------ packages/backend-core/src/cache/writethrough.ts | 10 +++++----- packages/backend-core/src/redis/redlockImpl.ts | 1 - packages/pro | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/backend-core/src/cache/tests/writethrough.spec.ts b/packages/backend-core/src/cache/tests/writethrough.spec.ts index 97d3ece7a6..37887b4bd9 100644 --- a/packages/backend-core/src/cache/tests/writethrough.spec.ts +++ b/packages/backend-core/src/cache/tests/writethrough.spec.ts @@ -1,15 +1,16 @@ import { DBTestConfiguration } from "../../../tests/extra" -import { - structures, - expectFunctionWasCalledTimesWith, - mocks, -} from "../../../tests" +import { structures } from "../../../tests" import { Writethrough } from "../writethrough" import { getDB } from "../../db" +import { Document } from "@budibase/types" import tk from "timekeeper" tk.freeze(Date.now()) +interface ValueDoc extends Document { + value: any +} + const DELAY = 5000 describe("writethrough", () => { @@ -117,7 +118,7 @@ describe("writethrough", () => { describe("get", () => { it("should be able to retrieve", async () => { await config.doInTenant(async () => { - const response = await writethrough.get(docId) + const response = await writethrough.get(docId) expect(response.value).toBe(4) }) }) diff --git a/packages/backend-core/src/cache/writethrough.ts b/packages/backend-core/src/cache/writethrough.ts index c331d791a6..364cc35c67 100644 --- a/packages/backend-core/src/cache/writethrough.ts +++ b/packages/backend-core/src/cache/writethrough.ts @@ -84,16 +84,16 @@ async function put( return { ok: true, id: output._id, rev: output._rev } } -async function get(db: Database, id: string): Promise { +async function get(db: Database, id: string): Promise { const cache = await getCache() const cacheKey = makeCacheKey(db, id) let cacheItem: CacheItem = await cache.get(cacheKey) if (!cacheItem) { - const doc = await db.get(id) + const doc = await db.get(id) cacheItem = makeCacheItem(doc) await cache.store(cacheKey, cacheItem) } - return cacheItem.doc + return cacheItem.doc as T } async function remove(db: Database, docOrId: any, rev?: any): Promise { @@ -123,8 +123,8 @@ export class Writethrough { return put(this.db, doc, writeRateMs) } - async get(id: string) { - return get(this.db, id) + async get(id: string) { + return get(this.db, id) } async remove(docOrId: any, rev?: any) { diff --git a/packages/backend-core/src/redis/redlockImpl.ts b/packages/backend-core/src/redis/redlockImpl.ts index 4de2516ab2..e57a3721b5 100644 --- a/packages/backend-core/src/redis/redlockImpl.ts +++ b/packages/backend-core/src/redis/redlockImpl.ts @@ -137,7 +137,6 @@ export async function doWithLock( const result = await task() return { executed: true, result } } catch (e: any) { - logWarn(`lock type: ${opts.type} error`, e) // lock limit exceeded if (e.name === "LockError") { if (opts.type === LockType.TRY_ONCE) { diff --git a/packages/pro b/packages/pro index 056c2093db..3b89670d88 160000 --- a/packages/pro +++ b/packages/pro @@ -1 +1 @@ -Subproject commit 056c2093dbc93d9a10ea9f5050c84a84edd8100c +Subproject commit 3b89670d8817eee95a1ce53a35b95307ee3b8e76 From 48d70c45b96b9e149f2a36b8c56833c0b099e93a Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 13 Dec 2023 15:35:44 +0000 Subject: [PATCH 2/4] Updating pro. --- packages/pro | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pro b/packages/pro index 3b89670d88..cae0b311d7 160000 --- a/packages/pro +++ b/packages/pro @@ -1 +1 @@ -Subproject commit 3b89670d8817eee95a1ce53a35b95307ee3b8e76 +Subproject commit cae0b311d7a304dbe9b41f610d1d543a2c9dd2d3 From c8128325c69fe1058a2aae722f020679f643d9f7 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 13 Dec 2023 15:39:04 +0000 Subject: [PATCH 3/4] Improving type handling. --- packages/backend-core/src/cache/writethrough.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/backend-core/src/cache/writethrough.ts b/packages/backend-core/src/cache/writethrough.ts index 364cc35c67..24e519dc7f 100644 --- a/packages/backend-core/src/cache/writethrough.ts +++ b/packages/backend-core/src/cache/writethrough.ts @@ -7,7 +7,7 @@ import * as locks from "../redis/redlockImpl" const DEFAULT_WRITE_RATE_MS = 10000 let CACHE: BaseCache | null = null -interface CacheItem { +interface CacheItem { doc: any lastWrite: number } @@ -24,7 +24,10 @@ function makeCacheKey(db: Database, key: string) { return db.name + key } -function makeCacheItem(doc: any, lastWrite: number | null = null): CacheItem { +function makeCacheItem( + doc: T, + lastWrite: number | null = null +): CacheItem { return { doc, lastWrite: lastWrite || Date.now() } } @@ -35,7 +38,7 @@ async function put( ) { const cache = await getCache() const key = doc._id - let cacheItem: CacheItem | undefined + let cacheItem: CacheItem | undefined if (key) { cacheItem = await cache.get(makeCacheKey(db, key)) } @@ -87,13 +90,13 @@ async function put( async function get(db: Database, id: string): Promise { const cache = await getCache() const cacheKey = makeCacheKey(db, id) - let cacheItem: CacheItem = await cache.get(cacheKey) + let cacheItem: CacheItem = await cache.get(cacheKey) if (!cacheItem) { const doc = await db.get(id) cacheItem = makeCacheItem(doc) await cache.store(cacheKey, cacheItem) } - return cacheItem.doc as T + return cacheItem.doc } async function remove(db: Database, docOrId: any, rev?: any): Promise { From 27b8cc7476a0c75d6555d6b032c116728b0faa64 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 13 Dec 2023 15:51:23 +0000 Subject: [PATCH 4/4] Updating pro. --- packages/pro | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pro b/packages/pro index cae0b311d7..992486c100 160000 --- a/packages/pro +++ b/packages/pro @@ -1 +1 @@ -Subproject commit cae0b311d7a304dbe9b41f610d1d543a2c9dd2d3 +Subproject commit 992486c10044a7495496b97bdf5f454d4020bfba