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 { protected recoverDoc(...updates: Buffer[]): Doc {
const doc = new Doc(); const doc = new Doc();
updates.forEach(update => { updates.forEach((update, i) => {
applyUpdate(doc, update); 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; return doc;

View File

@@ -105,6 +105,8 @@ export class RedisDocManager extends DocManager {
.catch(() => null); // safe; .catch(() => null); // safe;
if (!lockResult) { 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; return;
} }
@@ -141,7 +143,10 @@ export class RedisDocManager extends DocManager {
this.logger.error('Failed to remove merged updates from Redis', e); this.logger.error('Failed to remove merged updates from Redis', e);
}); });
} catch (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 await this.redis.sadd(pending, `${workspaceId}:${id}`).catch(() => null); // safe
} finally { } finally {
await this.redis.del(lockKey); await this.redis.del(lockKey);

View File

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