Skip to content

Commit

Permalink
feat: adds active_time
Browse files Browse the repository at this point in the history
  • Loading branch information
killme2008 committed Apr 30, 2024
1 parent 6153104 commit 86ab3d9
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 49 deletions.
27 changes: 23 additions & 4 deletions src/catalog/src/information_schema/cluster_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const VERSION: &str = "version";
const GIT_COMMIT: &str = "git_commit";
const START_TIME: &str = "start_time";
const UPTIME: &str = "uptime";
const ACTIVE_TIME: &str = "active_time";

const INIT_CAPACITY: usize = 42;

Expand All @@ -63,6 +64,7 @@ const INIT_CAPACITY: usize = 42;
/// - `git_commit`: the build git commit hash of the peer.
/// - `start_time`: the starting time of the peer.
/// - `uptime`: the uptime of the peer.
/// - `active_time`: the time since the last activity of the peer.
///
pub(super) struct InformationSchemaClusterInfo {
schema: SchemaRef,
Expand Down Expand Up @@ -92,6 +94,7 @@ impl InformationSchemaClusterInfo {
true,
),
ColumnSchema::new(UPTIME, ConcreteDataType::string_datatype(), true),
ColumnSchema::new(ACTIVE_TIME, ConcreteDataType::string_datatype(), true),
]))
}

Expand Down Expand Up @@ -150,6 +153,7 @@ struct InformationSchemaClusterInfoBuilder {
git_commits: StringVectorBuilder,
start_times: TimestampMillisecondVectorBuilder,
uptimes: StringVectorBuilder,
active_times: StringVectorBuilder,
}

impl InformationSchemaClusterInfoBuilder {
Expand All @@ -168,6 +172,7 @@ impl InformationSchemaClusterInfoBuilder {
git_commits: StringVectorBuilder::with_capacity(INIT_CAPACITY),
start_times: TimestampMillisecondVectorBuilder::with_capacity(INIT_CAPACITY),
uptimes: StringVectorBuilder::with_capacity(INIT_CAPACITY),
active_times: StringVectorBuilder::with_capacity(INIT_CAPACITY),
start_time_ms,
}
}
Expand Down Expand Up @@ -252,14 +257,27 @@ impl InformationSchemaClusterInfoBuilder {
.push(Some(TimestampMillisecond(Timestamp::new_millisecond(
node_info.start_time_ms as i64,
))));
let now = common_time::util::current_time_millis() as u64;
let duration_since_start = now - node_info.start_time_ms;
let format = humantime::format_duration(Duration::from_millis(duration_since_start));
self.uptimes.push(Some(format.to_string().as_str()));
self.uptimes.push(Some(
Self::format_duration_since(node_info.start_time_ms).as_str(),
));
} else {
self.start_times.push(None);
self.uptimes.push(None);
}

if node_info.last_activity_ts > 0 {
self.active_times.push(Some(
Self::format_duration_since(node_info.last_activity_ts as u64).as_str(),
));
} else {
self.active_times.push(None);
}
}

fn format_duration_since(ts: u64) -> String {
let now = common_time::util::current_time_millis() as u64;
let duration_since = now - ts;
humantime::format_duration(Duration::from_millis(duration_since)).to_string()
}

fn finish(&mut self) -> Result<RecordBatch> {
Expand All @@ -271,6 +289,7 @@ impl InformationSchemaClusterInfoBuilder {
Arc::new(self.git_commits.finish()),
Arc::new(self.start_times.finish()),
Arc::new(self.uptimes.finish()),
Arc::new(self.active_times.finish()),
];
RecordBatch::new(self.schema.clone(), columns).context(CreateRecordBatchSnafu)
}
Expand Down
43 changes: 22 additions & 21 deletions tests/cases/distributed/information_schema/cluster_info.result
Original file line number Diff line number Diff line change
Expand Up @@ -4,72 +4,73 @@ Affected Rows: 0

DESC TABLE CLUSTER_INFO;

