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

realm / realm-core / thomas.goyne_113

27 Oct 2023 10:49AM UTC coverage: 91.596% (+0.03%) from 91.571%
thomas.goyne_113

push

Evergreen

web-flow
Merge pull request #7085 from realm/release/13.23.2

Release/13.23.2

91788 of 168244 branches covered (0.0%)

230165 of 251282 relevant lines covered (91.6%)

6444552.92 hits per line

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

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

510✔
99
    auto tr = m_db->start_read();
1,018✔
100
    SyncMetadataSchemaVersions schema_versions(tr);
1,018✔
101
    if (auto schema_version = schema_versions.get_version_for(tr, internal_schema_groups::c_pending_bootstraps)) {
510✔
102
        if (*schema_version != c_schema_version) {
1,018✔
103
            throw RuntimeError(ErrorCodes::SchemaVersionMismatch,
32✔
104
                               "Invalid schema version for FLX sync pending bootstrap table group");
32✔
105
        }
986✔
106
        load_sync_metadata_schema(tr, &internal_tables);
986✔
107
    }
986✔
108
    else {
1,018✔
109
        tr->promote_to_write();
110
        create_sync_metadata_schema(tr, &internal_tables);
111
        schema_versions.set_version_for(tr, internal_schema_groups::c_pending_bootstraps, c_schema_version);
112
        tr->commit_and_continue_as_read();
113
    }
1,578✔
114

1,578✔
115
    if (auto bootstrap_table = tr->get_table(m_table); !bootstrap_table->is_empty()) {
1,578✔
116
        m_has_pending = true;
790✔
117
    }
1,578✔
118
    else {
1,594✔
119
        m_has_pending = false;
1,594✔
120
    }
1,594✔
121
}
1,594✔
122

1,594✔
123
void PendingBootstrapStore::add_batch(int64_t query_version, util::Optional<SyncProgress> progress,
790✔
124
                                      const _impl::ClientProtocol::ReceivedChangesets& changesets,
1,578✔
125
                                      bool* created_new_batch_out)
1,578✔
126
{
1,578✔
127
    std::vector<util::AppendBuffer<char>> compressed_changesets;
790✔
128
    compressed_changesets.reserve(changesets.size());
×
129

×
130
    util::compression::CompressMemoryArena arena;
×
131
    for (auto& changeset : changesets) {
1,578✔
132
        compressed_changesets.emplace_back();
790✔
133
        util::compression::allocate_and_compress_nonportable(arena, {changeset.data.get_first_chunk()},
1,578✔
134
                                                             compressed_changesets.back());
1,578✔
135
    }
1,578✔
136

1,414✔
137
    auto tr = m_db->start_write();
1,414✔
138
    auto bootstrap_table = tr->get_table(m_table);
1,414✔
139
    auto incomplete_bootstraps = Query(bootstrap_table).not_equal(m_query_version, query_version).find_all();
1,414✔
140
    incomplete_bootstraps.for_each([&](Obj obj) {
1,414✔
141
        m_logger.debug("Clearing incomplete bootstrap for query version %1", obj.get<int64_t>(m_query_version));
1,414✔
142
        return IteratorControl::AdvanceToNext;
1,414✔
143
    });
1,414✔
144
    incomplete_bootstraps.clear();
1,414✔
145

790✔
146
    bool did_create = false;
1,578✔
147
    auto bootstrap_obj = bootstrap_table->create_object_with_primary_key(Mixed{query_version}, &did_create);
3,172✔
148
    if (progress) {
1,594✔
149
        auto progress_obj = bootstrap_obj.create_and_set_linked_object(m_progress);
1,594✔
150
        progress_obj.set(m_progress_latest_server_version, int64_t(progress->latest_server_version.version));
1,594✔
151
        progress_obj.set(m_progress_latest_server_version_salt, int64_t(progress->latest_server_version.salt));
1,594✔
152
        progress_obj.set(m_progress_download_server_version, int64_t(progress->download.server_version));
1,594✔
153
        progress_obj.set(m_progress_download_client_version,
1,594✔
154
                         int64_t(progress->download.last_integrated_client_version));
1,594✔
155
        progress_obj.set(m_progress_upload_server_version, int64_t(progress->upload.last_integrated_server_version));
1,594✔
156
        progress_obj.set(m_progress_upload_client_version, int64_t(progress->upload.client_version));
1,594✔
157
    }
1,594✔
158

790✔
159
    auto changesets_list = bootstrap_obj.get_linklist(m_changesets);
1,578✔
160
    for (size_t idx = 0; idx < changesets.size(); ++idx) {
790✔
161
        auto cur_changeset = changesets_list.create_and_insert_linked_object(changesets_list.size());
1,578✔
162
        cur_changeset.set(m_changeset_remote_version, int64_t(changesets[idx].remote_version));
1,578✔
163
        cur_changeset.set(m_changeset_last_integrated_client_version,
1,578✔
164
                          int64_t(changesets[idx].last_integrated_local_version));
790✔
165
        cur_changeset.set(m_changeset_origin_file_ident, int64_t(changesets[idx].origin_file_ident));
1,578✔
166
        cur_changeset.set(m_changeset_origin_timestamp, int64_t(changesets[idx].origin_timestamp));
1,422✔
167
        cur_changeset.set(m_changeset_original_changeset_size, int64_t(changesets[idx].original_changeset_size));
1,422✔
168
        BinaryData compressed_data(compressed_changesets[idx].data(), compressed_changesets[idx].size());
156✔
169
        cur_changeset.set(m_changeset_data, compressed_data);
156✔
170
    }
156✔
171

1,578✔
172
    tr->commit();
1,414✔
173

1,414✔
174
    if (created_new_batch_out) {
1,578✔
175
        *created_new_batch_out = did_create;
1,578✔
176
    }
177

178
    if (did_create) {
5,376✔
179
        m_logger.debug("Created new pending bootstrap object for query version %1", query_version);
5,376✔
180
    }
5,376✔
181
    else {
182
        m_logger.debug("Added batch to pending bootstrap object for query version %1", query_version);
183
    }
12✔
184
    if (progress) {
12✔
185
        m_logger.debug("Finalized pending bootstrap object for query version %1", query_version);
12✔
186
    }
12✔
187
    m_has_pending = true;
12✔
188
}
12✔
189

