Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix miscalculation of tree size in concurrent editing #846

Merged
merged 3 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/document/crdt/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,11 +550,7 @@ export class CRDTTreeNode
}

if (alived) {
if (!this.parent!.removedAt) {
this.updateAncestorsSize();
} else {
this.parent!.size -= this.paddedSize;
}
this.updateAncestorsSize();
}
}

Expand Down
20 changes: 12 additions & 8 deletions src/util/index_tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ export abstract class IndexTreeNode<T extends IndexTreeNode<T>> {

while (parent) {
parent.size += this.paddedSize * sign;
if (parent.isRemoved) {
break;
}

parent = parent.parent;
}
}
Expand All @@ -152,17 +156,17 @@ export abstract class IndexTreeNode<T extends IndexTreeNode<T>> {
* the tree is newly created and the size of the descendants is not calculated.
*/
updateDescendantsSize(): number {
if (this.isRemoved) {
this.size = 0;
return 0;
}

let sum = 0;
let size = 0;
for (const child of this._children) {
sum += child.updateDescendantsSize();
const childSize = child.updateDescendantsSize();
if (child.isRemoved) {
continue;
}

size += childSize;
}

this.size += sum;
this.size += size;
hackerwins marked this conversation as resolved.
Show resolved Hide resolved

return this.paddedSize;
}
Expand Down
91 changes: 90 additions & 1 deletion test/integration/tree_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { describe, it, assert } from 'vitest';
import yorkie, { Tree, SyncMode } from '@yorkie-js-sdk/src/yorkie';
import yorkie, { Tree, SyncMode, converter } from '@yorkie-js-sdk/src/yorkie';
import {
testRPCAddr,
toDocKey,
Expand All @@ -27,6 +27,7 @@ import {
TreeStyleOpInfo,
} from '@yorkie-js-sdk/src/document/operation/operation';
import { Document, DocEventType } from '@yorkie-js-sdk/src/document/document';
import { toXML } from '@yorkie-js-sdk/src/document/crdt/tree';

describe('Tree', () => {
it('Can be created', function ({ task }) {
Expand Down Expand Up @@ -4122,6 +4123,94 @@ describe('Tree(edge cases)', () => {
}, task.name);
});

it('Can calculate size of index tree correctly during concurrent editing', async function ({
task,
}) {
await withTwoClientsAndDocuments<{ t: Tree }>(async (c1, d1, c2, d2) => {
d1.update((root) => {
root.t = new Tree({
type: 'doc',
children: [
{ type: 'p', children: [{ type: 'text', value: 'hello' }] },
],
});
});
await c1.sync();
await c2.sync();
assert.equal(d1.getRoot().t.toXML(), /*html*/ `<doc><p>hello</p></doc>`);
assert.equal(d2.getRoot().t.toXML(), /*html*/ `<doc><p>hello</p></doc>`);

d1.update((root) => root.t.edit(0, 7));
d2.update((root) => root.t.edit(1, 2, { type: 'text', value: 'p' }));
assert.equal(d1.getRoot().t.toXML(), /*html*/ `<doc></doc>`);
assert.equal(0, d1.getRoot().t.getSize());
assert.equal(d2.getRoot().t.toXML(), /*html*/ `<doc><p>pello</p></doc>`);
assert.equal(7, d2.getRoot().t.getSize());
await c1.sync();
await c2.sync();
await c1.sync();

assert.equal(d1.getRoot().t.toXML(), /*html*/ `<doc></doc>`);
assert.equal(d2.getRoot().t.toXML(), /*html*/ `<doc></doc>`);
assert.equal(d2.getRoot().t.getSize(), d1.getRoot().t.getSize());
}, task.name);
});

it('Can keep index tree consistent from snapshot', async function ({ task }) {
await withTwoClientsAndDocuments<{ t: Tree }>(async (c1, d1, c2, d2) => {
d1.update((root) => {
root.t = new Tree({
type: 'r',
children: [{ type: 'p', children: [] }],
});
});
await c1.sync();
await c2.sync();
assert.equal(d1.getRoot().t.toXML(), /*html*/ `<r><p></p></r>`);
assert.equal(d2.getRoot().t.toXML(), /*html*/ `<r><p></p></r>`);

d1.update((root) => root.t.edit(0, 2));
d2.update((root) => {
root.t.edit(1, 1, {
type: 'i',
children: [{ type: 'text', value: 'a' }],
});
root.t.edit(2, 3, { type: 'text', value: 'b' });
});
assert.equal(d1.getRoot().t.toXML(), /*html*/ `<r></r>`);
assert.equal(d1.getRoot().t.getSize(), 0);
assert.equal(d2.getRoot().t.toXML(), /*html*/ `<r><p><i>b</i></p></r>`);
assert.equal(5, d2.getRoot().t.getSize());
await c1.sync();
await c2.sync();
await c1.sync();
assert.equal(d1.getRoot().t.toXML(), /*html*/ `<r></r>`);
assert.equal(d2.getRoot().t.toXML(), /*html*/ `<r></r>`);

const d1Nodes: Array<[string, number, boolean]> = [];
const d2Nodes: Array<[string, number, boolean]> = [];
const sNodes: Array<[string, number, boolean]> = [];
d1.getRoot()
.t.getIndexTree()
.traverseAll((node) => {
d1Nodes.push([toXML(node), node.size, node.isRemoved]);
});
d2.getRoot()
.t.getIndexTree()
.traverseAll((node) => {
d2Nodes.push([toXML(node), node.size, node.isRemoved]);
});
const sRoot = converter.bytesToObject(
converter.objectToBytes(d1.getRootObject()),
);
(sRoot.get('t') as unknown as Tree).getIndexTree().traverseAll((node) => {
sNodes.push([toXML(node), node.size, node.isRemoved]);
});
assert.deepEqual(d1Nodes, d2Nodes);
assert.deepEqual(d1Nodes, sNodes);
}, task.name);
});

it('Can split and merge with empty paragraph: left', async function ({
task,
}) {
Expand Down
Loading