@@ -4,7 +4,7 @@ use tokio::net::tcp::OwnedWriteHalf;
4
4
5
5
use std:: collections:: HashMap ;
6
6
7
- use crate :: config:: { get_config, parse, Role } ;
7
+ use crate :: config:: { get_config, parse} ;
8
8
use crate :: errors:: Error ;
9
9
use crate :: messages:: * ;
10
10
use crate :: pool:: ConnectionPool ;
@@ -41,6 +41,15 @@ pub async fn handle_admin(
41
41
} else if query. starts_with ( "SHOW DATABASES" ) {
42
42
trace ! ( "SHOW DATABASES" ) ;
43
43
show_databases ( stream, & pool) . await
44
+ } else if query. starts_with ( "SHOW POOLS" ) {
45
+ trace ! ( "SHOW POOLS" ) ;
46
+ show_pools ( stream, & pool) . await
47
+ } else if query. starts_with ( "SHOW LISTS" ) {
48
+ trace ! ( "SHOW LISTS" ) ;
49
+ show_lists ( stream, & pool) . await
50
+ } else if query. starts_with ( "SHOW VERSION" ) {
51
+ trace ! ( "SHOW VERSION" ) ;
52
+ show_version ( stream) . await
44
53
} else if query. starts_with ( "SET " ) {
45
54
trace ! ( "SET" ) ;
46
55
ignore_set ( stream) . await
@@ -49,6 +58,118 @@ pub async fn handle_admin(
49
58
}
50
59
}
51
60
61
+ /// SHOW LISTS
62
+ async fn show_lists ( stream : & mut OwnedWriteHalf , pool : & ConnectionPool ) -> Result < ( ) , Error > {
63
+ let stats = get_stats ( ) ;
64
+
65
+ let columns = vec ! [ ( "list" , DataType :: Text ) , ( "items" , DataType :: Int4 ) ] ;
66
+
67
+ let mut res = BytesMut :: new ( ) ;
68
+ res. put ( row_description ( & columns) ) ;
69
+ res. put ( data_row ( & vec ! [
70
+ "databases" . to_string( ) ,
71
+ pool. databases( ) . to_string( ) ,
72
+ ] ) ) ;
73
+ res. put ( data_row ( & vec ! [ "users" . to_string( ) , "1" . to_string( ) ] ) ) ;
74
+ res. put ( data_row ( & vec ! [
75
+ "pools" . to_string( ) ,
76
+ pool. databases( ) . to_string( ) ,
77
+ ] ) ) ;
78
+ res. put ( data_row ( & vec ! [
79
+ "free_clients" . to_string( ) ,
80
+ stats[ "cl_idle" ] . to_string( ) ,
81
+ ] ) ) ;
82
+ res. put ( data_row ( & vec ! [
83
+ "used_clients" . to_string( ) ,
84
+ stats[ "cl_active" ] . to_string( ) ,
85
+ ] ) ) ;
86
+ res. put ( data_row ( & vec ! [
87
+ "login_clients" . to_string( ) ,
88
+ "0" . to_string( ) ,
89
+ ] ) ) ;
90
+ res. put ( data_row ( & vec ! [
91
+ "free_servers" . to_string( ) ,
92
+ stats[ "sv_idle" ] . to_string( ) ,
93
+ ] ) ) ;
94
+ res. put ( data_row ( & vec ! [
95
+ "used_servers" . to_string( ) ,
96
+ stats[ "sv_active" ] . to_string( ) ,
97
+ ] ) ) ;
98
+ res. put ( data_row ( & vec ! [ "dns_names" . to_string( ) , "0" . to_string( ) ] ) ) ;
99
+ res. put ( data_row ( & vec ! [ "dns_zones" . to_string( ) , "0" . to_string( ) ] ) ) ;
100
+ res. put ( data_row ( & vec ! [ "dns_queries" . to_string( ) , "0" . to_string( ) ] ) ) ;
101
+ res. put ( data_row ( & vec ! [ "dns_pending" . to_string( ) , "0" . to_string( ) ] ) ) ;
102
+
103
+ res. put ( command_complete ( "SHOW" ) ) ;
104
+
105
+ res. put_u8 ( b'Z' ) ;
106
+ res. put_i32 ( 5 ) ;
107
+ res. put_u8 ( b'I' ) ;
108
+
109
+ write_all_half ( stream, res) . await
110
+ }
111
+
112
+ /// SHOW VERSION
113
+ async fn show_version ( stream : & mut OwnedWriteHalf ) -> Result < ( ) , Error > {
114
+ let mut res = BytesMut :: new ( ) ;
115
+
116
+ res. put ( row_description ( & vec ! [ ( "version" , DataType :: Text ) ] ) ) ;
117
+ res. put ( data_row ( & vec ! [ "PgCat 0.1.0" . to_string( ) ] ) ) ;
118
+ res. put ( command_complete ( "SHOW" ) ) ;
119
+
120
+ res. put_u8 ( b'Z' ) ;
121
+ res. put_i32 ( 5 ) ;
122
+ res. put_u8 ( b'I' ) ;
123
+
124
+ write_all_half ( stream, res) . await
125
+ }
126
+
127
+ /// SHOW POOLS
128
+ async fn show_pools ( stream : & mut OwnedWriteHalf , _pool : & ConnectionPool ) -> Result < ( ) , Error > {
129
+ let stats = get_stats ( ) ;
130
+ let config = {
131
+ let guard = get_config ( ) ;
132
+ & * guard. clone ( )
133
+ } ;
134
+
135
+ let columns = vec ! [
136
+ ( "database" , DataType :: Text ) ,
137
+ ( "user" , DataType :: Text ) ,
138
+ ( "cl_active" , DataType :: Numeric ) ,
139
+ ( "cl_waiting" , DataType :: Numeric ) ,
140
+ ( "cl_cancel_req" , DataType :: Numeric ) ,
141
+ ( "sv_active" , DataType :: Numeric ) ,
142
+ ( "sv_idle" , DataType :: Numeric ) ,
143
+ ( "sv_used" , DataType :: Numeric ) ,
144
+ ( "sv_tested" , DataType :: Numeric ) ,
145
+ ( "sv_login" , DataType :: Numeric ) ,
146
+ ( "maxwait" , DataType :: Numeric ) ,
147
+ ( "maxwait_us" , DataType :: Numeric ) ,
148
+ ( "pool_mode" , DataType :: Text ) ,
149
+ ] ;
150
+
151
+ let mut res = BytesMut :: new ( ) ;
152
+ res. put ( row_description ( & columns) ) ;
153
+
154
+ let mut row = vec ! [ String :: from( "all" ) , config. user. name. clone( ) ] ;
155
+
156
+ for column in & columns[ 2 ..columns. len ( ) - 1 ] {
157
+ let value = stats. get ( column. 0 ) . unwrap_or ( & 0 ) . to_string ( ) ;
158
+ row. push ( value) ;
159
+ }
160
+
161
+ row. push ( config. general . pool_mode . to_string ( ) ) ;
162
+
163
+ res. put ( data_row ( & row) ) ;
164
+ res. put ( command_complete ( "SHOW" ) ) ;
165
+
166
+ res. put_u8 ( b'Z' ) ;
167
+ res. put_i32 ( 5 ) ;
168
+ res. put_u8 ( b'I' ) ;
169
+
170
+ write_all_half ( stream, res) . await
171
+ }
172
+
52
173
/// SHOW DATABASES
53
174
async fn show_databases ( stream : & mut OwnedWriteHalf , pool : & ConnectionPool ) -> Result < ( ) , Error > {
54
175
let guard = get_config ( ) ;
@@ -79,23 +200,13 @@ async fn show_databases(stream: &mut OwnedWriteHalf, pool: &ConnectionPool) -> R
79
200
80
201
for shard in 0 ..pool. shards ( ) {
81
202
let database_name = & config. shards [ & shard. to_string ( ) ] . database ;
82
- let mut replica_count = 0 ;
83
203
84
204
for server in 0 ..pool. servers ( shard) {
85
205
let address = pool. address ( shard, server) ;
86
- let name = match address. role {
87
- Role :: Primary => format ! ( "shard_{}_primary" , shard) ,
88
-
89
- Role :: Replica => {
90
- let name = format ! ( "shard_{}_replica_{}" , shard, replica_count) ;
91
- replica_count += 1 ;
92
- name
93
- }
94
- } ;
95
206
let pool_state = pool. pool_state ( shard, server) ;
96
207
97
208
res. put ( data_row ( & vec ! [
98
- name, // name
209
+ address . name( ) , // name
99
210
address. host. to_string( ) , // host
100
211
address. port. to_string( ) , // port
101
212
database_name. to_string( ) , // database
@@ -222,7 +333,7 @@ async fn show_stats(stream: &mut OwnedWriteHalf) -> Result<(), Error> {
222
333
res. put ( row_description ( & columns) ) ;
223
334
224
335
let mut row = vec ! [
225
- String :: from( "all shards " ) , // TODO: per-database stats,
336
+ String :: from( "all" ) , // TODO: per-database stats,
226
337
] ;
227
338
228
339
for column in & columns[ 1 ..] {
0 commit comments