12✔
190
bool PendingBootstrapStore::has_pending()
191
{
192
    return m_has_pending;
1,574✔
193
}
1,574✔
194

1,574✔
195
void PendingBootstrapStore::clear()
1,574✔
196
{
4✔
197
    auto tr = m_db->start_write();
4✔
198
    auto bootstrap_table = tr->get_table(m_table);
786✔
199
    bootstrap_table->clear();
786✔
200
    m_has_pending = false;
1,570✔
201
    tr->commit();
786✔
202
}
1,570✔
203

1,570✔
204
PendingBootstrapStore::PendingBatch PendingBootstrapStore::peek_pending(size_t limit_in_bytes)
1,570✔
205
{
786✔
206
    auto tr = m_db->start_read();
1,570✔
207
    auto bootstrap_table = tr->get_table(m_table);
1,558✔
208
    if (bootstrap_table->is_empty()) {
1,558✔
209
        return {};
1,558✔
210
    }
1,558✔
211

1,558✔
212
    // We should only have one pending bootstrap at a time.
1,558✔
213
    REALM_ASSERT(bootstrap_table->size() == 1);
1,558✔
214

1,558✔
215
    auto bootstrap_obj = bootstrap_table->get_object(0);
1,558✔
216
    PendingBatch ret;
1,558✔
217
    ret.query_version = bootstrap_obj.get<int64_t>(m_query_version);
1,558✔
218

786✔
219
    if (!bootstrap_obj.is_null(m_progress)) {
1,570✔
220
        auto progress_obj = bootstrap_obj.get_linked_object(m_progress);
1,570✔
221
        SyncProgress progress;
3,196✔
222
        progress.latest_server_version.version = progress_obj.get<int64_t>(m_progress_latest_server_version);
1,626✔
223
        progress.latest_server_version.salt = progress_obj.get<int64_t>(m_progress_latest_server_version_salt);
1,626✔
224
        progress.download.server_version = progress_obj.get<int64_t>(m_progress_download_server_version);
1,626✔
225
        progress.download.last_integrated_client_version =
814✔
226
            progress_obj.get<int64_t>(m_progress_download_client_version);
1,626✔
227
        progress.upload.last_integrated_server_version = progress_obj.get<int64_t>(m_progress_upload_server_version);
1,626✔
228
        progress.upload.client_version = progress_obj.get<int64_t>(m_progress_upload_client_version);
1,626✔
229
        ret.progress = std::move(progress);
1,626✔
230
    }
×
231

×
232
    auto changeset_list = bootstrap_obj.get_linklist(m_changesets);
×
233
    size_t bytes_so_far = 0;
1,626✔
234
    for (size_t idx = 0; idx < changeset_list.size() && bytes_so_far < limit_in_bytes; ++idx) {
814✔
235
        auto cur_changeset = changeset_list.get_object(idx);
1,626✔
236
        ret.changeset_data.push_back(util::AppendBuffer<char>());
1,626✔
237
        auto& uncompressed_buffer = ret.changeset_data.back();
1,626✔
238

1,626✔
239
        auto compressed_changeset_data = cur_changeset.get<BinaryData>(m_changeset_data);
1,626✔
240
        ChunkedBinaryInputStream changeset_is(compressed_changeset_data);
1,626✔
241
        auto ec = util::compression::decompress_nonportable(changeset_is, uncompressed_buffer);
1,626✔
242
        if (ec == util::compression::error::decompress_unsupported) {
1,626✔
243
            REALM_TERMINATE(
1,626✔
244
                "Synchronized Realm files with unprocessed pending bootstraps cannot be copied between platforms.");
1,626✔
245
        }
1,626✔
246
        REALM_ASSERT_3(ec, ==, std::error_code{});
1,626✔
247

1,570✔
248
        Transformer::RemoteChangeset parsed_changeset;
786✔
249
        parsed_changeset.original_changeset_size =
1,570✔
250
            static_cast<size_t>(cur_changeset.get<int64_t>(m_changeset_original_changeset_size));
1,570✔
251
        parsed_changeset.origin_timestamp = cur_changeset.get<int64_t>(m_changeset_origin_timestamp);
252
        parsed_changeset.origin_file_ident = cur_changeset.get<int64_t>(m_changeset_origin_file_ident);
253
        parsed_changeset.remote_version = cur_changeset.get<int64_t>(m_changeset_remote_version);
1,418✔
254
        parsed_changeset.last_integrated_local_version =
1,418✔
255
            cur_changeset.get<int64_t>(m_changeset_last_integrated_client_version);
1,418✔
256
        parsed_changeset.data = BinaryData(uncompressed_buffer.data(), uncompressed_buffer.size());
1,418✔
257
        bytes_so_far += parsed_changeset.data.size();
×
258
        ret.changesets.push_back(std::move(parsed_changeset));
×
259
    }
710✔
260
    ret.remaining_changesets = changeset_list.size() - ret.changesets.size();
1,418✔
261

710✔
262
    return ret;
1,418✔
263
}
1,418✔
264