+------------+----------------------+-----+------+---------+---------------+
| Column | Type | Key | Null | Default | Semantic Type |
+------------+----------------------+-----+------+---------+---------------+
| peer_id | Int64 | | NO | | FIELD |
| peer_type | String | | NO | | FIELD |
| peer_addr | String | | YES | | FIELD |
| version | String | | NO | | FIELD |
| git_commit | String | | NO | | FIELD |
| start_time | TimestampMillisecond | | YES | | FIELD |
| uptime | String | | YES | | FIELD |
+------------+----------------------+-----+------+---------+---------------+
+-------------+----------------------+-----+------+---------+---------------+
| Column | Type | Key | Null | Default | Semantic Type |
+-------------+----------------------+-----+------+---------+---------------+
| peer_id | Int64 | | NO | | FIELD |
| peer_type | String | | NO | | FIELD |
| peer_addr | String | | YES | | FIELD |
| version | String | | NO | | FIELD |
| git_commit | String | | NO | | FIELD |
| start_time | TimestampMillisecond | | YES | | FIELD |
| uptime | String | | YES | | FIELD |
| active_time | String | | YES | | FIELD |
+-------------+----------------------+-----+------+---------+---------------+

-- SQLNESS REPLACE version node_version
-- SQLNESS REPLACE unknown UNKNOWN
-- SQLNESS REPLACE (\s\d\.\d\.\d\s) Version
-- SQLNESS REPLACE (\s[a-z0-9]{7}\s) Hash
-- SQLNESS REPLACE (\s[\-0-9T:\.]{23}) Start_time
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Uptime
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Duration
-- SQLNESS REPLACE [\s\-]+
SELECT * FROM CLUSTER_INFO ORDER BY peer_type;

++++++++|peer_id|peer_type|peer_addr|node_version|git_commit|start_time|uptime|++++++++|1|DATANODE|127.0.0.1:4101|Version|Hash|Start_time|Uptime||2|DATANODE|127.0.0.1:4102|Version|Hash|Start_time|Uptime||3|DATANODE|127.0.0.1:4103|Version|Hash|Start_time|Uptime||1|FRONTEND|127.0.0.1:4001|Version|Hash|Start_time|Uptime||0|METASRV|127.0.0.1:3002|UNKNOWN|UNKNOWN|||++++++++
+++++++++|peer_id|peer_type|peer_addr|node_version|git_commit|start_time|uptime|active_time|+++++++++|1|DATANODE|127.0.0.1:4101|Version|Hash|Start_time|Duration|Duration||2|DATANODE|127.0.0.1:4102|Version|Hash|Start_time|Duration|Duration||3|DATANODE|127.0.0.1:4103|Version|Hash|Start_time|Duration|Duration||1|FRONTEND|127.0.0.1:4001|Version|Hash|Start_time|Duration|Duration||0|METASRV|127.0.0.1:3002|UNKNOWN|UNKNOWN||||+++++++++

-- SQLNESS REPLACE version node_version
-- SQLNESS REPLACE unknown UNKNOWN
-- SQLNESS REPLACE (\s\d\.\d\.\d\s) Version
-- SQLNESS REPLACE (\s[a-z0-9]{7}\s) Hash
-- SQLNESS REPLACE (\s[\-0-9T:\.]{23}) Start_time
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Uptime
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Duration
-- SQLNESS REPLACE [\s\-]+
SELECT * FROM CLUSTER_INFO WHERE PEER_TYPE = 'METASRV' ORDER BY peer_type;

++++++++|peer_id|peer_type|peer_addr|node_version|git_commit|start_time|uptime|++++++++|0|METASRV|127.0.0.1:3002|UNKNOWN|UNKNOWN|||++++++++
+++++++++|peer_id|peer_type|peer_addr|node_version|git_commit|start_time|uptime|active_time|+++++++++|0|METASRV|127.0.0.1:3002|UNKNOWN|UNKNOWN||||+++++++++

-- SQLNESS REPLACE version node_version
-- SQLNESS REPLACE unknown UNKNOWN
-- SQLNESS REPLACE (\s\d\.\d\.\d\s) Version
-- SQLNESS REPLACE (\s[a-z0-9]{7}\s) Hash
-- SQLNESS REPLACE (\s[\-0-9T:\.]{23}) Start_time
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Uptime
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Duration
-- SQLNESS REPLACE [\s\-]+
SELECT * FROM CLUSTER_INFO WHERE PEER_TYPE = 'FRONTEND' ORDER BY peer_type;

++++++++|peer_id|peer_type|peer_addr|node_version|git_commit|start_time|uptime|++++++++|1|FRONTEND|127.0.0.1:4001|Version|Hash|Start_time|Uptime|++++++++
+++++++++|peer_id|peer_type|peer_addr|node_version|git_commit|start_time|uptime|active_time|+++++++++|1|FRONTEND|127.0.0.1:4001|Version|Hash|Start_time|Duration|Duration|+++++++++

