pure-js-db 0.0.0-stage → 2.53.0
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/LICENSE +21 -0
- package/README.md +747 -3
- package/bindings/java/pom.xml +18 -0
- package/bindings/java/src/main/java/purejsdb/PureJsDBClient.java +146 -0
- package/bindings/java/src/main/java/purejsdb/TransactionClient.java +65 -0
- package/bindings/java/src/test/java/TestClient.java +68 -0
- package/bindings/orm/sequelize-adapter.js +1236 -0
- package/bindings/python/purejsdb/client.py +81 -0
- package/bindings/python/setup.py +9 -0
- package/bindings/python/test_client.py +63 -0
- package/config/datagrip-config.md +206 -0
- package/config/grafana-dashboard.json +1510 -0
- package/config/production.config.js +126 -0
- package/config/prometheus-scrape.yml +39 -0
- package/native/index.node +0 -0
- package/package.json +205 -6
- package/scripts/mcp-server.mjs +208 -0
- package/src/adapter/dialect-adapter.js +179 -0
- package/src/adapter/oracle-adapter.js +191 -0
- package/src/ai/ai-service.js +713 -0
- package/src/ai/rag-pipeline.js +160 -0
- package/src/alerting/alert-manager.js +622 -0
- package/src/alerting/index.js +10 -0
- package/src/arrow/arrow-ipc.js +973 -0
- package/src/arrow/flatbuffer.js +521 -0
- package/src/arrow/index.js +261 -0
- package/src/arrow/parquet.js +1525 -0
- package/src/audit/audit-log.js +1345 -0
- package/src/audit/audit-report.js +571 -0
- package/src/audit/index.js +6 -0
- package/src/backpressure/backpressure-controller.js +262 -0
- package/src/backpressure/index.js +6 -0
- package/src/backpressure/write-rate-limiter.js +243 -0
- package/src/backup/backup-manager.js +416 -0
- package/src/backup/index.js +8 -0
- package/src/backup/logical-backup.js +228 -0
- package/src/backup/snapshot.js +111 -0
- package/src/backup/toolkit.js +410 -0
- package/src/bindings/orm/prisma-adapter.js +571 -0
- package/src/bindings/orm/typeorm-adapter.js +477 -0
- package/src/cache/adaptive-cache-size.js +288 -0
- package/src/cache/adaptive-ttl.js +139 -0
- package/src/cache/cache-adapter.js +304 -0
- package/src/cache/circuit-breaker.js +294 -0
- package/src/cache/index.js +31 -0
- package/src/cache/lru-cache.js +353 -0
- package/src/cache/redis-adapter.js +391 -0
- package/src/cache/resilient-adapter.js +391 -0
- package/src/cache/result-cache.js +493 -0
- package/src/cache/semantic-cache.js +339 -0
- package/src/cdc/cdc-engine.js +1061 -0
- package/src/cdc/cdc-handlers.js +289 -0
- package/src/cdc/ddl-capture.js +330 -0
- package/src/cdc/debezium-adapter.js +366 -0
- package/src/cdc/schema-history.js +414 -0
- package/src/changelog/changelog-buffer.js +881 -0
- package/src/changelog/changelog-wal.js +299 -0
- package/src/changelog/linear-regression-predictor.js +186 -0
- package/src/changelog/predictive-growth.js +348 -0
- package/src/changelog/seasonal-predictor.js +450 -0
- package/src/changelog/write-rate-sampler.js +141 -0
- package/src/cli/dump-command.js +944 -0
- package/src/cli/interactive.js +188 -0
- package/src/cli/migrate-command.js +252 -0
- package/src/cli.js +533 -0
- package/src/cluster/failover.js +477 -0
- package/src/cluster/index.js +19 -0
- package/src/cluster/persistent-state.js +203 -0
- package/src/cluster/public-api.js +58 -0
- package/src/cluster/raft-http-transport.js +280 -0
- package/src/cluster/raft-log.js +313 -0
- package/src/cluster/raft.js +912 -0
- package/src/cluster/snapshot-persistence.js +131 -0
- package/src/concurrency/async-mutex.js +365 -0
- package/src/concurrency/circuit-breaker.js +326 -0
- package/src/concurrency/connection-pool.js +409 -0
- package/src/concurrency/connection-session.js +498 -0
- package/src/concurrency/index.js +29 -0
- package/src/concurrency/lock-manager.js +363 -0
- package/src/concurrency/lockfree-executor.js +322 -0
- package/src/concurrency/lockfree-manager.js +570 -0
- package/src/concurrency/mvcc.js +499 -0
- package/src/concurrency/occ-hot-row.js +726 -0
- package/src/concurrency/query-degradation.js +380 -0
- package/src/concurrency/query-isolation.js +374 -0
- package/src/concurrency/shared-memory-pool.js +459 -0
- package/src/concurrency/snapshot-buffer.js +461 -0
- package/src/concurrency/transaction-context.js +193 -0
- package/src/copy/copy-stream.js +643 -0
- package/src/copy/index.js +5 -0
- package/src/core/database.js +1947 -0
- package/src/core/index.js +13 -0
- package/src/daemon/compact-daemon.js +535 -0
- package/src/daemon/index.js +14 -0
- package/src/daemon/soa-daemon-integration.js +333 -0
- package/src/ddl/online-ddl.js +348 -0
- package/src/disruptor/disruptor.js +348 -0
- package/src/disruptor/event-bus.js +238 -0
- package/src/disruptor/event-pool.js +62 -0
- package/src/disruptor/index.js +31 -0
- package/src/disruptor/ring-buffer.js +150 -0
- package/src/disruptor/sequence.js +88 -0
- package/src/disruptor/wait-strategy.js +160 -0
- package/src/engine/public-api.js +210 -0
- package/src/executor/asof-join-executor.js +265 -0
- package/src/executor/bitmap-filter.js +1382 -0
- package/src/executor/compiled-expression.js +472 -0
- package/src/executor/compiled-predicate.js +458 -0
- package/src/executor/constraint-executor.js +577 -0
- package/src/executor/copy-executor.js +210 -0
- package/src/executor/cte/recursive-cte.js +89 -0
- package/src/executor/cte-executor.js +501 -0
- package/src/executor/cursor-executor.js +216 -0
- package/src/executor/database.js +41 -0
- package/src/executor/ddl-executor.js +3531 -0
- package/src/executor/dml-executor.js +3557 -0
- package/src/executor/executor-worker.js +331 -0
- package/src/executor/executor.js +10889 -0
- package/src/executor/explain-analyzer.js +422 -0
- package/src/executor/explain-executor.js +324 -0
- package/src/executor/expression.js +4972 -0
- package/src/executor/foreign-key-executor.js +691 -0
- package/src/executor/graph-table-executor.js +451 -0
- package/src/executor/grouping-executor.js +251 -0
- package/src/executor/hotpath.js +232 -0
- package/src/executor/hybrid-search-executor.js +326 -0
- package/src/executor/incremental-live-query.js +866 -0
- package/src/executor/index-maintenance-executor.js +731 -0
- package/src/executor/index.js +16 -0
- package/src/executor/info-schema-executor.js +123 -0
- package/src/executor/join/lateral-join.js +64 -0
- package/src/executor/join-executor.js +530 -0
- package/src/executor/lineage-executor.js +85 -0
- package/src/executor/live-query-manager.js +350 -0
- package/src/executor/loop-unroll.js +539 -0
- package/src/executor/match-recognize-executor.js +918 -0
- package/src/executor/multi-stream-wal.js +140 -0
- package/src/executor/nl2sql-executor.js +309 -0
- package/src/executor/parallel-dml.js +258 -0
- package/src/executor/pivot-executor.js +458 -0
- package/src/executor/pragma-executor.js +661 -0
- package/src/executor/select-executor.js +857 -0
- package/src/executor/sequence.js +139 -0
- package/src/executor/sql-reconstructor.js +296 -0
- package/src/executor/subquery-cache.js +186 -0
- package/src/executor/subquery-executor.js +1390 -0
- package/src/executor/system-catalog.js +731 -0
- package/src/executor/table-function-executor.js +656 -0
- package/src/executor/topn.js +103 -0
- package/src/executor/transaction-executor.js +926 -0
- package/src/executor/trigger-executor.js +441 -0
- package/src/executor/v8-turbofan-optimizer.js +363 -0
- package/src/executor/vector-acceleration.js +456 -0
- package/src/executor/vector-executor.js +847 -0
- package/src/executor/vectorized-columnar-engine.js +1130 -0
- package/src/executor/vectorized-executor-v2.js +993 -0
- package/src/executor/view-executor.js +149 -0
- package/src/executor/wal.js +1945 -0
- package/src/executor/wasm-simd-engine.js +929 -0
- package/src/executor/window-functions.js +894 -0
- package/src/fdw/fdw-manager.js +642 -0
- package/src/fdw/index.js +5 -0
- package/src/geo/geometry.js +447 -0
- package/src/geo/index.js +16 -0
- package/src/geo/spatial-functions.js +1241 -0
- package/src/graph/graph-helper.js +243 -0
- package/src/graph/pgq-matcher.js +2545 -0
- package/src/graph/property-graph.js +123 -0
- package/src/graphql-bridge.js +881 -0
- package/src/hotspot/access-tracker.js +188 -0
- package/src/hotspot/hotspot-detector.js +140 -0
- package/src/hotspot/hotspot-prefetcher.js +210 -0
- package/src/hotspot/index.js +7 -0
- package/src/http-api.js +1182 -0
- package/src/http-server/handlers/audit.js +224 -0
- package/src/http-server/handlers/changes-feed.js +261 -0
- package/src/http-server/handlers/management.js +464 -0
- package/src/http-server/handlers/query.js +573 -0
- package/src/http-server/handlers/transaction.js +251 -0
- package/src/http-server/index.js +89 -0
- package/src/http-server/openapi.js +656 -0
- package/src/http-server/router.js +120 -0
- package/src/http-server/server.js +1915 -0
- package/src/http-server/tls.js +126 -0
- package/src/http-server/ws-server.js +873 -0
- package/src/index/adaptive-hash-index.js +176 -0
- package/src/index/bitmap-index.js +429 -0
- package/src/index/brin-index.js +246 -0
- package/src/index/btree-index.js +428 -0
- package/src/index/btree.js +690 -0
- package/src/index/diskann-index.js +1903 -0
- package/src/index/fulltext/fulltext-index.js +66 -0
- package/src/index/fulltext/index.js +3 -0
- package/src/index/fulltext/inverted-index.js +197 -0
- package/src/index/fulltext/tokenizer.js +154 -0
- package/src/index/fulltext.js +287 -0
- package/src/index/gin-index.js +316 -0
- package/src/index/gist-index.js +463 -0
- package/src/index/gist-ops.js +325 -0
- package/src/index/graph-compressor.js +505 -0
- package/src/index/hash-index.js +180 -0
- package/src/index/hnsw-index.js +1232 -0
- package/src/index/hnsw-pq-index.js +1147 -0
- package/src/index/index-auto-tuner.js +1341 -0
- package/src/index/index-interface.js +364 -0
- package/src/index/index-selector.js +523 -0
- package/src/index/index.js +28 -0
- package/src/index/ivfpqsq-index.js +1163 -0
- package/src/index/offheap-btree-index.js +395 -0
- package/src/index/offheap-btree.js +748 -0
- package/src/index/product-quantizer.js +1447 -0
- package/src/index/robin-hood-hash-index.js +384 -0
- package/src/index/rtree-index.js +982 -0
- package/src/index/scalar-quantizer.js +411 -0
- package/src/index/semantic-index.js +283 -0
- package/src/index/string-interner.js +238 -0
- package/src/index/timestamp-index-advisor.js +491 -0
- package/src/index/timestamp-index-compressed.js +800 -0
- package/src/index/timestamp-index-compressor.js +547 -0
- package/src/index/timestamp-index.js +579 -0
- package/src/index/vector-index-serializer.js +499 -0
- package/src/index/vector-index-stats.js +450 -0
- package/src/index/vector-index.js +615 -0
- package/src/index-v3.js +27 -0
- package/src/index.js +33 -0
- package/src/internal/public-api.js +198 -0
- package/src/lineage/column-lineage.js +393 -0
- package/src/lineage/index.js +11 -0
- package/src/lineage/lineage-store.js +701 -0
- package/src/lineage/table-lineage.js +68 -0
- package/src/mcp/index.js +176 -0
- package/src/mcp/mcp-server.js +334 -0
- package/src/mcp/protocol.js +195 -0
- package/src/mcp/tools/backup-restore.js +131 -0
- package/src/mcp/tools/cdc-tools.js +201 -0
- package/src/mcp/tools/describe-table.js +100 -0
- package/src/mcp/tools/execute-sql.js +210 -0
- package/src/mcp/tools/explain-query.js +75 -0
- package/src/mcp/tools/index-management.js +191 -0
- package/src/mcp/tools/lineage.js +64 -0
- package/src/mcp/tools/list-tables.js +46 -0
- package/src/mcp/tools/monitoring.js +195 -0
- package/src/mcp/tools/schema-management.js +229 -0
- package/src/mcp/tools/text-to-sql.js +84 -0
- package/src/mcp/tools/transaction-management.js +191 -0
- package/src/mcp/tools/vector-search.js +101 -0
- package/src/mcp/transport/http.js +352 -0
- package/src/mcp/transport/stdio.js +117 -0
- package/src/memory/memory-manager.js +437 -0
- package/src/migration/migrator.js +389 -0
- package/src/monitor/index.js +36 -0
- package/src/monitor/metrics-exporter.js +517 -0
- package/src/monitor/monitor.js +385 -0
- package/src/monitor/performance-monitor.js +204 -0
- package/src/monitor/soa-monitor.js +628 -0
- package/src/multitenant/index.js +1 -0
- package/src/multitenant/tenant-manager.js +238 -0
- package/src/native/native-acceleration.js +3493 -0
- package/src/optimizer/adaptive-optimizer.js +666 -0
- package/src/optimizer/cse.js +383 -0
- package/src/optimizer/decorrelator.js +958 -0
- package/src/optimizer/derived-table-eliminator.js +367 -0
- package/src/optimizer/explain-visualizer.js +266 -0
- package/src/optimizer/histogram.js +290 -0
- package/src/optimizer/index.js +78 -0
- package/src/optimizer/jit-compiler.js +1200 -0
- package/src/optimizer/join-order-optimizer.js +717 -0
- package/src/optimizer/limit-pushdown.js +70 -0
- package/src/optimizer/optimized-plan.js +54 -0
- package/src/optimizer/optimizer-rule.js +295 -0
- package/src/optimizer/optimizer.js +1515 -0
- package/src/optimizer/pipeline.js +263 -0
- package/src/optimizer/predicate-pushdown.js +599 -0
- package/src/optimizer/query-plan-cache.js +275 -0
- package/src/optimizer/rule-adapters.js +343 -0
- package/src/optimizer/subquery-flattener.js +689 -0
- package/src/optimizer/timestamp-query-rewriter.js +498 -0
- package/src/orm/collection.js +160 -0
- package/src/orm/errors.js +76 -0
- package/src/orm/hooks.js +184 -0
- package/src/orm/index.js +93 -0
- package/src/orm/mixins/soft-delete.js +271 -0
- package/src/orm/model.js +951 -0
- package/src/orm/query-builder.js +681 -0
- package/src/orm/relations.js +303 -0
- package/src/orm/schema.js +443 -0
- package/src/orm/seeder.js +350 -0
- package/src/parallel/dml-pushdown-executor.js +430 -0
- package/src/parallel/dml-worker.js +151 -0
- package/src/parallel/morsel-queue.js +358 -0
- package/src/parallel/parallel-executor.js +277 -0
- package/src/parallel/pipeline-executor.js +423 -0
- package/src/parallel/shared-cache.js +206 -0
- package/src/parser/ast.js +3853 -0
- package/src/parser/parser.js +7909 -0
- package/src/parser/tokenizer.js +542 -0
- package/src/query/relation-preloader.js +327 -0
- package/src/replication/index.js +15 -0
- package/src/replication/lag-monitor.js +163 -0
- package/src/replication/master.js +117 -0
- package/src/replication/multi-master.js +495 -0
- package/src/replication/read-replica.js +490 -0
- package/src/replication/replica.js +395 -0
- package/src/replication/replication-http-transport.js +421 -0
- package/src/replication/wal-standby.js +334 -0
- package/src/runtime/index.js +4 -0
- package/src/runtime/plpgsql-interpreter.js +766 -0
- package/src/runtime/plpgsql-parser.js +852 -0
- package/src/runtime/stored-procedure.js +149 -0
- package/src/runtime/udf.js +194 -0
- package/src/schema/dsl-parser.js +896 -0
- package/src/schema/index.js +113 -0
- package/src/schema/introspector.js +246 -0
- package/src/schema/pg-migrator.js +599 -0
- package/src/schema/schema-differ.js +504 -0
- package/src/schema/schema-versioning.js +426 -0
- package/src/schema/type-generator.js +537 -0
- package/src/sdk/README.md +277 -0
- package/src/sdk/go/README.md +19 -0
- package/src/sdk/go/go.mod +3 -0
- package/src/sdk/go/pureclient/client.go +663 -0
- package/src/sdk/go/pureclient/client_test.go +413 -0
- package/src/sdk/java/README.md +19 -0
- package/src/sdk/java/pom.xml +87 -0
- package/src/sdk/java/pureclient/JsonUtil.java +366 -0
- package/src/sdk/java/pureclient/PureClient.java +835 -0
- package/src/sdk/java/pureclient/PureClientTest.java +570 -0
- package/src/sdk/java/pureclient/PureError.java +158 -0
- package/src/sdk/java/pureclient/Transaction.java +242 -0
- package/src/sdk/javascript/README.md +19 -0
- package/src/sdk/javascript/cdc.js +230 -0
- package/src/sdk/javascript/client.js +420 -0
- package/src/sdk/javascript/errors.js +66 -0
- package/src/sdk/javascript/index.js +35 -0
- package/src/sdk/javascript/pool.js +199 -0
- package/src/sdk/javascript/transaction.js +114 -0
- package/src/sdk/php/README.md +19 -0
- package/src/sdk/php/composer.json +30 -0
- package/src/sdk/php/src/PureClient.php +550 -0
- package/src/sdk/php/src/PureError.php +130 -0
- package/src/sdk/php/src/Transaction.php +172 -0
- package/src/sdk/public-api.js +16 -0
- package/src/sdk/python/README.md +19 -0
- package/src/sdk/python/pure_client.py +388 -0
- package/src/sdk/react-native/README.md +161 -0
- package/src/sdk/react-native/async-storage-adapter.js +147 -0
- package/src/sdk/react-native/embedded/embedded-client.js +266 -0
- package/src/sdk/react-native/embedded/hermes-jsi-bridge.js +207 -0
- package/src/sdk/react-native/embedded/sqlite-kv-adapter.js +345 -0
- package/src/sdk/react-native/expo-file-adapter.js +315 -0
- package/src/sdk/react-native/index.js +52 -0
- package/src/sdk/react-native/mobile-client.js +759 -0
- package/src/sdk/react-native/sync-manager.js +455 -0
- package/src/sdk/react-native/vector-search.js +322 -0
- package/src/sdk/rust/CHANGELOG.md +104 -0
- package/src/sdk/rust/Cargo.toml +35 -0
- package/src/sdk/rust/LICENSE +21 -0
- package/src/sdk/rust/PUBLISHING.md +202 -0
- package/src/sdk/rust/README.md +471 -0
- package/src/sdk/rust/RELEASE-CHECKLIST.md +340 -0
- package/src/sdk/rust/examples/basic_usage.rs +181 -0
- package/src/sdk/rust/scripts/publish.sh +233 -0
- package/src/sdk/rust/src/client.rs +481 -0
- package/src/sdk/rust/src/error.rs +95 -0
- package/src/sdk/rust/src/index/hnswpq.rs +626 -0
- package/src/sdk/rust/src/index/mod.rs +7 -0
- package/src/sdk/rust/src/lib.rs +66 -0
- package/src/sdk/rust/src/models.rs +198 -0
- package/src/sdk/rust/src/transaction.rs +123 -0
- package/src/sdk/rust/tests/concurrent_vector_search.rs +534 -0
- package/src/sdk/wechat/README.md +19 -0
- package/src/sdk/wechat/index.js +42 -0
- package/src/sdk/wechat/pure-js-db.js +105 -0
- package/src/sdk/wechat/stubs/empty.js +85 -0
- package/src/sdk/wechat/wx-database.js +225 -0
- package/src/sdk/wechat/wx-lite.js +191 -0
- package/src/sdk/wechat/wx-sqlite-adapter.js +655 -0
- package/src/sdk/wechat/wx-sqlite-database.js +146 -0
- package/src/sdk/wechat/wx-storage-adapter.js +609 -0
- package/src/security/api-key.js +215 -0
- package/src/security/auth.js +179 -0
- package/src/security/column-encryption-optimized.js +352 -0
- package/src/security/column-encryption.js +330 -0
- package/src/security/crypto-provider.js +345 -0
- package/src/security/data-masking.js +370 -0
- package/src/security/field-encryption.js +238 -0
- package/src/security/index.js +41 -0
- package/src/security/jwt.js +202 -0
- package/src/security/rbac.js +702 -0
- package/src/security/regex-guard.js +277 -0
- package/src/security/row-level-security.js +192 -0
- package/src/security/secure-erase.js +258 -0
- package/src/security/sql-firewall.js +185 -0
- package/src/security/sql-injection-detector.js +647 -0
- package/src/security/sql-rate-limiter.js +168 -0
- package/src/security/ssrf-guard.js +790 -0
- package/src/security/timing-safe.js +203 -0
- package/src/serializer/codecs.js +1155 -0
- package/src/serializer/index.js +13 -0
- package/src/serializer/serializer.js +468 -0
- package/src/server/public-api.js +14 -0
- package/src/server.js +31 -0
- package/src/service/log-service.js +593 -0
- package/src/sharding/cluster-manager.js +161 -0
- package/src/sharding/consistent-hash-ring-optimized.js +421 -0
- package/src/sharding/consistent-hash-ring.js +252 -0
- package/src/sharding/consistent-hash.js +175 -0
- package/src/sharding/distributed-executor.js +120 -0
- package/src/sharding/distributed-lock.js +256 -0
- package/src/sharding/distributed-transaction.js +262 -0
- package/src/sharding/failure-recovery.js +267 -0
- package/src/sharding/global-tx-coordinator.js +1299 -0
- package/src/sharding/index.js +16 -0
- package/src/sharding/parallel-write-coordinator.js +370 -0
- package/src/sharding/partition-manager.js +382 -0
- package/src/sharding/pushdown-executor.js +1101 -0
- package/src/sharding/scatter-gather-executor.js +460 -0
- package/src/sharding/shard-executor.js +624 -0
- package/src/sharding/shard-manager.js +717 -0
- package/src/sharding/three-phase-commit.js +699 -0
- package/src/sharding/two-phase-commit.js +1243 -0
- package/src/snapshot/historical-reader.js +257 -0
- package/src/snapshot/index.js +7 -0
- package/src/storage/batch-writer.js +110 -0
- package/src/storage/binary-storage-adapter.js +738 -0
- package/src/storage/blob-kv-adapter.js +253 -0
- package/src/storage/buffer-pool.js +458 -0
- package/src/storage/buffered-kv-adapter.js +325 -0
- package/src/storage/cold-data-migrator.js +519 -0
- package/src/storage/column-compression.js +878 -0
- package/src/storage/column-store-table.js +627 -0
- package/src/storage/column-store.js +1094 -0
- package/src/storage/columnar-file.js +489 -0
- package/src/storage/columnar-storage.js +485 -0
- package/src/storage/compact-recovery.js +394 -0
- package/src/storage/encrypted-kv-adapter.js +271 -0
- package/src/storage/file-kv-adapter.js +468 -0
- package/src/storage/hybrid-engine.js +1480 -0
- package/src/storage/importer.js +125 -0
- package/src/storage/in-memory-table-storage.js +291 -0
- package/src/storage/index.js +78 -0
- package/src/storage/io-thread-pool.js +240 -0
- package/src/storage/io-worker.js +42 -0
- package/src/storage/kv-adapter.js +322 -0
- package/src/storage/lsm/active-table.js +240 -0
- package/src/storage/lsm/block-cache.js +64 -0
- package/src/storage/lsm/bloom-filter.js +174 -0
- package/src/storage/lsm/compactor.js +460 -0
- package/src/storage/lsm/database-manager.js +177 -0
- package/src/storage/lsm/full-text-search.js +385 -0
- package/src/storage/lsm/index.js +14 -0
- package/src/storage/lsm/internal-key.js +140 -0
- package/src/storage/lsm/lsm-engine.js +798 -0
- package/src/storage/lsm/lsm-kv-adapter.js +137 -0
- package/src/storage/lsm/memtable.js +628 -0
- package/src/storage/lsm/mmap-region.js +289 -0
- package/src/storage/lsm/sstable-reader.js +403 -0
- package/src/storage/lsm/sstable-repair.js +437 -0
- package/src/storage/lsm/sstable-writer.js +202 -0
- package/src/storage/lsm/wal.js +800 -0
- package/src/storage/offheap-allocator.js +298 -0
- package/src/storage/page-checksum.js +221 -0
- package/src/storage/page-compression.js +95 -0
- package/src/storage/page-store.js +418 -0
- package/src/storage/page.js +261 -0
- package/src/storage/persisted-db.js +1885 -0
- package/src/storage/persisted-table-storage.js +280 -0
- package/src/storage/read-ahead.js +95 -0
- package/src/storage/shared-storage.js +688 -0
- package/src/storage/soa-table-storage.js +418 -0
- package/src/storage/storage-engine.js +187 -0
- package/src/storage/table-storage.js +215 -0
- package/src/storage/tiered-kv-adapter.js +381 -0
- package/src/storage/version-history-cleaner.js +888 -0
- package/src/studio/index.html +1503 -0
- package/src/studio/server.js +204 -0
- package/src/sync/change-log.js +144 -0
- package/src/sync/index.js +2 -0
- package/src/sync/sync-engine.js +428 -0
- package/src/sync/sync-handlers.js +196 -0
- package/src/test-utils.js +11 -0
- package/src/transaction/index.js +8 -0
- package/src/transaction/read-uncommitted.js +37 -0
- package/src/transaction/repeatable-read.js +380 -0
- package/src/transaction/rr-transaction-manager.js +262 -0
- package/src/transaction/ru-transaction-manager.js +76 -0
- package/src/transaction/serializable.js +205 -0
- package/src/transaction/transaction-manager.js +259 -0
- package/src/transaction/tx-manager.js +490 -0
- package/src/transaction/tx-state.js +43 -0
- package/src/tx/bfmark-snapshot.js +193 -0
- package/src/tx/row-lock.js +246 -0
- package/src/tx/ssi.js +229 -0
- package/src/types/datetime.js +621 -0
- package/src/types/decimal.js +220 -0
- package/src/types/enum-type.js +131 -0
- package/src/types/interval.js +565 -0
- package/src/types/json-type.js +376 -0
- package/src/types/jsonb.js +227 -0
- package/src/types/udt.js +180 -0
- package/src/utils/batch-processor.js +46 -0
- package/src/utils/debug-logger.js +116 -0
- package/src/utils/index-helper.js +74 -0
- package/src/utils/index.js +9 -0
- package/src/utils/join-engine.js +1713 -0
- package/src/utils/object-pool.js +610 -0
- package/src/utils/query-tracer.js +489 -0
- package/src/utils/timeout-util.js +92 -0
- package/src/utils/tx-log-persist.js +197 -0
- package/src/utils/undo-compress.js +93 -0
- package/src/utils/undo-log.js +196 -0
- package/src/utils/wal-common.js +138 -0
- package/src/vector/adaptive-ef.js +213 -0
- package/src/vector/distributed-vector-search.js +542 -0
- package/src/vector/federated-vector-search.js +707 -0
- package/src/vector/gpu-distance.js +647 -0
- package/src/vector/gpu-sq-distance.js +444 -0
- package/src/vector/hnsw-acceleration.js +298 -0
- package/src/vector/hnsw-profiler.js +341 -0
- package/src/vector/index.js +90 -0
- package/src/vector/public-api.js +29 -0
- package/src/vector/recall-benchmark.js +333 -0
- package/src/vector/simd-distance.js +511 -0
- package/src/vector/vector-scan.js +646 -0
- package/src/vector/wasm-hnsw.js +714 -0
- package/src/version/version-chain-manager.js +1277 -0
- package/src/version/version-delta-encoder.js +819 -0
- package/src/version.js +19 -0
- package/src/view/incremental-refresher.js +1015 -0
- package/src/view/index.js +12 -0
- package/src/view/materialized-view.js +204 -0
- package/src/view/view-manager.js +1368 -0
- package/src/wasm/bridge.js +79 -0
- package/src/worker/query-dispatcher.js +277 -0
- package/src/worker/session-worker-pool.js +355 -0
- package/src/worker/task-defs.js +330 -0
- package/src/worker/task-worker.js +406 -0
- package/src/worker/worker-pool.js +651 -0
- package/src/xml/xml-support.js +647 -0
- package/types/pure-js-db.d.ts +1988 -0
- package/types/schema.d.ts +65 -0
|
@@ -0,0 +1,1243 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* two-phase-commit.js — 两阶段提交(2PC)协调者
|
|
3
|
+
*
|
|
4
|
+
* 实现 X/Open XA 规范的核心协议:
|
|
5
|
+
* 阶段 1(Prepare):协调者向所有参与者发送"准备提交"请求
|
|
6
|
+
* 阶段 2(Commit/Rollback):全部成功则提交,任一失败则回滚
|
|
7
|
+
*
|
|
8
|
+
* 利用 Executor 向每个分片发送事务控制命令(BEGIN/COMMIT/ROLLBACK),
|
|
9
|
+
* 并通过 Database 快照机制(savepoint)实现参与者级别的回滚。
|
|
10
|
+
*
|
|
11
|
+
* P4 增强:支持事务日志持久化到磁盘(Node.js 环境),
|
|
12
|
+
* 浏览器环境静默降级为纯内存模式。
|
|
13
|
+
*
|
|
14
|
+
* v1.37.0 G4 性能优化:
|
|
15
|
+
* - Executor 实例缓存(避免高频 new Executor 开销)
|
|
16
|
+
* - 日志批量异步持久化(debounce,关键节点强制 flush)
|
|
17
|
+
* - 真正的并行 prepare(Promise.all)
|
|
18
|
+
* - 批量事务接口(executeBatchTransactions,减少协调者往返)
|
|
19
|
+
* - Pipeline 日志合并(PREPARING+PREPARED 合并写入)
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import { Executor } from '../executor/executor.js';
|
|
23
|
+
import fs from 'fs';
|
|
24
|
+
import path from 'path';
|
|
25
|
+
import { loadTransactionLog, saveTransactionLog } from '../utils/tx-log-persist.js';
|
|
26
|
+
import { DistributedLockManager } from './distributed-lock.js';
|
|
27
|
+
|
|
28
|
+
/** 事务状态枚举 */
|
|
29
|
+
export const TX_STATUS = {
|
|
30
|
+
ACTIVE: 'ACTIVE',
|
|
31
|
+
PREPARING: 'PREPARING',
|
|
32
|
+
PREPARED: 'PREPARED',
|
|
33
|
+
COMMITTING: 'COMMITTING',
|
|
34
|
+
COMMITTED: 'COMMITTED',
|
|
35
|
+
ABORTING: 'ABORTING',
|
|
36
|
+
ABORTED: 'ABORTED',
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/** 参与者状态枚举 */
|
|
40
|
+
export const PARTICIPANT_STATUS = {
|
|
41
|
+
ACTIVE: 'ACTIVE',
|
|
42
|
+
PREPARED: 'PREPARED',
|
|
43
|
+
COMMITTED: 'COMMITTED',
|
|
44
|
+
ABORTED: 'ABORTED',
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
let _txIdCounter = 0;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 生成全局唯一的事务 ID
|
|
51
|
+
*/
|
|
52
|
+
function generateTransactionId() {
|
|
53
|
+
_txIdCounter++;
|
|
54
|
+
const ts = Date.now().toString(36);
|
|
55
|
+
const counter = _txIdCounter.toString(36);
|
|
56
|
+
return `xid_${ts}_${counter}`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export class TwoPhaseCommitCoordinator {
|
|
60
|
+
/**
|
|
61
|
+
* @param {import('./shard-manager.js').ShardManager} shardManager
|
|
62
|
+
* @param {Object} [options]
|
|
63
|
+
* @param {string} [options.logFilePath] - 持久化日志文件路径,默认 './tx-log.json'
|
|
64
|
+
* @param {boolean} [options.persistLog] - 是否启用持久化,默认 true(Node.js 环境自动检测)
|
|
65
|
+
*/
|
|
66
|
+
constructor(shardManager, options = {}) {
|
|
67
|
+
this.shardManager = shardManager;
|
|
68
|
+
/** @type {Array<Object>} 事务日志 */
|
|
69
|
+
this._transactionLog = [];
|
|
70
|
+
/** @type {Map<string, Object>} 活跃事务索引 */
|
|
71
|
+
this._activeTransactions = new Map();
|
|
72
|
+
|
|
73
|
+
// P4 持久化事务日志
|
|
74
|
+
this._logFilePath = options.logFilePath || path.resolve('./tx-log.json');
|
|
75
|
+
this._persistEnabled = options.persistLog !== undefined ? options.persistLog : true;
|
|
76
|
+
this._fsAvailable = false;
|
|
77
|
+
|
|
78
|
+
// 检测 Node.js fs 模块是否可用(浏览器环境不可用)
|
|
79
|
+
try {
|
|
80
|
+
if (typeof fs.writeFileSync === 'function') {
|
|
81
|
+
this._fsAvailable = true;
|
|
82
|
+
}
|
|
83
|
+
} catch (e) {
|
|
84
|
+
this._fsAvailable = false;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// 启动时从文件加载历史日志
|
|
88
|
+
if (this._fsAvailable && this._persistEnabled) {
|
|
89
|
+
this._loadLog();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// 分布式锁管理器
|
|
93
|
+
this._lockManager = DistributedLockManager.getInstance(options);
|
|
94
|
+
|
|
95
|
+
// v1.37.0 G4:Executor 实例缓存(每 nodeName 一个,避免高频 new Executor)
|
|
96
|
+
/** @type {Map<string, Executor>} */
|
|
97
|
+
this._executorCache = new Map();
|
|
98
|
+
|
|
99
|
+
// v1.37.0 G4:日志批量异步持久化
|
|
100
|
+
// _log() 不再每次都写磁盘,而是标记 _logDirty,由 _flushLogSoon() 节流写入
|
|
101
|
+
// 关键事件(COMMITTED/ABORTED)调用 _flushLogNow() 立即落盘
|
|
102
|
+
this._logDirty = false;
|
|
103
|
+
this._logFlushScheduled = false;
|
|
104
|
+
this._logFlushTimer = null;
|
|
105
|
+
/** 日志批量持久化延迟(ms),0 表示禁用(每次都同步写) */
|
|
106
|
+
this._logFlushDelay = options.logFlushDelay !== undefined ? options.logFlushDelay : 10;
|
|
107
|
+
/** 强制 flush 的事件类型(这些事件必须立即落盘以保证崩溃一致性) */
|
|
108
|
+
this._criticalFlushEvents = new Set(['COMMITTED', 'ABORTED', 'PREPARED', 'PREPARE_FAILED']);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* 从磁盘文件加载事务日志(委托给 tx-log-persist 工具)
|
|
113
|
+
*/
|
|
114
|
+
_loadLog() {
|
|
115
|
+
this._transactionLog = loadTransactionLog(this._logFilePath);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* 将事务日志持久化到磁盘文件(委托给 tx-log-persist 工具)
|
|
120
|
+
*
|
|
121
|
+
* v1.37.0 G4:此方法现在仅在 _logDirty=true 时执行实际 I/O。
|
|
122
|
+
* 调用方应使用 _flushLogNow()(强制立即写入)或 _flushLogSoon()(节流写入)。
|
|
123
|
+
*/
|
|
124
|
+
_persistLog() {
|
|
125
|
+
if (!this._fsAvailable || !this._persistEnabled) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
if (!this._logDirty) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
saveTransactionLog(this._logFilePath, this._transactionLog);
|
|
132
|
+
this._logDirty = false;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* v1.37.0 G4:立即强制 flush 日志到磁盘
|
|
137
|
+
*/
|
|
138
|
+
_flushLogNow() {
|
|
139
|
+
if (!this._fsAvailable || !this._persistEnabled) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
if (this._logFlushTimer) {
|
|
143
|
+
clearTimeout(this._logFlushTimer);
|
|
144
|
+
this._logFlushTimer = null;
|
|
145
|
+
this._logFlushScheduled = false;
|
|
146
|
+
}
|
|
147
|
+
if (this._logDirty) {
|
|
148
|
+
saveTransactionLog(this._logFilePath, this._transactionLog);
|
|
149
|
+
this._logDirty = false;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* v1.37.0 G4:节流 flush 日志到磁盘(延迟 _logFlushDelay ms 合并多次写入)
|
|
155
|
+
*/
|
|
156
|
+
_flushLogSoon() {
|
|
157
|
+
if (!this._fsAvailable || !this._persistEnabled) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
if (this._logFlushDelay <= 0) {
|
|
161
|
+
// 延迟为 0 表示禁用节流,直接同步写
|
|
162
|
+
this._flushLogNow();
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (this._logFlushScheduled) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
this._logFlushScheduled = true;
|
|
169
|
+
this._logFlushTimer = setTimeout(() => {
|
|
170
|
+
this._logFlushTimer = null;
|
|
171
|
+
this._logFlushScheduled = false;
|
|
172
|
+
if (this._logDirty) {
|
|
173
|
+
saveTransactionLog(this._logFilePath, this._transactionLog);
|
|
174
|
+
this._logDirty = false;
|
|
175
|
+
}
|
|
176
|
+
}, this._logFlushDelay);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* 获取分片的 Executor
|
|
181
|
+
*
|
|
182
|
+
* v1.37.0 G4:使用缓存避免高频 new Executor(db) 开销
|
|
183
|
+
* @param {string} nodeName
|
|
184
|
+
* @returns {Executor}
|
|
185
|
+
*/
|
|
186
|
+
_getExecutor(nodeName) {
|
|
187
|
+
const db = this.shardManager.shards[nodeName];
|
|
188
|
+
if (!db) {
|
|
189
|
+
throw new Error(`分片 "${nodeName}" 不存在`);
|
|
190
|
+
}
|
|
191
|
+
let executor = this._executorCache.get(nodeName);
|
|
192
|
+
if (!executor) {
|
|
193
|
+
executor = new Executor(db);
|
|
194
|
+
this._executorCache.set(nodeName, executor);
|
|
195
|
+
}
|
|
196
|
+
return executor;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* v1.37.0 G4:清理 Executor 缓存(分片被移除时调用)
|
|
201
|
+
* @param {string} nodeName
|
|
202
|
+
*/
|
|
203
|
+
_invalidateExecutorCache(nodeName) {
|
|
204
|
+
this._executorCache.delete(nodeName);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* 记录事务日志
|
|
209
|
+
*
|
|
210
|
+
* v1.37.0 G4:根据事件类型决定持久化策略
|
|
211
|
+
* - 关键事件(COMMITTED/ABORTED/PREPARED/PREPARE_FAILED):立即 flush
|
|
212
|
+
* - 普通事件(BEGIN/PREPARING/COMMITTING/ABORTING):节流 flush
|
|
213
|
+
* - logFlushDelay=0 时退化为每次同步写(向后兼容)
|
|
214
|
+
*
|
|
215
|
+
* @param {Object} entry
|
|
216
|
+
*/
|
|
217
|
+
_log(entry) {
|
|
218
|
+
this._transactionLog.push({ ...entry, timestamp: Date.now() });
|
|
219
|
+
this._logDirty = true;
|
|
220
|
+
if (this._criticalFlushEvents.has(entry.event)) {
|
|
221
|
+
// 关键事件:立即落盘保证崩溃一致性
|
|
222
|
+
this._flushLogNow();
|
|
223
|
+
} else {
|
|
224
|
+
// 普通事件:节流落盘
|
|
225
|
+
this._flushLogSoon();
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* 开始分布式事务
|
|
231
|
+
* @returns {string|Promise<string>} transactionId(同步返回字符串,若内部有异步操作则返回 Promise)
|
|
232
|
+
*/
|
|
233
|
+
begin() {
|
|
234
|
+
const transactionId = generateTransactionId();
|
|
235
|
+
const nodes = Object.keys(this.shardManager.shards);
|
|
236
|
+
|
|
237
|
+
// 在所有分片上执行 BEGIN
|
|
238
|
+
let hasPromise = false;
|
|
239
|
+
const promises = [];
|
|
240
|
+
for (const nodeName of nodes) {
|
|
241
|
+
const executor = this._getExecutor(nodeName);
|
|
242
|
+
const result = executor.executeSQL('BEGIN');
|
|
243
|
+
if (result instanceof Promise) {
|
|
244
|
+
hasPromise = true;
|
|
245
|
+
promises.push(result);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const txRecord = {
|
|
250
|
+
transactionId,
|
|
251
|
+
status: TX_STATUS.ACTIVE,
|
|
252
|
+
participants: nodes.map(name => ({
|
|
253
|
+
node: name,
|
|
254
|
+
status: PARTICIPANT_STATUS.ACTIVE,
|
|
255
|
+
})),
|
|
256
|
+
beginTime: Date.now(),
|
|
257
|
+
operations: [],
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
this._activeTransactions.set(transactionId, txRecord);
|
|
261
|
+
this._log({ ...txRecord, event: 'BEGIN' });
|
|
262
|
+
|
|
263
|
+
// 同步路径:直接返回 txId
|
|
264
|
+
if (!hasPromise) {
|
|
265
|
+
return transactionId;
|
|
266
|
+
}
|
|
267
|
+
// 异步路径:等待所有 BEGIN 完成后返回 txId
|
|
268
|
+
return Promise.all(promises).then(() => transactionId);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* 在指定分片上执行 SQL(事务内)
|
|
273
|
+
* @param {string} transactionId
|
|
274
|
+
* @param {string} nodeName
|
|
275
|
+
* @param {string} sql
|
|
276
|
+
* @param {Array} [params]
|
|
277
|
+
* @returns {Object|Promise<Object>}
|
|
278
|
+
*/
|
|
279
|
+
executeOnShard(transactionId, nodeName, sql, params = []) {
|
|
280
|
+
const tx = this._activeTransactions.get(transactionId);
|
|
281
|
+
if (!tx) {
|
|
282
|
+
throw new Error(`事务 "${transactionId}" 不存在或已结束`);
|
|
283
|
+
}
|
|
284
|
+
if (tx.status !== TX_STATUS.ACTIVE) {
|
|
285
|
+
throw new Error(`事务 "${transactionId}" 已处于 ${tx.status} 状态,无法执行操作`);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const participant = tx.participants.find(p => p.node === nodeName);
|
|
289
|
+
if (!participant) {
|
|
290
|
+
throw new Error(`分片 "${nodeName}" 不是事务 "${transactionId}" 的参与者`);
|
|
291
|
+
}
|
|
292
|
+
if (participant.status !== PARTICIPANT_STATUS.ACTIVE) {
|
|
293
|
+
throw new Error(`分片 "${nodeName}" 在事务 "${transactionId}" 中已处于 ${participant.status} 状态`);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const executor = this._getExecutor(nodeName);
|
|
297
|
+
const result = executor.executeSQL(sql, params);
|
|
298
|
+
|
|
299
|
+
// 同步路径:直接记录并返回
|
|
300
|
+
if (!(result instanceof Promise)) {
|
|
301
|
+
tx.operations.push({ node: nodeName, sql, params, result });
|
|
302
|
+
return result;
|
|
303
|
+
}
|
|
304
|
+
// 异步路径:await 后记录
|
|
305
|
+
return result.then(resolved => {
|
|
306
|
+
tx.operations.push({ node: nodeName, sql, params, result: resolved });
|
|
307
|
+
return resolved;
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* 持久化 PREPARE TRANSACTION 到参与者的本地存储
|
|
313
|
+
*
|
|
314
|
+
* 将待提交事务的 prepare 状态写入参与者的持久化层(KV/WAL),
|
|
315
|
+
* 确保参与者崩溃后重启时能识别并恢复悬挂的 PREPARED 事务。
|
|
316
|
+
*
|
|
317
|
+
* 若参与者数据库支持 KV 持久化(PersistedDatabase),
|
|
318
|
+
* 写入专用 key: `db:{dbName}:prepared_tx:{transactionId}`。
|
|
319
|
+
* 若不支持(内存 Database),仅记录警告,由协调者日志兜底。
|
|
320
|
+
*
|
|
321
|
+
* @param {string} transactionId
|
|
322
|
+
* @param {object} participant - { node, status }
|
|
323
|
+
* @param {object} db - 参与者数据库实例
|
|
324
|
+
* @throws {Error} 持久化失败时抛出,使 prepare 阶段失败
|
|
325
|
+
*/
|
|
326
|
+
_persistPrepareTransaction(transactionId, participant, db) {
|
|
327
|
+
// 检查数据库是否支持 KV 持久化
|
|
328
|
+
if (db.kv && typeof db.kv.set === 'function') {
|
|
329
|
+
const dbName = db.dbName || db.name || 'default';
|
|
330
|
+
const key = `db:${dbName}:prepared_tx:${transactionId}`;
|
|
331
|
+
const value = JSON.stringify({
|
|
332
|
+
event: 'PREPARE_TRANSACTION',
|
|
333
|
+
transactionId,
|
|
334
|
+
node: participant.node,
|
|
335
|
+
timestamp: Date.now(),
|
|
336
|
+
});
|
|
337
|
+
// 同步写入 KV(writeToWAL 不可用时采用直接 KV 写入)
|
|
338
|
+
// 若 kv.set 返回 Promise,则抛错让 prepare 失败(不支持同步路径)
|
|
339
|
+
const result = db.kv.set(key, value);
|
|
340
|
+
if (result instanceof Promise) {
|
|
341
|
+
throw new Error(`PREPARE TRANSACTION 持久化失败: ${participant.node} 的 kv.set 返回异步结果,prepare 阶段不支持异步`);
|
|
342
|
+
}
|
|
343
|
+
// 若有 flush 方法,立即落盘
|
|
344
|
+
if (typeof db.kv.flush === 'function') {
|
|
345
|
+
db.kv.flush();
|
|
346
|
+
}
|
|
347
|
+
} else if (db._wal || (db._executor && db._executor.wal)) {
|
|
348
|
+
// 数据库有 WAL,通过 WAL 记录 prepare 事件
|
|
349
|
+
const wal = db._wal || db._executor.wal;
|
|
350
|
+
// WAL 目前没有直接支持 PREPARE_TRANSACTION 的操作码,
|
|
351
|
+
// 用 _append 写入一条自定义条目
|
|
352
|
+
if (typeof wal._append === 'function') {
|
|
353
|
+
const result = wal._append({
|
|
354
|
+
op: 'PREPARE_TRANSACTION',
|
|
355
|
+
table: '',
|
|
356
|
+
transactionId,
|
|
357
|
+
node: participant.node,
|
|
358
|
+
});
|
|
359
|
+
if (result instanceof Promise) {
|
|
360
|
+
throw new Error(`PREPARE TRANSACTION WAL 写入失败: ${participant.node} 的 WAL _append 返回异步结果`);
|
|
361
|
+
}
|
|
362
|
+
} else {
|
|
363
|
+
console.warn(`[2PC] 分片 "${participant.node}" 的 WAL 不支持 _append,prepare 状态仅记录在内存`);
|
|
364
|
+
}
|
|
365
|
+
} else {
|
|
366
|
+
// 无持久化支持:仅记录警告,prepare 状态由协调者日志兜底
|
|
367
|
+
console.warn(`[2PC] 分片 "${participant.node}" 不支持 PREPARE TRANSACTION 持久化,崩溃后 prepare 状态可能丢失`);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* 恢复所有悬挂的 PREPARED 事务
|
|
373
|
+
*
|
|
374
|
+
* 扫描所有分片的 KV 存储,查找 `db:*:prepared_tx:*` 模式的 key,
|
|
375
|
+
* 对每个悬挂的 PREPARED 事务,协调者根据自身事务日志决定提交或回滚。
|
|
376
|
+
*
|
|
377
|
+
* 此方法应在协调者启动时调用(recoverAfterCrash 内部已调用),
|
|
378
|
+
* 也可在运行时手动调用以清理异常悬挂的事务。
|
|
379
|
+
*
|
|
380
|
+
* @returns {Promise<{ recovered: number, details: Array<{transactionId: string, node: string, action: string}> }>}
|
|
381
|
+
*/
|
|
382
|
+
async _recoverPreparedTransactions() {
|
|
383
|
+
const details = [];
|
|
384
|
+
|
|
385
|
+
// 扫描所有分片
|
|
386
|
+
for (const [nodeName, db] of Object.entries(this.shardManager.shards)) {
|
|
387
|
+
if (!db.kv || typeof db.kv.keys !== 'function') {
|
|
388
|
+
continue;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
const dbName = db.dbName || db.name || 'default';
|
|
392
|
+
const prefix = `db:${dbName}:prepared_tx:`;
|
|
393
|
+
let keys;
|
|
394
|
+
try {
|
|
395
|
+
keys = await db.kv.keys(prefix);
|
|
396
|
+
} catch (e) {
|
|
397
|
+
console.error(`[2PC] 扫描分片 "${nodeName}" 的 PREPARED 事务 key 失败: ${e.message}`);
|
|
398
|
+
continue;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
if (!keys || keys.length === 0) {
|
|
402
|
+
continue;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
for (const key of keys) {
|
|
406
|
+
try {
|
|
407
|
+
const raw = await db.kv.get(key);
|
|
408
|
+
if (!raw) {
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
const meta = JSON.parse(raw);
|
|
412
|
+
if (meta.event !== 'PREPARE_TRANSACTION') {
|
|
413
|
+
continue;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
const txId = meta.transactionId;
|
|
417
|
+
|
|
418
|
+
// 检查协调者的日志中此事务的最终状态
|
|
419
|
+
const txLogs = this._transactionLog.filter(e => e.transactionId === txId);
|
|
420
|
+
const hasCommit = txLogs.some(e => e.event === 'COMMITTED');
|
|
421
|
+
const hasAbort = txLogs.some(e => e.event === 'ABORTED');
|
|
422
|
+
|
|
423
|
+
if (hasCommit) {
|
|
424
|
+
// 协调者日志显示已提交:清理 prepare marker
|
|
425
|
+
await db.kv.remove(key);
|
|
426
|
+
details.push({ transactionId: txId, node: nodeName, action: 'CLEANUP_COMMITTED' });
|
|
427
|
+
} else if (hasAbort) {
|
|
428
|
+
// 协调者日志显示已回滚:清理 prepare marker
|
|
429
|
+
await db.kv.remove(key);
|
|
430
|
+
details.push({ transactionId: txId, node: nodeName, action: 'CLEANUP_ABORTED' });
|
|
431
|
+
} else {
|
|
432
|
+
// 悬挂事务:记录日志,等待协调者的 recover() 流程处理
|
|
433
|
+
console.warn(`[2PC] 发现悬挂 PREPARED 事务: txId=${txId}, node=${nodeName},等待 recover() 处理`);
|
|
434
|
+
details.push({ transactionId: txId, node: nodeName, action: 'PENDING_RECOVER' });
|
|
435
|
+
}
|
|
436
|
+
} catch (e) {
|
|
437
|
+
console.error(`[2PC] 恢复 PREPARED 事务 key="${key}" 失败: ${e.message}`);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
return { recovered: details.length, details };
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* 准备阶段:所有参与者准备提交
|
|
447
|
+
* 验证所有参与者都在 ACTIVE 状态并可提交
|
|
448
|
+
*
|
|
449
|
+
* v1.28.1+ 修复:
|
|
450
|
+
* 1. PREPARING 日志现在包含 participants 列表与 operations 摘要,
|
|
451
|
+
* 协调者崩溃后可通过日志重建事务状态。
|
|
452
|
+
* 2. PREPARED 日志同时记录参与者列表,便于恢复时识别未完成事务的参与者。
|
|
453
|
+
*
|
|
454
|
+
* v1.34.11+ 修复:
|
|
455
|
+
* 添加 _persistPrepareTransaction() 将 prepare 状态持久化到参与者的本地存储,
|
|
456
|
+
* 参与者崩溃后可恢复悬挂的 PREPARED 事务。
|
|
457
|
+
*
|
|
458
|
+
* @param {string} transactionId
|
|
459
|
+
* @returns {{ success: boolean, preparedNodes: string[], failedNodes: Array<{node: string, error: string}> }}
|
|
460
|
+
*/
|
|
461
|
+
prepare(transactionId) {
|
|
462
|
+
const tx = this._activeTransactions.get(transactionId);
|
|
463
|
+
if (!tx) {
|
|
464
|
+
throw new Error(`事务 "${transactionId}" 不存在`);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
tx.status = TX_STATUS.PREPARING;
|
|
468
|
+
// v1.28.1+: 记录完整的参与者列表和操作摘要,用于崩溃恢复
|
|
469
|
+
this._log({
|
|
470
|
+
transactionId,
|
|
471
|
+
event: 'PREPARING',
|
|
472
|
+
participants: tx.participants.map(p => ({ node: p.node, status: p.status })),
|
|
473
|
+
operationsCount: tx.operations.length,
|
|
474
|
+
operationsSummary: tx.operations.map(op => ({ node: op.node, sql: op.sql })),
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
const preparedNodes = [];
|
|
478
|
+
const failedNodes = [];
|
|
479
|
+
|
|
480
|
+
for (const participant of tx.participants) {
|
|
481
|
+
try {
|
|
482
|
+
// 验证分片仍然存在且在事务中
|
|
483
|
+
const db = this.shardManager.shards[participant.node];
|
|
484
|
+
if (!db) {
|
|
485
|
+
throw new Error(`分片 "${participant.node}" 已不存在`);
|
|
486
|
+
}
|
|
487
|
+
if (!db.inTransaction) {
|
|
488
|
+
throw new Error(`分片 "${participant.node}" 不在事务中`);
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
// v1.34.11+:将 PREPARE TRANSACTION 状态持久化到参与者本地存储
|
|
492
|
+
// 写入后再标记 PREPARED,确保崩溃后 participant 可恢复
|
|
493
|
+
this._persistPrepareTransaction(transactionId, participant, db);
|
|
494
|
+
|
|
495
|
+
// 标记为 PREPARED
|
|
496
|
+
participant.status = PARTICIPANT_STATUS.PREPARED;
|
|
497
|
+
preparedNodes.push(participant.node);
|
|
498
|
+
} catch (err) {
|
|
499
|
+
participant.status = PARTICIPANT_STATUS.ACTIVE; // 保持原状
|
|
500
|
+
failedNodes.push({ node: participant.node, error: err.message });
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
if (failedNodes.length === 0) {
|
|
505
|
+
tx.status = TX_STATUS.PREPARED;
|
|
506
|
+
// v1.28.1+: PREPARED 日志包含参与者列表,便于恢复时识别未完成事务
|
|
507
|
+
this._log({
|
|
508
|
+
transactionId,
|
|
509
|
+
event: 'PREPARED',
|
|
510
|
+
preparedNodes,
|
|
511
|
+
participants: tx.participants.map(p => ({ node: p.node, status: p.status })),
|
|
512
|
+
});
|
|
513
|
+
} else {
|
|
514
|
+
tx.status = TX_STATUS.ABORTING;
|
|
515
|
+
this._log({ transactionId, event: 'PREPARE_FAILED', failedNodes });
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
return {
|
|
519
|
+
success: failedNodes.length === 0,
|
|
520
|
+
preparedNodes,
|
|
521
|
+
failedNodes,
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* v1.37.0 G4:并行 prepare(真正并行执行 _persistPrepareTransaction)
|
|
527
|
+
*
|
|
528
|
+
* 与 prepare() 的区别:
|
|
529
|
+
* - prepare():串行循环,每个 participant 顺序执行 _persistPrepareTransaction
|
|
530
|
+
* - prepareParallel():使用 Promise.all 并行执行所有 participant 的 prepare
|
|
531
|
+
*
|
|
532
|
+
* 在 _persistPrepareTransaction 涉及 I/O(KV 写入 / WAL append)时,
|
|
533
|
+
* 并行化可显著减少多分片场景下的 prepare 延迟。
|
|
534
|
+
*
|
|
535
|
+
* @param {string} transactionId
|
|
536
|
+
* @returns {Promise<{ success: boolean, preparedNodes: string[], failedNodes: Array<{node: string, error: string}> }>}
|
|
537
|
+
*/
|
|
538
|
+
async prepareParallel(transactionId) {
|
|
539
|
+
const tx = this._activeTransactions.get(transactionId);
|
|
540
|
+
if (!tx) {
|
|
541
|
+
throw new Error(`事务 "${transactionId}" 不存在`);
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
tx.status = TX_STATUS.PREPARING;
|
|
545
|
+
this._log({
|
|
546
|
+
transactionId,
|
|
547
|
+
event: 'PREPARING',
|
|
548
|
+
participants: tx.participants.map(p => ({ node: p.node, status: p.status })),
|
|
549
|
+
operationsCount: tx.operations.length,
|
|
550
|
+
operationsSummary: tx.operations.map(op => ({ node: op.node, sql: op.sql })),
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
// 并行执行所有 participant 的 prepare
|
|
554
|
+
const preparePromises = tx.participants.map(async (participant) => {
|
|
555
|
+
try {
|
|
556
|
+
const db = this.shardManager.shards[participant.node];
|
|
557
|
+
if (!db) {
|
|
558
|
+
throw new Error(`分片 "${participant.node}" 已不存在`);
|
|
559
|
+
}
|
|
560
|
+
if (!db.inTransaction) {
|
|
561
|
+
throw new Error(`分片 "${participant.node}" 不在事务中`);
|
|
562
|
+
}
|
|
563
|
+
this._persistPrepareTransaction(transactionId, participant, db);
|
|
564
|
+
participant.status = PARTICIPANT_STATUS.PREPARED;
|
|
565
|
+
return { node: participant.node, success: true };
|
|
566
|
+
} catch (err) {
|
|
567
|
+
participant.status = PARTICIPANT_STATUS.ACTIVE;
|
|
568
|
+
return { node: participant.node, success: false, error: err.message };
|
|
569
|
+
}
|
|
570
|
+
});
|
|
571
|
+
|
|
572
|
+
const results = await Promise.all(preparePromises);
|
|
573
|
+
const preparedNodes = results.filter(r => r.success).map(r => r.node);
|
|
574
|
+
const failedNodes = results.filter(r => !r.success).map(r => ({ node: r.node, error: r.error }));
|
|
575
|
+
|
|
576
|
+
if (failedNodes.length === 0) {
|
|
577
|
+
tx.status = TX_STATUS.PREPARED;
|
|
578
|
+
this._log({
|
|
579
|
+
transactionId,
|
|
580
|
+
event: 'PREPARED',
|
|
581
|
+
preparedNodes,
|
|
582
|
+
participants: tx.participants.map(p => ({ node: p.node, status: p.status })),
|
|
583
|
+
});
|
|
584
|
+
} else {
|
|
585
|
+
tx.status = TX_STATUS.ABORTING;
|
|
586
|
+
this._log({ transactionId, event: 'PREPARE_FAILED', failedNodes });
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
return {
|
|
590
|
+
success: failedNodes.length === 0,
|
|
591
|
+
preparedNodes,
|
|
592
|
+
failedNodes,
|
|
593
|
+
};
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
/**
|
|
597
|
+
* 从 _transactionLog 重建事务记录(崩溃恢复用)
|
|
598
|
+
*
|
|
599
|
+
* v1.28.1+ 新增:recoverAfterCrash 在 _activeTransactions 为空时,
|
|
600
|
+
* 需要从持久化的事务日志中重建事务记录才能调用 recover()。
|
|
601
|
+
*
|
|
602
|
+
* 重建规则:
|
|
603
|
+
* 1. 从 BEGIN 事件中提取 transactionId、participants、beginTime、operations
|
|
604
|
+
* 2. 从后续事件中追踪 participants 状态变化
|
|
605
|
+
* 3. 根据 PREPARED / PREPARE_FAILED / COMMITTED / ABORTED 决定最终 status
|
|
606
|
+
*
|
|
607
|
+
* @param {string} transactionId
|
|
608
|
+
* @returns {Object|null} 重建的事务记录,若日志中无此事务则返回 null
|
|
609
|
+
*/
|
|
610
|
+
_reconstructTxFromLog(transactionId) {
|
|
611
|
+
const txLogs = this._transactionLog.filter(e => e.transactionId === transactionId);
|
|
612
|
+
if (txLogs.length === 0) {
|
|
613
|
+
return null;
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
// BEGIN 事件包含完整的事务记录快照
|
|
617
|
+
const beginLog = txLogs.find(e => e.event === 'BEGIN');
|
|
618
|
+
if (!beginLog) {
|
|
619
|
+
return null;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
const tx = {
|
|
623
|
+
transactionId,
|
|
624
|
+
status: beginLog.status || TX_STATUS.ACTIVE,
|
|
625
|
+
participants: (beginLog.participants || []).map(p => ({ ...p })),
|
|
626
|
+
beginTime: beginLog.beginTime || Date.now(),
|
|
627
|
+
operations: (beginLog.operations || []).map(op => ({ ...op })),
|
|
628
|
+
};
|
|
629
|
+
|
|
630
|
+
// 按时间顺序应用后续事件以更新 status
|
|
631
|
+
for (const entry of txLogs) {
|
|
632
|
+
switch (entry.event) {
|
|
633
|
+
case 'PREPARING':
|
|
634
|
+
tx.status = TX_STATUS.PREPARING;
|
|
635
|
+
break;
|
|
636
|
+
case 'PREPARED':
|
|
637
|
+
tx.status = TX_STATUS.PREPARED;
|
|
638
|
+
if (entry.participants) {
|
|
639
|
+
tx.participants = entry.participants.map(p => ({ ...p }));
|
|
640
|
+
}
|
|
641
|
+
break;
|
|
642
|
+
case 'PREPARE_FAILED':
|
|
643
|
+
tx.status = TX_STATUS.ABORTING;
|
|
644
|
+
break;
|
|
645
|
+
case 'COMMITTING':
|
|
646
|
+
tx.status = TX_STATUS.COMMITTING;
|
|
647
|
+
break;
|
|
648
|
+
case 'COMMITTED':
|
|
649
|
+
tx.status = TX_STATUS.COMMITTED;
|
|
650
|
+
break;
|
|
651
|
+
case 'ABORTING':
|
|
652
|
+
tx.status = TX_STATUS.ABORTING;
|
|
653
|
+
break;
|
|
654
|
+
case 'ABORTED':
|
|
655
|
+
tx.status = TX_STATUS.ABORTED;
|
|
656
|
+
break;
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
return tx;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
/**
|
|
664
|
+
* 提交阶段:所有参与者提交事务
|
|
665
|
+
* @param {string} transactionId
|
|
666
|
+
* @returns {{ success: boolean, committedNodes: string[] }|Promise<{ success: boolean, committedNodes: string[] }>}
|
|
667
|
+
*/
|
|
668
|
+
commit(transactionId) {
|
|
669
|
+
const tx = this._activeTransactions.get(transactionId);
|
|
670
|
+
if (!tx) {
|
|
671
|
+
throw new Error(`事务 "${transactionId}" 不存在`);
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
tx.status = TX_STATUS.COMMITTING;
|
|
675
|
+
this._log({ transactionId, event: 'COMMITTING' });
|
|
676
|
+
|
|
677
|
+
const committedNodes = [];
|
|
678
|
+
const promises = [];
|
|
679
|
+
let hasPromise = false;
|
|
680
|
+
|
|
681
|
+
for (const participant of tx.participants) {
|
|
682
|
+
// v2.12 E16 M3: 节点提交成功才计入 committedNodes(同步路径行为不变;
|
|
683
|
+
// 异步路径 success 后标记,rejection 按协议记录 COMMIT_NODE_FAILED 不泄漏)
|
|
684
|
+
const finalizeParticipant = () => {
|
|
685
|
+
participant.status = PARTICIPANT_STATUS.COMMITTED;
|
|
686
|
+
committedNodes.push(participant.node);
|
|
687
|
+
};
|
|
688
|
+
const handleError = (err) => {
|
|
689
|
+
this._log({
|
|
690
|
+
transactionId,
|
|
691
|
+
event: 'COMMIT_NODE_FAILED',
|
|
692
|
+
node: participant.node,
|
|
693
|
+
error: err.message,
|
|
694
|
+
});
|
|
695
|
+
};
|
|
696
|
+
|
|
697
|
+
try {
|
|
698
|
+
const executor = this._getExecutor(participant.node);
|
|
699
|
+
const db = this.shardManager.shards[participant.node];
|
|
700
|
+
|
|
701
|
+
// 如果该分片尚在事务中,执行 COMMIT
|
|
702
|
+
if (db.inTransaction) {
|
|
703
|
+
const result = executor.executeSQL('COMMIT');
|
|
704
|
+
if (result instanceof Promise) {
|
|
705
|
+
// v2.12 E16 M3 修复:原代码 push 裸 Promise + 无条件提前标记节点已提交,
|
|
706
|
+
// 真实网络(executeSQL 必然异步)下节点失败时 rejection 泄漏(Promise.all reject)
|
|
707
|
+
// 且断连节点被误计为已提交(success 误报 true)。
|
|
708
|
+
hasPromise = true;
|
|
709
|
+
promises.push(result.then(finalizeParticipant).catch(handleError));
|
|
710
|
+
continue;
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
finalizeParticipant();
|
|
714
|
+
} catch (err) {
|
|
715
|
+
handleError(err);
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
const finalize = () => {
|
|
720
|
+
tx.status = TX_STATUS.COMMITTED;
|
|
721
|
+
this._log({ transactionId, event: 'COMMITTED', committedNodes });
|
|
722
|
+
return {
|
|
723
|
+
success: committedNodes.length === tx.participants.length,
|
|
724
|
+
committedNodes,
|
|
725
|
+
};
|
|
726
|
+
};
|
|
727
|
+
|
|
728
|
+
if (!hasPromise) {
|
|
729
|
+
return finalize();
|
|
730
|
+
}
|
|
731
|
+
return Promise.all(promises).then(finalize);
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
/**
|
|
735
|
+
* v1.37.0 G4:并行 commit(真正的 Promise.all 并行,所有 participant 同时 COMMIT)
|
|
736
|
+
*
|
|
737
|
+
* 与 commit() 的区别:
|
|
738
|
+
* - commit():循环中 await 每个 participant,串行执行 COMMIT
|
|
739
|
+
* - commitParallel():所有 participant 的 COMMIT 同时发起,Promise.all 等待全部完成
|
|
740
|
+
*
|
|
741
|
+
* 在 participant 跨网络分布或 COMMIT 涉及磁盘 I/O 时,
|
|
742
|
+
* 并行化可显著减少 commit 阶段延迟(尤其是多分片场景)。
|
|
743
|
+
*
|
|
744
|
+
* @param {string} transactionId
|
|
745
|
+
* @returns {Promise<{ success: boolean, committedNodes: string[], failedNodes: Array<{node: string, error: string}> }>}
|
|
746
|
+
*/
|
|
747
|
+
async commitParallel(transactionId) {
|
|
748
|
+
const tx = this._activeTransactions.get(transactionId);
|
|
749
|
+
if (!tx) {
|
|
750
|
+
throw new Error(`事务 "${transactionId}" 不存在`);
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
tx.status = TX_STATUS.COMMITTING;
|
|
754
|
+
this._log({ transactionId, event: 'COMMITTING' });
|
|
755
|
+
|
|
756
|
+
const commitPromises = tx.participants.map(async (participant) => {
|
|
757
|
+
try {
|
|
758
|
+
const executor = this._getExecutor(participant.node);
|
|
759
|
+
const db = this.shardManager.shards[participant.node];
|
|
760
|
+
if (db && db.inTransaction) {
|
|
761
|
+
await executor.executeSQL('COMMIT');
|
|
762
|
+
}
|
|
763
|
+
participant.status = PARTICIPANT_STATUS.COMMITTED;
|
|
764
|
+
return { node: participant.node, success: true };
|
|
765
|
+
} catch (err) {
|
|
766
|
+
this._log({
|
|
767
|
+
transactionId, event: 'COMMIT_NODE_FAILED',
|
|
768
|
+
node: participant.node, error: err.message,
|
|
769
|
+
});
|
|
770
|
+
return { node: participant.node, success: false, error: err.message };
|
|
771
|
+
}
|
|
772
|
+
});
|
|
773
|
+
|
|
774
|
+
const results = await Promise.all(commitPromises);
|
|
775
|
+
const committedNodes = results.filter(r => r.success).map(r => r.node);
|
|
776
|
+
const failedNodes = results.filter(r => !r.success).map(r => ({ node: r.node, error: r.error }));
|
|
777
|
+
|
|
778
|
+
tx.status = TX_STATUS.COMMITTED;
|
|
779
|
+
this._log({ transactionId, event: 'COMMITTED', committedNodes });
|
|
780
|
+
|
|
781
|
+
return {
|
|
782
|
+
success: committedNodes.length === tx.participants.length,
|
|
783
|
+
committedNodes,
|
|
784
|
+
failedNodes,
|
|
785
|
+
};
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
/**
|
|
789
|
+
* 回滚阶段:所有参与者回滚事务
|
|
790
|
+
* @param {string} transactionId
|
|
791
|
+
* @returns {{ success: boolean, rolledBackNodes: string[] }|Promise<{ success: boolean, rolledBackNodes: string[] }>}
|
|
792
|
+
*/
|
|
793
|
+
rollback(transactionId) {
|
|
794
|
+
const tx = this._activeTransactions.get(transactionId);
|
|
795
|
+
if (!tx) {
|
|
796
|
+
throw new Error(`事务 "${transactionId}" 不存在`);
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
tx.status = TX_STATUS.ABORTING;
|
|
800
|
+
this._log({ transactionId, event: 'ABORTING' });
|
|
801
|
+
|
|
802
|
+
const rolledBackNodes = [];
|
|
803
|
+
const promises = [];
|
|
804
|
+
let hasPromise = false;
|
|
805
|
+
|
|
806
|
+
for (const participant of tx.participants) {
|
|
807
|
+
// v2.12 E16 M3: 节点回滚成功才计入 rolledBackNodes(与 commit 同款异步 rejection 修复)
|
|
808
|
+
const finalizeParticipant = () => {
|
|
809
|
+
participant.status = PARTICIPANT_STATUS.ABORTED;
|
|
810
|
+
rolledBackNodes.push(participant.node);
|
|
811
|
+
};
|
|
812
|
+
const handleError = (err) => {
|
|
813
|
+
this._log({
|
|
814
|
+
transactionId,
|
|
815
|
+
event: 'ROLLBACK_NODE_FAILED',
|
|
816
|
+
node: participant.node,
|
|
817
|
+
error: err.message,
|
|
818
|
+
});
|
|
819
|
+
};
|
|
820
|
+
|
|
821
|
+
try {
|
|
822
|
+
const executor = this._getExecutor(participant.node);
|
|
823
|
+
const db = this.shardManager.shards[participant.node];
|
|
824
|
+
|
|
825
|
+
// 如果该分片尚在事务中,执行 ROLLBACK
|
|
826
|
+
if (db.inTransaction) {
|
|
827
|
+
const result = executor.executeSQL('ROLLBACK');
|
|
828
|
+
if (result instanceof Promise) {
|
|
829
|
+
hasPromise = true;
|
|
830
|
+
promises.push(result.then(finalizeParticipant).catch(handleError));
|
|
831
|
+
continue;
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
finalizeParticipant();
|
|
835
|
+
} catch (err) {
|
|
836
|
+
handleError(err);
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
const finalize = () => {
|
|
841
|
+
tx.status = TX_STATUS.ABORTED;
|
|
842
|
+
this._log({ transactionId, event: 'ABORTED', rolledBackNodes });
|
|
843
|
+
return {
|
|
844
|
+
success: rolledBackNodes.length === tx.participants.length,
|
|
845
|
+
rolledBackNodes,
|
|
846
|
+
};
|
|
847
|
+
};
|
|
848
|
+
|
|
849
|
+
if (!hasPromise) {
|
|
850
|
+
return finalize();
|
|
851
|
+
}
|
|
852
|
+
return Promise.all(promises).then(finalize);
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
/**
|
|
856
|
+
* 完整的两阶段提交流程
|
|
857
|
+
*
|
|
858
|
+
* @param {Array<{node: string, sql: string, params?: Array}>} sqls
|
|
859
|
+
* @returns {{ success: boolean, results: Object[], error: string|null }|Promise<{ success: boolean, results: Object[], error: string|null }>}
|
|
860
|
+
*/
|
|
861
|
+
executeTransaction(sqls) {
|
|
862
|
+
if (!Array.isArray(sqls) || sqls.length === 0) {
|
|
863
|
+
return { success: false, results: [], error: '没有要执行的 SQL' };
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
const lockName = `2pc_transaction_${Date.now()}`;
|
|
867
|
+
const lockAcquired = this._lockManager.acquire(lockName, { timeout: 10000, ttl: 60000 });
|
|
868
|
+
|
|
869
|
+
const runTransaction = (lockAcquired) => {
|
|
870
|
+
if (!lockAcquired) {
|
|
871
|
+
return { success: false, results: [], error: '无法获取分布式锁,事务超时' };
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
try {
|
|
875
|
+
const beginResult = this.begin();
|
|
876
|
+
const doRest = (transactionId) => {
|
|
877
|
+
const results = [];
|
|
878
|
+
|
|
879
|
+
// 阶段 1:执行所有 SQL
|
|
880
|
+
for (const { node, sql, params } of sqls) {
|
|
881
|
+
try {
|
|
882
|
+
const result = this.executeOnShard(transactionId, node, sql, params || []);
|
|
883
|
+
results.push({ node, sql, result, success: true });
|
|
884
|
+
} catch (err) {
|
|
885
|
+
results.push({ node, sql, error: err.message, success: false });
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
// 检查是否有执行失败的
|
|
890
|
+
const hasErrors = results.some(r => !r.success);
|
|
891
|
+
|
|
892
|
+
if (hasErrors) {
|
|
893
|
+
// 有错误 → 回滚
|
|
894
|
+
this.rollback(transactionId);
|
|
895
|
+
const errorMsg = results
|
|
896
|
+
.filter(r => !r.success)
|
|
897
|
+
.map(r => `[${r.node}] ${r.error}`)
|
|
898
|
+
.join('; ');
|
|
899
|
+
return { success: false, results, error: errorMsg };
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
// 阶段 2:准备提交
|
|
903
|
+
const prepareResult = this.prepare(transactionId);
|
|
904
|
+
if (!prepareResult.success) {
|
|
905
|
+
// 准备阶段失败 → 回滚
|
|
906
|
+
this.rollback(transactionId);
|
|
907
|
+
const errorMsg = prepareResult.failedNodes
|
|
908
|
+
.map(f => `[${f.node}] ${f.error}`)
|
|
909
|
+
.join('; ');
|
|
910
|
+
return { success: false, results, error: `准备阶段失败: ${errorMsg}` };
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
// 阶段 3:提交
|
|
914
|
+
const commitResult = this.commit(transactionId);
|
|
915
|
+
if (commitResult instanceof Promise) {
|
|
916
|
+
return commitResult.then(cr => {
|
|
917
|
+
if (!cr.success) {
|
|
918
|
+
return {
|
|
919
|
+
success: false,
|
|
920
|
+
results,
|
|
921
|
+
error: `部分节点提交失败: 已提交 ${cr.committedNodes.length}/${results.length}`,
|
|
922
|
+
};
|
|
923
|
+
}
|
|
924
|
+
return { success: true, results, error: null };
|
|
925
|
+
});
|
|
926
|
+
}
|
|
927
|
+
if (!commitResult.success) {
|
|
928
|
+
return {
|
|
929
|
+
success: false,
|
|
930
|
+
results,
|
|
931
|
+
error: `部分节点提交失败: 已提交 ${commitResult.committedNodes.length}/${results.length}`,
|
|
932
|
+
};
|
|
933
|
+
}
|
|
934
|
+
return { success: true, results, error: null };
|
|
935
|
+
};
|
|
936
|
+
|
|
937
|
+
if (beginResult instanceof Promise) {
|
|
938
|
+
return beginResult.then(doRest);
|
|
939
|
+
}
|
|
940
|
+
return doRest(beginResult);
|
|
941
|
+
} finally {
|
|
942
|
+
this._lockManager.release(lockName);
|
|
943
|
+
}
|
|
944
|
+
};
|
|
945
|
+
|
|
946
|
+
if (lockAcquired instanceof Promise) {
|
|
947
|
+
return lockAcquired.then(runTransaction);
|
|
948
|
+
}
|
|
949
|
+
return runTransaction(lockAcquired);
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
/**
|
|
953
|
+
* v1.37.0 G4:并行执行单个事务的完整 2PC 流程
|
|
954
|
+
*
|
|
955
|
+
* 与 executeTransaction() 的区别:
|
|
956
|
+
* - executeTransaction():串行 executeOnShard + 串行 prepare + 串行 commit
|
|
957
|
+
* - executeTransactionParallel():并行 executeOnShard + 并行 prepare + 并行 commit
|
|
958
|
+
*
|
|
959
|
+
* 适合每个分片操作互相独立的场景(无跨分片依赖)。
|
|
960
|
+
*
|
|
961
|
+
* @param {Array<{node: string, sql: string, params?: Array}>} sqls
|
|
962
|
+
* @returns {Promise<{ success: boolean, results: Object[], error: string|null }>}
|
|
963
|
+
*/
|
|
964
|
+
async executeTransactionParallel(sqls) {
|
|
965
|
+
if (!Array.isArray(sqls) || sqls.length === 0) {
|
|
966
|
+
return { success: false, results: [], error: '没有要执行的 SQL' };
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
// 按节点分组 SQL(同节点的 SQL 仍需串行执行,避免事务状态混乱)
|
|
970
|
+
const sqlsByNode = new Map();
|
|
971
|
+
for (const { node, sql, params } of sqls) {
|
|
972
|
+
if (!sqlsByNode.has(node)) {
|
|
973
|
+
sqlsByNode.set(node, []);
|
|
974
|
+
}
|
|
975
|
+
sqlsByNode.get(node).push({ node, sql, params: params || [] });
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
const beginResult = await this.begin();
|
|
979
|
+
// 兼容父类(返回字符串)与子类(返回 {transactionId, participants})两种格式
|
|
980
|
+
const transactionId = typeof beginResult === 'string' ? beginResult : beginResult.transactionId;
|
|
981
|
+
|
|
982
|
+
// 阶段 1:并行执行各节点的 SQL(同节点内串行)
|
|
983
|
+
const nodeResults = await Promise.all(
|
|
984
|
+
Array.from(sqlsByNode.entries()).map(async ([nodeName, nodeSqls]) => {
|
|
985
|
+
const nodeResult = [];
|
|
986
|
+
for (const { sql, params } of nodeSqls) {
|
|
987
|
+
try {
|
|
988
|
+
const result = await this.executeOnShard(transactionId, nodeName, sql, params);
|
|
989
|
+
nodeResult.push({ node: nodeName, sql, result, success: true });
|
|
990
|
+
} catch (err) {
|
|
991
|
+
nodeResult.push({ node: nodeName, sql, error: err.message, success: false });
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
return nodeResult;
|
|
995
|
+
}),
|
|
996
|
+
);
|
|
997
|
+
const results = nodeResults.flat();
|
|
998
|
+
|
|
999
|
+
// 检查执行失败的
|
|
1000
|
+
const hasErrors = results.some(r => !r.success);
|
|
1001
|
+
if (hasErrors) {
|
|
1002
|
+
await this.rollback(transactionId);
|
|
1003
|
+
const errorMsg = results
|
|
1004
|
+
.filter(r => !r.success)
|
|
1005
|
+
.map(r => `[${r.node}] ${r.error}`)
|
|
1006
|
+
.join('; ');
|
|
1007
|
+
return { success: false, results, error: errorMsg };
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
// 阶段 2:并行 prepare
|
|
1011
|
+
const prepareResult = await this.prepareParallel(transactionId);
|
|
1012
|
+
if (!prepareResult.success) {
|
|
1013
|
+
await this.rollback(transactionId);
|
|
1014
|
+
const errorMsg = prepareResult.failedNodes
|
|
1015
|
+
.map(f => `[${f.node}] ${f.error}`)
|
|
1016
|
+
.join('; ');
|
|
1017
|
+
return { success: false, results, error: `准备阶段失败: ${errorMsg}` };
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
// 阶段 3:并行 commit
|
|
1021
|
+
const commitResult = await this.commitParallel(transactionId);
|
|
1022
|
+
if (!commitResult.success) {
|
|
1023
|
+
return {
|
|
1024
|
+
success: false,
|
|
1025
|
+
results,
|
|
1026
|
+
error: `部分节点提交失败: 已提交 ${commitResult.committedNodes.length}/${results.length}`,
|
|
1027
|
+
};
|
|
1028
|
+
}
|
|
1029
|
+
return { success: true, results, error: null };
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
/**
|
|
1033
|
+
* v1.37.0 G4:批量执行多个独立事务(减少协调者往返)
|
|
1034
|
+
*
|
|
1035
|
+
* 与 executeTransaction() 的区别:
|
|
1036
|
+
* - executeTransaction():单个事务的 2PC 流程
|
|
1037
|
+
* - executeBatchTransactions():多个独立事务串行执行,每个事务内部并行 2PC
|
|
1038
|
+
*
|
|
1039
|
+
* 适合高吞吐场景:批量写入、ETL、数据迁移等。
|
|
1040
|
+
*
|
|
1041
|
+
* 注意:多个事务之间是串行的(每个事务独立 begin+commit),
|
|
1042
|
+
* 因为分片 db 实例同一时刻只能有一个活跃事务。
|
|
1043
|
+
* 但每个事务内部使用 executeTransactionParallel(并行 prepare + 并行 commit),
|
|
1044
|
+
* 因此相对串行 executeTransaction 仍有性能提升。
|
|
1045
|
+
*
|
|
1046
|
+
* @param {Array<Array<{node: string, sql: string, params?: Array}>>} transactions - 多个事务的 SQL 列表
|
|
1047
|
+
* @returns {Promise<Array<{ success: boolean, results: Object[], error: string|null }>>}
|
|
1048
|
+
*/
|
|
1049
|
+
async executeBatchTransactions(transactions) {
|
|
1050
|
+
if (!Array.isArray(transactions) || transactions.length === 0) {
|
|
1051
|
+
return [];
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
// 串行执行每个事务(每个事务内部并行 2PC)
|
|
1055
|
+
const results = [];
|
|
1056
|
+
for (const sqls of transactions) {
|
|
1057
|
+
const result = await this.executeTransactionParallel(sqls);
|
|
1058
|
+
results.push(result);
|
|
1059
|
+
}
|
|
1060
|
+
return results;
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
/**
|
|
1064
|
+
* v1.37.0 G4:刷新所有待写入的日志到磁盘(用于优雅关闭)
|
|
1065
|
+
*/
|
|
1066
|
+
flushLog() {
|
|
1067
|
+
this._flushLogNow();
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
/**
|
|
1071
|
+
* 获取事务状态
|
|
1072
|
+
* @param {string} transactionId
|
|
1073
|
+
* @returns {Object|null}
|
|
1074
|
+
*/
|
|
1075
|
+
getStatus(transactionId) {
|
|
1076
|
+
const tx = this._activeTransactions.get(transactionId);
|
|
1077
|
+
if (!tx) {
|
|
1078
|
+
return null;
|
|
1079
|
+
}
|
|
1080
|
+
return {
|
|
1081
|
+
transactionId: tx.transactionId,
|
|
1082
|
+
status: tx.status,
|
|
1083
|
+
participants: tx.participants.map(p => ({ ...p })),
|
|
1084
|
+
beginTime: tx.beginTime,
|
|
1085
|
+
operationsCount: tx.operations.length,
|
|
1086
|
+
elapsed: Date.now() - tx.beginTime,
|
|
1087
|
+
};
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
/**
|
|
1091
|
+
* 恢复(处理协调者故障后的恢复流程)
|
|
1092
|
+
* 查找 PREPARED 状态的事务,根据日志决定提交或回滚
|
|
1093
|
+
* @param {string} transactionId
|
|
1094
|
+
* @returns {Object|Promise<Object>}
|
|
1095
|
+
*/
|
|
1096
|
+
recover(transactionId) {
|
|
1097
|
+
const tx = this._activeTransactions.get(transactionId);
|
|
1098
|
+
if (!tx) {
|
|
1099
|
+
throw new Error(`事务 "${transactionId}" 不存在,无法恢复`);
|
|
1100
|
+
}
|
|
1101
|
+
|
|
1102
|
+
this._log({ transactionId, event: 'RECOVER' });
|
|
1103
|
+
|
|
1104
|
+
switch (tx.status) {
|
|
1105
|
+
case TX_STATUS.PREPARED:
|
|
1106
|
+
// 所有参与者已 PREPARED,可以安全提交
|
|
1107
|
+
console.warn(`[2PC 恢复] 事务 "${transactionId}" 处于 PREPARED 状态,执行提交`);
|
|
1108
|
+
return this.commit(transactionId);
|
|
1109
|
+
|
|
1110
|
+
case TX_STATUS.PREPARING:
|
|
1111
|
+
case TX_STATUS.ACTIVE:
|
|
1112
|
+
// 尚未完成 prepare,回滚
|
|
1113
|
+
console.warn(`[2PC 恢复] 事务 "${transactionId}" 处于 ${tx.status} 状态,执行回滚`);
|
|
1114
|
+
return this.rollback(transactionId);
|
|
1115
|
+
|
|
1116
|
+
case TX_STATUS.COMMITTING:
|
|
1117
|
+
// 提交中,重试提交
|
|
1118
|
+
console.warn(`[2PC 恢复] 事务 "${transactionId}" 处于 COMMITTING 状态,重试提交`);
|
|
1119
|
+
return this.commit(transactionId);
|
|
1120
|
+
|
|
1121
|
+
case TX_STATUS.ABORTING:
|
|
1122
|
+
// 回滚中,重试回滚
|
|
1123
|
+
console.warn(`[2PC 恢复] 事务 "${transactionId}" 处于 ABORTING 状态,重试回滚`);
|
|
1124
|
+
return this.rollback(transactionId);
|
|
1125
|
+
|
|
1126
|
+
case TX_STATUS.COMMITTED:
|
|
1127
|
+
case TX_STATUS.ABORTED:
|
|
1128
|
+
// 已完成,无需操作
|
|
1129
|
+
return {
|
|
1130
|
+
success: true,
|
|
1131
|
+
message: `事务 "${transactionId}" 已 ${tx.status === TX_STATUS.COMMITTED ? '提交' : '回滚'}`,
|
|
1132
|
+
};
|
|
1133
|
+
|
|
1134
|
+
default:
|
|
1135
|
+
throw new Error(`未知的事务状态: ${tx.status}`);
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
/**
|
|
1140
|
+
* 启动时自动扫描并恢复悬挂事务
|
|
1141
|
+
*
|
|
1142
|
+
* 扫描 _transactionLog 中所有 PREPARED 状态的事务,
|
|
1143
|
+
* 检查这些事务是否已完成(有 COMMITTED 或 ABORTED 日志),
|
|
1144
|
+
* 对未完成的悬挂事务自动执行 recover()。
|
|
1145
|
+
*
|
|
1146
|
+
* v1.28.1+ 修复:原实现在协调者崩溃后 _activeTransactions 为空,
|
|
1147
|
+
* recover() 会因找不到事务记录抛出"事务不存在"异常。
|
|
1148
|
+
* 现使用 _reconstructTxFromLog 从持久化日志重建事务记录后再调用 recover()。
|
|
1149
|
+
*
|
|
1150
|
+
* @returns {Promise<{ recovered: number, committed: number, aborted: number }>}
|
|
1151
|
+
*/
|
|
1152
|
+
recoverAfterCrash() {
|
|
1153
|
+
if (this._transactionLog.length === 0) {
|
|
1154
|
+
return { recovered: 0, committed: 0, aborted: 0 };
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
let recovered = 0;
|
|
1158
|
+
let committed = 0;
|
|
1159
|
+
let aborted = 0;
|
|
1160
|
+
|
|
1161
|
+
// 收集所有 PREPARED 事件,找出未完成的事务
|
|
1162
|
+
const preparedTxIds = new Set();
|
|
1163
|
+
for (const entry of this._transactionLog) {
|
|
1164
|
+
if (entry.transactionId && entry.event === 'PREPARED') {
|
|
1165
|
+
// 检查此事务是否有最终状态(COMMITTED 或 ABORTED)
|
|
1166
|
+
const hasFinalState = this._transactionLog.some(
|
|
1167
|
+
e => e.transactionId === entry.transactionId &&
|
|
1168
|
+
(e.event === 'COMMITTED' || e.event === 'ABORTED'),
|
|
1169
|
+
);
|
|
1170
|
+
if (!hasFinalState) {
|
|
1171
|
+
preparedTxIds.add(entry.transactionId);
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
const promises = [];
|
|
1177
|
+
let hasPromise = false;
|
|
1178
|
+
|
|
1179
|
+
for (const txId of preparedTxIds) {
|
|
1180
|
+
try {
|
|
1181
|
+
// v1.28.1+: 崩溃后 _activeTransactions 为空,需从日志重建事务记录
|
|
1182
|
+
if (!this._activeTransactions.has(txId)) {
|
|
1183
|
+
const reconstructed = this._reconstructTxFromLog(txId);
|
|
1184
|
+
if (reconstructed) {
|
|
1185
|
+
this._activeTransactions.set(txId, reconstructed);
|
|
1186
|
+
} else {
|
|
1187
|
+
throw new Error(`无法从事务日志重建事务记录: ${txId}`);
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
const result = this.recover(txId);
|
|
1191
|
+
if (result instanceof Promise) {
|
|
1192
|
+
hasPromise = true;
|
|
1193
|
+
promises.push(result.then(r => {
|
|
1194
|
+
if (r && r.success) {
|
|
1195
|
+
committed++;
|
|
1196
|
+
} else {
|
|
1197
|
+
aborted++;
|
|
1198
|
+
}
|
|
1199
|
+
recovered++;
|
|
1200
|
+
}).catch(err => {
|
|
1201
|
+
// P1-C 修复(v1.34.0):原 .catch(() => {}) 静默吞错,恢复失败时无日志。
|
|
1202
|
+
console.error(`[2PC] 恢复 commit 失败: ${err.message}`);
|
|
1203
|
+
aborted++;
|
|
1204
|
+
recovered++;
|
|
1205
|
+
}));
|
|
1206
|
+
} else {
|
|
1207
|
+
if (result && result.success) {
|
|
1208
|
+
committed++;
|
|
1209
|
+
} else {
|
|
1210
|
+
aborted++;
|
|
1211
|
+
}
|
|
1212
|
+
recovered++;
|
|
1213
|
+
}
|
|
1214
|
+
} catch (err) {
|
|
1215
|
+
aborted++;
|
|
1216
|
+
recovered++;
|
|
1217
|
+
this._log({ event: 'RECOVERY_FAILED', transactionId: txId, error: err.message });
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
const finalize = () => {
|
|
1222
|
+
this._log({ event: 'RECOVERY_COMPLETE', recovered, committed, aborted });
|
|
1223
|
+
return { recovered, committed, aborted };
|
|
1224
|
+
};
|
|
1225
|
+
|
|
1226
|
+
if (!hasPromise) {
|
|
1227
|
+
return finalize();
|
|
1228
|
+
}
|
|
1229
|
+
return Promise.all(promises).then(finalize);
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
/**
|
|
1233
|
+
* 获取事务日志
|
|
1234
|
+
* @param {string} [transactionId] - 可选,过滤指定事务
|
|
1235
|
+
* @returns {Array<Object>}
|
|
1236
|
+
*/
|
|
1237
|
+
getTransactionLog(transactionId) {
|
|
1238
|
+
if (transactionId) {
|
|
1239
|
+
return this._transactionLog.filter(entry => entry.transactionId === transactionId);
|
|
1240
|
+
}
|
|
1241
|
+
return [...this._transactionLog];
|
|
1242
|
+
}
|
|
1243
|
+
}
|