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

Distinguish between an empty and a null (not existing) reciprocal changeset #6367

Merged
merged 6 commits into from
Mar 15, 2023

Conversation

danieltabacaru
Copy link
Collaborator

@danieltabacaru danieltabacaru commented Mar 7, 2023

What, How & Why?

Once a reciprocal changeset has all its instructions discarded during OT, it is stored as a null changeset in the realm. This makes it hard to differentiate it from a non-existing reciprocal changeset and was causing histories to diverge among clients. A fix was put in place so that an empty reciprocal changeset is indeed saved as an empty changeset in the realm.

Fixes #6191.

☑️ ToDos

  • 📝 Changelog update
  • 🚦 Tests (or not relevant)
  • C-API, if public C++ API changed.

@danieltabacaru danieltabacaru marked this pull request as ready for review March 8, 2023 07:00
Copy link
Contributor

@michael-wb michael-wb left a comment

Choose a reason for hiding this comment

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

Looks good - I'm surprised this hasn't manifested in other ways than just the list out of order, although that might be the easiest one to detect.

if (is_compressed) {
size_t total_size;
auto decompressed = util::compression::decompress_nonportable_input_stream(in, total_size);
REALM_ASSERT(decompressed);
Copy link
Contributor

Choose a reason for hiding this comment

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

Update this to CHECK() or REQUIRE() so it integrates better into the test.

@jbreams
Copy link
Contributor

jbreams commented Mar 13, 2023

This is an excellent find, my only question is whether we're fixing this at the right place. Is it ever valid to call TransformHistory::set_reciprocal_transform() with a null BinaryData? Somehow changing the encoding of changesets so they contain intern strings whose values are never referenced doesn't feel right to me. What if instead of changing the encoding of changesets we made it so ClientHistory::set_reciprocal_transform() treated a null BinaryData as a non-null empty BinaryData? Maybe something like this?

void ClientHistory::set_reciprocal_transform(version_type version, BinaryData data)
{
    REALM_ASSERT(version > m_sync_history_base_version);
    std::size_t index = size_t(version - m_sync_history_base_version) - 1;
    REALM_ASSERT(index < sync_history_size());

    if (data.size() == 0) {
        m_arrays->reciprocal_transforms.set(index, BinaryData("", 0));
        return;
    }

    auto compressed = util::compression::allocate_and_compress_nonportable(data);
    m_arrays->reciprocal_transforms.set(index, BinaryData{compressed.data(), compressed.size()}); // Throws
}

Also, do we want to include the reproduction from the original issue here that actually instantiates some arrays and does moves on them as a slightly higher level integration test?

@tgoyne
Copy link
Member

tgoyne commented Mar 13, 2023

It looks like this bug was introduced in ae2e6d3. I removed the check which ensured that empty data was stored as empty instead of null because the compressed data would always have a header, and then changed it so that zero byte input compressed to zero byte output and made the check necessary again. add_sync_history_entry() may also need the check restored.

@danieltabacaru
Copy link
Collaborator Author

It looks like this bug was introduced in ae2e6d3. I removed the check which ensured that empty data was stored as empty instead of null because the compressed data would always have a header, and then changed it so that zero byte input compressed to zero byte output and made the check necessary again. add_sync_history_entry() may also need the check restored.

Thanks a lot for the insight. I restored the checks as they were before ae2e6d3.

@danieltabacaru
Copy link
Collaborator Author

This is an excellent find, my only question is whether we're fixing this at the right place. Is it ever valid to call TransformHistory::set_reciprocal_transform() with a null BinaryData? Somehow changing the encoding of changesets so they contain intern strings whose values are never referenced doesn't feel right to me. What if instead of changing the encoding of changesets we made it so ClientHistory::set_reciprocal_transform() treated a null BinaryData as a non-null empty BinaryData? Maybe something like this?

void ClientHistory::set_reciprocal_transform(version_type version, BinaryData data)
{
    REALM_ASSERT(version > m_sync_history_base_version);
    std::size_t index = size_t(version - m_sync_history_base_version) - 1;
    REALM_ASSERT(index < sync_history_size());

    if (data.size() == 0) {
        m_arrays->reciprocal_transforms.set(index, BinaryData("", 0));
        return;
    }

    auto compressed = util::compression::allocate_and_compress_nonportable(data);
    m_arrays->reciprocal_transforms.set(index, BinaryData{compressed.data(), compressed.size()}); // Throws
}

Also, do we want to include the reproduction from the original issue here that actually instantiates some arrays and does moves on them as a slightly higher level integration test?

I wanted to add that specific test but I need control on the changesets' timestamp to hit the critical code path which is not straightforward.

CHANGELOG.md Outdated
@@ -6,7 +6,7 @@

### Fixed
* <How do the end-user experience this issue? what was the impact?> ([#????](https://github.com/realm/realm-core/issues/????), since v?.?.?)
* None.
* Fixed a case of history diverging when empty reciprocal changesets are part of the merging window in OT ([#6191](https://github.com/realm/realm-core/issues/6191), since v11.13.0)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add something here like - this may have resulted in arrays being in different orders on different devices - so that users reading this will know what symptom to look for?

@danieltabacaru danieltabacaru merged commit d69c2dd into master Mar 15, 2023
@danieltabacaru danieltabacaru deleted the dt/fix_set_and_get_empty_reciprocal_changeset branch March 15, 2023 14:07
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RealmSwift.List order not in sync on different clients
4 participants