-- SQLNESS REPLACE version node_version
-- SQLNESS REPLACE unknown UNKNOWN
-- SQLNESS REPLACE (\s\d\.\d\.\d\s) Version
-- SQLNESS REPLACE (\s[a-z0-9]{7}\s) Hash
-- SQLNESS REPLACE (\s[\-0-9T:\.]{23}) Start_time
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Uptime
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Duration
-- SQLNESS REPLACE [\s\-]+
SELECT * FROM CLUSTER_INFO WHERE PEER_TYPE != 'FRONTEND' ORDER BY peer_type;

++++++++|peer_id|peer_type|peer_addr|node_version|git_commit|start_time|uptime|++++++++|1|DATANODE|127.0.0.1:4101|Version|Hash|Start_time|Uptime||2|DATANODE|127.0.0.1:4102|Version|Hash|Start_time|Uptime||3|DATANODE|127.0.0.1:4103|Version|Hash|Start_time|Uptime||0|METASRV|127.0.0.1:3002|UNKNOWN|UNKNOWN|||++++++++
+++++++++|peer_id|peer_type|peer_addr|node_version|git_commit|start_time|uptime|active_time|+++++++++|1|DATANODE|127.0.0.1:4101|Version|Hash|Start_time|Duration|Duration||2|DATANODE|127.0.0.1:4102|Version|Hash|Start_time|Duration|Duration||3|DATANODE|127.0.0.1:4103|Version|Hash|Start_time|Duration|Duration||0|METASRV|127.0.0.1:3002|UNKNOWN|UNKNOWN||||+++++++++

-- SQLNESS REPLACE version node_version
-- SQLNESS REPLACE unknown UNKNOWN
-- SQLNESS REPLACE (\s\d\.\d\.\d\s) Version
-- SQLNESS REPLACE (\s[a-z0-9]{7}\s) Hash
-- SQLNESS REPLACE (\s[\-0-9T:\.]{23}) Start_time
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Uptime
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Duration
-- SQLNESS REPLACE [\s\-]+
SELECT * FROM CLUSTER_INFO WHERE PEER_ID > 1 ORDER BY peer_type;

++++++++|peer_id|peer_type|peer_addr|node_version|git_commit|start_time|uptime|++++++++|2|DATANODE|127.0.0.1:4102|Version|Hash|Start_time|Uptime||3|DATANODE|127.0.0.1:4103|Version|Hash|Start_time|Uptime|++++++++
+++++++++|peer_id|peer_type|peer_addr|node_version|git_commit|start_time|uptime|active_time|+++++++++|2|DATANODE|127.0.0.1:4102|Version|Hash|Start_time|Duration|Duration||3|DATANODE|127.0.0.1:4103|Version|Hash|Start_time|Duration|Duration|+++++++++

USE PUBLIC;

Expand Down
10 changes: 5 additions & 5 deletions tests/cases/distributed/information_schema/cluster_info.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ DESC TABLE CLUSTER_INFO;
-- SQLNESS REPLACE (\s\d\.\d\.\d\s) Version
-- SQLNESS REPLACE (\s[a-z0-9]{7}\s) Hash
-- SQLNESS REPLACE (\s[\-0-9T:\.]{23}) Start_time
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Uptime
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Duration
-- SQLNESS REPLACE [\s\-]+
SELECT * FROM CLUSTER_INFO ORDER BY peer_type;

Expand All @@ -16,7 +16,7 @@ SELECT * FROM CLUSTER_INFO ORDER BY peer_type;
-- SQLNESS REPLACE (\s\d\.\d\.\d\s) Version
-- SQLNESS REPLACE (\s[a-z0-9]{7}\s) Hash
-- SQLNESS REPLACE (\s[\-0-9T:\.]{23}) Start_time
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Uptime
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Duration
-- SQLNESS REPLACE [\s\-]+
SELECT * FROM CLUSTER_INFO WHERE PEER_TYPE = 'METASRV' ORDER BY peer_type;

