• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

realm / realm-core / 2387

05 Jun 2024 03:48PM UTC coverage: 90.873% (+0.03%) from 90.844%
2387

push

Evergreen

web-flow
Revert "Disable replication when writing to the pending bootstrap store" (#7776)

This reverts commit afe0e0f3b.

DisableReplication actually disables replication on the DB, not the
Transaction, so it's extremely unsafe to use.

101884 of 180236 branches covered (56.53%)

27 of 31 new or added lines in 1 file covered. (87.1%)

43 existing lines in 14 files now uncovered.

214843 of 236420 relevant lines covered (90.87%)

5990812.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.37
/src/realm/sync/noinst/pending_bootstrap_store.cpp
1
/*************************************************************************
2
 *
3
 * Copyright 2022 Realm, Inc.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 **************************************************************************/
18

19
#include "realm/sync/noinst/pending_bootstrap_store.hpp"
20

21
#include "realm/binary_data.hpp"
22
#include "realm/chunked_binary.hpp"
23
#include "realm/data_type.hpp"
24
#include "realm/db.hpp"
25
#include "realm/list.hpp"
26
#include "realm/query.hpp"
27
#include "realm/sync/changeset_parser.hpp"
28
#include "realm/sync/noinst/protocol_codec.hpp"
29
#include "realm/sync/noinst/sync_metadata_schema.hpp"
30
#include "realm/sync/protocol.hpp"
31
#include "realm/sync/transform.hpp"
32
#include "realm/util/assert.hpp"
33
#include "realm/util/buffer.hpp"
34
#include "realm/util/compression.hpp"
35
#include "realm/util/logger.hpp"
36
#include <stdexcept>
37

38
namespace realm::sync {
39
namespace {
40
constexpr static int c_schema_version = 1;
41
constexpr static std::string_view c_progress_table("flx_pending_bootstrap_progress");
42
constexpr static std::string_view c_pending_bootstrap_table("flx_pending_bootstrap");
43
constexpr static std::string_view c_pending_changesets_table("flx_pending_bootstrap_changesets");
44
constexpr static std::string_view c_pending_bootstrap_query_version("query_version");
45
constexpr static std::string_view c_pending_bootstrap_changesets("changesets");
46
constexpr static std::string_view c_pending_bootstrap_progress("progress");
47
constexpr static std::string_view c_pending_changesets_remote_version("remote_version");
48
constexpr static std::string_view
49
    c_pending_changesets_last_integrated_client_version("last_integrated_client_version");
50
constexpr static std::string_view c_pending_changesets_origin_file_ident("origin_file_ident");
51
constexpr static std::string_view c_pending_changesets_origin_timestamp("origin_timestamp");
52
constexpr static std::string_view c_pending_changesets_original_size("original_size");
53
constexpr static std::string_view c_pending_changesets_data("data");
54
constexpr static std::string_view c_progress_download_server_version("download_server_version");
55
constexpr static std::string_view c_progress_download_client_version("download_client_version");
56
constexpr static std::string_view c_progress_upload_server_version("upload_server_version");
57
constexpr static std::string_view c_progress_upload_client_version("upload_client_version");
58
constexpr static std::string_view c_progress_latest_server_version("latest_server_version");
59
constexpr static std::string_view c_progress_latest_server_version_salt("latest_server_version_salt");
60

61
} // namespace
62

63
PendingBootstrapStore::PendingBootstrapStore(DBRef db, util::Logger& logger)
64
    : m_db(std::move(db))
762✔
65
    , m_logger(logger)
762✔
66
{
1,522✔
67
    std::vector<SyncMetadataTable> internal_tables{
1,522✔
68
        {&m_table,
1,522✔
69
         c_pending_bootstrap_table,
1,522✔
70
         {&m_query_version, c_pending_bootstrap_query_version, type_Int},
1,522✔
71
         {
1,522✔
72
             {&m_changesets, c_pending_bootstrap_changesets, c_pending_changesets_table, true},
1,522✔
73
             {&m_progress, c_pending_bootstrap_progress, c_progress_table, false},
1,522✔
74
         }},
1,522✔
75
        {&m_progress_table,
1,522✔
76
         c_progress_table,
1,522✔
77
         SyncMetadataTable::IsEmbeddedTag{},
1,522✔
78
         {
1,522✔
79
             {&m_progress_upload_server_version, c_progress_upload_server_version, type_Int},
1,522✔
80
             {&m_progress_upload_client_version, c_progress_upload_client_version, type_Int},
1,522✔
81
             {&m_progress_download_server_version, c_progress_download_server_version, type_Int},
1,522✔
82
             {&m_progress_download_client_version, c_progress_download_client_version, type_Int},
1,522✔
83
             {&m_progress_latest_server_version, c_progress_latest_server_version, type_Int},
1,522✔
84
             {&m_progress_latest_server_version_salt, c_progress_latest_server_version_salt, type_Int},
1,522✔
85
         }},
1,522✔
86
        {&m_changeset_table,
1,522✔
87
         c_pending_changesets_table,
1,522✔
88
         SyncMetadataTable::IsEmbeddedTag{},
1,522✔
89
         {
1,522✔
90
             {&m_changeset_remote_version, c_pending_changesets_remote_version, type_Int},
1,522✔
91
             {&m_changeset_last_integrated_client_version, c_pending_changesets_last_integrated_client_version,
1,522✔
92
              type_Int},
1,522✔
93
             {&m_changeset_origin_file_ident, c_pending_changesets_origin_file_ident, type_Int},
1,522✔
94
             {&m_changeset_origin_timestamp, c_pending_changesets_origin_timestamp, type_Int},
1,522✔
95
             {&m_changeset_original_changeset_size, c_pending_changesets_original_size, type_Int},
1,522✔
96
             {&m_changeset_data, c_pending_changesets_data, type_Binary, true},
1,522✔
97
         }}};
1,522✔
98

99
    auto tr = m_db->start_read();
1,522✔
100
    // Start with a reader so it doesn't try to write until we are ready
101
    SyncMetadataSchemaVersionsReader schema_versions_reader(tr);
1,522✔
102
    if (auto schema_version =
1,522✔
103
            schema_versions_reader.get_version_for(tr, internal_schema_groups::c_pending_bootstraps)) {
1,522✔
104
        if (*schema_version != c_schema_version) {
502✔
105
            throw RuntimeError(ErrorCodes::SchemaVersionMismatch,
×
106
                               "Invalid schema version for FLX sync pending bootstrap table group");
×
107
        }
×
108
        load_sync_metadata_schema(tr, &internal_tables);
502✔
109
    }
502✔
110
    else {
1,020✔
111
        tr->promote_to_write();
1,020✔
112
        // Ensure the schema versions table is initialized (may add its own commit)
113
        SyncMetadataSchemaVersions schema_versions(tr);
1,020✔
114
        // Create the metadata schema and set the version (in the same commit)
115
        schema_versions.set_version_for(tr, internal_schema_groups::c_pending_bootstraps, c_schema_version);
1,020✔
116
        create_sync_metadata_schema(tr, &internal_tables);
1,020✔
117
        tr->commit_and_continue_as_read();
1,020✔
118
    }
1,020✔
119
    REALM_ASSERT(m_table);
1,522✔
120

121
    if (auto bootstrap_table = tr->get_table(m_table); !bootstrap_table->is_empty()) {
1,522✔
122
        m_has_pending = true;
40✔
123
    }
40✔
124
    else {
1,482✔
125
        m_has_pending = false;
1,482✔
126
    }
1,482✔
127
}
1,522✔
128

129
void PendingBootstrapStore::add_batch(int64_t query_version, util::Optional<SyncProgress> progress,
130
                                      const _impl::ClientProtocol::ReceivedChangesets& changesets,
131
                                      bool* created_new_batch_out)
132
{
2,148✔
133
    std::vector<util::AppendBuffer<char>> compressed_changesets;
2,148✔
134
    compressed_changesets.reserve(changesets.size());
2,148✔
135

136
    util::compression::CompressMemoryArena arena;
2,148✔
137
    for (auto& changeset : changesets) {
2,164✔
138
        compressed_changesets.emplace_back();
2,164✔
139
        util::compression::allocate_and_compress_nonportable(arena, {changeset.data.get_first_chunk()},
2,164✔
140
                                                             compressed_changesets.back());
2,164✔
141
    }
2,164✔
142

143
    auto tr = m_db->start_write();
2,148✔
144
    auto bootstrap_table = tr->get_table(m_table);
2,148✔
145
    auto incomplete_bootstraps = Query(bootstrap_table).not_equal(m_query_version, query_version).find_all();
2,148✔
146
    incomplete_bootstraps.for_each([&](Obj obj) {
2,148✔
NEW
147
        m_logger.debug(util::LogCategory::changeset, "Clearing incomplete bootstrap for query version %1",
×
NEW
148
                       obj.get<int64_t>(m_query_version));
×
NEW
149
        return IteratorControl::AdvanceToNext;
×
NEW
150
    });
×
151
    incomplete_bootstraps.clear();
2,148✔
152

153
    bool did_create = false;
2,148✔
154
    auto bootstrap_obj = bootstrap_table->create_object_with_primary_key(Mixed{query_version}, &did_create);
2,148✔
155
    if (progress) {
2,148✔
156
        auto progress_obj = bootstrap_obj.create_and_set_linked_object(m_progress);
1,944✔
157
        progress_obj.set(m_progress_latest_server_version, int64_t(progress->latest_server_version.version));
1,944✔
158
        progress_obj.set(m_progress_latest_server_version_salt, int64_t(progress->latest_server_version.salt));
1,944✔
159
        progress_obj.set(m_progress_download_server_version, int64_t(progress->download.server_version));
1,944✔
160
        progress_obj.set(m_progress_download_client_version,
1,944✔
161
                         int64_t(progress->download.last_integrated_client_version));
1,944✔
162
        progress_obj.set(m_progress_upload_server_version, int64_t(progress->upload.last_integrated_server_version));
1,944✔
163
        progress_obj.set(m_progress_upload_client_version, int64_t(progress->upload.client_version));
1,944✔
164
    }
1,944✔
165

166
    auto changesets_list = bootstrap_obj.get_linklist(m_changesets);
2,148✔
167
    for (size_t idx = 0; idx < changesets.size(); ++idx) {
4,312✔
168
        auto cur_changeset = changesets_list.create_and_insert_linked_object(changesets_list.size());
2,164✔
169
        cur_changeset.set(m_changeset_remote_version, int64_t(changesets[idx].remote_version));
2,164✔
170
        cur_changeset.set(m_changeset_last_integrated_client_version,
2,164✔
171
                          int64_t(changesets[idx].last_integrated_local_version));
2,164✔
172
        cur_changeset.set(m_changeset_origin_file_ident, int64_t(changesets[idx].origin_file_ident));
2,164✔
173
        cur_changeset.set(m_changeset_origin_timestamp, int64_t(changesets[idx].origin_timestamp));
2,164✔
174
        cur_changeset.set(m_changeset_original_changeset_size, int64_t(changesets[idx].original_changeset_size));
2,164✔
175
        BinaryData compressed_data(compressed_changesets[idx].data(), compressed_changesets[idx].size());
2,164✔
176
        cur_changeset.set(m_changeset_data, compressed_data);
2,164✔
177
    }
2,164✔
178

179
    tr->commit();
2,148✔
180

181
    if (created_new_batch_out) {
2,148✔
182
        *created_new_batch_out = did_create;
2,148✔
183
    }
2,148✔
184

185
    if (did_create) {
2,148✔
186
        m_logger.debug(util::LogCategory::changeset, "Created new pending bootstrap object for query version %1",
1,952✔
187
                       query_version);
1,952✔
188
    }
1,952✔
189
    else {
196✔
190
        m_logger.debug(util::LogCategory::changeset, "Added batch to pending bootstrap object for query version %1",
196✔
191
                       query_version);
196✔
192
    }
196✔
193
    if (progress) {
2,148✔
194
        m_logger.debug(util::LogCategory::changeset, "Finalized pending bootstrap object for query version %1",
1,944✔
195
                       query_version);
1,944✔
196
    }
1,944✔
197
    m_has_pending = true;
2,148✔
198
}
2,148✔
199

200
bool PendingBootstrapStore::has_pending()
201
{
7,514✔
202
    return m_has_pending;
7,514✔
203
}
7,514✔
204

205
void PendingBootstrapStore::clear()
206
{
12✔
207
    auto tr = m_db->start_write();
12✔
208
    clear(*tr);
12✔
209
    tr->commit();
12✔
210
}
12✔
211

212
void PendingBootstrapStore::clear(Transaction& wt)
213
{
12✔
214
    auto bootstrap_table = wt.get_table(m_table);
12✔
215
    bootstrap_table->clear();
12✔
216
    m_has_pending = false;
12✔
217
}
12✔
218

219
PendingBootstrapStore::PendingBatch PendingBootstrapStore::peek_pending(size_t limit_in_bytes)
220
{
2,152✔
221
    auto tr = m_db->start_read();
2,152✔
222
    auto bootstrap_table = tr->get_table(m_table);
2,152✔
223
    if (bootstrap_table->is_empty()) {
2,152✔
224
        return {};
4✔
225
    }
4✔
226

227
    // We should only have one pending bootstrap at a time.
228
    REALM_ASSERT(bootstrap_table->size() == 1);
2,148✔
229

230
    auto bootstrap_obj = bootstrap_table->get_object(0);
2,148✔
231
    PendingBatch ret;
2,148✔
232
    ret.query_version = bootstrap_obj.get<int64_t>(m_query_version);
2,148✔
233

234
    if (!bootstrap_obj.is_null(m_progress)) {
2,148✔
235
        auto progress_obj = bootstrap_obj.get_linked_object(m_progress);
2,136✔
236
        SyncProgress progress;
2,136✔
237
        progress.latest_server_version.version = progress_obj.get<int64_t>(m_progress_latest_server_version);
2,136✔
238
        progress.latest_server_version.salt = progress_obj.get<int64_t>(m_progress_latest_server_version_salt);
2,136✔
239
        progress.download.server_version = progress_obj.get<int64_t>(m_progress_download_server_version);
2,136✔
240
        progress.download.last_integrated_client_version =
2,136✔
241
            progress_obj.get<int64_t>(m_progress_download_client_version);
2,136✔
242
        progress.upload.last_integrated_server_version = progress_obj.get<int64_t>(m_progress_upload_server_version);
2,136✔
243
        progress.upload.client_version = progress_obj.get<int64_t>(m_progress_upload_client_version);
2,136✔
244
        ret.progress = std::move(progress);
2,136✔
245
    }
2,136✔
246

247
    auto changeset_list = bootstrap_obj.get_linklist(m_changesets);
2,148✔
248
    size_t bytes_so_far = 0;
2,148✔
249
    for (size_t idx = 0; idx < changeset_list.size() && bytes_so_far < limit_in_bytes; ++idx) {
4,372✔
250
        auto cur_changeset = changeset_list.get_object(idx);
2,224✔
251
        ret.changeset_data.push_back(util::AppendBuffer<char>());
2,224✔
252
        auto& uncompressed_buffer = ret.changeset_data.back();
2,224✔
253

254
        auto compressed_changeset_data = cur_changeset.get<BinaryData>(m_changeset_data);
2,224✔
255
        ChunkedBinaryInputStream changeset_is(compressed_changeset_data);
2,224✔
256
        auto ec = util::compression::decompress_nonportable(changeset_is, uncompressed_buffer);
2,224✔
257
        if (ec == util::compression::error::decompress_unsupported) {
2,224✔
258
            REALM_TERMINATE(
259
                "Synchronized Realm files with unprocessed pending bootstraps cannot be copied between platforms.");
×
260
        }
×
261
        REALM_ASSERT_3(ec, ==, std::error_code{});
2,224✔
262

263
        RemoteChangeset parsed_changeset;
2,224✔
264
        parsed_changeset.original_changeset_size =
2,224✔
265
            static_cast<size_t>(cur_changeset.get<int64_t>(m_changeset_original_changeset_size));
2,224✔
266
        parsed_changeset.origin_timestamp = cur_changeset.get<int64_t>(m_changeset_origin_timestamp);
2,224✔
267
        parsed_changeset.origin_file_ident = cur_changeset.get<int64_t>(m_changeset_origin_file_ident);
2,224✔
268
        parsed_changeset.remote_version = cur_changeset.get<int64_t>(m_changeset_remote_version);
2,224✔
269
        parsed_changeset.last_integrated_local_version =
2,224✔
270
            cur_changeset.get<int64_t>(m_changeset_last_integrated_client_version);
2,224✔
271
        parsed_changeset.data = BinaryData(uncompressed_buffer.data(), uncompressed_buffer.size());
2,224✔
272
        bytes_so_far += parsed_changeset.data.size();
2,224✔
273
        ret.changesets.push_back(std::move(parsed_changeset));
2,224✔
274
    }
2,224✔
275
    ret.remaining_changesets = changeset_list.size() - ret.changesets.size();
2,148✔
276

277
    return ret;
2,148✔
278
}
2,152✔
279

280
PendingBootstrapStore::PendingBatchStats PendingBootstrapStore::pending_stats()
281
{
2,148✔
282
    auto tr = m_db->start_read();
2,148✔
283
    auto bootstrap_table = tr->get_table(m_table);
2,148✔
284
    if (bootstrap_table->is_empty()) {
2,148✔
285
        return {};
×
286
    }
×
287

288
    REALM_ASSERT(bootstrap_table->size() == 1);
2,148✔
289

290
    auto bootstrap_obj = bootstrap_table->get_object(0);
2,148✔
291
    auto changeset_list = bootstrap_obj.get_linklist(m_changesets);
2,148✔
292

293
    PendingBatchStats stats;
2,148✔
294
    stats.query_version = bootstrap_obj.get<int64_t>(m_query_version);
2,148✔
295
    stats.pending_changesets = changeset_list.size();
2,148✔
296
    changeset_list.for_each([&](Obj& cur_changeset) {
2,748✔
297
        stats.pending_changeset_bytes +=
2,748✔
298
            static_cast<size_t>(cur_changeset.get<int64_t>(m_changeset_original_changeset_size));
2,748✔
299
        return IteratorControl::AdvanceToNext;
2,748✔
300
    });
2,748✔
301

302
    return stats;
2,148✔
303
}
2,148✔
304

305
void PendingBootstrapStore::pop_front_pending(const TransactionRef& tr, size_t count)
306
{
2,104✔
307
    REALM_ASSERT_3(tr->get_transact_stage(), ==, DB::transact_Writing);
2,104✔
308
    auto bootstrap_table = tr->get_table(m_table);
2,104✔
309
    if (bootstrap_table->is_empty()) {
2,104✔
310
        return;
×
311
    }
×
312

313
    // We should only have one pending bootstrap at a time.
314
    REALM_ASSERT(bootstrap_table->size() == 1);
2,104✔
315

316
    auto bootstrap_obj = bootstrap_table->get_object(0);
2,104✔
317
    auto changeset_list = bootstrap_obj.get_linklist(m_changesets);
2,104✔
318
    REALM_ASSERT_3(changeset_list.size(), >=, count);
2,104✔
319
    if (count == changeset_list.size()) {
2,104✔
320
        changeset_list.clear();
1,928✔
321
    }
1,928✔
322
    else {
176✔
323
        for (size_t idx = 0; idx < count; ++idx) {
360✔
324
            changeset_list.remove(0);
184✔
325
        }
184✔
326
    }
176✔
327

328
    if (changeset_list.is_empty()) {
2,104✔
329
        m_logger.debug(util::LogCategory::changeset, "Removing pending bootstrap obj for query version %1",
1,928✔
330
                       bootstrap_obj.get<int64_t>(m_query_version));
1,928✔
331
        bootstrap_obj.remove();
1,928✔
332
    }
1,928✔
333
    else {
176✔
334
        m_logger.debug(util::LogCategory::changeset,
176✔
335
                       "Removing pending bootstrap batch for query version %1. %2 changeset remaining",
176✔
336
                       bootstrap_obj.get<int64_t>(m_query_version), changeset_list.size());
176✔
337
    }
176✔
338

339
    m_has_pending = !bootstrap_table->is_empty();
2,104✔
340
}
2,104✔
341

342
} // namespace realm::sync
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc