Skip to content

Commit a592484

Browse files
committed
Keep the metadata Realm open rather than reopening it on each use
1 parent a9c9b6c commit a592484

File tree

5 files changed

+45
-42
lines changed

5 files changed

+45
-42
lines changed

src/realm/object-store/sync/async_open_task.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <realm/object-store/impl/realm_coordinator.hpp>
2424
#include <realm/object-store/sync/sync_session.hpp>
2525
#include <realm/object-store/thread_safe_reference.hpp>
26+
#include <realm/object-store/util/scheduler.hpp>
2627

2728
namespace realm {
2829

@@ -126,7 +127,7 @@ void AsyncOpenTask::wait_for_bootstrap_or_complete(AsyncOpenCallback&& callback,
126127

127128
SharedRealm shared_realm;
128129
try {
129-
shared_realm = coordinator->get_realm(nullptr, m_db_first_open);
130+
shared_realm = coordinator->get_realm(util::Scheduler::make_dummy(), m_db_first_open);
130131
}
131132
catch (...) {
132133
async_open_complete(std::move(callback), coordinator, exception_to_status());

src/realm/object-store/sync/impl/app_metadata.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818

1919
#include <realm/object-store/sync/impl/app_metadata.hpp>
2020

21-
#include <realm/object-store/sync/impl/sync_file.hpp>
21+
#include <realm/object-store/impl/realm_coordinator.hpp>
2222
#include <realm/object-store/object_schema.hpp>
2323
#include <realm/object-store/object_store.hpp>
2424
#include <realm/object-store/property.hpp>
2525
#include <realm/object-store/results.hpp>
2626
#include <realm/object-store/schema.hpp>
27+
#include <realm/object-store/sync/impl/sync_file.hpp>
2728
#include <realm/object-store/util/scheduler.hpp>
29+
2830
#if REALM_PLATFORM_APPLE
2931
#include <realm/object-store/impl/apple/keychain_helper.hpp>
3032
#endif
@@ -336,13 +338,13 @@ std::shared_ptr<Realm> open_realm(RealmConfig& config, const app::AppConfig& app
336338
}
337339

338340
struct PersistedSyncMetadataManager : public app::MetadataStore {
339-
RealmConfig m_config;
341+
std::shared_ptr<_impl::RealmCoordinator> m_coordinator;
340342
SyncUserSchema m_user_schema;
341343
FileActionSchema m_file_action_schema;
342344
UserIdentitySchema m_user_identity_schema;
343345
CurrentUserSchema m_current_user_schema;
344346

345-
PersistedSyncMetadataManager(std::string path, const app::AppConfig& app_config, SyncFileManager& file_manager)
347+
PersistedSyncMetadataManager(const app::AppConfig& app_config, SyncFileManager& file_manager)
346348
{
347349
// Note that there are several deferred schema changes which don't
348350
// justify bumping the schema version by themself, but should be done
@@ -353,26 +355,27 @@ struct PersistedSyncMetadataManager : public app::MetadataStore {
353355
// - change most of the nullable columns to non-nullable
354356
constexpr uint64_t SCHEMA_VERSION = 7;
355357

356-
m_config.automatic_change_notifications = false;
357-
m_config.path = std::move(path);
358-
m_config.schema = Schema{
358+
RealmConfig config;
359+
config.automatic_change_notifications = false;
360+
config.path = file_manager.metadata_path();
361+
config.schema = Schema{
359362
UserIdentitySchema::object_schema(),
360363
SyncUserSchema::object_schema(),
361364
FileActionSchema::object_schema(),
362365
CurrentUserSchema::object_schema(),
363366
};
364367

365-
m_config.schema_version = SCHEMA_VERSION;
366-
m_config.schema_mode = SchemaMode::Automatic;
367-
m_config.scheduler = util::Scheduler::make_dummy();
368-
m_config.automatically_handle_backlinks_in_migrations = true;
369-
m_config.migration_function = [](std::shared_ptr<Realm> old_realm, std::shared_ptr<Realm> realm, Schema&) {
368+
config.schema_version = SCHEMA_VERSION;
369+
config.schema_mode = SchemaMode::Automatic;
370+
config.scheduler = util::Scheduler::make_dummy();
371+
config.automatically_handle_backlinks_in_migrations = true;
372+
config.migration_function = [](std::shared_ptr<Realm> old_realm, std::shared_ptr<Realm> realm, Schema&) {
370373
if (old_realm->schema_version() < 7) {
371374
migrate_to_v7(old_realm, realm);
372375
}
373376
};
374377

375-
auto realm = open_realm(m_config, app_config);
378+
auto realm = open_realm(config, app_config);
376379
m_user_schema.read(*realm);
377380
m_file_action_schema.read(*realm);
378381
m_user_identity_schema.read(*realm);
@@ -382,11 +385,13 @@ struct PersistedSyncMetadataManager : public app::MetadataStore {
382385
perform_file_actions(*realm, file_manager);
383386
remove_dead_users(*realm, file_manager);
384387
realm->commit_transaction();
388+
389+
m_coordinator = _impl::RealmCoordinator::get_existing_coordinator(config.path);
385390
}
386391

387392
std::shared_ptr<Realm> get_realm() const
388393
{
389-
return Realm::get_shared_realm(m_config);
394+
return m_coordinator->get_realm(util::Scheduler::make_dummy());
390395
}
391396

392397
void remove_dead_users(Realm& realm, SyncFileManager& file_manager)
@@ -933,5 +938,5 @@ std::unique_ptr<app::MetadataStore> app::create_metadata_store(const AppConfig&
933938
if (config.metadata_mode == AppConfig::MetadataMode::InMemory) {
934939
return std::make_unique<InMemoryMetadataStorage>();
935940
}
936-
return std::make_unique<PersistedSyncMetadataManager>(file_manager.metadata_path(), config, file_manager);
941+
return std::make_unique<PersistedSyncMetadataManager>(config, file_manager);
937942
}

test/object-store/realm.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,22 +1344,18 @@ TEST_CASE("Get Realm using Async Open", "[sync][pbs][async open]") {
13441344
// User should be logged in at this point
13451345
REQUIRE(config.sync_config->user->is_logged_in());
13461346

1347-
auto task = Realm::get_synchronized_realm(config);
1348-
auto pf = util::make_promise_future<std::exception_ptr>();
1349-
task->start([&pf](auto ref, auto error) mutable {
1350-
REQUIRE(!ref);
1351-
REQUIRE(error);
1352-
pf.promise.emplace_value(error);
1353-
});
1347+
auto status = async_open_realm(config);
1348+
REQUIRE_FALSE(status.is_ok());
13541349

1355-
auto result = pf.future.get_no_throw();
1356-
REQUIRE(result.is_ok());
1357-
REQUIRE(result.get_value());
1358-
std::lock_guard<std::mutex> lock(mutex);
1359-
REQUIRE(location_refresh_called);
1360-
if (failure != FailureMode::location_fails) {
1361-
REQUIRE(token_refresh_called);
1350+
{
1351+
std::lock_guard lock(mutex);
1352+
REQUIRE(location_refresh_called);
1353+
if (failure != FailureMode::location_fails) {
1354+
REQUIRE(token_refresh_called);
1355+
}
13621356
}
1357+
1358+
app->sync_manager()->tear_down_for_testing();
13631359
}
13641360

13651361
#endif // REALM_APP_SERVICES

test/object-store/sync/flx_schema_migration.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ TEST_CASE("Schema version mismatch between client and server", "[sync][flx][flx
389389
realm->sync_session()->shutdown_and_wait();
390390
check_realm_schema(config.path, schema_v0, 0);
391391
}
392-
_impl::RealmCoordinator::assert_no_open_realms();
392+
REQUIRE_FALSE(_impl::RealmCoordinator::get_existing_coordinator(config.path));
393393

394394
SECTION("Realm already on the latest schema version") {
395395
DBOptions options;
@@ -647,7 +647,7 @@ TEST_CASE("An interrupted schema migration can recover on the next session",
647647
wait_for_upload(*realm);
648648
check_realm_schema(config.path, schema_v0, 0);
649649
}
650-
_impl::RealmCoordinator::assert_no_open_realms();
650+
REQUIRE_FALSE(_impl::RealmCoordinator::get_existing_coordinator(config.path));
651651

652652
const AppSession& app_session = harness.session().app_session();
653653
auto schema_v1 = get_schema_v1();
@@ -754,7 +754,7 @@ TEST_CASE("Client reset during schema migration", "[sync][flx][flx schema migrat
754754
std::any(AnyDict{{"_id", ObjectId::gen()}, {"queryable_int_field", static_cast<int64_t>(42)}}));
755755
realm->commit_transaction();
756756
}
757-
_impl::RealmCoordinator::assert_no_open_realms();
757+
REQUIRE_FALSE(_impl::RealmCoordinator::get_existing_coordinator(config.path));
758758

759759
const AppSession& app_session = harness.session().app_session();
760760
auto schema_v1 = get_schema_v1();
@@ -842,7 +842,7 @@ TEST_CASE("Migrate to new schema version after migration to intermediate version
842842
realm->commit_transaction();
843843
realm->close();
844844
}
845-
_impl::RealmCoordinator::assert_no_open_realms();
845+
REQUIRE_FALSE(_impl::RealmCoordinator::get_existing_coordinator(config.path));
846846

847847
const AppSession& app_session = harness.session().app_session();
848848
auto schema_v1 = get_schema_v1();
@@ -989,7 +989,7 @@ TEST_CASE("Client reset and schema migration", "[sync][flx][flx schema migration
989989
// Trigger a client reset.
990990
reset_utils::trigger_client_reset(harness.session().app_session(), *realm->sync_session());
991991
}
992-
_impl::RealmCoordinator::assert_no_open_realms();
992+
REQUIRE_FALSE(_impl::RealmCoordinator::get_existing_coordinator(config.path));
993993

994994
const AppSession& app_session = harness.session().app_session();
995995
auto schema_v1 = get_schema_v1();

test/object-store/sync/flx_sync.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,7 +1369,7 @@ TEST_CASE("flx: client reset", "[sync][flx][client reset][baas]") {
13691369
realm->sync_session()->shutdown_and_wait();
13701370
realm->close();
13711371
}
1372-
_impl::RealmCoordinator::assert_no_open_realms();
1372+
REQUIRE_FALSE(_impl::RealmCoordinator::get_existing_coordinator(config_local.path));
13731373
{
13741374
// ensure that an additional schema change after the successful reset is also accepted by the server
13751375
changed_schema[0].persisted_properties.push_back(
@@ -2311,7 +2311,8 @@ TEST_CASE("flx: interrupted bootstrap restarts/recovers on reconnect", "[sync][f
23112311
realm->sync_session()->shutdown_and_wait();
23122312
}
23132313

2314-
_impl::RealmCoordinator::assert_no_open_realms();
2314+
// Verify that the file was fully closed
2315+
REQUIRE(DB::call_with_lock(interrupted_realm_config.path, [](auto&) {}));
23152316

23162317
{
23172318
DBOptions options;
@@ -2840,7 +2841,7 @@ TEST_CASE("flx: bootstrap batching prevents orphan documents", "[sync][flx][boot
28402841
realm->close();
28412842
}
28422843

2843-
_impl::RealmCoordinator::assert_no_open_realms();
2844+
REQUIRE_FALSE(_impl::RealmCoordinator::get_existing_coordinator(interrupted_realm_config.path));
28442845

28452846
// Open up the realm without the sync client attached and verify that the realm got interrupted in the state
28462847
// we expected it to be in.
@@ -2933,7 +2934,7 @@ TEST_CASE("flx: bootstrap batching prevents orphan documents", "[sync][flx][boot
29332934
realm->close();
29342935
}
29352936

2936-
_impl::RealmCoordinator::assert_no_open_realms();
2937+
REQUIRE_FALSE(_impl::RealmCoordinator::get_existing_coordinator(interrupted_realm_config.path));
29372938

29382939
// Open up the realm without the sync client attached and verify that the realm got interrupted in the state
29392940
// we expected it to be in.
@@ -3002,7 +3003,7 @@ TEST_CASE("flx: bootstrap batching prevents orphan documents", "[sync][flx][boot
30023003
realm->close();
30033004
}
30043005

3005-
_impl::RealmCoordinator::assert_no_open_realms();
3006+
REQUIRE_FALSE(_impl::RealmCoordinator::get_existing_coordinator(interrupted_realm_config.path));
30063007

30073008
// Open up the realm without the sync client attached and verify that the realm got interrupted in the state
30083009
// we expected it to be in.
@@ -3079,7 +3080,7 @@ TEST_CASE("flx: bootstrap batching prevents orphan documents", "[sync][flx][boot
30793080
realm->close();
30803081
}
30813082

3082-
_impl::RealmCoordinator::assert_no_open_realms();
3083+
REQUIRE_FALSE(_impl::RealmCoordinator::get_existing_coordinator(interrupted_realm_config.path));
30833084

30843085
// Open up the realm without the sync client attached and verify that the realm got interrupted in the state
30853086
// we expected it to be in.
@@ -4583,7 +4584,7 @@ TEST_CASE("flx: open realm + register subscription callback while bootstrapping"
45834584
realm->close();
45844585
}
45854586

4586-
_impl::RealmCoordinator::assert_no_open_realms();
4587+
REQUIRE_FALSE(_impl::RealmCoordinator::get_existing_coordinator(config.path));
45874588

45884589
// Client 2 uploads data matching Client 1's subscription at version 1
45894590
harness.load_initial_data([&](SharedRealm realm) {
@@ -4742,7 +4743,7 @@ TEST_CASE("flx sync: resend pending subscriptions when reconnecting", "[sync][fl
47424743
realm->close();
47434744
}
47444745

4745-
_impl::RealmCoordinator::assert_no_open_realms();
4746+
REQUIRE_FALSE(_impl::RealmCoordinator::get_existing_coordinator(interrupted_realm_config.path));
47464747

47474748
// Check at least one subscription set needs to be resent.
47484749
{

0 commit comments

Comments
 (0)