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

realm / realm-core / thomas.goyne_442

02 Jul 2024 07:51PM UTC coverage: 90.995% (+0.02%) from 90.974%
thomas.goyne_442

push

Evergreen

web-flow
[RCORE-2146] CAPI Remove `is_fatal` flag flip (#7751)

102372 of 180620 branches covered (56.68%)

0 of 1 new or added line in 1 file covered. (0.0%)

625 existing lines in 26 files now uncovered.

215592 of 236928 relevant lines covered (90.99%)

5608163.57 hits per line

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

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

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

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

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

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

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

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

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

6,312✔
179
    tr->commit();
6,312✔
180

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

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

2,012✔
200
bool PendingBootstrapStore::has_pending() const noexcept
2,012✔
201
{
2,012✔
202
    return m_has_pending;
2,012✔
203
}
204

2,270✔
205
void PendingBootstrapStore::clear()
2,270✔
206
{
207
    auto tr = m_db->start_write();
208
    clear(*tr);
18,090✔
209
    tr->commit();
18,090✔
210
}
18,090✔
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

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

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

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

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

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

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

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

2,196✔
277
    return ret;
2,196✔
278
}
279

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

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

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

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

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

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

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

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

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

1,996✔
339
    m_has_pending = !bootstrap_table->is_empty();
2,152✔
340
}
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