rocketmq-admin-cli 1.0.0

Command-line interface for managing RocketMQ-Rust and Apache RocketMQ clusters
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
// Copyright 2023 The RocketMQ Rust Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

mod auth;
mod broker;
mod cluster;
mod connection;
mod consumer;
mod controller;
mod export;
mod ha;
mod lite;
mod message;
mod namesrv;
mod offset;
mod producer;
mod queue;
mod release_checkpoint;
mod stats;
mod topic;

use clap::Parser;
use clap::Subcommand;
use tabled::Table;
use tabled::Tabled;
use tabled::settings::Style;

use rocketmq_error::Result as CanonicalResult;

/// A trait that defines the execution behavior for commands.
///
/// This trait is designed to be implemented by various command types
/// that require execution logic. The `execute` method provides the
/// functionality to execute a command with optional Admin credentials.
#[allow(async_fn_in_trait)]
pub trait CommandExecute {
    /// Executes the command.
    ///
    /// # Parameters
    /// - `credentials`: Optional credentials converted to a Client hook only inside the Admin Core
    ///   adapter.
    async fn execute(
        &self,
        credentials: Option<rocketmq_admin_core::core::security::AdminCredentials>,
        client_runtime: std::sync::Arc<rocketmq_admin_core::client_adapter::ClientRuntime>,
    ) -> CanonicalResult<()>;
}

#[cfg(test)]
pub(crate) fn test_client_runtime() -> std::sync::Arc<rocketmq_admin_core::client_adapter::ClientRuntime> {
    static OWNER: std::sync::LazyLock<rocketmq_runtime::RuntimeOwner> = std::sync::LazyLock::new(|| {
        rocketmq_runtime::RuntimeOwner::plan(rocketmq_runtime::RuntimeConfig {
            thread_name: "rocketmq-admin-cli-test".to_string(),
            ..Default::default()
        })
        .expect("runtime configuration is valid")
        .build()
        .expect("admin CLI test runtime should start")
    });
    rocketmq_admin_core::client_adapter::ClientRuntime::try_new(
        OWNER.root_context().component("client"),
        rocketmq_admin_core::client_adapter::ClientRuntimeConfig::default(),
        rocketmq_admin_core::client_adapter::TelemetryHandle::noop(),
    )
    .expect("admin CLI test client runtime should be valid")
}

#[derive(Debug, Parser, Clone)]
pub struct CommonArgs {
    /// The name server address list
    #[arg(
        short = 'n',
        long = "namesrvAddr",
        required = false,
        default_value = None,
        help = "Name server address list, eg: '192.168.0.1:9876;192.168.0.2:9876'"
    )]
    pub namesrv_addr: Option<String>,

    /// Skip confirmation prompts (automatically answer 'yes')
    #[arg(
        short = 'y',
        long = "yes",
        help = "Skip confirmation prompts for dangerous operations"
    )]
    pub skip_confirm: bool,
}

#[derive(Subcommand)]
pub enum Commands {
    #[command(subcommand)]
    #[command(about = "Auth commands")]
    #[command(name = "auth")]
    Auth(auth::AuthCommands),

    #[command(subcommand)]
    #[command(about = "Broker commands")]
    #[command(name = "broker")]
    Broker(broker::BrokerCommands),

    #[command(subcommand)]
    #[command(about = "Cluster commands")]
    #[command(name = "cluster")]
    Cluster(cluster::ClusterCommands),

    #[command(subcommand)]
    #[command(about = "Connection commands")]
    #[command(name = "connection")]
    Connection(connection::ConnectionCommands),

    #[command(subcommand)]
    #[command(about = "Consumer commands")]
    #[command(name = "consumer")]
    Consumer(consumer::ConsumerCommands),

    #[command(subcommand)]
    #[command(about = "Controller commands")]
    #[command(name = "controller")]
    Controller(controller::ControllerCommands),

    #[command(subcommand)]
    #[command(about = "Export commands")]
    #[command(name = "export")]
    Export(export::ExportCommands),