Expand All @@ -25,7 +25,7 @@ SELECT * FROM CLUSTER_INFO WHERE PEER_TYPE = 'METASRV' ORDER BY peer_type;
-- SQLNESS REPLACE (\s\d\.\d\.\d\s) Version
-- SQLNESS REPLACE (\s[a-z0-9]{7}\s) Hash
-- SQLNESS REPLACE (\s[\-0-9T:\.]{23}) Start_time
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Uptime
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Duration
-- SQLNESS REPLACE [\s\-]+
SELECT * FROM CLUSTER_INFO WHERE PEER_TYPE = 'FRONTEND' ORDER BY peer_type;

Expand All @@ -34,7 +34,7 @@ SELECT * FROM CLUSTER_INFO WHERE PEER_TYPE = 'FRONTEND' ORDER BY peer_type;
-- SQLNESS REPLACE (\s\d\.\d\.\d\s) Version
-- SQLNESS REPLACE (\s[a-z0-9]{7}\s) Hash
-- SQLNESS REPLACE (\s[\-0-9T:\.]{23}) Start_time
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Uptime
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Duration
-- SQLNESS REPLACE [\s\-]+
SELECT * FROM CLUSTER_INFO WHERE PEER_TYPE != 'FRONTEND' ORDER BY peer_type;

Expand All @@ -43,7 +43,7 @@ SELECT * FROM CLUSTER_INFO WHERE PEER_TYPE != 'FRONTEND' ORDER BY peer_type;
-- SQLNESS REPLACE (\s\d\.\d\.\d\s) Version
-- SQLNESS REPLACE (\s[a-z0-9]{7}\s) Hash
-- SQLNESS REPLACE (\s[\-0-9T:\.]{23}) Start_time
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Uptime
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Duration
-- SQLNESS REPLACE [\s\-]+
SELECT * FROM CLUSTER_INFO WHERE PEER_ID > 1 ORDER BY peer_type;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ select * from information_schema.columns order by table_schema, table_name, colu
| greptime | information_schema | check_constraints | constraint_catalog | 1 | 2147483647 | 2147483647 | | | | utf8 | utf8_bin | | | select,insert | | String | string | FIELD | | No | string | | |
| greptime | information_schema | check_constraints | constraint_name | 3 | 2147483647 | 2147483647 | | | | utf8 | utf8_bin | | | select,insert | | String | string | FIELD | | No | string | | |
| greptime | information_schema | check_constraints | constraint_schema | 2 | 2147483647 | 2147483647 | | | | utf8 | utf8_bin | | | select,insert | | String | string | FIELD | | No | string | | |
| greptime | information_schema | cluster_info | active_time | 8 | 2147483647 | 2147483647 | | | | utf8 | utf8_bin | | | select,insert | | String | string | FIELD | | Yes | string | | |
| greptime | information_schema | cluster_info | git_commit | 5 | 2147483647 | 2147483647 | | | | utf8 | utf8_bin | | | select,insert | | String | string | FIELD | | No | string | | |
| greptime | information_schema | cluster_info | peer_addr | 3 | 2147483647 | 2147483647 | | | | utf8 | utf8_bin | | | select,insert | | String | string | FIELD | | Yes | string | | |
| greptime | information_schema | cluster_info | peer_id | 1 | | | 19 | 0 | | | | | | select,insert | | Int64 | bigint | FIELD | | No | bigint | | |
Expand Down
33 changes: 17 additions & 16 deletions tests/cases/standalone/information_schema/cluster_info.result
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,38 @@ Affected Rows: 0

DESC TABLE CLUSTER_INFO;

+------------+----------------------+-----+------+---------+---------------+
| Column | Type | Key | Null | Default | Semantic Type |
+------------+----------------------+-----+------+---------+---------------+
| peer_id | Int64 | | NO | | FIELD |
| peer_type | String | | NO | | FIELD |
| peer_addr | String | | YES | | FIELD |
| version | String | | NO | | FIELD |
| git_commit | String | | NO | | FIELD |
| start_time | TimestampMillisecond | | YES | | FIELD |
| uptime | String | | YES | | FIELD |
+------------+----------------------+-----+------+---------+---------------+
+-------------+----------------------+-----+------+---------+---------------+
| Column | Type | Key | Null | Default | Semantic Type |
+-------------+----------------------+-----+------+---------+---------------+
| peer_id | Int64 | | NO | | FIELD |
| peer_type | String | | NO | | FIELD |
| peer_addr | String | | YES | | FIELD |
| version | String | | NO | | FIELD |
| git_commit | String | | NO | | FIELD |
| start_time | TimestampMillisecond | | YES | | FIELD |
| uptime | String | | YES | | FIELD |
| active_time | String | | YES | | FIELD |
+-------------+----------------------+-----+------+---------+---------------+

