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

Applying updates gives inconsistent result #31

Closed
davidbrochart opened this issue Apr 16, 2022 · 2 comments
Closed

Applying updates gives inconsistent result #31

davidbrochart opened this issue Apr 16, 2022 · 2 comments
Assignees

Comments

@davidbrochart
Copy link
Collaborator

Using y-py==0.4.3, running the following script sometimes prints "aba", sometimes "aab":

import y_py as Y

ydoc1 = Y.YDoc()
ysource1 = ydoc1.get_text("source")

with ydoc1.begin_transaction() as t:
    ysource1.push(t, "a")

updates = [
    [1, 1, 157, 204, 146, 147, 3, 0, 4, 1, 6, 115, 111, 117, 114, 99, 101, 1, 97, 0],
    [1, 2, 234, 178, 234, 232, 13, 0, 40, 1, 5, 115, 116, 97, 116, 101, 5, 100, 105, 114, 116, 121, 1, 121, 40, 1, 7, 99, 111, 110, 116, 101, 120, 116, 4, 112, 97, 116, 104, 1, 119, 13, 117, 110, 116, 105, 116, 108, 101, 100, 52, 46, 116, 120, 116, 0],
    [1, 1, 234, 178, 234, 232, 13, 2, 168, 234, 178, 234, 232, 13, 0, 1, 120, 1, 234, 178, 234, 232, 13, 1, 0, 1],
    [1, 1, 234, 178, 234, 232, 13, 3, 40, 1, 7, 99, 111, 110, 116, 101, 120, 116, 13, 108, 97, 115, 116, 95, 109, 111, 100, 105, 102, 105, 101, 100, 1, 119, 27, 50, 48, 50, 50, 45, 48, 52, 45, 49, 54, 84, 49, 51, 58, 51, 55, 58, 51, 56, 46, 50, 49, 54, 56, 55, 49, 90, 0],
    [1, 1, 234, 178, 234, 232, 13, 4, 168, 234, 178, 234, 232, 13, 2, 1, 121, 1, 234, 178, 234, 232, 13, 1, 2, 1],
    [1, 2, 234, 178, 234, 232, 13, 5, 132, 157, 204, 146, 147, 3, 0, 1, 98, 168, 234, 178, 234, 232, 13, 4, 1, 120, 0],
    [1, 1, 234, 178, 234, 232, 13, 6, 168, 234, 178, 234, 232, 13, 4, 1, 120, 1, 234, 178, 234, 232, 13, 1, 4, 1],
]

for update in updates:
    Y.apply_update(ydoc1, update)

print(ysource1.to_json())
@Horusiath Horusiath self-assigned this Apr 17, 2022
@Horusiath
Copy link

tl;dr this works as expected. If you want to keep result consistent eg. for the sake of deterministic testing, you should create document with and explicit ID using Y.YDoc(client_id=X).

Longer explanation

The series of updates we're receiving in this scenario - concerning the ysource shared text type is basically:

  1. Create a document with randomised identifier X and put character "a" at the beginning.
  2. Concurrently to pt.1 at document with ID 845456925 insert character "a" at the beginning.
  3. After pt.2 (but still concurrently to pt.1) at the document with ID 3709507946 insert character "b" after character "a" inserted by 845456925.
  4. Serialise updates from pt.2 and pt.3. and apply them to document with ID X from pt.1.

The consistent part is that points 2. and 3. generate a string "ab" that's supposed to be inserted at the beginning of a ysource... however the actual position of a second "a" character is not set in stone, as we have concurrent operation of inserting character "a" at the same position by a different document.

In this case the conflict resolution algorithm will use document IDs to determine which piece of text ("ab" or "a") to insert first and which should be inserted second. Eg. if our document from pt.1 was generated with ID < 3709507946 (pt.3) the result will be "aba", but if its generated ID was higher, then the result will be "aab".

This means that depending on the run, the randomly generated ID can affect the order of operations. If that ID was static (not generated) the order would stay the same.

@davidbrochart
Copy link
Collaborator Author

Makes sense, thanks a lot for the explanations!

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

No branches or pull requests

2 participants