• 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

76.13
/src/realm/sync/noinst/server/server.cpp
1
#include <realm/sync/noinst/server/server.hpp>
2

3
#include <realm/binary_data.hpp>
4
#include <realm/impl/simulated_failure.hpp>
5
#include <realm/object_id.hpp>
6
#include <realm/string_data.hpp>
7
#include <realm/sync/changeset.hpp>
8
#include <realm/sync/trigger.hpp>
9
#include <realm/sync/impl/clamped_hex_dump.hpp>
10
#include <realm/sync/impl/clock.hpp>
11
#include <realm/sync/network/http.hpp>
12
#include <realm/sync/network/network_ssl.hpp>
13
#include <realm/sync/network/websocket.hpp>
14
#include <realm/sync/noinst/client_history_impl.hpp>
15
#include <realm/sync/noinst/protocol_codec.hpp>
16
#include <realm/sync/noinst/server/access_control.hpp>
17
#include <realm/sync/noinst/server/server_dir.hpp>
18
#include <realm/sync/noinst/server/server_file_access_cache.hpp>
19
#include <realm/sync/noinst/server/server_impl_base.hpp>
20
#include <realm/sync/transform.hpp>
21
#include <realm/util/base64.hpp>
22
#include <realm/util/bind_ptr.hpp>
23
#include <realm/util/buffer_stream.hpp>
24
#include <realm/util/circular_buffer.hpp>
25
#include <realm/util/compression.hpp>
26
#include <realm/util/file.hpp>
27
#include <realm/util/json_parser.hpp>
28
#include <realm/util/load_file.hpp>
29
#include <realm/util/memory_stream.hpp>
30
#include <realm/util/optional.hpp>
31
#include <realm/util/platform_info.hpp>
32
#include <realm/util/random.hpp>
33
#include <realm/util/safe_int_ops.hpp>
34
#include <realm/util/scope_exit.hpp>
35
#include <realm/util/scratch_allocator.hpp>
36
#include <realm/util/thread.hpp>
37
#include <realm/util/thread_exec_guard.hpp>
38
#include <realm/util/value_reset_guard.hpp>
39
#include <realm/version.hpp>
40

41
#include <algorithm>
42
#include <atomic>
43
#include <cctype>
44
#include <chrono>
45
#include <cmath>
46
#include <condition_variable>
47
#include <cstdint>
48
#include <cstdio>
49
#include <cstring>
50
#include <functional>
51
#include <locale>
52
#include <map>
53
#include <memory>
54
#include <queue>
55
#include <sstream>
56
#include <stdexcept>
57
#include <thread>
58
#include <vector>
59

60
// NOTE: The protocol specification is in `/doc/protocol.md`
61

62

63
// FIXME: Verify that session identifier spoofing cannot be used to get access
64
// to sessions belonging to other network conections in any way.
65
// FIXME: Seems that server must close connection with zero sessions after a
66
// certain timeout.
67

68

69
using namespace realm;
70
using namespace realm::sync;
71
using namespace realm::util;
72

73
// clang-format off
74
using ServerHistory         = _impl::ServerHistory;
75
using ServerProtocol        = _impl::ServerProtocol;
76
using ServerFileAccessCache = _impl::ServerFileAccessCache;
77
using ServerImplBase = _impl::ServerImplBase;
78

79
using IntegratableChangeset = ServerHistory::IntegratableChangeset;
80
using IntegratableChangesetList = ServerHistory::IntegratableChangesetList;
81
using IntegratableChangesets = ServerHistory::IntegratableChangesets;
82
using IntegrationResult = ServerHistory::IntegrationResult;
83
using BootstrapError = ServerHistory::BootstrapError;
84
using ExtendedIntegrationError = ServerHistory::ExtendedIntegrationError;
85
using ClientType = ServerHistory::ClientType;
86
using FileIdentAllocSlot = ServerHistory::FileIdentAllocSlot;
87
using FileIdentAllocSlots = ServerHistory::FileIdentAllocSlots;
88

89
using UploadChangeset = ServerProtocol::UploadChangeset;
90
// clang-format on
91

92

93
using UploadChangesets = std::vector<UploadChangeset>;
94

95
using EventLoopMetricsHandler = network::Service::EventLoopMetricsHandler;
96

97

98
static_assert(std::numeric_limits<session_ident_type>::digits >= 63, "Bad session identifier type");
99
static_assert(std::numeric_limits<file_ident_type>::digits >= 63, "Bad file identifier type");
100
static_assert(std::numeric_limits<version_type>::digits >= 63, "Bad version type");
101
static_assert(std::numeric_limits<timestamp_type>::digits >= 63, "Bad timestamp type");
102

103

