Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

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

liuyie10868cd208d
diff --git a/apps/server/src/modules/doc/manager.ts b/apps/server/src/modules/doc/manager.ts
index 68b18ebafd..ff30539ee0 100644
--- a/apps/server/src/modules/doc/manager.ts
+++ b/apps/server/src/modules/doc/manager.ts
@@ -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;
diff --git a/apps/server/src/modules/doc/redis-manager.ts b/apps/server/src/modules/doc/redis-manager.ts
index 0cb964759f..6651394ec4 100644
--- a/apps/server/src/modules/doc/redis-manager.ts
+++ b/apps/server/src/modules/doc/redis-manager.ts
@@ -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);
diff --git a/apps/server/src/modules/workspaces/resolver.ts b/apps/server/src/modules/workspaces/resolver.ts
index 4eef098f86..d95e0d1e6e 100644
--- a/apps/server/src/modules/workspaces/resolver.ts
+++ b/apps/server/src/modules/workspaces/resolver.ts
@@ -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;
}