@@ -11,7 +11,10 @@ use health_reporter::{HealthReporter, State};
11
11
use hyper:: header:: CONTENT_TYPE ;
12
12
use hyper:: service:: make_service_fn;
13
13
use hyper:: { self , Body , Method , Request , Response , StatusCode } ;
14
- use readyset_alloc:: { dump_stats, print_memory_and_per_thread_stats} ;
14
+ use readyset_alloc:: {
15
+ activate_prof, deactivate_prof, dump_prof_to_string, dump_stats,
16
+ print_memory_and_per_thread_stats,
17
+ } ;
15
18
use readyset_client:: metrics:: recorded;
16
19
use readyset_errors:: ReadySetError ;
17
20
use readyset_util:: shutdown:: ShutdownReceiver ;
@@ -112,7 +115,7 @@ impl Service<Request<Body>> for NoriaServerHttpRouter {
112
115
let contents = match bincode:: deserialize ( & body) {
113
116
Err ( _) => {
114
117
return Ok ( res
115
- . status ( 400 )
118
+ . status ( StatusCode :: BAD_REQUEST )
116
119
. header ( CONTENT_TYPE , "text/plain" )
117
120
. body ( hyper:: Body :: from (
118
121
"body cannot be deserialized into failpoint name and action" ,
@@ -123,7 +126,7 @@ impl Service<Request<Body>> for NoriaServerHttpRouter {
123
126
} ;
124
127
let ( name, action) : ( String , String ) = contents;
125
128
let resp = res
126
- . status ( 200 )
129
+ . status ( StatusCode :: OK )
127
130
. header ( CONTENT_TYPE , "text/plain" )
128
131
. body ( hyper:: Body :: from (
129
132
:: bincode:: serialize ( & fail:: cfg ( name, & action) ) . unwrap ( ) ,
@@ -147,7 +150,7 @@ impl Service<Request<Body>> for NoriaServerHttpRouter {
147
150
let res = match render {
148
151
Some ( metrics) => res. body ( hyper:: Body :: from ( metrics) ) ,
149
152
None => res
150
- . status ( 404 )
153
+ . status ( StatusCode :: NOT_FOUND )
151
154
. body ( hyper:: Body :: from ( "Prometheus metrics were not enabled. To fix this, run Noria with --prometheus-metrics" . to_string ( ) ) ) ,
152
155
} ;
153
156
Box :: pin ( async move { Ok ( res. unwrap ( ) ) } )
@@ -158,11 +161,11 @@ impl Service<Request<Body>> for NoriaServerHttpRouter {
158
161
let body = format ! ( "Server is in {} state" , & state) . into ( ) ;
159
162
let res = match state {
160
163
State :: Healthy | State :: ShuttingDown => res
161
- . status ( 200 )
164
+ . status ( StatusCode :: OK )
162
165
. header ( CONTENT_TYPE , "text/plain" )
163
166
. body ( body) ,
164
167
_ => res
165
- . status ( 500 )
168
+ . status ( StatusCode :: INTERNAL_SERVER_ERROR )
166
169
. header ( CONTENT_TYPE , "text/plain" )
167
170
. body ( body) ,
168
171
} ;
@@ -177,7 +180,7 @@ impl Service<Request<Body>> for NoriaServerHttpRouter {
177
180
. header ( CONTENT_TYPE , "application/json" )
178
181
. body ( hyper:: Body :: from ( metrics) ) ,
179
182
None => res
180
- . status ( 404 )
183
+ . status ( StatusCode :: NOT_FOUND )
181
184
. header ( CONTENT_TYPE , "text/plain" )
182
185
. body ( hyper:: Body :: from ( "Noria metrics were not enabled. To fix this, run Noria with --noria-metrics" . to_string ( ) ) ) ,
183
186
} ;
@@ -242,35 +245,87 @@ impl Service<Request<Body>> for NoriaServerHttpRouter {
242
245
}
243
246
// Returns a summary of memory usage for the entire process and per-thread memory usage
244
247
( & Method :: POST , "/memory_stats" ) => {
245
- let res =
246
- match print_memory_and_per_thread_stats ( ) {
247
- Ok ( stats) => res
248
- . status ( 200 )
249
- . header ( CONTENT_TYPE , "text/plain" )
250
- . body ( hyper:: Body :: from ( stats) ) ,
251
- Err ( e) => res. status ( 500 ) . header ( CONTENT_TYPE , "text/plain" ) . body (
252
- hyper:: Body :: from ( format ! ( "Error fetching memory stats: {e}" ) ) ,
253
- ) ,
254
- } ;
248
+ let res = match print_memory_and_per_thread_stats ( ) {
249
+ Ok ( stats) => res
250
+ . status ( StatusCode :: OK )
251
+ . header ( CONTENT_TYPE , "text/plain" )
252
+ . body ( hyper:: Body :: from ( stats) ) ,
253
+ Err ( e) => res
254
+ . status ( StatusCode :: INTERNAL_SERVER_ERROR )
255
+ . header ( CONTENT_TYPE , "text/plain" )
256
+ . body ( hyper:: Body :: from ( format ! (
257
+ "Error fetching memory stats: {e}"
258
+ ) ) ) ,
259
+ } ;
255
260
256
261
Box :: pin ( async move { Ok ( res. unwrap ( ) ) } )
257
262
}
258
263
// Returns a large dump of jemalloc debugging information along with per-thread
259
264
// memory stats
260
265
( & Method :: POST , "/memory_stats_verbose" ) => {
261
- let res =
262
- match dump_stats ( ) {
263
- Ok ( stats) => res
264
- . status ( 200 )
265
- . header ( CONTENT_TYPE , "text/plain" )
266
- . body ( hyper:: Body :: from ( stats) ) ,
267
- Err ( e) => res. status ( 500 ) . header ( CONTENT_TYPE , "text/plain" ) . body (
268
- hyper:: Body :: from ( format ! ( "Error fetching memory stats: {e}" ) ) ,
269
- ) ,
270
- } ;
266
+ let res = match dump_stats ( ) {
267
+ Ok ( stats) => res
268
+ . status ( StatusCode :: OK )
269
+ . header ( CONTENT_TYPE , "text/plain" )
270
+ . body ( hyper:: Body :: from ( stats) ) ,
271
+ Err ( e) => res
272
+ . status ( StatusCode :: INTERNAL_SERVER_ERROR )
273
+ . header ( CONTENT_TYPE , "text/plain" )
274
+ . body ( hyper:: Body :: from ( format ! (
275
+ "Error fetching memory stats: {e}"
276
+ ) ) ) ,
277
+ } ;
271
278
272
279
Box :: pin ( async move { Ok ( res. unwrap ( ) ) } )
273
280
}
281
+ // Turns on jemalloc's profiler
282
+ ( & Method :: POST , "/jemalloc/profiling/activate" ) => {
283
+ let res = match activate_prof ( ) {
284
+ Ok ( _) => res
285
+ . status ( StatusCode :: OK )
286
+ . header ( CONTENT_TYPE , "text/plain" )
287
+ . body ( hyper:: Body :: from ( "Memory profiling activated" ) ) ,
288
+ Err ( e) => res
289
+ . status ( StatusCode :: INTERNAL_SERVER_ERROR )
290
+ . header ( CONTENT_TYPE , "text/plain" )
291
+ . body ( hyper:: Body :: from ( format ! (
292
+ "Error activating memory profiling: {e}"
293
+ ) ) ) ,
294
+ } ;
295
+ Box :: pin ( async move { Ok ( res. unwrap ( ) ) } )
296
+ }
297
+ // Disables jemalloc's profiler
298
+ ( & Method :: POST , "/jemalloc/profiling/deactivate" ) => {
299
+ let res = match deactivate_prof ( ) {
300
+ Ok ( _) => res
301
+ . status ( StatusCode :: OK )
302
+ . header ( CONTENT_TYPE , "text/plain" )
303
+ . body ( hyper:: Body :: from ( "Memory profiling deactivated" ) ) ,
304
+ Err ( e) => res
305
+ . status ( StatusCode :: INTERNAL_SERVER_ERROR )
306
+ . header ( CONTENT_TYPE , "text/plain" )
307
+ . body ( hyper:: Body :: from ( format ! (
308
+ "Error deactivating memory profiling: {e}"
309
+ ) ) ) ,
310
+ } ;
311
+ Box :: pin ( async move { Ok ( res. unwrap ( ) ) } )
312
+ }
313
+ // Returns the current jemalloc profiler output
314
+ ( & Method :: GET , "/jemalloc/profiling/dump" ) => Box :: pin ( async move {
315
+ let res = match dump_prof_to_string ( ) . await {
316
+ Ok ( dump) => res
317
+ . status ( StatusCode :: OK )
318
+ . header ( CONTENT_TYPE , "text/plain" )
319
+ . body ( hyper:: Body :: from ( dump) ) ,
320
+ Err ( e) => res
321
+ . status ( StatusCode :: INTERNAL_SERVER_ERROR )
322
+ . header ( CONTENT_TYPE , "text/plain" )
323
+ . body ( hyper:: Body :: from ( format ! (
324
+ "Error dumping profiling output: {e}"
325
+ ) ) ) ,
326
+ } ;
327
+ Ok ( res. unwrap ( ) )
328
+ } ) ,
274
329
_ => {
275
330
metrics:: counter!( recorded:: SERVER_CONTROLLER_REQUESTS ) . increment ( 1 ) ;
276
331
0 commit comments