104
namespace {
105

106
enum class SchedStatus { done = 0, pending, in_progress };
107

108
// Only used by the Sync Server to support older pbs sync clients (prior to protocol v8)
109
constexpr std::string_view get_old_pbs_websocket_protocol_prefix() noexcept
110
{
×
111
    return "com.mongodb.realm-sync/";
×
112
}
×
113

114
std::string short_token_fmt(const std::string& str, size_t cutoff = 30)
115
{
420✔
116
    if (str.size() > cutoff) {
420✔
117
        return "..." + str.substr(str.size() - cutoff);
×
118
    }
×
119
    else {
420✔
120
        return str;
420✔
121
    }
420✔
122
}
420✔
123

124

125
class HttpListHeaderValueParser {
126
public:
127
    HttpListHeaderValueParser(std::string_view string) noexcept
128
        : m_string{string}
129
    {
1,872✔
130
    }
1,872✔
131
    bool next(std::string_view& elem) noexcept
132
    {
18,720✔
133
        while (m_pos < m_string.size()) {
18,720✔
134
            size_type i = m_pos;
16,848✔
135
            size_type j = m_string.find(',', i);
16,848✔
136
            if (j != std::string_view::npos) {
16,848✔
137
                m_pos = j + 1;
14,976✔
138
            }
14,976✔
139
            else {
1,872✔
140
                j = m_string.size();
1,872✔
141
                m_pos = j;
1,872✔
142
            }
1,872✔
143

8,316✔
144
            // Exclude leading and trailing white space
8,316✔
145
            while (i < j && is_http_lws(m_string[i]))
31,824✔
146
                ++i;
14,976✔
147
            while (j > i && is_http_lws(m_string[j - 1]))
16,848✔
148
                --j;
×
149

8,316✔
150
            if (i != j) {
16,848✔
151
                elem = m_string.substr(i, j - i);
16,848✔
152
                return true;
16,848✔
153
            }
16,848✔
154
        }
16,848✔
155
        return false;
10,188✔
156
    }
18,720✔
157

158
private:
159
    using size_type = std::string_view::size_type;
160
    const std::string_view m_string;
161
    size_type m_pos = 0;
162
    static bool is_http_lws(char ch) noexcept
163
    {
48,672✔
164
        return (ch == '\t' || ch == '\n' || ch == '\r' || ch == ' ');
48,672✔
165
    }
48,672✔
166
};
167

168

169
using SteadyClock = std::conditional<std::chrono::high_resolution_clock::is_steady,
170
                                     std::chrono::high_resolution_clock, std::chrono::steady_clock>::type;
171
using SteadyTimePoint = SteadyClock::time_point;
172

173
SteadyTimePoint steady_clock_now() noexcept
174
{
148,862✔
175
    return SteadyClock::now();
148,862✔
176
}
148,862✔
177

178
milliseconds_type steady_duration(SteadyTimePoint start_time, SteadyTimePoint end_time = steady_clock_now()) noexcept
179
{
37,356✔
180
    auto duration = end_time - start_time;
37,356✔
181
    auto millis_duration = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
37,356✔
182
    return milliseconds_type(millis_duration);
37,356✔
183
}
37,356✔
184

185

186
bool determine_try_again(ProtocolError error_code) noexcept
187
{
94✔
188
    return (error_code == ProtocolError::connection_closed);
94✔
189
}
94✔
190

191

192
class ServerFile;
193
class ServerImpl;
194
class HTTPConnection;
195
class SyncConnection;
196
class Session;
197

198

199
using Formatter = util::ResettableExpandableBufferOutputStream;
200
using OutputBuffer = util::ResettableExpandableBufferOutputStream;
201

202
using ProtocolVersionRange = std::pair<int, int>;
203

204
class MiscBuffers {
205
public:
206
    Formatter formatter;
207
    OutputBuffer download_message;
208

209
    using ProtocolVersionRanges = std::vector<ProtocolVersionRange>;
210
    ProtocolVersionRanges protocol_version_ranges;
211

212
    std::vector<char> compress;
213

214
    MiscBuffers()
215
    {
7,968✔
216
        formatter.imbue(std::locale::classic());
7,968✔
217
        download_message.imbue(std::locale::classic());
7,968✔
218
    }
7,968✔
219
};
220

221

222
struct DownloadCache {
223
    std::unique_ptr<char[]> body;
224
    std::size_t uncompressed_body_size;
225
    std::size_t compressed_body_size;
226
    bool body_is_compressed;
227
    version_type end_version;
228
    DownloadCursor download_progress;
229
    std::uint_fast64_t downloadable_bytes;
230
    std::size_t num_changesets;
231
    std::size_t accum_original_size;
232
    std::size_t accum_compacted_size;
233
};
234

235

236
// An unblocked work unit is comprised of one Work object for each of the files
237
// that contribute work to the work unit, generally one reference file and a
238
// number of partial files.
239
class Work {
240
public:
241
    // In general, primary work is all forms of modifying work, including file
242
    // deletion.
243
    bool has_primary_work = false;
244

245
    // Only for reference files
246
    bool might_produce_new_sync_version = false;
247

248
    bool produced_new_realm_version = false;
249
    bool produced_new_sync_version = false;
250
    bool expired_reference_version = false;
251

252
    // True if, and only if changesets_from_downstream contains at least one
253
    // changeset.
254
    bool have_changesets_from_downstream = false;
255

256
    FileIdentAllocSlots file_ident_alloc_slots;
257
    std::vector<std::unique_ptr<char[]>> changeset_buffers;
258
    IntegratableChangesets changesets_from_downstream;
259

260
    VersionInfo version_info;
261

262
    // Result of integration of changesets from downstream clients
263
    IntegrationResult integration_result;
264

265
    void reset() noexcept
266
    {
37,308✔
267
        has_primary_work = false;
37,308✔
268

19,054✔
269
        might_produce_new_sync_version = false;
37,308✔
270

19,054✔
271
        produced_new_realm_version = false;
37,308✔
272
        produced_new_sync_version = false;
37,308✔
273
        expired_reference_version = false;
37,308✔
274
        have_changesets_from_downstream = false;
37,308✔
275

19,054✔
276
        file_ident_alloc_slots.clear();
37,308✔
277
        changeset_buffers.clear();
37,308✔
278
        changesets_from_downstream.clear();
37,308✔
279

19,054✔
280
        version_info = {};
37,308✔
281
        integration_result = {};
37,308✔
282
    }
37,308✔
283
};
284

285

286
class WorkerState {
287
public:
288
    FileIdentAllocSlots file_ident_alloc_slots;
289
    util::ScratchMemory scratch_memory;
290
    bool use_file_cache = true;
291
    std::unique_ptr<ServerHistory> reference_hist;
292
    DBRef reference_sg;
293
};
294

295

296
// ============================ SessionQueue ============================
297

298
class SessionQueue {
299
public:
300
    void push_back(Session*) noexcept;
301
    Session* pop_front() noexcept;
302
    void clear() noexcept;
303

304
private:
305
    Session* m_back = nullptr;
306
};
307

308

309
// ============================ FileIdentReceiver ============================
310

311
class FileIdentReceiver {
312
public:
313
    virtual void receive_file_ident(SaltedFileIdent) = 0;
314

315
protected:
316
    ~FileIdentReceiver() {}
5,054✔
317
};
318

319

320
// ============================ WorkerBox =============================
321

322
class WorkerBox {
323
public:
324
    using JobType = util::UniqueFunction<void(WorkerState&)>;
325
    void add_work(WorkerState& state, JobType job)
326
    {
×
327
        std::unique_lock<std::mutex> lock(m_mutex);
×
328
        if (m_jobs.size() >= m_queue_limit) {
×
329
            // Once we have many queued jobs, it is better to use this thread to run a new job
×
330
            // than to queue it.
×
331
            run_a_job(lock, state, job);
×
332
        }
×
333
        else {
×
334
            // Create worker threads on demand (if all existing threads are active):
×
335
            if (m_threads.size() < m_max_num_threads && m_active >= m_threads.size()) {
×
336
                m_threads.emplace_back([this]() {
×
337
                    WorkerState state;
×
338
                    state.use_file_cache = false;
×
339
                    JobType the_job;
×
340
                    std::unique_lock<std::mutex> lock(m_mutex);
×
341
                    for (;;) {
×
342
                        while (m_jobs.empty() && !m_finish_up)
×
343
                            m_changes.wait(lock);
×
344
                        if (m_finish_up)
×
345
                            break; // terminate thread
×
346
                        the_job = std::move(m_jobs.back());
×
347
                        m_jobs.pop_back();
×
348
                        run_a_job(lock, state, the_job);
×
349
                        m_changes.notify_all();
×
350
                    }
×
351
                });
×
352
            }
×
353

×
354
            // Submit the job for execution:
×
355
            m_jobs.emplace_back(std::move(job));
×
356
            m_changes.notify_all();
×
357
        }
×
358
    }
×
359

360
    // You should call wait_completion() before trying to destroy a WorkerBox to get proper
361
    // propagation of exceptions.
362
    void wait_completion(WorkerState& state)
363
    {
×
364
        std::unique_lock<std::mutex> lock(m_mutex);
×
365
        while (!m_jobs.empty() || m_active > 0) {
×
366
            if (!m_jobs.empty()) { // if possible, make this thread participate in running m_jobs
×
367
                JobType the_job = std::move(m_jobs.back());
×
368
                m_jobs.pop_back();
×
369
                run_a_job(lock, state, the_job);
×
370
            }
×
371
            else {
×
372
                m_changes.wait(lock);
×
373
            }
×
374
        }
×
375
        if (m_epr) {
×
376
            std::rethrow_exception(m_epr);
×
377
        }
×
378
    }
×
379

380
    WorkerBox(unsigned int num_threads)
381
    {
×
382
        m_queue_limit = num_threads * 10; // fudge factor for job size variation
×
383
        m_max_num_threads = num_threads;
×
384
    }
×
385

386
    ~WorkerBox()
387
    {
×
388
        {
×
389
            std::unique_lock<std::mutex> lock(m_mutex);
×
390
            m_finish_up = true;
×
391
            m_changes.notify_all();
×
392
        }
×
393
        for (auto& e : m_threads)
×
394
            e.join();
×
395
    }
×
396

397
private:
398
    std::mutex m_mutex;
399
    std::condition_variable m_changes;
400
    std::vector<std::thread> m_threads;
401
    std::vector<JobType> m_jobs;
402
    unsigned int m_active = 0;
403
    bool m_finish_up = false;
404
    unsigned int m_queue_limit = 0;
405
    unsigned int m_max_num_threads = 0;
406
    std::exception_ptr m_epr;
407

408
    void run_a_job(std::unique_lock<std::mutex>& lock, WorkerState& state, JobType& job)
409
    {
×
410
        ++m_active;
×
411
        lock.unlock();
×
412
        try {
×
413
            job(state);
×
414
            lock.lock();
×
415
        }
×
416
        catch (...) {
×
417
            lock.lock();
×
418
            if (!m_epr)
×
419
                m_epr = std::current_exception();
×
420
        }
×
421
        --m_active;
×
422
    }
×
423
};
424

425

426
// ============================ ServerFile ============================
427

428
class ServerFile : public util::RefCountBase {
429
public:
430
    util::PrefixLogger logger;
431

432
    // Logger to be used by the worker thread
433
    util::PrefixLogger wlogger;
434

435
    ServerFile(ServerImpl& server, ServerFileAccessCache& cache, const std::string& virt_path, std::string real_path,
436
               bool disable_sync_to_disk);
437
    ~ServerFile() noexcept;
438

439
    void initialize();
440
    void activate();
441

442
    ServerImpl& get_server() noexcept
443
    {
40,256✔
444
        return m_server;
40,256✔
445
    }
40,256✔
446

447
    const std::string& get_real_path() const noexcept
448
    {
×
449
        return m_file.realm_path;
×
450
    }
×
451

452
    const std::string& get_virt_path() const noexcept
453
    {
×
454
        return m_file.virt_path;
×
455
    }
×
456

457
    ServerFileAccessCache::File& access()
458
    {
48,002✔
459
        return m_file.access(); // Throws
48,002✔
460
    }
48,002✔
461

462
    ServerFileAccessCache::File& worker_access()
463
    {
37,280✔
464
        return m_worker_file.access(); // Throws
37,280✔
465
    }
37,280✔
466

467
    version_type get_realm_version() const noexcept
468
    {
×
469
        return m_version_info.realm_version;
×
470
    }
×
471

472
    version_type get_sync_version() const noexcept
473
    {
×
474
        return m_version_info.sync_version.version;
×
475
    }
×
476

477
    SaltedVersion get_salted_sync_version() const noexcept
478
    {
98,842✔
479
        return m_version_info.sync_version;
98,842✔
480
    }
98,842✔
481

482
    DownloadCache& get_download_cache() noexcept;
483

484
    void register_client_access(file_ident_type client_file_ident);
485

486
    using file_ident_request_type = std::int_fast64_t;
487

488
    // Initiate a request for a new client file identifier.
489
    //
490
    // Unless the request is cancelled, the identifier will be delivered to the
491
    // receiver by way of an invocation of
492
    // FileIdentReceiver::receive_file_ident().
493
    //
494
    // FileIdentReceiver::receive_file_ident() is guaranteed to not be called
495
    // until after request_file_ident() has returned (no callback reentrance).
496
    //
497
    // New client file identifiers will be delivered to receivers in the order
498
    // that they were requested.
499
    //
500
    // The returned value is a nonzero integer that can be used to cancel the
501
    // request before the file identifier is delivered using
502
    // cancel_file_ident_request().
503
    auto request_file_ident(FileIdentReceiver&, file_ident_type proxy_file, ClientType) -> file_ident_request_type;
504

505
    // Cancel the specified file identifier request.
506
    //
507
    // It is an error to call this function after the identifier has been
508
    // delivered.
509
    void cancel_file_ident_request(file_ident_request_type) noexcept;
510

511
    void add_unidentified_session(Session*);
512
    void identify_session(Session*, file_ident_type client_file_ident);
513

514
    void remove_unidentified_session(Session*) noexcept;
515
    void remove_identified_session(file_ident_type client_file_ident) noexcept;
516

517
    Session* get_identified_session(file_ident_type client_file_ident) noexcept;
518

519
    bool can_add_changesets_from_downstream() const noexcept;
520
    void add_changesets_from_downstream(file_ident_type client_file_ident, UploadCursor upload_progress,
521
                                        version_type locked_server_version, const UploadChangeset*,
522
                                        std::size_t num_changesets);
523

524
    // bootstrap_client_session calls the function of same name in server_history
525
    // but corrects the upload_progress with information from pending
526
    // integratable changesets. A situation can occur where a client terminates
527
    // a session and starts a new session and re-uploads changesets that are known
528
    // by the ServerFile object but not by the ServerHistory.
529
    BootstrapError bootstrap_client_session(SaltedFileIdent client_file_ident, DownloadCursor download_progress,
530
                                            SaltedVersion server_version, ClientType client_type,
531
                                            UploadCursor& upload_progress, version_type& locked_server_version,
532
                                            Logger&);
533

534
    // NOTE: This function is executed by the worker thread
535
    void worker_process_work_unit(WorkerState&);
536

537
    void recognize_external_change();
538

539
private:
540
    ServerImpl& m_server;
541
    ServerFileAccessCache::Slot m_file;
542

543
    // In general, `m_version_info` refers to the last snapshot of the Realm
544
    // file that is supposed to be visible to remote peers engaging in regular
545
    // Realm file synchronization.
546
    VersionInfo m_version_info;
547

548
    file_ident_request_type m_last_file_ident_request = 0;
549

550
    // The set of sessions whose client file identifier is not yet known, i.e.,
551
    // those for which an IDENT message has not yet been received,
552
    std::set<Session*> m_unidentified_sessions;
553

554
    // A map of the sessions whose client file identifier is known, i.e, those
555
    // for which an IDENT message has been received.
556
    std::map<file_ident_type, Session*> m_identified_sessions;
557

558
    // Used when a file used as partial view wants to allocate a client file
559
    // identifier from the reference Realm.
560
    file_ident_request_type m_file_ident_request = 0;
561

562
    struct FileIdentRequestInfo {
563
        FileIdentReceiver* receiver;
564
        file_ident_type proxy_file;
565
        ClientType client_type;
566
    };
567

568
    // When nonempty, it counts towards outstanding blocked work (see
569
    // `m_has_blocked_work`).
570
    std::map<file_ident_request_type, FileIdentRequestInfo> m_file_ident_requests;
571

572
    // Changesets received from the downstream clients, and waiting to be
573
    // integrated, as well as information about the clients progress in terms of
574
    // integrating changesets received from the server. When nonempty, it counts
575
    // towards outstanding blocked work (see `m_has_blocked_work`).
576
    //
577
    // At any given time, the set of changesets from a particular client-side
578
    // file may be comprised of changesets received via distinct sessions.
579
    //
580
    // See also `m_num_changesets_from_downstream`.
581
    IntegratableChangesets m_changesets_from_downstream;
582

583
    // Keeps track of the number of changesets in `m_changesets_from_downstream`.
584
    //
585
    // Its purpose is also to initialize
586
    // `Work::have_changesets_from_downstream`.
587
    std::size_t m_num_changesets_from_downstream = 0;
588

589
    // The total size, in bytes, of the changesets that were received from
590
    // clients, are targeting this file, and are currently part of the blocked
591
    // work unit.
592
    //
593
    // Together with `m_unblocked_changesets_from_downstream_byte_size`, its
594
    // purpose is to allow the server to keep track of the accumulated size of
595
    // changesets being processed, or waiting to be processed (metric
596
    // `upload.pending.bytes`) (see
597
    // ServerImpl::inc_byte_size_for_pending_downstream_changesets()).
598
    //
599
    // Its purpose is also to enable the "very poor man's" backpressure solution
600
    // (see can_add_changesets_from_downstream()).
601
    std::size_t m_blocked_changesets_from_downstream_byte_size = 0;
602

603
    // Same as `m_blocked_changesets_from_downstream_byte_size` but for the
604
    // currently unblocked work unit.
605
    std::size_t m_unblocked_changesets_from_downstream_byte_size = 0;
606

607
    // When nonempty, it counts towards outstanding blocked work (see
608
    // `m_has_blocked_work`).
609
    std::vector<std::string> m_permission_changes;
610

611
    // True iff this file, or any of its associated partial files (when
612
    // applicable), has a nonzero amount of outstanding work that is currently
613
    // held back from being passed to the worker thread because a previously
614
    // accumulated chunk of work related to this file is currently in progress.
615
    bool m_has_blocked_work = false;
616

617
    // A file, that is not a partial file, is considered *exposed to the worker
618
    // thread* from the point in time where it is submitted to the worker
619
    // (Worker::enqueue()) and up until the point in time where
620
    // group_postprocess_stage_1() starts to execute. A partial file is
621
    // considered *exposed to the worker thread* precisely when the associated
622
    // reference file is exposed to the worker thread, but only if it was in
623
    // `m_reference_file->m_work.partial_files` at the point in time where the
624
    // reference file was passed to the worker.
625
    //
626
    // While this file is exposed to the worker thread, all members of `m_work`
627
    // other than `changesets_from_downstream` may be accessed and modified by
628
    // the worker thread only.
629
    //
630
    // While this file is exposed to the worker thread,
631
    // `m_work.changesets_from_downstream` may be accessed by all threads, but
632
    // must not be modified by any thread. This special status of
633
    // `m_work.changesets_from_downstream` is required to allow
634
    // ServerFile::bootstrap_client_session() to read from it at any time.
635
    Work m_work;
636

637
    // For reference files, set to true when work is unblocked, and reset back
638
    // to false when the work finalization process completes
639
    // (group_postprocess_stage_3()). Always zero for partial files.
640
    bool m_has_work_in_progress = 0;
641

642
    // This one must only be accessed by the worker thread.
643
    //
644
    // More specifically, `m_worker_file.access()` must only be called by the
645
    // worker thread, and if it was ever called, it must be closed by the worker
646
    // thread before the ServerFile object is destroyed, if destruction happens
647
    // before the destruction of the server object itself.
648
    ServerFileAccessCache::Slot m_worker_file;
649

650
    std::vector<std::int_fast64_t> m_deleting_connections;
651

652
    DownloadCache m_download_cache;
653

654
    void on_changesets_from_downstream_added(std::size_t num_changesets, std::size_t num_bytes);
655
    void on_work_added();
656
    void group_unblock_work();
657
    void unblock_work();
658

659
    /// Resume history scanning in all sessions bound to this file. To be called
660
    /// after a successfull integration of a changeset.
661
    void resume_download() noexcept;
662

663
    // NOTE: These functions are executed by the worker thread
664
    void worker_allocate_file_identifiers();
665
    bool worker_integrate_changes_from_downstream(WorkerState&);
666
    ServerHistory& get_client_file_history(WorkerState& state, std::unique_ptr<ServerHistory>& hist_ptr,
667
                                           DBRef& sg_ptr);
668
    ServerHistory& get_reference_file_history(WorkerState& state);
669
    void group_postprocess_stage_1();
670
    void group_postprocess_stage_2();
671
    void group_postprocess_stage_3();
672
    void group_finalize_work_stage_1();
673
    void group_finalize_work_stage_2();
674
    void finalize_work_stage_1();
675
    void finalize_work_stage_2();
676
};
677

678

679
inline DownloadCache& ServerFile::get_download_cache() noexcept
680
{
38,130✔
681
    return m_download_cache;
38,130✔
682
}
38,130✔
683

684
inline void ServerFile::group_finalize_work_stage_1()
685
{
36,932✔
686
    finalize_work_stage_1(); // Throws
36,932✔
687
}
36,932✔
688

689
inline void ServerFile::group_finalize_work_stage_2()
690
{
36,932✔
691
    finalize_work_stage_2(); // Throws
36,932✔
692
}
36,932✔
693

694

695
// ============================ Worker ============================
696

697
// All write transaction on server-side Realm files performed on behalf of the
698
// server, must be performed by the worker thread, not the network event loop
699
// thread. This is to ensure that the network event loop thread never gets
700
// blocked waiting for the worker thread to end a long running write
701
// transaction.
702
//
703
// FIXME: Currently, the event loop thread does perform a number of write
704
// transactions, but only on subtier nodes of a star topology server cluster.
705
class Worker : public ServerHistory::Context {
706
public:
707
    util::PrefixLogger logger;
708

709
    explicit Worker(ServerImpl&);
710

711
    ServerFileAccessCache& get_file_access_cache() noexcept;
712

713
    void enqueue(ServerFile*);
714

715
    // Overriding members of ServerHistory::Context
716
    std::mt19937_64& server_history_get_random() noexcept override final;
717
    sync::Transformer& get_transformer() override final;
718
    util::Buffer<char>& get_transform_buffer() override final;
719

720
private:
721
    ServerImpl& m_server;
722
    std::mt19937_64 m_random;
723
    const std::unique_ptr<Transformer> m_transformer;
724
    util::Buffer<char> m_transform_buffer;
725
    ServerFileAccessCache m_file_access_cache;
726

727
    util::Mutex m_mutex;
728
    util::CondVar m_cond; // Protected by `m_mutex`
729

730
    bool m_stop = false; // Protected by `m_mutex`
731

732
    util::CircularBuffer<ServerFile*> m_queue; // Protected by `m_mutex`
733

734
    WorkerState m_state;
735

736
    void run();
737
    void stop() noexcept;
738

739
    friend class util::ThreadExecGuardWithParent<Worker, ServerImpl>;
740
};
1,090✔
741

1,090✔
742

1,090✔
743
inline ServerFileAccessCache& Worker::get_file_access_cache() noexcept
744
{
745
    return m_file_access_cache;
746
}
747

748

749
// ============================ ServerImpl ============================
750

751
class ServerImpl : public ServerImplBase, public ServerHistory::Context {
752
public:
753
    std::uint_fast64_t errors_seen = 0;
754

755
    std::atomic<milliseconds_type> m_par_time;
756
    std::atomic<milliseconds_type> m_seq_time;
757

758
    util::Mutex last_client_accesses_mutex;
759

760
    const std::shared_ptr<util::Logger> logger_ptr;
77,824✔
761
    util::Logger& logger;
77,824✔
762

77,824✔
763
    network::Service& get_service() noexcept
764
    {
765
        return m_service;
×
766
    }
×
767

×
768
    const network::Service& get_service() const noexcept
769
    {
770
        return m_service;
61,068✔
771
    }
61,068✔
772

61,068✔
773
    std::mt19937_64& get_random() noexcept
774
    {
775
        return m_random;
118,824✔
776
    }
118,824✔
777

118,824✔
778
    const Server::Config& get_config() const noexcept
779
    {
780
        return m_config;
44,724✔
781
    }
44,724✔
782

44,724✔
783
    std::size_t get_max_upload_backlog() const noexcept
784
    {
785
        return m_max_upload_backlog;
5,054✔
786
    }
5,054✔
787

5,054✔
788
    const std::string& get_root_dir() const noexcept
789
    {
790
        return m_root_dir;
48✔
791
    }
48✔
792

48✔
793
    network::ssl::Context& get_ssl_context() noexcept
794
    {
795
        return *m_ssl_context;
×
796
    }
×
797

×
798
    const AccessControl& get_access_control() const noexcept
799
    {
800
        return m_access_control;
1,872✔
801
    }
1,872✔
802

1,872✔
803
    ProtocolVersionRange get_protocol_version_range() const noexcept
804
    {
805
        return m_protocol_version_range;
128,568✔
806
    }
128,568✔
807

128,568✔
808
    ServerProtocol& get_server_protocol() noexcept
809
    {
810
        return m_server_protocol;
4,282✔
811
    }
4,282✔
812

4,282✔
813
    compression::CompressMemoryArena& get_compress_memory_arena() noexcept
814
    {
815
        return m_compress_memory_arena;
44,280✔
816
    }
44,280✔
817

44,280✔
818
    MiscBuffers& get_misc_buffers() noexcept
819
    {
820
        return m_misc_buffers;
×
821
    }
×
822

×
823
    int_fast64_t get_current_server_session_ident() const noexcept
824
    {
825
        return m_current_server_session_ident;
×
826
    }
×
827

×
828
    util::ScratchMemory& get_scratch_memory() noexcept
829
    {
830
        return m_scratch_memory;
39,476✔
831
    }
39,476✔
832

39,476✔
833
    Worker& get_worker() noexcept
834
    {
835
        return m_worker;
×
836
    }
×
837

×
838
    void get_workunit_timers(milliseconds_type& parallel_section, milliseconds_type& sequential_section)
×
839
    {
840
        parallel_section = m_par_time;
841
        sequential_section = m_seq_time;
842
    }
843

844
    ServerImpl(const std::string& root_dir, util::Optional<sync::PKey>, Server::Config);
845
    ~ServerImpl() noexcept;
846

672✔
847
    void start();
672✔
848

672✔
849
    void start(std::string listen_address, std::string listen_port, bool reuse_address)
672✔
850
    {
338✔
851
        m_config.listen_address = listen_address;
672✔
852
        m_config.listen_port = listen_port;
672✔
853
        m_config.reuse_address = reuse_address;
854

855
        start(); // Throws
7,972✔
856
    }
7,972✔
857

7,972✔
858
    network::Endpoint listen_endpoint() const
859
    {
860
        return m_acceptor.local_endpoint();
861
    }
862

863
    void run();
864
    void stop() noexcept;
865

866
    void remove_http_connection(std::int_fast64_t conn_id) noexcept;
867

868
    void add_sync_connection(int_fast64_t connection_id, std::unique_ptr<SyncConnection>&& sync_conn);
×
869
    void remove_sync_connection(int_fast64_t connection_id);
×
870

×
871
    size_t get_number_of_http_connections()
872
    {
873
        return m_http_connections.size();
×
874
    }
×
875

×
876
    size_t get_number_of_sync_connections()
877
    {
878
        return m_sync_connections.size();
39,180✔
879
    }
39,180✔
880

39,180✔
881
    bool is_sync_stopped()
882
    {
883
        return m_sync_stopped;
×
884
    }
×
885

×
886
    const std::set<std::string>& get_realm_names() const noexcept
887
    {
888
        return m_realm_names;
889
    }
5,026✔
890

5,026✔
891
    // virt_path must be valid when get_or_create_file() is called.
5,026✔
892
    util::bind_ptr<ServerFile> get_or_create_file(const std::string& virt_path)
4,374✔
893
    {
438✔
894
        util::bind_ptr<ServerFile> file = get_file(virt_path);
1,090✔
895
        if (REALM_LIKELY(file))
1,090✔
896
            return file;
1,090✔
897

438✔
898
        _impl::VirtualPathComponents virt_path_components =
1,090✔
899
            _impl::parse_virtual_path(m_root_dir, virt_path); // Throws
1,090✔
900
        REALM_ASSERT(virt_path_components.is_valid);
1,090✔
901

1,090✔
902
        _impl::make_dirs(m_root_dir, virt_path); // Throws
1,090✔
903
        m_realm_names.insert(virt_path);         // Throws
1,090✔
904
        {
1,090✔
905
            bool disable_sync_to_disk = m_config.disable_sync_to_disk;
438✔
906
            file.reset(new ServerFile(*this, m_file_access_cache, virt_path, virt_path_components.real_realm_path,
1,090✔
907
                                      disable_sync_to_disk)); // Throws
1,090✔
908
        }
1,090✔
909

1,090✔
910
        file->initialize();
1,090✔
911
        m_files[virt_path] = file; // Throws
912
        file->activate();          // Throws
913
        return file;
×
914
    }
×
915

×
916
    std::unique_ptr<ServerHistory> make_history_for_path()
917
    {
918
        return std::make_unique<ServerHistory>(*this);
5,026✔
919
    }
5,026✔
920

5,026✔
921
    util::bind_ptr<ServerFile> get_file(const std::string& virt_path) noexcept
4,374✔
922
    {
1,090✔
923
        auto i = m_files.find(virt_path);
1,090✔
924
        if (REALM_LIKELY(i != m_files.end()))
925
            return i->second;
926
        return {};
927
    }
928

×
929
    // Returns the number of seconds since the Epoch of
×
930
    // std::chrono::system_clock.
×
931
    std::chrono::system_clock::time_point token_expiration_clock_now() const noexcept
×
932
    {
×
933
        if (REALM_UNLIKELY(m_config.token_expiration_clock))
934
            return m_config.token_expiration_clock->now();
935
        return std::chrono::system_clock::now();
936
    }
937

938
    void set_connection_reaper_timeout(milliseconds_type);
939

940
    void close_connections();
941
    bool map_virtual_to_real_path(const std::string& virt_path, std::string& real_path);
942

943
    void recognize_external_change(const std::string& virt_path);
944

945
    void stop_sync_and_wait_for_backup_completion(util::UniqueFunction<void(bool did_backup)> completion_handler,
946
                                                  milliseconds_type timeout);
947

948
    // Server global outputbuffers that can be reused.
949
    // The server is single threaded, so there are no
950
    // synchronization issues.
951
    // output_buffers_count is equal to the
952
    // maximum number of buffers needed at any point.
953
    static constexpr int output_buffers_count = 1;
×
954
    OutputBuffer output_buffers[output_buffers_count];
×
955

×
956
    bool is_load_balancing_allowed() const
957
    {
958
        return m_allow_load_balancing;
959
    }
960

961
    // inc_byte_size_for_pending_downstream_changesets() must be called by
962
    // ServerFile objects when changesets from downstream clients have been
963
    // received.
964
    //
965
    // dec_byte_size_for_pending_downstream_changesets() must be called by
966
    // ServerFile objects when changesets from downstream clients have been
967
    // processed or discarded.
968
    //
969
    // ServerImpl uses this information to keep a running tally (metric
970
    // `upload.pending.bytes`) of the total byte size of pending changesets from
971
    // downstream clients.
972
    //
973
    // These functions must be called on the network thread.
974
    void inc_byte_size_for_pending_downstream_changesets(std::size_t byte_size);
975
    void dec_byte_size_for_pending_downstream_changesets(std::size_t byte_size);
976

977
    // Overriding member functions in _impl::ServerHistory::Context
978
    std::mt19937_64& server_history_get_random() noexcept override final;
979
    Transformer& get_transformer() noexcept override final;
980
    util::Buffer<char>& get_transform_buffer() noexcept override final;
981

982
private:
983
    Server::Config m_config;
984
    network::Service m_service;
985
    std::mt19937_64 m_random;
986
    const std::size_t m_max_upload_backlog;
987
    const std::string m_root_dir;
988
    const AccessControl m_access_control;
989
    const ProtocolVersionRange m_protocol_version_range;
990

991
    // The reserved files will be closed in situations where the server
992
    // runs out of file descriptors.
993
    std::unique_ptr<File> m_reserved_files[5];
994

995
    // The set of all Realm files known to this server, represented by their
996
    // virtual path.
997
    //
998
    // INVARIANT: If a Realm file is in the servers directory (i.e., it would be
999
    // reported by an invocation of _impl::get_realm_names()), then the
1000
    // corresponding virtual path is in `m_realm_names`, assuming no external
1001
    // file-system level intervention.
1002
    std::set<std::string> m_realm_names;
1003

1004
    std::unique_ptr<network::ssl::Context> m_ssl_context;
1005
    ServerFileAccessCache m_file_access_cache;
1006
    Worker m_worker;
1007
    std::map<std::string, util::bind_ptr<ServerFile>> m_files; // Key is virtual path
1008
    network::Acceptor m_acceptor;
1009
    std::int_fast64_t m_next_conn_id = 0;
1010
    std::unique_ptr<HTTPConnection> m_next_http_conn;
1011
    network::Endpoint m_next_http_conn_endpoint;
1012
    std::map<std::int_fast64_t, std::unique_ptr<HTTPConnection>> m_http_connections;
1013
    std::map<std::int_fast64_t, std::unique_ptr<SyncConnection>> m_sync_connections;
1014
    ServerProtocol m_server_protocol;
1015
    compression::CompressMemoryArena m_compress_memory_arena;
1016
    MiscBuffers m_misc_buffers;
1017
    std::unique_ptr<Transformer> m_transformer;
1018
    util::Buffer<char> m_transform_buffer;
1019
    int_fast64_t m_current_server_session_ident;
1020
    Optional<network::DeadlineTimer> m_connection_reaper_timer;
1021
    bool m_allow_load_balancing = false;
1022

1023
    util::Mutex m_mutex;
1024

1025
    bool m_stopped = false; // Protected by `m_mutex`
1026

1027
    // m_sync_stopped is used by stop_sync_and_wait_for_backup_completion().
1028
    // When m_sync_stopped is true, the server does not perform any sync.
1029
    bool m_sync_stopped = false;
1030

1031
    std::atomic<bool> m_running{false}; // Debugging facility
1032

1033
    std::size_t m_pending_changesets_from_downstream_byte_size = 0;
1034

1035
    util::CondVar m_wait_or_service_stopped_cond; // Protected by `m_mutex`
1036

1037
    util::ScratchMemory m_scratch_memory;
1038

1039
    void listen();
1040
    void initiate_accept();
7,968✔
1041
    void handle_accept(std::error_code);
7,968✔
1042

7,968✔
1043
    void reap_connections();
×
1044
    void initiate_connection_reaper_timer(milliseconds_type timeout);
×
1045
    void do_close_connections();
1046

1047
    static std::size_t determine_max_upload_backlog(Server::Config& config) noexcept
7,968✔
1048
    {
7,968✔
1049
        if (config.max_upload_backlog == 0)
7,968✔
1050
            return 4294967295; // 4GiB - 1 (largest allowable number on a 32-bit platform)
7,968✔
1051
        return config.max_upload_backlog;
7,968✔
1052
    }
7,968✔
1053

7,968!
1054
    static ProtocolVersionRange determine_protocol_version_range(Server::Config& config)
×
1055
    {
×
1056
        const int actual_min = ServerImplBase::get_oldest_supported_protocol_version();
×
1057
        const int actual_max = get_current_protocol_version();
×
1058
        static_assert(actual_min <= actual_max, "");
7,968✔
1059
        int min = actual_min;
7,968✔
1060
        int max = actual_max;
1061
        if (config.max_protocol_version != 0 && config.max_protocol_version < max) {
1062
            if (config.max_protocol_version < min)
1063
                throw Server::NoSupportedProtocolVersions();
1064
            max = config.max_protocol_version;
1065
        }
1066
        return {min, max};
1067
    }
1068

1069
    void do_recognize_external_change(const std::string& virt_path);
1070

1071
    void do_stop_sync_and_wait_for_backup_completion(util::UniqueFunction<void(bool did_complete)> completion_handler,
1072
                                                     milliseconds_type timeout);
1073
};
1074

1075
// ============================ SyncConnection ============================
1076

1077
class SyncConnection : public websocket::Config {
1078
public:
1079
    const std::shared_ptr<util::Logger> logger_ptr;
1080
    util::Logger& logger;
1081

1082
    // Clients with sync protocol version 8 or greater support pbs->flx migration
1083
    static constexpr int PBS_FLX_MIGRATION_PROTOCOL_VERSION = 8;
1084
    // Clients with sync protocol version less than 10 do not support log messages
1085
    static constexpr int SERVER_LOG_PROTOCOL_VERSION = 10;
1086

1087
    SyncConnection(ServerImpl& serv, std::int_fast64_t id, std::unique_ptr<network::Socket>&& socket,
1088
                   std::unique_ptr<network::ssl::Stream>&& ssl_stream,
1089
                   std::unique_ptr<network::ReadAheadBuffer>&& read_ahead_buffer, int client_protocol_version,
1090
                   std::string client_user_agent, std::string remote_endpoint, std::string appservices_request_id)
1091
        : logger_ptr{std::make_shared<util::PrefixLogger>(make_logger_prefix(id), serv.logger_ptr)} // Throws
1092
        , logger{*logger_ptr}
1093
        , m_server{serv}
1094
        , m_id{id}
1095
        , m_socket{std::move(socket)}
1,870✔
1096
        , m_ssl_stream{std::move(ssl_stream)}
924✔
1097
        , m_read_ahead_buffer{std::move(read_ahead_buffer)}
924✔
1098
        , m_websocket{*this}
1,870✔
1099
        , m_client_protocol_version{client_protocol_version}
924✔
1100
        , m_client_user_agent{std::move(client_user_agent)}
1,870✔
1101
        , m_remote_endpoint{std::move(remote_endpoint)}
91,144✔
1102
        , m_appservices_request_id{std::move(appservices_request_id)}
91,144✔
1103
    {
×
1104
        // Make the output buffer stream throw std::bad_alloc if it fails to
91,144✔
1105
        // expand the buffer
39,382✔
1106
        m_output_buffer.exceptions(std::ios_base::badbit | std::ios_base::failbit);
91,144✔
1107

1,870✔
1108
        network::Service& service = m_server.get_service();
1,870✔
1109
        auto handler = [this](Status status) {
1110
            if (!status.is_ok())
1111
                return;
1112
            if (!m_is_sending)
1113
                send_next_message(); // Throws
107,864✔
1114
        };
107,864✔
1115
        m_send_trigger = std::make_unique<Trigger<network::Service>>(&service, std::move(handler)); // Throws
107,864✔
1116
    }
1117

1118
    ~SyncConnection() noexcept;
128,566✔
1119

128,566✔
1120
    ServerImpl& get_server() noexcept
128,566✔
1121
    {
1122
        return m_server;
1123
    }
99,030✔
1124

99,030✔
1125
    ServerProtocol& get_server_protocol() noexcept
99,030✔
1126
    {
1127
        return m_server.get_server_protocol();
1128
    }
5,026✔
1129

5,026✔
1130
    int get_client_protocol_version()
5,026✔
1131
    {
1132
        return m_client_protocol_version;
1133
    }
5,026✔
1134

5,026✔
1135
    const std::string& get_client_user_agent() const noexcept
5,026✔
1136
    {
1137
        return m_client_user_agent;
1138
    }
1,870✔
1139

1,870✔
1140
    const std::string& get_remote_endpoint() const noexcept
1,870✔
1141
    {
1142
        return m_remote_endpoint;
1143
    }
59,978✔
1144

59,978✔
1145
    const std::shared_ptr<util::Logger>& websocket_get_logger() noexcept final
59,978✔
1146
    {
1147
        return logger_ptr;
1148
    }
69,046✔
1149

69,046✔
1150
    std::mt19937_64& websocket_get_random() noexcept final override
69,046✔
1151
    {
244✔
1152
        return m_server.get_random();
440✔
1153
    }
440✔
1154

440✔
1155
    bool websocket_binary_message_received(const char* data, size_t size) final override
35,396✔
1156
    {
35,396✔
1157
        using sf = _impl::SimulatedFailure;
35,396✔
1158
        if (sf::check_trigger(sf::sync_server__read_head)) {
35,396✔
1159
            // Suicide
68,606✔
1160
            read_error(sf::sync_server__read_head);
68,582✔
1161
            return false;
68,582✔
1162
        }
68,582✔
1163
        // After a connection level error has occurred, all incoming messages
68,606✔
1164
        // will be ignored. By continuing to read until end of input, the server
68,606✔
1165
        // is able to know when the client closes the connection, which in
1166
        // general means that is has received the ERROR message.
1167
        if (REALM_LIKELY(!m_is_closing)) {
×
1168
            m_last_activity_at = steady_clock_now();
×
1169
            handle_message_received(data, size);
×
1170
        }
×
1171
        return true;
×
1172
    }
×
1173

×
1174
    bool websocket_ping_message_received(const char* data, size_t size) final override
1175
    {
1176
        if (REALM_LIKELY(!m_is_closing)) {
59,982✔
1177
            m_last_activity_at = steady_clock_now();
59,982✔
1178
            handle_ping_received(data, size);
70✔
1179
        }
70✔
1180
        return true;
59,912✔
1181
    }
59,912✔
1182

59,912✔
1183
    void async_write(const char* data, size_t size, websocket::WriteCompletionHandler handler) final override
59,982✔
1184
    {
1185
        if (m_ssl_stream) {
1186
            m_ssl_stream->async_write(data, size, std::move(handler)); // Throws
208,524✔
1187
        }
208,524✔
1188
        else {
170✔
1189
            m_socket->async_write(data, size, std::move(handler)); // Throws
170✔
1190
        }
208,354✔
1191
    }
208,354✔
1192

208,354✔
1193
    void async_read(char* buffer, size_t size, websocket::ReadCompletionHandler handler) final override
208,524✔
1194
    {
1195
        if (m_ssl_stream) {
1196
            m_ssl_stream->async_read(buffer, size, *m_read_ahead_buffer, std::move(handler)); // Throws
1197
        }
×
1198
        else {
×
1199
            m_socket->async_read(buffer, size, *m_read_ahead_buffer, std::move(handler)); // Throws
×
1200
        }
×
1201
    }
×
1202

×
1203
    void async_read_until(char* buffer, size_t size, char delim,
×
1204
                          websocket::ReadCompletionHandler handler) final override
×
1205
    {
×
1206
        if (m_ssl_stream) {
×
1207
            m_ssl_stream->async_read_until(buffer, size, delim, *m_read_ahead_buffer,
1208
                                           std::move(handler)); // Throws
1209
        }
634✔
1210
        else {
634✔
1211
            m_socket->async_read_until(buffer, size, delim, *m_read_ahead_buffer,
634✔
1212
                                       std::move(handler)); // Throws
1213
        }
1214
    }
×
1215

×
1216
    void websocket_read_error_handler(std::error_code ec) final override
×
1217
    {
1218
        read_error(ec);
1219
    }
1220

×
1221
    void websocket_write_error_handler(std::error_code ec) final override
1222
    {
×
1223
        write_error(ec);
×
1224
    }
1225

1226
    void websocket_handshake_error_handler(std::error_code ec, const HTTPHeaders*,
×
1227
                                           const std::string_view*) final override
×
1228
    {
×
1229
        // WebSocket class has already logged a message for this error
×
1230
        close_due_to_error(ec); // Throws
1231
    }
1232

×
1233
    void websocket_protocol_error_handler(std::error_code ec) final override
1234
    {
×
1235
        logger.error("WebSocket protocol error (%1): %2", ec, ec.message()); // Throws
×
1236
        close_due_to_error(ec);                                              // Throws
1237
    }
1238

×
1239
    void websocket_handshake_completion_handler(const HTTPHeaders&) final override
×
1240
    {
×
1241
        // This is not called since we handle HTTP request in handle_request_for_sync()
1242
        REALM_TERMINATE("websocket_handshake_completion_handler should not have been called");
1243
    }
×
1244

×
1245
    int_fast64_t get_id() const noexcept
×
1246
    {
1247
        return m_id;
1248
    }
1249

1250
    network::Socket& get_socket() noexcept
1251
    {
1252
        return *m_socket;
1253
    }
1254

1255
    void initiate();
1256

1257
    // Commits suicide
1258
    template <class... Params>
1259
    void terminate(Logger::Level, const char* log_message, Params... log_params);
1260

1261
    // Commits suicide
59,982✔
1262
    void terminate_if_dead(SteadyTimePoint now);
59,982✔
1263

59,982✔
1264
    void enlist_to_send(Session*) noexcept;
59,982✔
1265

1266
    // Sessions should get the output_buffer and insert a message, after which
1267
    // they call initiate_write_output_buffer().
59,820✔
1268
    OutputBuffer& get_output_buffer()
1269
    {
1270
        m_output_buffer.reset();
1271
        return m_output_buffer;
1272
    }
1273

1274
    // More advanced memory strategies can be implemented if needed.
1275
    void release_output_buffer() {}
1276

1277
    // When this function is called, the connection will initiate a write with
1278
    // its output_buffer. Sessions use this method.
1279
    void initiate_write_output_buffer();
1280

1281
    void initiate_pong_output_buffer();
1282

1283
    void handle_protocol_error(Status status);
1284

1285
    void receive_bind_message(session_ident_type, std::string path, std::string signed_user_token,
1286
                              bool need_client_file_ident, bool is_subserver);
1287

1288
    void receive_ident_message(session_ident_type, file_ident_type client_file_ident,
1289
                               salt_type client_file_ident_salt, version_type scan_server_version,
1290
                               version_type scan_client_version, version_type latest_server_version,
1291
                               salt_type latest_server_version_salt);
1292

1293
    void receive_upload_message(session_ident_type, version_type progress_client_version,
1294
                                version_type progress_server_version, version_type locked_server_version,
1295
                                const UploadChangesets&);
1296

1297
    void receive_mark_message(session_ident_type, request_ident_type);
1298

1299
    void receive_unbind_message(session_ident_type);
1300

1301
    void receive_ping(milliseconds_type timestamp, milliseconds_type rtt);
1302

1303
    void receive_error_message(session_ident_type, int error_code, std::string_view error_body);
1304

1305
    void protocol_error(ProtocolError, Session* = nullptr);
1306

1307
    void initiate_soft_close();
1308

1309
    void discard_session(session_ident_type) noexcept;
1310

1311
    void send_log_message(util::Logger::Level level, const std::string&& message, session_ident_type sess_ident = 0,
1312
                          std::optional<std::string> co_id = std::nullopt);
1313

1314
private:
1315
    ServerImpl& m_server;
1316
    const int_fast64_t m_id;
1317
    std::unique_ptr<network::Socket> m_socket;
1318
    std::unique_ptr<network::ssl::Stream> m_ssl_stream;
1319
    std::unique_ptr<network::ReadAheadBuffer> m_read_ahead_buffer;
1320

1321
    websocket::Socket m_websocket;
1322
    std::unique_ptr<char[]> m_input_body_buffer;
1323
    OutputBuffer m_output_buffer;
1324
    std::map<session_ident_type, std::unique_ptr<Session>> m_sessions;
1325

1326
    // The protocol version in use by the connected client.
1327
    const int m_client_protocol_version;
1328

1329
    // The user agent description passed by the client.
1330
    const std::string m_client_user_agent;
1331

1332
    const std::string m_remote_endpoint;
1333

1334
    const std::string m_appservices_request_id;
1335

1336
    // A queue of sessions that have enlisted for an opportunity to send a
1337
    // message. Sessions will be served in the order that they enlist. A session
1338
    // can only occur once in this queue (linked list). If the queue is not
1339
    // empty, and no message is currently being written to the socket, the first
1340
    // session is taken out of the queue, and then granted an opportunity to
1341
    // send a message.
1342
    //
1343
    // Sessions will never be destroyed while in this queue. This is ensured
1344
    // because the connection owns the sessions that are associated with it, and
1345
    // the connection only removes a session from m_sessions at points in time
1346
    // where that session is guaranteed to not be in m_sessions_enlisted_to_send
1347
    // (Connection::send_next_message() and Connection::~Connection()).
1348
    SessionQueue m_sessions_enlisted_to_send;
1349

1350
    Session* m_receiving_session = nullptr;
1351

1352
    bool m_is_sending = false;
1353
    bool m_is_closing = false;
1354

1355
    bool m_send_pong = false;
1356
    bool m_sending_pong = false;
1357

1358
    std::unique_ptr<Trigger<network::Service>> m_send_trigger;
1359

1360
    milliseconds_type m_last_ping_timestamp = 0;
1361

1362
    // If `m_is_closing` is true, this is the time at which `m_is_closing` was
1363
    // set to true (initiation of soft close). Otherwise, if no messages have
1364
    // been received from the client, this is the time at which the connection
1365
    // object was initiated (completion of WebSocket handshake). Otherwise this
1366
    // is the time at which the last message was received from the client.
1367
    SteadyTimePoint m_last_activity_at;
1368

1369
    // These are initialized by do_initiate_soft_close().
1370
    //
1371
    // With recent versions of the protocol (when the version is greater than,
1372
    // or equal to 23), `m_error_session_ident` is always zero.
1373
    ProtocolError m_error_code = {};
1374
    session_ident_type m_error_session_ident = 0;
1375

1376
    struct LogMessage {
1377
        session_ident_type sess_ident;
1378
        util::Logger::Level level;
1379
        std::string message;
1,870✔
1380
        std::optional<std::string> co_id;
1,870✔
1381
    };
1,870✔
1382

1,870✔
1383
    std::mutex m_log_mutex;
1,870✔
1384
    std::queue<LogMessage> m_log_messages;
1,870✔
1385

1386
    static std::string make_logger_prefix(int_fast64_t id)
1387
    {
1388
        std::ostringstream out;
1389
        out.imbue(std::locale::classic());
1390
        out << "Sync Connection[" << id << "]: "; // Throws
1391
        return out.str();                         // Throws
1392
    }
1393

1394
    // The return value of handle_message_received() designates whether
1395
    // message processing should continue. If the connection object is
1396
    // destroyed during execution of handle_message_received(), the return
1397
    // value must be false.
1398
    void handle_message_received(const char* data, size_t size);
1399

1400
    void handle_ping_received(const char* data, size_t size);
1401

1402
    void send_next_message();
1403
    void send_pong(milliseconds_type timestamp);
1404
    void send_log_message(const LogMessage& log_msg);
1405

1406
    void handle_write_output_buffer();
1407
    void handle_pong_output_buffer();
1408

1409
    void initiate_write_error(ProtocolError, session_ident_type);
1410
    void handle_write_error(std::error_code ec);
1411

1412
    void do_initiate_soft_close(ProtocolError, session_ident_type);
1413
    void read_error(std::error_code);
1414
    void write_error(std::error_code);
1415

1416
    void close_due_to_close_by_client(std::error_code);
1417
    void close_due_to_error(std::error_code);
1418

1419
    void terminate_sessions();
1420

1,074✔
1421
    void bad_session_ident(const char* message_type, session_ident_type);
1,074✔
1422
    void message_after_unbind(const char* message_type, session_ident_type);
1,074✔
1423
    void message_before_ident(const char* message_type, session_ident_type);
336✔
1424
};
634✔
1425

634✔
1426

634✔
1427
inline void SyncConnection::read_error(std::error_code ec)
440✔
1428
{
×
1429
    REALM_ASSERT(ec != util::error::operation_aborted);
×
1430
    if (ec == util::MiscExtErrors::end_of_input || ec == util::error::connection_reset) {
×
1431
        // Suicide
×
1432
        close_due_to_close_by_client(ec); // Throws
244✔
1433
        return;
440✔
1434
    }
244✔
1435
    if (ec == util::MiscExtErrors::delim_not_found) {
244✔
1436
        logger.error("Input message head delimited not found"); // Throws
440✔
1437
        protocol_error(ProtocolError::limits_exceeded);         // Throws
440✔
1438
        return;
1439
    }
1440

×
1441
    logger.error("Reading failed: %1", ec.message()); // Throws
×
1442

×
1443
    // Suicide
1444
    close_due_to_error(ec); // Throws
×
1445
}
×
1446

×
1447
inline void SyncConnection::write_error(std::error_code ec)
×
1448
{
1449
    REALM_ASSERT(ec != util::error::operation_aborted);
1450
    if (ec == util::error::broken_pipe || ec == util::error::connection_reset) {
×
1451
        // Suicide
×
1452
        close_due_to_close_by_client(ec); // Throws
1453
        return;
1454
    }
1455
    logger.error("Writing failed: %1", ec.message()); // Throws
1456

1457
    // Suicide
1458
    close_due_to_error(ec); // Throws
1459
}
1460

1461

1462
// ============================ HTTPConnection ============================
1463

1464
std::string g_user_agent = "User-Agent";
1465

1466
class HTTPConnection {
1467
public:
1468
    const std::shared_ptr<Logger> logger_ptr;
1469
    util::Logger& logger;
1470

1471
    HTTPConnection(ServerImpl& serv, int_fast64_t id, bool is_ssl)
9,870✔
1472
        : logger_ptr{std::make_shared<PrefixLogger>(make_logger_prefix(id), serv.logger_ptr)} // Throws
4,872✔
1473
        , logger{*logger_ptr}
4,872✔
1474
        , m_server{serv}
9,870✔
1475
        , m_id{id}
4,872✔
1476
        , m_socket{new network::Socket{serv.get_service()}} // Throws
9,870✔
1477
        , m_read_ahead_buffer{new network::ReadAheadBuffer} // Throws
48✔
1478
        , m_http_server{*this, logger_ptr}
48✔
1479
    {
48✔
1480
        // Make the output buffer stream throw std::bad_alloc if it fails to
48✔
1481
        // expand the buffer
48✔
1482
        m_output_buffer.exceptions(std::ios_base::badbit | std::ios_base::failbit);
9,870✔
1483

1484
        if (is_ssl) {
1485
            using namespace network::ssl;
×
1486
            Context& ssl_context = serv.get_ssl_context();
×
1487
            m_ssl_stream = std::make_unique<Stream>(*m_socket, ssl_context,
×
1488
                                                    Stream::server); // Throws
1489
        }
1490
    }
1,902✔
1491

1,902✔
1492
    ServerImpl& get_server() noexcept
1,902✔
1493
    {
1494
        return m_server;
1495
    }
11,450✔
1496

11,450✔
1497
    int_fast64_t get_id() const noexcept
11,450✔
1498
    {
1499
        return m_id;
1500
    }
1501

1,892✔
1502
    network::Socket& get_socket() noexcept
1,892✔
1503
    {
14✔
1504
        return *m_socket;
14✔
1505
    }
1,878✔
1506

1,878✔
1507
    template <class H>
1,878✔
1508
    void async_write(const char* data, size_t size, H handler)
1,892✔
1509
    {
1510
        if (m_ssl_stream) {
1511
            m_ssl_stream->async_write(data, size, std::move(handler)); // Throws
1512
        }
8✔
1513
        else {
8✔
1514
            m_socket->async_write(data, size, std::move(handler)); // Throws
×
1515
        }
×
1516
    }
×
1517

8✔
1518
    template <class H>
8✔
1519
    void async_read(char* buffer, size_t size, H handler)
8✔
1520
    {
8✔
1521
        if (m_ssl_stream) {
8✔
1522
            m_ssl_stream->async_read(buffer, size, *m_read_ahead_buffer,
1523
                                     std::move(handler)); // Throws
1524
        }
1525
        else {
16,908✔
1526
            m_socket->async_read(buffer, size, *m_read_ahead_buffer,
16,908✔
1527
                                 std::move(handler)); // Throws
126✔
1528
        }
126✔
1529
    }
126✔
1530

16,782✔
1531
    template <class H>
16,782✔
1532
    void async_read_until(char* buffer, size_t size, char delim, H handler)
16,782✔
1533
    {
16,782✔
1534
        if (m_ssl_stream) {
16,908✔
1535
            m_ssl_stream->async_read_until(buffer, size, delim, *m_read_ahead_buffer,
1536
                                           std::move(handler)); // Throws
1537
        }
1,902✔
1538
        else {
1,902✔
1539
            m_socket->async_read_until(buffer, size, delim, *m_read_ahead_buffer,
1,902✔
1540
                                       std::move(handler)); // Throws
940✔
1541
        }
1,902✔
1542
    }
940✔
1543

1,902✔
1544
    void initiate(std::string remote_endpoint)
24✔
1545
    {
24✔
1546
        m_last_activity_at = steady_clock_now();
1,878✔
1547
        m_remote_endpoint = std::move(remote_endpoint);
1,878✔
1548

1,878✔
1549
        logger.detail("Connection from %1", m_remote_endpoint); // Throws
1,902✔
1550

1551
        if (m_ssl_stream) {
1552
            initiate_ssl_handshake(); // Throws
×
1553
        }
×
1554
        else {
×
1555
            initiate_http(); // Throws
1556
        }
1557
    }
×
1558

×
1559
    void respond_200_ok()
×
1560
    {
1561
        handle_text_response(HTTPStatus::Ok, "OK"); // Throws
1562
    }
×
1563

×
1564
    void respond_404_not_found()
×
1565
    {
1566
        handle_text_response(HTTPStatus::NotFound, "Not found"); // Throws
1567
    }
1568

1569
    void respond_503_service_unavailable()
30✔
1570
    {
30✔
1571
        handle_text_response(HTTPStatus::ServiceUnavailable, "Service unavailable"); // Throws
30✔
1572
    }
30✔
1573

30✔
1574
    // Commits suicide
30✔
1575
    template <class... Params>
1576
    void terminate(Logger::Level log_level, const char* log_message, Params... log_params)
1577
    {
1578
        logger.log(log_level, log_message, log_params...); // Throws
2✔
1579
        m_ssl_stream.reset();
2✔
1580
        m_socket.reset();
2✔
1581
        m_server.remove_http_connection(m_id); // Suicide
2✔
1582
    }
×
1583

1584
    // Commits suicide
×
1585
    void terminate_if_dead(SteadyTimePoint now)
×
1586
    {
×
1587
        milliseconds_type time = steady_duration(m_last_activity_at, now);
×
1588
        const Server::Config& config = m_server.get_config();
2✔
1589
        if (m_is_sending) {
2✔
1590
            if (time >= config.http_response_timeout) {
1591
                // Suicide
×
1592
                terminate(Logger::Level::detail,
×
1593
                          "HTTP connection closed (request timeout)"); // Throws
×
1594
            }
2✔
1595
        }
2✔
1596
        else {
1597
            if (time >= config.http_request_timeout) {
1598
                // Suicide
1,890✔
1599
                terminate(Logger::Level::detail,
1,890✔
1600
                          "HTTP connection closed (response timeout)"); // Throws
1,890✔
1601
            }
1602
        }
1603
    }
1604

1605
    std::string get_appservices_request_id() const
1606
    {
1607
        return m_appservices_request_id.to_string();
1608
    }
1609

1610
private:
1611
    ServerImpl& m_server;
1612
    const int_fast64_t m_id;
1613
    const ObjectId m_appservices_request_id = ObjectId::gen();
1614
    std::unique_ptr<network::Socket> m_socket;
1615
    std::unique_ptr<network::ssl::Stream> m_ssl_stream;
1616
    std::unique_ptr<network::ReadAheadBuffer> m_read_ahead_buffer;
1617
    HTTPServer<HTTPConnection> m_http_server;
24✔
1618
    OutputBuffer m_output_buffer;
24✔
1619
    bool m_is_sending = false;
24✔
1620
    SteadyTimePoint m_last_activity_at;
24✔
1621
    std::string m_remote_endpoint;
24✔
1622
    int m_negotiated_protocol_version = 0;
24✔
1623

24✔
1624
    void initiate_ssl_handshake()
1625
    {
1626
        auto handler = [this](std::error_code ec) {
24✔
1627
            if (ec != util::error::operation_aborted)
24✔
1628
                handle_ssl_handshake(ec); // Throws
10✔
1629
        };
10✔
1630
        m_ssl_stream->async_handshake(std::move(handler)); // Throws
10✔
1631
    }
10✔
1632

14✔
1633
    void handle_ssl_handshake(std::error_code ec)
14✔
1634
    {
1635
        if (ec) {
1636
            logger.error("SSL handshake error (%1): %2", ec, ec.message()); // Throws
1,892✔
1637
            close_due_to_error(ec);                                         // Throws
1,892✔
1638
            return;
934✔
1639
        }
1,892✔
1640
        initiate_http(); // Throws
1,892✔
1641
    }
934✔
1642

1,892✔
1643
    void initiate_http()
×
1644
    {
×
1645
        logger.debug("Connection initiates HTTP receipt");
×
1646

×
1647
        auto handler = [this](HTTPRequest request, std::error_code ec) {
1,892✔
1648
            if (REALM_UNLIKELY(ec == util::error::operation_aborted))
8✔
1649
                return;
8✔
1650
            if (REALM_UNLIKELY(ec == HTTPParserError::MalformedRequest)) {
8✔
1651
                logger.error("Malformed HTTP request");
8✔
1652
                close_due_to_error(ec); // Throws
8✔
1653
                return;
1,884✔
1654
            }
×
1655
            if (REALM_UNLIKELY(ec == HTTPParserError::BadRequest)) {
×
1656
                logger.error("Bad HTTP request");
×
1657
                const char* body = "The HTTP request was corrupted";
1,884✔
1658
                handle_400_bad_request(body); // Throws
1,884✔
1659
                return;
1,892✔
1660
            }
1,892✔
1661
            if (REALM_UNLIKELY(ec)) {
1662
                read_error(ec); // Throws
1663
                return;
1,884✔
1664
            }
1,884✔
1665
            handle_http_request(std::move(request)); // Throws
930✔
1666
        };
1,884✔
1667
        m_http_server.async_receive_request(std::move(handler)); // Throws
930✔
1668
    }
1,884✔
1669

1,884✔
1670
    void handle_http_request(const HTTPRequest& request)
930✔
1671
    {
930✔
1672
        StringData path = request.path;
930✔
1673

930✔
1674
        logger.debug("HTTP request received, request = %1", request);
930✔
1675

930✔
1676
        m_is_sending = true;
1,884✔
1677
        m_last_activity_at = steady_clock_now();
1,872✔
1678

1,872✔
1679
        // FIXME: When thinking of this function as a switching device, it seem
12✔
1680
        // wrong that it requires a `%2F` after `/realm-sync/`. If `%2F` is
12✔
1681
        // supposed to be mandatory, then that check ought to be delegated to
12✔
1682
        // handle_request_for_sync(), as that will yield a sharper separation of
1,884✔
1683
        // concerns.
1684
        if (path == "/realm-sync" || path.begins_with("/realm-sync?") || path.begins_with("/realm-sync/%2F")) {
1685
            handle_request_for_sync(request); // Throws
1,872✔
1686
        }
1,872✔
1687
        else {
×
1688
            handle_404_not_found(request); // Throws
×
1689
        }
×
1690
    }
×
1691

×
1692
    void handle_request_for_sync(const HTTPRequest& request)
×
1693
    {
924✔
1694
        if (m_server.is_sync_stopped()) {
1,872✔
1695
            logger.debug("Attempt to create a sync connection to a server that has been "
924✔
1696
                         "stopped"); // Throws
924✔
1697
            handle_503_service_unavailable(request, "The server does not accept sync "
924✔
1698
                                                    "connections"); // Throws
1,872✔
1699
            return;
1,872✔
1700
        }
1,872✔
1701

1,872✔
1702
        util::Optional<std::string> sec_websocket_protocol = websocket::read_sec_websocket_protocol(request);
1,872✔
1703

1,872✔
1704
        // Figure out whether there are any protocol versions supported by both
1,872✔
1705
        // the client and the server, and if so, choose the newest one of them.
1,872✔
1706
        MiscBuffers& misc_buffers = m_server.get_misc_buffers();
1,872✔
1707
        using ProtocolVersionRanges = MiscBuffers::ProtocolVersionRanges;
1,872✔
1708
        ProtocolVersionRanges& protocol_version_ranges = misc_buffers.protocol_version_ranges;
1,872✔
1709
        {
1,872✔
1710
            protocol_version_ranges.clear();
1,872✔
1711
            util::MemoryInputStream in;
18,720✔
1712
            in.imbue(std::locale::classic());
8,316✔
1713
            in.unsetf(std::ios_base::skipws);
16,848✔
1714
            std::string_view value;
16,848✔
1715
            if (sec_websocket_protocol)
16,848✔
1716
                value = *sec_websocket_protocol;
16,848✔
1717
            HttpListHeaderValueParser parser{value};
×
1718
            std::string_view elem;
×
1719
            while (parser.next(elem)) {
16,848✔
1720
                // FIXME: Use std::string_view::begins_with() in C++20.
16,848✔
1721
                const StringData protocol{elem};
16,848✔
1722
                std::string_view prefix;
16,848✔
1723
                if (protocol.begins_with(get_pbs_websocket_protocol_prefix()))
16,848✔
1724
                    prefix = get_pbs_websocket_protocol_prefix();
16,848✔
1725
                else if (protocol.begins_with(get_old_pbs_websocket_protocol_prefix()))
16,848✔
1726
                    prefix = get_old_pbs_websocket_protocol_prefix();
×
1727
                if (!prefix.empty()) {
×
1728
                    auto parse_version = [&](std::string_view str) {
16,848✔
1729
                        in.set_buffer(str.data(), str.data() + str.size());
16,848✔
1730
                        int version = 0;
16,848✔
1731
                        in >> version;
16,848✔
1732
                        if (REALM_LIKELY(in && in.eof() && version >= 0))
×
1733
                            return version;
×
1734
                        return -1;
×
1735
                    };
16,848✔
1736
                    int min, max;
16,848✔
1737
                    std::string_view range = elem.substr(prefix.size());
16,848✔
1738
                    auto i = range.find('-');
16,848✔
1739
                    if (i != std::string_view::npos) {
16,848✔
1740
                        min = parse_version(range.substr(0, i));
16,848✔
1741
                        max = parse_version(range.substr(i + 1));
16,848✔
1742
                    }
16,848✔
1743
                    else {
×
1744
                        min = parse_version(range);
×
1745
                        max = min;
×
1746
                    }
×
1747
                    if (REALM_LIKELY(min >= 0 && max >= 0 && min <= max)) {
×
1748
                        protocol_version_ranges.emplace_back(min, max); // Throws
×
1749
                        continue;
×
1750
                    }
×
1751
                    logger.error("Protocol version negotiation failed: Client sent malformed "
×
1752
                                 "specification of supported protocol versions: '%1'",
×
1753
                                 elem); // Throws
×
1754
                    handle_400_bad_request("Protocol version negotiation failed: Malformed "
×
1755
                                           "specification of supported protocol "
1,872✔
1756
                                           "versions\n"); // Throws
×
1757
                    return;
×
1758
                }
×
1759
                logger.warn("Unrecognized protocol token in HTTP response header "
×
1760
                            "Sec-WebSocket-Protocol: '%1'",
×
1761
                            elem); // Throws
×
1762
            }
1,872✔
1763
            if (protocol_version_ranges.empty()) {
1,872✔
1764
                logger.error("Protocol version negotiation failed: Client did not send a "
1,872✔
1765
                             "specification of supported protocol versions"); // Throws
1,872✔
1766
                handle_400_bad_request("Protocol version negotiation failed: Missing specification "
1,872✔
1767
                                       "of supported protocol versions\n"); // Throws
1,872✔
1768
                return;
1,872✔
1769
            }
1,872✔
1770
        }
16,848✔
1771
        {
16,848✔
1772
            ProtocolVersionRange server_range = m_server.get_protocol_version_range();
16,848✔
1773
            int server_min = server_range.first;
16,848✔
1774
            int server_max = server_range.second;
8,316✔
1775
            int best_match = 0;
16,848✔
1776
            int overall_client_min = std::numeric_limits<int>::max();
16,848✔
1777
            int overall_client_max = std::numeric_limits<int>::min();
1,872✔
1778
            for (const auto& range : protocol_version_ranges) {
1,872✔
1779
                int client_min = range.first;
16,848✔
1780
                int client_max = range.second;
16,848✔
1781
                if (client_max >= server_min && client_min <= server_max) {
16,848✔
1782
                    // Overlap
16,848✔
1783
                    int version = std::min(client_max, server_max);
1,872✔
1784
                    if (version > best_match) {
16,848✔
1785
                        best_match = version;
1,872✔
1786
                    }
1,872✔
1787
                }
×
1788
                if (client_min < overall_client_min)
×
1789
                    overall_client_min = client_min;
×
1790
                if (client_max > overall_client_max)
1791
                    overall_client_max = client_max;
×
1792
            }
×
1793
            Formatter& formatter = misc_buffers.formatter;
×
1794
            if (REALM_UNLIKELY(best_match == 0)) {
×
1795
                const char* elaboration = "No version supported by both client and server";
1796
                const char* identifier_hint = nullptr;
×
1797
                if (overall_client_max < server_min) {
×
1798
                    // Client is too old
×
1799
                    elaboration = "Client is too old for server";
×
1800
                    identifier_hint = "CLIENT_TOO_OLD";
×
1801
                }
×
1802
                else if (overall_client_min > server_max) {
×
1803
                    // Client is too new
×
1804
                    elaboration = "Client is too new for server";
×
1805
                    identifier_hint = "CLIENT_TOO_NEW";
×
1806
                }
×
1807
                auto format_ranges = [&](const auto& list) {
×
1808
                    bool nonfirst = false;
×
1809
                    for (auto range : list) {
×
1810
                        if (nonfirst)
×
1811
                            formatter << ", "; // Throws
×
1812
                        int min = range.first, max = range.second;
×
1813
                        REALM_ASSERT(min <= max);
×
1814
                        formatter << min;
×
1815
                        if (max != min)
×
1816
                            formatter << "-" << max;
×
1817
                        nonfirst = true;
×
1818
                    }
×
1819
                };
×
1820
                using Range = ProtocolVersionRange;
×
1821
                formatter.reset();
×
1822
                format_ranges(protocol_version_ranges); // Throws
×
1823
                logger.error("Protocol version negotiation failed: %1 "
×
1824
                             "(client supports: %2)",
×
1825
                             elaboration, std::string_view(formatter.data(), formatter.size())); // Throws
×
1826
                formatter.reset();
×
1827
                formatter << "Protocol version negotiation failed: "
×
1828
                             ""
×
1829
                          << elaboration << ".\n\n";                                   // Throws
×
1830
                formatter << "Server supports: ";                                      // Throws
×
1831
                format_ranges(std::initializer_list<Range>{{server_min, server_max}}); // Throws
×
1832
                formatter << "\n";                                                     // Throws
×
1833
                formatter << "Client supports: ";                                      // Throws
×
1834
                format_ranges(protocol_version_ranges);                                // Throws
×
1835
                formatter << "\n\n";                                                   // Throws
1,872✔
1836
                formatter << "REALM_SYNC_PROTOCOL_MISMATCH";                           // Throws
1,872✔
1837
                if (identifier_hint)
1,872✔
1838
                    formatter << ":" << identifier_hint;                      // Throws
1,872✔
1839
                formatter << "\n";                                            // Throws
1,872✔
1840
                handle_400_bad_request({formatter.data(), formatter.size()}); // Throws
924✔
1841
                return;
1,872✔
1842
            }
1,872✔
1843
            m_negotiated_protocol_version = best_match;
1,872✔
1844
            logger.debug("Received: Sync HTTP request (negotiated_protocol_version=%1)",
1,872✔
1845
                         m_negotiated_protocol_version); // Throws
924✔
1846
            formatter.reset();
1,872✔
1847
        }
1,872✔
1848

1,872✔
1849
        std::string sec_websocket_protocol_2;
1,872✔
1850
        {
1,872✔
1851
            std::string_view prefix =
1,872✔
1852
                m_negotiated_protocol_version < SyncConnection::PBS_FLX_MIGRATION_PROTOCOL_VERSION
924✔
1853
                    ? get_old_pbs_websocket_protocol_prefix()
1,872✔
1854
                    : get_pbs_websocket_protocol_prefix();
1,872✔
1855
            std::ostringstream out;
1,872✔
1856
            out.imbue(std::locale::classic());
924✔
1857
            out << prefix << m_negotiated_protocol_version; // Throws
1,872✔
1858
            sec_websocket_protocol_2 = std::move(out).str();
×
1859
        }
×
1860

×
1861
        std::error_code ec;
×
1862
        util::Optional<HTTPResponse> response =
×
1863
            websocket::make_http_response(request, sec_websocket_protocol_2, ec); // Throws
×
1864

×
1865
        if (ec) {
×
1866
            if (ec == websocket::HttpError::bad_request_header_upgrade) {
×
1867
                logger.error("There must be a header of the form 'Upgrade: websocket'");
×
1868
            }
×
1869
            else if (ec == websocket::HttpError::bad_request_header_connection) {
×
1870
                logger.error("There must be a header of the form 'Connection: Upgrade'");
1871
            }
×
1872
            else if (ec == websocket::HttpError::bad_request_header_websocket_version) {
×
1873
                logger.error("There must be a header of the form 'Sec-WebSocket-Version: 13'");
×
1874
            }
×
1875
            else if (ec == websocket::HttpError::bad_request_header_websocket_key) {
×
1876
                logger.error("The header Sec-WebSocket-Key is missing");
×
1877
            }
1,872✔
1878

1,872✔
1879
            logger.error("The HTTP request with the error is:\n%1", request);
924✔
1880
            logger.error("Check the proxy configuration and make sure that the "
1,872✔
1881
                         "HTTP request is a valid Websocket request.");
1,872✔
1882
            close_due_to_error(ec);
1,872✔
1883
            return;
1,872✔
1884
        }
1,872✔
1885
        REALM_ASSERT(response);
1,872✔
1886
        add_common_http_response_headers(*response);
924✔
1887

1,872✔
1888
        std::string user_agent;
1,870✔
1889
        {
924✔
1890
            auto i = request.headers.find(g_user_agent);
1,870✔
1891
            if (i != request.headers.end())
1,870✔
1892
                user_agent = i->second; // Throws (copy)
×
1893
        }
×
1894

×
1895
        auto handler = [protocol_version = m_negotiated_protocol_version, user_agent = std::move(user_agent),
924✔
1896
                        this](std::error_code ec) {
1,870✔
1897
            // If the operation is aborted, the socket object may have been destroyed.
1,870✔
1898
            if (ec != util::error::operation_aborted) {
1,870✔
1899
                if (ec) {
1,870✔
1900
                    write_error(ec);
1,870✔
1901
                    return;
1,870✔
1902
                }
1,870✔
1903

1,870✔
1904
                std::unique_ptr<SyncConnection> sync_conn = std::make_unique<SyncConnection>(
1,870✔
1905
                    m_server, m_id, std::move(m_socket), std::move(m_ssl_stream), std::move(m_read_ahead_buffer),
1,870✔
1906
                    protocol_version, std::move(user_agent), std::move(m_remote_endpoint),
1,872✔
1907
                    get_appservices_request_id()); // Throws
1,872✔
1908
                SyncConnection& sync_conn_ref = *sync_conn;
1909
                m_server.add_sync_connection(m_id, std::move(sync_conn));
1910
                m_server.remove_http_connection(m_id);
20✔
1911
                sync_conn_ref.initiate();
20✔
1912
            }
10✔
1913
        };
20✔
1914
        m_http_server.async_send_response(*response, std::move(handler));
20✔
1915
    }
20✔
1916

20✔
1917
    void handle_text_response(HTTPStatus http_status, std::string_view body)
10✔
1918
    {
20✔
1919
        std::string body_2 = std::string(body); // Throws
20✔
1920

20✔
1921
        HTTPResponse response;
20✔
1922
        response.status = http_status;
10✔
1923
        add_common_http_response_headers(response);
20✔
1924
        response.headers["Connection"] = "close";
20✔
1925

10✔
1926
        if (!body_2.empty()) {
20✔
1927
            response.headers["Content-Length"] = util::to_string(body_2.size());
×
1928
            response.body = std::move(body_2);
×
1929
        }
×
1930

20✔
1931
        auto handler = [this](std::error_code ec) {
20✔
1932
            if (REALM_UNLIKELY(ec == util::error::operation_aborted))
20✔
1933
                return;
20✔
1934
            if (REALM_UNLIKELY(ec)) {
1935
                write_error(ec);
1936
                return;
8✔
1937
            }
8✔
1938
            terminate(Logger::Level::detail, "HTTP connection closed"); // Throws
8✔
1939
        };
8✔
1940
        m_http_server.async_send_response(response, std::move(handler));
1941
    }
1942

12✔
1943
    void handle_400_bad_request(std::string_view body)
12✔
1944
    {
12✔
1945
        logger.detail("400 Bad Request");
12✔
1946
        handle_text_response(HTTPStatus::BadRequest, body); // Throws
12✔
1947
    }
1948

1949
    void handle_404_not_found(const HTTPRequest&)
×
1950
    {
×
1951
        logger.detail("404 Not Found"); // Throws
×
1952
        handle_text_response(HTTPStatus::NotFound,
×
1953
                             "Realm sync server\n\nPage not found\n"); // Throws
1954
    }
1955

1,892✔
1956
    void handle_503_service_unavailable(const HTTPRequest&, std::string_view message)
1,892✔
1957
    {
1,892✔
1958
        logger.debug("503 Service Unavailable");                       // Throws
10✔
1959
        handle_text_response(HTTPStatus::ServiceUnavailable, message); // Throws
20✔
1960
    }
20✔
1961

1,892✔
1962
    void add_common_http_response_headers(HTTPResponse& response)
1963
    {
1964
        response.headers["Server"] = "RealmSync/" REALM_VERSION_STRING; // Throws
×
1965
        if (m_negotiated_protocol_version < SyncConnection::SERVER_LOG_PROTOCOL_VERSION) {
×
1966
            // This isn't a real X-Appservices-Request-Id, but it should be enough to test with
×
1967
            response.headers["X-Appservices-Request-Id"] = get_appservices_request_id();
1968
        }
×
1969
    }
×
1970

×
1971
    void read_error(std::error_code ec)
×
1972
    {
×
1973
        REALM_ASSERT(ec != util::error::operation_aborted);
×
1974
        if (ec == util::MiscExtErrors::end_of_input || ec == util::error::connection_reset) {
×
1975
            // Suicide
×
1976
            close_due_to_close_by_client(ec); // Throws
1977
            return;
×
1978
        }
1979
        if (ec == util::MiscExtErrors::delim_not_found) {
1980
            logger.error("Input message head delimited not found"); // Throws
×
1981
            close_due_to_error(ec);                                 // Throws
×
1982
            return;
1983
        }
1984

×
1985
        logger.error("Reading failed: %1", ec.message()); // Throws
×
1986

×
1987
        // Suicide
1988
        close_due_to_error(ec); // Throws
×
1989
    }
×
1990

×
1991
    void write_error(std::error_code ec)
×
1992
    {
1993
        REALM_ASSERT(ec != util::error::operation_aborted);
1994
        if (ec == util::error::broken_pipe || ec == util::error::connection_reset) {
×
1995
            // Suicide
×
1996
            close_due_to_close_by_client(ec); // Throws
1997
            return;
1998
        }
×
1999
        logger.error("Writing failed: %1", ec.message()); // Throws
×
2000

2001
        // Suicide
×
2002
        close_due_to_error(ec); // Throws
×
2003
    }
2004

2005
    void close_due_to_close_by_client(std::error_code ec)
10✔
2006
    {
6✔
2007
        auto log_level = (ec == util::MiscExtErrors::end_of_input ? Logger::Level::detail : Logger::Level::info);
10✔
2008
        // Suicide
10✔
2009
        terminate(log_level, "HTTP connection closed by client: %1", ec.message()); // Throws
10✔
2010
    }
2011

2012
    void close_due_to_error(std::error_code ec)
9,870✔
2013
    {
9,870✔
2014
        // Suicide
9,870✔
2015
        terminate(Logger::Level::error, "HTTP connection closed due to error: %1",
9,870✔
2016
                  ec.message()); // Throws
9,870✔
2017
    }
9,870✔
2018

2019
    static std::string make_logger_prefix(int_fast64_t id)
2020
    {
2021
        std::ostringstream out;
2022
        out.imbue(std::locale::classic());
2023
        out << "HTTP Connection[" << id << "]: "; // Throws
2024
        return out.str();                         // Throws
2025
    }
2026
};
2027

2028

2029
class DownloadHistoryEntryHandler : public ServerHistory::HistoryEntryHandler {
2030
public:
2031
    std::size_t num_changesets = 0;
38,128✔
2032
    std::size_t accum_original_size = 0;
38,128✔
2033
    std::size_t accum_compacted_size = 0;
2034

2035
    DownloadHistoryEntryHandler(ServerProtocol& protocol, OutputBuffer& buffer, util::Logger& logger) noexcept
40,664✔
2036
        : m_protocol{protocol}
40,664✔
2037
        , m_buffer{buffer}
40,664✔
2038
        , m_logger{logger}
40,664✔
2039
    {
40,664✔
2040
    }
40,664✔
2041

40,664✔
2042
    void handle(version_type server_version, const HistoryEntry& entry, size_t original_size) override
40,664✔
2043
    {
2044
        version_type client_version = entry.remote_version;
2045
        ServerProtocol::ChangesetInfo info{server_version, client_version, entry, original_size};
2046
        m_protocol.insert_single_changeset_download_message(m_buffer, info, m_logger); // Throws
2047
        ++num_changesets;
2048
        accum_original_size += original_size;
2049
        accum_compacted_size += entry.changeset.size();
2050
    }
2051

2052
private:
2053
    ServerProtocol& m_protocol;
2054
    OutputBuffer& m_buffer;
2055
    util::Logger& m_logger;
2056
};
2057

2058

2059
// ============================ Session ============================
2060

2061
//                        Need cli-   Send     IDENT     UNBIND              ERROR
2062
//   Protocol             ent file    IDENT    message   message   Error     message
2063
//   state                identifier  message  received  received  occurred  sent
2064
// ---------------------------------------------------------------------------------
2065
//   AllocatingIdent      yes         yes      no        no        no        no
2066
//   SendIdent            no          yes      no        no        no        no
2067
//   WaitForIdent         no          no       no        no        no        no
2068
//   WaitForUnbind        maybe       no       yes       no        no        no
2069
//   SendError            maybe       maybe    maybe     no        yes       no
2070
//   WaitForUnbindErr     maybe       maybe    maybe     no        yes       yes
2071
//   SendUnbound          maybe       maybe    maybe     yes       maybe     no
2072
//
2073
//
2074
//   Condition                      Expression
2075
// ----------------------------------------------------------
2076
//   Need client file identifier    need_client_file_ident()
2077
//   Send IDENT message             must_send_ident_message()
2078
//   IDENT message received         ident_message_received()
2079
//   UNBIND message received        unbind_message_received()
2080
//   Error occurred                 error_occurred()
2081
//   ERROR message sent             m_error_message_sent
2082
//
2083
//
2084
//   Protocol
2085
//   state                Will send              Can receive
2086
// -----------------------------------------------------------------------
2087
//   AllocatingIdent      none                   UNBIND
2088
//   SendIdent            IDENT                  UNBIND
2089
//   WaitForIdent         none                   IDENT, UNBIND
2090
//   WaitForUnbind        DOWNLOAD, TRANSACT,    UPLOAD, TRANSACT, MARK,
2091
//                        MARK, ALLOC            ALLOC, UNBIND
2092
//   SendError            ERROR                  any
2093
//   WaitForUnbindErr     none                   any
2094
//   SendUnbound          UNBOUND                none
2095
//
2096
class Session final : private FileIdentReceiver {
5,052✔
2097
public:
5,052✔
2098
    util::PrefixLogger logger;
2099

2100
    Session(SyncConnection& conn, session_ident_type session_ident)
5,054✔
2101
        : logger{make_logger_prefix(session_ident), conn.logger_ptr} // Throws
5,054✔
2102
        , m_connection{conn}
5,054✔
2103
        , m_session_ident{session_ident}
5,054✔
2104
    {
2105
    }
2106

38,146✔
2107
    ~Session() noexcept
38,146✔
2108
    {
38,146✔
2109
        REALM_ASSERT(!is_enlisted_to_send());
2110
        detach_from_server_file();
2111
    }
×
2112

×
2113
    SyncConnection& get_connection() noexcept
×
2114
    {
2115
        return m_connection;
2116
    }
152✔
2117

152✔
2118
    const Optional<std::array<char, 64>>& get_encryption_key()
152✔
2119
    {
2120
        return m_connection.get_server().get_config().encryption_key;
2121
    }
53,976✔
2122

53,976✔
2123
    session_ident_type get_session_ident() const noexcept
53,976✔
2124
    {
2125
        return m_session_ident;
2126
    }
6,504✔
2127

6,504✔
2128
    ServerProtocol& get_server_protocol() noexcept
6,504✔
2129
    {
2130
        return m_connection.get_server_protocol();
2131
    }
4,000✔
2132

4,000✔
2133
    bool need_client_file_ident() const noexcept
4,000✔
2134
    {
2135
        return (m_file_ident_request != 0);
2136
    }
328,074✔
2137

328,074✔
2138
    bool must_send_ident_message() const noexcept
328,074✔
2139
    {
2140
        return m_send_ident_message;
2141
    }
330,690✔
2142

330,690✔
2143
    bool ident_message_received() const noexcept
330,690✔
2144
    {
2145
        return m_client_file_ident != 0;
2146
    }
323,330✔
2147

323,330✔
2148
    bool unbind_message_received() const noexcept
323,330✔
2149
    {
2150
        return m_unbind_message_received;
2151
    }
×
2152

×
2153
    bool error_occurred() const noexcept
×
2154
    {
2155
        return int(m_error_code) != 0;
2156
    }
2157

2158
    bool relayed_alloc_request_in_progress() const noexcept
×
2159
    {
×
2160
        return (need_client_file_ident() || m_allocated_file_ident.ident != 0);
×
2161
    }
2162

2163
    // Returns the file identifier (always a nonzero value) of the client side
5,054✔
2164
    // file if ident_message_received() returns true. Otherwise it returns zero.
5,054✔
2165
    file_ident_type get_client_file_ident() const noexcept
5,054✔
2166
    {
2167
        return m_client_file_ident;
2168
    }
4,142✔
2169

4,142✔
2170
    void initiate()
4,142✔
2171
    {
2172
        logger.detail("Session initiated", m_session_ident); // Throws
2173
    }
2174

2175
    void terminate()
2176
    {
2177
        logger.detail("Session terminated", m_session_ident); // Throws
2178
    }
2179

2180
    // Initiate the deactivation process, if it has not been initiated already
2181
    // by the client.
2182
    //
2183
    // IMPORTANT: This function must not be called with protocol versions
2184
    // earlier than 23.
2185
    //
76✔
2186
    // The deactivation process will eventually lead to termination of the
76✔
2187
    // session.
76✔
2188
    //
36✔
2189
    // The session will detach itself from the server file when the deactivation
36✔
2190
    // process is initiated, regardless of whether it is initiated by the
36✔
2191
    // client, or by calling this function.
76✔
2192
    void initiate_deactivation(ProtocolError error_code)
76✔
2193
    {
76✔
2194
        REALM_ASSERT(is_session_level_error(error_code));
36✔
2195
        REALM_ASSERT(!error_occurred()); // Must only be called once
76✔
2196

76✔
2197
        // If the UNBIND message has been received, then the client has
76✔
2198
        // initiated the deactivation process already.
36✔
2199
        if (REALM_LIKELY(!unbind_message_received())) {
76✔
2200
            detach_from_server_file();
2201
            m_error_code = error_code;
2202
            // Protocol state is now SendError
256,750✔
2203
            ensure_enlisted_to_send();
256,750✔
2204
            return;
256,750✔
2205
        }
2206
        // Protocol state was SendUnbound, and remains unchanged
2207
    }
50,084✔
2208

50,084✔
2209
    bool is_enlisted_to_send() const noexcept
48,606✔
2210
    {
50,084✔
2211
        return m_next != nullptr;
2212
    }
2213

102,784✔
2214
    void ensure_enlisted_to_send() noexcept
102,784✔
2215
    {
102,784✔
2216
        if (!is_enlisted_to_send())
2217
            enlist_to_send();
2218
    }
2219

1,252✔
2220
    void enlist_to_send() noexcept
548✔
2221
    {
1,252✔
2222
        m_connection.enlist_to_send(this);
1,252✔
2223
    }
1,252✔
2224

1,252✔
2225
    // Overriding memeber function in FileIdentReceiver
×
2226
    void receive_file_ident(SaltedFileIdent file_ident) override final
×
2227
    {
×
2228
        // Protocol state must be AllocatingIdent or WaitForUnbind
1,252✔
2229
        if (!ident_message_received()) {
1,252✔
2230
            REALM_ASSERT(need_client_file_ident());
1,252✔
2231
            REALM_ASSERT(m_send_ident_message);
548✔
2232
        }
1,252✔
2233
        else {
1,252✔
2234
            REALM_ASSERT(!m_send_ident_message);
548✔
2235
        }
548✔
2236
        REALM_ASSERT(!unbind_message_received());
548✔
2237
        REALM_ASSERT(!error_occurred());
548✔
2238
        REALM_ASSERT(!m_error_message_sent);
1,252✔
2239

1,252✔
2240
        m_file_ident_request = 0;
548✔
2241
        m_allocated_file_ident = file_ident;
1,252✔
2242

1,252✔
2243
        // If the protocol state was AllocatingIdent, it is now SendIdent,
2244
        // otherwise it continues to be WaitForUnbind.
2245

2246
        logger.debug("Acquired outbound salted file identifier (%1, %2)", file_ident.ident,
2247
                     file_ident.salt); // Throws
2248

2249
        ensure_enlisted_to_send();
2250
    }
102,610✔
2251

102,610✔
2252
    // Called by the associated connection object when this session is granted
100,170✔
2253
    // an opportunity to initiate the sending of a message.
100,096✔
2254
    //
52,272✔
2255
    // This function may lead to the destruction of the session object
98,844✔
2256
    // (suicide).
98,844✔
2257
    void send_message()
52,272✔
2258
    {
98,844✔
2259
        if (REALM_LIKELY(!unbind_message_received())) {
52,272✔
2260
            if (REALM_LIKELY(!error_occurred())) {
52,272✔
2261
                if (REALM_LIKELY(ident_message_received())) {
98,844✔
2262
                    // State is WaitForUnbind.
98,844✔
2263
                    bool relayed_alloc = (m_allocated_file_ident.ident != 0);
×
2264
                    if (REALM_LIKELY(!relayed_alloc)) {
×
2265
                        // Send DOWNLOAD or MARK.
×
2266
                        continue_history_scan(); // Throws
548✔
2267
                        // Session object may have been
1,252✔
2268
                        // destroyed at this point (suicide)
1,252✔
2269
                        return;
1,252✔
2270
                    }
36✔
2271
                    send_alloc_message(); // Throws
74✔
2272
                    return;
74✔
2273
                }
74✔
2274
                // State is SendIdent
1,432✔
2275
                send_ident_message(); // Throws
2,440✔
2276
                return;
2,440✔
2277
            }
2,440✔
2278
            // State is SendError
1,432✔
2279
            send_error_message(); // Throws
2,440✔
2280
            return;
2281
        }
2282
        // State is SendUnbound
2283
        send_unbound_message(); // Throws
5,054✔
2284
        terminate();            // Throws
5,054✔
2285
        m_connection.discard_session(m_session_ident);
392✔
2286
        // This session is now destroyed!
392✔
2287
    }
392✔
2288

392✔
2289
    bool receive_bind_message(std::string path, std::string signed_user_token, bool need_client_file_ident,
392✔
2290
                              bool is_subserver, ProtocolError& error)
2,800✔
2291
    {
5,054✔
2292
        if (logger.would_log(util::Logger::Level::info)) {
5,054✔
2293
            logger.detail("Received: BIND(server_path=%1, signed_user_token='%2', "
5,054✔
2294
                          "need_client_file_ident=%3, is_subserver=%4)",
2,800✔
2295
                          path, short_token_fmt(signed_user_token), int(need_client_file_ident),
5,054✔
2296
                          int(is_subserver)); // Throws
28✔
2297
        }
28✔
2298

28✔
2299
        ServerImpl& server = m_connection.get_server();
28✔
2300
        _impl::VirtualPathComponents virt_path_components =
28✔
2301
            _impl::parse_virtual_path(server.get_root_dir(), path); // Throws
28✔
2302

28✔
2303
        if (!virt_path_components.is_valid) {
2,786✔
2304
            logger.error("Bad virtual path (message_type='bind', path='%1', "
2,786✔
2305
                         "signed_user_token='%2')",
2,786✔
2306
                         path,
5,026✔
2307
                         short_token_fmt(signed_user_token)); // Throws
2,786✔
2308
            error = ProtocolError::illegal_realm_path;
5,026✔
2309
            return false;
2,786✔
2310
        }
5,026✔
2311

5,026✔
2312
        // The user has proper permissions at this stage.
5,026✔
2313

2,786✔
2314
        m_server_file = server.get_or_create_file(path); // Throws
5,026✔
2315

5,026✔
2316
        m_server_file->add_unidentified_session(this); // Throws
1,520✔
2317

2,872✔
2318
        logger.info("Client info: (path='%1', from=%2, protocol=%3) %4", path, m_connection.get_remote_endpoint(),
2,872✔
2319
                    m_connection.get_client_protocol_version(),
1,266✔
2320
                    m_connection.get_client_user_agent()); // Throws
1,266✔
2321

1,266✔
2322
        m_is_subserver = is_subserver;
2,154✔
2323
        if (REALM_LIKELY(!need_client_file_ident)) {
2,154✔
2324
            // Protocol state is now WaitForUnbind
2,154✔
2325
            return true;
2,154✔
2326
        }
1,266✔
2327

1,266✔
2328
        // FIXME: We must make a choice about client file ident for read only
2,154✔
2329
        // sessions. They should have a special read-only client file ident.
2,154✔
2330
        file_ident_type proxy_file = 0; // No proxy
2331
        ClientType client_type = (is_subserver ? ClientType::subserver : ClientType::regular);
2332
        m_file_ident_request = m_server_file->request_file_ident(*this, proxy_file, client_type); // Throws
2333
        m_send_ident_message = true;
2334
        // Protocol state is now AllocatingIdent
2335

3,998✔
2336
        return true;
2,008✔
2337
    }
3,998✔
2338

3,998✔
2339
    bool receive_ident_message(file_ident_type client_file_ident, salt_type client_file_ident_salt,
3,998✔
2340
                               version_type scan_server_version, version_type scan_client_version,
3,998✔
2341
                               version_type latest_server_version, salt_type latest_server_version_salt,
3,998✔
2342
                               ProtocolError& error)
3,998✔
2343
    {
2,008✔
2344
        // Protocol state must be WaitForIdent
3,998✔
2345
        REALM_ASSERT(!need_client_file_ident());
3,998✔
2346
        REALM_ASSERT(!m_send_ident_message);
3,998✔
2347
        REALM_ASSERT(!ident_message_received());
3,998✔
2348
        REALM_ASSERT(!unbind_message_received());
3,998✔
2349
        REALM_ASSERT(!error_occurred());
2,008✔
2350
        REALM_ASSERT(!m_error_message_sent);
3,998✔
2351

3,998✔
2352
        logger.debug("Received: IDENT(client_file_ident=%1, client_file_ident_salt=%2, "
3,998✔
2353
                     "scan_server_version=%3, scan_client_version=%4, latest_server_version=%5, "
3,998✔
2354
                     "latest_server_version_salt=%6)",
3,998✔
2355
                     client_file_ident, client_file_ident_salt, scan_server_version, scan_client_version,
3,998✔
2356
                     latest_server_version, latest_server_version_salt); // Throws
3,998✔
2357

3,998✔
2358
        SaltedFileIdent client_file_ident_2 = {client_file_ident, client_file_ident_salt};
3,998✔
2359
        DownloadCursor download_progress = {scan_server_version, scan_client_version};
3,998✔
2360
        SaltedVersion server_version_2 = {latest_server_version, latest_server_version_salt};
3,998✔
2361
        ClientType client_type = (m_is_subserver ? ClientType::subserver : ClientType::regular);
3,970✔
2362
        UploadCursor upload_threshold = {0, 0};
3,970✔
2363
        version_type locked_server_version = 0;
✔
2364
        BootstrapError error_2 =
×
2365
            m_server_file->bootstrap_client_session(client_file_ident_2, download_progress, server_version_2,
×
2366
                                                    client_type, upload_threshold, locked_server_version,
×
2367
                                                    logger); // Throws
✔
2368
        switch (error_2) {
×
2369
            case BootstrapError::no_error:
×
2370
                break;
×
2371
            case BootstrapError::client_file_expired:
×
2372
                logger.warn("Client (%1) expired", client_file_ident); // Throws
4✔
2373
                error = ProtocolError::client_file_expired;
4✔
2374
                return false;
4✔
2375
            case BootstrapError::bad_client_file_ident:
4✔
2376
                logger.error("Bad client file ident (%1) in IDENT message",
4✔
2377
                             client_file_ident); // Throws
✔
2378
                error = ProtocolError::bad_client_file_ident;
×
2379
                return false;
×
2380
            case BootstrapError::bad_client_file_ident_salt:
×
2381
                logger.error("Bad client file identifier salt (%1) in IDENT message",
4✔
2382
                             client_file_ident_salt); // Throws
4✔
2383
                error = ProtocolError::diverging_histories;
4✔
2384
                return false;
4✔
2385
            case BootstrapError::bad_download_server_version:
20✔
2386
                logger.error("Bad download progress server version in IDENT message"); // Throws
20✔
2387
                error = ProtocolError::bad_server_version;
20✔
2388
                return false;
20✔
2389
            case BootstrapError::bad_download_client_version:
4✔
2390
                logger.error("Bad download progress client version in IDENT message"); // Throws
4✔
2391
                error = ProtocolError::bad_client_version;
4✔
2392
                return false;
4✔
2393
            case BootstrapError::bad_server_version:
✔
2394
                logger.error("Bad server version (message_type='ident')"); // Throws
×
2395
                error = ProtocolError::bad_server_version;
×
2396
                return false;
2397
            case BootstrapError::bad_server_version_salt:
×
2398
                logger.error("Bad server version salt in IDENT message"); // Throws
3,970✔
2399
                error = ProtocolError::diverging_histories;
1,992✔
2400
                return false;
1,992✔
2401
            case BootstrapError::bad_client_type:
1,992✔
2402
                logger.error("Bad client type (%1) in IDENT message", int(client_type)); // Throws
3,970✔
2403
                error = ProtocolError::bad_client_file_ident; // FIXME: Introduce new protocol-level error
×
2404
                                                              // `bad_client_type`.
2405
                return false;
2406
        }
×
2407

×
2408
        // Make sure there is no other session currently associcated with the
×
2409
        // same client-side file
×
2410
        if (Session* other_sess = m_server_file->get_identified_session(client_file_ident)) {
×
2411
            SyncConnection& other_conn = other_sess->get_connection();
×
2412
            // It is a protocol violation if the other session is associated
2413
            // with the same connection
2414
            if (&other_conn == &m_connection) {
2415
                logger.error("Client file already bound in other session associated with "
2416
                             "the same connection"); // Throws
2417
                error = ProtocolError::bound_in_other_session;
2418
                return false;
2419
            }
2420
            // When the other session is associated with a different connection
2421
            // (`other_conn`), the clash may be due to the server not yet having
×
2422
            // realized that the other connection has been closed by the
×
2423
            // client. If so, the other connention is a "zombie". In the
×
2424
            // interest of getting rid of zombie connections as fast as
×
2425
            // possible, we shall assume that a clash with a session in another
1,992✔
2426
            // connection is always due to that other connection being a
3,970✔
2427
            // zombie. And when such a situation is detected, we want to close
1,992✔
2428
            // the zombie connection immediately.
3,970✔
2429
            auto log_level = Logger::Level::detail;
3,970✔
2430
            other_conn.terminate(log_level,
1,992✔
2431
                                 "Sync connection closed (superseded session)"); // Throws
3,970✔
2432
        }
1,992✔
2433

3,970✔
2434
        logger.info("Bound to client file (client_file_ident=%1)", client_file_ident); // Throws
3,970✔
2435

3,970✔
2436
        send_log_message(util::Logger::Level::debug, util::format("Session %1 bound to client file ident %2",
3,970✔
2437
                                                                  m_session_ident, client_file_ident));
1,992✔
2438

3,970✔
2439
        m_server_file->identify_session(this, client_file_ident); // Throws
3,970✔
2440

3,970✔
2441
        m_client_file_ident = client_file_ident;
1,992✔
2442
        m_download_progress = download_progress;
3,970✔
2443
        m_upload_threshold = upload_threshold;
×
2444
        m_locked_server_version = locked_server_version;
×
2445

×
2446
        ServerImpl& server = m_connection.get_server();
1,992✔
2447
        const Server::Config& config = server.get_config();
1,992✔
2448
        m_disable_download = (config.disable_download_for.count(client_file_ident) != 0);
3,970✔
2449

3,970✔
2450
        if (REALM_UNLIKELY(config.session_bootstrap_callback)) {
3,970✔
2451
            config.session_bootstrap_callback(m_server_file->get_virt_path(),
2452
                                              client_file_ident); // Throws
2453
        }
2454

2455
        // Protocol  state is now WaitForUnbind
44,720✔
2456
        enlist_to_send();
23,036✔
2457
        return true;
44,720✔
2458
    }
44,720✔
2459

44,720✔
2460
    bool receive_upload_message(version_type progress_client_version, version_type progress_server_version,
44,720✔
2461
                                version_type locked_server_version, const UploadChangesets& upload_changesets,
44,720✔
2462
                                ProtocolError& error)
23,036✔
2463
    {
44,720✔
2464
        // Protocol state must be WaitForUnbind
44,720✔
2465
        REALM_ASSERT(!m_send_ident_message);
44,720✔
2466
        REALM_ASSERT(ident_message_received());
44,720✔
2467
        REALM_ASSERT(!unbind_message_received());
23,036✔
2468
        REALM_ASSERT(!error_occurred());
23,036✔
2469
        REALM_ASSERT(!m_error_message_sent);
23,036✔
2470

23,036✔
2471
        logger.detail("Received: UPLOAD(progress_client_version=%1, progress_server_version=%2, "
23,036✔
2472
                      "locked_server_version=%3, num_changesets=%4)",
23,036✔
2473
                      progress_client_version, progress_server_version, locked_server_version,
23,036✔
2474
                      upload_changesets.size()); // Throws
23,036✔
2475

23,036✔
2476
        // We are unable to reproduce the cursor object for the upload progress
23,036✔
2477
        // when the protocol version is less than 29, because the client does
23,036✔
2478
        // not provide the required information. When the protocol version is
23,036✔
2479
        // less than 25, we can always get a consistent cursor by taking it from
23,036✔
2480
        // the changeset that was uploaded last, but in protocol versions 25,
23,036✔
2481
        // 26, 27, and 28, things are more complicated. Here, we receive new
23,036✔
2482
        // values for `last_integrated_server_version` which we cannot afford to
23,036✔
2483
        // ignore, but we do not know what client versions they correspond
23,036✔
2484
        // to. Fortunately, we can produce a cursor that works, and is mutually
23,036✔
2485
        // consistent with previous cursors, by simply bumping
23,036✔
2486
        // `upload_progress.client_version` when
23,036✔
2487
        // `upload_progress.last_intgerated_server_version` grows.
23,036✔
2488
        //
23,036✔
2489
        // To see that this scheme works, consider the last changeset, A, that
23,036✔
2490
        // will have already been uploaded and integrated at the beginning of
23,036✔
2491
        // the next session, and the first changeset, B, that follows A in the
23,036✔
2492
        // client side history, and is not upload skippable (of local origin and
23,036✔
2493
        // nonempty). We then need to show that A will be skipped, if uploaded
23,036✔
2494
        // in the next session, but B will not.
23,036✔
2495
        //
23,036✔
2496
        // Let V be the client version produced by A, and let T be the value of
23,036✔
2497
        // `upload_progress.client_version` as determined in this session, which
23,036✔
2498
        // is used as threshold in the next session. Then we know that A is
23,036✔
2499
        // skipped during the next session if V is less than, or equal to T. If
23,036✔
2500
        // the protocol version is at least 29, the protocol requires that T is
23,036✔
2501
        // greater than, or equal to V. If the protocol version is less than 25,
23,036✔
2502
        // T will be equal to V. Finally, if the protocol version is 25, 26, 27,
23,036✔
2503
        // or 28, we construct T such that it is always greater than, or equal
23,036✔
2504
        // to V, so in all cases, A will be skipped during the next session.
23,036✔
2505
        //
23,036✔
2506
        // Let W be the client version on which B is based. We then know that B
23,036✔
2507
        // will be retained if, and only if W is greater than, or equalto T. If
23,036✔
2508
        // the protocol version is at least 29, we know that T is less than, or
23,036✔
2509
        // equal to W, since B is not integrated until the next session. If the
23,036✔
2510
        // protocol version is less tahn 25, we know that T is V. Since V must
23,036✔
2511
        // be less than, or equal to W, we again know that T is less than, or
23,036✔
2512
        // equal to W. Finally, if the protocol version is 25, 26, 27, or 28, we
23,036✔
2513
        // construct T such that it is equal to V + N, where N is the number of
44,720✔
2514
        // observed increments in `last_integrated_server_version` since the
44,720✔
2515
        // client version prodiced by A. For each of these observed increments,
23,036✔
2516
        // there must have been a distinct new client version, but all these
44,720✔
2517
        // client versions must be less than, or equal to W, since B is not
44,720✔
2518
        // integrated until the next session. Therefore, we know that T = V + N
23,036✔
2519
        // is less than, or qual to W. So, in all cases, B will not skipped
23,036✔
2520
        // during the next session.
23,036✔
2521
        int protocol_version = m_connection.get_client_protocol_version();
44,720✔
2522
        static_cast<void>(protocol_version); // No protocol diversion (yet)
44,720✔
2523

×
2524
        UploadCursor upload_progress;
×
2525
        upload_progress = {progress_client_version, progress_server_version};
×
2526

×
2527
        // `upload_progress.client_version` must be nondecreasing across the
×
2528
        // session.
23,036✔
2529
        bool good_1 = (upload_progress.client_version >= m_upload_progress.client_version);
23,036✔
2530
        if (REALM_UNLIKELY(!good_1)) {
44,720✔
2531
            logger.error("Decreasing client version in upload progress (%1 < %2)", upload_progress.client_version,
44,720✔
2532
                         m_upload_progress.client_version); // Throws
×
2533
            error = ProtocolError::bad_client_version;
×
2534
            return false;
×
2535
        }
×
2536
        // `upload_progress.last_integrated_server_version` must be a version
×
2537
        // that the client can have heard about.
×
2538
        bool good_2 = (upload_progress.last_integrated_server_version <= m_download_progress.server_version);
23,036✔
2539
        if (REALM_UNLIKELY(!good_2)) {
23,036✔
2540
            logger.error("Bad last integrated server version in upload progress (%1 > %2)",
44,720✔
2541
                         upload_progress.last_integrated_server_version,
×
2542
                         m_download_progress.server_version); // Throws
×
2543
            error = ProtocolError::bad_server_version;
×
2544
            return false;
×
2545
        }
×
2546

23,036✔
2547
        // `upload_progress` must be consistent.
23,036✔
2548
        if (REALM_UNLIKELY(!is_consistent(upload_progress))) {
44,720✔
2549
            logger.error("Upload progress is inconsistent (%1, %2)", upload_progress.client_version,
×
2550
                         upload_progress.last_integrated_server_version); // Throws
×
2551
            error = ProtocolError::bad_server_version;
×
2552
            return false;
×
2553
        }
×
2554
        // `upload_progress` and `m_upload_threshold` must be mutually
×
2555
        // consistent.
×
2556
        if (REALM_UNLIKELY(!are_mutually_consistent(upload_progress, m_upload_threshold))) {
×
2557
            logger.error("Upload progress (%1, %2) is mutually inconsistent with "
23,036✔
2558
                         "threshold (%3, %4)",
23,036✔
2559
                         upload_progress.client_version, upload_progress.last_integrated_server_version,
44,720✔
2560
                         m_upload_threshold.client_version,
×
2561
                         m_upload_threshold.last_integrated_server_version); // Throws
×
2562
            error = ProtocolError::bad_server_version;
×
2563
            return false;
×
2564
        }
×
2565
        // `upload_progress` and `m_upload_progress` must be mutually
×
2566
        // consistent.
×
2567
        if (REALM_UNLIKELY(!are_mutually_consistent(upload_progress, m_upload_progress))) {
×
2568
            logger.error("Upload progress (%1, %2) is mutually inconsistent with previous "
23,036✔
2569
                         "upload progress (%3, %4)",
44,720✔
2570
                         upload_progress.client_version, upload_progress.last_integrated_server_version,
23,036✔
2571
                         m_upload_progress.client_version,
23,036✔
2572
                         m_upload_progress.last_integrated_server_version); // Throws
23,036✔
2573
            error = ProtocolError::bad_server_version;
44,720✔
2574
            return false;
×
2575
        }
×
2576

×
2577
        version_type locked_server_version_2 = locked_server_version;
×
2578

×
2579
        // `locked_server_version_2` must be nondecreasing over the lifetime of
23,036✔
2580
        // the client-side file.
23,036✔
2581
        if (REALM_UNLIKELY(locked_server_version_2 < m_locked_server_version)) {
44,720✔
2582
            logger.error("Decreasing locked server version (%1 < %2)", locked_server_version_2,
×
2583
                         m_locked_server_version); // Throws
×
2584
            error = ProtocolError::bad_server_version;
×
2585
            return false;
×
2586
        }
×
2587
        // `locked_server_version_2` must be a version that the client can have
23,036✔
2588
        // heard about.
44,720✔
2589
        if (REALM_UNLIKELY(locked_server_version_2 > m_download_progress.server_version)) {
44,720✔
2590
            logger.error("Bad locked server version (%1 > %2)", locked_server_version_2,
23,422✔
2591
                         m_download_progress.server_version); // Throws
36,850✔
2592
            error = ProtocolError::bad_server_version;
17,670✔
2593
            return false;
17,670✔
2594
        }
17,670✔
2595

17,670✔
2596
        std::size_t num_previously_integrated_changesets = 0;
36,850✔
2597
        if (!upload_changesets.empty()) {
×
2598
            UploadCursor up = m_upload_progress;
×
2599
            for (const ServerProtocol::UploadChangeset& uc : upload_changesets) {
×
2600
                // `uc.upload_cursor.client_version` must be increasing across
×
2601
                // all the changesets in this UPLOAD message, and all must be
×
2602
                // greater than upload_progress.client_version of previous
×
2603
                // UPLOAD message.
×
2604
                if (REALM_UNLIKELY(uc.upload_cursor.client_version <= up.client_version)) {
17,670✔
2605
                    logger.error("Nonincreasing client version in upload cursor of uploaded "
36,850✔
2606
                                 "changeset (%1 <= %2)",
×
2607
                                 uc.upload_cursor.client_version,
×
2608
                                 up.client_version); // Throws
×
2609
                    error = ProtocolError::bad_client_version;
×
2610
                    return false;
×
2611
                }
×
2612
                // `uc.upload_progress` must be consistent.
17,670✔
2613
                if (REALM_UNLIKELY(!is_consistent(uc.upload_cursor))) {
17,670✔
2614
                    logger.error("Upload cursor of uploaded changeset is inconsistent (%1, %2)",
36,850✔
2615
                                 uc.upload_cursor.client_version,
×
2616
                                 uc.upload_cursor.last_integrated_server_version); // Throws
×
2617
                    error = ProtocolError::bad_server_version;
×
2618
                    return false;
×
2619
                }
×
2620
                // `uc.upload_progress` must be mutually consistent with
×
2621
                // previous upload cursor.
×
2622
                if (REALM_UNLIKELY(!are_mutually_consistent(uc.upload_cursor, up))) {
17,670✔
2623
                    logger.error("Upload cursor of uploaded changeset (%1, %2) is mutually "
17,670✔
2624
                                 "inconsistent with previous upload cursor (%3, %4)",
17,670✔
2625
                                 uc.upload_cursor.client_version, uc.upload_cursor.last_integrated_server_version,
17,670✔
2626
                                 up.client_version, up.last_integrated_server_version); // Throws
17,670✔
2627
                    error = ProtocolError::bad_server_version;
36,850✔
2628
                    return false;
36,850✔
2629
                }
×
2630
                // `uc.upload_progress` must be mutually consistent with
×
2631
                // threshold, that is, for changesets that have not previously
×
2632
                // been integrated, it is important that the specified value of
×
2633
                // `last_integrated_server_version` is greater than, or equal to
×
2634
                // the reciprocal history base version.
×
2635
                bool consistent_with_threshold = are_mutually_consistent(uc.upload_cursor, m_upload_threshold);
×
2636
                if (REALM_UNLIKELY(!consistent_with_threshold)) {
×
2637
                    logger.error("Upload cursor of uploaded changeset (%1, %2) is mutually "
36,850✔
2638
                                 "inconsistent with threshold (%3, %4)",
36,850✔
2639
                                 uc.upload_cursor.client_version, uc.upload_cursor.last_integrated_server_version,
2,458✔
2640
                                 m_upload_threshold.client_version,
36,850✔
2641
                                 m_upload_threshold.last_integrated_server_version); // Throws
36,850✔
2642
                    error = ProtocolError::bad_server_version;
12,346✔
2643
                    return false;
12,346✔
2644
                }
12,346✔
2645
                bool previously_integrated = (uc.upload_cursor.client_version <= m_upload_threshold.client_version);
23,422✔
2646
                if (previously_integrated)
×
2647
                    ++num_previously_integrated_changesets;
×
2648
                up = uc.upload_cursor;
×
2649
            }
×
2650
            // `upload_progress.client_version` must be greater than, or equal
×
2651
            // to client versions produced by each of the changesets in this
×
2652
            // UPLOAD message.
×
2653
            if (REALM_UNLIKELY(up.client_version > upload_progress.client_version)) {
12,346✔
2654
                logger.error("Upload progress less than client version produced by uploaded "
12,346✔
2655
                             "changeset (%1 > %2)",
23,422✔
2656
                             up.client_version,
×
2657
                             upload_progress.client_version); // Throws
×
2658
                error = ProtocolError::bad_client_version;
×
2659
                return false;
×
2660
            }
×
2661
            // The upload cursor of last uploaded changeset must be mutually
×
2662
            // consistent with the reported upload progress.
×
2663
            if (REALM_UNLIKELY(!are_mutually_consistent(up, upload_progress))) {
44,720✔
2664
                logger.error("Upload cursor (%1, %2) of last uploaded changeset is mutually "
23,036✔
2665
                             "inconsistent with upload progress (%3, %4)",
23,036✔
2666
                             up.client_version, up.last_integrated_server_version, upload_progress.client_version,
23,036✔
2667
                             upload_progress.last_integrated_server_version); // Throws
44,720✔
2668
                error = ProtocolError::bad_server_version;
×
2669
                return false;
2670
            }
2671
        }
2672

×
2673
        // FIXME: Part of a very poor man's substitute for a proper backpressure
×
2674
        // scheme.
×
2675
        if (REALM_UNLIKELY(!m_server_file->can_add_changesets_from_downstream())) {
23,036✔
2676
            logger.debug("Terminating uploading session because buffer is full"); // Throws
44,720✔
2677
            // Using this exact error code, because it causes `try_again` flag
23,036✔
2678
            // to be set to true, which causes the client to wait for about 5
44,720✔
2679
            // minuites before trying to connect again.
44,720✔
2680
            error = ProtocolError::connection_closed;
23,036✔
2681
            return false;
44,720✔
2682
        }
44,720✔
2683

23,036✔
2684
        m_upload_progress = upload_progress;
44,720✔
2685

44,720✔
2686
        bool have_real_upload_progress = (upload_progress.client_version > m_upload_threshold.client_version);
200✔
2687
        bool bump_locked_server_version = (locked_server_version_2 > m_locked_server_version);
22,932✔
2688

44,520✔
2689
        std::size_t num_changesets_to_integrate = upload_changesets.size() - num_previously_integrated_changesets;
×
2690
        REALM_ASSERT(have_real_upload_progress || num_changesets_to_integrate == 0);
22,932✔
2691

44,520✔
2692
        bool have_anything_to_do = (have_real_upload_progress || bump_locked_server_version);
704✔
2693
        if (!have_anything_to_do)
704✔
2694
            return true;
704✔
2695

44,520✔
2696
        if (!have_real_upload_progress)
23,086✔
2697
            upload_progress = m_upload_threshold;
23,086✔
2698

23,086✔
2699
        if (num_previously_integrated_changesets > 0) {
22,932✔
2700
            logger.detail("Ignoring %1 previously integrated changesets",
44,520✔
2701
                          num_previously_integrated_changesets); // Throws
44,520✔
2702
        }
44,520✔
2703
        if (num_changesets_to_integrate > 0) {
44,520✔
2704
            logger.detail("Initiate integration of %1 remote changesets",
44,520✔
2705
                          num_changesets_to_integrate); // Throws
22,932✔
2706
        }
44,520✔
2707

44,520✔
2708
        REALM_ASSERT(m_server_file);
44,520✔
2709
        ServerFile& file = *m_server_file;
2710
        std::size_t offset = num_previously_integrated_changesets;
2711
        file.add_changesets_from_downstream(m_client_file_ident, upload_progress, locked_server_version_2,
12,086✔
2712
                                            upload_changesets.data() + offset, num_changesets_to_integrate); // Throws
5,982✔
2713

12,086✔
2714
        m_locked_server_version = locked_server_version_2;
12,086✔
2715
        return true;
12,086✔
2716
    }
12,086✔
2717

12,086✔
2718
    bool receive_mark_message(request_ident_type request_ident, ProtocolError&)
5,982✔
2719
    {
12,086✔
2720
        // Protocol state must be WaitForUnbind
5,982✔
2721
        REALM_ASSERT(!m_send_ident_message);
12,086✔
2722
        REALM_ASSERT(ident_message_received());
5,982✔
2723
        REALM_ASSERT(!unbind_message_received());
12,086✔
2724
        REALM_ASSERT(!error_occurred());
12,086✔
2725
        REALM_ASSERT(!m_error_message_sent);
12,086✔
2726

2727
        logger.debug("Received: MARK(request_ident=%1)", request_ident); // Throws
2728

2729
        m_download_completion_request = request_ident;
2730

2731
        ensure_enlisted_to_send();
2732
        return true;
2733
    }
2,476✔
2734

1,452✔
2735
    // Returns true if the deactivation process has been completed, at which
2,476✔
2736
    // point the caller (SyncConnection::receive_unbind_message()) should
1,452✔
2737
    // terminate the session.
2,476✔
2738
    //
1,452✔
2739
    // CAUTION: This function may commit suicide!
2,476✔
2740
    void receive_unbind_message()
2,476✔
2741
    {
1,452✔
2742
        // Protocol state may be anything but SendUnbound
1,452✔
2743
        REALM_ASSERT(!m_unbind_message_received);
2,476✔
2744

14✔
2745
        logger.detail("Received: UNBIND"); // Throws
26✔
2746

26✔
2747
        detach_from_server_file();
14✔
2748
        m_unbind_message_received = true;
26✔
2749

26✔
2750
        // Detect completion of the deactivation process
1,438✔
2751
        if (m_error_message_sent) {
1,438✔
2752
            // Deactivation process completed
2,450✔
2753
            terminate(); // Throws
2,450✔
2754
            m_connection.discard_session(m_session_ident);
2755
            // This session is now destroyed!
2756
            return;
×
2757
        }
×
2758

2759
        // Protocol state is now SendUnbound
×
2760
        ensure_enlisted_to_send();
×
2761
    }
2762

2763
    void receive_error_message(session_ident_type, int, std::string_view)
2764
    {
2765
        REALM_ASSERT(!m_unbind_message_received);
2766

2767
        logger.detail("Received: ERROR"); // Throws
2768
    }
2769

2770
private:
2771
    SyncConnection& m_connection;
2772

2773
    const session_ident_type m_session_ident;
2774

2775
    // Not null if, and only if this session is in
2776
    // m_connection.m_sessions_enlisted_to_send.
2777
    Session* m_next = nullptr;
2778

2779
    // Becomes nonnull when the BIND message is received, if no error occurs. Is
2780
    // reset to null when the deactivation process is initiated, either when the
2781
    // UNBIND message is recieved, or when initiate_deactivation() is called.
2782
    util::bind_ptr<ServerFile> m_server_file;
2783

2784
    bool m_disable_download = false;
2785
    bool m_is_subserver = false;
2786

2787
    using file_ident_request_type = ServerFile::file_ident_request_type;
2788

2789
    // When nonzero, this session has an outstanding request for a client file
2790
    // identifier.
2791
    file_ident_request_type m_file_ident_request = 0;
2792

2793
    // Payload for next outgoing ALLOC message.
2794
    SaltedFileIdent m_allocated_file_ident = {0, 0};
2795

2796
    // Zero until the session receives an IDENT message from the client.
2797
    file_ident_type m_client_file_ident = 0;
2798

2799
    // Zero until initiate_deactivation() is called.
2800
    ProtocolError m_error_code = {};
2801

2802
    // The current point of progression of the download process. Set to (<server
2803
    // version>, <client version>) of the IDENT message when the IDENT message
2804
    // is received. At the time of return from continue_history_scan(), it
2805
    // points to the latest server version such that all preceding changesets in
2806
    // the server-side history have been downloaded, are currently being
2807
    // downloaded, or are *download excluded*.
2808
    DownloadCursor m_download_progress = {0, 0};
2809

2810
    request_ident_type m_download_completion_request = 0;
2811

2812
    // Records the progress of the upload process. Used to check that the client
2813
    // uploads changesets in order. Also, when m_upload_progress >
2814
    // m_upload_threshold, m_upload_progress works as a cache of the persisted
2815
    // version of the upload progress.
2816
    UploadCursor m_upload_progress = {0, 0};
2817

2818
    // Initialized on reception of the IDENT message. Specifies the actual
2819
    // upload progress (as recorded on the server-side) at the beginning of the
2820
    // session, and it remains fixed throughout the session.
2821
    //
2822
    // m_upload_threshold includes the progress resulting from the received
2823
    // changesets that have not yet been integrated (only relevant for
2824
    // synchronous backup).
2825
    UploadCursor m_upload_threshold = {0, 0};
2826

2827
    // Works partially as a cache of the persisted value, and partially as a way
2828
    // of checking that the client respects that it can never decrease.
2829
    version_type m_locked_server_version = 0;
2830

2831
    bool m_send_ident_message = false;
2832
    bool m_unbind_message_received = false;
2833
    bool m_error_message_sent = false;
2834

2835
    /// m_one_download_message_sent denotes whether at least one DOWNLOAD message
5,052✔
2836
    /// has been sent in the current session. The variable is used to ensure
5,052✔
2837
    /// that a DOWNLOAD message is always sent in a session. The received
5,052✔
2838
    /// DOWNLOAD message is needed by the client to ensure that its current
5,052✔
2839
    /// download progress is up to date.
5,052✔
2840
    bool m_one_download_message_sent = false;
5,052✔
2841

2842
    static std::string make_logger_prefix(session_ident_type session_ident)
2843
    {
2844
        std::ostringstream out;
2845
        out.imbue(std::locale::classic());
2846
        out << "Session[" << session_ident << "]: "; // Throws
2847
        return out.str();                            // Throws
2848
    }
2849

2850
    // Scan the history for changesets to be downloaded.
2851
    // If the history is longer than the end point of the previous scan,
2852
    // a DOWNLOAD message will be sent.
98,844✔
2853
    // A MARK message is sent if no DOWNLOAD message is sent, and the client has
52,272✔
2854
    // requested to be notified about download completion.
98,844✔
2855
    // In case neither a DOWNLOAD nor a MARK is sent, no message is sent.
98,844✔
2856
    //
98,844✔
2857
    // This function may lead to the destruction of the session object
98,844✔
2858
    // (suicide).
98,844✔
2859
    void continue_history_scan()
98,844✔
2860
    {
52,272✔
2861
        // Protocol state must be WaitForUnbind
98,844✔
2862
        REALM_ASSERT(!m_send_ident_message);
98,844✔
2863
        REALM_ASSERT(ident_message_received());
52,272✔
2864
        REALM_ASSERT(!unbind_message_received());
98,844✔
2865
        REALM_ASSERT(!error_occurred());
98,844✔
2866
        REALM_ASSERT(!m_error_message_sent);
98,844✔
2867
        REALM_ASSERT(!is_enlisted_to_send());
52,272✔
2868

52,272✔
2869
        SaltedVersion last_server_version = m_server_file->get_salted_sync_version();
98,844✔
2870
        REALM_ASSERT(last_server_version.version >= m_download_progress.server_version);
98,844✔
2871

98,844✔
2872
        ServerImpl& server = m_connection.get_server();
38,130✔
2873
        const Server::Config& config = server.get_config();
38,130✔
2874
        if (REALM_UNLIKELY(m_disable_download))
38,130✔
2875
            return;
38,130✔
2876

38,130✔
2877
        bool have_more_to_scan =
38,130✔
2878
            (last_server_version.version > m_download_progress.server_version || !m_one_download_message_sent);
38,130✔
2879
        if (have_more_to_scan) {
38,130✔
2880
            m_server_file->register_client_access(m_client_file_ident);     // Throws
38,130✔
2881
            const ServerHistory& history = m_server_file->access().history; // Throws
38,130✔
2882
            const char* body;
38,130✔
2883
            std::size_t uncompressed_body_size;
38,130✔
2884
            std::size_t compressed_body_size = 0;
38,130✔
2885
            bool body_is_compressed = false;
38,130✔
2886
            version_type end_version = last_server_version.version;
38,130✔
2887
            DownloadCursor download_progress;
38,130!
2888
            UploadCursor upload_progress = {0, 0};
20,466!
2889
            std::uint_fast64_t downloadable_bytes = 0;
38,130✔
2890
            std::size_t num_changesets;
38,130!
2891
            std::size_t accum_original_size;
38,130✔
2892
            std::size_t accum_compacted_size;
×
2893
            ServerProtocol& protocol = get_server_protocol();
×
2894
            bool disable_download_compaction = config.disable_download_compaction;
×
2895
            bool enable_cache = (config.enable_download_bootstrap_cache && m_download_progress.server_version == 0 &&
×
2896
                                 m_upload_progress.client_version == 0 && m_upload_threshold.client_version == 0);
×
2897
            DownloadCache& cache = m_server_file->get_download_cache();
×
2898
            bool fetch_from_cache = (enable_cache && cache.body && end_version == cache.end_version);
×
2899
            if (fetch_from_cache) {
×
2900
                body = cache.body.get();
×
2901
                uncompressed_body_size = cache.uncompressed_body_size;
×
2902
                compressed_body_size = cache.compressed_body_size;
38,130✔
2903
                body_is_compressed = cache.body_is_compressed;
20,466✔
2904
                download_progress = cache.download_progress;
20,466✔
2905
                downloadable_bytes = cache.downloadable_bytes;
20,466✔
2906
                num_changesets = cache.num_changesets;
20,466✔
2907
                accum_original_size = cache.accum_original_size;
38,130✔
2908
                accum_compacted_size = cache.accum_compacted_size;
×
2909
            }
20,466✔
2910
            else {
38,130✔
2911
                // Discard the old cached DOWNLOAD body before generating a new
38,130✔
2912
                // one to be cached. This can make a big difference because the
38,130✔
2913
                // size of that body can be very large (10GiB has been seen in a
38,130✔
2914
                // real-world case).
38,128✔
2915
                if (enable_cache)
38,128✔
2916
                    cache.body = {};
38,128✔
2917

38,128✔
2918
                OutputBuffer& out = server.get_misc_buffers().download_message;
38,128✔
2919
                out.reset();
38,128✔
2920
                download_progress = m_download_progress;
38,128✔
2921
                auto fetch_and_compress = [&](std::size_t max_download_size) {
38,128✔
2922
                    DownloadHistoryEntryHandler handler{protocol, out, logger};
38,128✔
2923
                    std::uint_fast64_t cumulative_byte_size_current;
38,128✔
2924
                    std::uint_fast64_t cumulative_byte_size_total;
×
2925
                    bool not_expired = history.fetch_download_info(
×
2926
                        m_client_file_ident, download_progress, end_version, upload_progress, handler,
×
2927
                        cumulative_byte_size_current, cumulative_byte_size_total, disable_download_compaction,
2928
                        max_download_size); // Throws
2929
                    REALM_ASSERT(upload_progress.client_version >= download_progress.last_integrated_client_version);
×
2930
                    SyncConnection& conn = get_connection();
×
2931
                    if (REALM_UNLIKELY(!not_expired)) {
20,464✔
2932
                        logger.debug("History scanning failed: Client file entry "
38,128✔
2933
                                     "expired during session"); // Throws
38,128✔
2934
                        conn.protocol_error(ProtocolError::client_file_expired, this);
38,128✔
2935
                        // Session object may have been destroyed at this point
38,128✔
2936
                        // (suicide).
38,128✔
2937
                        return false;
38,128✔
2938
                    }
4,282✔
2939

4,282✔
2940
                    downloadable_bytes = cumulative_byte_size_total - cumulative_byte_size_current;
4,282✔
2941
                    uncompressed_body_size = out.size();
4,282✔
2942
                    BinaryData uncompressed = {out.data(), uncompressed_body_size};
4,282✔
2943
                    body = uncompressed.data();
4,282✔
2944
                    std::size_t max_uncompressed = 1024;
4,282✔
2945
                    if (uncompressed.size() > max_uncompressed) {
4,282✔
2946
                        compression::CompressMemoryArena& arena = server.get_compress_memory_arena();
4,282✔
2947
                        std::vector<char>& buffer = server.get_misc_buffers().compress;
38,128✔
2948
                        compression::allocate_and_compress(arena, uncompressed, buffer); // Throws
38,128✔
2949
                        if (buffer.size() < uncompressed.size()) {
38,128✔
2950
                            body = buffer.data();
38,128✔
2951
                            compressed_body_size = buffer.size();
38,128✔
2952
                            body_is_compressed = true;
38,130✔
2953
                        }
×
2954
                    }
×
2955
                    num_changesets = handler.num_changesets;
2956
                    accum_original_size = handler.accum_original_size;
2957
                    accum_compacted_size = handler.accum_compacted_size;
×
2958
                    return true;
×
2959
                };
×
2960
                if (enable_cache) {
×
2961
                    std::size_t max_download_size = std::numeric_limits<size_t>::max();
×
2962
                    if (!fetch_and_compress(max_download_size)) { // Throws
×
2963
                        // Session object may have been destroyed at this point
×
2964
                        // (suicide).
×
2965
                        return;
×
2966
                    }
×
2967
                    REALM_ASSERT(upload_progress.client_version == 0);
×
2968
                    std::size_t body_size = (body_is_compressed ? compressed_body_size : uncompressed_body_size);
×
2969
                    cache.body = std::make_unique<char[]>(body_size); // Throws
×
2970
                    std::copy(body, body + body_size, cache.body.get());
×
2971
                    cache.uncompressed_body_size = uncompressed_body_size;
×
2972
                    cache.compressed_body_size = compressed_body_size;
×
2973
                    cache.body_is_compressed = body_is_compressed;
38,130✔
2974
                    cache.end_version = end_version;
38,130✔
2975
                    cache.download_progress = download_progress;
38,130✔
2976
                    cache.downloadable_bytes = downloadable_bytes;
2977
                    cache.num_changesets = num_changesets;
2978
                    cache.accum_original_size = accum_original_size;
×
2979
                    cache.accum_compacted_size = accum_compacted_size;
×
2980
                }
38,130✔
2981
                else {
38,130✔
2982
                    std::size_t max_download_size = config.max_download_size;
20,466✔
2983
                    if (!fetch_and_compress(max_download_size)) { // Throws
38,130✔
2984
                        // Session object may have been destroyed at this point
38,130✔
2985
                        // (suicide).
38,130✔
2986
                        return;
38,130✔
2987
                    }
38,130✔
2988
                }
38,130✔
2989
            }
38,130✔
2990

20,466✔
2991
            OutputBuffer& out = m_connection.get_output_buffer();
38,130✔
2992
            protocol.make_download_message(
38,130✔
2993
                m_connection.get_client_protocol_version(), out, m_session_ident, download_progress.server_version,
29,740✔
2994
                download_progress.last_integrated_client_version, last_server_version.version,
38,130✔
2995
                last_server_version.salt, upload_progress.client_version,
38,130✔
2996
                upload_progress.last_integrated_server_version, downloadable_bytes, num_changesets, body,
20,466✔
2997
                uncompressed_body_size, compressed_body_size, body_is_compressed, logger); // Throws
38,130✔
2998

38,130✔
2999
            if (!disable_download_compaction) {
38,130✔
3000
                std::size_t saved = accum_original_size - accum_compacted_size;
38,130✔
3001
                double saved_2 = (accum_original_size == 0 ? 0 : std::round(saved * 100.0 / accum_original_size));
38,130✔
3002
                logger.detail("Download compaction: Saved %1 bytes (%2%%)", saved, saved_2); // Throws
20,466✔
3003
            }
38,130✔
3004

38,130✔
3005
            m_download_progress = download_progress;
60,714✔
3006
            logger.debug("Setting of m_download_progress.server_version = %1",
5,978✔
3007
                         m_download_progress.server_version); // Throws
12,078✔
3008
            send_download_message();
12,078✔
3009
            m_one_download_message_sent = true;
12,078✔
3010

12,078✔
3011
            enlist_to_send();
12,078✔
3012
        }
98,844✔
3013
        else if (m_download_completion_request) {
3014
            // Send a MARK message
3015
            request_ident_type request_ident = m_download_completion_request;
1,252✔
3016
            send_mark_message(request_ident);  // Throws
548✔
3017
            m_download_completion_request = 0; // Request handled
1,252✔
3018
            enlist_to_send();
1,252✔
3019
        }
1,252✔
3020
    }
1,252✔
3021

1,252✔
3022
    void send_ident_message()
1,252✔
3023
    {
548✔
3024
        // Protocol state must be SendIdent
1,252✔
3025
        REALM_ASSERT(!need_client_file_ident());
548✔
3026
        REALM_ASSERT(m_send_ident_message);
1,252✔
3027
        REALM_ASSERT(!ident_message_received());
1,252✔
3028
        REALM_ASSERT(!unbind_message_received());
548✔
3029
        REALM_ASSERT(!error_occurred());
1,252✔
3030
        REALM_ASSERT(!m_error_message_sent);
1,252✔
3031

548✔
3032
        REALM_ASSERT(m_allocated_file_ident.ident != 0);
1,252✔
3033

1,252✔
3034
        file_ident_type client_file_ident = m_allocated_file_ident.ident;
1,252✔
3035
        salt_type client_file_ident_salt = m_allocated_file_ident.salt;
1,252✔
3036

1,252✔
3037
        logger.debug("Sending: IDENT(client_file_ident=%1, client_file_ident_salt=%2)", client_file_ident,
1,252✔
3038
                     client_file_ident_salt); // Throws
548✔
3039

1,252✔
3040
        ServerProtocol& protocol = get_server_protocol();
1,252✔
3041
        OutputBuffer& out = m_connection.get_output_buffer();
548✔
3042
        int protocol_version = m_connection.get_client_protocol_version();
1,252✔
3043
        protocol.make_ident_message(protocol_version, out, m_session_ident, client_file_ident,
3044
                                    client_file_ident_salt); // Throws
3045
        m_connection.initiate_write_output_buffer();         // Throws
38,130✔
3046

38,130✔
3047
        m_allocated_file_ident.ident = 0; // Consumed
38,130✔
3048
        m_send_ident_message = false;
3049
        // Protocol state is now WaitForStateRequest or WaitForIdent
3050
    }
12,078✔
3051

12,078✔
3052
    void send_download_message()
5,978✔
3053
    {
12,078✔
3054
        m_connection.initiate_write_output_buffer(); // Throws
12,078✔
3055
    }
12,078✔
3056

12,078✔
3057
    void send_mark_message(request_ident_type request_ident)
12,078✔
3058
    {
3059
        logger.debug("Sending: MARK(request_ident=%1)", request_ident); // Throws
3060

×
3061
        ServerProtocol& protocol = get_server_protocol();
3062
        OutputBuffer& out = m_connection.get_output_buffer();
×
3063
        protocol.make_mark_message(out, m_session_ident, request_ident); // Throws
×
3064
        m_connection.initiate_write_output_buffer();                     // Throws
×
3065
    }
×
3066

×
3067
    void send_alloc_message()
3068
    {
×
3069
        // Protocol state must be WaitForUnbind
3070
        REALM_ASSERT(!m_send_ident_message);
3071
        REALM_ASSERT(ident_message_received());
×
3072
        REALM_ASSERT(!unbind_message_received());
3073
        REALM_ASSERT(!error_occurred());
×
3074
        REALM_ASSERT(!m_error_message_sent);
3075

×
3076
        REALM_ASSERT(m_allocated_file_ident.ident != 0);
3077

×
3078
        // Relayed allocations are only allowed from protocol version 23 (old protocol).
×
3079
        REALM_ASSERT(false);
×
3080

×
3081
        file_ident_type file_ident = m_allocated_file_ident.ident;
3082

×
3083
        logger.debug("Sending: ALLOC(file_ident=%1)", file_ident); // Throws
3084

3085
        ServerProtocol& protocol = get_server_protocol();
×
3086
        OutputBuffer& out = m_connection.get_output_buffer();
×
3087
        protocol.make_alloc_message(out, m_session_ident, file_ident); // Throws
3088
        m_connection.initiate_write_output_buffer();                   // Throws
3089

2,442✔
3090
        m_allocated_file_ident.ident = 0; // Consumed
1,432✔
3091

2,442✔
3092
        // Other messages may be waiting to be sent.
2,442✔
3093
        enlist_to_send();
1,432✔
3094
    }
2,442✔
3095

1,432✔
3096
    void send_unbound_message()
2,442✔
3097
    {
2,442✔
3098
        // Protocol state must be SendUnbound
2,442✔
3099
        REALM_ASSERT(unbind_message_received());
2,442✔
3100
        REALM_ASSERT(!m_error_message_sent);
2,442✔
3101

3102
        logger.debug("Sending: UNBOUND"); // Throws
3103

74✔
3104
        ServerProtocol& protocol = get_server_protocol();
36✔
3105
        OutputBuffer& out = m_connection.get_output_buffer();
74✔
3106
        protocol.make_unbound_message(out, m_session_ident); // Throws
74✔
3107
        m_connection.initiate_write_output_buffer();         // Throws
74✔
3108
    }
36✔
3109

74✔
3110
    void send_error_message()
36✔
3111
    {
74✔
3112
        // Protocol state must be SendError
74✔
3113
        REALM_ASSERT(!unbind_message_received());
74✔
3114
        REALM_ASSERT(error_occurred());
74✔
3115
        REALM_ASSERT(!m_error_message_sent);
36✔
3116

74✔
3117
        REALM_ASSERT(is_session_level_error(m_error_code));
74✔
3118

36✔
3119
        ProtocolError error_code = m_error_code;
74✔
3120
        const char* message = get_protocol_error_message(int(error_code));
74✔
3121
        std::size_t message_size = std::strlen(message);
74✔
3122
        bool try_again = determine_try_again(error_code);
74✔
3123

74✔
3124
        logger.detail("Sending: ERROR(error_code=%1, message_size=%2, try_again=%3)", int(error_code), message_size,
74✔
3125
                      try_again); // Throws
36✔
3126

74✔
3127
        ServerProtocol& protocol = get_server_protocol();
36✔
3128
        OutputBuffer& out = m_connection.get_output_buffer();
74✔
3129
        int protocol_version = m_connection.get_client_protocol_version();
3130
        protocol.make_error_message(protocol_version, out, error_code, message, message_size, try_again,
3131
                                    m_session_ident); // Throws
3,970✔
3132
        m_connection.initiate_write_output_buffer();  // Throws
3,970✔
3133

×
3134
        m_error_message_sent = true;
×
3135
        // Protocol state is now WaitForUnbindErr
1,992✔
3136
    }
3,970✔
3137

3,970✔
3138
    void send_log_message(util::Logger::Level level, const std::string&& message)
3139
    {
3140
        if (m_connection.get_client_protocol_version() < SyncConnection::SERVER_LOG_PROTOCOL_VERSION) {
3141
            return logger.log(level, message.c_str());
7,604✔
3142
        }
7,604✔
3143

2,578✔
3144
        m_connection.send_log_message(level, std::move(message), m_session_ident);
5,026✔
3145
    }
5,026✔
3146

3,970✔
3147
    // Idempotent
3,970✔
3148
    void detach_from_server_file() noexcept
1,056✔
3149
    {
1,056✔
3150
        if (!m_server_file)
1,056✔
3151
            return;
5,026✔
3152
        ServerFile& file = *m_server_file;
902✔
3153
        if (ident_message_received()) {
5,026✔
3154
            file.remove_identified_session(m_client_file_ident);
5,026✔
3155
        }
3156
        else {
3157
            file.remove_unidentified_session(this);
3158
        }
3159
        if (m_file_ident_request != 0)
3160
            file.cancel_file_ident_request(m_file_ident_request);
3161
        m_server_file.reset();
3162
    }
3163

102,780✔
3164
    friend class SessionQueue;
102,780✔
3165
};
102,780✔
3166

37,982✔
3167

37,982✔
3168
// ============================ SessionQueue implementation ============================
37,982✔
3169

64,798✔
3170
void SessionQueue::push_back(Session* sess) noexcept
64,798✔
3171
{
64,798✔
3172
    REALM_ASSERT(!sess->m_next);
102,780✔
3173
    if (m_back) {
102,780✔
3174
        sess->m_next = m_back->m_next;
3175
        m_back->m_next = sess;
3176
    }
3177
    else {
147,838✔
3178
        sess->m_next = sess;
147,838✔
3179
    }
147,838✔
3180
    m_back = sess;
102,612✔
3181
}
102,612✔
3182

37,918✔
3183

37,918✔
3184
Session* SessionQueue::pop_front() noexcept
64,694✔
3185
{
64,694✔
3186
    Session* sess = nullptr;
64,694✔
3187
    if (m_back) {
102,612✔
3188
        sess = m_back->m_next;
102,612✔
3189
        if (sess != m_back) {
147,838✔
3190
            m_back->m_next = sess->m_next;
147,838✔
3191
        }
3192
        else {
3193
            m_back = nullptr;
3194
        }
2,988✔
3195
        sess->m_next = nullptr;
2,988✔
3196
    }
102✔
3197
    return sess;
168✔
3198
}
168✔
3199

168✔
3200

168✔
3201
void SessionQueue::clear() noexcept
102✔
3202
{
66✔
3203
    if (m_back) {
66✔
3204
        Session* sess = m_back;
102✔
3205
        for (;;) {
102✔
3206
            Session* next = sess->m_next;
2,988✔
3207
            sess->m_next = nullptr;
3208
            if (next == m_back)
3209
                break;
3210
            sess = next;
3211
        }
3212
        m_back = nullptr;
3213
    }
3214
}
3215

3216

3217
// ============================ ServerFile implementation ============================
3218

1,090✔
3219
ServerFile::ServerFile(ServerImpl& server, ServerFileAccessCache& cache, const std::string& virt_path,
1,090✔
3220
                       std::string real_path, bool disable_sync_to_disk)
3221
    : logger{"ServerFile[" + virt_path + "]: ", server.logger_ptr}           // Throws
3222
    , wlogger{"ServerFile[" + virt_path + "]: ", server.get_worker().logger} // Throws
3223
    , m_server{server}
1,090✔
3224
    , m_file{cache, real_path, virt_path, false, disable_sync_to_disk} // Throws
1,090✔
3225
    , m_worker_file{server.get_worker().get_file_access_cache(), real_path, virt_path, true, disable_sync_to_disk}
1,090✔
3226
{
1,090✔
3227
}
1,090✔
3228

3229

3230
ServerFile::~ServerFile() noexcept
3231
{
1,090✔
3232
    REALM_ASSERT(m_unidentified_sessions.empty());
1,090✔
3233
    REALM_ASSERT(m_identified_sessions.empty());
1,090✔
3234
    REALM_ASSERT(m_file_ident_request == 0);
1,090✔
3235
}
1,090✔
3236

1,090✔
3237

1,090✔
3238
void ServerFile::initialize()
1,090✔
3239
{
1,090✔
3240
    const ServerHistory& history = access().history; // Throws
1,090✔
3241
    file_ident_type partial_file_ident = 0;
3242
    version_type partial_progress_reference_version = 0;
3243
    bool has_upstream_sync_status;
1,090✔
3244
    history.get_status(m_version_info, has_upstream_sync_status, partial_file_ident,
3245
                       partial_progress_reference_version); // Throws
3246
    REALM_ASSERT(!has_upstream_sync_status);
3247
    REALM_ASSERT(partial_file_ident == 0);
3248
}
3249

86,620✔
3250

3251
void ServerFile::activate() {}
3252

3253

3254
// This function must be called only after a completed invocation of
2,154✔
3255
// initialize(). Both functinos must only ever be called by the network event
2,154✔
3256
// loop thread.
2,154✔
3257
void ServerFile::register_client_access(file_ident_type) {}
1,266✔
3258

2,154✔
3259

2,154✔
3260
auto ServerFile::request_file_ident(FileIdentReceiver& receiver, file_ident_type proxy_file, ClientType client_type)
2,154✔
3261
    -> file_ident_request_type
3262
{
3263
    auto request = ++m_last_file_ident_request;
3264
    m_file_ident_requests[request] = {&receiver, proxy_file, client_type}; // Throws
902✔
3265

902✔
3266
    on_work_added(); // Throws
902✔
3267
    return request;
902✔
3268
}
902✔
3269

902✔
3270

902✔
3271
void ServerFile::cancel_file_ident_request(file_ident_request_type request) noexcept
3272
{
3273
    auto i = m_file_ident_requests.find(request);
3274
    REALM_ASSERT(i != m_file_ident_requests.end());
5,026✔
3275
    FileIdentRequestInfo& info = i->second;
5,026✔
3276
    REALM_ASSERT(info.receiver);
5,026✔
3277
    info.receiver = nullptr;
5,026✔
3278
}
3279

3280

3281
void ServerFile::add_unidentified_session(Session* sess)
3,970✔
3282
{
3,970✔
3283
    REALM_ASSERT(m_unidentified_sessions.count(sess) == 0);
3,970✔
3284
    m_unidentified_sessions.insert(sess); // Throws
1,992✔
3285
}
3,970✔
3286

3,970✔
3287

3,970✔
3288
void ServerFile::identify_session(Session* sess, file_ident_type client_file_ident)
3289
{
3290
    REALM_ASSERT(m_unidentified_sessions.count(sess) == 1);
3291
    REALM_ASSERT(m_identified_sessions.count(client_file_ident) == 0);
1,056✔
3292

1,056✔
3293
    m_identified_sessions[client_file_ident] = sess; // Throws
1,056✔
3294
    m_unidentified_sessions.erase(sess);
1,056✔
3295
}
3296

3297

3298
void ServerFile::remove_unidentified_session(Session* sess) noexcept
3,970✔
3299
{
3,970✔
3300
    REALM_ASSERT(m_unidentified_sessions.count(sess) == 1);
3,970✔
3301
    m_unidentified_sessions.erase(sess);
3,970✔
3302
}
3303

3304

3305
void ServerFile::remove_identified_session(file_ident_type client_file_ident) noexcept
3,970✔
3306
{
3,970✔
3307
    REALM_ASSERT(m_identified_sessions.count(client_file_ident) == 1);
3,970✔
3308
    m_identified_sessions.erase(client_file_ident);
3,970✔
3309
}
×
3310

×
3311

3312
Session* ServerFile::get_identified_session(file_ident_type client_file_ident) noexcept
3313
{
44,724✔
3314
    auto i = m_identified_sessions.find(client_file_ident);
44,724✔
3315
    if (i == m_identified_sessions.end())
44,724✔
3316
        return nullptr;
3317
    return i->second;
3318
}
3319

3320
bool ServerFile::can_add_changesets_from_downstream() const noexcept
3321
{
44,524✔
3322
    return (m_blocked_changesets_from_downstream_byte_size < m_server.get_max_upload_backlog());
44,524✔
3323
}
22,932✔
3324

44,524✔
3325

22,932✔
3326
void ServerFile::add_changesets_from_downstream(file_ident_type client_file_ident, UploadCursor upload_progress,
44,524✔
3327
                                                version_type locked_server_version, const UploadChangeset* changesets,
44,524✔
3328
                                                std::size_t num_changesets)
78,914✔
3329
{
34,390✔
3330
    register_client_access(client_file_ident); // Throws
34,390✔
3331

34,390✔
3332
    bool dirty = false;
34,390✔
3333

34,390✔
3334
    IntegratableChangesetList& list = m_changesets_from_downstream[client_file_ident]; // Throws
34,390✔
3335
    std::size_t num_bytes = 0;
34,390✔
3336
    for (std::size_t i = 0; i < num_changesets; ++i) {
22,932✔
3337
        const UploadChangeset& uc = changesets[i];
44,524✔
3338
        auto& changesets = list.changesets;
44,524✔
3339
        changesets.emplace_back(client_file_ident, uc.origin_timestamp, uc.origin_file_ident, uc.upload_cursor,
44,528✔
3340
                                uc.changeset); // Throws
44,524✔
3341
        num_bytes += uc.changeset.size();
44,524✔
3342
        dirty = true;
44,524✔
3343
    }
22,932✔
3344

44,524✔
3345
    REALM_ASSERT(upload_progress.client_version >= list.upload_progress.client_version);
44,524✔
3346
    REALM_ASSERT(are_mutually_consistent(upload_progress, list.upload_progress));
38,132✔
3347
    if (upload_progress.client_version > list.upload_progress.client_version) {
38,132✔
3348
        list.upload_progress = upload_progress;
38,132✔
3349
        dirty = true;
22,932✔
3350
    }
44,528✔
3351

44,528✔
3352
    REALM_ASSERT(locked_server_version >= list.locked_server_version);
23,086✔
3353
    if (locked_server_version > list.locked_server_version) {
23,086✔
3354
        list.locked_server_version = locked_server_version;
21,442✔
3355
        dirty = true;
21,442✔
3356
    }
21,442✔
3357

44,528✔
3358
    if (REALM_LIKELY(dirty)) {
44,524✔
3359
        if (num_changesets > 0) {
3360
            on_changesets_from_downstream_added(num_changesets, num_bytes); // Throws
3361
        }
3362
        else {
3363
            on_work_added(); // Throws
3364
        }
3365
    }
4,000✔
3366
}
2,006✔
3367

2,006✔
3368

4,000✔
3369
BootstrapError ServerFile::bootstrap_client_session(SaltedFileIdent client_file_ident,
20✔
3370
                                                    DownloadCursor download_progress, SaltedVersion server_version,
1,996✔
3371
                                                    ClientType client_type, UploadCursor& upload_progress,
3,980✔
3372
                                                    version_type& locked_server_version, Logger& logger)
3,980✔
3373
{
3,980✔
3374
    // The Realm file may contain a later snapshot than the one reflected by
3,980✔
3375
    // `m_sync_version`, but if so, the client cannot "legally" know about it.
1,996✔
3376
    if (server_version.version > m_version_info.sync_version.version)
1,996✔
3377
        return BootstrapError::bad_server_version;
1,996✔
3378

1,996✔
3379
    const ServerHistory& hist = access().history; // Throws
1,996✔
3380
    BootstrapError error = hist.bootstrap_client_session(client_file_ident, download_progress, server_version,
1,996✔
3381
                                                         client_type, upload_progress, locked_server_version,
1,996✔
3382
                                                         logger); // Throws
1,996✔
3383

3,980✔
3384
    // FIXME: Rather than taking previously buffered changesets from the same
3,970✔
3385
    // client file into account when determining the upload progress, and then
1,992✔
3386
    // allowing for an error during the integration of those changesets to be
1,992✔
3387
    // reported to, and terminate the new session, consider to instead postpone
1,992✔
3388
    // the bootstrapping of the new session until all previously buffered
3,970✔
3389
    // changesets from same client file have been fully processed.
3,970✔
3390

1,284✔
3391
    if (error == BootstrapError::no_error) {
1,284✔
3392
        register_client_access(client_file_ident.ident); // Throws
1,284✔
3393

1,284✔
3394
        // If upload, or releaseing of server versions progressed further during
1,284✔
3395
        // previous sessions than the persisted points, take that into account
1,284✔
3396
        auto i = m_work.changesets_from_downstream.find(client_file_ident.ident);
3,970✔
3397
        if (i != m_work.changesets_from_downstream.end()) {
3,970✔
3398
            const IntegratableChangesetList& list = i->second;
148✔
3399
            REALM_ASSERT(list.upload_progress.client_version >= upload_progress.client_version);
148✔
3400
            upload_progress = list.upload_progress;
148✔
3401
            REALM_ASSERT(list.locked_server_version >= locked_server_version);
148✔
3402
            locked_server_version = list.locked_server_version;
148✔
3403
        }
148✔
3404
        auto j = m_changesets_from_downstream.find(client_file_ident.ident);
3,970✔
3405
        if (j != m_changesets_from_downstream.end()) {
1,996✔
3406
            const IntegratableChangesetList& list = j->second;
3,980✔
3407
            REALM_ASSERT(list.upload_progress.client_version >= upload_progress.client_version);
3,980✔
3408
            upload_progress = list.upload_progress;
3409
            REALM_ASSERT(list.locked_server_version >= locked_server_version);
3410
            locked_server_version = list.locked_server_version;
3411
        }
37,276✔
3412
    }
37,276✔
3413

37,276✔
3414
    return error;
19,046✔
3415
}
37,276✔
3416

37,276✔
3417
// NOTE: This function is executed by the worker thread
19,046✔
3418
void ServerFile::worker_process_work_unit(WorkerState& state)
37,278✔
3419
{
37,278✔
3420
    SteadyTimePoint start_time = steady_clock_now();
19,890✔
3421
    milliseconds_type parallel_time = 0;
19,048✔
3422

37,278✔
3423
    Work& work = m_work;
35,878✔
3424
    wlogger.debug("Work unit execution started"); // Throws
37,278✔
3425

19,046✔
3426
    if (work.has_primary_work) {
37,276✔
3427
        if (REALM_UNLIKELY(!m_work.file_ident_alloc_slots.empty()))
19,046✔
3428
            worker_allocate_file_identifiers(); // Throws
37,276✔
3429

37,276✔
3430
        if (!m_work.changesets_from_downstream.empty())
37,276✔
3431
            worker_integrate_changes_from_downstream(state); // Throws
37,276✔
3432
    }
19,046✔
3433

19,046✔
3434
    wlogger.debug("Work unit execution completed"); // Throws
37,276✔
3435

37,160✔
3436
    milliseconds_type time = steady_duration(start_time);
18,818✔
3437
    milliseconds_type seq_time = time - parallel_time;
18,818✔
3438
    m_server.m_seq_time.fetch_add(seq_time, std::memory_order_relaxed);
18,818✔
3439
    m_server.m_par_time.fetch_add(parallel_time, std::memory_order_relaxed);
36,932✔
3440

18,818✔
3441
    // Pass control back to the network event loop thread
36,932✔
3442
    network::Service& service = m_server.get_service();
37,276✔
3443
    service.post([this](Status) {
3444
        // FIXME: The safety of capturing `this` here, relies on the fact
3445
        // that ServerFile objects currently are not destroyed until the
3446
        // server object is destroyed.
23,086✔
3447
        group_postprocess_stage_1(); // Throws
23,086✔
3448
        // Suicide may have happened at this point
12,180✔
3449
    }); // Throws
23,086✔
3450
}
23,086✔
3451

23,086✔
3452

23,086✔
3453
void ServerFile::on_changesets_from_downstream_added(std::size_t num_changesets, std::size_t num_bytes)
12,180✔
3454
{
23,086✔
3455
    m_num_changesets_from_downstream += num_changesets;
23,086✔
3456

3457
    if (num_bytes > 0) {
3458
        m_blocked_changesets_from_downstream_byte_size += num_bytes;
3459
        get_server().inc_byte_size_for_pending_downstream_changesets(num_bytes); // Throws
46,680✔
3460
    }
46,680✔
3461

9,332✔
3462
    on_work_added(); // Throws
37,348✔
3463
}
19,082✔
3464

37,348✔
3465

11,152✔
3466
void ServerFile::on_work_added()
26,196✔
3467
{
26,196✔
3468
    if (m_has_blocked_work)
3469
        return;
3470
    m_has_blocked_work = true;
3471
    // Reference file
37,308✔
3472
    if (m_has_work_in_progress)
37,308✔
3473
        return;
37,308✔
3474
    group_unblock_work(); // Throws
37,308✔
3475
}
37,308✔
3476

37,308✔
3477

37,294✔
3478
void ServerFile::group_unblock_work()
37,294✔
3479
{
37,294✔
3480
    REALM_ASSERT(!m_has_work_in_progress);
37,294✔
3481
    if (REALM_LIKELY(!m_server.is_sync_stopped())) {
37,294✔
3482
        unblock_work(); // Throws
37,308✔
3483
        const Work& work = m_work;
37,308✔
3484
        if (REALM_LIKELY(work.has_primary_work)) {
3485
            logger.trace("Work unit unblocked"); // Throws
3486
            m_has_work_in_progress = true;
3487
            Worker& worker = m_server.get_worker();
37,308✔
3488
            worker.enqueue(this); // Throws
37,308✔
3489
        }
19,054✔
3490
    }
37,308✔
3491
}
19,054✔
3492

19,054✔
3493

19,054✔
3494
void ServerFile::unblock_work()
37,308✔
3495
{
37,308✔
3496
    REALM_ASSERT(m_has_blocked_work);
37,308✔
3497

39,460✔
3498
    m_work.reset();
2,152✔
3499

2,152✔
3500
    // Discard requests for file identifiers whose receiver is no longer
2,152✔
3501
    // waiting.
704✔
3502
    {
2,152✔
3503
        auto i = m_file_ident_requests.begin();
37,308✔
3504
        auto end = m_file_ident_requests.end();
37,308✔
3505
        while (i != end) {
37,308✔
3506
            auto j = i++;
1,404✔
3507
            const FileIdentRequestInfo& info = j->second;
1,404✔
3508
            if (!info.receiver)
1,448✔
3509
                m_file_ident_requests.erase(j);
1,448✔
3510
        }
1,448✔
3511
    }
1,448✔
3512
    std::size_t n = m_file_ident_requests.size();
1,448✔
3513
    if (n > 0) {
1,448✔
3514
        m_work.file_ident_alloc_slots.resize(n); // Throws
1,448✔
3515
        std::size_t i = 0;
1,404✔
3516
        for (const auto& pair : m_file_ident_requests) {
1,404✔
3517
            const FileIdentRequestInfo& info = pair.second;
19,054✔
3518
            FileIdentAllocSlot& slot = m_work.file_ident_alloc_slots[i];
19,054✔
3519
            slot.proxy_file = info.proxy_file;
19,054✔
3520
            slot.client_type = info.client_type;
19,054✔
3521
            ++i;
19,054✔
3522
        }
37,308✔
3523
        m_work.has_primary_work = true;
37,308✔
3524
    }
37,308✔
3525

37,308✔
3526
    // FIXME: `ServerFile::m_changesets_from_downstream` and
37,308✔
3527
    // `Work::changesets_from_downstream` should be renamed to something else,
35,894✔
3528
    // as it may contain kinds of data other than changesets.
35,894✔
3529

19,054✔
3530
    using std::swap;
19,054✔
3531
    swap(m_changesets_from_downstream, m_work.changesets_from_downstream);
37,308✔
3532
    m_work.have_changesets_from_downstream = (m_num_changesets_from_downstream > 0);
37,308✔
3533
    bool has_changesets = !m_work.changesets_from_downstream.empty();
37,308✔
3534
    if (has_changesets) {
19,054✔
3535
        m_work.has_primary_work = true;
37,308✔
3536
    }
37,308✔
3537

37,308✔
3538
    // Keep track of the size of pending changesets
3539
    REALM_ASSERT(m_unblocked_changesets_from_downstream_byte_size == 0);
3540
    m_unblocked_changesets_from_downstream_byte_size = m_blocked_changesets_from_downstream_byte_size;
3541
    m_blocked_changesets_from_downstream_byte_size = 0;
21,954✔
3542

34,220✔
3543
    m_num_changesets_from_downstream = 0;
34,220✔
3544
    m_has_blocked_work = false;
34,220✔
3545
}
34,220✔
3546

21,954✔
3547

3548
void ServerFile::resume_download() noexcept
3549
{
3550
    for (const auto& entry : m_identified_sessions) {
4,800✔
3551
        Session& sess = *entry.second;
4,800✔
3552
        sess.ensure_enlisted_to_send();
4,800✔
3553
    }
4,800✔
3554
}
4,800✔
3555

4,800✔
3556

4,800✔
3557
void ServerFile::recognize_external_change()
4,800✔
3558
{
2,400✔
3559
    VersionInfo prev_version_info = m_version_info;
4,800✔
3560
    const ServerHistory& history = access().history;       // Throws
4,800✔
3561
    bool has_upstream_status;                              // Dummy
4,800✔
3562
    sync::file_ident_type partial_file_ident;              // Dummy
4,798✔
3563
    sync::version_type partial_progress_reference_version; // Dummy
4,798✔
3564
    history.get_status(m_version_info, has_upstream_status, partial_file_ident,
4,798✔
3565
                       partial_progress_reference_version); // Throws
4,800✔
3566

3567
    REALM_ASSERT(m_version_info.realm_version >= prev_version_info.realm_version);
3568
    REALM_ASSERT(m_version_info.sync_version.version >= prev_version_info.sync_version.version);
3569
    if (m_version_info.sync_version.version > prev_version_info.sync_version.version) {
3570
        REALM_ASSERT(m_version_info.realm_version > prev_version_info.realm_version);
1,398✔
3571
        resume_download();
1,398✔
3572
    }
1,398✔
3573
}
1,398✔
3574

1,398✔
3575

1,398✔
3576
// NOTE: This function is executed by the worker thread
1,398✔
3577
void ServerFile::worker_allocate_file_identifiers()
3578
{
3579
    Work& work = m_work;
3580
    REALM_ASSERT(!work.file_ident_alloc_slots.empty());
3581
    ServerHistory& hist = worker_access().history;                                      // Throws
3582
    hist.allocate_file_identifiers(m_work.file_ident_alloc_slots, m_work.version_info); // Throws
3583
    m_work.produced_new_realm_version = true;
3584
}
35,878✔
3585

35,878✔
3586

18,490✔
3587
// Returns true when, and only when this function produces a new sync version
35,878✔
3588
// (adds a new entry to the sync history).
35,878✔
3589
//
35,878✔
3590
// NOTE: This function is executed by the worker thread
35,878✔
3591
bool ServerFile::worker_integrate_changes_from_downstream(WorkerState& state)
35,878✔
3592
{
35,878✔
3593
    REALM_ASSERT(!m_work.changesets_from_downstream.empty());
35,878✔
3594

35,878✔
3595
    std::unique_ptr<ServerHistory> hist_ptr;
35,878✔
3596
    DBRef sg_ptr;
35,878✔
3597
    ServerHistory& hist = get_client_file_history(state, hist_ptr, sg_ptr);
35,868✔
3598
    bool backup_whole_realm = false;
35,868✔
3599
    bool produced_new_realm_version = hist.integrate_client_changesets(
17,166✔
3600
        m_work.changesets_from_downstream, m_work.version_info, backup_whole_realm, m_work.integration_result,
17,166✔
3601
        wlogger); // Throws
35,868✔
3602
    bool produced_new_sync_version = !m_work.integration_result.integrated_changesets.empty();
35,878✔
3603
    REALM_ASSERT(!produced_new_sync_version || produced_new_realm_version);
35,878✔
3604
    if (produced_new_realm_version) {
3605
        m_work.produced_new_realm_version = true;
3606
        if (produced_new_sync_version) {
3607
            m_work.produced_new_sync_version = true;
35,878✔
3608
        }
35,878✔
3609
    }
35,882✔
3610
    return produced_new_sync_version;
4,294,967,294✔
3611
}
4,294,967,294✔
3612

4,294,967,294✔
3613
ServerHistory& ServerFile::get_client_file_history(WorkerState& state, std::unique_ptr<ServerHistory>& hist_ptr,
4,294,967,294✔
3614
                                                   DBRef& sg_ptr)
4,294,967,294✔
3615
{
4,294,967,294✔
3616
    if (state.use_file_cache)
4,294,967,294✔
3617
        return worker_access().history; // Throws
3618
    const std::string& path = m_worker_file.realm_path;
3619
    hist_ptr = m_server.make_history_for_path();                   // Throws
3620
    DBOptions options = m_worker_file.make_shared_group_options(); // Throws
3621
    sg_ptr = DB::create(*hist_ptr, path, options);                 // Throws
36,932✔
3622
    sg_ptr->claim_sync_agent();                                    // Throws
36,932✔
3623
    return *hist_ptr;                                              // Throws
18,818✔
3624
}
36,932✔
3625

36,932✔
3626

36,932✔
3627
// When worker thread finishes work unit.
36,932✔
3628
void ServerFile::group_postprocess_stage_1()
3629
{
3630
    REALM_ASSERT(m_has_work_in_progress);
3631

36,934✔
3632
    group_finalize_work_stage_1(); // Throws
36,934✔
3633
    group_finalize_work_stage_2(); // Throws
36,934✔
3634
    group_postprocess_stage_2();   // Throws
18,818✔
3635
}
36,934✔
3636

3637

3638
void ServerFile::group_postprocess_stage_2()
3639
{
3640
    REALM_ASSERT(m_has_work_in_progress);
36,934✔
3641
    group_postprocess_stage_3(); // Throws
36,934✔
3642
    // Suicide may have happened at this point
36,934✔
3643
}
18,818✔
3644

36,934✔
3645

36,934✔
3646
// When all files, including the reference file, have been backed up.
11,110✔
3647
void ServerFile::group_postprocess_stage_3()
36,934✔
3648
{
3649
    REALM_ASSERT(m_has_work_in_progress);
3650
    m_has_work_in_progress = false;
3651

36,932✔
3652
    logger.trace("Work unit postprocessing complete"); // Throws
36,932✔
3653
    if (m_has_blocked_work)
9,094✔
3654
        group_unblock_work(); // Throws
17,172✔
3655
}
17,172✔
3656

17,172✔
3657

17,172✔
3658
void ServerFile::finalize_work_stage_1()
18,818✔
3659
{
18,818✔
3660
    if (m_unblocked_changesets_from_downstream_byte_size > 0) {
36,932✔
3661
        // Report the byte size of completed downstream changesets.
36,932✔
3662
        std::size_t byte_size = m_unblocked_changesets_from_downstream_byte_size;
18,828✔
3663
        get_server().dec_byte_size_for_pending_downstream_changesets(byte_size); // Throws
16✔
3664
        m_unblocked_changesets_from_downstream_byte_size = 0;
16✔
3665
    }
16✔
3666

16✔
3667
    // Deal with errors (bad changesets) pertaining to downstream clients
✔
3668
    std::size_t num_changesets_removed = 0;
×
3669
    std::size_t num_bytes_removed = 0;
×
3670
    for (const auto& entry : m_work.integration_result.excluded_client_files) {
×
3671
        file_ident_type client_file_ident = entry.first;
×
3672
        ExtendedIntegrationError error = entry.second;
✔
3673
        ProtocolError error_2 = ProtocolError::other_session_error;
×
3674
        switch (error) {
×
3675
            case ExtendedIntegrationError::client_file_expired:
16✔
3676
                logger.debug("Changeset integration failed: Client file entry "
16✔
3677
                             "expired during session"); // Throws
16✔
3678
                error_2 = ProtocolError::client_file_expired;
16✔
3679
                break;
16✔
3680
            case ExtendedIntegrationError::bad_origin_file_ident:
16✔
3681
                error_2 = ProtocolError::bad_origin_file_ident;
16✔
3682
                break;
16✔
3683
            case ExtendedIntegrationError::bad_changeset:
16✔
3684
                error_2 = ProtocolError::bad_changeset;
16✔
3685
                break;
16✔
3686
        }
16✔
3687
        auto i = m_identified_sessions.find(client_file_ident);
16✔
3688
        if (i != m_identified_sessions.end()) {
16✔
3689
            Session& sess = *i->second;
×
3690
            SyncConnection& conn = sess.get_connection();
16✔
3691
            conn.protocol_error(error_2, &sess); // Throws
16✔
3692
        }
16✔
3693
        const IntegratableChangesetList& list = m_changesets_from_downstream[client_file_ident];
16✔
3694
        std::size_t num_changesets = list.changesets.size();
16✔
3695
        std::size_t num_bytes = 0;
16✔
3696
        for (const IntegratableChangeset& ic : list.changesets)
18,818✔
3697
            num_bytes += ic.changeset.size();
36,932✔
3698
        logger.info("Excluded %1 changesets of combined byte size %2 for client file %3", num_changesets, num_bytes,
36,932✔
3699
                    client_file_ident); // Throws
18,818✔
3700
        num_changesets_removed += num_changesets;
36,932✔
3701
        num_bytes_removed += num_bytes;
36,932✔
3702
        m_changesets_from_downstream.erase(client_file_ident);
3703
    }
×
3704

3705
    REALM_ASSERT(num_changesets_removed <= m_num_changesets_from_downstream);
3706
    REALM_ASSERT(num_bytes_removed <= m_blocked_changesets_from_downstream_byte_size);
×
3707

×
3708
    if (num_changesets_removed == 0)
×
3709
        return;
×
3710

×
3711
    m_num_changesets_from_downstream -= num_changesets_removed;
3712

3713
    // The byte size of the blocked changesets must be decremented.
3714
    if (num_bytes_removed > 0) {
36,932✔
3715
        m_blocked_changesets_from_downstream_byte_size -= num_bytes_removed;
18,818✔
3716
        get_server().dec_byte_size_for_pending_downstream_changesets(num_bytes_removed); // Throws
36,932✔
3717
    }
36,932✔
3718
}
36,906✔
3719

36,906✔
3720

36,906✔
3721
void ServerFile::finalize_work_stage_2()
18,818✔
3722
{
36,932✔
3723
    // Expose new snapshot to remote peers
18,818✔
3724
    REALM_ASSERT(m_work.produced_new_realm_version || m_work.version_info.realm_version == 0);
18,818✔
3725
    if (m_work.version_info.realm_version > m_version_info.realm_version) {
36,932✔
3726
        REALM_ASSERT(m_work.version_info.sync_version.version >= m_version_info.sync_version.version);
36,932✔
3727
        m_version_info = m_work.version_info;
36,932✔
3728
    }
19,670✔
3729

1,426✔
3730
    bool resume_download_and_upload = m_work.produced_new_sync_version;
1,426✔
3731

1,426✔
3732
    // Deliver allocated file identifiers to requesters
1,426✔
3733
    REALM_ASSERT(m_file_ident_requests.size() >= m_work.file_ident_alloc_slots.size());
1,252✔
3734
    auto begin = m_file_ident_requests.begin();
1,252✔
3735
    auto i = begin;
1,252✔
3736
    for (const FileIdentAllocSlot& slot : m_work.file_ident_alloc_slots) {
1,426✔
3737
        FileIdentRequestInfo& info = i->second;
1,426✔
3738
        REALM_ASSERT(info.proxy_file == slot.proxy_file);
36,932✔
3739
        REALM_ASSERT(info.client_type == slot.client_type);
18,818✔
3740
        if (FileIdentReceiver* receiver = info.receiver) {
18,818✔
3741
            info.receiver = nullptr;
36,932✔
3742
            receiver->receive_file_ident(slot.file_ident); // Throws
17,156✔
3743
        }
17,156✔
3744
        ++i;
36,932✔
3745
    }
3746
    m_file_ident_requests.erase(begin, i);
3747

3748
    // Resume download to downstream clients
3749
    if (resume_download_and_upload) {
3750
        resume_download();
3751
    }
3752
}
7,968✔
3753

7,968✔
3754
// ============================ Worker implementation ============================
7,968✔
3755

3756
Worker::Worker(ServerImpl& server)
3757
    : logger{"Worker: ", server.logger_ptr} // Throws
3758
    , m_server{server}
37,296✔
3759
    , m_transformer{make_transformer()} // Throws
37,296✔
3760
    , m_file_access_cache{server.get_config().max_open_files, logger, *this, server.get_config().encryption_key}
37,296✔
3761
{
37,296✔
3762
    util::seed_prng_nondeterministically(m_random); // Throws
37,296✔
3763
}
3764

3765

3766
void Worker::enqueue(ServerFile* file)
2,458✔
3767
{
2,458✔
3768
    util::LockGuard lock{m_mutex};
2,458✔
3769
    m_queue.push_back(file); // Throws
3770
    m_cond.notify_all();
3771
}
3772

7,916✔
3773

45,194✔
3774
std::mt19937_64& Worker::server_history_get_random() noexcept
45,194✔
3775
{
45,194✔
3776
    return m_random;
45,194✔
3777
}
89,802✔
3778

89,802✔
3779

49,568✔
3780
sync::Transformer& Worker::get_transformer()
81,886✔
3781
{
37,278✔
3782
    return *m_transformer;
37,278✔
3783
}
37,278✔
3784

37,278✔
3785

44,608✔
3786
util::Buffer<char>& Worker::get_transform_buffer()
44,608✔
3787
{
45,194✔
3788
    return m_transform_buffer;
41,184✔
3789
}
37,278✔
3790

7,916✔
3791
void Worker::run()
3792
{
3793
    for (;;) {
3794
        ServerFile* file = nullptr;
7,916✔
3795
        {
7,916✔
3796
            util::LockGuard lock{m_mutex};
7,916✔
3797
            for (;;) {
7,916✔
3798
                if (REALM_UNLIKELY(m_stop))
7,916✔
3799
                    return;
3800
                if (!m_queue.empty()) {
3801
                    file = m_queue.front();
3802
                    m_queue.pop_front();
3803
                    break;
3804
                }
3805
                m_cond.wait(lock);
3806
            }
3807
        }
3808
        file->worker_process_work_unit(m_state); // Throws
3809
    }
3810
}
3811

3812

3813
void Worker::stop() noexcept
3814
{
3815
    util::LockGuard lock{m_mutex};
3816
    m_stop = true;
3817
    m_cond.notify_all();
7,968✔
3818
}
7,968✔
3819

24✔
3820

24✔
3821
// ============================ ServerImpl implementation ============================
24✔
3822

24✔
3823

7,968✔
3824
ServerImpl::ServerImpl(const std::string& root_dir, util::Optional<sync::PKey> pkey, Server::Config config)
3825
    : logger_ptr{config.logger ? std::move(config.logger) : Logger::get_default_logger()}
3826
    , logger{*logger_ptr}
3827
    , m_config{std::move(config)}
7,968✔
3828
    , m_max_upload_backlog{determine_max_upload_backlog(config)}
7,968✔
3829
    , m_root_dir{root_dir} // Throws
7,968✔
3830
    , m_access_control{std::move(pkey)}
7,968✔
3831
    , m_protocol_version_range{determine_protocol_version_range(config)}                 // Throws
3832
    , m_file_access_cache{m_config.max_open_files, logger, *this, config.encryption_key} // Throws
3833
    , m_worker{*this}                                                                    // Throws
3834
    , m_acceptor{get_service()}
7,968✔
3835
    , m_server_protocol{}       // Throws
7,968✔
3836
    , m_compress_memory_arena{} // Throws
7,968✔
3837
{
7,968✔
3838
    if (m_config.ssl) {
7,968✔
3839
        m_ssl_context = std::make_unique<network::ssl::Context>();                // Throws
7,968✔
3840
        m_ssl_context->use_certificate_chain_file(m_config.ssl_certificate_path); // Throws
7,968✔
3841
        m_ssl_context->use_private_key_file(m_config.ssl_certificate_key_path);   // Throws
7,968✔
3842
    }
7,968✔
3843
}
7,968✔
3844

7,968✔
3845

7,968✔
3846
ServerImpl::~ServerImpl() noexcept
7,968✔
3847
{
7,968✔
3848
    bool server_destroyed_while_still_running = m_running;
7,968✔
3849
    REALM_ASSERT_RELEASE(!server_destroyed_while_still_running);
7,968✔
3850
}
×
3851

×
3852

×
3853
void ServerImpl::start()
7,968✔
3854
{
7,968✔
3855
    logger.info("Realm sync server started (%1)", REALM_VER_CHUNK); // Throws
7,968✔
3856
    logger.info("Supported protocol versions: %1-%2 (%3-%4 configured)",
7,968✔
3857
                ServerImplBase::get_oldest_supported_protocol_version(), get_current_protocol_version(),
7,968✔
3858
                m_protocol_version_range.first,
7,968✔
3859
                m_protocol_version_range.second); // Throws
7,968✔
3860
    logger.info("Platform: %1", util::get_platform_info());
7,968✔
3861
    bool is_debug_build = false;
7,968✔
3862
#if REALM_DEBUG
7,968✔
3863
    is_debug_build = true;
4✔
3864
#endif
4✔
3865
    {
7,964✔
3866
        const char* lead_text = "Build mode";
7,964✔
3867
        if (is_debug_build) {
7,964✔
3868
            logger.info("%1: Debug", lead_text); // Throws
7,968✔
3869
        }
7,968✔
3870
        else {
7,968✔
3871
            logger.info("%1: Release", lead_text); // Throws
7,968✔
3872
        }
7,968✔
3873
    }
7,276✔
3874
    if (is_debug_build) {
7,276✔
3875
        logger.warn("Build mode is Debug! CAN SEVERELY IMPACT PERFORMANCE - "
692✔
3876
                    "NOT RECOMMENDED FOR PRODUCTION"); // Throws
692✔
3877
    }
692✔
3878
    logger.info("Directory holding persistent state: %1", m_root_dir);        // Throws
7,968✔
3879
    logger.info("Maximum number of open files: %1", m_config.max_open_files); // Throws
7,968✔
3880
    {
7,276✔
3881
        const char* lead_text = "Encryption";
7,276✔
3882
        if (m_config.encryption_key) {
7,276✔
3883
            logger.info("%1: Yes", lead_text); // Throws
7,968✔
3884
        }
7,968✔
3885
        else {
7,968✔
3886
            logger.info("%1: No", lead_text); // Throws
7,968✔
3887
        }
7,968✔
3888
    }
7,968✔
3889
    logger.info("Log level: %1", logger.get_level_threshold()); // Throws
7,968✔
3890
    {
7,968✔
3891
        const char* lead_text = "Disable sync to disk";
7,968✔
3892
        if (m_config.disable_sync_to_disk) {
7,968✔
3893
            logger.info("%1: All files", lead_text); // Throws
7,968✔
3894
        }
7,968✔
3895
        else {
3,932✔
3896
            logger.info("%1: No", lead_text); // Throws
7,968✔
3897
        }
3,932✔
3898
    }
7,968✔
3899
    if (m_config.disable_sync_to_disk) {
3,932✔
3900
        logger.warn("Testing/debugging feature 'disable sync to disk' enabled - "
7,968✔
3901
                    "never do this in production!"); // Throws
7,968✔
3902
    }
3903
    logger.info("Download compaction: %1",
3904
                (m_config.disable_download_compaction ? "No" : "Yes")); // Throws
3905
    logger.info("Download bootstrap caching: %1",
7,916✔
3906
                (m_config.enable_download_bootstrap_cache ? "Yes" : "No"));                // Throws
7,916✔
3907
    logger.info("Max download size: %1 bytes", m_config.max_download_size);                // Throws
3,906✔
3908
    logger.info("Max upload backlog: %1 bytes", m_max_upload_backlog);                     // Throws
7,916✔
3909
    logger.info("HTTP request timeout: %1 ms", m_config.http_request_timeout);             // Throws
7,916✔
3910
    logger.info("HTTP response timeout: %1 ms", m_config.http_response_timeout);           // Throws
7,916✔
3911
    logger.info("Connection reaper timeout: %1 ms", m_config.connection_reaper_timeout);   // Throws
7,916✔
3912
    logger.info("Connection reaper interval: %1 ms", m_config.connection_reaper_interval); // Throws
7,916✔
3913
    logger.info("Connection soft close timeout: %1 ms", m_config.soft_close_timeout);      // Throws
7,916✔
3914
    logger.debug("Authorization header name: %1", m_config.authorization_header_name);     // Throws
7,916✔
3915

×
3916
    m_transformer = make_transformer(); // Throws
×
3917

×
3918
    m_realm_names = _impl::find_realm_files(m_root_dir); // Throws
3,906✔
3919

7,916✔
3920
    initiate_connection_reaper_timer(m_config.connection_reaper_interval); // Throws
3,906✔
3921

7,916✔
3922
    listen(); // Throws
7,916✔
3923
}
3,906✔
3924

7,916✔
3925

7,916✔
3926
void ServerImpl::run()
3927
{
3928
    auto ta = util::make_temp_assign(m_running, true);
3929

8,810✔
3930
    {
8,810✔
3931
        auto worker_thread = util::make_thread_exec_guard(m_worker, *this); // Throws
8,810✔
3932
        std::string name;
842✔
3933
        if (util::Thread::get_name(name)) {
7,968✔
3934
            name += "-worker";
7,968✔
3935
            worker_thread.start_with_signals_blocked(name); // Throws
7,968✔
3936
        }
7,968✔
3937
        else {
3938
            worker_thread.start_with_signals_blocked(); // Throws
3939
        }
3940

23,086✔
3941
        m_service.run(); // Throws
23,086✔
3942

23,086✔
3943
        worker_thread.stop_and_rethrow(); // Throws
23,086✔
3944
    }
23,086✔
3945

23,086✔
3946
    logger.info("Realm sync server stopped");
23,086✔
3947
}
3948

3949

3950
void ServerImpl::stop() noexcept
17,170✔
3951
{
17,170✔
3952
    util::LockGuard lock{m_mutex};
17,170✔
3953
    if (m_stopped)
17,170✔
3954
        return;
17,170✔
3955
    m_stopped = true;
17,170✔
3956
    m_wait_or_service_stopped_cond.notify_all();
17,170✔
3957
    m_service.stop();
17,170✔
3958
}
3959

3960

3961
void ServerImpl::inc_byte_size_for_pending_downstream_changesets(std::size_t byte_size)
1,090✔
3962
{
1,090✔
3963
    m_pending_changesets_from_downstream_byte_size += byte_size;
1,090✔
3964
    logger.debug("Byte size for pending downstream changesets incremented by "
3965
                 "%1 to reach a total of %2",
3966
                 byte_size,
3967
                 m_pending_changesets_from_downstream_byte_size); // Throws
7,968✔
3968
}
7,968✔
3969

7,968✔
3970

7,968✔
3971
void ServerImpl::dec_byte_size_for_pending_downstream_changesets(std::size_t byte_size)
7,968✔
3972
{
3,932✔
3973
    REALM_ASSERT(byte_size <= m_pending_changesets_from_downstream_byte_size);
7,968✔
3974
    m_pending_changesets_from_downstream_byte_size -= byte_size;
7,968✔
3975
    logger.debug("Byte size for pending downstream changesets decremented by "
7,968✔
3976
                 "%1 to reach a total of %2",
7,968✔
3977
                 byte_size,
7,968✔
3978
                 m_pending_changesets_from_downstream_byte_size); // Throws
7,968✔
3979
}
7,966✔
3980

7,966✔
3981

7,968✔
3982
std::mt19937_64& ServerImpl::server_history_get_random() noexcept
7,968✔
3983
{
7,968✔
3984
    return get_random();
7,968✔
3985
}
2,147,483,647✔
3986

2,147,483,647✔
3987

2,147,483,647✔
3988
Transformer& ServerImpl::get_transformer() noexcept
3,932!
3989
{
×
3990
    return *m_transformer;
3991
}
3992

×
3993

×
3994
util::Buffer<char>& ServerImpl::get_transform_buffer() noexcept
×
3995
{
×
3996
    return m_transform_buffer;
×
3997
}
×
3998

×
3999

×
4000
void ServerImpl::listen()
3,932✔
4001
{
7,968✔
4002
    network::Resolver resolver{get_service()};
3,932✔
4003
    network::Resolver::Query query(m_config.listen_address, m_config.listen_port,
7,968✔
4004
                                   network::Resolver::Query::passive | network::Resolver::Query::address_configured);
7,958✔
4005
    network::Endpoint::List endpoints = resolver.resolve(query); // Throws
7,968✔
4006

7,968✔
4007
    auto i = endpoints.begin();
3,932✔
4008
    auto end = endpoints.end();
7,968✔
4009
    for (;;) {
7,968✔
4010
        std::error_code ec;
4011
        m_acceptor.open(i->protocol(), ec);
4012
        if (!ec) {
4013
            using SocketBase = network::SocketBase;
9,870✔
4014
            m_acceptor.set_option(SocketBase::reuse_address(m_config.reuse_address), ec);
5,834✔
4015
            if (!ec) {
1,902✔
4016
                m_acceptor.bind(*i, ec);
1,902✔
4017
                if (!ec)
1,902✔
4018
                    break;
9,870✔
4019
            }
9,870✔
4020
            m_acceptor.close();
9,870✔
4021
        }
9,870✔
4022
        if (i + 1 == end) {
4023
            for (auto i2 = endpoints.begin(); i2 != i; ++i2) {
4024
                // FIXME: We don't have the error code for previous attempts, so
4025
                // can't print a nice message.
1,902✔
4026
                logger.error("Failed to bind to %1:%2", i2->address(),
1,902✔
4027
                             i2->port()); // Throws
×
4028
            }
×
4029
            logger.error("Failed to bind to %1:%2: %3", i->address(), i->port(),
4030
                         ec.message()); // Throws
4031
            throw std::runtime_error("Could not create a listening socket: All endpoints failed");
×
4032
        }
×
4033
    }
×
4034

4035
    m_acceptor.listen(m_config.listen_backlog);
4036

4037
    network::Endpoint local_endpoint = m_acceptor.local_endpoint();
4038
    const char* ssl_mode = (m_ssl_context ? "TLS" : "non-TLS");
×
4039
    logger.info("Listening on %1:%2 (max backlog is %3, %4)", local_endpoint.address(), local_endpoint.port(),
×
4040
                m_config.listen_backlog, ssl_mode); // Throws
×
4041

×
4042
    initiate_accept();
×
4043
}
×
4044

×
4045

×
4046
void ServerImpl::initiate_accept()
×
4047
{
×
4048
    auto handler = [this](std::error_code ec) {
×
4049
        if (ec != util::error::operation_aborted)
1,902✔
4050
            handle_accept(ec);
1,902✔
4051
    };
1,902✔
4052
    bool is_ssl = bool(m_ssl_context);
1,584✔
4053
    m_next_http_conn.reset(new HTTPConnection(*this, ++m_next_conn_id, is_ssl));                            // Throws
1,902✔
4054
    m_acceptor.async_accept(m_next_http_conn->get_socket(), m_next_http_conn_endpoint, std::move(handler)); // Throws
1,902✔
4055
}
1,902✔
4056

1,902✔
4057

1,902✔
4058
void ServerImpl::handle_accept(std::error_code ec)
1,902✔
4059
{
1,902✔
4060
    if (ec) {
1,902✔
4061
        if (ec != util::error::connection_aborted) {
1,902✔
4062
            REALM_ASSERT(ec != util::error::operation_aborted);
4063

4064
            // We close the reserved files to get a few extra file descriptors.
4065
            for (size_t i = 0; i < sizeof(m_reserved_files) / sizeof(m_reserved_files[0]); ++i) {
1,900✔
4066
                m_reserved_files[i].reset();
1,900✔
4067
            }
1,900✔
4068

4069
            // FIXME: There are probably errors that need to be treated
4070
            // specially, and not cause the server to "crash".
4071

1,870✔
4072
            if (ec == make_basic_system_error_code(EMFILE)) {
1,870✔
4073
                logger.error("Failed to accept a connection due to the file descriptor limit, "
1,870✔
4074
                             "consider increasing the limit in your system config"); // Throws
4075
                throw OutOfFilesError(ec);
4076
            }
4077
            else {
1,078✔
4078
                throw std::system_error(ec);
1,078✔
4079
            }
1,078✔
4080
        }
4081
        logger.debug("Skipping aborted connection"); // Throws
4082
    }
4083
    else {
4✔
4084
        HTTPConnection& conn = *m_next_http_conn;
4✔
4085
        if (m_config.tcp_no_delay)
4✔
4086
            conn.get_socket().set_option(network::SocketBase::no_delay(true));       // Throws
4✔
4087
        m_http_connections.emplace(conn.get_id(), std::move(m_next_http_conn));      // Throws
4✔
4088
        Formatter& formatter = m_misc_buffers.formatter;
4089
        formatter.reset();
4090
        formatter << "[" << m_next_http_conn_endpoint.address() << "]:" << m_next_http_conn_endpoint.port(); // Throws
4091
        std::string remote_endpoint = {formatter.data(), formatter.size()};                                  // Throws
20✔
4092
        conn.initiate(std::move(remote_endpoint));                                                           // Throws
20✔
4093
    }
20✔
4094
    initiate_accept(); // Throws
20✔
4095
}
20✔
4096

4097

4098
void ServerImpl::remove_http_connection(std::int_fast64_t conn_id) noexcept
4099
{
72✔
4100
    m_http_connections.erase(conn_id);
72✔
4101
}
72✔
4102

4103

4104
void ServerImpl::add_sync_connection(int_fast64_t connection_id, std::unique_ptr<SyncConnection>&& sync_conn)
4105
{
4,800✔
4106
    m_sync_connections.emplace(connection_id, std::move(sync_conn));
4,800✔
4107
}
4,800✔
4108

4,800✔
4109

4,800✔
4110
void ServerImpl::remove_sync_connection(int_fast64_t connection_id)
4,800✔
4111
{
4112
    m_sync_connections.erase(connection_id);
4113
}
4114

4115

×
4116
void ServerImpl::set_connection_reaper_timeout(milliseconds_type timeout)
×
4117
{
×
4118
    get_service().post([this, timeout](Status) {
×
4119
        m_config.connection_reaper_timeout = timeout;
4120
    });
×
4121
}
×
4122

×
4123

×
4124
void ServerImpl::close_connections()
×
4125
{
4126
    get_service().post([this](Status) {
4127
        do_close_connections(); // Throws
4128
    });
8,050✔
4129
}
8,050✔
4130

4,014✔
4131

82✔
4132
bool ServerImpl::map_virtual_to_real_path(const std::string& virt_path, std::string& real_path)
82✔
4133
{
82✔
4134
    return _impl::map_virt_to_real_realm_path(m_root_dir, virt_path, real_path); // Throws
82✔
4135
}
82✔
4136

8,050✔
4137

4138
void ServerImpl::recognize_external_change(const std::string& virt_path)
4139
{
4140
    std::string virt_path_2 = virt_path; // Throws (copy)
82✔
4141
    get_service().post([this, virt_path = std::move(virt_path_2)](Status) {
82✔
4142
        do_recognize_external_change(virt_path); // Throws
82✔
4143
    });                                          // Throws
82✔
4144
}
82✔
4145

82✔
4146

84✔
4147
void ServerImpl::stop_sync_and_wait_for_backup_completion(
2✔
4148
    util::UniqueFunction<void(bool did_backup)> completion_handler, milliseconds_type timeout)
2✔
4149
{
4150
    logger.info("stop_sync_and_wait_for_backup_completion() called with "
2✔
4151
                "timeout = %1",
2✔
4152
                timeout); // Throws
82✔
4153

82✔
4154
    get_service().post([this, completion_handler = std::move(completion_handler), timeout](Status) mutable {
82✔
4155
        do_stop_sync_and_wait_for_backup_completion(std::move(completion_handler),
82✔
4156
                                                    timeout); // Throws
158✔
4157
    });
76✔
4158
}
76✔
4159

70✔
4160

76✔
4161
void ServerImpl::initiate_connection_reaper_timer(milliseconds_type timeout)
76✔
4162
{
82✔
4163
    m_connection_reaper_timer.emplace(get_service());
82✔
4164
    m_connection_reaper_timer->async_wait(std::chrono::milliseconds(timeout), [this, timeout](Status status) {
4165
        if (status != ErrorCodes::OperationAborted) {
4166
            reap_connections();                        // Throws
4167
            initiate_connection_reaper_timer(timeout); // Throws
20✔
4168
        }
20✔
4169
    }); // Throws
20✔
4170
}
20✔
4171

20✔
4172

20✔
4173
void ServerImpl::reap_connections()
4174
{
4175
    logger.debug("Discarding dead connections"); // Throws
4176
    SteadyTimePoint now = steady_clock_now();
4,800✔
4177
    {
4,800✔
4178
        auto end = m_http_connections.end();
4,800✔
4179
        auto i = m_http_connections.begin();
×
4180
        while (i != end) {
4,800✔
4181
            HTTPConnection& conn = *i->second;
4,800✔
4182
            ++i;
4,800✔
4183
            // Suicide
4184
            conn.terminate_if_dead(now); // Throws
4185
        }
4186
    }
4187
    {
×
4188
        auto end = m_sync_connections.end();
×
4189
        auto i = m_sync_connections.begin();
×
4190
        while (i != end) {
×
4191
            SyncConnection& conn = *i->second;
×
4192
            ++i;
×
4193
            // Suicide
×
4194
            conn.terminate_if_dead(now); // Throws
×
4195
        }
×
4196
    }
4197
}
4198

4199

4200
void ServerImpl::do_close_connections()
4201
{
1,870✔
4202
    for (auto& entry : m_sync_connections) {
1,870✔
4203
        SyncConnection& conn = *entry.second;
1,870✔
4204
        conn.initiate_soft_close(); // Throws
1,870✔
4205
    }
4206
}
4207

4208

1,870✔
4209
void ServerImpl::do_recognize_external_change(const std::string& virt_path)
1,870✔
4210
{
1,870✔
4211
    auto i = m_files.find(virt_path);
1,870✔
4212
    if (i == m_files.end())
1,870✔
4213
        return;
1,870✔
4214
    ServerFile& file = *i->second;
1,870✔
4215
    file.recognize_external_change();
4216
}
4217

4218

4219
void ServerImpl::do_stop_sync_and_wait_for_backup_completion(
1,078✔
4220
    util::UniqueFunction<void(bool did_complete)> completion_handler, milliseconds_type timeout)
1,078✔
4221
{
1,078✔
4222
    static_cast<void>(timeout);
1,078✔
4223
    if (m_sync_stopped)
1,078✔
4224
        return;
1,078✔
4225
    do_close_connections(); // Throws
582✔
4226
    m_sync_stopped = true;
1,078✔
4227
    bool completion_reached = false;
1,078✔
4228
    completion_handler(completion_reached); // Throws
4229
}
4230

4231

76✔
4232
// ============================ SyncConnection implementation ============================
76✔
4233

76✔
4234
SyncConnection::~SyncConnection() noexcept
76✔
4235
{
×
4236
    m_sessions_enlisted_to_send.clear();
4237
    m_sessions.clear();
×
4238
}
×
4239

×
4240

×
4241
void SyncConnection::initiate()
76✔
4242
{
76✔
4243
    m_last_activity_at = steady_clock_now();
2✔
4244
    logger.debug("Sync Connection initiated");
4✔
4245
    m_websocket.initiate_server_websocket_after_handshake();
4✔
4246
    send_log_message(util::Logger::Level::info, "Client connection established with server", 0,
4✔
4247
                     m_appservices_request_id);
76✔
4248
}
76✔
4249

4250

4251
template <class... Params>
4252
void SyncConnection::terminate(Logger::Level log_level, const char* log_message, Params... log_params)
102,786✔
4253
{
102,786✔
4254
    terminate_sessions();                              // Throws
102,786✔
4255
    logger.log(log_level, log_message, log_params...); // Throws
102,786✔
4256
    m_websocket.stop();
102,786✔
4257
    m_ssl_stream.reset();
102,786✔
4258
    m_socket.reset();
102,786✔
4259
    // Suicide
4260
    m_server.remove_sync_connection(m_id);
4261
}
4262

×
4263

×
4264
void SyncConnection::terminate_if_dead(SteadyTimePoint now)
×
4265
{
×
4266
    milliseconds_type time = steady_duration(m_last_activity_at, now);
×
4267
    const Server::Config& config = m_server.get_config();
×
4268
    if (m_is_closing) {
×
4269
        if (time >= config.soft_close_timeout) {
×
4270
            // Suicide
×
4271
            terminate(Logger::Level::detail,
×
4272
                      "Sync connection closed (timeout during soft close)"); // Throws
×
4273
        }
×
4274
    }
×
4275
    else {
×
4276
        if (time >= config.connection_reaper_timeout) {
4277
            // Suicide
4278
            terminate(Logger::Level::detail,
4279
                      "Sync connection closed (no heartbeat)"); // Throws
4280
        }
5,054✔
4281
    }
5,054✔
4282
}
5,054✔
4283

5,054✔
4284

×
4285
void SyncConnection::enlist_to_send(Session* sess) noexcept
×
4286
{
×
4287
    REALM_ASSERT(m_send_trigger);
×
4288
    REALM_ASSERT(!m_is_closing);
×
4289
    REALM_ASSERT(!sess->is_enlisted_to_send());
5,054✔
4290
    m_sessions_enlisted_to_send.push_back(sess);
5,054✔
4291
    m_send_trigger->trigger();
5,054✔
4292
}
2,800✔
4293

×
4294

×
4295
void SyncConnection::handle_protocol_error(Status status)
×
4296
{
2,800✔
4297
    logger.error("%1", status);
5,054✔
4298
    switch (status.code()) {
5,054✔
4299
        case ErrorCodes::SyncProtocolInvariantFailed:
5,054✔
4300
            protocol_error(ProtocolError::bad_syntax); // Throws
5,054✔
4301
            break;
5,054✔
4302
        case ErrorCodes::LimitExceeded:
5,054✔
4303
            protocol_error(ProtocolError::limits_exceeded); // Throws
5,054✔
4304
            break;
2,814✔
4305
        default:
5,054✔
4306
            protocol_error(ProtocolError::other_error);
4307
            break;
4308
    }
4309
}
4310

4311
void SyncConnection::receive_bind_message(session_ident_type session_ident, std::string path,
4312
                                          std::string signed_user_token, bool need_client_file_ident,
4,018✔
4313
                                          bool is_subserver)
4,018✔
4314
{
4,018✔
4315
    auto p = m_sessions.emplace(session_ident, nullptr); // Throws
×
4316
    bool was_inserted = p.second;
×
4317
    if (REALM_UNLIKELY(!was_inserted)) {
×
4318
        logger.error("Overlapping reuse of session identifier %1 in BIND message",
4,018✔
4319
                     session_ident);                           // Throws
4,018✔
4320
        protocol_error(ProtocolError::reuse_of_session_ident); // Throws
×
4321
        return;
×
4322
    }
×
4323
    try {
4,018✔
4324
        p.first->second.reset(new Session(*this, session_ident)); // Throws
8✔
4325
    }
8✔
4326
    catch (...) {
16✔
4327
        m_sessions.erase(p.first);
16✔
4328
        throw;
4,002✔
4329
    }
×
4330

×
4331
    Session& sess = *p.first->second;
×
4332
    sess.initiate(); // Throws
×
4333
    ProtocolError error;
4,002✔
4334
    bool success =
×
4335
        sess.receive_bind_message(std::move(path), std::move(signed_user_token), need_client_file_ident, is_subserver,
×
4336
                                  error); // Throws
×
4337
    if (REALM_UNLIKELY(!success))         // Throws
×
4338
        protocol_error(error, &sess);     // Throws
2,008✔
4339
}
4,002✔
4340

4,002✔
4341

4,002✔
4342
void SyncConnection::receive_ident_message(session_ident_type session_ident, file_ident_type client_file_ident,
4,002✔
4343
                                           salt_type client_file_ident_salt, version_type scan_server_version,
4,002✔
4344
                                           version_type scan_client_version, version_type latest_server_version,
2,024✔
4345
                                           salt_type latest_server_version_salt)
4,002✔
4346
{
4347
    auto i = m_sessions.find(session_ident);
4348
    if (REALM_UNLIKELY(i == m_sessions.end())) {
4349
        bad_session_ident("IDENT", session_ident); // Throws
4350
        return;
44,726✔
4351
    }
44,726✔
4352
    Session& sess = *i->second;
44,726✔
4353
    if (REALM_UNLIKELY(sess.unbind_message_received())) {
×
4354
        message_after_unbind("IDENT", session_ident); // Throws
×
4355
        return;
×
4356
    }
44,726✔
4357
    if (REALM_UNLIKELY(sess.error_occurred())) {
44,726✔
4358
        // Protocol state is SendError or WaitForUnbindErr. In these states, all
×
4359
        // messages, other than UNBIND, must be ignored.
×
4360
        return;
×
4361
    }
44,726✔
4362
    if (REALM_UNLIKELY(sess.must_send_ident_message())) {
4363
        logger.error("Received IDENT message before IDENT message was sent"); // Throws
4364
        protocol_error(ProtocolError::bad_message_order);                     // Throws
×
4365
        return;
×
4366
    }
44,726✔
4367
    if (REALM_UNLIKELY(sess.ident_message_received())) {
×
4368
        logger.error("Received second IDENT message for session"); // Throws
×
4369
        protocol_error(ProtocolError::bad_message_order);          // Throws
×
4370
        return;
23,038✔
4371
    }
44,726✔
4372

44,726✔
4373
    ProtocolError error = {};
44,726✔
4374
    bool success = sess.receive_ident_message(client_file_ident, client_file_ident_salt, scan_server_version,
44,726✔
4375
                                              scan_client_version, latest_server_version, latest_server_version_salt,
23,038✔
4376
                                              error); // Throws
44,726✔
4377
    if (REALM_UNLIKELY(!success))                     // Throws
4378
        protocol_error(error, &sess);                 // Throws
4379
}
4380

12,134✔
4381
void SyncConnection::receive_upload_message(session_ident_type session_ident, version_type progress_client_version,
12,134✔
4382
                                            version_type progress_server_version, version_type locked_server_version,
12,134✔
4383
                                            const UploadChangesets& upload_changesets)
×
4384
{
×
4385
    auto i = m_sessions.find(session_ident);
×
4386
    if (REALM_UNLIKELY(i == m_sessions.end())) {
12,134✔
4387
        bad_session_ident("UPLOAD", session_ident); // Throws
12,134✔
4388
        return;
×
4389
    }
×
4390
    Session& sess = *i->second;
×
4391
    if (REALM_UNLIKELY(sess.unbind_message_received())) {
12,134✔
4392
        message_after_unbind("UPLOAD", session_ident); // Throws
24✔
4393
        return;
24✔
4394
    }
48✔
4395
    if (REALM_UNLIKELY(sess.error_occurred())) {
48✔
4396
        // Protocol state is SendError or WaitForUnbindErr. In these states, all
12,086✔
4397
        // messages, other than UNBIND, must be ignored.
×
4398
        return;
×
4399
    }
×
4400
    if (REALM_UNLIKELY(!sess.ident_message_received())) {
5,982✔
4401
        message_before_ident("UPLOAD", session_ident); // Throws
12,086✔
4402
        return;
12,086✔
4403
    }
12,086✔
4404

5,982✔
4405
    ProtocolError error = {};
12,086✔
4406
    bool success = sess.receive_upload_message(progress_client_version, progress_server_version,
4407
                                               locked_server_version, upload_changesets, error); // Throws
4408
    if (REALM_UNLIKELY(!success))                                                                // Throws
4409
        protocol_error(error, &sess);                                                            // Throws
2,476✔
4410
}
2,476✔
4411

2,476✔
4412

×
4413
void SyncConnection::receive_mark_message(session_ident_type session_ident, request_ident_type request_ident)
×
4414
{
×
4415
    auto i = m_sessions.find(session_ident);
2,476✔
4416
    if (REALM_UNLIKELY(i == m_sessions.end())) {
2,476✔
4417
        bad_session_ident("MARK", session_ident);
×
4418
        return;
×
4419
    }
×
4420
    Session& sess = *i->second;
1,452✔
4421
    if (REALM_UNLIKELY(sess.unbind_message_received())) {
2,476✔
4422
        message_after_unbind("MARK", session_ident); // Throws
1,452✔
4423
        return;
2,476✔
4424
    }
4425
    if (REALM_UNLIKELY(sess.error_occurred())) {
4426
        // Protocol state is SendError or WaitForUnbindErr. In these states, all
4427
        // messages, other than UNBIND, must be ignored.
178✔
4428
        return;
178✔
4429
    }
178✔
4430
    if (REALM_UNLIKELY(!sess.ident_message_received())) {
178✔
4431
        message_before_ident("MARK", session_ident); // Throws
178✔
4432
        return;
176✔
4433
    }
178✔
4434

4435
    ProtocolError error;
4436
    bool success = sess.receive_mark_message(request_ident, error); // Throws
4437
    if (REALM_UNLIKELY(!success))                                   // Throws
4438
        protocol_error(error, &sess);                               // Throws
×
4439
}
×
4440

×
4441

×
4442
void SyncConnection::receive_unbind_message(session_ident_type session_ident)
×
4443
{
×
4444
    auto i = m_sessions.find(session_ident); // Throws
×
4445
    if (REALM_UNLIKELY(i == m_sessions.end())) {
×
4446
        bad_session_ident("UNBIND", session_ident); // Throws
×
4447
        return;
×
4448
    }
×
4449
    Session& sess = *i->second;
×
4450
    if (REALM_UNLIKELY(sess.unbind_message_received())) {
×
4451
        message_after_unbind("UNBIND", session_ident); // Throws
4452
        return;
×
4453
    }
×
4454

4455
    sess.receive_unbind_message(); // Throws
4456
    // NOTE: The session might have gotten destroyed at this time!
4457
}
5,838✔
4458

5,838✔
4459

×
4460
void SyncConnection::receive_ping(milliseconds_type timestamp, milliseconds_type rtt)
×
4461
{
2,916✔
4462
    logger.debug("Received: PING(timestamp=%1, rtt=%2)", timestamp, rtt); // Throws
5,838✔
4463
    m_send_pong = true;
5,838✔
4464
    m_last_ping_timestamp = timestamp;
5,838✔
4465
    if (!m_is_sending)
5,838✔
4466
        send_next_message();
5,838✔
4467
}
5,838✔
4468

5,838✔
4469

4470
void SyncConnection::receive_error_message(session_ident_type session_ident, int error_code,
4471
                                           std::string_view error_body)
4472
{
×
4473
    logger.debug("Received: ERROR(error_code=%1, message_size=%2, session_ident=%3)", error_code, error_body.size(),
×
4474
                 session_ident); // Throws
×
4475
    auto i = m_sessions.find(session_ident);
×
4476
    if (REALM_UNLIKELY(i == m_sessions.end())) {
×
4477
        bad_session_ident("ERROR", session_ident);
4478
        return;
4479
    }
4480
    Session& sess = *i->second;
×
4481
    if (REALM_UNLIKELY(sess.unbind_message_received())) {
×
4482
        message_after_unbind("ERROR", session_ident); // Throws
×
4483
        return;
×
4484
    }
×
4485

4486
    sess.receive_error_message(session_ident, error_code, error_body); // Throws
4487
}
4488

×
4489
void SyncConnection::send_log_message(util::Logger::Level level, const std::string&& message,
×
4490
                                      session_ident_type sess_ident, std::optional<std::string> co_id)
×
4491
{
×
4492
    if (get_client_protocol_version() < SyncConnection::SERVER_LOG_PROTOCOL_VERSION) {
×
4493
        return logger.log(level, message.c_str());
4494
    }
4495

4496
    LogMessage log_msg{sess_ident, level, std::move(message), std::move(co_id)};
68,584✔
4497
    {
35,386✔
4498
        std::lock_guard lock(m_log_mutex);
35,386✔
4499
        m_log_messages.push(std::move(log_msg));
68,584✔
4500
    }
68,584✔
4501
    m_send_trigger->trigger();
68,584✔
4502
}
4503

4504

4505
void SyncConnection::bad_session_ident(const char* message_type, session_ident_type session_ident)
×
4506
{
4507
    logger.error("Bad session identifier in %1 message, session_ident = %2", message_type,
4508
                 session_ident);                      // Throws
×
4509
    protocol_error(ProtocolError::bad_session_ident); // Throws
×
4510
}
×
4511

4512

4513
void SyncConnection::message_after_unbind(const char* message_type, session_ident_type session_ident)
4514
{
99,380✔
4515
    logger.error("Received %1 message after UNBIND message, session_ident = %2", message_type,
99,380✔
4516
                 session_ident);                      // Throws
99,380✔
4517
    protocol_error(ProtocolError::bad_message_order); // Throws
99,380✔
4518
}
178✔
4519

178✔
4520

178✔
4521
void SyncConnection::message_before_ident(const char* message_type, session_ident_type session_ident)
99,202✔
4522
{
147,838✔
4523
    logger.error("Received %1 message before IDENT message, session_ident = %2", message_type,
147,838✔
4524
                 session_ident);                      // Throws
147,838✔
4525
    protocol_error(ProtocolError::bad_message_order); // Throws
23,164✔
4526
}
45,230✔
4527

45,220✔
4528

10✔
4529
void SyncConnection::handle_message_received(const char* data, size_t size)
20✔
4530
{
20✔
4531
    // parse_message_received() parses the message and calls the
20✔
4532
    // proper handler on the SyncConnection object (this).
20✔
4533
    get_server_protocol().parse_message_received<SyncConnection>(*this, std::string_view(data, size));
102,608✔
4534
    return;
54,286✔
4535
}
54,286✔
4536

54,286✔
4537

54,286✔
4538
void SyncConnection::handle_ping_received(const char* data, size_t size)
54,286✔
4539
{
54,286✔
4540
    // parse_message_received() parses the message and calls the
102,608✔
4541
    // proper handler on the SyncConnection object (this).
53,974✔
4542
    get_server_protocol().parse_ping_received<SyncConnection>(*this, std::string_view(data, size));
102,608✔
4543
    return;
73,676✔
4544
}
45,208✔
4545

45,208✔
4546

5,810✔
4547
void SyncConnection::send_next_message()
5,810✔
4548
{
5,810✔
4549
    REALM_ASSERT(!m_is_sending);
45,208✔
4550
    REALM_ASSERT(!m_sending_pong);
23,156✔
4551
    if (m_send_pong) {
45,208✔
4552
        send_pong(m_last_ping_timestamp);
4553
        if (m_sending_pong)
4554
            return;
4555
    }
59,786✔
4556
    for (;;) {
59,758✔
4557
        Session* sess = m_sessions_enlisted_to_send.pop_front();
59,754✔
4558
        if (!sess) {
59,642✔
4559
            // No sessions were enlisted to send
59,642✔
4560
            if (REALM_LIKELY(!m_is_closing))
59,754✔
4561
                break; // Check to see if there are any log messages to go out
31,372✔
4562
            // Send a connection level ERROR
59,786✔
4563
            REALM_ASSERT(!is_session_level_error(m_error_code));
59,786✔
4564
            initiate_write_error(m_error_code, m_error_session_ident); // Throws
59,786✔
4565
            return;
59,786✔
4566
        }
4567
        sess->send_message(); // Throws
4568
        // NOTE: The session might have gotten destroyed at this time!
4569

178✔
4570
        // At this point, `m_is_sending` is true if, and only if the session
178✔
4571
        // chose to send a message. If it chose to not send a message, we must
178✔
4572
        // loop back and give the next session in `m_sessions_enlisted_to_send`
178✔
4573
        // a chance.
178✔
4574
        if (m_is_sending)
178✔
4575
            return;
74✔
4576
    }
178✔
4577
    {
178✔
4578
        std::lock_guard lock(m_log_mutex);
178✔
4579
        if (!m_log_messages.empty()) {
178✔
4580
            send_log_message(m_log_messages.front());
74✔
4581
            m_log_messages.pop();
178✔
4582
        }
178✔
4583
    }
178✔
4584
    // Otherwise, nothing to do
4585
}
4586

4587

178✔
4588
void SyncConnection::initiate_write_output_buffer()
178✔
4589
{
178✔
4590
    auto handler = [this](std::error_code ec, size_t) {
178✔
4591
        if (!ec) {
178✔
4592
            handle_write_output_buffer();
74✔
4593
        }
178✔
4594
    };
178✔
4595

74✔
4596
    m_websocket.async_write_binary(m_output_buffer.data(), m_output_buffer.size(),
178✔
4597
                                   std::move(handler)); // Throws
178✔
4598
    m_is_sending = true;
4599
}
4600

5,810✔
4601

5,810✔
4602
void SyncConnection::initiate_pong_output_buffer()
5,810✔
4603
{
5,810✔
4604
    auto handler = [this](std::error_code ec, size_t) {
2,912✔
4605
        if (!ec) {
5,810✔
4606
            handle_pong_output_buffer();
5,810✔
4607
        }
4608
    };
4609

4610
    REALM_ASSERT(!m_is_sending);
59,644✔
4611
    REALM_ASSERT(!m_sending_pong);
59,644✔
4612
    m_websocket.async_write_binary(m_output_buffer.data(), m_output_buffer.size(),
59,644✔
4613
                                   std::move(handler)); // Throws
59,644✔
4614

59,644✔
4615
    m_is_sending = true;
4616
    m_sending_pong = true;
4617
}
4618

178✔
4619

178✔
4620
void SyncConnection::send_pong(milliseconds_type timestamp)
178✔
4621
{
178✔
4622
    REALM_ASSERT(m_send_pong);
178✔
4623
    REALM_ASSERT(!m_sending_pong);
178✔
4624
    m_send_pong = false;
178✔
4625
    logger.debug("Sending: PONG(timestamp=%1)", timestamp); // Throws
178✔
4626

4627
    OutputBuffer& out = get_output_buffer();
4628
    get_server_protocol().make_pong(out, timestamp); // Throws
4629

20✔
4630
    initiate_pong_output_buffer(); // Throws
20✔
4631
}
20✔
4632

20✔
4633
void SyncConnection::send_log_message(const LogMessage& log_msg)
10✔
4634
{
20✔
4635
    OutputBuffer& out = get_output_buffer();
20✔
4636
    get_server_protocol().make_log_message(out, log_msg.level, log_msg.message, log_msg.sess_ident,
10✔
4637
                                           log_msg.co_id); // Throws
20✔
4638

20✔
4639
    initiate_write_output_buffer(); // Throws
20✔
4640
}
20✔
4641

10✔
4642

20✔
4643
void SyncConnection::handle_write_output_buffer()
20✔
4644
{
20✔
4645
    release_output_buffer();
20✔
4646
    m_is_sending = false;
20✔
4647
    send_next_message(); // Throws
20✔
4648
}
4649

4650

4651
void SyncConnection::handle_pong_output_buffer()
20✔
4652
{
20✔
4653
    release_output_buffer();
20✔
4654
    REALM_ASSERT(m_is_sending);
20✔
4655
    REALM_ASSERT(m_sending_pong);
20✔
4656
    m_is_sending = false;
20!
4657
    m_sending_pong = false;
×
4658
    send_next_message(); // Throws
20✔
4659
}
20✔
4660

4661

4662
void SyncConnection::initiate_write_error(ProtocolError error_code, session_ident_type session_ident)
4663
{
4664
    const char* message = get_protocol_error_message(int(error_code));
4665
    std::size_t message_size = std::strlen(message);
4666
    bool try_again = determine_try_again(error_code);
4667

4668
    logger.detail("Sending: ERROR(error_code=%1, message_size=%2, try_again=%3, session_ident=%4)", int(error_code),
4669
                  message_size, try_again, session_ident); // Throws
4670

4671
    OutputBuffer& out = get_output_buffer();
4672
    int protocol_version = get_client_protocol_version();
4673
    get_server_protocol().make_error_message(protocol_version, out, error_code, message, message_size, try_again,
4674
                                             session_ident); // Throws
4675

76✔
4676
    auto handler = [this](std::error_code ec, size_t) {
76✔
4677
        handle_write_error(ec); // Throws
76✔
4678
    };
76✔
4679
    m_websocket.async_write_binary(out.data(), out.size(), std::move(handler));
76✔
4680
    m_is_sending = true;
76✔
4681
}
×
4682

×
4683

×
4684
void SyncConnection::handle_write_error(std::error_code ec)
×
4685
{
76✔
4686
    m_is_sending = false;
76✔
4687
    REALM_ASSERT(m_is_closing);
76✔
4688
    if (!m_ssl_stream) {
76✔
4689
        m_socket->shutdown(network::Socket::shutdown_send, ec);
76✔
4690
        if (ec && ec != make_basic_system_error_code(ENOTCONN))
×
4691
            throw std::system_error(ec);
×
4692
    }
4693
}
4694

4695

20✔
4696
// For connection level errors, `sess` is ignored. For session level errors, a
20✔
4697
// session must be specified.
10✔
4698
//
10✔
4699
// If a session is specified, that session object will have been detached from
10✔
4700
// the ServerFile object (and possibly destroyed) upon return from
10✔
4701
// protocol_error().
10✔
4702
//
10✔
4703
// If a session is specified for a protocol level error, that session object
20✔
4704
// will have been destroyed upon return from protocol_error(). For session level
20✔
4705
// errors, the specified session will have been destroyed upon return from
10✔
4706
// protocol_error() if, and only if the negotiated protocol version is less than
20✔
4707
// 23.
20✔
4708
void SyncConnection::protocol_error(ProtocolError error_code, Session* sess)
20✔
4709
{
10✔
4710
    REALM_ASSERT(!m_is_closing);
20✔
4711
    bool session_level = is_session_level_error(error_code);
20✔
4712
    REALM_ASSERT(!session_level || sess);
10✔
4713
    REALM_ASSERT(!sess || m_sessions.count(sess->get_session_ident()) == 1);
10✔
4714
    if (logger.would_log(util::Logger::Level::debug)) {
20✔
4715
        const char* message = get_protocol_error_message(int(error_code));
20✔
4716
        Logger& logger_2 = (session_level ? sess->logger : logger);
10✔
4717
        logger_2.debug("Protocol error: %1 (error_code=%2)", message, int(error_code)); // Throws
20✔
4718
    }
10✔
4719
    session_ident_type session_ident = (session_level ? sess->get_session_ident() : 0);
20✔
4720
    if (session_level) {
10✔
4721
        sess->initiate_deactivation(error_code); // Throws
20✔
4722
        return;
20✔
4723
    }
4724
    do_initiate_soft_close(error_code, session_ident); // Throws
4725
}
4726

634✔
4727

538✔
4728
void SyncConnection::do_initiate_soft_close(ProtocolError error_code, session_ident_type session_ident)
336✔
4729
{
634✔
4730
    REALM_ASSERT(get_protocol_error_message(int(error_code)));
634✔
4731

4732
    // With recent versions of the protocol (when the version is greater than,
4733
    // or equal to 23), this function will only be called for connection level
4734
    // errors, never for session specific errors. However, for the purpose of
440✔
4735
    // emulating earlier protocol versions, this function might be called for
244✔
4736
    // session specific errors too.
440✔
4737
    REALM_ASSERT(is_session_level_error(error_code) == (session_ident != 0));
440✔
4738
    REALM_ASSERT(!is_session_level_error(error_code));
440✔
4739

4740
    REALM_ASSERT(m_send_trigger);
4741
    REALM_ASSERT(!m_is_closing);
4742
    m_is_closing = true;
1,098✔
4743

1,674✔
4744
    m_error_code = error_code;
1,674✔
4745
    m_error_session_ident = session_ident;
1,674✔
4746

1,674✔
4747
    // Don't waste time and effort sending any other messages
1,098✔
4748
    m_send_pong = false;
1,098✔
4749
    m_sessions_enlisted_to_send.clear();
1,098✔
4750

4751
    m_receiving_session = nullptr;
4752

4753
    terminate_sessions(); // Throws
20✔
4754

20✔
4755
    m_send_trigger->trigger();
20✔
4756
}
20✔
4757

20✔
4758

20✔
4759
void SyncConnection::close_due_to_close_by_client(std::error_code ec)
4760
{
4761
    auto log_level = (ec == util::MiscExtErrors::end_of_input ? Logger::Level::detail : Logger::Level::info);
4762
    // Suicide
2,468✔
4763
    terminate(log_level, "Sync connection closed by client: %1", ec.message()); // Throws
2,468✔
4764
}
2,468✔
4765

4766

4767
void SyncConnection::close_due_to_error(std::error_code ec)
4768
{
4769
    // Suicide
4770
    terminate(Logger::Level::error, "Sync connection closed due to error: %1",
4771
              ec.message()); // Throws
4772
}
4773

4774

4775
void SyncConnection::terminate_sessions()
7,968✔
4776
{
7,968✔
4777
    for (auto& entry : m_sessions) {
7,968✔
4778
        Session& sess = *entry.second;
4779
        sess.terminate(); // Throws
4780
    }
4781
    m_sessions_enlisted_to_send.clear();
4782
    m_sessions.clear();
4783
}
7,968✔
4784

7,968✔
4785

4786
void SyncConnection::initiate_soft_close()
4787
{
4788
    if (!m_is_closing) {
4789
        session_ident_type session_ident = 0;                                    // Not session specific
×
4790
        do_initiate_soft_close(ProtocolError::connection_closed, session_ident); // Throws
×
4791
    }
4792
}
4793

7,968✔
4794

4795
void SyncConnection::discard_session(session_ident_type session_ident) noexcept
4796
{
4797
    m_sessions.erase(session_ident);
7,296✔
4798
}
7,296✔
4799

7,296✔
4800
} // anonymous namespace
4801

