rubydb 0.1.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.
- checksums.yaml +7 -0
- data/.gitattributes +20 -0
- data/.github/ISSUE_TEMPLATE/bug.yml +0 -0
- data/.github/ISSUE_TEMPLATE/feature.yml +0 -0
- data/.github/ISSUE_TEMPLATE/performance.yml +0 -0
- data/.github/ISSUE_TEMPLATE/security.yml +0 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +15 -0
- data/.github/dependabot.yml +0 -0
- data/.github/workflows/benchmark.yml +26 -0
- data/.github/workflows/compatibility.yml +63 -0
- data/.github/workflows/fuzz.yml +33 -0
- data/.github/workflows/lint.yml +21 -0
- data/.github/workflows/operations.yml +24 -0
- data/.github/workflows/production-validation.yml +111 -0
- data/.github/workflows/release.yml +77 -0
- data/.github/workflows/security.yml +37 -0
- data/.github/workflows/test.yml +26 -0
- data/.github/workflows/workload.yml +58 -0
- data/.gitignore +43 -0
- data/.rspec +4 -0
- data/.rubocop.yml +44 -0
- data/.ruby-version +1 -0
- data/.standard.yml +14 -0
- data/ARCHITECTURE.md +21 -0
- data/CHANGELOG.md +31 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/CONTRIBUTING.md +29 -0
- data/GOVERNANCE.md +16 -0
- data/Gemfile +17 -0
- data/Gemfile.lock +71 -0
- data/LICENSE +60 -0
- data/README.md +162 -0
- data/ROADMAP.md +27 -0
- data/Rakefile +71 -0
- data/SECURITY.md +54 -0
- data/SUPPORT.md +14 -0
- data/adapters/activerecord/Gemfile +11 -0
- data/adapters/activerecord/README.md +9 -0
- data/adapters/activerecord/lib/active_record/connection_adapters/rubydb_adapter.rb +887 -0
- data/adapters/activerecord/rubydb-activerecord.gemspec +27 -0
- data/adapters/activerecord/spec/rubydb_adapter_integration_spec.rb +151 -0
- data/adapters/ruby/README.md +18 -0
- data/adapters/sequel/README.md +11 -0
- data/bin/rubydb +19 -0
- data/bin/rubydb-server +126 -0
- data/config/development.yml +0 -0
- data/config/monitoring/prometheus-alerts.yml +39 -0
- data/config/production.yml +36 -0
- data/config/test.yml +0 -0
- data/docs/README.md +71 -0
- data/docs/architecture/concurrency.md +14 -0
- data/docs/architecture/current-state.md +125 -0
- data/docs/architecture/execution-engine.md +25 -0
- data/docs/architecture/indexes.md +19 -0
- data/docs/architecture/mvcc.md +19 -0
- data/docs/architecture/overview.md +13 -0
- data/docs/architecture/pages.md +11 -0
- data/docs/architecture/production-roadmap.md +82 -0
- data/docs/architecture/query-planner.md +20 -0
- data/docs/architecture/recovery.md +18 -0
- data/docs/architecture/sql-engine.md +12 -0
- data/docs/architecture/storage-engine.md +14 -0
- data/docs/architecture/transactions.md +10 -0
- data/docs/architecture/wal.md +28 -0
- data/docs/cli-cheatsheet.md +98 -0
- data/docs/cli.md +275 -0
- data/docs/contributing/architecture.md +9 -0
- data/docs/contributing/benchmarking.md +14 -0
- data/docs/contributing/development.md +16 -0
- data/docs/contributing/release-process.md +49 -0
- data/docs/contributing/testing.md +16 -0
- data/docs/debugging.md +229 -0
- data/docs/developer/branching.md +10 -0
- data/docs/developer/database-diff.md +10 -0
- data/docs/developer/local-development.md +17 -0
- data/docs/developer/snapshots.md +9 -0
- data/docs/developer/temporal-data.md +10 -0
- data/docs/developer-guide.md +297 -0
- data/docs/getting-started/first-database.md +16 -0
- data/docs/getting-started/first-query.md +13 -0
- data/docs/getting-started/installation.md +19 -0
- data/docs/getting-started/local-to-production.md +300 -0
- data/docs/getting-started/quickstart.md +17 -0
- data/docs/getting-started/rails.md +16 -0
- data/docs/hardening_backlog.md +93 -0
- data/docs/lessons-learned.md +112 -0
- data/docs/operations/backups.md +33 -0
- data/docs/operations/disaster-recovery.md +31 -0
- data/docs/operations/failover.md +30 -0
- data/docs/operations/monitoring.md +25 -0
- data/docs/operations/production-guide.md +295 -0
- data/docs/operations/production-runbook.md +45 -0
- data/docs/operations/replication.md +33 -0
- data/docs/operations/restore.md +6 -0
- data/docs/operations/runbook.md +34 -0
- data/docs/operations/upgrades.md +14 -0
- data/docs/operations/workload-testing.md +17 -0
- data/docs/production-readiness.md +118 -0
- data/docs/production_validation.md +150 -0
- data/docs/rails/active-record.md +11 -0
- data/docs/rails/compatibility-guide.md +90 -0
- data/docs/rails/database-yml.md +92 -0
- data/docs/rails/installation.md +17 -0
- data/docs/rails/migrations.md +17 -0
- data/docs/rails/production.md +82 -0
- data/docs/rails/troubleshooting.md +18 -0
- data/docs/release.md +59 -0
- data/docs/server/architecture.md +10 -0
- data/docs/server/authentication.md +10 -0
- data/docs/server/configuration.md +16 -0
- data/docs/server/connection-pooling.md +10 -0
- data/docs/server/deployment.md +10 -0
- data/docs/server/protocol.md +12 -0
- data/docs/sql/compatibility-guide.md +82 -0
- data/docs/sql/compatibility.md +39 -0
- data/docs/sql/data-types.md +10 -0
- data/docs/sql/functions.md +9 -0
- data/docs/sql/joins.md +9 -0
- data/docs/sql/operators.md +9 -0
- data/docs/sql/sqlite-compatibility.md +21 -0
- data/docs/sql/syntax.md +10 -0
- data/docs/sql/transactions.md +10 -0
- data/docs/troubleshooting.md +244 -0
- data/exe/rubydb +23 -0
- data/lib/rubydb/backup/archive.rb +334 -0
- data/lib/rubydb/backup/backup.rb +401 -0
- data/lib/rubydb/backup/incremental.rb +353 -0
- data/lib/rubydb/backup/restore.rb +290 -0
- data/lib/rubydb/backup/snapshot.rb +267 -0
- data/lib/rubydb/backup/verification.rb +279 -0
- data/lib/rubydb/branching/branch.rb +181 -0
- data/lib/rubydb/branching/branch_manager.rb +311 -0
- data/lib/rubydb/branching/branch_metadata.rb +140 -0
- data/lib/rubydb/branching/checkout.rb +166 -0
- data/lib/rubydb/branching/copy_on_write.rb +272 -0
- data/lib/rubydb/branching/diff.rb +138 -0
- data/lib/rubydb/branching/merge.rb +285 -0
- data/lib/rubydb/build_info.rb +15 -0
- data/lib/rubydb/catalog/catalog.rb +391 -0
- data/lib/rubydb/catalog/column.rb +112 -0
- data/lib/rubydb/catalog/constraint.rb +180 -0
- data/lib/rubydb/catalog/database.rb +184 -0
- data/lib/rubydb/catalog/index.rb +97 -0
- data/lib/rubydb/catalog/schema.rb +103 -0
- data/lib/rubydb/catalog/sequence.rb +90 -0
- data/lib/rubydb/catalog/system_catalog.rb +698 -0
- data/lib/rubydb/catalog/table.rb +178 -0
- data/lib/rubydb/catalog/trigger.rb +102 -0
- data/lib/rubydb/catalog/view.rb +66 -0
- data/lib/rubydb/cli/application.rb +163 -0
- data/lib/rubydb/cli/commands/backup.rb +81 -0
- data/lib/rubydb/cli/commands/branch.rb +72 -0
- data/lib/rubydb/cli/commands/checkout.rb +54 -0
- data/lib/rubydb/cli/commands/create.rb +58 -0
- data/lib/rubydb/cli/commands/diff.rb +77 -0
- data/lib/rubydb/cli/commands/doctor.rb +74 -0
- data/lib/rubydb/cli/commands/drop.rb +57 -0
- data/lib/rubydb/cli/commands/init.rb +102 -0
- data/lib/rubydb/cli/commands/inspect.rb +95 -0
- data/lib/rubydb/cli/commands/merge.rb +63 -0
- data/lib/rubydb/cli/commands/migrate.rb +62 -0
- data/lib/rubydb/cli/commands/restart.rb +39 -0
- data/lib/rubydb/cli/commands/restore.rb +121 -0
- data/lib/rubydb/cli/commands/shell.rb +365 -0
- data/lib/rubydb/cli/commands/snapshot.rb +79 -0
- data/lib/rubydb/cli/commands/start.rb +82 -0
- data/lib/rubydb/cli/commands/status.rb +92 -0
- data/lib/rubydb/cli/commands/stop.rb +47 -0
- data/lib/rubydb/cli/commands/vacuum.rb +58 -0
- data/lib/rubydb/cli/formatter.rb +221 -0
- data/lib/rubydb/cli/output.rb +168 -0
- data/lib/rubydb/client/client.rb +304 -0
- data/lib/rubydb/client/connection.rb +415 -0
- data/lib/rubydb/client/connection_pool.rb +168 -0
- data/lib/rubydb/client/connection_url.rb +96 -0
- data/lib/rubydb/client/prepared_statement.rb +60 -0
- data/lib/rubydb/client/result.rb +123 -0
- data/lib/rubydb/client/statement.rb +52 -0
- data/lib/rubydb/client/transaction.rb +130 -0
- data/lib/rubydb/concurrency/concurrency.rb +19 -0
- data/lib/rubydb/concurrency/deadlock_detector.rb +150 -0
- data/lib/rubydb/concurrency/latch.rb +101 -0
- data/lib/rubydb/concurrency/lock_graph.rb +165 -0
- data/lib/rubydb/concurrency/mutex.rb +183 -0
- data/lib/rubydb/concurrency/rw_lock.rb +180 -0
- data/lib/rubydb/concurrency/scheduler.rb +250 -0
- data/lib/rubydb/concurrency/worker_pool.rb +143 -0
- data/lib/rubydb/configuration/config.rb +170 -0
- data/lib/rubydb/configuration/defaults.rb +179 -0
- data/lib/rubydb/configuration/environment.rb +152 -0
- data/lib/rubydb/configuration/parser.rb +185 -0
- data/lib/rubydb/configuration/validation.rb +221 -0
- data/lib/rubydb/constants.rb +74 -0
- data/lib/rubydb/constraints/check.rb +181 -0
- data/lib/rubydb/constraints/constraint.rb +101 -0
- data/lib/rubydb/constraints/foreign_key.rb +130 -0
- data/lib/rubydb/constraints/not_null.rb +64 -0
- data/lib/rubydb/constraints/primary_key.rb +99 -0
- data/lib/rubydb/constraints/unique.rb +108 -0
- data/lib/rubydb/constraints/validator.rb +350 -0
- data/lib/rubydb/errors/authentication_error.rb +10 -0
- data/lib/rubydb/errors/authorization_error.rb +23 -0
- data/lib/rubydb/errors/client_error.rb +10 -0
- data/lib/rubydb/errors/configuration_error.rb +10 -0
- data/lib/rubydb/errors/connection_error.rb +10 -0
- data/lib/rubydb/errors/constraint_error.rb +23 -0
- data/lib/rubydb/errors/corruption_error.rb +10 -0
- data/lib/rubydb/errors/database_error.rb +10 -0
- data/lib/rubydb/errors/error.rb +20 -0
- data/lib/rubydb/errors/execution_error.rb +10 -0
- data/lib/rubydb/errors/parser_error.rb +10 -0
- data/lib/rubydb/errors/recovery_error.rb +10 -0
- data/lib/rubydb/errors/replication_error.rb +10 -0
- data/lib/rubydb/errors/server_error.rb +6 -0
- data/lib/rubydb/errors/storage_error.rb +10 -0
- data/lib/rubydb/errors/transaction_error.rb +10 -0
- data/lib/rubydb/execution/aggregate_executor.rb +138 -0
- data/lib/rubydb/execution/delete_executor.rb +112 -0
- data/lib/rubydb/execution/distinct_executor.rb +135 -0
- data/lib/rubydb/execution/executor.rb +1192 -0
- data/lib/rubydb/execution/expression.rb +193 -0
- data/lib/rubydb/execution/index_scan.rb +142 -0
- data/lib/rubydb/execution/insert_executor.rb +217 -0
- data/lib/rubydb/execution/join_executor.rb +249 -0
- data/lib/rubydb/execution/limit_executor.rb +85 -0
- data/lib/rubydb/execution/optimizer.rb +215 -0
- data/lib/rubydb/execution/plan.rb +361 -0
- data/lib/rubydb/execution/planner.rb +546 -0
- data/lib/rubydb/execution/predicate.rb +235 -0
- data/lib/rubydb/execution/scan.rb +49 -0
- data/lib/rubydb/execution/sequential_scan.rb +63 -0
- data/lib/rubydb/execution/sort_executor.rb +185 -0
- data/lib/rubydb/execution/update_executor.rb +162 -0
- data/lib/rubydb/functions/aggregate.rb +70 -0
- data/lib/rubydb/functions/date_functions.rb +278 -0
- data/lib/rubydb/functions/function.rb +85 -0
- data/lib/rubydb/functions/json_functions.rb +215 -0
- data/lib/rubydb/functions/numeric_functions.rb +346 -0
- data/lib/rubydb/functions/scalar.rb +52 -0
- data/lib/rubydb/functions/string_functions.rb +383 -0
- data/lib/rubydb/functions/system_functions.rb +246 -0
- data/lib/rubydb/history/as_of.rb +238 -0
- data/lib/rubydb/history/change.rb +105 -0
- data/lib/rubydb/history/history.rb +131 -0
- data/lib/rubydb/history/history_manager.rb +229 -0
- data/lib/rubydb/history/temporal_query.rb +202 -0
- data/lib/rubydb/history/timeline.rb +144 -0
- data/lib/rubydb/indexes/btree.rb +186 -0
- data/lib/rubydb/indexes/btree_cursor.rb +258 -0
- data/lib/rubydb/indexes/btree_node.rb +385 -0
- data/lib/rubydb/indexes/hash_index.rb +150 -0
- data/lib/rubydb/indexes/index.rb +71 -0
- data/lib/rubydb/indexes/index_manager.rb +406 -0
- data/lib/rubydb/indexes/index_scan.rb +470 -0
- data/lib/rubydb/migrations/migration.rb +254 -0
- data/lib/rubydb/migrations/migration_lock.rb +146 -0
- data/lib/rubydb/migrations/migration_manager.rb +176 -0
- data/lib/rubydb/migrations/migration_version.rb +71 -0
- data/lib/rubydb/migrations/schema_diff.rb +211 -0
- data/lib/rubydb/migrations/schema_version.rb +64 -0
- data/lib/rubydb/monitoring/events.rb +160 -0
- data/lib/rubydb/monitoring/health.rb +222 -0
- data/lib/rubydb/monitoring/logger.rb +193 -0
- data/lib/rubydb/monitoring/metrics.rb +359 -0
- data/lib/rubydb/monitoring/performance.rb +176 -0
- data/lib/rubydb/monitoring/statistics.rb +170 -0
- data/lib/rubydb/mvcc/garbage_collector.rb +199 -0
- data/lib/rubydb/mvcc/mvcc.rb +16 -0
- data/lib/rubydb/mvcc/snapshot.rb +147 -0
- data/lib/rubydb/mvcc/vacuum.rb +180 -0
- data/lib/rubydb/mvcc/version.rb +106 -0
- data/lib/rubydb/mvcc/version_store.rb +398 -0
- data/lib/rubydb/mvcc/visibility.rb +109 -0
- data/lib/rubydb/protocol/capabilities.rb +125 -0
- data/lib/rubydb/protocol/decoder.rb +145 -0
- data/lib/rubydb/protocol/encoder.rb +136 -0
- data/lib/rubydb/protocol/handshake.rb +305 -0
- data/lib/rubydb/protocol/message.rb +121 -0
- data/lib/rubydb/protocol/protocol.rb +277 -0
- data/lib/rubydb/protocol/version.rb +54 -0
- data/lib/rubydb/rails/adapter.rb +239 -0
- data/lib/rubydb/rails/connection.rb +314 -0
- data/lib/rubydb/rails/database_statements.rb +122 -0
- data/lib/rubydb/rails/migration.rb +131 -0
- data/lib/rubydb/rails/quoting.rb +109 -0
- data/lib/rubydb/rails/result.rb +120 -0
- data/lib/rubydb/rails/schema_statements.rb +339 -0
- data/lib/rubydb/rails/transaction.rb +105 -0
- data/lib/rubydb/rails/type.rb +126 -0
- data/lib/rubydb/recovery/checkpoint.rb +257 -0
- data/lib/rubydb/recovery/consistency.rb +467 -0
- data/lib/rubydb/recovery/corruption_detector.rb +5 -0
- data/lib/rubydb/recovery/crash_recovery.rb +387 -0
- data/lib/rubydb/recovery/recovery_manager.rb +206 -0
- data/lib/rubydb/recovery/redo.rb +237 -0
- data/lib/rubydb/recovery/undo.rb +206 -0
- data/lib/rubydb/replication/failover.rb +5 -0
- data/lib/rubydb/replication/fencing.rb +63 -0
- data/lib/rubydb/replication/primary.rb +450 -0
- data/lib/rubydb/replication/replica.rb +384 -0
- data/lib/rubydb/replication/replication_log.rb +200 -0
- data/lib/rubydb/replication/replication_manager.rb +308 -0
- data/lib/rubydb/replication/replication_slot.rb +295 -0
- data/lib/rubydb/replication/replication_stream.rb +201 -0
- data/lib/rubydb/rubydb.rb +560 -0
- data/lib/rubydb/security/access_control.rb +254 -0
- data/lib/rubydb/security/audit_log.rb +213 -0
- data/lib/rubydb/security/authentication.rb +302 -0
- data/lib/rubydb/security/authorization.rb +282 -0
- data/lib/rubydb/security/credentials.rb +196 -0
- data/lib/rubydb/security/password.rb +215 -0
- data/lib/rubydb/security/permissions.rb +74 -0
- data/lib/rubydb/security/role.rb +101 -0
- data/lib/rubydb/security/user.rb +86 -0
- data/lib/rubydb/server/connection.rb +366 -0
- data/lib/rubydb/server/connection_pool.rb +193 -0
- data/lib/rubydb/server/lifecycle.rb +228 -0
- data/lib/rubydb/server/listener.rb +136 -0
- data/lib/rubydb/server/request_handler.rb +276 -0
- data/lib/rubydb/server/server.rb +364 -0
- data/lib/rubydb/server/session.rb +369 -0
- data/lib/rubydb/server/worker.rb +210 -0
- data/lib/rubydb/server/worker_pool.rb +168 -0
- data/lib/rubydb/sql/ast/alter_table.rb +169 -0
- data/lib/rubydb/sql/ast/begin_transaction.rb +47 -0
- data/lib/rubydb/sql/ast/commit.rb +37 -0
- data/lib/rubydb/sql/ast/constraint.rb +83 -0
- data/lib/rubydb/sql/ast/create_database.rb +41 -0
- data/lib/rubydb/sql/ast/create_index.rb +61 -0
- data/lib/rubydb/sql/ast/create_schema.rb +52 -0
- data/lib/rubydb/sql/ast/create_table.rb +187 -0
- data/lib/rubydb/sql/ast/delete.rb +54 -0
- data/lib/rubydb/sql/ast/drop_database.rb +41 -0
- data/lib/rubydb/sql/ast/drop_index.rb +41 -0
- data/lib/rubydb/sql/ast/drop_schema.rb +49 -0
- data/lib/rubydb/sql/ast/drop_table.rb +49 -0
- data/lib/rubydb/sql/ast/explain.rb +64 -0
- data/lib/rubydb/sql/ast/expression.rb +604 -0
- data/lib/rubydb/sql/ast/insert.rb +82 -0
- data/lib/rubydb/sql/ast/node.rb +42 -0
- data/lib/rubydb/sql/ast/rollback.rb +63 -0
- data/lib/rubydb/sql/ast/savepoint.rb +59 -0
- data/lib/rubydb/sql/ast/select.rb +88 -0
- data/lib/rubydb/sql/ast/set_operation.rb +20 -0
- data/lib/rubydb/sql/ast/trigger.rb +29 -0
- data/lib/rubydb/sql/ast/update.rb +88 -0
- data/lib/rubydb/sql/ast/vacuum.rb +19 -0
- data/lib/rubydb/sql/ast/view.rb +32 -0
- data/lib/rubydb/sql/ast/with.rb +32 -0
- data/lib/rubydb/sql/grammar.rb +86 -0
- data/lib/rubydb/sql/keywords.rb +156 -0
- data/lib/rubydb/sql/lexer.rb +214 -0
- data/lib/rubydb/sql/operators.rb +100 -0
- data/lib/rubydb/sql/parser.rb +1185 -0
- data/lib/rubydb/sql/planner/analyzer.rb +302 -0
- data/lib/rubydb/sql/planner/binder.rb +550 -0
- data/lib/rubydb/sql/planner/type_checker.rb +431 -0
- data/lib/rubydb/sql/token.rb +210 -0
- data/lib/rubydb/storage/buffer_frame.rb +44 -0
- data/lib/rubydb/storage/buffer_pool.rb +155 -0
- data/lib/rubydb/storage/database_lock.rb +74 -0
- data/lib/rubydb/storage/deserializer.rb +342 -0
- data/lib/rubydb/storage/engine.rb +2338 -0
- data/lib/rubydb/storage/file_manager.rb +187 -0
- data/lib/rubydb/storage/free_space_map.rb +81 -0
- data/lib/rubydb/storage/page.rb +94 -0
- data/lib/rubydb/storage/page_allocator.rb +855 -0
- data/lib/rubydb/storage/page_header.rb +67 -0
- data/lib/rubydb/storage/page_manager.rb +131 -0
- data/lib/rubydb/storage/record.rb +58 -0
- data/lib/rubydb/storage/row.rb +78 -0
- data/lib/rubydb/storage/serializer.rb +51 -0
- data/lib/rubydb/storage/storage_layout.rb +151 -0
- data/lib/rubydb/storage/storage_manager.rb +114 -0
- data/lib/rubydb/storage/tuple.rb +461 -0
- data/lib/rubydb/storage/visibility_map.rb +984 -0
- data/lib/rubydb/transactions/commit_manager.rb +220 -0
- data/lib/rubydb/transactions/isolation.rb +98 -0
- data/lib/rubydb/transactions/lock.rb +76 -0
- data/lib/rubydb/transactions/lock_manager.rb +362 -0
- data/lib/rubydb/transactions/savepoint.rb +143 -0
- data/lib/rubydb/transactions/transaction.rb +215 -0
- data/lib/rubydb/transactions/transaction_id.rb +84 -0
- data/lib/rubydb/transactions/transaction_log.rb +257 -0
- data/lib/rubydb/transactions/transaction_manager.rb +435 -0
- data/lib/rubydb/types/bigint.rb +36 -0
- data/lib/rubydb/types/blob.rb +37 -0
- data/lib/rubydb/types/boolean.rb +34 -0
- data/lib/rubydb/types/date.rb +39 -0
- data/lib/rubydb/types/decimal.rb +48 -0
- data/lib/rubydb/types/float.rb +34 -0
- data/lib/rubydb/types/integer.rb +36 -0
- data/lib/rubydb/types/json.rb +41 -0
- data/lib/rubydb/types/null.rb +34 -0
- data/lib/rubydb/types/smallint.rb +36 -0
- data/lib/rubydb/types/text.rb +37 -0
- data/lib/rubydb/types/time.rb +46 -0
- data/lib/rubydb/types/timestamp.rb +39 -0
- data/lib/rubydb/types/type.rb +119 -0
- data/lib/rubydb/types/uuid.rb +47 -0
- data/lib/rubydb/types/varchar.rb +37 -0
- data/lib/rubydb/version.rb +33 -0
- data/lib/rubydb/wal/archive.rb +193 -0
- data/lib/rubydb/wal/checkpoint.rb +183 -0
- data/lib/rubydb/wal/lsn.rb +94 -0
- data/lib/rubydb/wal/reader.rb +260 -0
- data/lib/rubydb/wal/record.rb +105 -0
- data/lib/rubydb/wal/segment.rb +193 -0
- data/lib/rubydb/wal/wal.rb +452 -0
- data/lib/rubydb/wal/writer.rb +236 -0
- data/lib/rubydb.rb +7 -0
- data/packaging/docker/Dockerfile +36 -0
- data/packaging/docker/docker-compose.failover.yml +43 -0
- data/packaging/docker/entrypoint.sh +15 -0
- data/packaging/homebrew/rubydb.rb +19 -0
- data/packaging/systemd/rubydb.service +22 -0
- data/rubydb.gemspec +57 -0
- data/scripts/benchmark +7 -0
- data/scripts/durability_drill +37 -0
- data/scripts/format +0 -0
- data/scripts/fuzz +63 -0
- data/scripts/lint +0 -0
- data/scripts/release +50 -0
- data/scripts/release_check +43 -0
- data/scripts/replication_failover_drill +250 -0
- data/scripts/replication_network_failover_drill +255 -0
- data/scripts/restore_drill +45 -0
- data/scripts/security +0 -0
- data/scripts/setup +0 -0
- data/scripts/test +0 -0
- data/scripts/test-chaos +0 -0
- data/scripts/test-crash +0 -0
- data/scripts/test-integration +0 -0
- data/scripts/test-unit +0 -0
- data/visibility_map.json +1 -0
- metadata +538 -0
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "time"
|
|
4
|
+
require "thread"
|
|
5
|
+
require "json"
|
|
6
|
+
require "monitor"
|
|
7
|
+
|
|
8
|
+
module RubyDB
|
|
9
|
+
module Monitoring
|
|
10
|
+
# Metrics - Collects and manages metrics
|
|
11
|
+
class Metrics
|
|
12
|
+
attr_reader :stats
|
|
13
|
+
|
|
14
|
+
# Metric types
|
|
15
|
+
TYPE_COUNTER = :counter
|
|
16
|
+
TYPE_GAUGE = :gauge
|
|
17
|
+
TYPE_HISTOGRAM = :histogram
|
|
18
|
+
TYPE_SUMMARY = :summary
|
|
19
|
+
|
|
20
|
+
def initialize(config = {})
|
|
21
|
+
@config = config
|
|
22
|
+
@metrics = {}
|
|
23
|
+
# Mutating helpers call counter/gauge/histogram, which also acquire
|
|
24
|
+
# the lock. A reentrant monitor prevents self-deadlock while keeping
|
|
25
|
+
# metric updates atomic across threads.
|
|
26
|
+
@lock = Monitor.new
|
|
27
|
+
@collectors = []
|
|
28
|
+
@flush_interval = config[:flush_interval] || 60
|
|
29
|
+
@retention_period = config[:retention_period] || 3600
|
|
30
|
+
@stats = {
|
|
31
|
+
metrics_count: 0,
|
|
32
|
+
samples_collected: 0,
|
|
33
|
+
flushes: 0,
|
|
34
|
+
errors: 0
|
|
35
|
+
}
|
|
36
|
+
@flush_thread = nil
|
|
37
|
+
@running = false
|
|
38
|
+
@storage = []
|
|
39
|
+
|
|
40
|
+
start_flush_thread if config[:auto_flush] != false
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def counter(name, labels = {})
|
|
44
|
+
@lock.synchronize do
|
|
45
|
+
key = metric_key(name, labels)
|
|
46
|
+
@metrics[key] ||= {
|
|
47
|
+
type: TYPE_COUNTER,
|
|
48
|
+
name: name,
|
|
49
|
+
labels: labels,
|
|
50
|
+
value: 0,
|
|
51
|
+
created_at: Time.now,
|
|
52
|
+
updated_at: Time.now
|
|
53
|
+
}
|
|
54
|
+
@metrics[key]
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def gauge(name, labels = {})
|
|
59
|
+
@lock.synchronize do
|
|
60
|
+
key = metric_key(name, labels)
|
|
61
|
+
@metrics[key] ||= {
|
|
62
|
+
type: TYPE_GAUGE,
|
|
63
|
+
name: name,
|
|
64
|
+
labels: labels,
|
|
65
|
+
value: 0,
|
|
66
|
+
created_at: Time.now,
|
|
67
|
+
updated_at: Time.now
|
|
68
|
+
}
|
|
69
|
+
@metrics[key]
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def histogram(name, labels = {}, buckets = nil)
|
|
74
|
+
@lock.synchronize do
|
|
75
|
+
key = metric_key(name, labels)
|
|
76
|
+
@metrics[key] ||= {
|
|
77
|
+
type: TYPE_HISTOGRAM,
|
|
78
|
+
name: name,
|
|
79
|
+
labels: labels,
|
|
80
|
+
buckets: buckets || [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10],
|
|
81
|
+
values: [],
|
|
82
|
+
created_at: Time.now,
|
|
83
|
+
updated_at: Time.now
|
|
84
|
+
}
|
|
85
|
+
@metrics[key]
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def summary(name, labels = {})
|
|
90
|
+
@lock.synchronize do
|
|
91
|
+
key = metric_key(name, labels)
|
|
92
|
+
@metrics[key] ||= {
|
|
93
|
+
type: TYPE_SUMMARY,
|
|
94
|
+
name: name,
|
|
95
|
+
labels: labels,
|
|
96
|
+
values: [],
|
|
97
|
+
created_at: Time.now,
|
|
98
|
+
updated_at: Time.now
|
|
99
|
+
}
|
|
100
|
+
@metrics[key]
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def increment(name, labels = {}, amount = 1)
|
|
105
|
+
@lock.synchronize do
|
|
106
|
+
metric = counter(name, labels)
|
|
107
|
+
metric[:value] += amount
|
|
108
|
+
metric[:updated_at] = Time.now
|
|
109
|
+
@stats[:samples_collected] += 1
|
|
110
|
+
metric[:value]
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def decrement(name, labels = {}, amount = 1)
|
|
115
|
+
@lock.synchronize do
|
|
116
|
+
metric = counter(name, labels)
|
|
117
|
+
metric[:value] -= amount
|
|
118
|
+
metric[:updated_at] = Time.now
|
|
119
|
+
@stats[:samples_collected] += 1
|
|
120
|
+
metric[:value]
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def set_gauge(name, labels = {}, value)
|
|
125
|
+
@lock.synchronize do
|
|
126
|
+
metric = gauge(name, labels)
|
|
127
|
+
metric[:value] = value
|
|
128
|
+
metric[:updated_at] = Time.now
|
|
129
|
+
@stats[:samples_collected] += 1
|
|
130
|
+
metric[:value]
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def observe_histogram(name, labels = {}, value)
|
|
135
|
+
@lock.synchronize do
|
|
136
|
+
metric = histogram(name, labels)
|
|
137
|
+
metric[:values] << value
|
|
138
|
+
metric[:updated_at] = Time.now
|
|
139
|
+
@stats[:samples_collected] += 1
|
|
140
|
+
metric[:values].size
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def observe_summary(name, labels = {}, value)
|
|
145
|
+
@lock.synchronize do
|
|
146
|
+
metric = summary(name, labels)
|
|
147
|
+
metric[:values] << value
|
|
148
|
+
metric[:updated_at] = Time.now
|
|
149
|
+
@stats[:samples_collected] += 1
|
|
150
|
+
metric[:values].size
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def get(name, labels = {})
|
|
155
|
+
@lock.synchronize do
|
|
156
|
+
key = metric_key(name, labels)
|
|
157
|
+
@metrics[key]
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def get_all
|
|
162
|
+
@lock.synchronize do
|
|
163
|
+
@metrics.dup
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def get_by_name(name)
|
|
168
|
+
@lock.synchronize do
|
|
169
|
+
@metrics.select { |key, _| key.start_with?(name.to_s) }
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def reset(name, labels = {})
|
|
174
|
+
@lock.synchronize do
|
|
175
|
+
key = metric_key(name, labels)
|
|
176
|
+
metric = @metrics[key]
|
|
177
|
+
if metric
|
|
178
|
+
case metric[:type]
|
|
179
|
+
when TYPE_COUNTER
|
|
180
|
+
metric[:value] = 0
|
|
181
|
+
when TYPE_GAUGE
|
|
182
|
+
metric[:value] = 0
|
|
183
|
+
when TYPE_HISTOGRAM, TYPE_SUMMARY
|
|
184
|
+
metric[:values] = []
|
|
185
|
+
end
|
|
186
|
+
metric[:updated_at] = Time.now
|
|
187
|
+
end
|
|
188
|
+
metric
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def clear
|
|
193
|
+
@lock.synchronize do
|
|
194
|
+
@metrics.clear
|
|
195
|
+
@storage.clear
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
def flush
|
|
200
|
+
@lock.synchronize do
|
|
201
|
+
return if @metrics.empty?
|
|
202
|
+
|
|
203
|
+
snapshot = {
|
|
204
|
+
timestamp: Time.now.iso8601,
|
|
205
|
+
metrics: @metrics.dup
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
@storage << snapshot
|
|
209
|
+
@stats[:flushes] += 1
|
|
210
|
+
|
|
211
|
+
# Trim old data
|
|
212
|
+
cutoff = Time.now - @retention_period
|
|
213
|
+
@storage.delete_if { |s| Time.parse(s[:timestamp]) < cutoff }
|
|
214
|
+
|
|
215
|
+
snapshot
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
def query(start_time = nil, end_time = nil, name = nil)
|
|
220
|
+
@lock.synchronize do
|
|
221
|
+
start_time = Time.parse(start_time) if start_time.is_a?(String)
|
|
222
|
+
end_time = Time.parse(end_time) if end_time.is_a?(String)
|
|
223
|
+
|
|
224
|
+
results = @storage.select do |snapshot|
|
|
225
|
+
time = Time.parse(snapshot[:timestamp])
|
|
226
|
+
(start_time.nil? || time >= start_time) &&
|
|
227
|
+
(end_time.nil? || time <= end_time)
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
if name
|
|
231
|
+
results = results.map do |snapshot|
|
|
232
|
+
{
|
|
233
|
+
timestamp: snapshot[:timestamp],
|
|
234
|
+
metrics: snapshot[:metrics].select { |key, _| key.start_with?(name.to_s) }
|
|
235
|
+
}
|
|
236
|
+
end
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
results
|
|
240
|
+
end
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
def add_collector(collector)
|
|
244
|
+
@collectors << collector
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
def collect
|
|
248
|
+
@lock.synchronize do
|
|
249
|
+
@collectors.each do |collector|
|
|
250
|
+
begin
|
|
251
|
+
collector.call(self)
|
|
252
|
+
rescue => e
|
|
253
|
+
@stats[:errors] += 1
|
|
254
|
+
end
|
|
255
|
+
end
|
|
256
|
+
end
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
def stats
|
|
260
|
+
@lock.synchronize do
|
|
261
|
+
@stats.merge({
|
|
262
|
+
metrics_count: @metrics.size,
|
|
263
|
+
storage_size: @storage.size,
|
|
264
|
+
running: @running,
|
|
265
|
+
flush_interval: @flush_interval,
|
|
266
|
+
retention_period: @retention_period
|
|
267
|
+
})
|
|
268
|
+
end
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
def to_json
|
|
272
|
+
@lock.synchronize do
|
|
273
|
+
JSON.generate({
|
|
274
|
+
timestamp: Time.now.iso8601,
|
|
275
|
+
metrics: @metrics,
|
|
276
|
+
stats: @stats
|
|
277
|
+
})
|
|
278
|
+
end
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
# Render the current metrics using the Prometheus text exposition
|
|
282
|
+
# format. Labels are escaped and metric names are normalized so this
|
|
283
|
+
# output can be scraped without trusting caller-provided names.
|
|
284
|
+
def to_prometheus
|
|
285
|
+
@lock.synchronize do
|
|
286
|
+
lines = []
|
|
287
|
+
@metrics.each_value do |metric|
|
|
288
|
+
name = prometheus_name(metric[:name])
|
|
289
|
+
labels = prometheus_labels(metric[:labels])
|
|
290
|
+
case metric[:type]
|
|
291
|
+
when TYPE_COUNTER, TYPE_GAUGE
|
|
292
|
+
lines << "#{name}#{labels} #{metric[:value]}"
|
|
293
|
+
when TYPE_HISTOGRAM
|
|
294
|
+
values = metric[:values]
|
|
295
|
+
metric[:buckets].each do |bucket|
|
|
296
|
+
count = values.count { |value| value <= bucket }
|
|
297
|
+
lines << "#{name}_bucket#{prometheus_labels(metric[:labels].merge(le: bucket))} #{count}"
|
|
298
|
+
end
|
|
299
|
+
inf_label = "+Inf"
|
|
300
|
+
lines << "#{name}_bucket#{prometheus_labels(metric[:labels].merge(le: inf_label))} #{values.size}"
|
|
301
|
+
lines << "#{name}_count#{labels} #{values.size}"
|
|
302
|
+
lines << "#{name}_sum#{labels} #{values.sum}"
|
|
303
|
+
when TYPE_SUMMARY
|
|
304
|
+
sorted = metric[:values].sort
|
|
305
|
+
[0.5, 0.9, 0.99].each do |quantile|
|
|
306
|
+
index = [(sorted.size * quantile).ceil - 1, 0].max
|
|
307
|
+
value = sorted.empty? ? 0 : sorted[index]
|
|
308
|
+
lines << "#{name}#{prometheus_labels(metric[:labels].merge(quantile: quantile))} #{value}"
|
|
309
|
+
end
|
|
310
|
+
lines << "#{name}_count#{labels} #{sorted.size}"
|
|
311
|
+
lines << "#{name}_sum#{labels} #{sorted.sum}"
|
|
312
|
+
end
|
|
313
|
+
end
|
|
314
|
+
lines.join("\n") + (lines.empty? ? "" : "\n")
|
|
315
|
+
end
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
private
|
|
319
|
+
|
|
320
|
+
def metric_key(name, labels)
|
|
321
|
+
key = name.to_s.dup
|
|
322
|
+
labels.each do |k, v|
|
|
323
|
+
key << "_#{k}_#{v}"
|
|
324
|
+
end
|
|
325
|
+
key
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
def prometheus_name(name)
|
|
329
|
+
normalized = name.to_s.gsub(/[^a-zA-Z0-9_:]/, "_")
|
|
330
|
+
normalized = "rubydb_#{normalized}" unless normalized.start_with?("rubydb_")
|
|
331
|
+
normalized
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
def prometheus_labels(labels)
|
|
335
|
+
return "" if labels.empty?
|
|
336
|
+
encoded = labels.sort_by { |key, _| key.to_s }.map do |key, value|
|
|
337
|
+
escaped = value.to_s.gsub(/\\/, "\\\\").gsub('"', '\\"').gsub("\n", "\\n")
|
|
338
|
+
"#{key}=\"#{escaped}\""
|
|
339
|
+
end
|
|
340
|
+
"{#{encoded.join(',')}}"
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
def start_flush_thread
|
|
344
|
+
@running = true
|
|
345
|
+
@flush_thread = Thread.new do
|
|
346
|
+
while @running
|
|
347
|
+
sleep(@flush_interval)
|
|
348
|
+
begin
|
|
349
|
+
collect
|
|
350
|
+
flush
|
|
351
|
+
rescue => e
|
|
352
|
+
@stats[:errors] += 1
|
|
353
|
+
end
|
|
354
|
+
end
|
|
355
|
+
end
|
|
356
|
+
end
|
|
357
|
+
end
|
|
358
|
+
end
|
|
359
|
+
end
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RubyDB
|
|
4
|
+
module Monitoring
|
|
5
|
+
# Performance - Performance monitoring
|
|
6
|
+
class Performance
|
|
7
|
+
attr_reader :stats
|
|
8
|
+
|
|
9
|
+
def initialize(engine, config = {})
|
|
10
|
+
@engine = engine
|
|
11
|
+
@config = config
|
|
12
|
+
@statistics = Statistics.new
|
|
13
|
+
@metrics = {
|
|
14
|
+
query_times: [],
|
|
15
|
+
transaction_times: [],
|
|
16
|
+
connection_times: [],
|
|
17
|
+
wal_write_times: [],
|
|
18
|
+
checkpoint_times: [],
|
|
19
|
+
replication_lag: []
|
|
20
|
+
}
|
|
21
|
+
@query_counts = {}
|
|
22
|
+
@slow_queries = []
|
|
23
|
+
@max_slow_queries = config[:max_slow_queries] || 100
|
|
24
|
+
@slow_query_threshold = config[:slow_query_threshold] || 1000
|
|
25
|
+
@stats = {
|
|
26
|
+
total_queries: 0,
|
|
27
|
+
total_transactions: 0,
|
|
28
|
+
avg_query_time: 0,
|
|
29
|
+
slow_query_count: 0,
|
|
30
|
+
transaction_throughput: 0,
|
|
31
|
+
query_throughput: 0
|
|
32
|
+
}
|
|
33
|
+
@lock = Mutex.new
|
|
34
|
+
@start_time = Time.now
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def record_query(query, time_ms)
|
|
38
|
+
@lock.synchronize do
|
|
39
|
+
@stats[:total_queries] += 1
|
|
40
|
+
@metrics[:query_times] << time_ms
|
|
41
|
+
|
|
42
|
+
# Track query type
|
|
43
|
+
type = extract_query_type(query)
|
|
44
|
+
@query_counts[type] ||= 0
|
|
45
|
+
@query_counts[type] += 1
|
|
46
|
+
|
|
47
|
+
# Record slow queries
|
|
48
|
+
if time_ms > @slow_query_threshold
|
|
49
|
+
@slow_queries << {
|
|
50
|
+
query: query,
|
|
51
|
+
time_ms: time_ms,
|
|
52
|
+
timestamp: Time.now.iso8601
|
|
53
|
+
}
|
|
54
|
+
@slow_queries = @slow_queries.last(@max_slow_queries)
|
|
55
|
+
@stats[:slow_query_count] += 1
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
update_stats
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def record_transaction(time_ms)
|
|
63
|
+
@lock.synchronize do
|
|
64
|
+
@stats[:total_transactions] += 1
|
|
65
|
+
@metrics[:transaction_times] << time_ms
|
|
66
|
+
update_stats
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def record_connection(time_ms)
|
|
71
|
+
@lock.synchronize do
|
|
72
|
+
@metrics[:connection_times] << time_ms
|
|
73
|
+
update_stats
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def record_wal_write(time_ms)
|
|
78
|
+
@lock.synchronize do
|
|
79
|
+
@metrics[:wal_write_times] << time_ms
|
|
80
|
+
update_stats
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def record_checkpoint(time_ms)
|
|
85
|
+
@lock.synchronize do
|
|
86
|
+
@metrics[:checkpoint_times] << time_ms
|
|
87
|
+
update_stats
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def record_replication_lag(lag_ms)
|
|
92
|
+
@lock.synchronize do
|
|
93
|
+
@metrics[:replication_lag] << lag_ms
|
|
94
|
+
update_stats
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def query_performance
|
|
99
|
+
@lock.synchronize do
|
|
100
|
+
times = @metrics[:query_times]
|
|
101
|
+
{
|
|
102
|
+
total: @stats[:total_queries],
|
|
103
|
+
avg: @statistics.mean(times),
|
|
104
|
+
median: @statistics.median(times),
|
|
105
|
+
p95: @statistics.percentile(times, 95),
|
|
106
|
+
p99: @statistics.percentile(times, 99),
|
|
107
|
+
min: @statistics.min(times),
|
|
108
|
+
max: @statistics.max(times)
|
|
109
|
+
}
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def transaction_performance
|
|
114
|
+
@lock.synchronize do
|
|
115
|
+
times = @metrics[:transaction_times]
|
|
116
|
+
{
|
|
117
|
+
total: @stats[:total_transactions],
|
|
118
|
+
avg: @statistics.mean(times),
|
|
119
|
+
median: @statistics.median(times),
|
|
120
|
+
p95: @statistics.percentile(times, 95),
|
|
121
|
+
p99: @statistics.percentile(times, 99),
|
|
122
|
+
min: @statistics.min(times),
|
|
123
|
+
max: @statistics.max(times)
|
|
124
|
+
}
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def slow_queries
|
|
129
|
+
@slow_queries.dup
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def query_counts
|
|
133
|
+
@query_counts.dup
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def throughput
|
|
137
|
+
@lock.synchronize do
|
|
138
|
+
elapsed = (Time.now - @start_time).to_f
|
|
139
|
+
{
|
|
140
|
+
queries_per_second: @stats[:total_queries] / elapsed,
|
|
141
|
+
transactions_per_second: @stats[:total_transactions] / elapsed
|
|
142
|
+
}
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def stats
|
|
147
|
+
@lock.synchronize do
|
|
148
|
+
elapsed = (Time.now - @start_time).to_f
|
|
149
|
+
@stats.merge({
|
|
150
|
+
uptime_seconds: elapsed.round(2),
|
|
151
|
+
query_stats: query_performance,
|
|
152
|
+
transaction_stats: transaction_performance,
|
|
153
|
+
throughput: throughput,
|
|
154
|
+
slow_query_count: @slow_queries.size,
|
|
155
|
+
query_types: @query_counts
|
|
156
|
+
})
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
private
|
|
161
|
+
|
|
162
|
+
def extract_query_type(query)
|
|
163
|
+
query.strip.split.first&.upcase || "UNKNOWN"
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def update_stats
|
|
167
|
+
times = @metrics[:query_times]
|
|
168
|
+
@stats[:avg_query_time] = @statistics.mean(times) if times.any?
|
|
169
|
+
|
|
170
|
+
elapsed = (Time.now - @start_time).to_f
|
|
171
|
+
@stats[:query_throughput] = @stats[:total_queries] / elapsed if elapsed > 0
|
|
172
|
+
@stats[:transaction_throughput] = @stats[:total_transactions] / elapsed if elapsed > 0
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
end
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RubyDB
|
|
4
|
+
module Monitoring
|
|
5
|
+
# Statistics - Statistical calculations
|
|
6
|
+
class Statistics
|
|
7
|
+
attr_reader :stats
|
|
8
|
+
|
|
9
|
+
def initialize
|
|
10
|
+
@stats = {
|
|
11
|
+
calculations: 0,
|
|
12
|
+
errors: 0
|
|
13
|
+
}
|
|
14
|
+
@lock = Mutex.new
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def mean(values)
|
|
18
|
+
return 0 if values.empty?
|
|
19
|
+
values.sum / values.size.to_f
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def median(values)
|
|
23
|
+
return 0 if values.empty?
|
|
24
|
+
sorted = values.sort
|
|
25
|
+
mid = sorted.size / 2
|
|
26
|
+
if sorted.size.even?
|
|
27
|
+
(sorted[mid - 1] + sorted[mid]) / 2.0
|
|
28
|
+
else
|
|
29
|
+
sorted[mid]
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def mode(values)
|
|
34
|
+
return nil if values.empty?
|
|
35
|
+
freq = values.each_with_object(Hash.new(0)) { |v, h| h[v] += 1 }
|
|
36
|
+
max_freq = freq.values.max
|
|
37
|
+
freq.select { |_, f| f == max_freq }.keys
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def variance(values)
|
|
41
|
+
return 0 if values.size < 2
|
|
42
|
+
m = mean(values)
|
|
43
|
+
values.sum { |v| (v - m) ** 2 } / (values.size - 1).to_f
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def standard_deviation(values)
|
|
47
|
+
Math.sqrt(variance(values))
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def percentile(values, p)
|
|
51
|
+
return 0 if values.empty?
|
|
52
|
+
sorted = values.sort
|
|
53
|
+
index = (p / 100.0) * (sorted.size - 1)
|
|
54
|
+
if index.to_i == index
|
|
55
|
+
sorted[index.to_i]
|
|
56
|
+
else
|
|
57
|
+
lower = sorted[index.floor]
|
|
58
|
+
upper = sorted[index.ceil]
|
|
59
|
+
lower + (upper - lower) * (index - index.floor)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def min(values)
|
|
64
|
+
values.min || 0
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def max(values)
|
|
68
|
+
values.max || 0
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def sum(values)
|
|
72
|
+
values.sum
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def count(values)
|
|
76
|
+
values.size
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def histogram(values, bins = 10)
|
|
80
|
+
return {} if values.empty?
|
|
81
|
+
min_val = min(values)
|
|
82
|
+
max_val = max(values)
|
|
83
|
+
range = max_val - min_val
|
|
84
|
+
bin_width = range / bins.to_f
|
|
85
|
+
|
|
86
|
+
histogram = {}
|
|
87
|
+
bins.times do |i|
|
|
88
|
+
low = min_val + i * bin_width
|
|
89
|
+
high = low + bin_width
|
|
90
|
+
key = "#{low.round(2)}-#{high.round(2)}"
|
|
91
|
+
histogram[key] = values.count { |v| v >= low && v < high }
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
histogram
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def moving_average(values, window = 10)
|
|
98
|
+
return [] if values.empty?
|
|
99
|
+
result = []
|
|
100
|
+
values.each_with_index do |v, i|
|
|
101
|
+
start_idx = [0, i - window + 1].max
|
|
102
|
+
window_values = values[start_idx..i]
|
|
103
|
+
result << mean(window_values)
|
|
104
|
+
end
|
|
105
|
+
result
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def exponential_moving_average(values, alpha = 0.1)
|
|
109
|
+
return [] if values.empty?
|
|
110
|
+
result = []
|
|
111
|
+
ema = values.first
|
|
112
|
+
values.each do |v|
|
|
113
|
+
ema = alpha * v + (1 - alpha) * ema
|
|
114
|
+
result << ema
|
|
115
|
+
end
|
|
116
|
+
result
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def z_score(values, value)
|
|
120
|
+
return 0 if values.empty?
|
|
121
|
+
m = mean(values)
|
|
122
|
+
sd = standard_deviation(values)
|
|
123
|
+
return 0 if sd == 0
|
|
124
|
+
(value - m) / sd
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def correlation(x_values, y_values)
|
|
128
|
+
return 0 if x_values.size != y_values.size || x_values.empty?
|
|
129
|
+
n = x_values.size
|
|
130
|
+
sum_x = x_values.sum
|
|
131
|
+
sum_y = y_values.sum
|
|
132
|
+
sum_xy = x_values.zip(y_values).sum { |x, y| x * y }
|
|
133
|
+
sum_x2 = x_values.sum { |x| x * x }
|
|
134
|
+
sum_y2 = y_values.sum { |y| y * y }
|
|
135
|
+
|
|
136
|
+
numerator = n * sum_xy - sum_x * sum_y
|
|
137
|
+
denominator = Math.sqrt((n * sum_x2 - sum_x * sum_x) * (n * sum_y2 - sum_y * sum_y))
|
|
138
|
+
return 0 if denominator == 0
|
|
139
|
+
numerator / denominator
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def calculate_summary(values)
|
|
143
|
+
@stats[:calculations] += 1
|
|
144
|
+
{
|
|
145
|
+
count: count(values),
|
|
146
|
+
sum: sum(values),
|
|
147
|
+
mean: mean(values),
|
|
148
|
+
median: median(values),
|
|
149
|
+
min: min(values),
|
|
150
|
+
max: max(values),
|
|
151
|
+
variance: variance(values),
|
|
152
|
+
std_dev: standard_deviation(values),
|
|
153
|
+
p25: percentile(values, 25),
|
|
154
|
+
p50: percentile(values, 50),
|
|
155
|
+
p75: percentile(values, 75),
|
|
156
|
+
p90: percentile(values, 90),
|
|
157
|
+
p95: percentile(values, 95),
|
|
158
|
+
p99: percentile(values, 99)
|
|
159
|
+
}
|
|
160
|
+
rescue => e
|
|
161
|
+
@stats[:errors] += 1
|
|
162
|
+
{}
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def stats
|
|
166
|
+
@stats
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
end
|