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

realm / realm-core / nikola.irinchev_478

10 Jun 2024 05:02PM UTC coverage: 90.821% (-0.2%) from 90.975%
nikola.irinchev_478

Pull #7792

Evergreen

nirinchev
Replace app_config_get_sync_client_config with _set_sync_client_config
Pull Request #7792: Replace app_config_get_sync_client_config with _set_sync_client_config

101696 of 180086 branches covered (56.47%)

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

1872 existing lines in 57 files now uncovered.

214568 of 236253 relevant lines covered (90.82%)

5116114.57 hits per line

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

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

19
#ifndef REALM_SYNC_CONFIG_HPP
20
#define REALM_SYNC_CONFIG_HPP
21

22
#include <realm/exceptions.hpp>
23
#include <realm/sync/protocol.hpp>
24

25
#include <functional>
26
#include <map>
27
#include <memory>
28
#include <optional>
29
#include <string>
30
#include <unordered_map>
31

32
namespace realm {
33

34
class SyncUser;
35
class SyncSession;
36
class Realm;
37
class ThreadSafeReference;
38

39
namespace bson {
40
class Bson;
41
}
42

43
namespace sync {
44
using port_type = std::uint_fast16_t;
45
enum class ProtocolError;
46
} // namespace sync
47

48
struct SyncError {
49
    enum class ClientResetModeAllowed { DoNotClientReset, RecoveryPermitted, RecoveryNotPermitted };
50

51
    Status status;
52

53
    bool is_fatal;
54

55
    // The following two string_view's are views into the reason string of the status member. Users of
56
    // SyncError should take care not to modify the status if they are going to access these views into
57
    // the reason string.
58
    // Just the minimal error message, without any log URL.
59
    std::string_view simple_message;
60
    // The URL to the associated server log if any. If not supplied by the server, this will be `empty()`.
61
    std::string_view logURL;
62
    /// A dictionary of extra user information associated with this error.
63
    /// If this is a client reset error, the keys for c_original_file_path_key and c_recovery_file_path_key will be
64
    /// populated with the relevant filesystem paths.
65
    std::unordered_map<std::string, std::string> user_info;
66
    /// The sync server may send down an error that the client does not recognize,
67
    /// whether because of a version mismatch or an oversight. It is still valuable
68
    /// to expose these errors so that users can do something about them.
69
    bool is_unrecognized_by_client = false;
70
    // the server may explicitly send down an action the client should take as part of an error (i.e, client reset)
71
    // if this is set, it overrides the clients interpretation of the error
72
    sync::ProtocolErrorInfo::Action server_requests_action = sync::ProtocolErrorInfo::Action::NoAction;
73
    // If this error resulted from a compensating write, this vector will contain information about each object
74
    // that caused a compensating write and why the write was illegal.
75
    std::vector<sync::CompensatingWriteErrorInfo> compensating_writes_info;
76

77
    SyncError(Status status, bool is_fatal, std::optional<std::string_view> server_log = std::nullopt,
78
              std::vector<sync::CompensatingWriteErrorInfo> compensating_writes = {});
79

80
    static constexpr const char c_original_file_path_key[] = "ORIGINAL_FILE_PATH";
81
    static constexpr const char c_recovery_file_path_key[] = "RECOVERY_FILE_PATH";
82

83
    /// The error indicates a client reset situation.
84
    bool is_client_reset_requested() const;
85
};
86

87
using SyncSessionErrorHandler = void(std::shared_ptr<SyncSession>, SyncError);
88

89
enum class ReconnectMode {
90
    /// This is the mode that should always be used by SDKs. In this
91
    /// mode the client uses a scheme for determining a reconnect delay that
92
    /// prevents it from creating too many connection requests in a short
93
    /// amount of time (i.e., a server hammering protection mechanism).
94
    normal,
95

96
    /// For internal sync-client testing purposes only.
97
    ///
98
    /// Never reconnect automatically after the connection is closed due to
99
    /// an error. Allow immediate reconnect if the connection was closed
100
    /// voluntarily (e.g., due to sessions being abandoned).
101
    ///
102
    /// In this mode, Client::cancel_reconnect_delay() and
103
    /// Session::cancel_reconnect_delay() can still be used to trigger
104
    /// another reconnection attempt (with no delay) after an error has
105
    /// caused the connection to be closed.
106
    testing
107
};
108

109
enum class SyncSessionStopPolicy {
110
    Immediately,          // Immediately stop the session as soon as all Realms/Sessions go out of scope.
111
    LiveIndefinitely,     // Never stop the session.
112
    AfterChangesUploaded, // Once all Realms/Sessions go out of scope, wait for uploads to complete and stop.
113
};
114

115
enum class ClientResyncMode : unsigned char {
116
    // Fire a client reset error
117
    Manual,
118
    // Discard local changes, without disrupting accessors or closing the Realm
119
    DiscardLocal,
120
    // Attempt to recover unsynchronized but committed changes.
121
    Recover,
122
    // Attempt recovery and if that fails, discard local.
123
    RecoverOrDiscard,
124
};
125

126
enum class SyncClientHookEvent {
127
    DownloadMessageReceived,
128
    DownloadMessageIntegrated,
129
    BootstrapMessageProcessed,
130
    BootstrapProcessed,
131
    ErrorMessageReceived,
132
    SessionActivating,
133
    SessionSuspended,
134
    BindMessageSent,
135
    BootstrapBatchAboutToProcess,
136
};
137

138
enum class SyncClientHookAction {
139
    NoAction,
140
    EarlyReturn,
141
    SuspendWithRetryableError,
142
    TriggerReconnect,
143
};
144

145
inline std::ostream& operator<<(std::ostream& os, SyncClientHookAction action)
UNCOV
146
{
×
147
    switch (action) {
×
148
        case SyncClientHookAction::NoAction:
×
149
            return os << "NoAction";
×
150
        case SyncClientHookAction::EarlyReturn:
×
151
            return os << "EarlyReturn";
×
152
        case SyncClientHookAction::SuspendWithRetryableError:
×
153
            return os << "SuspendWithRetryableError";
×
154
        case SyncClientHookAction::TriggerReconnect:
×
155
            return os << "TriggerReconnect";
×
156
    }
×
157
    REALM_TERMINATE("Invalid SyncClientHookAction value");
UNCOV
158
}
×
159

160
struct SyncClientHookData {
161
    SyncClientHookEvent event;
162
    sync::SyncProgress progress;
163
    int64_t query_version;
164
    sync::DownloadBatchState batch_state;
165
    size_t num_changesets;
166
    const sync::ProtocolErrorInfo* error_info = nullptr;
167
};
168

169
struct SyncConfig {
170
    struct FLXSyncEnabled {};
171

172
    struct ProxyConfig {
173
        using port_type = sync::port_type;
174
        enum class Type { HTTP, HTTPS } type;
175
        std::string address;
176
        port_type port;
177
    };
178
    using SSLVerifyCallback = bool(const std::string& server_address, ProxyConfig::port_type server_port,
179
                                   const char* pem_data, size_t pem_size, int preverify_ok, int depth);
180

181
    std::shared_ptr<SyncUser> user;
182
    std::string partition_value;
183
    SyncSessionStopPolicy stop_policy = SyncSessionStopPolicy::AfterChangesUploaded;
184
    std::function<SyncSessionErrorHandler> error_handler;
185
    bool flx_sync_requested = false;
186

187
    // When integrating a flexible sync bootstrap, process this many bytes of changeset data in a single integration
188
    // attempt. This many bytes of changesets will be uncompressed and held in memory while being applied.
189
    size_t flx_bootstrap_batch_size_bytes = 1024 * 1024;
190

191
    // {@
192
    /// DEPRECATED - Will be removed in a future release
193
    // The following parameters are only used by the default SyncSocket implementation. Custom SyncSocket
194
    // implementations must handle these directly, if these features are supported.
195
    util::Optional<std::string> authorization_header_name; // not used
196
    std::map<std::string, std::string> custom_http_headers;
197
    bool client_validate_ssl = true;
198
    util::Optional<std::string> ssl_trust_certificate_path;
199
    std::function<SSLVerifyCallback> ssl_verify_callback;
200
    util::Optional<ProxyConfig> proxy_config;
201
    // @}
202

203
    // If true, upload/download waits are canceled on any sync error and not just fatal ones
204
    bool cancel_waits_on_nonfatal_error = false;
205

206
    // If false, changesets incoming from the server are discarded without
207
    // applying them to the Realm file. This is required when writing objects
208
    // directly to replication, and will break horribly otherwise
209
    bool apply_server_changes = true;
210

211
    // The name of the directory which Realms should be backed up to following
212
    // a client reset in ClientResyncMode::Manual mode
213
    util::Optional<std::string> recovery_directory;
214
    ClientResyncMode client_resync_mode = ClientResyncMode::Manual;
215
    std::function<void(std::shared_ptr<Realm> before)> notify_before_client_reset;
216
    std::function<void(std::shared_ptr<Realm> frozen_before, ThreadSafeReference after, bool did_recover)>
217
        notify_after_client_reset;
218
    // If true, the Realm passed as the `before` argument to the before reset
219
    // callbacks will be frozen
220
    bool freeze_before_reset_realm = true;
221

222
    // Used by core testing to hook into the sync client when various events occur and maybe inject
223
    // errors/disconnects deterministically.
224
    std::function<SyncClientHookAction(std::weak_ptr<SyncSession>, const SyncClientHookData&)>
225
        on_sync_client_event_hook;
226

227
    bool simulate_integration_error = false;
228

229
    // callback invoked right after DataInitializationFunction. It is used in order to setup an initial subscription.
230
    using SubscriptionInitializerCallback = std::function<void(std::shared_ptr<Realm>)>;
231
    SubscriptionInitializerCallback subscription_initializer;
232

233
    // in case the initial subscription contains a dynamic query, the user may want to force
234
    // the query to be run again every time the realm is opened. This flag should be set to true
235
    // in this case.
236
    bool rerun_init_subscription_on_open{false};
237

238
    SyncConfig() = default;
239
    explicit SyncConfig(std::shared_ptr<SyncUser> user, bson::Bson partition);
240
    explicit SyncConfig(std::shared_ptr<SyncUser> user, std::string partition);
241
    explicit SyncConfig(std::shared_ptr<SyncUser> user, const char* partition);
242
    explicit SyncConfig(std::shared_ptr<SyncUser> user, FLXSyncEnabled);
243
};
244

245
} // namespace realm
246

247
#endif // REALM_SYNC_CONFIG_HPP
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