4802

4803
// ============================ sync::Server implementation ============================
672✔
4804

672✔
4805
class Server::Implementation : public ServerImpl {
672✔
4806
public:
4807
    Implementation(const std::string& root_dir, util::Optional<PKey> pkey, Server::Config config)
4808
        : ServerImpl{root_dir, std::move(pkey), std::move(config)} // Throws
4809
    {
7,972✔
4810
    }
7,972✔
4811
    virtual ~Implementation() {}
7,972✔
4812
};
4813

4814

4815
Server::Server(const std::string& root_dir, util::Optional<sync::PKey> pkey, Config config)
7,916✔
4816
    : m_impl{new Implementation{root_dir, std::move(pkey), std::move(config)}} // Throws
7,916✔
4817
{
7,916✔
4818
}
4819

4820

4821
Server::Server(Server&& serv) noexcept
8,810✔
4822
    : m_impl{std::move(serv.m_impl)}
8,810✔
4823
{
8,810✔
4824
}
4825

4826

4827
Server::~Server() noexcept {}
672✔
4828

672✔
4829

672✔
4830
void Server::start()
4831
{
4832
    m_impl->start(); // Throws
4833
}
4834

×
4835

×
4836
void Server::start(const std::string& listen_address, const std::string& listen_port, bool reuse_address)
×
4837
{
4838
    m_impl->start(listen_address, listen_port, reuse_address); // Throws
4839
}
4840