-- SQLNESS REPLACE version node_version
-- SQLNESS REPLACE (\d\.\d\.\d) Version
-- SQLNESS REPLACE (\s[a-z0-9]{7}\s) Hash
-- SQLNESS REPLACE (\s[\-0-9T:\.]{23}) Start_time
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Uptime
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Duration
-- SQLNESS REPLACE [\s\-]+
SELECT * FROM CLUSTER_INFO;

++++++++|peer_id|peer_type|peer_addr|node_version|git_commit|start_time|uptime|++++++++|0|STANDALONE||Version|Hash|Start_time|Uptime|++++++++
+++++++++|peer_id|peer_type|peer_addr|node_version|git_commit|start_time|uptime|active_time|+++++++++|0|STANDALONE||Version|Hash|Start_time|Duration||+++++++++

-- SQLNESS REPLACE version node_version
-- SQLNESS REPLACE (\d\.\d\.\d) Version
-- SQLNESS REPLACE (\s[a-z0-9]{7}\s) Hash
-- SQLNESS REPLACE (\s[\-0-9T:\.]{23}) Start_time
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Uptime
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Duration
-- SQLNESS REPLACE [\s\-]+
SELECT * FROM CLUSTER_INFO WHERE PEER_TYPE = 'STANDALONE';

++++++++|peer_id|peer_type|peer_addr|node_version|git_commit|start_time|uptime|++++++++|0|STANDALONE||Version|Hash|Start_time|Uptime|++++++++
+++++++++|peer_id|peer_type|peer_addr|node_version|git_commit|start_time|uptime|active_time|+++++++++|0|STANDALONE||Version|Hash|Start_time|Duration||+++++++++

SELECT * FROM CLUSTER_INFO WHERE PEER_TYPE != 'STANDALONE';

Expand All @@ -45,7 +46,7 @@ SELECT * FROM CLUSTER_INFO WHERE PEER_TYPE != 'STANDALONE';
-- SQLNESS REPLACE (\d\.\d\.\d) Version
-- SQLNESS REPLACE (\s[a-z0-9]{7}\s) Hash
-- SQLNESS REPLACE (\s[\-0-9T:\.]{23}) Start_time
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Uptime
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Duration
-- SQLNESS REPLACE [\s\-]+
SELECT * FROM CLUSTER_INFO WHERE PEER_ID = 0;

Expand Down
6 changes: 3 additions & 3 deletions tests/cases/standalone/information_schema/cluster_info.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ DESC TABLE CLUSTER_INFO;
-- SQLNESS REPLACE (\d\.\d\.\d) Version
-- SQLNESS REPLACE (\s[a-z0-9]{7}\s) Hash
-- SQLNESS REPLACE (\s[\-0-9T:\.]{23}) Start_time
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Uptime
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Duration
-- SQLNESS REPLACE [\s\-]+
SELECT * FROM CLUSTER_INFO;

-- SQLNESS REPLACE version node_version
-- SQLNESS REPLACE (\d\.\d\.\d) Version
-- SQLNESS REPLACE (\s[a-z0-9]{7}\s) Hash
-- SQLNESS REPLACE (\s[\-0-9T:\.]{23}) Start_time
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Uptime
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Duration
-- SQLNESS REPLACE [\s\-]+
SELECT * FROM CLUSTER_INFO WHERE PEER_TYPE = 'STANDALONE';

Expand All @@ -24,7 +24,7 @@ SELECT * FROM CLUSTER_INFO WHERE PEER_TYPE != 'STANDALONE';
-- SQLNESS REPLACE (\d\.\d\.\d) Version
-- SQLNESS REPLACE (\s[a-z0-9]{7}\s) Hash
-- SQLNESS REPLACE (\s[\-0-9T:\.]{23}) Start_time
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Uptime
-- SQLNESS REPLACE ((\d+(s|ms|m)\s)+) Duration
-- SQLNESS REPLACE [\s\-]+
SELECT * FROM CLUSTER_INFO WHERE PEER_ID = 0;

Expand Down

0 comments on commit 86ab3d9

Please sign in to comment.