Skip to content

Commit

Permalink
fix: correct return types and inits
Browse files Browse the repository at this point in the history
Signed-off-by: Yogesh01000100 <yogeshone678@gmail.com>
  • Loading branch information
Yogesh01000100 committed Sep 16, 2024
1 parent 639e2d5 commit 728e7cb
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, SessionData>;
private sessions: Map<string, SessionData>;
private crashRecoveryHandler: CrashRecoveryHandler;
private factory: RollbackStrategyFactory;
private logRepository: ILocalLogRepository;
Expand All @@ -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<string, SessionData>();
this.sessions = new Map<string, SessionData>();
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",
Expand All @@ -76,10 +85,13 @@ export class CrashRecoveryManager {
return CrashRecoveryManager.CLASS_NAME;
}

public async init(): Promise<void> {
this.sessions = await this.getSessions();
}

// todo read from local log to get session data
private async getSessions(): Map<string, SessionData> {
/*private async getSessions(): Map<string, SessionData> {
const sessionMap = new Map<string, SessionData>();
// todo! session data mismatch with logs??
try {
const allSessions = await this.logRepository.readLogsNotProofs();
allSessions.forEach((log) => {
Expand All @@ -92,6 +104,43 @@ export class CrashRecoveryManager {
this.log.error(`Error initializing sessions: ${error}`);
}
return sessionMap;
}*/

private async getSessions(): Promise<Map<string, SessionData>> {
const sessionMap = new Map<string, SessionData>();

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;
}

Expand Down Expand Up @@ -200,7 +249,7 @@ export class CrashRecoveryManager {
}
}

private async handleRecovery(session: SATPSession): Promise<void> {
public async handleRecovery(session: SATPSession): Promise<void> {
const fnTag = `${this.className}#handleRecovery()`;

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import {
import bodyParser from "body-parser";
import {
CrashOcurrence,
CrashStatusManager,
CrashRecoveryManager,
ICrashRecoveryManagerOptions,
} from "./core/recovery/crash-manager";
import cors from "cors";
Expand Down Expand Up @@ -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()`;
Expand Down Expand Up @@ -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();
}

Expand Down

0 comments on commit 728e7cb

Please sign in to comment.