meta_srv/event/
region_migration.rs1use std::any::Any;
16use std::time::Duration;
17
18use api::v1::value::ValueData;
19use api::v1::{ColumnSchema, Row};
20use common_event_recorder::error::{Result, SerializeEventSnafu};
21use common_event_recorder::event_table::{
22 REGION_ID_COLUMN, REGION_MIGRATION_DST_NODE_ID_COLUMN, REGION_MIGRATION_DST_PEER_ADDR_COLUMN,
23 REGION_MIGRATION_SRC_NODE_ID_COLUMN, REGION_MIGRATION_SRC_PEER_ADDR_COLUMN,
24 REGION_MIGRATION_TRIGGER_REASON_COLUMN, REGION_NUMBER_COLUMN, TABLE_ID_COLUMN, column_schemas,
25};
26use common_event_recorder::{Event, TriggerReason};
27use serde::Serialize;
28use snafu::ResultExt;
29use store_api::storage::RegionId;
30
31use crate::procedure::region_migration::{PersistentContext, RegionMigrationTriggerReason};
32
33pub const REGION_MIGRATION_EVENT_TYPE: &str = "region_migration";
34
35#[derive(Debug)]
37pub(crate) struct RegionMigrationEvent {
38 region_ids: Vec<RegionId>,
40 trigger_reason: RegionMigrationTriggerReason,
42 src_node_id: u64,
44 src_peer_addr: String,
46 dst_node_id: u64,
48 dst_peer_addr: String,
50 timeout: Duration,
52}
53
54#[derive(Debug, Serialize)]
55struct Payload {
56 #[serde(with = "humantime_serde")]
57 timeout: Duration,
58}
59
60impl RegionMigrationEvent {
61 pub fn from_persistent_ctx(ctx: &PersistentContext, trigger_reason: TriggerReason) -> Self {
62 Self {
63 region_ids: ctx.region_ids.clone(),
64 trigger_reason: RegionMigrationTriggerReason::from_trigger_reason(trigger_reason),
65 src_node_id: ctx.from_peer.id,
66 src_peer_addr: ctx.from_peer.addr.clone(),
67 dst_node_id: ctx.to_peer.id,
68 dst_peer_addr: ctx.to_peer.addr.clone(),
69 timeout: ctx.timeout,
70 }
71 }
72}
73
74impl Event for RegionMigrationEvent {
75 fn event_type(&self) -> &str {
76 REGION_MIGRATION_EVENT_TYPE
77 }
78
79 fn extra_schema(&self) -> Vec<ColumnSchema> {
80 column_schemas([
81 ®ION_ID_COLUMN,
82 &TABLE_ID_COLUMN,
83 ®ION_NUMBER_COLUMN,
84 ®ION_MIGRATION_TRIGGER_REASON_COLUMN,
85 ®ION_MIGRATION_SRC_NODE_ID_COLUMN,
86 ®ION_MIGRATION_SRC_PEER_ADDR_COLUMN,
87 ®ION_MIGRATION_DST_NODE_ID_COLUMN,
88 ®ION_MIGRATION_DST_PEER_ADDR_COLUMN,
89 ])
90 }
91
92 fn extra_rows(&self) -> Result<Vec<Row>> {
93 let mut extra_rows = Vec::with_capacity(self.region_ids.len());
94 for region_id in &self.region_ids {
95 extra_rows.push(Row {
96 values: vec![
97 ValueData::U64Value(region_id.as_u64()).into(),
98 ValueData::U32Value(region_id.table_id()).into(),
99 ValueData::U32Value(region_id.region_number()).into(),
100 ValueData::StringValue(self.trigger_reason.to_string()).into(),
101 ValueData::U64Value(self.src_node_id).into(),
102 ValueData::StringValue(self.src_peer_addr.clone()).into(),
103 ValueData::U64Value(self.dst_node_id).into(),
104 ValueData::StringValue(self.dst_peer_addr.clone()).into(),
105 ],
106 });
107 }
108
109 Ok(extra_rows)
110 }
111
112 fn json_payload(&self) -> Result<serde_json::Value> {
113 serde_json::to_value(Payload {
114 timeout: self.timeout,
115 })
116 .context(SerializeEventSnafu)
117 }
118
119 fn as_any(&self) -> &dyn Any {
120 self
121 }
122}