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.
This commit is contained in:
Zaid Humaimidi committed 2023-11-23 10:34:47 -08:00
1 parent d122b224b0
commit f4596db9f7
4 files changed
+6 -100

No files matched your search

-4
View File
@@ -367,7 +367,6 @@ export async function deleteDevice(deviceId: string): Promise<void> {
$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) {
+6 -90
View File
@@ -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<void> {
}
async function endSession(sessionContext: SessionContext): Promise<void> {
let saveCache = sessionContext.cacheUntil != null;
if (sessionContext.provisions.length) {
const fault = {
code: "session_terminated",
@@ -794,7 +790,6 @@ async function endSession(sessionContext: SessionContext): Promise<void> {
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<void> {
}
if (sessionContext.doneTasks?.length) {
saveCache = true;
promises.push(
clearTasks(sessionContext.deviceId, sessionContext.doneTasks)
);
@@ -818,7 +812,6 @@ async function endSession(sessionContext: SessionContext): Promise<void> {
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<void> {
}
}
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<void> {
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<void> {
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)) {
-5
View File
@@ -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 {
-1
View File
@@ -160,7 +160,6 @@ export interface SessionContext {
authState: number;
tasks?: Task[];
operations?: { [commandKey: string]: Operation };
cacheUntil?: number;
syncState?: SyncState;
lastActivity?: number;
extendLock?: number;