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

Implement Document.GarbageCollect in JS SDK #101

Merged
merged 16 commits into from
Nov 22, 2020

Conversation

mojosoeun
Copy link
Member

@mojosoeun mojosoeun commented Nov 10, 2020

What does this PR do?

Implement Document.GarbageCollect in JS SDK

How should this be manually tested?

Any background context you want to provide?

I implemented this feature based on yorkie-team/yorkie#3. I tried to figure out how can I solve it. And after running npm run build:proto, lots of files in api folder were updated automatically 🤔. I'm not sure I did it correctly, so please feel free to feedback about my code :)

What are the relevant tickets?

Fixes #38

Checklist

  • Added relevant tests
  • Didn't break anything

@mojosoeun
Copy link
Member Author

hmm, the test failed. Let me check the test cases!

@dc7303
Copy link
Member

dc7303 commented Nov 13, 2020

Thanks for the great work PR.

I tested based on the yorkie-team/yorkie#100 issue. It seems that the same issue occurs as a result of testing.
I tested it by adding the temporary code below.

// document_test.ts
  it('garbage collection test3', function () {
    const doc = Document.create('test-col', 'test-doc');
    assert.equal('{}', doc.toSortedJSON());

    doc.update((root) => {
      root['list'] = [1, 2, 3];
    }, 'set list [1, 2, 3]');
    assert.equal('{"list":[1,2,3]}', doc.toSortedJSON());

    doc.update((root) => {
      delete root['list'][1];
    }, 'deletes index 1');
    assert.equal('{"list":[1,3]}', doc.toSortedJSON());
    assert.equal(1, doc.getGarbageLen());
    assert.equal(1, doc.garbageCollect(MaxTimeTicket));
    assert.equal(0, doc.getGarbageLen());

    (doc.getRootForDebug().getObject().get('list') as JSONArray).printAnnotatedString();
    (doc.getCloneRootForDebug().getObject().get('list') as JSONArray).printAnnotatedString();
    // for test
    assert.equal(0, 1);
  });
// document.ts
export class Document implements Observable<DocEvent> {
  ...

  public getRootForDebug() {
    return this.root;
  }

  public getCloneRootForDebug() {
    return this.clone;
  }
}
// array.ts
export class JSONArray extends JSONContainer {
  ...
  public printAnnotatedString() {
    logger.debug(this.elements.getAnnotatedString();
  }
}

If you add test codes and temporary codes and run the test, you can see the following results.

...
LOG: 'YORKIE D: [1:000000000000000000000000:2:1][1:000000000000000000000000:4:3]'
Chrome Headless 86.0.4240.193 (Mac OS 10.15.7): Executed 11 of 42 SUCCESS (0 secs / 0.217 secs)
LOG: 'YORKIE D: [1:000000000000000000000000:2:1]{1:000000000000000000000000:3:2}[1:000000000000000000000000:4:3]'
Chrome Headless 86.0.4240.193 (Mac OS 10.15.7): Executed 11 of 42 SUCCESS (0 secs / 0.217 secs)
...

If you look at the debug log, you can see that there are still nodes in the cloned root. If this happens, we believe that garbage collection at the language level will not be possible, resulting in a memory leak.

I think we must solve this problem first.
What is your opinion?

@mojosoeun
Copy link
Member Author

@dc7303 I think that's a good idea! Let me check the issue again!

@mojosoeun
Copy link
Member Author

@dc7303 @hackerwins I'm thinking about adding a test for comparing cloned root and root in the gc tests. What's your take on it?

@dc7303
Copy link
Member

dc7303 commented Nov 14, 2020

@mojosoeun I think it's a good idea :)
And it would be nice to add additional tests related to synchronization between clients.
https://github.com/yorkie-team/yorkie/blob/a56aa2553b4a1a84fb6f610da4006f00cb687329/client/client_test.go#L809-L935

1. add a test for comparing cloned root and root
2. add a garbage collection test in yorkie_test
3. add a garbage collection with detached document test
Copy link
Member

@hackerwins hackerwins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I like the overall modification and approach. 👍 Leave a few comments.

about dist/yorkie.js

We deleted the dist folder from the PR below. Is there any reason to add dist?
098090f

src/document/json/array.ts Outdated Show resolved Hide resolved
src/document/json/object.ts Outdated Show resolved Hide resolved
src/document/json/rht_pq_map.ts Show resolved Hide resolved
1. delete dist folder
2. add a new line for comment in array.ts
3. change return type from IterableIterator<JSONElement> to void for getDescendants function
@mojosoeun
Copy link
Member Author

@hackerwins Thank you for reviewing my code! When it comes to the dist folder, I think it was cached in my local, so it seems not to ignore the dist folder! After running this command git rm -r --cached ${dir}, it is finally ignored

Copy link
Member

@dc7303 dc7303 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for adding the test case. :)

