Line data Source code
1 : //! Simple pub-sub based on grpc (tonic) and Tokio broadcast channel for storage
2 : //! nodes messaging.
3 : //!
4 : //! Subscriptions to 1) single timeline 2) all timelines are possible. We could
5 : //! add subscription to the set of timelines to save grpc streams, but testing
6 : //! shows many individual streams is also ok.
7 : //!
8 : //! Message is dropped if subscriber can't consume it, not affecting other
9 : //! subscribers.
10 : //!
11 : //! Only safekeeper message is supported, but it is not hard to add something
12 : //! else with generics.
13 : use clap::{command, Parser};
14 : use futures_core::Stream;
15 : use futures_util::StreamExt;
16 : use hyper::header::CONTENT_TYPE;
17 : use hyper::server::conn::AddrStream;
18 : use hyper::service::{make_service_fn, service_fn};
19 : use hyper::{Body, Method, StatusCode};
20 : use parking_lot::RwLock;
21 : use std::collections::HashMap;
22 : use std::convert::Infallible;
23 : use std::net::SocketAddr;
24 : use std::pin::Pin;
25 : use std::sync::Arc;
26 : use std::time::Duration;
27 : use tokio::sync::broadcast;
28 : use tokio::sync::broadcast::error::RecvError;
29 : use tokio::time;
30 : use tonic::codegen::Service;
31 : use tonic::transport::server::Connected;
32 : use tonic::Code;
33 : use tonic::{Request, Response, Status};
34 : use tracing::*;
35 : use utils::signals::ShutdownSignals;
36 :
37 : use metrics::{Encoder, TextEncoder};
38 : use storage_broker::metrics::{
39 : BROADCASTED_MESSAGES_TOTAL, BROADCAST_DROPPED_MESSAGES_TOTAL, NUM_PUBS, NUM_SUBS_ALL,
40 : NUM_SUBS_TIMELINE, PROCESSED_MESSAGES_TOTAL, PUBLISHED_ONEOFF_MESSAGES_TOTAL,
41 : };
42 : use storage_broker::proto::broker_service_server::{BrokerService, BrokerServiceServer};
43 : use storage_broker::proto::subscribe_safekeeper_info_request::SubscriptionKey as ProtoSubscriptionKey;
44 : use storage_broker::proto::{
45 : FilterTenantTimelineId, MessageType, SafekeeperDiscoveryRequest, SafekeeperDiscoveryResponse,
46 : SafekeeperTimelineInfo, SubscribeByFilterRequest, SubscribeSafekeeperInfoRequest, TypedMessage,
47 : };
48 : use storage_broker::{
49 : parse_proto_ttid, EitherBody, DEFAULT_KEEPALIVE_INTERVAL, DEFAULT_LISTEN_ADDR,
50 : };
51 : use utils::id::TenantTimelineId;
52 : use utils::logging::{self, LogFormat};
53 : use utils::sentry_init::init_sentry;
54 : use utils::{project_build_tag, project_git_version};
55 :
56 : project_git_version!(GIT_VERSION);
57 : project_build_tag!(BUILD_TAG);
58 :
59 : const DEFAULT_CHAN_SIZE: usize = 32;
60 : const DEFAULT_ALL_KEYS_CHAN_SIZE: usize = 16384;
61 :
62 0 : #[derive(Parser, Debug)]
63 : #[command(version = GIT_VERSION, about = "Broker for neon storage nodes communication", long_about = None)]
64 : struct Args {
65 : /// Endpoint to listen on.
66 : #[arg(short, long, default_value = DEFAULT_LISTEN_ADDR)]
67 0 : listen_addr: SocketAddr,
68 : /// Size of the queue to the per timeline subscriber.
69 0 : #[arg(long, default_value_t = DEFAULT_CHAN_SIZE)]
70 0 : timeline_chan_size: usize,
71 : /// Size of the queue to the all keys subscriber.
72 0 : #[arg(long, default_value_t = DEFAULT_ALL_KEYS_CHAN_SIZE)]
73 0 : all_keys_chan_size: usize,
74 : /// HTTP/2 keepalive interval.
75 : #[arg(long, value_parser= humantime::parse_duration, default_value = DEFAULT_KEEPALIVE_INTERVAL)]
76 0 : http2_keepalive_interval: Duration,
77 : /// Format for logging, either 'plain' or 'json'.
78 : #[arg(long, default_value = "plain")]
79 0 : log_format: String,
80 : }
81 :
82 : /// Id of publisher for registering in maps
83 : type PubId = u64;
84 :
85 : /// Id of subscriber for registering in maps
86 : type SubId = u64;
87 :
88 : /// Single enum type for all messages.
89 : #[derive(Clone, Debug, PartialEq)]
90 : #[allow(clippy::enum_variant_names)]
91 : enum Message {
92 : SafekeeperTimelineInfo(SafekeeperTimelineInfo),
93 : SafekeeperDiscoveryRequest(SafekeeperDiscoveryRequest),
94 : SafekeeperDiscoveryResponse(SafekeeperDiscoveryResponse),
95 : }
96 :
97 : impl Message {
98 : /// Convert proto message to internal message.
99 0 : pub fn from(proto_msg: TypedMessage) -> Result<Self, Status> {
100 0 : match proto_msg.r#type() {
101 : MessageType::SafekeeperTimelineInfo => Ok(Message::SafekeeperTimelineInfo(
102 0 : proto_msg.safekeeper_timeline_info.ok_or_else(|| {
103 0 : Status::new(Code::InvalidArgument, "missing safekeeper_timeline_info")
104 0 : })?,
105 : )),
106 : MessageType::SafekeeperDiscoveryRequest => Ok(Message::SafekeeperDiscoveryRequest(
107 0 : proto_msg.safekeeper_discovery_request.ok_or_else(|| {
108 0 : Status::new(
109 0 : Code::InvalidArgument,
110 0 : "missing safekeeper_discovery_request",
111 0 : )
112 0 : })?,
113 : )),
114 : MessageType::SafekeeperDiscoveryResponse => Ok(Message::SafekeeperDiscoveryResponse(
115 0 : proto_msg.safekeeper_discovery_response.ok_or_else(|| {
116 0 : Status::new(
117 0 : Code::InvalidArgument,
118 0 : "missing safekeeper_discovery_response",
119 0 : )
120 0 : })?,
121 : )),
122 0 : MessageType::Unknown => Err(Status::new(
123 0 : Code::InvalidArgument,
124 0 : format!("invalid message type: {:?}", proto_msg.r#type),
125 0 : )),
126 : }
127 0 : }
128 :
129 : /// Get the tenant_timeline_id from the message.
130 2 : pub fn tenant_timeline_id(&self) -> Result<Option<TenantTimelineId>, Status> {
131 2 : match self {
132 2 : Message::SafekeeperTimelineInfo(msg) => Ok(msg
133 2 : .tenant_timeline_id
134 2 : .as_ref()
135 2 : .map(parse_proto_ttid)
136 2 : .transpose()?),
137 0 : Message::SafekeeperDiscoveryRequest(msg) => Ok(msg
138 0 : .tenant_timeline_id
139 0 : .as_ref()
140 0 : .map(parse_proto_ttid)
141 0 : .transpose()?),
142 0 : Message::SafekeeperDiscoveryResponse(msg) => Ok(msg
143 0 : .tenant_timeline_id
144 0 : .as_ref()
145 0 : .map(parse_proto_ttid)
146 0 : .transpose()?),
147 : }
148 2 : }
149 :
150 : /// Convert internal message to the protobuf struct.
151 0 : pub fn as_typed_message(&self) -> TypedMessage {
152 0 : let mut res = TypedMessage {
153 0 : r#type: self.message_type() as i32,
154 0 : ..Default::default()
155 0 : };
156 0 : match self {
157 0 : Message::SafekeeperTimelineInfo(msg) => {
158 0 : res.safekeeper_timeline_info = Some(msg.clone())
159 : }
160 0 : Message::SafekeeperDiscoveryRequest(msg) => {
161 0 : res.safekeeper_discovery_request = Some(msg.clone())
162 : }
163 0 : Message::SafekeeperDiscoveryResponse(msg) => {
164 0 : res.safekeeper_discovery_response = Some(msg.clone())
165 : }
166 : }
167 0 : res
168 0 : }
169 :
170 : /// Get the message type.
171 0 : pub fn message_type(&self) -> MessageType {
172 0 : match self {
173 0 : Message::SafekeeperTimelineInfo(_) => MessageType::SafekeeperTimelineInfo,
174 0 : Message::SafekeeperDiscoveryRequest(_) => MessageType::SafekeeperDiscoveryRequest,
175 0 : Message::SafekeeperDiscoveryResponse(_) => MessageType::SafekeeperDiscoveryResponse,
176 : }
177 0 : }
178 : }
179 :
180 : #[derive(Copy, Clone, Debug)]
181 : enum SubscriptionKey {
182 : All,
183 : Timeline(TenantTimelineId),
184 : }
185 :
186 : impl SubscriptionKey {
187 : /// Parse protobuf subkey (protobuf doesn't have fixed size bytes, we get vectors).
188 0 : pub fn from_proto_subscription_key(key: ProtoSubscriptionKey) -> Result<Self, Status> {
189 0 : match key {
190 0 : ProtoSubscriptionKey::All(_) => Ok(SubscriptionKey::All),
191 0 : ProtoSubscriptionKey::TenantTimelineId(proto_ttid) => {
192 0 : Ok(SubscriptionKey::Timeline(parse_proto_ttid(&proto_ttid)?))
193 : }
194 : }
195 0 : }
196 :
197 : /// Parse from FilterTenantTimelineId
198 0 : pub fn from_proto_filter_tenant_timeline_id(
199 0 : opt: Option<&FilterTenantTimelineId>,
200 0 : ) -> Result<Self, Status> {
201 0 : if opt.is_none() {
202 0 : return Ok(SubscriptionKey::All);
203 0 : }
204 0 :
205 0 : let f = opt.unwrap();
206 0 : if !f.enabled {
207 0 : return Ok(SubscriptionKey::All);
208 0 : }
209 :
210 0 : let ttid =
211 0 : parse_proto_ttid(f.tenant_timeline_id.as_ref().ok_or_else(|| {
212 0 : Status::new(Code::InvalidArgument, "missing tenant_timeline_id")
213 0 : })?)?;
214 0 : Ok(SubscriptionKey::Timeline(ttid))
215 0 : }
216 : }
217 :
218 : /// Channel to timeline subscribers.
219 : struct ChanToTimelineSub {
220 : chan: broadcast::Sender<Message>,
221 : /// Tracked separately to know when delete the shmem entry. receiver_count()
222 : /// is unhandy for that as unregistering and dropping the receiver side
223 : /// happens at different moments.
224 : num_subscribers: u64,
225 : }
226 :
227 : struct SharedState {
228 : next_pub_id: PubId,
229 : num_pubs: i64,
230 : next_sub_id: SubId,
231 : num_subs_to_timelines: i64,
232 : chans_to_timeline_subs: HashMap<TenantTimelineId, ChanToTimelineSub>,
233 : num_subs_to_all: i64,
234 : chan_to_all_subs: broadcast::Sender<Message>,
235 : }
236 :
237 : impl SharedState {
238 1 : pub fn new(all_keys_chan_size: usize) -> Self {
239 1 : SharedState {
240 1 : next_pub_id: 0,
241 1 : num_pubs: 0,
242 1 : next_sub_id: 0,
243 1 : num_subs_to_timelines: 0,
244 1 : chans_to_timeline_subs: HashMap::new(),
245 1 : num_subs_to_all: 0,
246 1 : chan_to_all_subs: broadcast::channel(all_keys_chan_size).0,
247 1 : }
248 1 : }
249 :
250 : // Register new publisher.
251 1 : pub fn register_publisher(&mut self) -> PubId {
252 1 : let pub_id = self.next_pub_id;
253 1 : self.next_pub_id += 1;
254 1 : self.num_pubs += 1;
255 1 : NUM_PUBS.set(self.num_pubs);
256 1 : pub_id
257 1 : }
258 :
259 : // Unregister publisher.
260 1 : pub fn unregister_publisher(&mut self) {
261 1 : self.num_pubs -= 1;
262 1 : NUM_PUBS.set(self.num_pubs);
263 1 : }
264 :
265 : // Register new subscriber.
266 2 : pub fn register_subscriber(
267 2 : &mut self,
268 2 : sub_key: SubscriptionKey,
269 2 : timeline_chan_size: usize,
270 2 : ) -> (SubId, broadcast::Receiver<Message>) {
271 2 : let sub_id = self.next_sub_id;
272 2 : self.next_sub_id += 1;
273 2 : let sub_rx = match sub_key {
274 : SubscriptionKey::All => {
275 1 : self.num_subs_to_all += 1;
276 1 : NUM_SUBS_ALL.set(self.num_subs_to_all);
277 1 : self.chan_to_all_subs.subscribe()
278 : }
279 1 : SubscriptionKey::Timeline(ttid) => {
280 1 : self.num_subs_to_timelines += 1;
281 1 : NUM_SUBS_TIMELINE.set(self.num_subs_to_timelines);
282 1 : // Create new broadcast channel for this key, or subscriber to
283 1 : // the existing one.
284 1 : let chan_to_timeline_sub =
285 1 : self.chans_to_timeline_subs
286 1 : .entry(ttid)
287 1 : .or_insert(ChanToTimelineSub {
288 1 : chan: broadcast::channel(timeline_chan_size).0,
289 1 : num_subscribers: 0,
290 1 : });
291 1 : chan_to_timeline_sub.num_subscribers += 1;
292 1 : chan_to_timeline_sub.chan.subscribe()
293 : }
294 : };
295 2 : (sub_id, sub_rx)
296 2 : }
297 :
298 : // Unregister the subscriber.
299 2 : pub fn unregister_subscriber(&mut self, sub_key: SubscriptionKey) {
300 2 : match sub_key {
301 1 : SubscriptionKey::All => {
302 1 : self.num_subs_to_all -= 1;
303 1 : NUM_SUBS_ALL.set(self.num_subs_to_all);
304 1 : }
305 1 : SubscriptionKey::Timeline(ttid) => {
306 1 : self.num_subs_to_timelines -= 1;
307 1 : NUM_SUBS_TIMELINE.set(self.num_subs_to_timelines);
308 1 :
309 1 : // Remove from the map, destroying the channel, if we are the
310 1 : // last subscriber to this timeline.
311 1 :
312 1 : // Missing entry is a bug; we must have registered.
313 1 : let chan_to_timeline_sub = self
314 1 : .chans_to_timeline_subs
315 1 : .get_mut(&ttid)
316 1 : .expect("failed to find sub entry in shmem during unregister");
317 1 : chan_to_timeline_sub.num_subscribers -= 1;
318 1 : if chan_to_timeline_sub.num_subscribers == 0 {
319 1 : self.chans_to_timeline_subs.remove(&ttid);
320 1 : }
321 : }
322 : }
323 2 : }
324 : }
325 :
326 : // SharedState wrapper.
327 : #[derive(Clone)]
328 : struct Registry {
329 : shared_state: Arc<RwLock<SharedState>>,
330 : timeline_chan_size: usize,
331 : }
332 :
333 : impl Registry {
334 : // Register new publisher in shared state.
335 1 : pub fn register_publisher(&self, remote_addr: SocketAddr) -> Publisher {
336 1 : let pub_id = self.shared_state.write().register_publisher();
337 1 : info!("publication started id={} addr={:?}", pub_id, remote_addr);
338 1 : Publisher {
339 1 : id: pub_id,
340 1 : registry: self.clone(),
341 1 : remote_addr,
342 1 : }
343 1 : }
344 :
345 1 : pub fn unregister_publisher(&self, publisher: &Publisher) {
346 1 : self.shared_state.write().unregister_publisher();
347 1 : info!(
348 0 : "publication ended id={} addr={:?}",
349 : publisher.id, publisher.remote_addr
350 : );
351 1 : }
352 :
353 : // Register new subscriber in shared state.
354 2 : pub fn register_subscriber(
355 2 : &self,
356 2 : sub_key: SubscriptionKey,
357 2 : remote_addr: SocketAddr,
358 2 : ) -> Subscriber {
359 2 : let (sub_id, sub_rx) = self
360 2 : .shared_state
361 2 : .write()
362 2 : .register_subscriber(sub_key, self.timeline_chan_size);
363 2 : info!(
364 0 : "subscription started id={}, key={:?}, addr={:?}",
365 : sub_id, sub_key, remote_addr
366 : );
367 2 : Subscriber {
368 2 : id: sub_id,
369 2 : key: sub_key,
370 2 : sub_rx,
371 2 : registry: self.clone(),
372 2 : remote_addr,
373 2 : }
374 2 : }
375 :
376 : // Unregister the subscriber
377 2 : pub fn unregister_subscriber(&self, subscriber: &Subscriber) {
378 2 : self.shared_state
379 2 : .write()
380 2 : .unregister_subscriber(subscriber.key);
381 2 : info!(
382 0 : "subscription ended id={}, key={:?}, addr={:?}",
383 : subscriber.id, subscriber.key, subscriber.remote_addr
384 : );
385 2 : }
386 :
387 : /// Send msg to relevant subscribers.
388 2 : pub fn send_msg(&self, msg: &Message) -> Result<(), Status> {
389 2 : PROCESSED_MESSAGES_TOTAL.inc();
390 2 :
391 2 : // send message to subscribers for everything
392 2 : let shared_state = self.shared_state.read();
393 2 : // Err means there is no subscribers, it is fine.
394 2 : shared_state.chan_to_all_subs.send(msg.clone()).ok();
395 :
396 : // send message to per timeline subscribers, if there is ttid
397 2 : let ttid = msg.tenant_timeline_id()?;
398 2 : if let Some(ttid) = ttid {
399 2 : if let Some(subs) = shared_state.chans_to_timeline_subs.get(&ttid) {
400 1 : // Err can't happen here, as tx is destroyed only after removing
401 1 : // from the map the last subscriber along with tx.
402 1 : subs.chan
403 1 : .send(msg.clone())
404 1 : .expect("rx is still in the map with zero subscribers");
405 1 : }
406 0 : }
407 2 : Ok(())
408 2 : }
409 : }
410 :
411 : // Private subscriber state.
412 : struct Subscriber {
413 : id: SubId,
414 : key: SubscriptionKey,
415 : // Subscriber receives messages from publishers here.
416 : sub_rx: broadcast::Receiver<Message>,
417 : // to unregister itself from shared state in Drop
418 : registry: Registry,
419 : // for logging
420 : remote_addr: SocketAddr,
421 : }
422 :
423 : impl Drop for Subscriber {
424 2 : fn drop(&mut self) {
425 2 : self.registry.unregister_subscriber(self);
426 2 : }
427 : }
428 :
429 : // Private publisher state
430 : struct Publisher {
431 : id: PubId,
432 : registry: Registry,
433 : // for logging
434 : remote_addr: SocketAddr,
435 : }
436 :
437 : impl Publisher {
438 : /// Send msg to relevant subscribers.
439 2 : pub fn send_msg(&mut self, msg: &Message) -> Result<(), Status> {
440 2 : self.registry.send_msg(msg)
441 2 : }
442 : }
443 :
444 : impl Drop for Publisher {
445 1 : fn drop(&mut self) {
446 1 : self.registry.unregister_publisher(self);
447 1 : }
448 : }
449 :
450 : struct Broker {
451 : registry: Registry,
452 : }
453 :
454 : #[tonic::async_trait]
455 : impl BrokerService for Broker {
456 0 : async fn publish_safekeeper_info(
457 0 : &self,
458 0 : request: Request<tonic::Streaming<SafekeeperTimelineInfo>>,
459 0 : ) -> Result<Response<()>, Status> {
460 0 : let remote_addr = request
461 0 : .remote_addr()
462 0 : .expect("TCPConnectInfo inserted by handler");
463 0 : let mut publisher = self.registry.register_publisher(remote_addr);
464 0 :
465 0 : let mut stream = request.into_inner();
466 :
467 : loop {
468 0 : match stream.next().await {
469 0 : Some(Ok(msg)) => publisher.send_msg(&Message::SafekeeperTimelineInfo(msg))?,
470 0 : Some(Err(e)) => return Err(e), // grpc error from the stream
471 0 : None => break, // closed stream
472 0 : }
473 0 : }
474 0 :
475 0 : Ok(Response::new(()))
476 0 : }
477 :
478 : type SubscribeSafekeeperInfoStream =
479 : Pin<Box<dyn Stream<Item = Result<SafekeeperTimelineInfo, Status>> + Send + 'static>>;
480 :
481 0 : async fn subscribe_safekeeper_info(
482 0 : &self,
483 0 : request: Request<SubscribeSafekeeperInfoRequest>,
484 0 : ) -> Result<Response<Self::SubscribeSafekeeperInfoStream>, Status> {
485 0 : let remote_addr = request
486 0 : .remote_addr()
487 0 : .expect("TCPConnectInfo inserted by handler");
488 0 : let proto_key = request
489 0 : .into_inner()
490 0 : .subscription_key
491 0 : .ok_or_else(|| Status::new(Code::InvalidArgument, "missing subscription key"))?;
492 0 : let sub_key = SubscriptionKey::from_proto_subscription_key(proto_key)?;
493 0 : let mut subscriber = self.registry.register_subscriber(sub_key, remote_addr);
494 0 :
495 0 : // transform rx into stream with item = Result, as method result demands
496 0 : let output = async_stream::try_stream! {
497 0 : let mut warn_interval = time::interval(Duration::from_millis(1000));
498 0 : let mut missed_msgs: u64 = 0;
499 0 : loop {
500 0 : match subscriber.sub_rx.recv().await {
501 0 : Ok(info) => {
502 0 : match info {
503 0 : Message::SafekeeperTimelineInfo(info) => yield info,
504 0 : _ => {},
505 0 : }
506 0 : BROADCASTED_MESSAGES_TOTAL.inc();
507 0 : },
508 0 : Err(RecvError::Lagged(skipped_msg)) => {
509 0 : BROADCAST_DROPPED_MESSAGES_TOTAL.inc_by(skipped_msg);
510 0 : missed_msgs += skipped_msg;
511 0 : if (futures::poll!(Box::pin(warn_interval.tick()))).is_ready() {
512 0 : warn!("subscription id={}, key={:?} addr={:?} dropped {} messages, channel is full",
513 0 : subscriber.id, subscriber.key, subscriber.remote_addr, missed_msgs);
514 0 : missed_msgs = 0;
515 0 : }
516 0 : }
517 0 : Err(RecvError::Closed) => {
518 0 : // can't happen, we never drop the channel while there is a subscriber
519 0 : Err(Status::new(Code::Internal, "channel unexpectantly closed"))?;
520 0 : }
521 0 : }
522 0 : }
523 0 : };
524 0 :
525 0 : Ok(Response::new(
526 0 : Box::pin(output) as Self::SubscribeSafekeeperInfoStream
527 0 : ))
528 0 : }
529 :
530 : type SubscribeByFilterStream =
531 : Pin<Box<dyn Stream<Item = Result<TypedMessage, Status>> + Send + 'static>>;
532 :
533 : /// Subscribe to all messages, limited by a filter.
534 0 : async fn subscribe_by_filter(
535 0 : &self,
536 0 : request: Request<SubscribeByFilterRequest>,
537 0 : ) -> std::result::Result<Response<Self::SubscribeByFilterStream>, Status> {
538 0 : let remote_addr = request
539 0 : .remote_addr()
540 0 : .expect("TCPConnectInfo inserted by handler");
541 0 : let proto_filter = request.into_inner();
542 0 : let ttid_filter = proto_filter.tenant_timeline_id.as_ref();
543 :
544 0 : let sub_key = SubscriptionKey::from_proto_filter_tenant_timeline_id(ttid_filter)?;
545 0 : let types_set = proto_filter
546 0 : .types
547 0 : .iter()
548 0 : .map(|t| t.r#type)
549 0 : .collect::<std::collections::HashSet<_>>();
550 0 :
551 0 : let mut subscriber = self.registry.register_subscriber(sub_key, remote_addr);
552 0 :
553 0 : // transform rx into stream with item = Result, as method result demands
554 0 : let output = async_stream::try_stream! {
555 0 : let mut warn_interval = time::interval(Duration::from_millis(1000));
556 0 : let mut missed_msgs: u64 = 0;
557 0 : loop {
558 0 : match subscriber.sub_rx.recv().await {
559 0 : Ok(msg) => {
560 0 : let msg_type = msg.message_type() as i32;
561 0 : if types_set.contains(&msg_type) {
562 0 : yield msg.as_typed_message();
563 0 : BROADCASTED_MESSAGES_TOTAL.inc();
564 0 : }
565 0 : },
566 0 : Err(RecvError::Lagged(skipped_msg)) => {
567 0 : BROADCAST_DROPPED_MESSAGES_TOTAL.inc_by(skipped_msg);
568 0 : missed_msgs += skipped_msg;
569 0 : if (futures::poll!(Box::pin(warn_interval.tick()))).is_ready() {
570 0 : warn!("subscription id={}, key={:?} addr={:?} dropped {} messages, channel is full",
571 0 : subscriber.id, subscriber.key, subscriber.remote_addr, missed_msgs);
572 0 : missed_msgs = 0;
573 0 : }
574 0 : }
575 0 : Err(RecvError::Closed) => {
576 0 : // can't happen, we never drop the channel while there is a subscriber
577 0 : Err(Status::new(Code::Internal, "channel unexpectantly closed"))?;
578 0 : }
579 0 : }
580 0 : }
581 0 : };
582 0 :
583 0 : Ok(Response::new(
584 0 : Box::pin(output) as Self::SubscribeByFilterStream
585 0 : ))
586 0 : }
587 :
588 : /// Publish one message.
589 0 : async fn publish_one(
590 0 : &self,
591 0 : request: Request<TypedMessage>,
592 0 : ) -> std::result::Result<Response<()>, Status> {
593 0 : let msg = Message::from(request.into_inner())?;
594 0 : PUBLISHED_ONEOFF_MESSAGES_TOTAL.inc();
595 0 : self.registry.send_msg(&msg)?;
596 0 : Ok(Response::new(()))
597 0 : }
598 : }
599 :
600 : // We serve only metrics and healthcheck through http1.
601 0 : async fn http1_handler(
602 0 : req: hyper::Request<hyper::body::Body>,
603 0 : ) -> Result<hyper::Response<Body>, Infallible> {
604 0 : let resp = match (req.method(), req.uri().path()) {
605 0 : (&Method::GET, "/metrics") => {
606 0 : let mut buffer = vec![];
607 0 : let metrics = metrics::gather();
608 0 : let encoder = TextEncoder::new();
609 0 : encoder.encode(&metrics, &mut buffer).unwrap();
610 0 :
611 0 : hyper::Response::builder()
612 0 : .status(StatusCode::OK)
613 0 : .header(CONTENT_TYPE, encoder.format_type())
614 0 : .body(Body::from(buffer))
615 0 : .unwrap()
616 : }
617 0 : (&Method::GET, "/status") => hyper::Response::builder()
618 0 : .status(StatusCode::OK)
619 0 : .body(Body::empty())
620 0 : .unwrap(),
621 0 : _ => hyper::Response::builder()
622 0 : .status(StatusCode::NOT_FOUND)
623 0 : .body(Body::empty())
624 0 : .unwrap(),
625 : };
626 0 : Ok(resp)
627 0 : }
628 :
629 : #[tokio::main]
630 0 : async fn main() -> Result<(), Box<dyn std::error::Error>> {
631 0 : let args = Args::parse();
632 0 :
633 0 : // important to keep the order of:
634 0 : // 1. init logging
635 0 : // 2. tracing panic hook
636 0 : // 3. sentry
637 0 : logging::init(
638 0 : LogFormat::from_config(&args.log_format)?,
639 0 : logging::TracingErrorLayerEnablement::Disabled,
640 0 : logging::Output::Stdout,
641 0 : )?;
642 0 : logging::replace_panic_hook_with_tracing_panic_hook().forget();
643 0 : // initialize sentry if SENTRY_DSN is provided
644 0 : let _sentry_guard = init_sentry(Some(GIT_VERSION.into()), &[]);
645 0 : info!("version: {GIT_VERSION} build_tag: {BUILD_TAG}");
646 0 : metrics::set_build_info_metric(GIT_VERSION, BUILD_TAG);
647 0 :
648 0 : // On any shutdown signal, log receival and exit.
649 0 : std::thread::spawn(move || {
650 0 : ShutdownSignals::handle(|signal| {
651 0 : info!("received {}, terminating", signal.name());
652 0 : std::process::exit(0);
653 0 : })
654 0 : });
655 0 :
656 0 : let registry = Registry {
657 0 : shared_state: Arc::new(RwLock::new(SharedState::new(args.all_keys_chan_size))),
658 0 : timeline_chan_size: args.timeline_chan_size,
659 0 : };
660 0 : let storage_broker_impl = Broker {
661 0 : registry: registry.clone(),
662 0 : };
663 0 : let storage_broker_server = BrokerServiceServer::new(storage_broker_impl);
664 0 :
665 0 : info!("listening on {}", &args.listen_addr);
666 0 :
667 0 : // grpc is served along with http1 for metrics on a single port, hence we
668 0 : // don't use tonic's Server.
669 0 : hyper::Server::bind(&args.listen_addr)
670 0 : .http2_keep_alive_interval(Some(args.http2_keepalive_interval))
671 0 : .serve(make_service_fn(move |conn: &AddrStream| {
672 0 : let storage_broker_server_cloned = storage_broker_server.clone();
673 0 : let connect_info = conn.connect_info();
674 0 : async move {
675 0 : Ok::<_, Infallible>(service_fn(move |mut req| {
676 0 : // That's what tonic's MakeSvc.call does to pass conninfo to
677 0 : // the request handler (and where its request.remote_addr()
678 0 : // expects it to find).
679 0 : req.extensions_mut().insert(connect_info.clone());
680 0 :
681 0 : // Technically this second clone is not needed, but consume
682 0 : // by async block is apparently unavoidable. BTW, error
683 0 : // message is enigmatic, see
684 0 : // https://github.com/rust-lang/rust/issues/68119
685 0 : //
686 0 : // We could get away without async block at all, but then we
687 0 : // need to resort to futures::Either to merge the result,
688 0 : // which doesn't caress an eye as well.
689 0 : let mut storage_broker_server_svc = storage_broker_server_cloned.clone();
690 0 : async move {
691 0 : if req.headers().get("content-type").map(|x| x.as_bytes())
692 0 : == Some(b"application/grpc")
693 0 : {
694 0 : let res_resp = storage_broker_server_svc.call(req).await;
695 0 : // Grpc and http1 handlers have slightly different
696 0 : // Response types: it is UnsyncBoxBody for the
697 0 : // former one (not sure why) and plain hyper::Body
698 0 : // for the latter. Both implement HttpBody though,
699 0 : // and EitherBody is used to merge them.
700 0 : res_resp.map(|resp| resp.map(EitherBody::Left))
701 0 : } else {
702 0 : let res_resp = http1_handler(req).await;
703 0 : res_resp.map(|resp| resp.map(EitherBody::Right))
704 0 : }
705 0 : }
706 0 : }))
707 0 : }
708 0 : }))
709 0 : .await?;
710 0 : Ok(())
711 0 : }
712 :
713 : #[cfg(test)]
714 : mod tests {
715 : use super::*;
716 : use storage_broker::proto::TenantTimelineId as ProtoTenantTimelineId;
717 : use tokio::sync::broadcast::error::TryRecvError;
718 : use utils::id::{TenantId, TimelineId};
719 :
720 2 : fn msg(timeline_id: Vec<u8>) -> Message {
721 2 : Message::SafekeeperTimelineInfo(SafekeeperTimelineInfo {
722 2 : safekeeper_id: 1,
723 2 : tenant_timeline_id: Some(ProtoTenantTimelineId {
724 2 : tenant_id: vec![0x00; 16],
725 2 : timeline_id,
726 2 : }),
727 2 : term: 0,
728 2 : last_log_term: 0,
729 2 : flush_lsn: 1,
730 2 : commit_lsn: 2,
731 2 : backup_lsn: 3,
732 2 : remote_consistent_lsn: 4,
733 2 : peer_horizon_lsn: 5,
734 2 : safekeeper_connstr: "neon-1-sk-1.local:7676".to_owned(),
735 2 : http_connstr: "neon-1-sk-1.local:7677".to_owned(),
736 2 : local_start_lsn: 0,
737 2 : availability_zone: None,
738 2 : standby_horizon: 0,
739 2 : })
740 2 : }
741 :
742 3 : fn tli_from_u64(i: u64) -> Vec<u8> {
743 3 : let mut timeline_id = vec![0xFF; 8];
744 3 : timeline_id.extend_from_slice(&i.to_be_bytes());
745 3 : timeline_id
746 3 : }
747 :
748 3 : fn mock_addr() -> SocketAddr {
749 3 : "127.0.0.1:8080".parse().unwrap()
750 3 : }
751 :
752 : #[tokio::test]
753 1 : async fn test_registry() {
754 1 : let registry = Registry {
755 1 : shared_state: Arc::new(RwLock::new(SharedState::new(16))),
756 1 : timeline_chan_size: 16,
757 1 : };
758 1 :
759 1 : // subscribe to timeline 2
760 1 : let ttid_2 = TenantTimelineId {
761 1 : tenant_id: TenantId::from_slice(&[0x00; 16]).unwrap(),
762 1 : timeline_id: TimelineId::from_slice(&tli_from_u64(2)).unwrap(),
763 1 : };
764 1 : let sub_key_2 = SubscriptionKey::Timeline(ttid_2);
765 1 : let mut subscriber_2 = registry.register_subscriber(sub_key_2, mock_addr());
766 1 : let mut subscriber_all = registry.register_subscriber(SubscriptionKey::All, mock_addr());
767 1 :
768 1 : // send two messages with different keys
769 1 : let msg_1 = msg(tli_from_u64(1));
770 1 : let msg_2 = msg(tli_from_u64(2));
771 1 : let mut publisher = registry.register_publisher(mock_addr());
772 1 : publisher.send_msg(&msg_1).expect("failed to send msg");
773 1 : publisher.send_msg(&msg_2).expect("failed to send msg");
774 1 :
775 1 : // msg with key 2 should arrive to subscriber_2
776 1 : assert_eq!(subscriber_2.sub_rx.try_recv().unwrap(), msg_2);
777 1 :
778 1 : // but nothing more
779 1 : assert_eq!(
780 1 : subscriber_2.sub_rx.try_recv().unwrap_err(),
781 1 : TryRecvError::Empty
782 1 : );
783 1 :
784 1 : // subscriber_all should receive both messages
785 1 : assert_eq!(subscriber_all.sub_rx.try_recv().unwrap(), msg_1);
786 1 : assert_eq!(subscriber_all.sub_rx.try_recv().unwrap(), msg_2);
787 1 : assert_eq!(
788 1 : subscriber_all.sub_rx.try_recv().unwrap_err(),
789 1 : TryRecvError::Empty
790 1 : );
791 1 : }
792 : }
|