    #[command(subcommand)]
    #[command(about = "HA commands")]
    #[command(name = "ha")]
    HA(ha::HACommands),

    #[command(subcommand)]
    #[command(about = "Lite commands")]
    #[command(name = "lite")]
    Lite(lite::LiteCommands),

    #[command(subcommand)]
    #[command(about = "Message commands")]
    #[command(name = "message")]
    Message(message::MessageCommands),

    #[command(subcommand)]
    #[command(about = "Name server commands")]
    #[command(name = "nameserver")]
    NameServer(namesrv::NameServerCommands),

    #[command(subcommand)]
    #[command(about = "Offset commands")]
    #[command(name = "offset")]
    Offset(offset::OffsetCommands),

    #[command(subcommand)]
    #[command(about = "Producer commands")]
    #[command(name = "producer")]
    Producer(producer::ProducerCommands),

    #[command(subcommand)]
    #[command(about = "Queue commands")]
    #[command(name = "queue")]
    Queue(queue::QueueCommands),

    #[command(subcommand)]
    #[command(about = "Authorized release checkpoint commands")]
    #[command(name = "release-checkpoint")]
    ReleaseCheckpoint(release_checkpoint::ReleaseCheckpointCommands),

    #[command(subcommand)]
    #[command(about = "Stats commands")]
    #[command(name = "stats")]
    Stats(stats::StatsCommands),

    #[command(subcommand)]
    #[command(about = "Topic commands")]
    Topic(topic::TopicCommands),

    #[command(about = "Category commands show")]
    Show(ClassificationTablePrint),
}

impl CommandExecute for Commands {
    async fn execute(
        &self,
        credentials: Option<rocketmq_admin_core::core::security::AdminCredentials>,
        client_runtime: std::sync::Arc<rocketmq_admin_core::client_adapter::ClientRuntime>,
    ) -> CanonicalResult<()> {
        match self {
            Commands::Auth(value) => value.execute(credentials, client_runtime.clone()).await,
            Commands::Broker(value) => value.execute(credentials, client_runtime.clone()).await,
            Commands::Cluster(value) => value.execute(credentials, client_runtime.clone()).await,
            Commands::Connection(value) => value.execute(credentials, client_runtime.clone()).await,
            Commands::Consumer(value) => value.execute(credentials, client_runtime.clone()).await,
            Commands::Controller(value) => value.execute(credentials, client_runtime.clone()).await,
            Commands::Export(value) => value.execute(credentials, client_runtime.clone()).await,
            Commands::HA(value) => value.execute(credentials, client_runtime.clone()).await,
            Commands::Lite(value) => value.execute(credentials, client_runtime.clone()).await,
            Commands::Message(value) => value.execute(credentials, client_runtime.clone()).await,
            Commands::NameServer(value) => value.execute(credentials, client_runtime.clone()).await,
            Commands::Offset(value) => value.execute(credentials, client_runtime.clone()).await,
            Commands::Producer(value) => value.execute(credentials, client_runtime.clone()).await,
            Commands::Queue(value) => value.execute(credentials, client_runtime.clone()).await,
            Commands::ReleaseCheckpoint(value) => value.execute(credentials, client_runtime.clone()).await,
            Commands::Stats(value) => value.execute(credentials, client_runtime.clone()).await,
            Commands::Topic(value) => value.execute(credentials, client_runtime.clone()).await,
            Commands::Show(value) => value.execute(credentials, client_runtime.clone()).await,
        }
    }
}

// ================for commands table print================
#[derive(Tabled, Clone)]
struct Command {
    #[tabled(rename = "Category")]
    category: &'static str,

    #[tabled(rename = "Command")]
    command: &'static str,

    #[tabled(rename = "Remark")]
    remark: &'static str,
}

#[derive(Parser)]
pub struct ClassificationTablePrint;

