Skip to main content

rocketmq_admin_cli/
commands.rs

1// Copyright 2023 The RocketMQ Rust Authors
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
15mod auth;
16mod broker;
17mod cluster;
18mod connection;
19mod consumer;
20mod container;
21mod controller;
22mod export;
23mod ha;
24mod lite;
25mod message;
26mod namesrv;
27mod offset;
28mod producer;
29mod queue;
30mod stats;
31mod topic;
32
33use std::sync::Arc;
34
35use clap::Parser;
36use clap::Subcommand;
37use rocketmq_remoting::runtime::RPCHook;
38use tabled::Table;
39use tabled::Tabled;
40use tabled::settings::Style;
41
42use rocketmq_admin_core::core::RocketMQResult;
43
44/// A trait that defines the execution behavior for commands.
45///
46/// This trait is designed to be implemented by various command types
47/// that require execution logic. The `execute` method provides the
48/// functionality to execute a command with a given RPC hook.
49#[allow(async_fn_in_trait)]
50pub trait CommandExecute {
51    /// Executes the command.
52    ///
53    /// # Parameters
54    /// - `rpcHook`: An `Arc` containing a reference to a type that implements the `RPCHook` trait.
55    ///   This hook is used to customize the behavior of remote procedure calls during command
56    ///   execution.
57    async fn execute(&self, rpc_hook: Option<Arc<dyn RPCHook>>) -> RocketMQResult<()>;
58}
59
60#[derive(Debug, Parser, Clone)]
61pub struct CommonArgs {
62    /// The name server address list
63    #[arg(
64        short = 'n',
65        long = "namesrvAddr",
66        required = false,
67        default_value = None,
68        help = "Name server address list, eg: '192.168.0.1:9876;192.168.0.2:9876'"
69    )]
70    pub namesrv_addr: Option<String>,
71
72    /// Skip confirmation prompts (automatically answer 'yes')
73    #[arg(
74        short = 'y',
75        long = "yes",
76        help = "Skip confirmation prompts for dangerous operations"
77    )]
78    pub skip_confirm: bool,
79}
80
81#[derive(Subcommand)]
82pub enum Commands {
83    #[command(subcommand)]
84    #[command(about = "Auth commands")]
85    #[command(name = "auth")]
86    Auth(auth::AuthCommands),
87
88    #[command(subcommand)]
89    #[command(about = "Broker commands")]
90    #[command(name = "broker")]
91    Broker(broker::BrokerCommands),
92
93    #[command(subcommand)]
94    #[command(about = "Cluster commands")]
95    #[command(name = "cluster")]
96    Cluster(cluster::ClusterCommands),
97
98    #[command(subcommand)]
99    #[command(about = "Connection commands")]
100    #[command(name = "connection")]
101    Connection(connection::ConnectionCommands),
102
103    #[command(subcommand)]
104    #[command(about = "Consumer commands")]
105    #[command(name = "consumer")]
106    Consumer(consumer::ConsumerCommands),
107
108    #[command(subcommand)]
109    #[command(about = "Broker container commands")]
110    #[command(name = "container")]
111    Container(container::ContainerCommands),
112
113    #[command(subcommand)]
114    #[command(about = "Controller commands")]
115    #[command(name = "controller")]
116    Controller(controller::ControllerCommands),
117
118    #[command(subcommand)]
119    #[command(about = "Export commands")]
120    #[command(name = "export")]
121    Export(export::ExportCommands),
122
123    #[command(subcommand)]
124    #[command(about = "HA commands")]
125    #[command(name = "ha")]
126    HA(ha::HACommands),
127
128    #[command(subcommand)]
129    #[command(about = "Lite commands")]
130    #[command(name = "lite")]
131    Lite(lite::LiteCommands),
132
133    #[command(subcommand)]
134    #[command(about = "Message commands")]
135    #[command(name = "message")]
136    Message(message::MessageCommands),
137
138    #[command(subcommand)]
139    #[command(about = "Name server commands")]
140    #[command(name = "nameserver")]
141    NameServer(namesrv::NameServerCommands),
142
143    #[command(subcommand)]
144    #[command(about = "Offset commands")]
145    #[command(name = "offset")]
146    Offset(offset::OffsetCommands),
147
148    #[command(subcommand)]
149    #[command(about = "Producer commands")]
150    #[command(name = "producer")]
151    Producer(producer::ProducerCommands),
152
153    #[command(subcommand)]
154    #[command(about = "Queue commands")]
155    #[command(name = "queue")]
156    Queue(queue::QueueCommands),
157
158    #[command(subcommand)]
159    #[command(about = "Stats commands")]
160    #[command(name = "stats")]
161    Stats(stats::StatsCommands),
162
163    #[command(subcommand)]
164    #[command(about = "Topic commands")]
165    Topic(topic::TopicCommands),
166
167    #[command(about = "Category commands show")]
168    Show(ClassificationTablePrint),
169}
170
171impl CommandExecute for Commands {
172    async fn execute(&self, rpc_hook: Option<Arc<dyn RPCHook>>) -> RocketMQResult<()> {
173        match self {
174            Commands::Auth(value) => value.execute(rpc_hook).await,
175            Commands::Broker(value) => value.execute(rpc_hook).await,
176            Commands::Cluster(value) => value.execute(rpc_hook).await,
177            Commands::Connection(value) => value.execute(rpc_hook).await,
178            Commands::Consumer(value) => value.execute(rpc_hook).await,
179            Commands::Container(value) => value.execute(rpc_hook).await,
180            Commands::Controller(value) => value.execute(rpc_hook).await,
181            Commands::Export(value) => value.execute(rpc_hook).await,
182            Commands::HA(value) => value.execute(rpc_hook).await,
183            Commands::Lite(value) => value.execute(rpc_hook).await,
184            Commands::Message(value) => value.execute(rpc_hook).await,
185            Commands::NameServer(value) => value.execute(rpc_hook).await,
186            Commands::Offset(value) => value.execute(rpc_hook).await,
187            Commands::Producer(value) => value.execute(rpc_hook).await,
188            Commands::Queue(value) => value.execute(rpc_hook).await,
189            Commands::Stats(value) => value.execute(rpc_hook).await,
190            Commands::Topic(value) => value.execute(rpc_hook).await,
191            Commands::Show(value) => value.execute(rpc_hook).await,
192        }
193    }
194}
195
196// ================for commands table print================
197#[derive(Tabled, Clone)]
198struct Command {
199    #[tabled(rename = "Category")]
200    category: &'static str,
201
202    #[tabled(rename = "Command")]
203    command: &'static str,
204
205    #[tabled(rename = "Remark")]
206    remark: &'static str,
207}
208
209#[derive(Parser)]
210pub struct ClassificationTablePrint;
211
212impl CommandExecute for ClassificationTablePrint {
213    async fn execute(&self, _rpc_hook: Option<Arc<dyn RPCHook>>) -> RocketMQResult<()> {
214        let commands: Vec<Command> = vec![
215            Command {
216                category: "Auth",
217                command: "copyAcl",
218                remark: "Copy acl to cluster.",
219            },
220            Command {
221                category: "Auth",
222                command: "copyUser",
223                remark: "Copy user to cluster.",
224            },
225            Command {
226                category: "Auth",
227                command: "createAcl",
228                remark: "Create acl to cluster.",
229            },
230            Command {
231                category: "Auth",
232                command: "createUser",
233                remark: "Create user to cluster.",
234            },
235            Command {
236                category: "Auth",
237                command: "deleteAcl",
238                remark: "Delete acl from cluster.",
239            },
240            Command {
241                category: "Auth",
242                command: "deleteUser",
243                remark: "Delete user from cluster.",
244            },
245            Command {
246                category: "Auth",
247                command: "getAcl",
248                remark: "Get acl from cluster.",
249            },
250            Command {
251                category: "Auth",
252                command: "getUser",
253                remark: "Get user from cluster.",
254            },
255            Command {
256                category: "Auth",
257                command: "listAcl",
258                remark: "List acl from cluster.",
259            },
260            Command {
261                category: "Auth",
262                command: "listUser",
263                remark: "List users from cluster.",
264            },
265            Command {
266                category: "Auth",
267                command: "updateAcl",
268                remark: "Update ACL.",
269            },
270            Command {
271                category: "Auth",
272                command: "updateUser",
273                remark: "Update user to cluster.",
274            },
275            Command {
276                category: "Broker",
277                command: "brokerConsumeStats",
278                remark: "Fetch broker consume stats data.",
279            },
280            Command {
281                category: "Broker",
282                command: "brokerStatus",
283                remark: "Fetch broker runtime status data.",
284            },
285            Command {
286                category: "Broker",
287                command: "cleanExpiredCQ",
288                remark: "Clean expired ConsumeQueue on broker.",
289            },
290            Command {
291                category: "Broker",
292                command: "cleanUnusedTopic",
293                remark: "Clean unused topic on broker.",
294            },
295            Command {
296                category: "Broker",
297                command: "deleteExpiredCommitLog",
298                remark: "Delete expired CommitLog files.",
299            },
300            Command {
301                category: "Broker",
302                command: "getBrokerConfig",
303                remark: "Get broker config by cluster or special broker.",
304            },
305            Command {
306                category: "Broker",
307                command: "getBrokerEpoch",
308                remark: "Fetch broker epoch entries.",
309            },
310            Command {
311                category: "Broker",
312                command: "getColdDataFlowCtrInfo",
313                remark: "Get cold data flow ctr info.",
314            },
315            Command {
316                category: "Broker",
317                command: "removeColdDataFlowCtrGroupConfig",
318                remark: "Remove consumer from cold ctr config.",
319            },
320            Command {
321                category: "Broker",
322                command: "resetMasterFlushOffset",
323                remark: "Reset master flush offset in slave.",
324            },
325            Command {
326                category: "Broker",
327                command: "sendMsgStatus",
328                remark: "Send msg to broker.",
329            },
330            Command {
331                category: "Broker",
332                command: "switchTimerEngine",
333                remark: "Switch the engine of timer message in broker.",
334            },
335            Command {
336                category: "Broker",
337                command: "updateBrokerConfig",
338                remark: "Update broker config by broker or cluster.",
339            },
340            Command {
341                category: "Broker",
342                command: "updateColdDataFlowCtrGroupConfig",
343                remark: "Update cold data flow ctr group config.",
344            },
345            Command {
346                category: "Broker",
347                command: "setCommitLogReadAheadMode",
348                remark: "Set read ahead mode for all commitlog files.",
349            },
350            Command {
351                category: "Cluster",
352                command: "clusterRT",
353                remark: "List All clusters Message Send RT.",
354            },
355            Command {
356                category: "Cluster",
357                command: "clusterList",
358                remark: "List cluster infos.",
359            },
360            Command {
361                category: "Connection",
362                command: "consumerConnection",
363                remark: "Query consumer's socket connection, client version and subscription.",
364            },
365            Command {
366                category: "Connection",
367                command: "producerConnection",
368                remark: "Query producer's socket connection and client version.",
369            },
370            Command {
371                category: "Container",
372                command: "addBroker",
373                remark: "Add a broker to specified container.",
374            },
375            Command {
376                category: "Container",
377                command: "removeBroker",
378                remark: "Remove a broker from specified container.",
379            },
380            Command {
381                category: "Consumer",
382                command: "consumerStatus",
383                remark: "Query and display consumer's internal data structures.",
384            },
385            Command {
386                category: "Consumer",
387                command: "consumerProgress",
388                remark: "Query consumer progress.",
389            },
390            Command {
391                category: "Consumer",
392                command: "consumer",
393                remark: "Query consumer's connection, status, etc.",
394            },
395            Command {
396                category: "Consumer",
397                command: "deleteSubGroup",
398                remark: "Delete subscription group from broker.",
399            },
400            Command {
401                category: "Consumer",
402                command: "getConsumerConfig",
403                remark: "Get consumer config by subscription group name.",
404            },
405            Command {
406                category: "Consumer",
407                command: "updateSubGroup",
408                remark: "Update consumer sub group.",
409            },
410            Command {
411                category: "Consumer",
412                command: "updateSubGroupList",
413                remark: "Create or update subscription groups in batch.",
414            },
415            Command {
416                category: "Consumer",
417                command: "startMonitoring",
418                remark: "Start Monitoring.",
419            },
420            Command {
421                category: "Consumer",
422                command: "setConsumeMode",
423                remark: "Set consume mode for consumer group.",
424            },
425            Command {
426                category: "Controller",
427                command: "cleanBrokerMetadata",
428                remark: "Clean metadata of broker on controller.",
429            },
430            Command {
431                category: "Controller",
432                command: "getControllerConfig",
433                remark: "Get configuration of controller(s).",
434            },
435            Command {
436                category: "Controller",
437                command: "getControllerMetaData",
438                remark: "Get meta data of controller.",
439            },
440            Command {
441                category: "Controller",
442                command: "electMaster",
443                remark: "Re-elect the specified broker as master.",
444            },
445            Command {
446                category: "Controller",
447                command: "updateControllerConfig",
448                remark: "Update configuration of controller(s).",
449            },
450            Command {
451                category: "Export",
452                command: "exportConfigs",
453                remark: "Export configs",
454            },
455            Command {
456                category: "Export",
457                command: "exportMetadata",
458                remark: "Export metadata.",
459            },
460            Command {
461                category: "Export",
462                command: "exportMetrics",
463                remark: "Export metrics.",
464            },
465            Command {
466                category: "Export",
467                command: "exportMetadataInRocksDB",
468                remark: "Export RocksDB kv config (topics/subscriptionGroups). Recommend to use [mqadmin \
469                         rocksDBConfigToJson]",
470            },
471            Command {
472                category: "Export",
473                command: "exportPopRecord",
474                remark: "Export pop consumer records.",
475            },
476            Command {
477                category: "Export",
478                command: "rocksDBConfigToJson",
479                remark: "Convert RocksDB kv config to json.",
480            },
481            Command {
482                category: "HA",
483                command: "getSyncStateSet",
484                remark: "Fetch sync state set for target brokers.",
485            },
486            Command {
487                category: "HA",
488                command: "haStatus",
489                remark: "Fetch ha runtime status data.",
490            },
491            Command {
492                category: "Lite",
493                command: "getBrokerLiteInfo",
494                remark: "Get broker lite info.",
495            },
496            Command {
497                category: "Lite",
498                command: "getLiteClientInfo",
499                remark: "Get lite client info.",
500            },
501            Command {
502                category: "Lite",
503                command: "getLiteGroupInfo",
504                remark: "Get lite group info.",
505            },
506            Command {
507                category: "Lite",
508                command: "getLiteTopicInfo",
509                remark: "Get lite topic info.",
510            },
511            Command {
512                category: "Lite",
513                command: "getParentTopicInfo",
514                remark: "Get parent topic info.",
515            },
516            Command {
517                category: "Lite",
518                command: "triggerLiteDispatch",
519                remark: "Trigger Lite Dispatch.",
520            },
521            Command {
522                category: "Message",
523                command: "checkMsgSendRT",
524                remark: "Check message send response time.",
525            },
526            Command {
527                category: "Message",
528                command: "consumeMessage",
529                remark: "Consume message.",
530            },
531            Command {
532                category: "Message",
533                command: "decodeMessageId",
534                remark: "Decode unique message ID.",
535            },
536            Command {
537                category: "Message",
538                command: "dumpCompactionLog",
539                remark: "Parse compaction log to message.",
540            },
541            Command {
542                category: "Message",
543                command: "printMsg",
544                remark: "Print Message Detail.",
545            },
546            Command {
547                category: "Message",
548                command: "printMsgByQueue",
549                remark: "Print Message Detail by queueId.",
550            },
551            Command {
552                category: "Message",
553                command: "queryMsgById",
554                remark: "Query message by message ID.",
555            },
556            Command {
557                category: "Message",
558                command: "queryMsgByKey",
559                remark: "Query Message by Key.",
560            },
561            Command {
562                category: "Message",
563                command: "queryMsgByOffset",
564                remark: "Query Message by offset.",
565            },
566            Command {
567                category: "Message",
568                command: "queryMsgByUniqueKey",
569                remark: "Query Message by Unique key.",
570            },
571            Command {
572                category: "Message",
573                command: "queryMsgTraceById",
574                remark: "Query message trace by message ID.",
575            },
576            Command {
577                category: "Message",
578                command: "sendMessage",
579                remark: "Send a message.",
580            },
581            Command {
582                category: "NameServer",
583                command: "addWritePerm",
584                remark: "Add write perm of broker in all name server.",
585            },
586            Command {
587                category: "NameServer",
588                command: "deleteKvConfig",
589                remark: "Delete KV config.",
590            },
591            Command {
592                category: "NameServer",
593                command: "getNamesrvConfig",
594                remark: "Get configs of name server.",
595            },
596            Command {
597                category: "NameServer",
598                command: "updateKvConfig",
599                remark: "Create or update KV config.",
600            },
601            Command {
602                category: "NameServer",
603                command: "updateNamesrvConfig",
604                remark: "Update configs of name server.",
605            },
606            Command {
607                category: "NameServer",
608                command: "wipeWritePerm",
609                remark: "Wipe write perm of broker in all name server.",
610            },
611            Command {
612                category: "Offset",
613                command: "cloneGroupOffset",
614                remark: "Clone offset from other group.",
615            },
616            Command {
617                category: "Offset",
618                command: "getConsumerStatus",
619                remark: "Get consumer status from client.",
620            },
621            Command {
622                category: "Offset",
623                command: "resetOffsetByTime",
624                remark: "Reset consumer group offsets to a specific timestamp (no restart required).",
625            },
626            Command {
627                category: "Offset",
628                command: "resetOffsetByTimeOld",
629                remark: "Reset consumer offset by timestamp (execute this command required client restart).",
630            },
631            Command {
632                category: "Offset",
633                command: "skipAccumulatedMessage",
634                remark: "Skip all messages that are accumulated (not consumed) currently.",
635            },
636            Command {
637                category: "Producer",
638                command: "producer",
639                remark: "Query producer's instances, connection, status, etc.",
640            },
641            Command {
642                category: "Queue",
643                command: "checkRocksdbCqWriteProgress",
644                remark: "Check if rocksdb cq is same as file cq.",
645            },
646            Command {
647                category: "Queue",
648                command: "queryCq",
649                remark: "Query cq command.",
650            },
651            Command {
652                category: "Stats",
653                command: "statsAll",
654                remark: "Topic and Consumer tps stats.",
655            },
656            Command {
657                category: "Topic",
658                command: "allocateMQ",
659                remark: "Allocate MQ.",
660            },
661            Command {
662                category: "Topic",
663                command: "deleteTopic",
664                remark: "Delete topic.",
665            },
666            Command {
667                category: "Topic",
668                command: "remappingStaticTopic",
669                remark: "Remapping static topic.",
670            },
671            Command {
672                category: "Topic",
673                command: "topicClusterList",
674                remark: "Get cluster info for topic.",
675            },
676            Command {
677                category: "Topic",
678                command: "topicList",
679                remark: "Get topic list.",
680            },
681            Command {
682                category: "Topic",
683                command: "topicRoute",
684                remark: "Examine topic route info.",
685            },
686            Command {
687                category: "Topic",
688                command: "topicStatus",
689                remark: "Examine topic status info.",
690            },
691            Command {
692                category: "Topic",
693                command: "updateOrderConf",
694                remark: "Create or update order conf.",
695            },
696            Command {
697                category: "Topic",
698                command: "updateStaticTopic",
699                remark: "Update or create static topic.",
700            },
701            Command {
702                category: "Topic",
703                command: "updateTopicPerm",
704                remark: "Update topic perm.",
705            },
706            Command {
707                category: "Topic",
708                command: "updateTopicList",
709                remark: "Create or update topic configs in batch.",
710            },
711            Command {
712                category: "Topic",
713                command: "updateTopic",
714                remark: "Update or create topic.",
715            },
716        ];
717        let mut table = Table::new(commands);
718        table.with(Style::extended());
719        print!("{table}");
720        Ok(())
721    }
722}