@powersync/service-core 0.0.0-dev-20240620165206
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.probes/.gitkeep +0 -0
- package/CHANGELOG.md +82 -0
- package/LICENSE +67 -0
- package/README.md +3 -0
- package/dist/api/api-index.d.ts +2 -0
- package/dist/api/api-index.js +3 -0
- package/dist/api/api-index.js.map +1 -0
- package/dist/api/diagnostics.d.ts +21 -0
- package/dist/api/diagnostics.js +183 -0
- package/dist/api/diagnostics.js.map +1 -0
- package/dist/api/schema.d.ts +5 -0
- package/dist/api/schema.js +88 -0
- package/dist/api/schema.js.map +1 -0
- package/dist/auth/CachedKeyCollector.d.ts +46 -0
- package/dist/auth/CachedKeyCollector.js +116 -0
- package/dist/auth/CachedKeyCollector.js.map +1 -0
- package/dist/auth/CompoundKeyCollector.d.ts +8 -0
- package/dist/auth/CompoundKeyCollector.js +23 -0
- package/dist/auth/CompoundKeyCollector.js.map +1 -0
- package/dist/auth/JwtPayload.d.ts +10 -0
- package/dist/auth/JwtPayload.js +2 -0
- package/dist/auth/JwtPayload.js.map +1 -0
- package/dist/auth/KeyCollector.d.ts +24 -0
- package/dist/auth/KeyCollector.js +2 -0
- package/dist/auth/KeyCollector.js.map +1 -0
- package/dist/auth/KeySpec.d.ts +26 -0
- package/dist/auth/KeySpec.js +49 -0
- package/dist/auth/KeySpec.js.map +1 -0
- package/dist/auth/KeyStore.d.ts +39 -0
- package/dist/auth/KeyStore.js +131 -0
- package/dist/auth/KeyStore.js.map +1 -0
- package/dist/auth/LeakyBucket.d.ts +39 -0
- package/dist/auth/LeakyBucket.js +57 -0
- package/dist/auth/LeakyBucket.js.map +1 -0
- package/dist/auth/RemoteJWKSCollector.d.ts +24 -0
- package/dist/auth/RemoteJWKSCollector.js +106 -0
- package/dist/auth/RemoteJWKSCollector.js.map +1 -0
- package/dist/auth/StaticKeyCollector.d.ts +14 -0
- package/dist/auth/StaticKeyCollector.js +19 -0
- package/dist/auth/StaticKeyCollector.js.map +1 -0
- package/dist/auth/SupabaseKeyCollector.d.ts +22 -0
- package/dist/auth/SupabaseKeyCollector.js +61 -0
- package/dist/auth/SupabaseKeyCollector.js.map +1 -0
- package/dist/auth/auth-index.d.ts +10 -0
- package/dist/auth/auth-index.js +11 -0
- package/dist/auth/auth-index.js.map +1 -0
- package/dist/db/db-index.d.ts +1 -0
- package/dist/db/db-index.js +2 -0
- package/dist/db/db-index.js.map +1 -0
- package/dist/db/mongo.d.ts +29 -0
- package/dist/db/mongo.js +65 -0
- package/dist/db/mongo.js.map +1 -0
- package/dist/entry/cli-entry.d.ts +15 -0
- package/dist/entry/cli-entry.js +36 -0
- package/dist/entry/cli-entry.js.map +1 -0
- package/dist/entry/commands/config-command.d.ts +10 -0
- package/dist/entry/commands/config-command.js +21 -0
- package/dist/entry/commands/config-command.js.map +1 -0
- package/dist/entry/commands/migrate-action.d.ts +2 -0
- package/dist/entry/commands/migrate-action.js +18 -0
- package/dist/entry/commands/migrate-action.js.map +1 -0
- package/dist/entry/commands/start-action.d.ts +3 -0
- package/dist/entry/commands/start-action.js +15 -0
- package/dist/entry/commands/start-action.js.map +1 -0
- package/dist/entry/commands/teardown-action.d.ts +2 -0
- package/dist/entry/commands/teardown-action.js +17 -0
- package/dist/entry/commands/teardown-action.js.map +1 -0
- package/dist/entry/entry-index.d.ts +5 -0
- package/dist/entry/entry-index.js +6 -0
- package/dist/entry/entry-index.js.map +1 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -0
- package/dist/locks/LockManager.d.ts +10 -0
- package/dist/locks/LockManager.js +7 -0
- package/dist/locks/LockManager.js.map +1 -0
- package/dist/locks/MongoLocks.d.ts +36 -0
- package/dist/locks/MongoLocks.js +81 -0
- package/dist/locks/MongoLocks.js.map +1 -0
- package/dist/locks/locks-index.d.ts +2 -0
- package/dist/locks/locks-index.js +3 -0
- package/dist/locks/locks-index.js.map +1 -0
- package/dist/metrics/Metrics.d.ts +30 -0
- package/dist/metrics/Metrics.js +176 -0
- package/dist/metrics/Metrics.js.map +1 -0
- package/dist/migrations/db/migrations/1684951997326-init.d.ts +3 -0
- package/dist/migrations/db/migrations/1684951997326-init.js +31 -0
- package/dist/migrations/db/migrations/1684951997326-init.js.map +1 -0
- package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.d.ts +2 -0
- package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js +5 -0
- package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js.map +1 -0
- package/dist/migrations/db/migrations/1702295701188-sync-rule-state.d.ts +3 -0
- package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js +54 -0
- package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js.map +1 -0
- package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.d.ts +3 -0
- package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js +27 -0
- package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js.map +1 -0
- package/dist/migrations/definitions.d.ts +18 -0
- package/dist/migrations/definitions.js +6 -0
- package/dist/migrations/definitions.js.map +1 -0
- package/dist/migrations/executor.d.ts +16 -0
- package/dist/migrations/executor.js +64 -0
- package/dist/migrations/executor.js.map +1 -0
- package/dist/migrations/migrations-index.d.ts +3 -0
- package/dist/migrations/migrations-index.js +4 -0
- package/dist/migrations/migrations-index.js.map +1 -0
- package/dist/migrations/migrations.d.ts +10 -0
- package/dist/migrations/migrations.js +90 -0
- package/dist/migrations/migrations.js.map +1 -0
- package/dist/migrations/store/migration-store.d.ts +11 -0
- package/dist/migrations/store/migration-store.js +46 -0
- package/dist/migrations/store/migration-store.js.map +1 -0
- package/dist/replication/ErrorRateLimiter.d.ts +17 -0
- package/dist/replication/ErrorRateLimiter.js +43 -0
- package/dist/replication/ErrorRateLimiter.js.map +1 -0
- package/dist/replication/PgRelation.d.ts +16 -0
- package/dist/replication/PgRelation.js +26 -0
- package/dist/replication/PgRelation.js.map +1 -0
- package/dist/replication/WalConnection.d.ts +34 -0
- package/dist/replication/WalConnection.js +190 -0
- package/dist/replication/WalConnection.js.map +1 -0
- package/dist/replication/WalStream.d.ts +57 -0
- package/dist/replication/WalStream.js +515 -0
- package/dist/replication/WalStream.js.map +1 -0
- package/dist/replication/WalStreamManager.d.ts +30 -0
- package/dist/replication/WalStreamManager.js +198 -0
- package/dist/replication/WalStreamManager.js.map +1 -0
- package/dist/replication/WalStreamRunner.d.ts +38 -0
- package/dist/replication/WalStreamRunner.js +155 -0
- package/dist/replication/WalStreamRunner.js.map +1 -0
- package/dist/replication/replication-index.d.ts +7 -0
- package/dist/replication/replication-index.js +8 -0
- package/dist/replication/replication-index.js.map +1 -0
- package/dist/replication/util.d.ts +9 -0
- package/dist/replication/util.js +62 -0
- package/dist/replication/util.js.map +1 -0
- package/dist/routes/auth.d.ts +56 -0
- package/dist/routes/auth.js +182 -0
- package/dist/routes/auth.js.map +1 -0
- package/dist/routes/endpoints/admin.d.ts +1011 -0
- package/dist/routes/endpoints/admin.js +207 -0
- package/dist/routes/endpoints/admin.js.map +1 -0
- package/dist/routes/endpoints/checkpointing.d.ts +76 -0
- package/dist/routes/endpoints/checkpointing.js +36 -0
- package/dist/routes/endpoints/checkpointing.js.map +1 -0
- package/dist/routes/endpoints/dev.d.ts +312 -0
- package/dist/routes/endpoints/dev.js +172 -0
- package/dist/routes/endpoints/dev.js.map +1 -0
- package/dist/routes/endpoints/route-endpoints-index.d.ts +6 -0
- package/dist/routes/endpoints/route-endpoints-index.js +7 -0
- package/dist/routes/endpoints/route-endpoints-index.js.map +1 -0
- package/dist/routes/endpoints/socket-route.d.ts +2 -0
- package/dist/routes/endpoints/socket-route.js +119 -0
- package/dist/routes/endpoints/socket-route.js.map +1 -0
- package/dist/routes/endpoints/sync-rules.d.ts +174 -0
- package/dist/routes/endpoints/sync-rules.js +202 -0
- package/dist/routes/endpoints/sync-rules.js.map +1 -0
- package/dist/routes/endpoints/sync-stream.d.ts +132 -0
- package/dist/routes/endpoints/sync-stream.js +83 -0
- package/dist/routes/endpoints/sync-stream.js.map +1 -0
- package/dist/routes/hooks.d.ts +10 -0
- package/dist/routes/hooks.js +32 -0
- package/dist/routes/hooks.js.map +1 -0
- package/dist/routes/route-register.d.ts +10 -0
- package/dist/routes/route-register.js +87 -0
- package/dist/routes/route-register.js.map +1 -0
- package/dist/routes/router-socket.d.ts +10 -0
- package/dist/routes/router-socket.js +5 -0
- package/dist/routes/router-socket.js.map +1 -0
- package/dist/routes/router.d.ts +26 -0
- package/dist/routes/router.js +7 -0
- package/dist/routes/router.js.map +1 -0
- package/dist/routes/routes-index.d.ts +6 -0
- package/dist/routes/routes-index.js +7 -0
- package/dist/routes/routes-index.js.map +1 -0
- package/dist/runner/teardown.d.ts +2 -0
- package/dist/runner/teardown.js +94 -0
- package/dist/runner/teardown.js.map +1 -0
- package/dist/storage/BucketStorage.d.ts +307 -0
- package/dist/storage/BucketStorage.js +25 -0
- package/dist/storage/BucketStorage.js.map +1 -0
- package/dist/storage/ChecksumCache.d.ts +50 -0
- package/dist/storage/ChecksumCache.js +234 -0
- package/dist/storage/ChecksumCache.js.map +1 -0
- package/dist/storage/MongoBucketStorage.d.ts +52 -0
- package/dist/storage/MongoBucketStorage.js +409 -0
- package/dist/storage/MongoBucketStorage.js.map +1 -0
- package/dist/storage/SourceTable.d.ts +39 -0
- package/dist/storage/SourceTable.js +50 -0
- package/dist/storage/SourceTable.js.map +1 -0
- package/dist/storage/mongo/MongoBucketBatch.d.ts +48 -0
- package/dist/storage/mongo/MongoBucketBatch.js +581 -0
- package/dist/storage/mongo/MongoBucketBatch.js.map +1 -0
- package/dist/storage/mongo/MongoIdSequence.d.ts +12 -0
- package/dist/storage/mongo/MongoIdSequence.js +21 -0
- package/dist/storage/mongo/MongoIdSequence.js.map +1 -0
- package/dist/storage/mongo/MongoPersistedSyncRules.d.ts +9 -0
- package/dist/storage/mongo/MongoPersistedSyncRules.js +9 -0
- package/dist/storage/mongo/MongoPersistedSyncRules.js.map +1 -0
- package/dist/storage/mongo/MongoPersistedSyncRulesContent.d.ts +20 -0
- package/dist/storage/mongo/MongoPersistedSyncRulesContent.js +26 -0
- package/dist/storage/mongo/MongoPersistedSyncRulesContent.js.map +1 -0
- package/dist/storage/mongo/MongoSyncBucketStorage.d.ts +29 -0
- package/dist/storage/mongo/MongoSyncBucketStorage.js +391 -0
- package/dist/storage/mongo/MongoSyncBucketStorage.js.map +1 -0
- package/dist/storage/mongo/MongoSyncRulesLock.d.ts +16 -0
- package/dist/storage/mongo/MongoSyncRulesLock.js +65 -0
- package/dist/storage/mongo/MongoSyncRulesLock.js.map +1 -0
- package/dist/storage/mongo/OperationBatch.d.ts +26 -0
- package/dist/storage/mongo/OperationBatch.js +101 -0
- package/dist/storage/mongo/OperationBatch.js.map +1 -0
- package/dist/storage/mongo/PersistedBatch.d.ts +46 -0
- package/dist/storage/mongo/PersistedBatch.js +213 -0
- package/dist/storage/mongo/PersistedBatch.js.map +1 -0
- package/dist/storage/mongo/db.d.ts +26 -0
- package/dist/storage/mongo/db.js +35 -0
- package/dist/storage/mongo/db.js.map +1 -0
- package/dist/storage/mongo/models.d.ts +140 -0
- package/dist/storage/mongo/models.js +27 -0
- package/dist/storage/mongo/models.js.map +1 -0
- package/dist/storage/mongo/util.d.ts +26 -0
- package/dist/storage/mongo/util.js +81 -0
- package/dist/storage/mongo/util.js.map +1 -0
- package/dist/storage/storage-index.d.ts +14 -0
- package/dist/storage/storage-index.js +15 -0
- package/dist/storage/storage-index.js.map +1 -0
- package/dist/sync/BroadcastIterable.d.ts +38 -0
- package/dist/sync/BroadcastIterable.js +153 -0
- package/dist/sync/BroadcastIterable.js.map +1 -0
- package/dist/sync/LastValueSink.d.ts +25 -0
- package/dist/sync/LastValueSink.js +84 -0
- package/dist/sync/LastValueSink.js.map +1 -0
- package/dist/sync/merge.d.ts +39 -0
- package/dist/sync/merge.js +175 -0
- package/dist/sync/merge.js.map +1 -0
- package/dist/sync/safeRace.d.ts +1 -0
- package/dist/sync/safeRace.js +91 -0
- package/dist/sync/safeRace.js.map +1 -0
- package/dist/sync/sync-index.d.ts +6 -0
- package/dist/sync/sync-index.js +7 -0
- package/dist/sync/sync-index.js.map +1 -0
- package/dist/sync/sync.d.ts +18 -0
- package/dist/sync/sync.js +259 -0
- package/dist/sync/sync.js.map +1 -0
- package/dist/sync/util.d.ts +26 -0
- package/dist/sync/util.js +73 -0
- package/dist/sync/util.js.map +1 -0
- package/dist/system/CorePowerSyncSystem.d.ts +23 -0
- package/dist/system/CorePowerSyncSystem.js +52 -0
- package/dist/system/CorePowerSyncSystem.js.map +1 -0
- package/dist/system/system-index.d.ts +1 -0
- package/dist/system/system-index.js +2 -0
- package/dist/system/system-index.js.map +1 -0
- package/dist/util/Mutex.d.ts +47 -0
- package/dist/util/Mutex.js +132 -0
- package/dist/util/Mutex.js.map +1 -0
- package/dist/util/PgManager.d.ts +24 -0
- package/dist/util/PgManager.js +55 -0
- package/dist/util/PgManager.js.map +1 -0
- package/dist/util/alerting.d.ts +2 -0
- package/dist/util/alerting.js +8 -0
- package/dist/util/alerting.js.map +1 -0
- package/dist/util/config/collectors/config-collector.d.ts +29 -0
- package/dist/util/config/collectors/config-collector.js +116 -0
- package/dist/util/config/collectors/config-collector.js.map +1 -0
- package/dist/util/config/collectors/impl/base64-config-collector.d.ts +6 -0
- package/dist/util/config/collectors/impl/base64-config-collector.js +15 -0
- package/dist/util/config/collectors/impl/base64-config-collector.js.map +1 -0
- package/dist/util/config/collectors/impl/fallback-config-collector.d.ts +11 -0
- package/dist/util/config/collectors/impl/fallback-config-collector.js +19 -0
- package/dist/util/config/collectors/impl/fallback-config-collector.js.map +1 -0
- package/dist/util/config/collectors/impl/filesystem-config-collector.d.ts +6 -0
- package/dist/util/config/collectors/impl/filesystem-config-collector.js +37 -0
- package/dist/util/config/collectors/impl/filesystem-config-collector.js.map +1 -0
- package/dist/util/config/compound-config-collector.d.ts +32 -0
- package/dist/util/config/compound-config-collector.js +130 -0
- package/dist/util/config/compound-config-collector.js.map +1 -0
- package/dist/util/config/sync-rules/impl/base64-sync-rules-collector.d.ts +7 -0
- package/dist/util/config/sync-rules/impl/base64-sync-rules-collector.js +17 -0
- package/dist/util/config/sync-rules/impl/base64-sync-rules-collector.js.map +1 -0
- package/dist/util/config/sync-rules/impl/filesystem-sync-rules-collector.d.ts +7 -0
- package/dist/util/config/sync-rules/impl/filesystem-sync-rules-collector.js +21 -0
- package/dist/util/config/sync-rules/impl/filesystem-sync-rules-collector.js.map +1 -0
- package/dist/util/config/sync-rules/impl/inline-sync-rules-collector.d.ts +7 -0
- package/dist/util/config/sync-rules/impl/inline-sync-rules-collector.js +17 -0
- package/dist/util/config/sync-rules/impl/inline-sync-rules-collector.js.map +1 -0
- package/dist/util/config/sync-rules/sync-collector.d.ts +6 -0
- package/dist/util/config/sync-rules/sync-collector.js +3 -0
- package/dist/util/config/sync-rules/sync-collector.js.map +1 -0
- package/dist/util/config/types.d.ts +57 -0
- package/dist/util/config/types.js +7 -0
- package/dist/util/config/types.js.map +1 -0
- package/dist/util/config.d.ts +7 -0
- package/dist/util/config.js +35 -0
- package/dist/util/config.js.map +1 -0
- package/dist/util/env.d.ts +9 -0
- package/dist/util/env.js +26 -0
- package/dist/util/env.js.map +1 -0
- package/dist/util/memory-tracking.d.ts +7 -0
- package/dist/util/memory-tracking.js +58 -0
- package/dist/util/memory-tracking.js.map +1 -0
- package/dist/util/migration_lib.d.ts +11 -0
- package/dist/util/migration_lib.js +64 -0
- package/dist/util/migration_lib.js.map +1 -0
- package/dist/util/pgwire_utils.d.ts +24 -0
- package/dist/util/pgwire_utils.js +117 -0
- package/dist/util/pgwire_utils.js.map +1 -0
- package/dist/util/populate_test_data.d.ts +8 -0
- package/dist/util/populate_test_data.js +65 -0
- package/dist/util/populate_test_data.js.map +1 -0
- package/dist/util/protocol-types.d.ts +182 -0
- package/dist/util/protocol-types.js +42 -0
- package/dist/util/protocol-types.js.map +1 -0
- package/dist/util/secs.d.ts +2 -0
- package/dist/util/secs.js +49 -0
- package/dist/util/secs.js.map +1 -0
- package/dist/util/util-index.d.ts +22 -0
- package/dist/util/util-index.js +23 -0
- package/dist/util/util-index.js.map +1 -0
- package/dist/util/utils.d.ts +17 -0
- package/dist/util/utils.js +92 -0
- package/dist/util/utils.js.map +1 -0
- package/package.json +59 -0
- package/src/api/api-index.ts +2 -0
- package/src/api/diagnostics.ts +221 -0
- package/src/api/schema.ts +99 -0
- package/src/auth/CachedKeyCollector.ts +132 -0
- package/src/auth/CompoundKeyCollector.ts +33 -0
- package/src/auth/JwtPayload.ts +11 -0
- package/src/auth/KeyCollector.ts +27 -0
- package/src/auth/KeySpec.ts +67 -0
- package/src/auth/KeyStore.ts +156 -0
- package/src/auth/LeakyBucket.ts +66 -0
- package/src/auth/RemoteJWKSCollector.ts +130 -0
- package/src/auth/StaticKeyCollector.ts +21 -0
- package/src/auth/SupabaseKeyCollector.ts +67 -0
- package/src/auth/auth-index.ts +10 -0
- package/src/db/db-index.ts +1 -0
- package/src/db/mongo.ts +72 -0
- package/src/entry/cli-entry.ts +40 -0
- package/src/entry/commands/config-command.ts +36 -0
- package/src/entry/commands/migrate-action.ts +25 -0
- package/src/entry/commands/start-action.ts +24 -0
- package/src/entry/commands/teardown-action.ts +23 -0
- package/src/entry/entry-index.ts +5 -0
- package/src/index.ts +40 -0
- package/src/locks/LockManager.ts +16 -0
- package/src/locks/MongoLocks.ts +142 -0
- package/src/locks/locks-index.ts +2 -0
- package/src/metrics/Metrics.ts +265 -0
- package/src/migrations/db/migrations/1684951997326-init.ts +33 -0
- package/src/migrations/db/migrations/1688556755264-initial-sync-rules.ts +5 -0
- package/src/migrations/db/migrations/1702295701188-sync-rule-state.ts +99 -0
- package/src/migrations/db/migrations/1711543888062-write-checkpoint-index.ts +32 -0
- package/src/migrations/definitions.ts +21 -0
- package/src/migrations/executor.ts +87 -0
- package/src/migrations/migrations-index.ts +3 -0
- package/src/migrations/migrations.ts +118 -0
- package/src/migrations/store/migration-store.ts +63 -0
- package/src/replication/ErrorRateLimiter.ts +50 -0
- package/src/replication/PgRelation.ts +42 -0
- package/src/replication/WalConnection.ts +227 -0
- package/src/replication/WalStream.ts +624 -0
- package/src/replication/WalStreamManager.ts +213 -0
- package/src/replication/WalStreamRunner.ts +180 -0
- package/src/replication/replication-index.ts +7 -0
- package/src/replication/util.ts +76 -0
- package/src/routes/auth.ts +215 -0
- package/src/routes/endpoints/admin.ts +237 -0
- package/src/routes/endpoints/checkpointing.ts +41 -0
- package/src/routes/endpoints/dev.ts +199 -0
- package/src/routes/endpoints/route-endpoints-index.ts +6 -0
- package/src/routes/endpoints/socket-route.ts +135 -0
- package/src/routes/endpoints/sync-rules.ts +227 -0
- package/src/routes/endpoints/sync-stream.ts +101 -0
- package/src/routes/hooks.ts +46 -0
- package/src/routes/route-register.ts +104 -0
- package/src/routes/router-socket.ts +13 -0
- package/src/routes/router.ts +46 -0
- package/src/routes/routes-index.ts +6 -0
- package/src/runner/teardown.ts +108 -0
- package/src/storage/BucketStorage.ts +396 -0
- package/src/storage/ChecksumCache.ts +294 -0
- package/src/storage/MongoBucketStorage.ts +519 -0
- package/src/storage/SourceTable.ts +60 -0
- package/src/storage/mongo/MongoBucketBatch.ts +752 -0
- package/src/storage/mongo/MongoIdSequence.ts +24 -0
- package/src/storage/mongo/MongoPersistedSyncRules.ts +16 -0
- package/src/storage/mongo/MongoPersistedSyncRulesContent.ts +47 -0
- package/src/storage/mongo/MongoSyncBucketStorage.ts +533 -0
- package/src/storage/mongo/MongoSyncRulesLock.ts +81 -0
- package/src/storage/mongo/OperationBatch.ts +115 -0
- package/src/storage/mongo/PersistedBatch.ts +268 -0
- package/src/storage/mongo/db.ts +73 -0
- package/src/storage/mongo/models.ts +162 -0
- package/src/storage/mongo/util.ts +88 -0
- package/src/storage/storage-index.ts +15 -0
- package/src/sync/BroadcastIterable.ts +161 -0
- package/src/sync/LastValueSink.ts +100 -0
- package/src/sync/merge.ts +200 -0
- package/src/sync/safeRace.ts +99 -0
- package/src/sync/sync-index.ts +6 -0
- package/src/sync/sync.ts +319 -0
- package/src/sync/util.ts +98 -0
- package/src/system/CorePowerSyncSystem.ts +64 -0
- package/src/system/system-index.ts +1 -0
- package/src/util/Mutex.ts +159 -0
- package/src/util/PgManager.ts +64 -0
- package/src/util/alerting.ts +9 -0
- package/src/util/config/collectors/config-collector.ts +143 -0
- package/src/util/config/collectors/impl/base64-config-collector.ts +18 -0
- package/src/util/config/collectors/impl/fallback-config-collector.ts +22 -0
- package/src/util/config/collectors/impl/filesystem-config-collector.ts +43 -0
- package/src/util/config/compound-config-collector.ts +176 -0
- package/src/util/config/sync-rules/impl/base64-sync-rules-collector.ts +21 -0
- package/src/util/config/sync-rules/impl/filesystem-sync-rules-collector.ts +26 -0
- package/src/util/config/sync-rules/impl/inline-sync-rules-collector.ts +21 -0
- package/src/util/config/sync-rules/sync-collector.ts +8 -0
- package/src/util/config/types.ts +66 -0
- package/src/util/config.ts +39 -0
- package/src/util/env.ts +30 -0
- package/src/util/memory-tracking.ts +67 -0
- package/src/util/migration_lib.ts +79 -0
- package/src/util/pgwire_utils.ts +139 -0
- package/src/util/populate_test_data.ts +78 -0
- package/src/util/protocol-types.ts +228 -0
- package/src/util/secs.ts +54 -0
- package/src/util/util-index.ts +25 -0
- package/src/util/utils.ts +122 -0
- package/test/src/__snapshots__/pg_test.test.ts.snap +256 -0
- package/test/src/__snapshots__/sync.test.ts.snap +247 -0
- package/test/src/auth.test.ts +342 -0
- package/test/src/broadcast_iterable.test.ts +156 -0
- package/test/src/checksum_cache.test.ts +436 -0
- package/test/src/data_storage.test.ts +1176 -0
- package/test/src/env.ts +8 -0
- package/test/src/large_batch.test.ts +194 -0
- package/test/src/merge_iterable.test.ts +355 -0
- package/test/src/pg_test.test.ts +450 -0
- package/test/src/schema_changes.test.ts +545 -0
- package/test/src/setup.ts +7 -0
- package/test/src/slow_tests.test.ts +257 -0
- package/test/src/sql_functions.test.ts +254 -0
- package/test/src/sql_operators.test.ts +132 -0
- package/test/src/sync.test.ts +293 -0
- package/test/src/sync_rules.test.ts +1053 -0
- package/test/src/util.ts +76 -0
- package/test/src/validation.test.ts +63 -0
- package/test/src/wal_stream.test.ts +319 -0
- package/test/src/wal_stream_utils.ts +147 -0
- package/test/tsconfig.json +20 -0
- package/tsconfig.json +31 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/vitest.config.ts +9 -0
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
import {
|
|
2
|
+
EvaluatedParameters,
|
|
3
|
+
EvaluatedRow,
|
|
4
|
+
SqlSyncRules,
|
|
5
|
+
SqliteJsonRow,
|
|
6
|
+
SqliteJsonValue,
|
|
7
|
+
SqliteRow,
|
|
8
|
+
ToastableSqliteRow
|
|
9
|
+
} from '@powersync/service-sync-rules';
|
|
10
|
+
|
|
11
|
+
import * as replication from '../replication/replication-index.js';
|
|
12
|
+
import * as util from '../util/util-index.js';
|
|
13
|
+
import { SourceTable } from './SourceTable.js';
|
|
14
|
+
|
|
15
|
+
export interface BucketStorageFactory {
|
|
16
|
+
/**
|
|
17
|
+
* Update sync rules from configuration, if changed.
|
|
18
|
+
*/
|
|
19
|
+
configureSyncRules(
|
|
20
|
+
sync_rules: string,
|
|
21
|
+
options?: { lock?: boolean }
|
|
22
|
+
): Promise<{ updated: boolean; persisted_sync_rules?: PersistedSyncRulesContent; lock?: ReplicationLock }>;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Get a storage instance to query sync data for specific sync rules.
|
|
26
|
+
*/
|
|
27
|
+
getInstance(options: PersistedSyncRules): SyncRulesBucketStorage;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Deploy new sync rules.
|
|
31
|
+
*/
|
|
32
|
+
updateSyncRules(options: UpdateSyncRulesOptions): Promise<PersistedSyncRulesContent>;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Indicate that a slot was removed, and we should re-sync by creating
|
|
36
|
+
* a new sync rules instance.
|
|
37
|
+
*
|
|
38
|
+
* This is roughly the same as deploying a new version of the current sync
|
|
39
|
+
* rules, but also accounts for cases where the current sync rules are not
|
|
40
|
+
* the latest ones.
|
|
41
|
+
*
|
|
42
|
+
* Replication should be restarted after this.
|
|
43
|
+
*
|
|
44
|
+
* @param slot_name The removed slot
|
|
45
|
+
*/
|
|
46
|
+
slotRemoved(slot_name: string): Promise<void>;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Get the sync rules used for querying.
|
|
50
|
+
*/
|
|
51
|
+
getActiveSyncRules(): Promise<PersistedSyncRules | null>;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Get the sync rules used for querying.
|
|
55
|
+
*/
|
|
56
|
+
getActiveSyncRulesContent(): Promise<PersistedSyncRulesContent | null>;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Get the sync rules that will be active next once done with initial replicatino.
|
|
60
|
+
*/
|
|
61
|
+
getNextSyncRules(): Promise<PersistedSyncRules | null>;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Get the sync rules that will be active next once done with initial replicatino.
|
|
65
|
+
*/
|
|
66
|
+
getNextSyncRulesContent(): Promise<PersistedSyncRulesContent | null>;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Get all sync rules currently replicating. Typically this is the "active" and "next" sync rules.
|
|
70
|
+
*/
|
|
71
|
+
getReplicatingSyncRules(): Promise<PersistedSyncRulesContent[]>;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Get all sync rules stopped but not terminated yet.
|
|
75
|
+
*/
|
|
76
|
+
getStoppedSyncRules(): Promise<PersistedSyncRulesContent[]>;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Same as:
|
|
80
|
+
* getInstance(await getActiveSyncRules()).getCheckpoint().
|
|
81
|
+
*/
|
|
82
|
+
getActiveCheckpoint(): Promise<ActiveCheckpoint>;
|
|
83
|
+
|
|
84
|
+
createWriteCheckpoint(user_id: string, lsns: Record<string, string>): Promise<bigint>;
|
|
85
|
+
|
|
86
|
+
lastWriteCheckpoint(user_id: string, lsn: string): Promise<bigint | null>;
|
|
87
|
+
|
|
88
|
+
watchWriteCheckpoint(user_id: string, signal: AbortSignal): AsyncIterable<WriteCheckpoint>;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Get storage size of active sync rules.
|
|
92
|
+
*/
|
|
93
|
+
getStorageMetrics(): Promise<StorageMetrics>;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Get the unique identifier for this instance of Powersync
|
|
97
|
+
*/
|
|
98
|
+
getPowerSyncInstanceId(): Promise<string>;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export interface WriteCheckpoint {
|
|
102
|
+
base: ActiveCheckpoint;
|
|
103
|
+
writeCheckpoint: bigint | null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export interface ActiveCheckpoint {
|
|
107
|
+
readonly checkpoint: util.OpId;
|
|
108
|
+
readonly lsn: string;
|
|
109
|
+
|
|
110
|
+
hasSyncRules(): boolean;
|
|
111
|
+
|
|
112
|
+
getBucketStorage(): Promise<SyncRulesBucketStorage | null>;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export interface StorageMetrics {
|
|
116
|
+
/**
|
|
117
|
+
* Size of operations (bucket_data)
|
|
118
|
+
*/
|
|
119
|
+
operations_size_bytes: number;
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Size of parameter storage.
|
|
123
|
+
*
|
|
124
|
+
* Replication storage -> raw data as received from Postgres.
|
|
125
|
+
*/
|
|
126
|
+
parameters_size_bytes: number;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Size of current_data.
|
|
130
|
+
*/
|
|
131
|
+
replication_size_bytes: number;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export interface PersistedSyncRulesContent {
|
|
135
|
+
readonly id: number;
|
|
136
|
+
readonly sync_rules_content: string;
|
|
137
|
+
readonly slot_name: string;
|
|
138
|
+
|
|
139
|
+
readonly last_fatal_error?: string | null;
|
|
140
|
+
readonly last_keepalive_ts?: Date | null;
|
|
141
|
+
readonly last_checkpoint_ts?: Date | null;
|
|
142
|
+
|
|
143
|
+
parsed(): PersistedSyncRules;
|
|
144
|
+
|
|
145
|
+
lock(): Promise<ReplicationLock>;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export interface ReplicationLock {
|
|
149
|
+
sync_rules_id: number;
|
|
150
|
+
|
|
151
|
+
release(): Promise<void>;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export interface PersistedSyncRules {
|
|
155
|
+
readonly id: number;
|
|
156
|
+
readonly sync_rules: SqlSyncRules;
|
|
157
|
+
readonly slot_name: string;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export class DefaultPersistedSyncRules implements PersistedSyncRules {
|
|
161
|
+
public readonly checkpoint_lsn: string | null;
|
|
162
|
+
|
|
163
|
+
constructor(public readonly id: number, public readonly sync_rules: SqlSyncRules, checkpoint_lsn: string | null) {
|
|
164
|
+
this.checkpoint_lsn = checkpoint_lsn;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
get slot_name(): string {
|
|
168
|
+
return `powersync_${this.id}`;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export interface UpdateSyncRulesOptions {
|
|
173
|
+
content: string;
|
|
174
|
+
lock?: boolean;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export interface SyncRulesBucketStorageOptions {
|
|
178
|
+
sync_rules: SqlSyncRules;
|
|
179
|
+
group_id: number;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export const DEFAULT_DOCUMENT_BATCH_LIMIT = 1000;
|
|
183
|
+
export const DEFAULT_DOCUMENT_CHUNK_LIMIT_BYTES = 1 * 1024 * 1024;
|
|
184
|
+
|
|
185
|
+
export interface BucketDataBatchOptions {
|
|
186
|
+
/** Limit number of documents returned. Defaults to 1000. */
|
|
187
|
+
limit?: number;
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Limit size of chunks returned. Defaults to 1MB.
|
|
191
|
+
*
|
|
192
|
+
* This is a lower bound, not an upper bound. As soon as the chunk size goes over this limit,
|
|
193
|
+
* it is returned.
|
|
194
|
+
*
|
|
195
|
+
* Note that an individual data row can be close to 16MB in size, so this does not help in
|
|
196
|
+
* extreme cases.
|
|
197
|
+
*/
|
|
198
|
+
chunkLimitBytes?: number;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export interface SyncRulesBucketStorage {
|
|
202
|
+
readonly sync_rules: SqlSyncRules;
|
|
203
|
+
readonly group_id: number;
|
|
204
|
+
readonly slot_name: string;
|
|
205
|
+
|
|
206
|
+
readonly factory: BucketStorageFactory;
|
|
207
|
+
|
|
208
|
+
resolveTable(options: ResolveTableOptions): Promise<ResolveTableResult>;
|
|
209
|
+
|
|
210
|
+
startBatch(options: {}, callback: (batch: BucketStorageBatch) => Promise<void>): Promise<FlushedResult | null>;
|
|
211
|
+
|
|
212
|
+
getCheckpoint(): Promise<{ checkpoint: util.OpId; lsn: string }>;
|
|
213
|
+
|
|
214
|
+
getParameterSets(checkpoint: util.OpId, lookups: SqliteJsonValue[][]): Promise<SqliteJsonRow[]>;
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Get a "batch" of data for a checkpoint.
|
|
218
|
+
*
|
|
219
|
+
* The results will be split into separate SyncBucketData chunks to:
|
|
220
|
+
* 1. Separate buckets.
|
|
221
|
+
* 2. Limit the size of each individual chunk according to options.batchSizeLimitBytes.
|
|
222
|
+
*
|
|
223
|
+
* @param checkpoint the checkpoint
|
|
224
|
+
* @param dataBuckets current bucket states
|
|
225
|
+
* @param options batch size options
|
|
226
|
+
*/
|
|
227
|
+
getBucketDataBatch(
|
|
228
|
+
checkpoint: util.OpId,
|
|
229
|
+
dataBuckets: Map<string, string>,
|
|
230
|
+
options?: BucketDataBatchOptions
|
|
231
|
+
): AsyncIterable<util.SyncBucketData>;
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Compute checksums for a given list of buckets.
|
|
235
|
+
*
|
|
236
|
+
* Returns zero checksums for any buckets not found.
|
|
237
|
+
*/
|
|
238
|
+
getChecksums(checkpoint: util.OpId, buckets: string[]): Promise<util.ChecksumMap>;
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Terminate the sync rules.
|
|
242
|
+
*
|
|
243
|
+
* This clears the storage, and sets state to TERMINATED.
|
|
244
|
+
*
|
|
245
|
+
* Must only be called on stopped sync rules.
|
|
246
|
+
*/
|
|
247
|
+
terminate(): Promise<void>;
|
|
248
|
+
|
|
249
|
+
getStatus(): Promise<SyncRuleStatus>;
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Clear the storage, without changing state.
|
|
253
|
+
*/
|
|
254
|
+
clear(): Promise<void>;
|
|
255
|
+
|
|
256
|
+
setSnapshotDone(lsn: string): Promise<void>;
|
|
257
|
+
|
|
258
|
+
autoActivate(): Promise<void>;
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Record a replication error.
|
|
262
|
+
*
|
|
263
|
+
* This could be a recoverable error (e.g. temporary network failure),
|
|
264
|
+
* or a permanent error (e.g. missing toast data).
|
|
265
|
+
*
|
|
266
|
+
* Errors are cleared on commit.
|
|
267
|
+
*/
|
|
268
|
+
reportError(e: any): Promise<void>;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export interface SyncRuleStatus {
|
|
272
|
+
checkpoint_lsn: string | null;
|
|
273
|
+
active: boolean;
|
|
274
|
+
snapshot_done: boolean;
|
|
275
|
+
}
|
|
276
|
+
export interface ResolveTableOptions {
|
|
277
|
+
group_id: number;
|
|
278
|
+
connection_id: number;
|
|
279
|
+
connection_tag: string;
|
|
280
|
+
relation: replication.PgRelation;
|
|
281
|
+
|
|
282
|
+
sync_rules: SqlSyncRules;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export interface ResolveTableResult {
|
|
286
|
+
table: SourceTable;
|
|
287
|
+
dropTables: SourceTable[];
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
export interface FlushedResult {
|
|
291
|
+
flushed_op: string;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export interface BucketStorageBatch {
|
|
295
|
+
/**
|
|
296
|
+
* Save an op, and potentially flush.
|
|
297
|
+
*
|
|
298
|
+
* This can be an insert, update or delete op.
|
|
299
|
+
*/
|
|
300
|
+
save(record: SaveOptions): Promise<FlushedResult | null>;
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Replicate a truncate op - deletes all data in the specified tables.
|
|
304
|
+
*/
|
|
305
|
+
truncate(sourceTables: SourceTable[]): Promise<FlushedResult | null>;
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Drop one or more tables.
|
|
309
|
+
*
|
|
310
|
+
* This is the same as truncate, but additionally removes the SourceTable record.
|
|
311
|
+
*/
|
|
312
|
+
drop(sourceTables: SourceTable[]): Promise<FlushedResult | null>;
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Explicitly flush all pending changes in the batch.
|
|
316
|
+
*
|
|
317
|
+
* This does not create a new checkpoint until `commit()` is called. This means it's
|
|
318
|
+
* safe to flush multiple times in the middle of a large transaction.
|
|
319
|
+
*
|
|
320
|
+
* @returns null if there are no changes to flush.
|
|
321
|
+
*/
|
|
322
|
+
flush(): Promise<FlushedResult | null>;
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Flush and commit any saved ops. This creates a new checkpoint.
|
|
326
|
+
*
|
|
327
|
+
* Only call this after a transaction.
|
|
328
|
+
*/
|
|
329
|
+
commit(lsn: string): Promise<boolean>;
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Advance the checkpoint LSN position, without any associated op.
|
|
333
|
+
*
|
|
334
|
+
* This must only be called when not inside a transaction.
|
|
335
|
+
*
|
|
336
|
+
* @returns true if the checkpoint was advanced, false if this was a no-op
|
|
337
|
+
*/
|
|
338
|
+
keepalive(lsn: string): Promise<boolean>;
|
|
339
|
+
|
|
340
|
+
markSnapshotDone(tables: SourceTable[], no_checkpoint_before_lsn: string): Promise<SourceTable[]>;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
export interface SaveParameterData {
|
|
344
|
+
sourceTable: SourceTable;
|
|
345
|
+
/** UUID */
|
|
346
|
+
sourceKey: string;
|
|
347
|
+
evaluated: EvaluatedParameters[];
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
export interface SaveBucketData {
|
|
351
|
+
sourceTable: SourceTable;
|
|
352
|
+
/** UUID */
|
|
353
|
+
sourceKey: string;
|
|
354
|
+
|
|
355
|
+
evaluated: EvaluatedRow[];
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
export type SaveOptions = SaveInsert | SaveUpdate | SaveDelete;
|
|
359
|
+
|
|
360
|
+
export interface SaveInsert {
|
|
361
|
+
tag: 'insert';
|
|
362
|
+
sourceTable: SourceTable;
|
|
363
|
+
before?: undefined;
|
|
364
|
+
after: SqliteRow;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
export interface SaveUpdate {
|
|
368
|
+
tag: 'update';
|
|
369
|
+
sourceTable: SourceTable;
|
|
370
|
+
before?: SqliteRow;
|
|
371
|
+
/**
|
|
372
|
+
* A null value means null column.
|
|
373
|
+
*
|
|
374
|
+
* An undefined value means it's a TOAST value - must be copied from another record.
|
|
375
|
+
*/
|
|
376
|
+
after: ToastableSqliteRow;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
export interface SaveDelete {
|
|
380
|
+
tag: 'delete';
|
|
381
|
+
sourceTable: SourceTable;
|
|
382
|
+
before: SqliteRow;
|
|
383
|
+
after?: undefined;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
export function mergeToast(record: ToastableSqliteRow, persisted: ToastableSqliteRow): ToastableSqliteRow {
|
|
387
|
+
const newRecord: ToastableSqliteRow = {};
|
|
388
|
+
for (let key in record) {
|
|
389
|
+
if (typeof record[key] == 'undefined') {
|
|
390
|
+
newRecord[key] = persisted[key];
|
|
391
|
+
} else {
|
|
392
|
+
newRecord[key] = record[key];
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
return newRecord;
|
|
396
|
+
}
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { BucketChecksum, OpId } from '../util/protocol-types.js';
|
|
2
|
+
import { ChecksumMap, addBucketChecksums } from '../util/utils.js';
|
|
3
|
+
import { LRUCache } from 'lru-cache/min';
|
|
4
|
+
import { OrderedSet } from '@js-sdsl/ordered-set';
|
|
5
|
+
|
|
6
|
+
interface ChecksumFetchContext {
|
|
7
|
+
fetch(bucket: string): Promise<BucketChecksum>;
|
|
8
|
+
checkpoint: bigint;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface FetchPartialBucketChecksum {
|
|
12
|
+
bucket: string;
|
|
13
|
+
start?: OpId;
|
|
14
|
+
end: OpId;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type FetchChecksums = (batch: FetchPartialBucketChecksum[]) => Promise<ChecksumMap>;
|
|
18
|
+
|
|
19
|
+
export interface ChecksumCacheOptions {
|
|
20
|
+
/**
|
|
21
|
+
* Upstream checksum implementation.
|
|
22
|
+
*
|
|
23
|
+
* This fetches a batch of either entire bucket checksums, or a partial range.
|
|
24
|
+
*/
|
|
25
|
+
fetchChecksums: FetchChecksums;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Maximum number of cached checksums.
|
|
29
|
+
*/
|
|
30
|
+
maxSize?: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Approximately 5MB of memory, if we assume 50 bytes per entry
|
|
34
|
+
const DEFAULT_MAX_SIZE = 100_000;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Implement a LRU cache for checksum requests. Each (bucket, checkpoint) request is cached separately,
|
|
38
|
+
* while the lookups occur in batches.
|
|
39
|
+
*
|
|
40
|
+
* For each bucket, we keep a separate OrderedSet of cached checkpoints.
|
|
41
|
+
* This allows us to do incrementally update checksums by using the last cached checksum for the same bucket.
|
|
42
|
+
*
|
|
43
|
+
* We use the LRUCache fetchMethod to deduplicate in-progress requests.
|
|
44
|
+
*/
|
|
45
|
+
export class ChecksumCache {
|
|
46
|
+
/**
|
|
47
|
+
* The primary checksum cache, with key of `${checkpoint}/${bucket}`.
|
|
48
|
+
*/
|
|
49
|
+
private cache: LRUCache<string, BucketChecksum, ChecksumFetchContext>;
|
|
50
|
+
/**
|
|
51
|
+
* For each bucket, an ordered set of cached checkpoints.
|
|
52
|
+
*/
|
|
53
|
+
private bucketCheckpoints = new Map<string, OrderedSet<bigint>>();
|
|
54
|
+
|
|
55
|
+
private fetchChecksums: FetchChecksums;
|
|
56
|
+
|
|
57
|
+
constructor(options: ChecksumCacheOptions) {
|
|
58
|
+
this.fetchChecksums = options.fetchChecksums;
|
|
59
|
+
|
|
60
|
+
this.cache = new LRUCache<string, BucketChecksum, ChecksumFetchContext>({
|
|
61
|
+
max: options.maxSize ?? DEFAULT_MAX_SIZE,
|
|
62
|
+
fetchMethod: async (cacheKey, _staleValue, options) => {
|
|
63
|
+
// Called when this checksum hasn't been cached yet.
|
|
64
|
+
// Pass the call back to the request, which implements batch fetching.
|
|
65
|
+
const { bucket } = parseCacheKey(cacheKey);
|
|
66
|
+
const result = await options.context.fetch(bucket);
|
|
67
|
+
|
|
68
|
+
// Add to the set of cached checkpoints for the bucket.
|
|
69
|
+
let checkpointSet = this.bucketCheckpoints.get(bucket);
|
|
70
|
+
if (checkpointSet == null) {
|
|
71
|
+
checkpointSet = new OrderedSet();
|
|
72
|
+
this.bucketCheckpoints.set(bucket, checkpointSet);
|
|
73
|
+
}
|
|
74
|
+
checkpointSet.insert(options.context.checkpoint);
|
|
75
|
+
return result;
|
|
76
|
+
},
|
|
77
|
+
|
|
78
|
+
dispose: (value, key) => {
|
|
79
|
+
// Remove from the set of cached checkpoints for the bucket
|
|
80
|
+
const { checkpointString } = parseCacheKey(key);
|
|
81
|
+
const checkpoint = BigInt(checkpointString);
|
|
82
|
+
const checkpointSet = this.bucketCheckpoints.get(value.bucket);
|
|
83
|
+
if (checkpointSet == null) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
checkpointSet.eraseElementByKey(checkpoint);
|
|
87
|
+
if (checkpointSet.length == 0) {
|
|
88
|
+
this.bucketCheckpoints.delete(value.bucket);
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
|
|
92
|
+
noDisposeOnSet: true,
|
|
93
|
+
|
|
94
|
+
// When we have more fetches than the cache size, complete the fetches instead
|
|
95
|
+
// of failing with Error('evicted').
|
|
96
|
+
ignoreFetchAbort: true
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async getChecksums(checkpoint: OpId, buckets: string[]): Promise<BucketChecksum[]> {
|
|
101
|
+
const checksums = await this.getChecksumMap(checkpoint, buckets);
|
|
102
|
+
// Return results in the same order as the request
|
|
103
|
+
return buckets.map((bucket) => checksums.get(bucket)!);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Get bucket checksums for a checkpoint.
|
|
108
|
+
*
|
|
109
|
+
* Any checksums not found upstream are returned as zero checksums.
|
|
110
|
+
*
|
|
111
|
+
* @returns a Map with exactly one entry for each bucket requested
|
|
112
|
+
*/
|
|
113
|
+
async getChecksumMap(checkpoint: OpId, buckets: string[]): Promise<ChecksumMap> {
|
|
114
|
+
// Buckets that don't have a cached checksum for this checkpoint yet
|
|
115
|
+
let toFetch = new Set<string>();
|
|
116
|
+
|
|
117
|
+
// Newly fetched results
|
|
118
|
+
let fetchResults = new Map<string, BucketChecksum>();
|
|
119
|
+
|
|
120
|
+
// Promise for the bactch new fetch requests
|
|
121
|
+
let resolveFetch!: () => void;
|
|
122
|
+
let rejectFetch!: (err: any) => void;
|
|
123
|
+
let fetchPromise = new Promise<void>((resolve, reject) => {
|
|
124
|
+
resolveFetch = resolve;
|
|
125
|
+
rejectFetch = reject;
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
// Accumulated results - both from cached checksums, and fetched checksums
|
|
129
|
+
let finalResults = new Map<string, BucketChecksum>();
|
|
130
|
+
|
|
131
|
+
const context: ChecksumFetchContext = {
|
|
132
|
+
async fetch(bucket) {
|
|
133
|
+
await fetchPromise;
|
|
134
|
+
if (!toFetch.has(bucket)) {
|
|
135
|
+
// Should never happen
|
|
136
|
+
throw new Error(`Expected to fetch ${bucket}`);
|
|
137
|
+
}
|
|
138
|
+
const checksum = fetchResults.get(bucket);
|
|
139
|
+
if (checksum == null) {
|
|
140
|
+
// Should never happen
|
|
141
|
+
throw new Error(`Failed to fetch checksum for bucket ${bucket}`);
|
|
142
|
+
}
|
|
143
|
+
return checksum;
|
|
144
|
+
},
|
|
145
|
+
checkpoint: BigInt(checkpoint)
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
// One promise to await to ensure all fetch requests completed.
|
|
149
|
+
let settledPromise: Promise<PromiseSettledResult<void>[]> | null = null;
|
|
150
|
+
|
|
151
|
+
try {
|
|
152
|
+
// Individual cache fetch promises
|
|
153
|
+
let cacheFetchPromises: Promise<void>[] = [];
|
|
154
|
+
|
|
155
|
+
for (let bucket of buckets) {
|
|
156
|
+
const cacheKey = makeCacheKey(checkpoint, bucket);
|
|
157
|
+
let status: LRUCache.Status<BucketChecksum> = {};
|
|
158
|
+
const p = this.cache.fetch(cacheKey, { context: context, status: status }).then((checksums) => {
|
|
159
|
+
if (checksums == null) {
|
|
160
|
+
// Should never happen
|
|
161
|
+
throw new Error(`Failed to get checksums for ${cacheKey}`);
|
|
162
|
+
}
|
|
163
|
+
finalResults.set(bucket, checksums);
|
|
164
|
+
});
|
|
165
|
+
cacheFetchPromises.push(p);
|
|
166
|
+
if (status.fetch == 'hit' || status.fetch == 'inflight') {
|
|
167
|
+
// The checksums is either cached already (hit), or another request is busy
|
|
168
|
+
// fetching (inflight).
|
|
169
|
+
// In either case, we don't need to fetch a new checksum.
|
|
170
|
+
} else {
|
|
171
|
+
// We need a new request for this checksum.
|
|
172
|
+
toFetch.add(bucket);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
// We do this directly after creating the promises, otherwise
|
|
176
|
+
// we could end up with weird uncaught rejection errors.
|
|
177
|
+
settledPromise = Promise.allSettled(cacheFetchPromises);
|
|
178
|
+
|
|
179
|
+
if (toFetch.size == 0) {
|
|
180
|
+
// Nothing to fetch, but resolve in case
|
|
181
|
+
resolveFetch();
|
|
182
|
+
} else {
|
|
183
|
+
let bucketRequests: FetchPartialBucketChecksum[] = [];
|
|
184
|
+
// Partial checksum (previously cached) to add to the partial fetch
|
|
185
|
+
let add = new Map<string, BucketChecksum>();
|
|
186
|
+
|
|
187
|
+
for (let bucket of toFetch) {
|
|
188
|
+
let bucketRequest: FetchPartialBucketChecksum | null = null;
|
|
189
|
+
const checkpointSet = this.bucketCheckpoints.get(bucket);
|
|
190
|
+
if (checkpointSet != null) {
|
|
191
|
+
// Find smaller checkpoints, sorted in descending order
|
|
192
|
+
let iter = checkpointSet.reverseUpperBound(context.checkpoint);
|
|
193
|
+
const begin = checkpointSet.begin();
|
|
194
|
+
while (iter.isAccessible()) {
|
|
195
|
+
const cp = iter.pointer;
|
|
196
|
+
const cacheKey = makeCacheKey(cp, bucket);
|
|
197
|
+
// peek to avoid refreshing the key
|
|
198
|
+
const cached = this.cache.peek(cacheKey);
|
|
199
|
+
// As long as dispose() works correctly, the checkpointset should
|
|
200
|
+
// match up with the cache, and `cached` should also have a value here.
|
|
201
|
+
// However, we handle caces where it's not present either way.
|
|
202
|
+
// Test by disabling the `dispose()` callback.
|
|
203
|
+
if (cached != null) {
|
|
204
|
+
// Partial checksum found - make a partial checksum request
|
|
205
|
+
bucketRequest = {
|
|
206
|
+
bucket,
|
|
207
|
+
start: cp.toString(),
|
|
208
|
+
end: checkpoint
|
|
209
|
+
};
|
|
210
|
+
add.set(bucket, cached);
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (iter.equals(begin)) {
|
|
215
|
+
// Cannot iterate further
|
|
216
|
+
break;
|
|
217
|
+
}
|
|
218
|
+
// Iterate backwards
|
|
219
|
+
iter = iter.pre();
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (bucketRequest == null) {
|
|
224
|
+
// No partial checksum found - make a new full checksum request
|
|
225
|
+
bucketRequest = {
|
|
226
|
+
bucket,
|
|
227
|
+
end: checkpoint
|
|
228
|
+
};
|
|
229
|
+
add.set(bucket, {
|
|
230
|
+
bucket,
|
|
231
|
+
checksum: 0,
|
|
232
|
+
count: 0
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
bucketRequests.push(bucketRequest);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// Fetch partial checksums from upstream
|
|
239
|
+
const results = await this.fetchChecksums(bucketRequests);
|
|
240
|
+
|
|
241
|
+
for (let bucket of toFetch) {
|
|
242
|
+
const result = results.get(bucket);
|
|
243
|
+
const toAdd = add.get(bucket);
|
|
244
|
+
if (toAdd == null) {
|
|
245
|
+
// Should never happen
|
|
246
|
+
throw new Error(`toAdd null for ${bucket}`);
|
|
247
|
+
}
|
|
248
|
+
// Compute the full checksum from the two partials.
|
|
249
|
+
// No results returned are treated the same as a zero result.
|
|
250
|
+
const added = addBucketChecksums(toAdd, result ?? null);
|
|
251
|
+
fetchResults.set(bucket, added);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// fetchResults is fully populated, so we resolve the Promise
|
|
255
|
+
resolveFetch();
|
|
256
|
+
}
|
|
257
|
+
} catch (e) {
|
|
258
|
+
// Failure when fetching checksums - reject the Promise.
|
|
259
|
+
// This will reject all individual cache fetch requests, and each will be retried
|
|
260
|
+
// on the next request.
|
|
261
|
+
rejectFetch(e);
|
|
262
|
+
|
|
263
|
+
// Wait for the above rejection to propagate, otherwise we end up with "uncaught" errors.
|
|
264
|
+
// This promise never throws.
|
|
265
|
+
await settledPromise;
|
|
266
|
+
|
|
267
|
+
throw e;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// Wait for all cache fetch reqeusts to complete
|
|
271
|
+
const settledResults = (await settledPromise) ?? [];
|
|
272
|
+
// Check if any of them failed
|
|
273
|
+
for (let result of settledResults) {
|
|
274
|
+
if (result.status == 'rejected') {
|
|
275
|
+
throw result.reason;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (finalResults.size != buckets.length) {
|
|
280
|
+
// Should not happen
|
|
281
|
+
throw new Error(`Bucket results mismatch: ${finalResults.size} != ${buckets.length}`);
|
|
282
|
+
}
|
|
283
|
+
return finalResults;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function makeCacheKey(checkpoint: bigint | string, bucket: string) {
|
|
288
|
+
return `${checkpoint}/${bucket}`;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function parseCacheKey(key: string) {
|
|
292
|
+
const index = key.indexOf('/');
|
|
293
|
+
return { checkpointString: key.substring(0, index), bucket: key.substring(index + 1) };
|
|
294
|
+
}
|