@feltdb/core 0.15.0 → 0.16.1
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/dist/agent-state.d.ts +20 -0
- package/dist/agent-state.d.ts.map +1 -1
- package/dist/agent-state.js +36 -0
- package/dist/build-identity.json +12 -4
- package/dist/cli/commands.js +187 -4
- package/dist/cli/publish-engine.js +116 -16
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +20 -2
- package/dist/create/build-identity.json +12 -4
- package/dist/create/managed-account.js +93 -35
- package/dist/create/package-versions.js +1 -1
- package/dist/create/server-source/Cargo.lock +6 -509
- package/dist/create/server-source/Cargo.toml +2 -13
- package/dist/create/server-source/crates/feltdb-client/Cargo.toml +1 -1
- package/dist/create/server-source/crates/feltdb-server/Cargo.toml +1 -1
- package/dist/create/server-source/crates/feltdb-server/src/main.rs +356 -24
- package/dist/create/server-source/crates/feltdb-server/tests/rust_client.rs +10 -1
- package/dist/create/server-source/crates/feltdb-wasm/Cargo.toml +1 -1
- package/dist/db.d.ts +11 -1
- package/dist/db.d.ts.map +1 -1
- package/dist/db.js +15 -2
- package/dist/error-codes.d.ts +5 -0
- package/dist/error-codes.d.ts.map +1 -1
- package/dist/error-codes.js +9 -1
- package/dist/http-db.d.ts +16 -0
- package/dist/http-db.d.ts.map +1 -1
- package/dist/http-db.js +36 -6
- package/dist/managed-recovery.d.ts +6 -0
- package/dist/managed-recovery.d.ts.map +1 -1
- package/dist/managed-recovery.js +9 -0
- package/dist/studio-app/assets/{feltdb_wasm-BjM1ylL3.js → feltdb_wasm-BUIxCuTR.js} +1 -1
- package/dist/studio-app/assets/feltdb_wasm_bg-C3D6y6DK.wasm +0 -0
- package/dist/studio-app/assets/index-BhXYAhgS.js +37 -0
- package/dist/studio-app/index.html +1 -1
- package/dist/wasm/feltdb_wasm_bg.wasm +0 -0
- package/dist/wasm/package.json +1 -1
- package/dist/workload.d.ts +55 -3
- package/dist/workload.d.ts.map +1 -1
- package/dist/workload.js +23 -0
- package/dist/workspace/workspace-connection.d.ts.map +1 -1
- package/dist/workspace/workspace-connection.js +14 -3
- package/package.json +1 -1
- package/dist/create/server-source/crates/feltdb/Cargo.lock +0 -175
- package/dist/create/server-source/crates/feltdb/Cargo.toml +0 -102
- package/dist/create/server-source/crates/feltdb/LICENSE +0 -21
- package/dist/create/server-source/crates/feltdb/README.md +0 -75
- package/dist/create/server-source/crates/feltdb/benches/baselines/gate-13-redux.json +0 -370
- package/dist/create/server-source/crates/feltdb/benches/gate13_baseline.rs +0 -589
- package/dist/create/server-source/crates/feltdb/benches/gate13_phase_7_1_release_economics.rs +0 -247
- package/dist/create/server-source/crates/feltdb/benches/gate_13_redux.rs +0 -440
- package/dist/create/server-source/crates/feltdb/benches/gate_13_regression_runner.rs +0 -275
- package/dist/create/server-source/crates/feltdb/benches/gate_14a_concurrent_writer_scaling.rs +0 -381
- package/dist/create/server-source/crates/feltdb/benches/gate_14a_production_admission_revalidation.rs +0 -467
- package/dist/create/server-source/crates/feltdb/benches/gate_14a_rc2_admission_contract.rs +0 -489
- package/dist/create/server-source/crates/feltdb/benches/gate_14a_rc_root_cause.rs +0 -281
- package/dist/create/server-source/crates/feltdb/benches/gate_14a_sync1_queued_prototype.rs +0 -606
- package/dist/create/server-source/crates/feltdb/benches/gate_14a_sync_economics.rs +0 -531
- package/dist/create/server-source/crates/feltdb/benches/gate_14b_causal_backlog_scaling.rs +0 -462
- package/dist/create/server-source/crates/feltdb/benches/gate_14c_replication_contract_test.rs +0 -505
- package/dist/create/server-source/crates/feltdb/benches/gate_14c_replication_scaling.rs +0 -484
- package/dist/create/server-source/crates/feltdb/benches/gate_14d_combined_dimension_scaling.rs +0 -758
- package/dist/create/server-source/crates/feltdb/benches/phase_7_1_2_optimization_benchmark.rs +0 -421
- package/dist/create/server-source/crates/feltdb/benches/phase_7_1_3_crossover_analysis.rs +0 -329
- package/dist/create/server-source/crates/feltdb/src/acceptance_tests.rs +0 -1705
- package/dist/create/server-source/crates/feltdb/src/acquisition.rs +0 -286
- package/dist/create/server-source/crates/feltdb/src/admission.rs +0 -185
- package/dist/create/server-source/crates/feltdb/src/admission_contract_tests.rs +0 -507
- package/dist/create/server-source/crates/feltdb/src/adversarial_transport.rs +0 -539
- package/dist/create/server-source/crates/feltdb/src/agent_state.rs +0 -320
- package/dist/create/server-source/crates/feltdb/src/agent_transition_contract.rs +0 -1545
- package/dist/create/server-source/crates/feltdb/src/analytics.rs +0 -521
- package/dist/create/server-source/crates/feltdb/src/application.rs +0 -2674
- package/dist/create/server-source/crates/feltdb/src/application_runtime.rs +0 -1070
- package/dist/create/server-source/crates/feltdb/src/authority.rs +0 -479
- package/dist/create/server-source/crates/feltdb/src/authority_failover.rs +0 -847
- package/dist/create/server-source/crates/feltdb/src/authorization.rs +0 -1030
- package/dist/create/server-source/crates/feltdb/src/authorization_security_tests.rs +0 -1122
- package/dist/create/server-source/crates/feltdb/src/bin/feltdb-authority-client.rs +0 -32
- package/dist/create/server-source/crates/feltdb/src/bin/feltdb-authority.rs +0 -19
- package/dist/create/server-source/crates/feltdb/src/bin/feltdb_node.rs +0 -1474
- package/dist/create/server-source/crates/feltdb/src/capabilities/mod.rs +0 -8
- package/dist/create/server-source/crates/feltdb/src/capabilities/search.rs +0 -418
- package/dist/create/server-source/crates/feltdb/src/capabilities/vector.rs +0 -482
- package/dist/create/server-source/crates/feltdb/src/capability.rs +0 -903
- package/dist/create/server-source/crates/feltdb/src/cardinality_diagnostics.rs +0 -263
- package/dist/create/server-source/crates/feltdb/src/cardinality_endpoint.rs +0 -77
- package/dist/create/server-source/crates/feltdb/src/causal_backlog_bound.rs +0 -496
- package/dist/create/server-source/crates/feltdb/src/causal_dependency_barrier.rs +0 -2574
- package/dist/create/server-source/crates/feltdb/src/causal_dependency_barrier_phase_7_1.rs +0 -212
- package/dist/create/server-source/crates/feltdb/src/concurrency_fuzzing.rs +0 -422
- package/dist/create/server-source/crates/feltdb/src/consistency_contract.rs +0 -445
- package/dist/create/server-source/crates/feltdb/src/content_distribution.rs +0 -465
- package/dist/create/server-source/crates/feltdb/src/convergence.rs +0 -634
- package/dist/create/server-source/crates/feltdb/src/crash_atomic_boundary.rs +0 -427
- package/dist/create/server-source/crates/feltdb/src/crash_injection.rs +0 -376
- package/dist/create/server-source/crates/feltdb/src/crash_recovery_tests.rs +0 -365
- package/dist/create/server-source/crates/feltdb/src/cron.rs +0 -294
- package/dist/create/server-source/crates/feltdb/src/dedup_bound_investigation.rs +0 -483
- package/dist/create/server-source/crates/feltdb/src/distributed_indexing.rs +0 -477
- package/dist/create/server-source/crates/feltdb/src/distributed_tests.rs +0 -1003
- package/dist/create/server-source/crates/feltdb/src/distributed_transactions.rs +0 -1334
- package/dist/create/server-source/crates/feltdb/src/durability_guarantees.rs +0 -368
- package/dist/create/server-source/crates/feltdb/src/durable_dedup_set.rs +0 -231
- package/dist/create/server-source/crates/feltdb/src/durable_operation_identity.rs +0 -482
- package/dist/create/server-source/crates/feltdb/src/durable_operation_log.rs +0 -203
- package/dist/create/server-source/crates/feltdb/src/durable_sync.rs +0 -317
- package/dist/create/server-source/crates/feltdb/src/equality_index.rs +0 -595
- package/dist/create/server-source/crates/feltdb/src/execution.rs +0 -426
- package/dist/create/server-source/crates/feltdb/src/in_process_transport.rs +0 -214
- package/dist/create/server-source/crates/feltdb/src/indexing.rs +0 -776
- package/dist/create/server-source/crates/feltdb/src/lib.rs +0 -7365
- package/dist/create/server-source/crates/feltdb/src/managed_cas_tests.rs +0 -234
- package/dist/create/server-source/crates/feltdb/src/materialization.rs +0 -184
- package/dist/create/server-source/crates/feltdb/src/metrics.rs +0 -266
- package/dist/create/server-source/crates/feltdb/src/multi_node_convergence.rs +0 -310
- package/dist/create/server-source/crates/feltdb/src/multi_operation_transaction.rs +0 -736
- package/dist/create/server-source/crates/feltdb/src/observability.rs +0 -379
- package/dist/create/server-source/crates/feltdb/src/operation.rs +0 -290
- package/dist/create/server-source/crates/feltdb/src/operation_algebra.rs +0 -439
- package/dist/create/server-source/crates/feltdb/src/operation_log.rs +0 -349
- package/dist/create/server-source/crates/feltdb/src/p1_application_atomicity.rs +0 -332
- package/dist/create/server-source/crates/feltdb/src/p1_atomicity_acceptance.rs +0 -718
- package/dist/create/server-source/crates/feltdb/src/partition_reconciliation.rs +0 -487
- package/dist/create/server-source/crates/feltdb/src/peer_registry.rs +0 -166
- package/dist/create/server-source/crates/feltdb/src/permutation_scheduler.rs +0 -289
- package/dist/create/server-source/crates/feltdb/src/persistence_reality.rs +0 -566
- package/dist/create/server-source/crates/feltdb/src/phase1b_acceptance.rs +0 -3391
- package/dist/create/server-source/crates/feltdb/src/phase1c1_acceptance.rs +0 -203
- package/dist/create/server-source/crates/feltdb/src/phase1c2_acceptance.rs +0 -261
- package/dist/create/server-source/crates/feltdb/src/phase1c3_acceptance.rs +0 -502
- package/dist/create/server-source/crates/feltdb/src/phase1c_atomicity_proof.rs +0 -216
- package/dist/create/server-source/crates/feltdb/src/phase5_integration.rs +0 -303
- package/dist/create/server-source/crates/feltdb/src/phase5_scenarios.rs +0 -323
- package/dist/create/server-source/crates/feltdb/src/phase6_adversarial_scenarios.rs +0 -531
- package/dist/create/server-source/crates/feltdb/src/phase6_convergence_validator.rs +0 -406
- package/dist/create/server-source/crates/feltdb/src/phase6_persistence.rs +0 -436
- package/dist/create/server-source/crates/feltdb/src/phase_1c_real_tcp.rs +0 -387
- package/dist/create/server-source/crates/feltdb/src/phase_2a_failures.rs +0 -378
- package/dist/create/server-source/crates/feltdb/src/phase_2b_network.rs +0 -359
- package/dist/create/server-source/crates/feltdb/src/phase_2c_cascading.rs +0 -372
- package/dist/create/server-source/crates/feltdb/src/phase_3_durability.rs +0 -404
- package/dist/create/server-source/crates/feltdb/src/phase_4_baseline.rs +0 -376
- package/dist/create/server-source/crates/feltdb/src/phase_5_soak.rs +0 -461
- package/dist/create/server-source/crates/feltdb/src/policy_evaluation.rs +0 -2125
- package/dist/create/server-source/crates/feltdb/src/production_api.rs +0 -418
- package/dist/create/server-source/crates/feltdb/src/provenance.rs +0 -207
- package/dist/create/server-source/crates/feltdb/src/query_execution_diagnostics.rs +0 -126
- package/dist/create/server-source/crates/feltdb/src/query_performance.rs +0 -218
- package/dist/create/server-source/crates/feltdb/src/read_basis.rs +0 -257
- package/dist/create/server-source/crates/feltdb/src/references.rs +0 -404
- package/dist/create/server-source/crates/feltdb/src/replay_fuzzing.rs +0 -401
- package/dist/create/server-source/crates/feltdb/src/replica_acknowledgements.rs +0 -501
- package/dist/create/server-source/crates/feltdb/src/replica_membership.rs +0 -680
- package/dist/create/server-source/crates/feltdb/src/replication_manager.rs +0 -163
- package/dist/create/server-source/crates/feltdb/src/replication_protocol.rs +0 -133
- package/dist/create/server-source/crates/feltdb/src/routing.rs +0 -409
- package/dist/create/server-source/crates/feltdb/src/semantic_decision.rs +0 -2029
- package/dist/create/server-source/crates/feltdb/src/semantic_query.rs +0 -599
- package/dist/create/server-source/crates/feltdb/src/sharding.rs +0 -549
- package/dist/create/server-source/crates/feltdb/src/state_conflict_contract.rs +0 -516
- package/dist/create/server-source/crates/feltdb/src/state_contract.rs +0 -6003
- package/dist/create/server-source/crates/feltdb/src/state_diff_contract.rs +0 -222
- package/dist/create/server-source/crates/feltdb/src/state_facade.rs +0 -320
- package/dist/create/server-source/crates/feltdb/src/state_hash.rs +0 -291
- package/dist/create/server-source/crates/feltdb/src/state_model.rs +0 -4158
- package/dist/create/server-source/crates/feltdb/src/state_transition_store.rs +0 -379
- package/dist/create/server-source/crates/feltdb/src/state_trigger.rs +0 -673
- package/dist/create/server-source/crates/feltdb/src/storage.rs +0 -273
- package/dist/create/server-source/crates/feltdb/src/submission.rs +0 -471
- package/dist/create/server-source/crates/feltdb/src/sync.rs +0 -495
- package/dist/create/server-source/crates/feltdb/src/sync_contract.rs +0 -822
- package/dist/create/server-source/crates/feltdb/src/tcp_transport.rs +0 -433
- package/dist/create/server-source/crates/feltdb/src/transaction_api.rs +0 -462
- package/dist/create/server-source/crates/feltdb/src/transaction_invariants.rs +0 -965
- package/dist/create/server-source/crates/feltdb/src/transaction_preconditions.rs +0 -1120
- package/dist/create/server-source/crates/feltdb/src/transactions.rs +0 -643
- package/dist/create/server-source/crates/feltdb/src/trigger.rs +0 -390
- package/dist/create/server-source/crates/feltdb/src/trigger_contract.rs +0 -749
- package/dist/create/server-source/crates/feltdb/src/versioned_object_substrate.rs +0 -708
- package/dist/create/server-source/crates/feltdb/src/worker_mesh.rs +0 -1269
- package/dist/create/server-source/crates/feltdb/src/workflow.rs +0 -713
- package/dist/create/server-source/crates/feltdb/src/workflow_acceptance_tests.rs +0 -510
- package/dist/create/server-source/crates/feltdb/src/workflow_integration.rs +0 -354
- package/dist/create/server-source/crates/feltdb/src/workflow_runtime.rs +0 -594
- package/dist/create/server-source/crates/feltdb/src/workload.rs +0 -4222
- package/dist/create/server-source/crates/feltdb/src/workload_diagnostics.rs +0 -456
- package/dist/create/server-source/crates/feltdb/tests/agent_state_native.rs +0 -1188
- package/dist/create/server-source/crates/feltdb/tests/agent_transition_contract_conformance.rs +0 -2884
- package/dist/create/server-source/crates/feltdb/tests/authority_process.rs +0 -297
- package/dist/create/server-source/crates/feltdb/tests/authority_transaction_identity.rs +0 -534
- package/dist/create/server-source/crates/feltdb/tests/bounded_read_contract.rs +0 -132
- package/dist/create/server-source/crates/feltdb/tests/branching_evidence.rs +0 -299
- package/dist/create/server-source/crates/feltdb/tests/common/mod.rs +0 -96
- package/dist/create/server-source/crates/feltdb/tests/compaction_stall_contract.rs +0 -272
- package/dist/create/server-source/crates/feltdb/tests/crash_durability_contract.rs +0 -467
- package/dist/create/server-source/crates/feltdb/tests/current_revision_authority_evidence.rs +0 -309
- package/dist/create/server-source/crates/feltdb/tests/durable_backup_contract.rs +0 -445
- package/dist/create/server-source/crates/feltdb/tests/durable_corruption_contract.rs +0 -518
- package/dist/create/server-source/crates/feltdb/tests/durable_format_compatibility.rs +0 -392
- package/dist/create/server-source/crates/feltdb/tests/feltdb_state_boundary_tests.rs +0 -850
- package/dist/create/server-source/crates/feltdb/tests/fixtures/agent_transition_corpus.json +0 -23340
- package/dist/create/server-source/crates/feltdb/tests/fixtures/state_conflict_contract_corpus.json +0 -1916
- package/dist/create/server-source/crates/feltdb/tests/fixtures/state_diff_contract_corpus.json +0 -1878
- package/dist/create/server-source/crates/feltdb/tests/fixtures/trigger_contract_corpus.json +0 -1862
- package/dist/create/server-source/crates/feltdb/tests/generalized_version_control_evidence.rs +0 -123
- package/dist/create/server-source/crates/feltdb/tests/generalized_version_control_substrate_completion.rs +0 -299
- package/dist/create/server-source/crates/feltdb/tests/local_process_ownership.rs +0 -69
- package/dist/create/server-source/crates/feltdb/tests/managed_incident_regression.rs +0 -191
- package/dist/create/server-source/crates/feltdb/tests/operational_health_contract.rs +0 -278
- package/dist/create/server-source/crates/feltdb/tests/pr34_query_collection.rs +0 -234
- package/dist/create/server-source/crates/feltdb/tests/pr35_equality_index.rs +0 -947
- package/dist/create/server-source/crates/feltdb/tests/pr7_self_authorization_proof.rs +0 -404
- package/dist/create/server-source/crates/feltdb/tests/pr8_vocabulary_assessment.rs +0 -916
- package/dist/create/server-source/crates/feltdb/tests/pr9_phase2_boundary_tests.rs +0 -1045
- package/dist/create/server-source/crates/feltdb/tests/pr9_phase3a_path_a_tests.rs +0 -347
- package/dist/create/server-source/crates/feltdb/tests/pr9_phase3c_authorized_mutations.rs +0 -361
- package/dist/create/server-source/crates/feltdb/tests/pr9_phase3c_role_based_authorization.rs +0 -330
- package/dist/create/server-source/crates/feltdb/tests/pr9_phase3c_simple_auth_delete.rs +0 -93
- package/dist/create/server-source/crates/feltdb/tests/pr9_phase3c_team_delete_role_authorization.rs +0 -622
- package/dist/create/server-source/crates/feltdb/tests/pr9_teams_role_based_access.rs +0 -517
- package/dist/create/server-source/crates/feltdb/tests/production_certification.rs +0 -1153
- package/dist/create/server-source/crates/feltdb/tests/production_contract.rs +0 -771
- package/dist/create/server-source/crates/feltdb/tests/production_readiness_contract.rs +0 -1383
- package/dist/create/server-source/crates/feltdb/tests/reconciliation_application.rs +0 -868
- package/dist/create/server-source/crates/feltdb/tests/reconciliation_wire_format_evidence.rs +0 -221
- package/dist/create/server-source/crates/feltdb/tests/replicated_history_contract.rs +0 -417
- package/dist/create/server-source/crates/feltdb/tests/resource_scoped_revisions.rs +0 -338
- package/dist/create/server-source/crates/feltdb/tests/revision_identity_contract.rs +0 -1039
- package/dist/create/server-source/crates/feltdb/tests/revision_model_decision.rs +0 -739
- package/dist/create/server-source/crates/feltdb/tests/revision_retention_boundary_evidence.rs +0 -452
- package/dist/create/server-source/crates/feltdb/tests/saas_authorization_integration.rs +0 -81
- package/dist/create/server-source/crates/feltdb/tests/saas_invitation_lifecycle.rs +0 -438
- package/dist/create/server-source/crates/feltdb/tests/semantic_decision.rs +0 -1165
- package/dist/create/server-source/crates/feltdb/tests/semantic_query.rs +0 -767
- package/dist/create/server-source/crates/feltdb/tests/semantic_query_contract.rs +0 -363
- package/dist/create/server-source/crates/feltdb/tests/state_conflict_contract_conformance.rs +0 -1799
- package/dist/create/server-source/crates/feltdb/tests/state_diff_contract_conformance.rs +0 -1316
- package/dist/create/server-source/crates/feltdb/tests/state_model_integration.rs +0 -358
- package/dist/create/server-source/crates/feltdb/tests/state_persistence_integration.rs +0 -365
- package/dist/create/server-source/crates/feltdb/tests/state_ref_authority.rs +0 -918
- package/dist/create/server-source/crates/feltdb/tests/state_store_boundary_evidence.rs +0 -299
- package/dist/create/server-source/crates/feltdb/tests/storage_independent_versioned_object_substrate.rs +0 -328
- package/dist/create/server-source/crates/feltdb/tests/sync_divergence_evidence.rs +0 -255
- package/dist/create/server-source/crates/feltdb/tests/three_way_input_boundary_evidence.rs +0 -249
- package/dist/create/server-source/crates/feltdb/tests/trigger_contract_conformance.rs +0 -994
- package/dist/create/server-source/crates/feltdb/tests/workload_envelope_contract.rs +0 -484
- package/dist/studio-app/assets/feltdb_wasm_bg-TseJn2Ax.wasm +0 -0
- package/dist/studio-app/assets/index-1wd0uKua.js +0 -30
|
@@ -1,1269 +0,0 @@
|
|
|
1
|
-
//! Worker and mesh semantics. Compute is replaceable; durable workload state is authority.
|
|
2
|
-
use crate::authorization::{SignedGrant, Subject};
|
|
3
|
-
use crate::workload::{Workload, WorkloadClaim, WorkloadError, WorkloadStore};
|
|
4
|
-
use serde::{Deserialize, Serialize};
|
|
5
|
-
use serde_json::Value;
|
|
6
|
-
use sha2::{Digest, Sha256};
|
|
7
|
-
use std::collections::{BTreeMap, BTreeSet};
|
|
8
|
-
use std::fs;
|
|
9
|
-
use std::path::{Path, PathBuf};
|
|
10
|
-
pub const WORKER_CONTRACT_VERSION: u32 = 1;
|
|
11
|
-
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
|
|
12
|
-
#[serde(rename_all = "snake_case")]
|
|
13
|
-
pub enum WorkerRuntimeKind {
|
|
14
|
-
Wasm,
|
|
15
|
-
Process,
|
|
16
|
-
Container,
|
|
17
|
-
Browser,
|
|
18
|
-
Remote,
|
|
19
|
-
Hosted,
|
|
20
|
-
Edge,
|
|
21
|
-
Customer,
|
|
22
|
-
}
|
|
23
|
-
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
|
|
24
|
-
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
|
25
|
-
pub enum WorkerLifecycle {
|
|
26
|
-
Registered,
|
|
27
|
-
Ready,
|
|
28
|
-
Busy,
|
|
29
|
-
Draining,
|
|
30
|
-
Offline,
|
|
31
|
-
Recovering,
|
|
32
|
-
}
|
|
33
|
-
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
|
|
34
|
-
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
|
35
|
-
pub enum WorkerHealth {
|
|
36
|
-
Healthy,
|
|
37
|
-
Degraded,
|
|
38
|
-
Unhealthy,
|
|
39
|
-
Unknown,
|
|
40
|
-
}
|
|
41
|
-
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
|
42
|
-
pub struct WorkerResourceProfile {
|
|
43
|
-
pub cpu_millis: Option<u64>,
|
|
44
|
-
pub memory_bytes: Option<u64>,
|
|
45
|
-
pub disk_bytes: Option<u64>,
|
|
46
|
-
pub gpu_count: Option<u32>,
|
|
47
|
-
}
|
|
48
|
-
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
49
|
-
pub struct WorkerCapacity {
|
|
50
|
-
pub concurrency: u32,
|
|
51
|
-
#[serde(default)]
|
|
52
|
-
pub active: u32,
|
|
53
|
-
}
|
|
54
|
-
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
55
|
-
pub struct WorkerRegistration {
|
|
56
|
-
pub tenant_id: String,
|
|
57
|
-
pub application_id: String,
|
|
58
|
-
pub name: String,
|
|
59
|
-
pub runtime: WorkerRuntimeKind,
|
|
60
|
-
pub version: String,
|
|
61
|
-
#[serde(default)]
|
|
62
|
-
pub capabilities: BTreeSet<String>,
|
|
63
|
-
#[serde(default)]
|
|
64
|
-
pub workload_kinds: BTreeSet<String>,
|
|
65
|
-
#[serde(default)]
|
|
66
|
-
pub resource_profile: WorkerResourceProfile,
|
|
67
|
-
#[serde(default)]
|
|
68
|
-
pub labels: BTreeMap<String, String>,
|
|
69
|
-
pub capacity: WorkerCapacity,
|
|
70
|
-
pub authorization_grant: SignedGrant,
|
|
71
|
-
pub registered_by: Subject,
|
|
72
|
-
pub registered_at: i64,
|
|
73
|
-
}
|
|
74
|
-
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
75
|
-
pub struct Worker {
|
|
76
|
-
pub contract_version: u32,
|
|
77
|
-
pub worker_id: String,
|
|
78
|
-
pub tenant_id: String,
|
|
79
|
-
pub application_id: String,
|
|
80
|
-
pub name: String,
|
|
81
|
-
pub runtime: WorkerRuntimeKind,
|
|
82
|
-
pub version: String,
|
|
83
|
-
pub declared_capabilities: BTreeSet<String>,
|
|
84
|
-
pub granted_capabilities: BTreeSet<String>,
|
|
85
|
-
pub workload_kinds: BTreeSet<String>,
|
|
86
|
-
pub resource_profile: WorkerResourceProfile,
|
|
87
|
-
pub labels: BTreeMap<String, String>,
|
|
88
|
-
pub capacity: WorkerCapacity,
|
|
89
|
-
pub health: WorkerHealth,
|
|
90
|
-
pub lifecycle: WorkerLifecycle,
|
|
91
|
-
pub heartbeat_interval_ms: u64,
|
|
92
|
-
pub heartbeat_timeout_ms: u64,
|
|
93
|
-
pub last_seen: i64,
|
|
94
|
-
pub capability_version: u64,
|
|
95
|
-
pub authorization_grant_id: String,
|
|
96
|
-
#[serde(default)]
|
|
97
|
-
pub claims: BTreeSet<String>,
|
|
98
|
-
}
|
|
99
|
-
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
100
|
-
pub struct WorkerHeartbeat {
|
|
101
|
-
pub worker_id: String,
|
|
102
|
-
pub version: String,
|
|
103
|
-
pub capability_version: u64,
|
|
104
|
-
pub capacity: WorkerCapacity,
|
|
105
|
-
pub health: WorkerHealth,
|
|
106
|
-
pub timestamp: i64,
|
|
107
|
-
#[serde(default)]
|
|
108
|
-
pub running_claims: BTreeSet<String>,
|
|
109
|
-
}
|
|
110
|
-
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
111
|
-
pub struct WorkerPool {
|
|
112
|
-
pub pool_id: String,
|
|
113
|
-
pub tenant_id: String,
|
|
114
|
-
pub application_id: Option<String>,
|
|
115
|
-
pub name: String,
|
|
116
|
-
#[serde(default)]
|
|
117
|
-
pub capabilities: BTreeSet<String>,
|
|
118
|
-
#[serde(default)]
|
|
119
|
-
pub required_labels: BTreeMap<String, String>,
|
|
120
|
-
#[serde(default)]
|
|
121
|
-
pub worker_ids: BTreeSet<String>,
|
|
122
|
-
pub max_concurrency: Option<u32>,
|
|
123
|
-
pub created_at: i64,
|
|
124
|
-
}
|
|
125
|
-
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
|
126
|
-
pub struct PlacementPolicy {
|
|
127
|
-
#[serde(default)]
|
|
128
|
-
pub required_labels: BTreeMap<String, String>,
|
|
129
|
-
#[serde(default)]
|
|
130
|
-
pub affinity: BTreeMap<String, String>,
|
|
131
|
-
#[serde(default)]
|
|
132
|
-
pub anti_affinity: BTreeMap<String, String>,
|
|
133
|
-
pub pool_id: Option<String>,
|
|
134
|
-
}
|
|
135
|
-
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
136
|
-
pub struct PlacementDecision {
|
|
137
|
-
pub decision_id: String,
|
|
138
|
-
pub workload_id: String,
|
|
139
|
-
pub eligible_workers: Vec<String>,
|
|
140
|
-
pub selected_worker: Option<String>,
|
|
141
|
-
pub reason: String,
|
|
142
|
-
pub evaluated_at: i64,
|
|
143
|
-
}
|
|
144
|
-
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
145
|
-
pub struct MeshExecutionClaim {
|
|
146
|
-
pub workload_id: String,
|
|
147
|
-
pub attempt_id: String,
|
|
148
|
-
pub worker_id: String,
|
|
149
|
-
pub lease_id: String,
|
|
150
|
-
pub claim_id: String,
|
|
151
|
-
pub fencing_token: u64,
|
|
152
|
-
pub capability_snapshot: BTreeSet<String>,
|
|
153
|
-
pub execution_context_id: String,
|
|
154
|
-
pub issued_at: i64,
|
|
155
|
-
pub expires_at: i64,
|
|
156
|
-
}
|
|
157
|
-
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
158
|
-
pub struct RuntimePolicy {
|
|
159
|
-
pub max_cpu_millis: Option<u64>,
|
|
160
|
-
pub max_memory_bytes: Option<u64>,
|
|
161
|
-
pub max_duration_ms: u64,
|
|
162
|
-
pub network_policy: String,
|
|
163
|
-
pub filesystem_policy: String,
|
|
164
|
-
pub secret_policy: String,
|
|
165
|
-
pub artifact_policy: String,
|
|
166
|
-
}
|
|
167
|
-
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
168
|
-
pub struct RuntimeExecutionResult {
|
|
169
|
-
pub result_id: String,
|
|
170
|
-
pub output: Value,
|
|
171
|
-
#[serde(default)]
|
|
172
|
-
pub logs: Vec<String>,
|
|
173
|
-
#[serde(default)]
|
|
174
|
-
pub artifacts: Vec<String>,
|
|
175
|
-
#[serde(default)]
|
|
176
|
-
pub resource_usage: BTreeMap<String, u64>,
|
|
177
|
-
}
|
|
178
|
-
pub trait WorkerRuntime {
|
|
179
|
-
fn start(
|
|
180
|
-
&mut self,
|
|
181
|
-
claim: &MeshExecutionClaim,
|
|
182
|
-
policy: &RuntimePolicy,
|
|
183
|
-
) -> Result<(), WorkerMeshError>;
|
|
184
|
-
fn execute(
|
|
185
|
-
&mut self,
|
|
186
|
-
claim: &MeshExecutionClaim,
|
|
187
|
-
input: &Value,
|
|
188
|
-
) -> Result<RuntimeExecutionResult, WorkerMeshError>;
|
|
189
|
-
fn cancel(&mut self, claim: &MeshExecutionClaim) -> Result<(), WorkerMeshError>;
|
|
190
|
-
fn timeout(&mut self, claim: &MeshExecutionClaim) -> Result<(), WorkerMeshError>;
|
|
191
|
-
}
|
|
192
|
-
pub trait WorkerPlacement {
|
|
193
|
-
fn eligible_workers(
|
|
194
|
-
&self,
|
|
195
|
-
workload: &Workload,
|
|
196
|
-
workers: &[Worker],
|
|
197
|
-
policy: &PlacementPolicy,
|
|
198
|
-
) -> Vec<String>;
|
|
199
|
-
fn select_worker(
|
|
200
|
-
&self,
|
|
201
|
-
workload: &Workload,
|
|
202
|
-
candidates: &[Worker],
|
|
203
|
-
) -> Result<String, WorkerMeshError>;
|
|
204
|
-
}
|
|
205
|
-
#[derive(Default)]
|
|
206
|
-
pub struct DeterministicPlacement;
|
|
207
|
-
impl WorkerPlacement for DeterministicPlacement {
|
|
208
|
-
fn eligible_workers(
|
|
209
|
-
&self,
|
|
210
|
-
w: &Workload,
|
|
211
|
-
workers: &[Worker],
|
|
212
|
-
policy: &PlacementPolicy,
|
|
213
|
-
) -> Vec<String> {
|
|
214
|
-
let mut ids = workers
|
|
215
|
-
.iter()
|
|
216
|
-
.filter(|worker| {
|
|
217
|
-
worker.tenant_id == w.tenant_id
|
|
218
|
-
&& worker.application_id == w.application_id
|
|
219
|
-
&& worker.lifecycle == WorkerLifecycle::Ready
|
|
220
|
-
&& worker.health == WorkerHealth::Healthy
|
|
221
|
-
&& worker.capacity.active < worker.capacity.concurrency
|
|
222
|
-
&& w.capability_snapshot
|
|
223
|
-
.is_subset(&worker.granted_capabilities)
|
|
224
|
-
&& (!worker.workload_kinds.is_empty())
|
|
225
|
-
.then_some(worker.workload_kinds.contains(&w.definition_id))
|
|
226
|
-
.unwrap_or(true)
|
|
227
|
-
&& policy
|
|
228
|
-
.required_labels
|
|
229
|
-
.iter()
|
|
230
|
-
.all(|(k, v)| worker.labels.get(k) == Some(v))
|
|
231
|
-
&& policy
|
|
232
|
-
.anti_affinity
|
|
233
|
-
.iter()
|
|
234
|
-
.all(|(k, v)| worker.labels.get(k) != Some(v))
|
|
235
|
-
})
|
|
236
|
-
.map(|v| v.worker_id.clone())
|
|
237
|
-
.collect::<Vec<_>>();
|
|
238
|
-
ids.sort();
|
|
239
|
-
ids
|
|
240
|
-
}
|
|
241
|
-
fn select_worker(
|
|
242
|
-
&self,
|
|
243
|
-
_: &Workload,
|
|
244
|
-
candidates: &[Worker],
|
|
245
|
-
) -> Result<String, WorkerMeshError> {
|
|
246
|
-
candidates
|
|
247
|
-
.iter()
|
|
248
|
-
.min_by_key(|worker| {
|
|
249
|
-
(
|
|
250
|
-
worker.capacity.active,
|
|
251
|
-
std::cmp::Reverse(worker.capacity.concurrency),
|
|
252
|
-
worker.worker_id.clone(),
|
|
253
|
-
)
|
|
254
|
-
})
|
|
255
|
-
.map(|v| v.worker_id.clone())
|
|
256
|
-
.ok_or_else(|| {
|
|
257
|
-
WorkerMeshError::new(
|
|
258
|
-
"MESH_NO_ELIGIBLE_WORKER",
|
|
259
|
-
"no worker satisfies placement policy",
|
|
260
|
-
)
|
|
261
|
-
})
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
265
|
-
pub struct WorkerReconciliation {
|
|
266
|
-
pub worker_id: String,
|
|
267
|
-
pub known_claims: BTreeSet<String>,
|
|
268
|
-
pub running_attempts: BTreeSet<String>,
|
|
269
|
-
pub completed_attempts: BTreeSet<String>,
|
|
270
|
-
pub failed_attempts: BTreeSet<String>,
|
|
271
|
-
pub worker_version: String,
|
|
272
|
-
pub capability_version: u64,
|
|
273
|
-
pub timestamp: i64,
|
|
274
|
-
}
|
|
275
|
-
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
276
|
-
pub struct WorkerReconciliationResult {
|
|
277
|
-
pub authoritative_claims: BTreeSet<String>,
|
|
278
|
-
pub fenced_claims: BTreeSet<String>,
|
|
279
|
-
pub cancel_attempts: BTreeSet<String>,
|
|
280
|
-
pub worker: Worker,
|
|
281
|
-
}
|
|
282
|
-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
|
283
|
-
pub struct WorkerMeshError {
|
|
284
|
-
pub code: String,
|
|
285
|
-
pub message: String,
|
|
286
|
-
}
|
|
287
|
-
impl WorkerMeshError {
|
|
288
|
-
fn new(code: &str, message: impl Into<String>) -> Self {
|
|
289
|
-
Self {
|
|
290
|
-
code: code.into(),
|
|
291
|
-
message: message.into(),
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
impl std::fmt::Display for WorkerMeshError {
|
|
296
|
-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
297
|
-
write!(f, "{}: {}", self.code, self.message)
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
impl std::error::Error for WorkerMeshError {}
|
|
301
|
-
impl From<WorkloadError> for WorkerMeshError {
|
|
302
|
-
fn from(v: WorkloadError) -> Self {
|
|
303
|
-
Self::new(&v.code, v.message)
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
308
|
-
pub struct WorkerMeshStore {
|
|
309
|
-
#[serde(skip)]
|
|
310
|
-
path: PathBuf,
|
|
311
|
-
#[serde(default)]
|
|
312
|
-
pub workers: BTreeMap<String, Worker>,
|
|
313
|
-
#[serde(default)]
|
|
314
|
-
pub pools: BTreeMap<String, WorkerPool>,
|
|
315
|
-
#[serde(default)]
|
|
316
|
-
pub placements: Vec<PlacementDecision>,
|
|
317
|
-
}
|
|
318
|
-
impl WorkerMeshStore {
|
|
319
|
-
pub fn load(path: impl AsRef<Path>) -> Result<Self, WorkerMeshError> {
|
|
320
|
-
let path = path.as_ref().to_path_buf();
|
|
321
|
-
if !path.exists() {
|
|
322
|
-
return Ok(Self {
|
|
323
|
-
path,
|
|
324
|
-
workers: BTreeMap::new(),
|
|
325
|
-
pools: BTreeMap::new(),
|
|
326
|
-
placements: vec![],
|
|
327
|
-
});
|
|
328
|
-
}
|
|
329
|
-
let mut value: Self = serde_json::from_slice(&fs::read(&path).map_err(io)?).map_err(io)?;
|
|
330
|
-
value.path = path;
|
|
331
|
-
Ok(value)
|
|
332
|
-
}
|
|
333
|
-
fn persist(&self) -> Result<(), WorkerMeshError> {
|
|
334
|
-
if let Some(parent) = self.path.parent() {
|
|
335
|
-
fs::create_dir_all(parent).map_err(io)?;
|
|
336
|
-
}
|
|
337
|
-
let temp = self.path.with_extension("tmp");
|
|
338
|
-
fs::write(&temp, serde_json::to_vec_pretty(self).map_err(io)?).map_err(io)?;
|
|
339
|
-
fs::rename(temp, &self.path).map_err(io)
|
|
340
|
-
}
|
|
341
|
-
pub fn register(&mut self, input: WorkerRegistration) -> Result<Worker, WorkerMeshError> {
|
|
342
|
-
if input.capacity.concurrency == 0 {
|
|
343
|
-
return Err(WorkerMeshError::new(
|
|
344
|
-
"WORKER_INVALID",
|
|
345
|
-
"concurrency must be positive",
|
|
346
|
-
));
|
|
347
|
-
}
|
|
348
|
-
if input.authorization_grant.grant.subject != input.registered_by
|
|
349
|
-
|| input.authorization_grant.grant.tenant_id != input.tenant_id
|
|
350
|
-
|| input.authorization_grant.grant.application_id != input.application_id
|
|
351
|
-
{
|
|
352
|
-
return Err(WorkerMeshError::new(
|
|
353
|
-
"WORKER_UNAUTHORIZED",
|
|
354
|
-
"registration grant scope mismatch",
|
|
355
|
-
));
|
|
356
|
-
}
|
|
357
|
-
let allowed = input
|
|
358
|
-
.authorization_grant
|
|
359
|
-
.grant
|
|
360
|
-
.capabilities
|
|
361
|
-
.iter()
|
|
362
|
-
.filter_map(|v| v.strip_prefix("worker:capability:").map(String::from))
|
|
363
|
-
.collect::<BTreeSet<_>>();
|
|
364
|
-
let granted = input.capabilities.intersection(&allowed).cloned().collect();
|
|
365
|
-
let seed = format!(
|
|
366
|
-
"{}:{}:{}",
|
|
367
|
-
input.tenant_id, input.application_id, input.name
|
|
368
|
-
);
|
|
369
|
-
let id = format!("worker_{}", &hex(&Sha256::digest(seed))[..24]);
|
|
370
|
-
if let Some(existing) = self.workers.get(&id) {
|
|
371
|
-
return Ok(existing.clone());
|
|
372
|
-
}
|
|
373
|
-
let worker = Worker {
|
|
374
|
-
contract_version: 1,
|
|
375
|
-
worker_id: id.clone(),
|
|
376
|
-
tenant_id: input.tenant_id,
|
|
377
|
-
application_id: input.application_id,
|
|
378
|
-
name: input.name,
|
|
379
|
-
runtime: input.runtime,
|
|
380
|
-
version: input.version,
|
|
381
|
-
declared_capabilities: input.capabilities,
|
|
382
|
-
granted_capabilities: granted,
|
|
383
|
-
workload_kinds: input.workload_kinds,
|
|
384
|
-
resource_profile: input.resource_profile,
|
|
385
|
-
labels: input.labels,
|
|
386
|
-
capacity: input.capacity,
|
|
387
|
-
health: WorkerHealth::Unknown,
|
|
388
|
-
lifecycle: WorkerLifecycle::Registered,
|
|
389
|
-
heartbeat_interval_ms: 10_000,
|
|
390
|
-
heartbeat_timeout_ms: 30_000,
|
|
391
|
-
last_seen: input.registered_at,
|
|
392
|
-
capability_version: input.authorization_grant.grant.revision,
|
|
393
|
-
authorization_grant_id: input.authorization_grant.grant.id,
|
|
394
|
-
claims: BTreeSet::new(),
|
|
395
|
-
};
|
|
396
|
-
self.workers.insert(id, worker.clone());
|
|
397
|
-
self.persist()?;
|
|
398
|
-
Ok(worker)
|
|
399
|
-
}
|
|
400
|
-
pub fn heartbeat(
|
|
401
|
-
&mut self,
|
|
402
|
-
id: &str,
|
|
403
|
-
input: WorkerHeartbeat,
|
|
404
|
-
clock_tolerance_seconds: i64,
|
|
405
|
-
) -> Result<Worker, WorkerMeshError> {
|
|
406
|
-
let worker = self
|
|
407
|
-
.workers
|
|
408
|
-
.get_mut(id)
|
|
409
|
-
.ok_or_else(|| WorkerMeshError::new("WORKER_NOT_FOUND", "worker not found"))?;
|
|
410
|
-
if input.worker_id != id
|
|
411
|
-
|| input.capability_version != worker.capability_version
|
|
412
|
-
|| input.version != worker.version
|
|
413
|
-
{
|
|
414
|
-
return Err(WorkerMeshError::new(
|
|
415
|
-
"WORKER_HEARTBEAT_REJECTED",
|
|
416
|
-
"worker identity, version, or capability version changed",
|
|
417
|
-
));
|
|
418
|
-
}
|
|
419
|
-
if input.timestamp + clock_tolerance_seconds < worker.last_seen {
|
|
420
|
-
return Err(WorkerMeshError::new(
|
|
421
|
-
"WORKER_CLOCK_INVALID",
|
|
422
|
-
"heartbeat is outside clock tolerance",
|
|
423
|
-
));
|
|
424
|
-
}
|
|
425
|
-
if matches!(worker.lifecycle, WorkerLifecycle::Offline) {
|
|
426
|
-
return Err(WorkerMeshError::new(
|
|
427
|
-
"WORKER_RECOVERY_REQUIRED",
|
|
428
|
-
"offline worker must reconcile before heartbeat",
|
|
429
|
-
));
|
|
430
|
-
}
|
|
431
|
-
worker.last_seen = input.timestamp;
|
|
432
|
-
worker.health = input.health;
|
|
433
|
-
worker.capacity.active = input.capacity.active.min(worker.capacity.concurrency);
|
|
434
|
-
worker.claims = input.running_claims;
|
|
435
|
-
worker.lifecycle = if worker.lifecycle == WorkerLifecycle::Draining {
|
|
436
|
-
if worker.claims.is_empty() {
|
|
437
|
-
WorkerLifecycle::Offline
|
|
438
|
-
} else {
|
|
439
|
-
WorkerLifecycle::Draining
|
|
440
|
-
}
|
|
441
|
-
} else if worker.capacity.active >= worker.capacity.concurrency {
|
|
442
|
-
WorkerLifecycle::Busy
|
|
443
|
-
} else {
|
|
444
|
-
WorkerLifecycle::Ready
|
|
445
|
-
};
|
|
446
|
-
let result = worker.clone();
|
|
447
|
-
self.persist()?;
|
|
448
|
-
Ok(result)
|
|
449
|
-
}
|
|
450
|
-
pub fn transition(&mut self, id: &str, to: WorkerLifecycle) -> Result<Worker, WorkerMeshError> {
|
|
451
|
-
let worker = self
|
|
452
|
-
.workers
|
|
453
|
-
.get_mut(id)
|
|
454
|
-
.ok_or_else(|| WorkerMeshError::new("WORKER_NOT_FOUND", "worker not found"))?;
|
|
455
|
-
if !worker_transition(worker.lifecycle, to) {
|
|
456
|
-
return Err(WorkerMeshError::new(
|
|
457
|
-
"WORKER_TRANSITION_DENIED",
|
|
458
|
-
format!("{:?} cannot transition to {:?}", worker.lifecycle, to),
|
|
459
|
-
));
|
|
460
|
-
}
|
|
461
|
-
worker.lifecycle = to;
|
|
462
|
-
let result = worker.clone();
|
|
463
|
-
self.persist()?;
|
|
464
|
-
Ok(result)
|
|
465
|
-
}
|
|
466
|
-
pub fn drain(&mut self, id: &str) -> Result<Worker, WorkerMeshError> {
|
|
467
|
-
self.transition(id, WorkerLifecycle::Draining)
|
|
468
|
-
}
|
|
469
|
-
pub fn recover(&mut self, id: &str) -> Result<Worker, WorkerMeshError> {
|
|
470
|
-
self.transition(id, WorkerLifecycle::Recovering)
|
|
471
|
-
}
|
|
472
|
-
pub fn remove(&mut self, id: &str) -> Result<(), WorkerMeshError> {
|
|
473
|
-
let worker = self
|
|
474
|
-
.workers
|
|
475
|
-
.get(id)
|
|
476
|
-
.ok_or_else(|| WorkerMeshError::new("WORKER_NOT_FOUND", "worker not found"))?;
|
|
477
|
-
if !worker.claims.is_empty() {
|
|
478
|
-
return Err(WorkerMeshError::new(
|
|
479
|
-
"WORKER_BUSY",
|
|
480
|
-
"worker with active claims cannot be removed",
|
|
481
|
-
));
|
|
482
|
-
}
|
|
483
|
-
self.workers.remove(id);
|
|
484
|
-
for pool in self.pools.values_mut() {
|
|
485
|
-
pool.worker_ids.remove(id);
|
|
486
|
-
}
|
|
487
|
-
self.persist()
|
|
488
|
-
}
|
|
489
|
-
/// Recover work whose claim lease expired, and release those claims from
|
|
490
|
-
/// the workers that held them so their capacity is not held by work they
|
|
491
|
-
/// no longer own. Recovery itself — `WorkerLost`, the attempt budget, the
|
|
492
|
-
/// decision — is `WorkloadStore::recover_expired`. Worker liveness is not
|
|
493
|
-
/// judged here; that is `detect_failures`.
|
|
494
|
-
/// Time out work past a deadline ([`WorkloadStore::enforce_deadlines`]) and
|
|
495
|
-
/// release the claims the committed workloads no longer hold, as
|
|
496
|
-
/// [`Self::recover_expired_work`] does for expired leases.
|
|
497
|
-
pub fn enforce_deadlines(
|
|
498
|
-
&mut self,
|
|
499
|
-
now: i64,
|
|
500
|
-
workloads: &mut WorkloadStore,
|
|
501
|
-
) -> Result<Vec<String>, WorkerMeshError> {
|
|
502
|
-
// The claims this node's projection knows, listed before the store is
|
|
503
|
-
// re-read: a claim another writer already ended must still be released
|
|
504
|
-
// here, so the list cannot come from the refreshed projection.
|
|
505
|
-
let claimed: Vec<(String, String, String)> = workloads
|
|
506
|
-
.workloads
|
|
507
|
-
.values()
|
|
508
|
-
.filter_map(|w| {
|
|
509
|
-
w.current_claim.as_ref().map(|claim| {
|
|
510
|
-
(
|
|
511
|
-
w.workload_id.clone(),
|
|
512
|
-
claim.worker_id.clone(),
|
|
513
|
-
claim.claim_id.clone(),
|
|
514
|
-
)
|
|
515
|
-
})
|
|
516
|
-
})
|
|
517
|
-
.collect();
|
|
518
|
-
let expired = workloads.enforce_deadlines(now)?;
|
|
519
|
-
for (workload_id, worker_id, claim_id) in claimed {
|
|
520
|
-
let still_current = workloads
|
|
521
|
-
.workloads
|
|
522
|
-
.get(&workload_id)
|
|
523
|
-
.and_then(|w| w.current_claim.as_ref())
|
|
524
|
-
.is_some_and(|claim| claim.claim_id == claim_id);
|
|
525
|
-
// Releasing is idempotent: a claim the worker no longer holds is a no-op.
|
|
526
|
-
if !still_current && self.workers.contains_key(&worker_id) {
|
|
527
|
-
self.release_claim(&worker_id, &claim_id)?;
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
Ok(expired)
|
|
531
|
-
}
|
|
532
|
-
pub fn recover_expired_work(
|
|
533
|
-
&mut self,
|
|
534
|
-
now: i64,
|
|
535
|
-
workloads: &mut WorkloadStore,
|
|
536
|
-
) -> Result<Vec<String>, WorkerMeshError> {
|
|
537
|
-
// Listed before `recover_expired` re-reads the store, for the same
|
|
538
|
-
// reason as in `enforce_deadlines`.
|
|
539
|
-
let expired: Vec<(String, String, String)> = workloads
|
|
540
|
-
.workloads
|
|
541
|
-
.values()
|
|
542
|
-
.filter_map(|w| w.current_claim.as_ref().map(|claim| (w, claim)))
|
|
543
|
-
.filter(|(_, claim)| claim.expires_at <= now)
|
|
544
|
-
.map(|(w, claim)| {
|
|
545
|
-
(
|
|
546
|
-
w.workload_id.clone(),
|
|
547
|
-
claim.worker_id.clone(),
|
|
548
|
-
claim.claim_id.clone(),
|
|
549
|
-
)
|
|
550
|
-
})
|
|
551
|
-
.collect();
|
|
552
|
-
let recovered = workloads.recover_expired(now)?;
|
|
553
|
-
for (workload_id, worker_id, claim_id) in expired {
|
|
554
|
-
// Release a claim only once the committed workload no longer holds
|
|
555
|
-
// it, whichever writer of the store recovered it.
|
|
556
|
-
let still_current = workloads
|
|
557
|
-
.workloads
|
|
558
|
-
.get(&workload_id)
|
|
559
|
-
.and_then(|w| w.current_claim.as_ref())
|
|
560
|
-
.is_some_and(|claim| claim.claim_id == claim_id);
|
|
561
|
-
let held = self
|
|
562
|
-
.workers
|
|
563
|
-
.get(&worker_id)
|
|
564
|
-
.is_some_and(|worker| worker.claims.contains(&claim_id));
|
|
565
|
-
if held && !still_current {
|
|
566
|
-
self.release_claim(&worker_id, &claim_id)?;
|
|
567
|
-
}
|
|
568
|
-
}
|
|
569
|
-
Ok(recovered)
|
|
570
|
-
}
|
|
571
|
-
pub fn detect_failures(
|
|
572
|
-
&mut self,
|
|
573
|
-
now: i64,
|
|
574
|
-
workloads: &mut WorkloadStore,
|
|
575
|
-
) -> Result<Vec<String>, WorkerMeshError> {
|
|
576
|
-
let failed = self
|
|
577
|
-
.workers
|
|
578
|
-
.values()
|
|
579
|
-
.filter(|w| {
|
|
580
|
-
!matches!(w.lifecycle, WorkerLifecycle::Offline)
|
|
581
|
-
&& w.last_seen + (w.heartbeat_timeout_ms / 1000) as i64 <= now
|
|
582
|
-
})
|
|
583
|
-
.map(|w| w.worker_id.clone())
|
|
584
|
-
.collect::<Vec<_>>();
|
|
585
|
-
for id in &failed {
|
|
586
|
-
if let Some(worker) = self.workers.get_mut(id) {
|
|
587
|
-
worker.lifecycle = WorkerLifecycle::Offline;
|
|
588
|
-
worker.health = WorkerHealth::Unhealthy;
|
|
589
|
-
worker.claims.clear();
|
|
590
|
-
}
|
|
591
|
-
}
|
|
592
|
-
self.recover_expired_work(now, workloads)?;
|
|
593
|
-
self.persist()?;
|
|
594
|
-
Ok(failed)
|
|
595
|
-
}
|
|
596
|
-
pub fn create_pool(&mut self, pool: WorkerPool) -> Result<WorkerPool, WorkerMeshError> {
|
|
597
|
-
if self.pools.contains_key(&pool.pool_id) {
|
|
598
|
-
return Err(WorkerMeshError::new(
|
|
599
|
-
"WORKER_POOL_EXISTS",
|
|
600
|
-
"worker pool exists",
|
|
601
|
-
));
|
|
602
|
-
}
|
|
603
|
-
for worker in &pool.worker_ids {
|
|
604
|
-
if !self.workers.contains_key(worker) {
|
|
605
|
-
return Err(WorkerMeshError::new("WORKER_NOT_FOUND", worker));
|
|
606
|
-
}
|
|
607
|
-
}
|
|
608
|
-
self.pools.insert(pool.pool_id.clone(), pool.clone());
|
|
609
|
-
self.persist()?;
|
|
610
|
-
Ok(pool)
|
|
611
|
-
}
|
|
612
|
-
pub fn eligible(&self, workload: &Workload, policy: &PlacementPolicy) -> Vec<String> {
|
|
613
|
-
let mut workers = self.workers.values().cloned().collect::<Vec<_>>();
|
|
614
|
-
if let Some(pool) = policy.pool_id.as_ref().and_then(|id| self.pools.get(id)) {
|
|
615
|
-
workers.retain(|v| {
|
|
616
|
-
pool.worker_ids.contains(&v.worker_id)
|
|
617
|
-
&& pool.capabilities.is_subset(&v.granted_capabilities)
|
|
618
|
-
});
|
|
619
|
-
}
|
|
620
|
-
DeterministicPlacement.eligible_workers(workload, &workers, policy)
|
|
621
|
-
}
|
|
622
|
-
pub fn place_and_claim(
|
|
623
|
-
&mut self,
|
|
624
|
-
workloads: &mut WorkloadStore,
|
|
625
|
-
workload_id: &str,
|
|
626
|
-
worker_id: &str,
|
|
627
|
-
now: i64,
|
|
628
|
-
lease_ms: u64,
|
|
629
|
-
) -> Result<MeshExecutionClaim, WorkerMeshError> {
|
|
630
|
-
let workload = workloads.get(workload_id)?.clone();
|
|
631
|
-
let worker = self
|
|
632
|
-
.workers
|
|
633
|
-
.get(worker_id)
|
|
634
|
-
.ok_or_else(|| WorkerMeshError::new("WORKER_NOT_FOUND", "worker not found"))?
|
|
635
|
-
.clone();
|
|
636
|
-
if !self
|
|
637
|
-
.eligible(&workload, &PlacementPolicy::default())
|
|
638
|
-
.contains(&worker_id.to_string())
|
|
639
|
-
{
|
|
640
|
-
return Err(WorkerMeshError::new(
|
|
641
|
-
"MESH_PLACEMENT_DENIED",
|
|
642
|
-
"worker is not eligible",
|
|
643
|
-
));
|
|
644
|
-
}
|
|
645
|
-
let claim = workloads.claim(
|
|
646
|
-
workload_id,
|
|
647
|
-
worker_id,
|
|
648
|
-
&worker.granted_capabilities,
|
|
649
|
-
now,
|
|
650
|
-
lease_ms,
|
|
651
|
-
)?;
|
|
652
|
-
let attempt = workloads
|
|
653
|
-
.get(workload_id)?
|
|
654
|
-
.attempts
|
|
655
|
-
.last()
|
|
656
|
-
.unwrap()
|
|
657
|
-
.attempt_id
|
|
658
|
-
.clone();
|
|
659
|
-
let mesh = mesh_claim(&claim, &attempt);
|
|
660
|
-
let current = self.workers.get_mut(worker_id).unwrap();
|
|
661
|
-
current.claims.insert(claim.claim_id.clone());
|
|
662
|
-
current.capacity.active += 1;
|
|
663
|
-
current.lifecycle = if current.capacity.active >= current.capacity.concurrency {
|
|
664
|
-
WorkerLifecycle::Busy
|
|
665
|
-
} else {
|
|
666
|
-
WorkerLifecycle::Ready
|
|
667
|
-
};
|
|
668
|
-
self.placements.push(PlacementDecision {
|
|
669
|
-
decision_id: format!("placement_{}", self.placements.len() + 1),
|
|
670
|
-
workload_id: workload_id.into(),
|
|
671
|
-
eligible_workers: vec![worker_id.into()],
|
|
672
|
-
selected_worker: Some(worker_id.into()),
|
|
673
|
-
reason: "deterministic capability and capacity match".into(),
|
|
674
|
-
evaluated_at: now,
|
|
675
|
-
});
|
|
676
|
-
self.persist()?;
|
|
677
|
-
Ok(mesh)
|
|
678
|
-
}
|
|
679
|
-
pub fn release_claim(
|
|
680
|
-
&mut self,
|
|
681
|
-
worker_id: &str,
|
|
682
|
-
claim_id: &str,
|
|
683
|
-
) -> Result<(), WorkerMeshError> {
|
|
684
|
-
let worker = self
|
|
685
|
-
.workers
|
|
686
|
-
.get_mut(worker_id)
|
|
687
|
-
.ok_or_else(|| WorkerMeshError::new("WORKER_NOT_FOUND", "worker not found"))?;
|
|
688
|
-
// Releasing is idempotent: only a claim the worker still holds frees capacity.
|
|
689
|
-
if !worker.claims.remove(claim_id) {
|
|
690
|
-
return Ok(());
|
|
691
|
-
}
|
|
692
|
-
worker.capacity.active = worker.capacity.active.saturating_sub(1);
|
|
693
|
-
if worker.lifecycle == WorkerLifecycle::Draining && worker.claims.is_empty() {
|
|
694
|
-
worker.lifecycle = WorkerLifecycle::Offline
|
|
695
|
-
} else if worker.lifecycle == WorkerLifecycle::Busy {
|
|
696
|
-
worker.lifecycle = WorkerLifecycle::Ready
|
|
697
|
-
}
|
|
698
|
-
self.persist()
|
|
699
|
-
}
|
|
700
|
-
pub fn reconcile(
|
|
701
|
-
&mut self,
|
|
702
|
-
input: WorkerReconciliation,
|
|
703
|
-
workloads: &WorkloadStore,
|
|
704
|
-
) -> Result<WorkerReconciliationResult, WorkerMeshError> {
|
|
705
|
-
let worker = self
|
|
706
|
-
.workers
|
|
707
|
-
.get_mut(&input.worker_id)
|
|
708
|
-
.ok_or_else(|| WorkerMeshError::new("WORKER_NOT_FOUND", "worker not found"))?;
|
|
709
|
-
if input.worker_version != worker.version
|
|
710
|
-
|| input.capability_version != worker.capability_version
|
|
711
|
-
{
|
|
712
|
-
return Err(WorkerMeshError::new(
|
|
713
|
-
"WORKER_RECONCILIATION_DENIED",
|
|
714
|
-
"worker contract version changed",
|
|
715
|
-
));
|
|
716
|
-
}
|
|
717
|
-
let authoritative = workloads
|
|
718
|
-
.workloads
|
|
719
|
-
.values()
|
|
720
|
-
.filter_map(|w| w.current_claim.as_ref())
|
|
721
|
-
.filter(|c| c.worker_id == input.worker_id && c.expires_at > input.timestamp)
|
|
722
|
-
.map(|c| c.claim_id.clone())
|
|
723
|
-
.collect::<BTreeSet<_>>();
|
|
724
|
-
let fenced = input
|
|
725
|
-
.known_claims
|
|
726
|
-
.difference(&authoritative)
|
|
727
|
-
.cloned()
|
|
728
|
-
.collect();
|
|
729
|
-
let cancel = input
|
|
730
|
-
.running_attempts
|
|
731
|
-
.iter()
|
|
732
|
-
.filter(|attempt| {
|
|
733
|
-
!workloads.workloads.values().any(|w| {
|
|
734
|
-
w.attempts.iter().any(|a| &a.attempt_id == *attempt)
|
|
735
|
-
&& matches!(
|
|
736
|
-
w.state,
|
|
737
|
-
crate::workload::WorkloadState::Running
|
|
738
|
-
| crate::workload::WorkloadState::Checkpointed
|
|
739
|
-
)
|
|
740
|
-
})
|
|
741
|
-
})
|
|
742
|
-
.cloned()
|
|
743
|
-
.collect();
|
|
744
|
-
worker.claims = authoritative.clone();
|
|
745
|
-
worker.last_seen = input.timestamp;
|
|
746
|
-
worker.lifecycle = WorkerLifecycle::Ready;
|
|
747
|
-
worker.health = WorkerHealth::Healthy;
|
|
748
|
-
let result = WorkerReconciliationResult {
|
|
749
|
-
authoritative_claims: authoritative,
|
|
750
|
-
fenced_claims: fenced,
|
|
751
|
-
cancel_attempts: cancel,
|
|
752
|
-
worker: worker.clone(),
|
|
753
|
-
};
|
|
754
|
-
self.persist()?;
|
|
755
|
-
Ok(result)
|
|
756
|
-
}
|
|
757
|
-
}
|
|
758
|
-
fn mesh_claim(claim: &WorkloadClaim, attempt: &str) -> MeshExecutionClaim {
|
|
759
|
-
MeshExecutionClaim {
|
|
760
|
-
workload_id: claim.workload_id.clone(),
|
|
761
|
-
attempt_id: attempt.into(),
|
|
762
|
-
worker_id: claim.worker_id.clone(),
|
|
763
|
-
lease_id: claim.lease_id.clone(),
|
|
764
|
-
claim_id: claim.claim_id.clone(),
|
|
765
|
-
fencing_token: claim.fencing_token,
|
|
766
|
-
capability_snapshot: claim.capability_snapshot.clone(),
|
|
767
|
-
execution_context_id: format!("context_{}", claim.claim_id),
|
|
768
|
-
issued_at: claim.claimed_at,
|
|
769
|
-
expires_at: claim.expires_at,
|
|
770
|
-
}
|
|
771
|
-
}
|
|
772
|
-
pub fn worker_transition(from: WorkerLifecycle, to: WorkerLifecycle) -> bool {
|
|
773
|
-
use WorkerLifecycle::*;
|
|
774
|
-
matches!(
|
|
775
|
-
(from, to),
|
|
776
|
-
(Registered, Ready)
|
|
777
|
-
| (Ready, Busy)
|
|
778
|
-
| (Busy, Ready)
|
|
779
|
-
| (Ready, Draining)
|
|
780
|
-
| (Busy, Draining)
|
|
781
|
-
| (Draining, Offline)
|
|
782
|
-
| (Ready, Offline)
|
|
783
|
-
| (Busy, Offline)
|
|
784
|
-
| (Offline, Recovering)
|
|
785
|
-
| (Recovering, Ready)
|
|
786
|
-
| (Recovering, Offline)
|
|
787
|
-
)
|
|
788
|
-
}
|
|
789
|
-
pub fn validate_mesh_claim(
|
|
790
|
-
claim: &MeshExecutionClaim,
|
|
791
|
-
worker: &Worker,
|
|
792
|
-
workload: &Workload,
|
|
793
|
-
now: i64,
|
|
794
|
-
) -> Result<(), WorkerMeshError> {
|
|
795
|
-
if claim.worker_id != worker.worker_id
|
|
796
|
-
|| claim.workload_id != workload.workload_id
|
|
797
|
-
|| worker.tenant_id != workload.tenant_id
|
|
798
|
-
|| worker.application_id != workload.application_id
|
|
799
|
-
{
|
|
800
|
-
return Err(WorkerMeshError::new(
|
|
801
|
-
"MESH_CLAIM_SCOPE_DENIED",
|
|
802
|
-
"claim crosses worker/workload scope",
|
|
803
|
-
));
|
|
804
|
-
}
|
|
805
|
-
if claim.expires_at <= now
|
|
806
|
-
|| workload.current_claim.as_ref().is_none_or(|current| {
|
|
807
|
-
current.claim_id != claim.claim_id || current.fencing_token != claim.fencing_token
|
|
808
|
-
})
|
|
809
|
-
{
|
|
810
|
-
return Err(WorkerMeshError::new(
|
|
811
|
-
"MESH_STALE_FENCE",
|
|
812
|
-
"claim is expired or fenced",
|
|
813
|
-
));
|
|
814
|
-
}
|
|
815
|
-
if !claim
|
|
816
|
-
.capability_snapshot
|
|
817
|
-
.is_subset(&worker.granted_capabilities)
|
|
818
|
-
|| claim.capability_snapshot != workload.capability_snapshot
|
|
819
|
-
{
|
|
820
|
-
return Err(WorkerMeshError::new(
|
|
821
|
-
"MESH_CAPABILITY_DENIED",
|
|
822
|
-
"claim capability snapshot is invalid",
|
|
823
|
-
));
|
|
824
|
-
}
|
|
825
|
-
Ok(())
|
|
826
|
-
}
|
|
827
|
-
fn io(e: impl std::fmt::Display) -> WorkerMeshError {
|
|
828
|
-
WorkerMeshError::new("MESH_STORAGE_FAILURE", e.to_string())
|
|
829
|
-
}
|
|
830
|
-
fn hex(bytes: &[u8]) -> String {
|
|
831
|
-
bytes.iter().map(|v| format!("{v:02x}")).collect()
|
|
832
|
-
}
|
|
833
|
-
|
|
834
|
-
#[cfg(test)]
|
|
835
|
-
mod tests {
|
|
836
|
-
use super::*;
|
|
837
|
-
use crate::authorization::{
|
|
838
|
-
Grant, GrantConstraints, GrantSigner, GrantStatus, ResourceScope, ResourceScopeKind,
|
|
839
|
-
};
|
|
840
|
-
use crate::workload::{CreateWorkload, WorkloadRetryPolicy, WorkloadTimeouts};
|
|
841
|
-
fn setup(
|
|
842
|
-
name: &str,
|
|
843
|
-
) -> (
|
|
844
|
-
PathBuf,
|
|
845
|
-
PathBuf,
|
|
846
|
-
WorkerMeshStore,
|
|
847
|
-
WorkloadStore,
|
|
848
|
-
SignedGrant,
|
|
849
|
-
) {
|
|
850
|
-
let mesh = std::env::temp_dir().join(format!("mesh-{name}-{}.json", std::process::id()));
|
|
851
|
-
let work =
|
|
852
|
-
std::env::temp_dir().join(format!("mesh-work-{name}-{}.json", std::process::id()));
|
|
853
|
-
let signer = GrantSigner::new("k", vec![4; 32]).unwrap();
|
|
854
|
-
let subject = Subject::Service("registrar".into());
|
|
855
|
-
let grant = Grant {
|
|
856
|
-
contract_version: 1,
|
|
857
|
-
id: "g".into(),
|
|
858
|
-
issuer: Subject::Human("h".into()),
|
|
859
|
-
subject,
|
|
860
|
-
tenant_id: "t".into(),
|
|
861
|
-
application_id: "a".into(),
|
|
862
|
-
capabilities: [
|
|
863
|
-
"worker:register".into(),
|
|
864
|
-
"worker:capability:documents.extract".into(),
|
|
865
|
-
]
|
|
866
|
-
.into(),
|
|
867
|
-
resources: vec![ResourceScope {
|
|
868
|
-
kind: ResourceScopeKind::ResourcePrefix,
|
|
869
|
-
uri: "flow://a/workers/*".into(),
|
|
870
|
-
}],
|
|
871
|
-
constraints: GrantConstraints::default(),
|
|
872
|
-
issued_at: 1,
|
|
873
|
-
expires_at: 999,
|
|
874
|
-
parent_grant: None,
|
|
875
|
-
revision: 1,
|
|
876
|
-
status: GrantStatus::Active,
|
|
877
|
-
};
|
|
878
|
-
(
|
|
879
|
-
mesh.clone(),
|
|
880
|
-
work.clone(),
|
|
881
|
-
WorkerMeshStore::load(&mesh).unwrap(),
|
|
882
|
-
WorkloadStore::load(&work).unwrap(),
|
|
883
|
-
signer.sign(grant).unwrap(),
|
|
884
|
-
)
|
|
885
|
-
}
|
|
886
|
-
fn register(store: &mut WorkerMeshStore, grant: SignedGrant) -> Worker {
|
|
887
|
-
store
|
|
888
|
-
.register(WorkerRegistration {
|
|
889
|
-
tenant_id: "t".into(),
|
|
890
|
-
application_id: "a".into(),
|
|
891
|
-
name: "edge".into(),
|
|
892
|
-
runtime: WorkerRuntimeKind::Customer,
|
|
893
|
-
version: "1".into(),
|
|
894
|
-
capabilities: ["documents.extract".into(), "database.admin".into()].into(),
|
|
895
|
-
workload_kinds: BTreeSet::new(),
|
|
896
|
-
resource_profile: Default::default(),
|
|
897
|
-
labels: [("environment".into(), "production".into())].into(),
|
|
898
|
-
capacity: WorkerCapacity {
|
|
899
|
-
concurrency: 1,
|
|
900
|
-
active: 0,
|
|
901
|
-
},
|
|
902
|
-
registered_by: Subject::Service("registrar".into()),
|
|
903
|
-
authorization_grant: grant,
|
|
904
|
-
registered_at: 10,
|
|
905
|
-
})
|
|
906
|
-
.unwrap()
|
|
907
|
-
}
|
|
908
|
-
#[test]
|
|
909
|
-
fn registration_intersects_capabilities_and_lifecycle_is_strict() {
|
|
910
|
-
let (m, w, mut mesh, _, grant) = setup("register");
|
|
911
|
-
let worker = register(&mut mesh, grant);
|
|
912
|
-
assert_eq!(
|
|
913
|
-
worker.granted_capabilities,
|
|
914
|
-
["documents.extract".into()].into()
|
|
915
|
-
);
|
|
916
|
-
assert!(mesh
|
|
917
|
-
.transition(&worker.worker_id, WorkerLifecycle::Busy)
|
|
918
|
-
.is_err());
|
|
919
|
-
mesh.heartbeat(
|
|
920
|
-
&worker.worker_id,
|
|
921
|
-
WorkerHeartbeat {
|
|
922
|
-
worker_id: worker.worker_id.clone(),
|
|
923
|
-
version: "1".into(),
|
|
924
|
-
capability_version: 1,
|
|
925
|
-
capacity: WorkerCapacity {
|
|
926
|
-
concurrency: 1,
|
|
927
|
-
active: 0,
|
|
928
|
-
},
|
|
929
|
-
health: WorkerHealth::Healthy,
|
|
930
|
-
timestamp: 11,
|
|
931
|
-
running_claims: BTreeSet::new(),
|
|
932
|
-
},
|
|
933
|
-
2,
|
|
934
|
-
)
|
|
935
|
-
.unwrap();
|
|
936
|
-
let _ = fs::remove_file(m);
|
|
937
|
-
let _ = fs::remove_file(w);
|
|
938
|
-
}
|
|
939
|
-
#[test]
|
|
940
|
-
fn releasing_a_claim_twice_frees_capacity_once() {
|
|
941
|
-
let (m, wpath, mut mesh, mut workloads, grant) = setup("release-twice");
|
|
942
|
-
let worker = mesh
|
|
943
|
-
.register(WorkerRegistration {
|
|
944
|
-
tenant_id: "t".into(),
|
|
945
|
-
application_id: "a".into(),
|
|
946
|
-
name: "edge".into(),
|
|
947
|
-
runtime: WorkerRuntimeKind::Customer,
|
|
948
|
-
version: "1".into(),
|
|
949
|
-
capabilities: ["documents.extract".into()].into(),
|
|
950
|
-
workload_kinds: BTreeSet::new(),
|
|
951
|
-
resource_profile: Default::default(),
|
|
952
|
-
labels: BTreeMap::new(),
|
|
953
|
-
capacity: WorkerCapacity {
|
|
954
|
-
concurrency: 2,
|
|
955
|
-
active: 0,
|
|
956
|
-
},
|
|
957
|
-
registered_by: Subject::Service("registrar".into()),
|
|
958
|
-
authorization_grant: grant.clone(),
|
|
959
|
-
registered_at: 10,
|
|
960
|
-
})
|
|
961
|
-
.unwrap();
|
|
962
|
-
mesh.heartbeat(
|
|
963
|
-
&worker.worker_id,
|
|
964
|
-
WorkerHeartbeat {
|
|
965
|
-
worker_id: worker.worker_id.clone(),
|
|
966
|
-
version: "1".into(),
|
|
967
|
-
capability_version: 1,
|
|
968
|
-
capacity: WorkerCapacity {
|
|
969
|
-
concurrency: 2,
|
|
970
|
-
active: 0,
|
|
971
|
-
},
|
|
972
|
-
health: WorkerHealth::Healthy,
|
|
973
|
-
timestamp: 11,
|
|
974
|
-
running_claims: BTreeSet::new(),
|
|
975
|
-
},
|
|
976
|
-
2,
|
|
977
|
-
)
|
|
978
|
-
.unwrap();
|
|
979
|
-
let subject = grant.grant.subject.clone();
|
|
980
|
-
let create = |key: &str| CreateWorkload {
|
|
981
|
-
tenant_id: "t".into(),
|
|
982
|
-
application_id: "a".into(),
|
|
983
|
-
environment: "production".into(),
|
|
984
|
-
definition_id: "extract".into(),
|
|
985
|
-
definition_revision: "r".into(),
|
|
986
|
-
trigger: "manual".into(),
|
|
987
|
-
input: serde_json::json!({"file": key}),
|
|
988
|
-
requested_by: subject.clone(),
|
|
989
|
-
session_id: None,
|
|
990
|
-
authorization_snapshot: grant.clone(),
|
|
991
|
-
capability_snapshot: ["documents.extract".into()].into(),
|
|
992
|
-
connection_scopes: BTreeSet::new(),
|
|
993
|
-
priority: 0,
|
|
994
|
-
idempotency_key: key.into(),
|
|
995
|
-
retry_policy: WorkloadRetryPolicy::default(),
|
|
996
|
-
timeouts: WorkloadTimeouts::default(),
|
|
997
|
-
dependencies: vec![],
|
|
998
|
-
not_before: None,
|
|
999
|
-
created_at: 11,
|
|
1000
|
-
};
|
|
1001
|
-
let mut claims = vec![];
|
|
1002
|
-
for key in ["first", "second"] {
|
|
1003
|
-
let work = workloads.create(create(key)).unwrap();
|
|
1004
|
-
mesh.place_and_claim(
|
|
1005
|
-
&mut workloads,
|
|
1006
|
-
&work.workload_id,
|
|
1007
|
-
&worker.worker_id,
|
|
1008
|
-
12,
|
|
1009
|
-
10_000,
|
|
1010
|
-
)
|
|
1011
|
-
.unwrap();
|
|
1012
|
-
claims.push(
|
|
1013
|
-
workloads
|
|
1014
|
-
.get(&work.workload_id)
|
|
1015
|
-
.unwrap()
|
|
1016
|
-
.current_claim
|
|
1017
|
-
.clone()
|
|
1018
|
-
.unwrap()
|
|
1019
|
-
.claim_id,
|
|
1020
|
-
);
|
|
1021
|
-
}
|
|
1022
|
-
assert_eq!(mesh.workers[&worker.worker_id].capacity.active, 2);
|
|
1023
|
-
// A completion retried after a lost response releases the same claim again.
|
|
1024
|
-
mesh.release_claim(&worker.worker_id, &claims[0]).unwrap();
|
|
1025
|
-
mesh.release_claim(&worker.worker_id, &claims[0]).unwrap();
|
|
1026
|
-
let state = &mesh.workers[&worker.worker_id];
|
|
1027
|
-
assert_eq!(state.claims.len(), 1);
|
|
1028
|
-
assert_eq!(state.capacity.active, 1, "the second claim is still held");
|
|
1029
|
-
let _ = fs::remove_file(m);
|
|
1030
|
-
let _ = fs::remove_file(wpath);
|
|
1031
|
-
}
|
|
1032
|
-
#[test]
|
|
1033
|
-
fn a_claim_recovered_by_another_writer_is_still_released() {
|
|
1034
|
-
let (m, wpath, mut mesh, mut workloads, grant) = setup("release-raced");
|
|
1035
|
-
let worker = register(&mut mesh, grant.clone());
|
|
1036
|
-
let heartbeat = |timestamp| WorkerHeartbeat {
|
|
1037
|
-
worker_id: worker.worker_id.clone(),
|
|
1038
|
-
version: "1".into(),
|
|
1039
|
-
capability_version: 1,
|
|
1040
|
-
capacity: WorkerCapacity {
|
|
1041
|
-
concurrency: 1,
|
|
1042
|
-
active: 0,
|
|
1043
|
-
},
|
|
1044
|
-
health: WorkerHealth::Healthy,
|
|
1045
|
-
timestamp,
|
|
1046
|
-
running_claims: BTreeSet::new(),
|
|
1047
|
-
};
|
|
1048
|
-
mesh.heartbeat(&worker.worker_id, heartbeat(11), 2).unwrap();
|
|
1049
|
-
let subject = grant.grant.subject.clone();
|
|
1050
|
-
let create = |key: &str| CreateWorkload {
|
|
1051
|
-
tenant_id: "t".into(),
|
|
1052
|
-
application_id: "a".into(),
|
|
1053
|
-
environment: "production".into(),
|
|
1054
|
-
definition_id: "extract".into(),
|
|
1055
|
-
definition_revision: "r".into(),
|
|
1056
|
-
trigger: "manual".into(),
|
|
1057
|
-
input: serde_json::json!({"file": key}),
|
|
1058
|
-
requested_by: subject.clone(),
|
|
1059
|
-
session_id: None,
|
|
1060
|
-
authorization_snapshot: grant.clone(),
|
|
1061
|
-
capability_snapshot: ["documents.extract".into()].into(),
|
|
1062
|
-
connection_scopes: BTreeSet::new(),
|
|
1063
|
-
priority: 0,
|
|
1064
|
-
idempotency_key: key.into(),
|
|
1065
|
-
retry_policy: WorkloadRetryPolicy::default(),
|
|
1066
|
-
timeouts: WorkloadTimeouts::default(),
|
|
1067
|
-
dependencies: vec![],
|
|
1068
|
-
not_before: None,
|
|
1069
|
-
created_at: 11,
|
|
1070
|
-
};
|
|
1071
|
-
let first = workloads.create(create("first")).unwrap();
|
|
1072
|
-
let second = workloads.create(create("second")).unwrap();
|
|
1073
|
-
mesh.place_and_claim(
|
|
1074
|
-
&mut workloads,
|
|
1075
|
-
&first.workload_id,
|
|
1076
|
-
&worker.worker_id,
|
|
1077
|
-
12,
|
|
1078
|
-
1000,
|
|
1079
|
-
)
|
|
1080
|
-
.unwrap();
|
|
1081
|
-
assert_eq!(mesh.workers[&worker.worker_id].capacity.active, 1);
|
|
1082
|
-
// Another writer of the same store recovers the work first.
|
|
1083
|
-
let mut other = WorkloadStore::from_db(workloads.db_for_tests()).unwrap();
|
|
1084
|
-
assert_eq!(
|
|
1085
|
-
other.recover_expired(14).unwrap(),
|
|
1086
|
-
vec![first.workload_id.clone()]
|
|
1087
|
-
);
|
|
1088
|
-
// This node's pass loses the guarded commit, adopts the committed
|
|
1089
|
-
// workload, does not report it as its own recovery, and still frees the
|
|
1090
|
-
// worker, whose claim is no longer the workload's.
|
|
1091
|
-
assert!(mesh
|
|
1092
|
-
.recover_expired_work(14, &mut workloads)
|
|
1093
|
-
.unwrap()
|
|
1094
|
-
.is_empty());
|
|
1095
|
-
let recovered = workloads.get(&first.workload_id).unwrap();
|
|
1096
|
-
assert_eq!(recovered.state, crate::workload::WorkloadState::Ready);
|
|
1097
|
-
assert_eq!(
|
|
1098
|
-
recovered
|
|
1099
|
-
.execution_history
|
|
1100
|
-
.iter()
|
|
1101
|
-
.filter(|event| event.to == crate::workload::WorkloadState::Recovering)
|
|
1102
|
-
.count(),
|
|
1103
|
-
1
|
|
1104
|
-
);
|
|
1105
|
-
assert!(mesh.workers[&worker.worker_id].claims.is_empty());
|
|
1106
|
-
assert_eq!(mesh.workers[&worker.worker_id].capacity.active, 0);
|
|
1107
|
-
let _ = second;
|
|
1108
|
-
let _ = fs::remove_file(m);
|
|
1109
|
-
let _ = fs::remove_file(wpath);
|
|
1110
|
-
}
|
|
1111
|
-
#[test]
|
|
1112
|
-
fn recovering_an_expired_claim_releases_it_from_a_live_worker() {
|
|
1113
|
-
let (m, wpath, mut mesh, mut workloads, grant) = setup("release-expired");
|
|
1114
|
-
let worker = register(&mut mesh, grant.clone());
|
|
1115
|
-
let heartbeat = |timestamp| WorkerHeartbeat {
|
|
1116
|
-
worker_id: worker.worker_id.clone(),
|
|
1117
|
-
version: "1".into(),
|
|
1118
|
-
capability_version: 1,
|
|
1119
|
-
capacity: WorkerCapacity {
|
|
1120
|
-
concurrency: 1,
|
|
1121
|
-
active: 0,
|
|
1122
|
-
},
|
|
1123
|
-
health: WorkerHealth::Healthy,
|
|
1124
|
-
timestamp,
|
|
1125
|
-
running_claims: BTreeSet::new(),
|
|
1126
|
-
};
|
|
1127
|
-
mesh.heartbeat(&worker.worker_id, heartbeat(11), 2).unwrap();
|
|
1128
|
-
let subject = grant.grant.subject.clone();
|
|
1129
|
-
let create = |key: &str| CreateWorkload {
|
|
1130
|
-
tenant_id: "t".into(),
|
|
1131
|
-
application_id: "a".into(),
|
|
1132
|
-
environment: "production".into(),
|
|
1133
|
-
definition_id: "extract".into(),
|
|
1134
|
-
definition_revision: "r".into(),
|
|
1135
|
-
trigger: "manual".into(),
|
|
1136
|
-
input: serde_json::json!({"file": key}),
|
|
1137
|
-
requested_by: subject.clone(),
|
|
1138
|
-
session_id: None,
|
|
1139
|
-
authorization_snapshot: grant.clone(),
|
|
1140
|
-
capability_snapshot: ["documents.extract".into()].into(),
|
|
1141
|
-
connection_scopes: BTreeSet::new(),
|
|
1142
|
-
priority: 0,
|
|
1143
|
-
idempotency_key: key.into(),
|
|
1144
|
-
retry_policy: WorkloadRetryPolicy::default(),
|
|
1145
|
-
timeouts: WorkloadTimeouts::default(),
|
|
1146
|
-
dependencies: vec![],
|
|
1147
|
-
not_before: None,
|
|
1148
|
-
created_at: 11,
|
|
1149
|
-
};
|
|
1150
|
-
let first = workloads.create(create("first")).unwrap();
|
|
1151
|
-
let second = workloads.create(create("second")).unwrap();
|
|
1152
|
-
mesh.place_and_claim(
|
|
1153
|
-
&mut workloads,
|
|
1154
|
-
&first.workload_id,
|
|
1155
|
-
&worker.worker_id,
|
|
1156
|
-
12,
|
|
1157
|
-
1000,
|
|
1158
|
-
)
|
|
1159
|
-
.unwrap();
|
|
1160
|
-
assert_eq!(mesh.workers[&worker.worker_id].capacity.active, 1);
|
|
1161
|
-
// The lease expired at 13. The server's recovery is claim recovery: it
|
|
1162
|
-
// recovers the work and frees the worker, and does not judge liveness.
|
|
1163
|
-
let recovered = mesh.recover_expired_work(14, &mut workloads).unwrap();
|
|
1164
|
-
assert_eq!(recovered, vec![first.workload_id.clone()]);
|
|
1165
|
-
let live = &mesh.workers[&worker.worker_id];
|
|
1166
|
-
assert!(live.claims.is_empty());
|
|
1167
|
-
assert_eq!(live.capacity.active, 0);
|
|
1168
|
-
assert_eq!(live.lifecycle, WorkerLifecycle::Ready);
|
|
1169
|
-
assert_eq!(
|
|
1170
|
-
workloads.get(&first.workload_id).unwrap().state,
|
|
1171
|
-
crate::workload::WorkloadState::Ready
|
|
1172
|
-
);
|
|
1173
|
-
// Its capacity is usable again.
|
|
1174
|
-
mesh.place_and_claim(
|
|
1175
|
-
&mut workloads,
|
|
1176
|
-
&second.workload_id,
|
|
1177
|
-
&worker.worker_id,
|
|
1178
|
-
15,
|
|
1179
|
-
10_000,
|
|
1180
|
-
)
|
|
1181
|
-
.unwrap();
|
|
1182
|
-
// Long past its heartbeat timeout, claim recovery still leaves the
|
|
1183
|
-
// worker's lifecycle alone; only `detect_failures` marks it offline.
|
|
1184
|
-
mesh.recover_expired_work(1_000, &mut workloads).unwrap();
|
|
1185
|
-
assert_ne!(
|
|
1186
|
-
mesh.workers[&worker.worker_id].lifecycle,
|
|
1187
|
-
WorkerLifecycle::Offline
|
|
1188
|
-
);
|
|
1189
|
-
assert!(mesh.workers[&worker.worker_id].claims.is_empty());
|
|
1190
|
-
let _ = fs::remove_file(m);
|
|
1191
|
-
let _ = fs::remove_file(wpath);
|
|
1192
|
-
}
|
|
1193
|
-
#[test]
|
|
1194
|
-
fn placement_claim_fencing_failure_and_recovery() {
|
|
1195
|
-
let (m, wpath, mut mesh, mut workloads, grant) = setup("claim");
|
|
1196
|
-
let worker = register(&mut mesh, grant.clone());
|
|
1197
|
-
mesh.heartbeat(
|
|
1198
|
-
&worker.worker_id,
|
|
1199
|
-
WorkerHeartbeat {
|
|
1200
|
-
worker_id: worker.worker_id.clone(),
|
|
1201
|
-
version: "1".into(),
|
|
1202
|
-
capability_version: 1,
|
|
1203
|
-
capacity: WorkerCapacity {
|
|
1204
|
-
concurrency: 1,
|
|
1205
|
-
active: 0,
|
|
1206
|
-
},
|
|
1207
|
-
health: WorkerHealth::Healthy,
|
|
1208
|
-
timestamp: 11,
|
|
1209
|
-
running_claims: BTreeSet::new(),
|
|
1210
|
-
},
|
|
1211
|
-
2,
|
|
1212
|
-
)
|
|
1213
|
-
.unwrap();
|
|
1214
|
-
let subject = grant.grant.subject.clone();
|
|
1215
|
-
let workload = workloads
|
|
1216
|
-
.create(CreateWorkload {
|
|
1217
|
-
tenant_id: "t".into(),
|
|
1218
|
-
application_id: "a".into(),
|
|
1219
|
-
environment: "production".into(),
|
|
1220
|
-
definition_id: "extract".into(),
|
|
1221
|
-
definition_revision: "r".into(),
|
|
1222
|
-
trigger: "manual".into(),
|
|
1223
|
-
input: serde_json::json!({"file":"artifact://x"}),
|
|
1224
|
-
requested_by: subject,
|
|
1225
|
-
session_id: None,
|
|
1226
|
-
authorization_snapshot: grant,
|
|
1227
|
-
capability_snapshot: ["documents.extract".into()].into(),
|
|
1228
|
-
connection_scopes: BTreeSet::new(),
|
|
1229
|
-
priority: 0,
|
|
1230
|
-
idempotency_key: "x".into(),
|
|
1231
|
-
retry_policy: WorkloadRetryPolicy::default(),
|
|
1232
|
-
timeouts: WorkloadTimeouts::default(),
|
|
1233
|
-
dependencies: vec![],
|
|
1234
|
-
not_before: None,
|
|
1235
|
-
created_at: 11,
|
|
1236
|
-
})
|
|
1237
|
-
.unwrap();
|
|
1238
|
-
let claim = mesh
|
|
1239
|
-
.place_and_claim(
|
|
1240
|
-
&mut workloads,
|
|
1241
|
-
&workload.workload_id,
|
|
1242
|
-
&worker.worker_id,
|
|
1243
|
-
12,
|
|
1244
|
-
1000,
|
|
1245
|
-
)
|
|
1246
|
-
.unwrap();
|
|
1247
|
-
assert!(validate_mesh_claim(
|
|
1248
|
-
&claim,
|
|
1249
|
-
mesh.workers.get(&worker.worker_id).unwrap(),
|
|
1250
|
-
workloads.get(&workload.workload_id).unwrap(),
|
|
1251
|
-
12
|
|
1252
|
-
)
|
|
1253
|
-
.is_ok());
|
|
1254
|
-
mesh.detect_failures(50, &mut workloads).unwrap();
|
|
1255
|
-
assert_eq!(
|
|
1256
|
-
validate_mesh_claim(
|
|
1257
|
-
&claim,
|
|
1258
|
-
mesh.workers.get(&worker.worker_id).unwrap(),
|
|
1259
|
-
workloads.get(&workload.workload_id).unwrap(),
|
|
1260
|
-
50
|
|
1261
|
-
)
|
|
1262
|
-
.unwrap_err()
|
|
1263
|
-
.code,
|
|
1264
|
-
"MESH_STALE_FENCE"
|
|
1265
|
-
);
|
|
1266
|
-
let _ = fs::remove_file(m);
|
|
1267
|
-
let _ = fs::remove_file(wpath);
|
|
1268
|
-
}
|
|
1269
|
-
}
|