From 728e7cbdb63daa4229bb202ed5dcf329fe93aa91 Mon Sep 17 00:00:00 2001 From: Yogesh01000100 Date: Mon, 16 Sep 2024 18:26:15 +0000 Subject: [PATCH] fix: correct return types and inits Signed-off-by: Yogesh01000100 --- .../typescript/core/recovery/crash-manager.ts | 59 +++++++++++++++++-- .../core/recovery/crash-recovery-handler.ts | 9 +-- .../typescript/core/recovery/crash-utils.ts | 20 +++---- .../typescript/plugin-satp-hermes-gateway.ts | 7 ++- 4 files changed, 73 insertions(+), 22 deletions(-) diff --git a/packages/cactus-plugin-satp-hermes/src/main/typescript/core/recovery/crash-manager.ts b/packages/cactus-plugin-satp-hermes/src/main/typescript/core/recovery/crash-manager.ts index 1186c50056b..90f6984602c 100644 --- a/packages/cactus-plugin-satp-hermes/src/main/typescript/core/recovery/crash-manager.ts +++ b/packages/cactus-plugin-satp-hermes/src/main/typescript/core/recovery/crash-manager.ts @@ -44,7 +44,7 @@ export class CrashRecoveryManager { public static readonly CLASS_NAME = "CrashRecoveryManager"; private readonly log: Logger; private readonly instanceId: string; - private readonly sessions: Map; + private sessions: Map; private crashRecoveryHandler: CrashRecoveryHandler; private factory: RollbackStrategyFactory; private logRepository: ILocalLogRepository; @@ -57,10 +57,19 @@ export class CrashRecoveryManager { const label = this.className; this.log = LoggerProvider.getOrCreate({ level, label }); this.instanceId = options.instanceId; - this.sessions = this.getSessions() || new Map(); + this.sessions = new Map(); this.log.info(`Instantiated ${this.className} OK`); this.factory = new RollbackStrategyFactory(options.bridgeConfig); this.logRepository = new LocalLogRepository(options.knexConfig); + const crashRecoveryServiceOptions = { + logLevel: this.options.logLevel, + instanceId: this.instanceId, + loggerOptions: { + label: "CrashRecoveryService", + level: this.options.logLevel || "DEBUG", + }, + logRepository: this.logRepository, + }; this.crashRecoveryHandler = new CrashRecoveryHandler({ loggerOptions: { label: "CrashRecoveryHandler", @@ -76,10 +85,13 @@ export class CrashRecoveryManager { return CrashRecoveryManager.CLASS_NAME; } + public async init(): Promise { + this.sessions = await this.getSessions(); + } + // todo read from local log to get session data - private async getSessions(): Map { + /*private async getSessions(): Map { const sessionMap = new Map(); - // todo! session data mismatch with logs?? try { const allSessions = await this.logRepository.readLogsNotProofs(); allSessions.forEach((log) => { @@ -92,6 +104,43 @@ export class CrashRecoveryManager { this.log.error(`Error initializing sessions: ${error}`); } + return sessionMap; + }*/ + + private async getSessions(): Promise> { + const sessionMap = new Map(); + + try { + const allLogs = await this.logRepository.readLogsNotProofs(); + + for (const log of allLogs) { + const sessionId = log.sessionID; + + let sessionData = sessionMap.get(sessionId); + if (!sessionData) { + sessionData = new SessionData(); + sessionData.id = sessionId; + sessionMap.set(sessionId, sessionData); + } + + try { + const logEntry = JSON.parse(log.data); + + Object.assign(sessionData, logEntry); + + if (logEntry.sequenceNumber !== undefined) { + sessionData.lastSequenceNumber = logEntry.sequenceNumber; + } + } catch (error) { + this.log.error( + `Error parsing log data for session ${sessionId}: ${error}`, + ); + } + } + } catch (error) { + this.log.error(`Error initializing sessions: ${error}`); + } + return sessionMap; } @@ -200,7 +249,7 @@ export class CrashRecoveryManager { } } - private async handleRecovery(session: SATPSession): Promise { + public async handleRecovery(session: SATPSession): Promise { const fnTag = `${this.className}#handleRecovery()`; try { diff --git a/packages/cactus-plugin-satp-hermes/src/main/typescript/core/recovery/crash-recovery-handler.ts b/packages/cactus-plugin-satp-hermes/src/main/typescript/core/recovery/crash-recovery-handler.ts index e1dc840fc88..a8af8ceafb7 100644 --- a/packages/cactus-plugin-satp-hermes/src/main/typescript/core/recovery/crash-recovery-handler.ts +++ b/packages/cactus-plugin-satp-hermes/src/main/typescript/core/recovery/crash-recovery-handler.ts @@ -64,16 +64,17 @@ export class CrashRecoveryHandler { throw new Error(`${fnTag}, Session not found`); } - const recoverMessage: RecoverMessage = { + const recoverMessage = new RecoverMessage({ sessionId: sessionId, messageType: "Recover", - satpPhase: sessionData.phase, - sequenceNumber: sessionData.lastSequenceNumber, + satpPhase: "phase", + sequenceNumber: Number(sessionData.lastSequenceNumber), isBackup: false, newIdentityPublicKey: "", lastEntryTimestamp: sessionData.lastSequenceNumber, senderSignature: "", - }; + }); + const updateMessage = this.service.createRecoverUpdateMessage(recoverMessage); diff --git a/packages/cactus-plugin-satp-hermes/src/main/typescript/core/recovery/crash-utils.ts b/packages/cactus-plugin-satp-hermes/src/main/typescript/core/recovery/crash-utils.ts index c958a5c5a7a..4a73a8635bf 100644 --- a/packages/cactus-plugin-satp-hermes/src/main/typescript/core/recovery/crash-utils.ts +++ b/packages/cactus-plugin-satp-hermes/src/main/typescript/core/recovery/crash-utils.ts @@ -16,7 +16,7 @@ interface ICrashRecoveryServiceOptions { loggerOptions: ILoggerOptions; logRepository: ILocalLogRepository; } -// todo: correct the data types + export class CrashRecoveryService { private readonly logger: Logger; private readonly logRepository: ILocalLogRepository; @@ -35,39 +35,39 @@ export class CrashRecoveryService { request.lastEntryTimestamp.toString(), ); - return { + return new RecoverUpdateMessage({ sessionId: request.sessionId, - messageType: "RecoverUpdate", + messageType: "urn:ietf:SATP-2pc:msgtype:recover-msg", hashRecoverMessage: "", recoveredLogs: recoveredLogs, senderSignature: "", - }; + }); } createRecoverSuccessMessage( request: RecoverUpdateMessage, ): RecoverSuccessMessage { this.logger.debug("Creating RecoverSuccessMessage..."); - return { + return new RecoverSuccessMessage({ sessionId: request.sessionId, - messageType: "RecoverSuccess", + messageType: "urn:ietf:SATP-2pc:msgtype:recover-update-msg", hashRecoverUpdateMessage: "", success: true, entriesChanged: [], senderSignature: "", - }; + }); } createRollbackAckMessage(request: RollbackMessage): RollbackAckMessage { this.logger.debug("Creating RollbackAckMessage..."); - return { + return new RollbackAckMessage({ sessionId: request.sessionId, - messageType: "RollbackAck", + messageType: "urn:ietf:SATP-2pc:msgtype:rollback-msg", success: true, actionsPerformed: [], proofs: [], senderSignature: "", - }; + }); } async sendRecoverMessage( diff --git a/packages/cactus-plugin-satp-hermes/src/main/typescript/plugin-satp-hermes-gateway.ts b/packages/cactus-plugin-satp-hermes/src/main/typescript/plugin-satp-hermes-gateway.ts index 9ac8e5829d8..59adfd5556e 100644 --- a/packages/cactus-plugin-satp-hermes/src/main/typescript/plugin-satp-hermes-gateway.ts +++ b/packages/cactus-plugin-satp-hermes/src/main/typescript/plugin-satp-hermes-gateway.ts @@ -57,7 +57,7 @@ import { import bodyParser from "body-parser"; import { CrashOcurrence, - CrashStatusManager, + CrashRecoveryManager, ICrashRecoveryManagerOptions, } from "./core/recovery/crash-manager"; import cors from "cors"; @@ -97,7 +97,7 @@ export class SATPGateway implements IPluginWebService, ICactusPlugin { public localRepository?: ILocalLogRepository; public remoteRepository?: IRemoteLogRepository; private readonly shutdownHooks: ShutdownHook[]; - private readonly crashManager: CrashStatusManager; + private readonly crashManager: CrashRecoveryManager; constructor(public readonly options: SATPGatewayConfig) { const fnTag = `${this.className}#constructor()`; @@ -186,8 +186,9 @@ export class SATPGateway implements IPluginWebService, ICactusPlugin { const crashOptions: ICrashRecoveryManagerOptions = { instanceId: this.instanceId, logLevel: this.config.logLevel, + bridgeConfig: SATPBridgeConfig, }; - this.crashManager = new CrashStatusManager(); + this.crashManager = new CrashRecoveryManager(crashOptions); this.crashManager.checkAndResolveCrash(); }