-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: impl pubsub in metasrv (#2045)
* feat: impl pubsub * add test_subscriber_disconnect unit test * chore: cr * cr * cr
- Loading branch information
1 parent
fdd4929
commit dda9225
Showing
13 changed files
with
591 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2023 Greptime Team | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use api::v1::meta::{HeartbeatRequest, Role}; | ||
use async_trait::async_trait; | ||
|
||
use crate::error::Result; | ||
use crate::handler::{HeartbeatAccumulator, HeartbeatHandler}; | ||
use crate::metasrv::Context; | ||
use crate::pubsub::{Message, PublishRef}; | ||
|
||
pub struct PublishHeartbeatHandler { | ||
publish: PublishRef, | ||
} | ||
|
||
impl PublishHeartbeatHandler { | ||
pub fn new(publish: PublishRef) -> PublishHeartbeatHandler { | ||
PublishHeartbeatHandler { publish } | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl HeartbeatHandler for PublishHeartbeatHandler { | ||
fn is_acceptable(&self, role: Role) -> bool { | ||
role == Role::Datanode | ||
} | ||
|
||
async fn handle( | ||
&self, | ||
req: &HeartbeatRequest, | ||
_: &mut Context, | ||
_: &mut HeartbeatAccumulator, | ||
) -> Result<()> { | ||
let msg = Message::Heartbeat(Box::new(req.clone())); | ||
self.publish.send_msg(msg).await; | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2023 Greptime Team | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use api::v1::meta::HeartbeatRequest; | ||
|
||
mod publish; | ||
mod subscribe_manager; | ||
mod subscriber; | ||
#[cfg(test)] | ||
mod tests; | ||
|
||
pub use publish::{DefaultPublish, Publish, PublishRef}; | ||
pub use subscribe_manager::{ | ||
AddSubRequest, DefaultSubscribeManager, SubscribeManager, SubscribeManagerRef, SubscribeQuery, | ||
UnSubRequest, | ||
}; | ||
pub use subscriber::{Subscriber, SubscriberRef, Transport}; | ||
|
||
/// Subscribed topic type, determined by the ability of meta. | ||
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] | ||
pub enum Topic { | ||
Heartbeat, | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub enum Message { | ||
Heartbeat(Box<HeartbeatRequest>), | ||
} | ||
|
||
impl Message { | ||
pub fn topic(&self) -> Topic { | ||
match self { | ||
Message::Heartbeat(_) => Topic::Heartbeat, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2023 Greptime Team | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::fmt::Debug; | ||
use std::marker::PhantomData; | ||
use std::sync::Arc; | ||
|
||
use common_telemetry::error; | ||
|
||
use crate::pubsub::{Message, SubscribeManager, Transport, UnSubRequest}; | ||
|
||
/// This trait provides a `send_msg` method that can be used by other modules | ||
/// of meta to publish [Message]. | ||
#[async_trait::async_trait] | ||
pub trait Publish: Send + Sync { | ||
async fn send_msg(&self, message: Message); | ||
} | ||
|
||
pub type PublishRef = Arc<dyn Publish>; | ||
|
||
/// The default implementation of [Publish] | ||
pub struct DefaultPublish<M, T> { | ||
subscribe_manager: Arc<M>, | ||
_transport: PhantomData<T>, | ||
} | ||
|
||
impl<M, T> DefaultPublish<M, T> { | ||
pub fn new(subscribe_manager: Arc<M>) -> Self { | ||
Self { | ||
subscribe_manager, | ||
_transport: PhantomData, | ||
} | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl<M, T> Publish for DefaultPublish<M, T> | ||
where | ||
M: SubscribeManager<T>, | ||
T: Transport + Debug, | ||
{ | ||
async fn send_msg(&self, message: Message) { | ||
let sub_list = self | ||
.subscribe_manager | ||
.subscribers_by_topic(&message.topic()); | ||
|
||
for sub in sub_list { | ||
if sub.transport_msg(message.clone()).await.is_err() { | ||
// If an error occurs, we consider the subscriber offline, | ||
// so un_subscribe here. | ||
let req = UnSubRequest { | ||
subscriber_id: sub.id(), | ||
}; | ||
|
||
if let Err(e) = self.subscribe_manager.un_subscribe(req.clone()) { | ||
error!(e; "failed to un_subscribe, req: {:?}", req); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.