Another request, I think it would be nice to add a comment to the code you added.
Because we have confirmed that contributors may suffer development delays if comments are missing.

src/document/json/array.ts Outdated Show resolved Hide resolved
src/document/json/root.ts Outdated Show resolved Hide resolved
src/document/operation/remove_operation.ts Outdated Show resolved Hide resolved
@dc7303 dc7303 self-requested a review November 15, 2020 02:03
@mojosoeun
Copy link
Member Author

@dc7303 Thank you for reviewing my code! 🙏 when it comes to comments in this code, How about dealing with it when resolving this issue 🤔 ?

Copy link
Member

@hackerwins hackerwins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for reflecting on the feedback. I left one more comment.

When it comes to comments, we can write to them next time(#98 is an issue that is intended to create a reference document for the user).

src/util/heap.ts Outdated Show resolved Hide resolved
mojosoeun and others added 7 commits November 16, 2020 18:00
remove redundant code in array, object, root's getDescendants function
delete the given node from the heap
use findIndex instead of for loop
1. implement deleting a node from binary heap
2. add test cases for release function
Copy link
Member

@hackerwins hackerwins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

Thank you. While reviewing this PR, I learned how to delete nodes from the heap.

And we are adding the GC function to the Text. Can you review it together?
yorkie-team/yorkie#104

src/util/heap.ts Outdated Show resolved Hide resolved
Copy link
Member

@dc7303 dc7303 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@codecov
Copy link

codecov bot commented Nov 22, 2020

Codecov Report

Merging #101 (167f402) into master (2a86f30) will increase coverage by 1.30%.
The diff coverage is 80.89%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #101      +/-   ##
==========================================
+ Coverage   72.02%   73.33%   +1.30%     
==========================================
  Files          48       48              
  Lines        2481     2550      +69     
  Branches      396      409      +13     
==========================================
+ Hits         1787     1870      +83     
+ Misses        537      511      -26     
- Partials      157      169      +12     
Impacted Files Coverage Δ
src/document/json/element.ts 73.33% <ø> (ø)
src/document/json/array.ts 82.35% <20.00%> (-2.50%) ⬇️
src/document/json/rga_tree_list.ts 81.65% <33.33%> (+5.68%) ⬆️
src/document/json/object.ts 80.00% <60.00%> (-4.10%) ⬇️
src/document/json/rht_pq_map.ts 66.66% <63.63%> (-2.09%) ⬇️
src/document/operation/remove_operation.ts 72.72% <75.00%> (-4.20%) ⬇️
src/util/heap.ts 85.96% <83.33%> (-5.15%) ⬇️
src/api/converter.ts 73.92% <100.00%> (+0.06%) ⬆️
src/document/change/change_pack.ts 100.00% <100.00%> (ø)
src/document/change/context.ts 100.00% <100.00%> (ø)
... and 9 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2a86f30...167f402. Read the comment docs.

@hackerwins hackerwins merged commit 34d33a5 into yorkie-team:master Nov 22, 2020
parkeunae pushed a commit to parkeunae/yorkie-js-sdk that referenced this pull request Jul 23, 2022
* Implement `Document.GarbageCollect` in JS SDK
* Run garbage collection for the clone
* Add garbage collection tests
  * Add a test for comparing cloned root and root
  * Add a garbage collection test in yorkie_test
  * Add a garbage collection with detached document test
* Add heap release function in-place way

Co-authored-by: hackerwins <susukang98@gmail.com>
@coderabbitai coderabbitai bot mentioned this pull request Jun 4, 2024
2 tasks
@coderabbitai coderabbitai bot mentioned this pull request Jun 14, 2024
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement Document.GarbageCollect in JS SDK
3 participants