From f4596db9f7744a2fc73e45d37cf78e5ade8855db Mon Sep 17 00:00:00 2001 From: Zaid Humaimidi Date: Thu, 23 Nov 2023 10:34:47 -0800 Subject: [PATCH] Remove caching of tasks, faults and operations No longer seems to provide significant performance benefit to justify the drawback of the added code complexity and the additional DB write query. --- lib/api-functions.ts | 4 -- lib/cwmp.ts | 96 +++----------------------------------------- lib/nbi.ts | 5 --- lib/types.ts | 1 - 4 files changed, 6 insertions(+), 100 deletions(-) diff --git a/lib/api-functions.ts b/lib/api-functions.ts index 898fbec..5bb021e 100644 --- a/lib/api-functions.ts +++ b/lib/api-functions.ts @@ -367,7 +367,6 @@ export async function deleteDevice(deviceId: string): Promise { $regex: `^${common.escapeRegExp(deviceId)}\\:`, }, }), - cache.del(`${deviceId}_tasks_faults_operations`), ]); } @@ -387,7 +386,6 @@ export async function deleteResource( if (channel.startsWith("task_")) proms.push(deleteTask(new ObjectId(channel.slice(5)))); await Promise.all(proms); - await cache.del(`${deviceId}_tasks_faults_operations`); } else if (resource === "provisions") { await deleteProvision(id); await cache.del("cwmp-local-cache-hash"); @@ -439,8 +437,6 @@ export async function postTasks( }; } - await cache.del(`${deviceId}_tasks_faults_operations`); - const status = await connectionRequest(deviceId, device); if (status) { diff --git a/lib/cwmp.ts b/lib/cwmp.ts index c601508..de7e281 100644 --- a/lib/cwmp.ts +++ b/lib/cwmp.ts @@ -53,10 +53,8 @@ import { SessionContext, AcsRequest, SessionFault, - Operation, Fault, Expression, - Task, SoapMessage, InformRequest, Preset, @@ -768,8 +766,6 @@ async function nextRpc(sessionContext: SessionContext): Promise { } async function endSession(sessionContext: SessionContext): Promise { - let saveCache = sessionContext.cacheUntil != null; - if (sessionContext.provisions.length) { const fault = { code: "session_terminated", @@ -794,7 +790,6 @@ async function endSession(sessionContext: SessionContext): Promise { if (sessionContext.operationsTouched) { for (const k of Object.keys(sessionContext.operationsTouched)) { - saveCache = true; if (sessionContext.operations[k]) { promises.push( saveOperation( @@ -810,7 +805,6 @@ async function endSession(sessionContext: SessionContext): Promise { } if (sessionContext.doneTasks?.length) { - saveCache = true; promises.push( clearTasks(sessionContext.deviceId, sessionContext.doneTasks) ); @@ -818,7 +812,6 @@ async function endSession(sessionContext: SessionContext): Promise { if (sessionContext.faultsTouched) { for (const k of Object.keys(sessionContext.faultsTouched)) { - saveCache = true; if (sessionContext.faults[k]) { sessionContext.faults[k].retries = sessionContext.retries[k]; promises.push( @@ -830,18 +823,6 @@ async function endSession(sessionContext: SessionContext): Promise { } } - if (saveCache) { - promises.push( - cacheDueTasksAndFaultsAndOperations( - sessionContext.deviceId, - sessionContext.tasks, - sessionContext.faults, - sessionContext.operations, - sessionContext.cacheUntil - ) - ); - } - await Promise.all(promises); await lock.releaseLock( `cwmp_session_${sessionContext.deviceId}`, @@ -987,66 +968,6 @@ setInterval(() => { stats.initiatedSessions = 0; }, 10000).unref(); -async function getDueTasksAndFaultsAndOperations( - deviceId, - timestamp -): Promise<{ - tasks: Task[]; - faults: { [channel: string]: SessionFault }; - operations: { [commandKey: string]: Operation }; - ttl: number; -}> { - const res = await cache.get(`${deviceId}_tasks_faults_operations`); - if (res) { - const resParsed = JSON.parse(res); - return { - tasks: resParsed.tasks || [], - faults: resParsed.faults || {}, - operations: resParsed.operations || {}, - ttl: 0, - }; - } - - const res2 = await Promise.all([ - getDueTasks(deviceId, timestamp), - getFaults(deviceId), - getOperations(deviceId), - ]); - return { - tasks: res2[0][0], - faults: res2[1], - operations: res2[2], - ttl: res2[0][1] || 0, - }; -} - -async function cacheDueTasksAndFaultsAndOperations( - deviceId, - tasks, - faults, - operations, - cacheUntil -): Promise { - const v = { - tasks: null, - faults: null, - operations: null, - }; - if (tasks.length) v.tasks = tasks; - if (Object.keys(faults).length) v.faults = faults; - if (Object.keys(operations).length) v.operations = operations; - - let ttl; - if (cacheUntil) ttl = Math.trunc((Date.now() - cacheUntil) / 1000); - else ttl = config.get("MAX_CACHE_TTL", deviceId); - - await cache.set( - `${deviceId}_tasks_faults_operations`, - JSON.stringify(v), - ttl - ); -} - async function reportBadState(sessionContext: SessionContext): Promise { logger.accessError({ message: "Bad session state", @@ -1640,19 +1561,14 @@ async function listenerAsync( _sessionContext.httpResponse = httpResponse; _sessionContext.sessionId = crypto.randomBytes(8).toString("hex"); - const { - tasks: dueTasks, - faults, - operations, - ttl: cacheUntil, - } = await getDueTasksAndFaultsAndOperations( - deviceId, - _sessionContext.timestamp - ); + const [dueTasks, faults, operations] = await Promise.all([ + getDueTasks(deviceId, _sessionContext.timestamp), + getFaults(deviceId), + getOperations(deviceId), + ]); - _sessionContext.tasks = dueTasks; + _sessionContext.tasks = dueTasks[0]; _sessionContext.operations = operations; - _sessionContext.cacheUntil = cacheUntil; _sessionContext.faults = faults; _sessionContext.retries = {}; for (const [k, v] of Object.entries(_sessionContext.faults)) { diff --git a/lib/nbi.ts b/lib/nbi.ts index d7adf8c..f9330e9 100644 --- a/lib/nbi.ts +++ b/lib/nbi.ts @@ -261,13 +261,11 @@ async function handler( if (channel.startsWith("task_")) { const objId = new ObjectId(channel.slice(5)); await collections.tasks.deleteOne({ _id: objId }); - await cache.del(`${deviceId}_tasks_faults_operations`); response.writeHead(200); response.end(); return; } - await cache.del(`${deviceId}_tasks_faults_operations`); response.writeHead(200); response.end(); } else { @@ -312,7 +310,6 @@ async function handler( deviceId, 30000 ); - await cache.del(`${deviceId}_tasks_faults_operations`); if (url.searchParams.has("connection_request")) { if (socketTimeout) request.socket.setTimeout(socketTimeout); response.writeHead(202, { @@ -456,7 +453,6 @@ async function handler( _id: `${deviceId}:task_${taskId}`, }); - await cache.del(`${deviceId}_tasks_faults_operations`); response.writeHead(200); response.end(); } else { @@ -475,7 +471,6 @@ async function handler( _id: `${deviceId}:task_${taskId}`, }); - await cache.del(`${deviceId}_tasks_faults_operations`); response.writeHead(200); response.end(); } else { diff --git a/lib/types.ts b/lib/types.ts index cb6fb58..415e204 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -160,7 +160,6 @@ export interface SessionContext { authState: number; tasks?: Task[]; operations?: { [commandKey: string]: Operation }; - cacheUntil?: number; syncState?: SyncState; lastActivity?: number; extendLock?: number;