|
| 1 | +// Copyright 2023 Greptime Team |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use std::collections::HashMap; |
| 16 | +use std::time::Duration; |
| 17 | + |
| 18 | +use api::v1::meta::MailboxMessage; |
| 19 | +use common_meta::datanode::RegionStat; |
| 20 | +use common_meta::instruction::{Instruction, InstructionReply}; |
| 21 | +use common_meta::key::TableMetadataManagerRef; |
| 22 | +use common_meta::key::table_route::PhysicalTableRouteValue; |
| 23 | +use common_meta::peer::Peer; |
| 24 | +use common_telemetry::error; |
| 25 | +use snafu::{OptionExt, ResultExt as _}; |
| 26 | +use table::metadata::TableId; |
| 27 | + |
| 28 | +use crate::cluster::MetaPeerClientRef; |
| 29 | +use crate::error; |
| 30 | +use crate::error::{Result, TableMetadataManagerSnafu}; |
| 31 | +use crate::handler::HeartbeatMailbox; |
| 32 | +use crate::service::mailbox::{Channel, MailboxRef}; |
| 33 | + |
| 34 | +#[async_trait::async_trait] |
| 35 | +pub(crate) trait SchedulerCtx: Send + Sync { |
| 36 | + async fn get_table_to_region_stats(&self) -> Result<HashMap<TableId, Vec<RegionStat>>>; |
| 37 | + |
| 38 | + async fn get_table_route( |
| 39 | + &self, |
| 40 | + table_id: TableId, |
| 41 | + ) -> Result<(TableId, PhysicalTableRouteValue)>; |
| 42 | + |
| 43 | + async fn send_instruction( |
| 44 | + &self, |
| 45 | + peer: &Peer, |
| 46 | + instruction: Instruction, |
| 47 | + description: &str, |
| 48 | + timeout: Duration, |
| 49 | + ) -> Result<InstructionReply>; |
| 50 | +} |
| 51 | + |
| 52 | +pub(crate) struct DefaultGcSchedulerCtx { |
| 53 | + /// The metadata manager. |
| 54 | + pub(crate) table_metadata_manager: TableMetadataManagerRef, |
| 55 | + /// For getting `RegionStats`. |
| 56 | + pub(crate) meta_peer_client: MetaPeerClientRef, |
| 57 | + /// The mailbox to send messages. |
| 58 | + pub(crate) mailbox: MailboxRef, |
| 59 | + /// The server address. |
| 60 | + pub(crate) server_addr: String, |
| 61 | +} |
| 62 | + |
| 63 | +#[async_trait::async_trait] |
| 64 | +impl SchedulerCtx for DefaultGcSchedulerCtx { |
| 65 | + async fn get_table_to_region_stats(&self) -> Result<HashMap<TableId, Vec<RegionStat>>> { |
| 66 | + let dn_stats = self.meta_peer_client.get_all_dn_stat_kvs().await?; |
| 67 | + let mut table_to_region_stats: HashMap<TableId, Vec<RegionStat>> = HashMap::new(); |
| 68 | + for (_dn_id, stats) in dn_stats { |
| 69 | + let mut stats = stats.stats; |
| 70 | + stats.sort_by_key(|s| s.timestamp_millis); |
| 71 | + |
| 72 | + let Some(latest_stat) = stats.last().cloned() else { |
| 73 | + continue; |
| 74 | + }; |
| 75 | + |
| 76 | + for region_stat in latest_stat.region_stats { |
| 77 | + table_to_region_stats |
| 78 | + .entry(region_stat.id.table_id()) |
| 79 | + .or_default() |
| 80 | + .push(region_stat); |
| 81 | + } |
| 82 | + } |
| 83 | + Ok(table_to_region_stats) |
| 84 | + } |
| 85 | + |
| 86 | + async fn get_table_route( |
| 87 | + &self, |
| 88 | + table_id: TableId, |
| 89 | + ) -> Result<(TableId, PhysicalTableRouteValue)> { |
| 90 | + self.table_metadata_manager |
| 91 | + .table_route_manager() |
| 92 | + .get_physical_table_route(table_id) |
| 93 | + .await |
| 94 | + .context(TableMetadataManagerSnafu) |
| 95 | + } |
| 96 | + |
| 97 | + async fn send_instruction( |
| 98 | + &self, |
| 99 | + peer: &Peer, |
| 100 | + instruction: Instruction, |
| 101 | + description: &str, |
| 102 | + timeout: Duration, |
| 103 | + ) -> Result<InstructionReply> { |
| 104 | + let msg = MailboxMessage::json_message( |
| 105 | + &format!("{}: {}", description, instruction), |
| 106 | + &format!("Metasrv@{}", self.server_addr), |
| 107 | + &format!("Datanode-{}@{}", peer.id, peer.addr), |
| 108 | + common_time::util::current_time_millis(), |
| 109 | + &instruction, |
| 110 | + ) |
| 111 | + .with_context(|_| error::SerializeToJsonSnafu { |
| 112 | + input: instruction.to_string(), |
| 113 | + })?; |
| 114 | + |
| 115 | + let mailbox_rx = self |
| 116 | + .mailbox |
| 117 | + .send(&Channel::Datanode(peer.id), msg, timeout) |
| 118 | + .await?; |
| 119 | + |
| 120 | + match mailbox_rx.await { |
| 121 | + Ok(reply_msg) => { |
| 122 | + let reply = HeartbeatMailbox::json_reply(&reply_msg)?; |
| 123 | + Ok(reply) |
| 124 | + } |
| 125 | + Err(e) => { |
| 126 | + error!( |
| 127 | + "Failed to receive reply from datanode {} for {}: {}", |
| 128 | + peer, description, e |
| 129 | + ); |
| 130 | + Err(e) |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments