fix(server): deal with unexpected updates (#4064)

This commit is contained in:
liuyi
2023-08-31 16:56:33 +08:00
committed by GitHub
parent 9bffe3cf24
commit e10868cd20
3 changed files with 26 additions and 10 deletions

View File

@@ -67,8 +67,17 @@ export class DocManager implements OnModuleInit, OnModuleDestroy {
protected recoverDoc(...updates: Buffer[]): Doc {
const doc = new Doc();
updates.forEach(update => {
applyUpdate(doc, update);
updates.forEach((update, i) => {
try {
if (update.length) {
applyUpdate(doc, update);
}
} catch (e) {
this.logger.error(
`Failed to apply updates, index: ${i}`,
updates.map(u => u.toString('hex'))
);
}
});
return doc;

View File

@@ -105,6 +105,8 @@ export class RedisDocManager extends DocManager {
.catch(() => null); // safe;
if (!lockResult) {
// we failed to acquire the lock, put the pending doc back to queue.
await this.redis.sadd(pending, pendingDoc).catch(() => null); // safe
return;
}
@@ -141,7 +143,10 @@ export class RedisDocManager extends DocManager {
this.logger.error('Failed to remove merged updates from Redis', e);
});
} catch (e) {
this.logger.error('Failed to merge updates with snapshot', e);
this.logger.error(
`Failed to merge updates with snapshot for ${pendingDoc}`,
e
);
await this.redis.sadd(pending, `${workspaceId}:${id}`).catch(() => null); // safe
} finally {
await this.redis.del(lockKey);

View File

@@ -326,13 +326,15 @@ export class WorkspaceResolver {
},
});
await this.prisma.snapshot.create({
data: {
id: workspace.id,
workspaceId: workspace.id,
blob: buffer,
},
});
if (buffer.length) {
await this.prisma.snapshot.create({
data: {
id: workspace.id,
workspaceId: workspace.id,
blob: buffer,
},
});
}
return workspace;
}