impl CommandExecute for ClassificationTablePrint {
    async fn execute(
        &self,
        _credentials: Option<rocketmq_admin_core::core::security::AdminCredentials>,
        _client_runtime: std::sync::Arc<rocketmq_admin_core::client_adapter::ClientRuntime>,
    ) -> CanonicalResult<()> {
        let commands: Vec<Command> = vec![
            Command {
                category: "Auth",
                command: "copyAcl",
                remark: "Copy acl to cluster.",
            },
            Command {
                category: "Auth",
                command: "copyUser",
                remark: "Copy user to cluster.",
            },
            Command {
                category: "Auth",
                command: "createAcl",
                remark: "Create acl to cluster.",
            },
            Command {
                category: "Auth",
                command: "createUser",
                remark: "Create user to cluster.",
            },
            Command {
                category: "Auth",
                command: "deleteAcl",
                remark: "Delete acl from cluster.",
            },
            Command {
                category: "Auth",
                command: "deleteUser",
                remark: "Delete user from cluster.",
            },
            Command {
                category: "Auth",
                command: "getAcl",
                remark: "Get acl from cluster.",
            },
            Command {
                category: "Auth",
                command: "getUser",
                remark: "Get user from cluster.",
            },
            Command {
                category: "Auth",
                command: "listAcl",
                remark: "List acl from cluster.",
            },
            Command {
                category: "Auth",
                command: "listUser",
                remark: "List users from cluster.",
            },
            Command {
                category: "Auth",
                command: "updateAcl",
                remark: "Update ACL.",
            },
            Command {
                category: "Auth",
                command: "updateUser",
                remark: "Update user to cluster.",
            },
            Command {
                category: "Broker",
                command: "brokerConsumeStats",
                remark: "Fetch broker consume stats data.",
            },
            Command {
                category: "Broker",
                command: "brokerStatus",
                remark: "Fetch broker runtime status data.",
            },
            Command {
                category: "Broker",
                command: "cleanExpiredCQ",
                remark: "Clean expired ConsumeQueue on broker.",
            },
            Command {
                category: "Broker",
                command: "cleanUnusedTopic",
                remark: "Clean unused topic on broker.",
            },
            Command {
                category: "Broker",
                command: "deleteExpiredCommitLog",
                remark: "Delete expired CommitLog files.",
            },
            Command {
                category: "Broker",
                command: "getBrokerConfig",
                remark: "Get broker config by cluster or special broker.",
            },
            Command {
                category: "Broker",
                command: "getBrokerEpoch",
                remark: "Fetch broker epoch entries.",
            },
            Command {
                category: "Broker",
                command: "getColdDataFlowCtrInfo",
                remark: "Get cold data flow ctr info.",
            },
            Command {
                category: "Broker",
                command: "removeColdDataFlowCtrGroupConfig",
                remark: "Remove consumer from cold ctr config.",
            },
            Command {
                category: "Broker",
                command: "resetMasterFlushOffset",
                remark: "Reset master flush offset in slave.",
            },
            Command {
                category: "Broker",
                command: "sendMsgStatus",
                remark: "Send msg to broker.",
            },
            Command {
                category: "Broker",
                command: "switchTimerEngine",
                remark: "Switch the engine of timer message in broker.",
            },
            Command {
                category: "Broker",
                command: "updateBrokerConfig",
                remark: "Update broker config by broker or cluster.",
            },
            Command {
                category: "Broker",
                command: "updateColdDataFlowCtrGroupConfig",
                remark: "Update cold data flow ctr group config.",
            },
            Command {
                category: "Broker",
                command: "setCommitLogReadAheadMode",
                remark: "Set read ahead mode for all commitlog files.",
            },
            Command {
                category: "Cluster",
                command: "clusterRT",
                remark: "List All clusters Message Send RT.",
            },
            Command {
                category: "Cluster",
                command: "clusterList",
                remark: "List cluster infos.",
            },
            Command {
                category: "Connection",
                command: "consumerConnection",
                remark: "Query consumer's socket connection, client version and subscription.",
            },
            Command {
                category: "Connection",
                command: "producerConnection",
                remark: "Query producer's socket connection and client version.",
            },
            Command {
                category: "Consumer",
                command: "consumerStatus",
                remark: "Query and display consumer's internal data structures.",
            },
            Command {
                category: "Consumer",
                command: "consumerProgress",
                remark: "Query consumer progress.",
            },
            Command {
                category: "Consumer",
                command: "consumer",
                remark: "Query consumer's connection, status, etc.",
            },
            Command {
                category: "Consumer",
                command: "deleteSubGroup",
                remark: "Delete subscription group from broker.",
            },
            Command {
                category: "Consumer",
                command: "getConsumerConfig",
                remark: "Get consumer config by subscription group name.",
            },
            Command {
                category: "Consumer",
                command: "updateSubGroup",
                remark: "Update consumer sub group.",
            },
            Command {
                category: "Consumer",
                command: "updateSubGroupList",
                remark: "Create or update subscription groups in batch.",
            },
            Command {
                category: "Consumer",
                command: "startMonitoring",
                remark: "Start Monitoring.",
            },
            Command {
                category: "Consumer",
                command: "setConsumeMode",
                remark: "Set consume mode for consumer group.",
            },
            Command {
                category: "Controller",
                command: "cleanBrokerMetadata",
                remark: "Clean metadata of broker on controller.",
            },
            Command {
                category: "Controller",
                command: "getControllerConfig",
                remark: "Get configuration of controller(s).",
            },
            Command {
                category: "Controller",
                command: "getControllerMetaData",
                remark: "Get meta data of controller.",
            },
            Command {
                category: "Controller",
                command: "electMaster",
                remark: "Re-elect the specified broker as master.",
            },
            Command {
                category: "Controller",
                command: "updateControllerConfig",
                remark: "Update configuration of controller(s).",
            },
            Command {
                category: "Export",
                command: "exportConfigs",
                remark: "Export configs",
            },
            Command {
                category: "Export",
                command: "exportMetadata",
                remark: "Export metadata.",
            },
            Command {
                category: "Export",
                command: "exportMetrics",
                remark: "Export metrics.",
            },
            Command {
                category: "Export",
                command: "exportMetadataInRocksDB",
                remark: "Export RocksDB kv config (topics/subscriptionGroups). Recommend to use [mqadmin \
                         rocksDBConfigToJson]",
            },
            Command {
                category: "Export",
                command: "exportPopRecord",
                remark: "Export pop consumer records.",
            },
            Command {
                category: "Export",
                command: "rocksDBConfigToJson",
                remark: "Convert RocksDB kv config to json.",
            },
            Command {
                category: "HA",
                command: "getSyncStateSet",
                remark: "Fetch sync state set for target brokers.",
            },
            Command {
                category: "HA",
                command: "haStatus",
                remark: "Fetch ha runtime status data.",
            },
            Command {
                category: "Lite",
                command: "getBrokerLiteInfo",
                remark: "Get broker lite info.",
            },
            Command {
                category: "Lite",
                command: "getLiteClientInfo",
                remark: "Get lite client info.",
            },
            Command {
                category: "Lite",
                command: "getLiteGroupInfo",
                remark: "Get lite group info.",
            },
            Command {
                category: "Lite",
                command: "getLiteTopicInfo",
                remark: "Get lite topic info.",
            },
            Command {
                category: "Lite",
                command: "getParentTopicInfo",
                remark: "Get parent topic info.",
            },
            Command {
                category: "Lite",
                command: "triggerLiteDispatch",
                remark: "Trigger Lite Dispatch.",
            },
            Command {
                category: "Message",
                command: "checkMsgSendRT",
                remark: "Check message send response time.",
            },
            Command {
                category: "Message",
                command: "consumeMessage",
                remark: "Consume message.",
            },
            Command {
                category: "Message",
                command: "decodeMessageId",
                remark: "Decode unique message ID.",
            },
            Command {
                category: "Message",
                command: "dumpCompactionLog",
                remark: "Parse compaction log to message.",
            },
            Command {
                category: "Message",
                command: "printMsg",
                remark: "Print Message Detail.",
            },
            Command {
                category: "Message",
                command: "printMsgByQueue",
                remark: "Print Message Detail by queueId.",
            },
            Command {
                category: "Message",
                command: "queryMsgById",
                remark: "Query message by message ID.",
            },
            Command {
                category: "Message",
                command: "queryMsgByKey",
                remark: "Query Message by Key.",
            },
            Command {
                category: "Message",
                command: "queryMsgByOffset",
                remark: "Query Message by offset.",
            },
            Command {
                category: "Message",
                command: "queryMsgByUniqueKey",
                remark: "Query Message by Unique key.",
            },
            Command {
                category: "Message",
                command: "queryMsgTraceById",
                remark: "Query message trace by message ID.",
            },
            Command {
                category: "Message",
                command: "sendMessage",
                remark: "Send a message.",
            },
            Command {
                category: "NameServer",
                command: "addWritePerm",
                remark: "Add write perm of broker in all name server.",
            },
            Command {
                category: "NameServer",
                command: "deleteKvConfig",
                remark: "Delete KV config.",
            },
            Command {
                category: "NameServer",
                command: "getNamesrvConfig",
                remark: "Get configs of name server.",
            },
            Command {
                category: "NameServer",
                command: "updateKvConfig",
                remark: "Create or update KV config.",
            },
            Command {
                category: "NameServer",
                command: "updateNamesrvConfig",
                remark: "Update configs of name server.",
            },
            Command {
                category: "NameServer",
                command: "wipeWritePerm",
                remark: "Wipe write perm of broker in all name server.",
            },
            Command {
                category: "Offset",
                command: "cloneGroupOffset",
                remark: "Clone offset from other group.",
            },
            Command {
                category: "Offset",
                command: "getConsumerStatus",
                remark: "Get consumer status from client.",
            },
            Command {
                category: "Offset",
                command: "resetOffsetByTime",
                remark: "Reset consumer group offsets to a specific timestamp (no restart required).",
            },
            Command {
                category: "Offset",
                command: "resetOffsetByTimeOld",
                remark: "Reset consumer offset by timestamp (execute this command required client restart).",
            },
            Command {
                category: "Offset",
                command: "skipAccumulatedMessage",
                remark: "Skip all messages that are accumulated (not consumed) currently.",
            },
            Command {
                category: "Producer",
                command: "producer",
                remark: "Query producer's instances, connection, status, etc.",
            },
            Command {
                category: "Queue",
                command: "checkRocksdbCqWriteProgress",
                remark: "Check if rocksdb cq is same as file cq.",
            },
            Command {
                category: "Queue",
                command: "queryCq",
                remark: "Query cq command.",
            },
            Command {
                category: "Stats",
                command: "statsAll",
                remark: "Topic and Consumer tps stats.",
            },
            Command {
                category: "Topic",
                command: "allocateMQ",
                remark: "Allocate MQ.",
            },
            Command {
                category: "Topic",
                command: "deleteTopic",
                remark: "Delete topic.",
            },
            Command {
                category: "Topic",
                command: "remappingStaticTopic",
                remark: "Remapping static topic.",
            },
            Command {
                category: "Topic",
                command: "topicClusterList",
                remark: "Get cluster info for topic.",
            },
            Command {
                category: "Topic",
                command: "topicList",
                remark: "Get topic list.",
            },
            Command {
                category: "Topic",
                command: "topicRoute",
                remark: "Examine topic route info.",
            },
            Command {
                category: "Topic",
                command: "topicStatus",
                remark: "Examine topic status info.",
            },
            Command {
                category: "Topic",
                command: "updateOrderConf",
                remark: "Create or update order conf.",
            },
            Command {
                category: "Topic",
                command: "updateStaticTopic",
                remark: "Update or create static topic.",
            },
            Command {
                category: "Topic",
                command: "updateTopicPerm",
                remark: "Update topic perm.",
            },
            Command {
                category: "Topic",
                command: "updateTopicList",
                remark: "Create or update topic configs in batch.",
            },
            Command {
                category: "Topic",
                command: "updateTopic",
                remark: "Update or create topic.",
            },
        ];
        let mut table = Table::new(commands);
        table.with(Style::extended());
        print!("{table}");
        Ok(())
    }
}