4✔
4841

4✔
4842
network::Endpoint Server::listen_endpoint() const
4✔
4843
{
4844
    return m_impl->listen_endpoint(); // Throws
4845
}
4846

20✔
4847

20✔
4848
void Server::run()
20✔
4849
{
4850
    m_impl->run(); // Throws
4851
}
4852

72✔
4853

72✔
4854
void Server::stop() noexcept
72✔
4855
{
4856
    m_impl->stop();
4857
}
4858

4,800✔
4859

4,800✔
4860
uint_fast64_t Server::errors_seen() const noexcept
4,800✔
4861
{
4862
    return m_impl->errors_seen;
4863
}
4864

×
4865

×
4866
void Server::stop_sync_and_wait_for_backup_completion(util::UniqueFunction<void(bool did_backup)> completion_handler,
×
4867
                                                      milliseconds_type timeout)
4868
{
4869
    m_impl->stop_sync_and_wait_for_backup_completion(std::move(completion_handler), timeout); // Throws
4870
}
4871

4872

4873
void Server::set_connection_reaper_timeout(milliseconds_type timeout)
4874
{
4875
    m_impl->set_connection_reaper_timeout(timeout);
4876
}
4877

4878

4879
void Server::close_connections()
4880
{
4881
    m_impl->close_connections();
4882
}
4883

4884

4885
bool Server::map_virtual_to_real_path(const std::string& virt_path, std::string& real_path)
4886
{
4887
    return m_impl->map_virtual_to_real_path(virt_path, real_path); // Throws
4888
}
4889

4890

4891
void Server::recognize_external_change(const std::string& virt_path)
4892
{
4893
    m_impl->recognize_external_change(virt_path); // Throws
4894
}
4895

4896

4897
void Server::get_workunit_timers(milliseconds_type& parallel_section, milliseconds_type& sequential_section)
4898
{
4899
    m_impl->get_workunit_timers(parallel_section, sequential_section);
4900
}
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

© 2025 Coveralls, Inc