-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathserver.rs
More file actions
179 lines (162 loc) · 7.21 KB
/
Copy pathserver.rs
File metadata and controls
179 lines (162 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// SPDX-License-Identifier: BUSL-1.1
//! `NativeTestServer` — spawns a single-core NodeDB server with the native
//! (MessagePack/JSON) protocol listener bound to an ephemeral port.
use std::sync::Arc;
use std::time::Duration;
use nodedb::bridge::dispatch::Dispatcher;
use nodedb::config::auth::AuthMode;
use nodedb::control::server::listener::Listener;
use nodedb::control::state::SharedState;
use nodedb::data::executor::core_loop::CoreLoop;
use nodedb::event::{EventPlane, create_event_bus};
use nodedb::wal::WalManager;
/// A running native-protocol test server.
pub struct NativeTestServer {
pub addr: std::net::SocketAddr,
/// Shared Control-Plane state, exposed so tests can inspect the same
/// metrics and authorization stores used by the running server.
pub shared: Arc<SharedState>,
pub(super) shutdown_bus: nodedb::control::shutdown::ShutdownBus,
pub(super) poller_shutdown_tx: tokio::sync::watch::Sender<bool>,
pub(super) core_stop_tx: std::sync::mpsc::Sender<()>,
pub(super) _listener_handle: tokio::task::JoinHandle<()>,
pub(super) _poller_handle: tokio::task::JoinHandle<()>,
pub(super) _core_handle: tokio::task::JoinHandle<()>,
pub(super) _event_plane: EventPlane,
pub(super) _dir: tempfile::TempDir,
}
impl NativeTestServer {
/// Spawn a single-core NodeDB server with the native listener bound to
/// an ephemeral `127.0.0.1` port (trust-mode auth).
pub async fn start() -> Self {
Self::start_with_auth_mode(AuthMode::Trust).await
}
/// Spawn a single-core server that requires an explicit native Auth frame.
pub async fn start_authenticated() -> Self {
Self::start_with_auth_mode(AuthMode::Password).await
}
async fn start_with_auth_mode(auth_mode: AuthMode) -> Self {
let dir = tempfile::tempdir().expect("tempdir");
let wal_path = dir.path().join("test.wal");
let wal = Arc::new(WalManager::open_for_testing(&wal_path).expect("open wal"));
let (dispatcher, data_sides) = Dispatcher::new(1, 64);
let (event_producers, event_consumers) = create_event_bus(1);
// Use catalog-backed credential store (mirrors pgwire_harness::start)
// so DDL apply (`apply_locally_if_needed`) and planner reads
// (`OriginCatalog::get_collection`) resolve against a real catalog and
// collections created over the native protocol are visible.
let catalog_path = dir.path().join("system.redb");
let credential_store =
nodedb::control::security::credential::store::CredentialStore::open(&catalog_path)
.expect("open credential store");
let credentials = Arc::new(credential_store);
// Provision the harness superuser `nodedb` so Trust-mode strict
// identity resolution accepts the default test connection.
let _ = credentials.create_user(
"nodedb",
"nodedb",
nodedb::types::TenantId::new(1),
vec![nodedb::control::security::identity::Role::Superuser],
);
// Ensure the built-in `default` database (id 0) is present in the
// catalog so the default connection database works in tests.
let _ = credentials.catalog().bootstrap_default_database();
let shared = SharedState::new_with_credentials(dispatcher, Arc::clone(&wal), credentials)
.expect("build shared state");
let data_side = data_sides.into_iter().next().expect("data side");
let core_dir = dir.path().to_path_buf();
let event_producer = event_producers.into_iter().next().expect("event producer");
let core_array_catalog = shared.array_catalog.clone();
let (core_stop_tx, core_stop_rx) = std::sync::mpsc::channel::<()>();
let _core_handle = tokio::task::spawn_blocking(move || {
let mut core = CoreLoop::open_with_array_catalog(
0,
data_side.request_rx,
data_side.response_tx,
&core_dir,
std::sync::Arc::new(nodedb_types::OrdinalClock::new()),
core_array_catalog,
)
.expect("open core");
core.set_event_producer(event_producer);
while matches!(
core_stop_rx.try_recv(),
Err(std::sync::mpsc::TryRecvError::Empty)
) {
core.tick();
std::thread::sleep(Duration::from_millis(1));
}
});
let shared_poller = Arc::clone(&shared);
let (poller_shutdown_tx, mut poller_shutdown_rx) = tokio::sync::watch::channel(false);
let _poller_handle = tokio::spawn(async move {
loop {
shared_poller.poll_and_route_responses();
tokio::select! {
_ = tokio::time::sleep(Duration::from_millis(1)) => {}
_ = poller_shutdown_rx.changed() => break,
}
}
});
let watermark_store = Arc::new(
nodedb::event::watermark::WatermarkStore::open(dir.path()).expect("watermark"),
);
let trigger_dlq = Arc::new(std::sync::Mutex::new(
nodedb::event::trigger::TriggerDlq::open(dir.path()).expect("trigger dlq"),
));
let _event_plane = EventPlane::spawn(
event_consumers,
Arc::clone(&wal),
watermark_store,
Arc::clone(&shared),
trigger_dlq,
Arc::clone(&shared.cdc_router),
Arc::clone(&shared.shutdown),
);
let listener = Listener::bind("127.0.0.1:0".parse().expect("addr"))
.await
.expect("bind");
let addr = listener.local_addr();
let (shutdown_bus, _) =
nodedb::control::shutdown::ShutdownBus::new(Arc::clone(&shared.shutdown));
let shared_listener = Arc::clone(&shared);
let test_startup_gate = Arc::clone(&shared.startup);
let bus_listener = shutdown_bus.clone();
let _listener_handle = tokio::spawn(async move {
listener
.run(nodedb::control::server::listener::ListenerRunParams {
state: shared_listener,
auth_mode,
tls_acceptor: None,
conn_semaphore: Arc::new(tokio::sync::Semaphore::new(128)),
startup_gate: test_startup_gate,
bus: bus_listener,
admission: Arc::new(
nodedb::control::server::admission::AdmissionRegistry::new(),
),
})
.await
.expect("listener");
});
tokio::time::sleep(Duration::from_millis(50)).await;
Self {
addr,
shared: Arc::clone(&shared),
shutdown_bus,
poller_shutdown_tx,
core_stop_tx,
_listener_handle,
_poller_handle,
_core_handle,
_event_plane,
_dir: dir,
}
}
/// Shut down the server and give background tasks time to unwind.
pub async fn shutdown(self) {
self.shutdown_bus.initiate();
let _ = self.poller_shutdown_tx.send(true);
let _ = self.core_stop_tx.send(());
tokio::time::sleep(Duration::from_millis(50)).await;
}
}