710✔
265
PendingBootstrapStore::PendingBatchStats PendingBootstrapStore::pending_stats()
1,418✔
266
{
1,418✔
267
    auto tr = m_db->start_read();
1,418✔
268
    auto bootstrap_table = tr->get_table(m_table);
1,586✔
269
    if (bootstrap_table->is_empty()) {
1,586✔
270
        return {};
1,586✔
271
    }
1,586✔
272

1,586✔
273
    REALM_ASSERT(bootstrap_table->size() == 1);
710✔
274

1,418✔
275
    auto bootstrap_obj = bootstrap_table->get_object(0);
1,418✔
276
    auto changeset_list = bootstrap_obj.get_linklist(m_changesets);
277

278
    PendingBatchStats stats;
1,534✔
279
    stats.query_version = bootstrap_obj.get<int64_t>(m_query_version);
1,534✔
280
    stats.pending_changesets = changeset_list.size();
1,534✔
281
    changeset_list.for_each([&](Obj& cur_changeset) {
1,534✔
282
        stats.pending_changeset_bytes +=
×
283
            static_cast<size_t>(cur_changeset.get<int64_t>(m_changeset_original_changeset_size));
×
284
        return IteratorControl::AdvanceToNext;
768✔
285
    });
768✔
286

1,534✔
287
    return stats;
768✔
288
}
1,534✔
289

1,534✔
290
void PendingBootstrapStore::pop_front_pending(const TransactionRef& tr, size_t count)
1,534✔
291
{
1,534✔
292
    REALM_ASSERT_3(tr->get_transact_stage(), ==, DB::transact_Writing);
1,398✔
293
    auto bootstrap_table = tr->get_table(m_table);
1,398✔
294
    if (bootstrap_table->is_empty()) {
136✔
295
        return;
280✔
296
    }
144✔
297

144✔
298
    // We should only have one pending bootstrap at a time.
136✔
299
    REALM_ASSERT(bootstrap_table->size() == 1);
768✔
300

1,534✔
301
    auto bootstrap_obj = bootstrap_table->get_object(0);
1,398✔
302
    auto changeset_list = bootstrap_obj.get_linklist(m_changesets);
1,398✔
303
    REALM_ASSERT_3(changeset_list.size(), >=, count);
1,398✔
304
    if (count == changeset_list.size()) {
1,398✔
305
        changeset_list.clear();
136✔
306
    }
136✔
307
    else {
136✔
308
        for (size_t idx = 0; idx < count; ++idx) {
136✔
309
            changeset_list.remove(0);
768✔
310
        }
1,534✔
311
    }
1,534✔
312

313
    if (changeset_list.is_empty()) {
314
        m_logger.debug("Removing pending bootstrap obj for query version %1",
315
                       bootstrap_obj.get<int64_t>(m_query_version));
316
        bootstrap_obj.remove();
317
    }
318
    else {
319
        m_logger.debug("Removing pending bootstrap batch for query version %1. %2 changeset remaining",
320
                       bootstrap_obj.get<int64_t>(m_query_version), changeset_list.size());
321
    }
322

323
    m_has_pending = (bootstrap_table->is_empty() == false);
324
}
325

326
} // 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