-
-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathquery_server.rs
383 lines (362 loc) · 16.7 KB
/
query_server.rs
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
* Parseable Server (C) 2022 - 2024 Parseable, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
use crate::correlation::CORRELATIONS;
use crate::handlers::airplane;
use crate::handlers::http::logstream::create_internal_stream_if_not_exists;
use crate::handlers::http::middleware::{DisAllowRootUser, RouteExt};
use crate::handlers::http::{self, role};
use crate::handlers::http::{base_path, cluster};
use crate::handlers::http::{logstream, MAX_EVENT_PAYLOAD_SIZE};
use crate::hottier::HotTierManager;
use crate::rbac::role::Action;
use crate::sync;
use crate::users::dashboards::DASHBOARDS;
use crate::users::filters::FILTERS;
use crate::{analytics, metrics, migration, storage};
use actix_web::web::{resource, ServiceConfig};
use actix_web::{web, Scope};
use async_trait::async_trait;
use bytes::Bytes;
use tokio::sync::oneshot;
use tracing::error;
use crate::{option::CONFIG, ParseableServer};
use super::query::{querier_ingest, querier_logstream, querier_rbac, querier_role};
use super::server::Server;
use super::OpenIdClient;
pub struct QueryServer;
#[async_trait]
impl ParseableServer for QueryServer {
// configure the api routes
fn configure_routes(config: &mut ServiceConfig, oidc_client: Option<OpenIdClient>) {
config
.service(
web::scope(&base_path())
.service(Server::get_correlation_webscope())
.service(Server::get_query_factory())
.service(Server::get_liveness_factory())
.service(Server::get_readiness_factory())
.service(Server::get_about_factory())
.service(Self::get_logstream_webscope())
.service(Self::get_user_webscope())
.service(Server::get_dashboards_webscope())
.service(Server::get_filters_webscope())
.service(Server::get_llm_webscope())
.service(Server::get_oauth_webscope(oidc_client))
.service(Self::get_user_role_webscope())
.service(Server::get_counts_webscope())
.service(Server::get_metrics_webscope())
.service(Self::get_cluster_web_scope()),
)
.service(Server::get_generated());
}
async fn load_metadata(&self) -> anyhow::Result<Option<Bytes>> {
// parseable can't use local storage for persistence when running a distributed setup
if CONFIG.get_storage_mode_string() == "Local drive" {
return Err(anyhow::anyhow!(
"This instance of the Parseable server has been configured to run in a distributed setup, it doesn't support local storage.",
));
}
migration::run_file_migration(&CONFIG).await?;
let parseable_json = CONFIG.validate_storage().await?;
migration::run_metadata_migration(&CONFIG, &parseable_json).await?;
Ok(parseable_json)
}
/// initialize the server, run migrations as needed and start an instance
async fn init(&self, shutdown_rx: oneshot::Receiver<()>) -> anyhow::Result<()> {
let prometheus = metrics::build_metrics_handler();
CONFIG.storage().register_store_metrics(&prometheus);
migration::run_migration(&CONFIG).await?;
//create internal stream at server start
create_internal_stream_if_not_exists().await?;
if let Err(e) = CORRELATIONS.load().await {
error!("{e}");
}
FILTERS.load().await?;
DASHBOARDS.load().await?;
// track all parquet files already in the data directory
storage::retention::load_retention_from_global();
metrics::init_system_metrics_scheduler().await?;
cluster::init_cluster_metrics_scheduler().await?;
// all internal data structures populated now.
// start the analytics scheduler if enabled
if CONFIG.options.send_analytics {
analytics::init_analytics_scheduler()?;
}
if let Some(hot_tier_manager) = HotTierManager::global() {
hot_tier_manager.put_internal_stream_hot_tier().await?;
hot_tier_manager.download_from_s3()?;
};
let (localsync_handler, mut localsync_outbox, localsync_inbox) =
sync::run_local_sync().await;
let (mut remote_sync_handler, mut remote_sync_outbox, mut remote_sync_inbox) =
sync::object_store_sync().await;
tokio::spawn(airplane::server());
let app = self.start(shutdown_rx, prometheus, CONFIG.options.openid());
tokio::pin!(app);
loop {
tokio::select! {
e = &mut app => {
// actix server finished .. stop other threads and stop the server
remote_sync_inbox.send(()).unwrap_or(());
localsync_inbox.send(()).unwrap_or(());
if let Err(e) = localsync_handler.await {
error!("Error joining localsync_handler: {:?}", e);
}
if let Err(e) = remote_sync_handler.await {
error!("Error joining remote_sync_handler: {:?}", e);
}
return e
},
_ = &mut localsync_outbox => {
// crash the server if localsync fails for any reason
// panic!("Local Sync thread died. Server will fail now!")
return Err(anyhow::Error::msg("Failed to sync local data to drive. Please restart the Parseable server.\n\nJoin us on Parseable Slack if the issue persists after restart : https://launchpass.com/parseable"))
},
_ = &mut remote_sync_outbox => {
// remote_sync failed, this is recoverable by just starting remote_sync thread again
if let Err(e) = remote_sync_handler.await {
error!("Error joining remote_sync_handler: {:?}", e);
}
(remote_sync_handler, remote_sync_outbox, remote_sync_inbox) = sync::object_store_sync().await;
}
};
}
}
}
impl QueryServer {
// get the role webscope
pub fn get_user_role_webscope() -> Scope {
web::scope("/role")
// GET Role List
.service(resource("").route(web::get().to(role::list).authorize(Action::ListRole)))
.service(
// PUT and GET Default Role
resource("/default")
.route(web::put().to(role::put_default).authorize(Action::PutRole))
.route(web::get().to(role::get_default).authorize(Action::GetRole)),
)
.service(
// PUT, GET, DELETE Roles
resource("/{name}")
.route(web::put().to(querier_role::put).authorize(Action::PutRole))
.route(web::delete().to(role::delete).authorize(Action::DeleteRole))
.route(web::get().to(role::get).authorize(Action::GetRole)),
)
}
// get the user webscope
pub fn get_user_webscope() -> Scope {
web::scope("/user")
.service(
web::resource("")
// GET /user => List all users
.route(
web::get()
.to(http::rbac::list_users)
.authorize(Action::ListUser),
),
)
.service(
web::resource("/{username}")
// PUT /user/{username} => Create a new user
.route(
web::post()
.to(querier_rbac::post_user)
.authorize(Action::PutUser),
)
// DELETE /user/{username} => Delete a user
.route(
web::delete()
.to(querier_rbac::delete_user)
.authorize(Action::DeleteUser),
)
.wrap(DisAllowRootUser),
)
.service(
web::resource("/{username}/role")
// PUT /user/{username}/roles => Put roles for user
.route(
web::put()
.to(querier_rbac::put_role)
.authorize(Action::PutUserRoles)
.wrap(DisAllowRootUser),
)
.route(
web::get()
.to(http::rbac::get_role)
.authorize_for_user(Action::GetUserRoles),
),
)
.service(
web::resource("/{username}/generate-new-password")
// POST /user/{username}/generate-new-password => reset password for this user
.route(
web::post()
.to(querier_rbac::post_gen_password)
.authorize(Action::PutUser)
.wrap(DisAllowRootUser),
),
)
}
// get the logstream web scope
pub fn get_logstream_webscope() -> Scope {
web::scope("/logstream")
.service(
// GET "/logstream" ==> Get list of all Log Streams on the server
web::resource("")
.route(web::get().to(logstream::list).authorize(Action::ListStream)),
)
.service(
web::scope("/schema/detect").service(
web::resource("")
// PUT "/logstream/{logstream}" ==> Create log stream
.route(
web::post()
.to(logstream::detect_schema)
.authorize(Action::DetectSchema),
),
),
)
.service(
web::scope("/{logstream}")
.service(
web::resource("")
// PUT "/logstream/{logstream}" ==> Create log stream
.route(
web::put()
.to(querier_logstream::put_stream)
.authorize_for_stream(Action::CreateStream),
)
// POST "/logstream/{logstream}" ==> Post logs to given log stream
.route(
web::post()
.to(querier_ingest::post_event)
.authorize_for_stream(Action::Ingest),
)
// DELETE "/logstream/{logstream}" ==> Delete log stream
.route(
web::delete()
.to(querier_logstream::delete)
.authorize_for_stream(Action::DeleteStream),
)
.app_data(web::JsonConfig::default().limit(MAX_EVENT_PAYLOAD_SIZE)),
)
.service(
// GET "/logstream/{logstream}/info" ==> Get info for given log stream
web::resource("/info").route(
web::get()
.to(logstream::get_stream_info)
.authorize_for_stream(Action::GetStreamInfo),
),
)
.service(
web::resource("/alert")
// PUT "/logstream/{logstream}/alert" ==> Set alert for given log stream
.route(
web::put()
.to(logstream::put_alert)
.authorize_for_stream(Action::PutAlert),
)
// GET "/logstream/{logstream}/alert" ==> Get alert for given log stream
.route(
web::get()
.to(logstream::get_alert)
.authorize_for_stream(Action::GetAlert),
),
)
.service(
// GET "/logstream/{logstream}/schema" ==> Get schema for given log stream
web::resource("/schema").route(
web::get()
.to(logstream::schema)
.authorize_for_stream(Action::GetSchema),
),
)
.service(
// GET "/logstream/{logstream}/stats" ==> Get stats for given log stream
web::resource("/stats").route(
web::get()
.to(querier_logstream::get_stats)
.authorize_for_stream(Action::GetStats),
),
)
.service(
web::resource("/retention")
// PUT "/logstream/{logstream}/retention" ==> Set retention for given logstream
.route(
web::put()
.to(logstream::put_retention)
.authorize_for_stream(Action::PutRetention),
)
// GET "/logstream/{logstream}/retention" ==> Get retention for given logstream
.route(
web::get()
.to(logstream::get_retention)
.authorize_for_stream(Action::GetRetention),
),
)
.service(
web::resource("/hottier")
// PUT "/logstream/{logstream}/hottier" ==> Set hottier for given logstream
.route(
web::put()
.to(logstream::put_stream_hot_tier)
.authorize_for_stream(Action::PutHotTierEnabled),
)
.route(
web::get()
.to(logstream::get_stream_hot_tier)
.authorize_for_stream(Action::GetHotTierEnabled),
)
.route(
web::delete()
.to(logstream::delete_stream_hot_tier)
.authorize_for_stream(Action::DeleteHotTierEnabled),
),
),
)
}
pub fn get_cluster_web_scope() -> actix_web::Scope {
web::scope("/cluster")
.service(
// GET "/cluster/info" ==> Get info of the cluster
web::resource("/info").route(
web::get()
.to(cluster::get_cluster_info)
.authorize(Action::ListCluster),
),
)
// GET "/cluster/metrics" ==> Get metrics of the cluster
.service(
web::resource("/metrics").route(
web::get()
.to(cluster::get_cluster_metrics)
.authorize(Action::ListClusterMetrics),
),
)
// DELETE "/cluster/{ingestor_domain:port}" ==> Delete an ingestor from the cluster
.service(
web::scope("/{ingestor}").service(
web::resource("").route(
web::delete()
.to(cluster::remove_ingestor)
.authorize(Action::Deleteingestor),
),
),
)
}
}