Repository: robustmq/robustmq (language: rust)

Issue:
[BUG] Connection jitter can lead to the connection being banned during testing.
### Have you checked the documentation for submitting an Issue?

- [x] Yes.

### What type of bug is this?

Incorrect result

### What subsystems are affected?

Datanode

### Minimal reproduce step

no

### What did you expect to see?

no

### What did you see instead?

no

### What operating system did you use?

macos

### Relevant log output and stack trace

```bash
no
```

### Are you willing to submit PR?

- [ ] Yes. I would be willing to submit a PR with guidance from the RobustMQ community to improve.
- [x] No. I cannot submit a PR at this time.


Discussion / hints:

Fix patch:
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 325aae0c0..b4c5c0714 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -115,12 +115,13 @@ repos:
         language: system
         files: \.rs$
         pass_filenames: false
-      - id: cargo-test
-        stages:
-          - pre-push
-        name: cargo test
-        description: unit test for the project
-        entry: bash -c 'make test'
-        language: system
-        files: \.rs$
-        pass_filenames: false
+# we think it is not necessary to run tests in pre-commit
+#      - id: cargo-test
+#        stages:
+#          - pre-push
+#        name: cargo test
+#        description: unit test for the project
+#        entry: bash -c 'make test'
+#        language: system
+#        files: \.rs$
+#        pass_filenames: false
diff --git a/config/journal-server.toml b/config/journal-server.toml
index 63842d295..7809e298f 100644
--- a/config/journal-server.toml
+++ b/config/journal-server.toml
@@ -57,5 +57,4 @@ max_segment_size = 1048576
 
 [log]
 log_config = "./config/log4rs.yaml"
-log_path = "./logs/journal-server"
-
+log_path = "./robust-data/journal-server/logs"
diff --git a/docs/.design/plan/mqtt-stable-to-do.md b/docs/.design/plan/mqtt-stable-to-do.md
index 9518693a6..f99d42d64 100644
--- a/docs/.design/plan/mqtt-stable-to-do.md
+++ b/docs/.design/plan/mqtt-stable-to-do.md
@@ -44,8 +44,8 @@
    20. 自动订阅
    21. 主题重写
    22. 通配符订阅
-3.  
-   
+3.
+
 ## 压测
 1. Placement 集群模式 Read/Write 延时和吞吐压测
 2. MQTT 发布订阅模式压测
@@ -93,4 +93,4 @@
    -  集成 promethrus
    -  集成 OpenTelemetry
 -  MQTT Over Quic
--  
\ No newline at end of file
+-
diff --git a/docs/en/ContributionGuide/ContributingCode/Cargo-Running.md b/docs/en/ContributionGuide/ContributingCode/Cargo-Running.md
index 27ff06cf1..520741484 100644
--- a/docs/en/ContributionGuide/ContributingCode/Cargo-Running.md
+++ b/docs/en/ContributionGuide/ContributingCode/Cargo-Running.md
@@ -15,5 +15,13 @@
    cargo run --package cmd --bin mqtt-server -- --conf=config/mqtt-server.toml
    ```
 
-   If the following output is displayed, it indicates that the mqtt-server has started successfully:
-   ![image](../../../images/Cargo-Running-2.png)
+   If the following output is displayed, it indicates that the mqtt-server has started successfully:![Cargo-Running-run-mqtt-server-2.png](../../../images/Cargo-Running-run-mqtt-server-2.png)
+
+
+3. **Run standalone by journal-server**
+
+   ```
+   cargo run --package cmd --bin journal-server -- --conf=config/journal-server.toml
+   ```
+   The following output indicates that the journal-server has started successfully:
+   ![Cargo-Running-run-journal-server-3.png](../../../images/Cargo-Running-run-journal-server-3.png)
diff --git a/docs/images/Cargo-Running-run-journal-server-3.png b/docs/images/Cargo-Running-run-journal-server-3.png
new file mode 100644
index 000000000..5ebd09df0
Binary files /dev/null and b/docs/images/Cargo-Running-run-journal-server-3.png differ
diff --git a/docs/images/Cargo-Running-2.png b/docs/images/Cargo-Running-run-mqtt-server-2.png
similarity index 100%
rename from docs/images/Cargo-Running-2.png
rename to docs/images/Cargo-Running-run-mqtt-server-2.png
diff --git a/docs/images/WechatGroup.png b/docs/images/WechatGroup.png
index 541daae85..058853042 100644
Binary files a/docs/images/WechatGroup.png and b/docs/images/WechatGroup.png differ
diff --git a/docs/zh/ContributionGuide/ContributingCode/Cargo-Running.md b/docs/zh/ContributionGuide/ContributingCode/Cargo-Running.md
index a209b224e..ac6c95a7c 100644
--- a/docs/zh/ContributionGuide/ContributingCode/Cargo-Running.md
+++ b/docs/zh/ContributionGuide/ContributingCode/Cargo-Running.md
@@ -12,5 +12,13 @@ cargo run --package cmd --bin placement-center -- --conf=config/placement-center
 cargo run --package cmd --bin mqtt-server -- --conf=config/mqtt-server.toml
 ```
 
-输出如下信息，标识 mqtt-server 启动成功：
-![image](../../../images/Cargo-Running-2.png)
+输出如下信息，表示 mqtt-server 启动成功：
+![Cargo-Running-run-mqtt-server-2.png](../../../images/Cargo-Running-run-mqtt-server-2.png)
+
+3. Run standalone by journal-server
+
+```
+cargo run --package cmd --bin journal-server -- --conf=config/journal-server.toml
+```
+输出如下信息，表示 journal-server 启动成功:
+![Cargo-Running-run-journal-server-3.png](../../../images/Cargo-Running-run-journal-server-3.png)
diff --git a/src/mqtt-broker/src/bridge/mod.rs b/src/mqtt-broker/src/bridge/mod.rs
index 2f907a43e..88041165f 100644
--- a/src/mqtt-broker/src/bridge/mod.rs
+++ b/src/mqtt-broker/src/bridge/mod.rs
@@ -11,4 +11,3 @@
 // 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.
-
diff --git a/src/mqtt-broker/src/handler/delay_message.rs b/src/mqtt-broker/src/handler/delay_message.rs
index b7861a5b3..e56f14b54 100644
--- a/src/mqtt-broker/src/handler/delay_message.rs
+++ b/src/mqtt-broker/src/handler/delay_message.rs
@@ -1,3 +1,17 @@
+// Copyright 2023 RobustMQ Team
+//
+// 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.
+
 pub fn is_delay_message() -> bool {
     true
 }
@@ -6,11 +20,8 @@ pub fn decode_delay_message() -> bool {
     true
 }
 
-
 #[cfg(test)]
 mod test {
     #[test]
-    pub fn is_delay_message_test() {
-
-    }
-}
\ No newline at end of file
+    pub fn is_delay_message_test() {}
+}
diff --git a/src/mqtt-broker/src/handler/flapping_detect.rs b/src/mqtt-broker/src/handler/flapping_detect.rs
index f8809c10a..35a29a1e2 100644
--- a/src/mqtt-broker/src/handler/flapping_detect.rs
+++ b/src/mqtt-broker/src/handler/flapping_detect.rs
@@ -35,7 +35,7 @@ pub struct UpdateFlappingDetectCache {
 #[derive(Clone, Debug)]
 pub struct FlappingDetectCondition {
     pub client_id: String,
-    pub connect_times: u64,
+    pub before_last_window_connections: u64,
     pub first_request_time: u64,
 }
 
@@ -100,7 +100,7 @@ pub fn check_flapping_detect(client_id: String, cache_manager: &Arc<CacheManager
     } else {
         FlappingDetectCondition {
             client_id: client_id.clone(),
-            connect_times: current_counter,
+            before_last_window_connections: current_counter + 1,
             first_request_time: current_request_time,
         }
     };
@@ -115,6 +115,7 @@ pub fn check_flapping_detect(client_id: String, cache_manager: &Arc<CacheManager
 
     let config = cache_manager.get_flapping_detect_config();
     let current_counter = event_metrics::get_client_connection_counter(client_id.clone());
+    debug!("get current_counter : {current_counter} by client_id: {client_id}");
 
     if is_within_window_time(
         current_request_time,
@@ -122,7 +123,7 @@ pub fn check_flapping_detect(client_id: String, cache_manager: &Arc<CacheManager
         config.window_time as u64,
     ) && is_exceed_max_client_connections(
         current_counter,
-        flapping_detect_condition.connect_times,
+        flapping_detect_condition.before_last_window_connections,
         config.max_client_connections,
     ) {
         debug!("add a new client_id: {client_id} into blacklist.");
@@ -164,7 +165,7 @@ fn is_exceed_max_client_connections(
     connect_times: u64,
     max_client_connections: u64,
 ) -> bool {
-    current_time - connect_times > max_client_connections
+    current_time - connect_times >= max_client_connections
 }
 
 pub async fn enable_flapping_detect(
diff --git a/src/mqtt-broker/src/lib.rs b/src/mqtt-broker/src/lib.rs
index d7180b00a..6a6ed509b 100644
--- a/src/mqtt-broker/src/lib.rs
+++ b/src/mqtt-broker/src/lib.rs
@@ -56,12 +56,12 @@ lazy_static! {
     pub static ref BROKER_START_TIME: u64 = now_second();
 }
 
+pub mod bridge;
 pub mod handler;
 pub mod observability;
 pub mod security;
 mod server;
 pub mod storage;
-pub mod bridge;
 mod subscribe;
 
 pub fn start_mqtt_broker_server(stop_send: broadcast::Sender<bool>) {
diff --git a/src/mqtt-broker/src/security/acl/metadata.rs b/src/mqtt-broker/src/security/acl/metadata.rs
index 5a29871ec..e71449015 100644
--- a/src/mqtt-broker/src/security/acl/metadata.rs
+++ b/src/mqtt-broker/src/security/acl/metadata.rs
@@ -252,12 +252,12 @@ mod test {
         let acl_metadata = AclMetadata::new();
         let condition1 = FlappingDetectCondition {
             client_id: "test_id_1".to_string(),
-            connect_times: 15,
+            before_last_window_connections: 15,
             first_request_time: now_second() - 10,
         };
         let condition2 = FlappingDetectCondition {
             client_id: "test_id_2".to_string(),
-            connect_times: 15,
+            before_last_window_connections: 15,
             first_request_time: now_second() - 70,
         };
 


Test patch:
diff --git a/tests/tests/mqtt_client/flapping_detect_test.rs b/tests/tests/mqtt_client/flapping_detect_test.rs
index 4f1d6404d..df315158e 100644
--- a/tests/tests/mqtt_client/flapping_detect_test.rs
+++ b/tests/tests/mqtt_client/flapping_detect_test.rs
@@ -36,14 +36,14 @@ mod tests {
     async fn test_enable_flapping_detect() {
         // per
         let request = EnableFlappingDetectRequest {
-            is_enable: false,
-            window_time: 0,
-            max_client_connections: 0,
-            ban_time: 0,
+            is_enable: true,
+            window_time: 1,
+            max_client_connections: 15,
+            ban_time: 1,
         };
 
         // result
-        let reply = EnableFlappingDetectReply { is_enable: false };
+        let reply = EnableFlappingDetectReply { is_enable: true };
 
         // action
         match open_flapping_detect(request).await {
@@ -56,20 +56,17 @@ mod tests {
                 std::process::exit(1);
             }
         }
-    }
 
-    #[tokio::test]
-    async fn test_enable_flapping_detect_is_true() {
         // per
         let request = EnableFlappingDetectRequest {
-            is_enable: true,
+            is_enable: false,
             window_time: 1,
             max_client_connections: 15,
             ban_time: 1,
         };
 
         // result
-        let reply = EnableFlappingDetectReply { is_enable: true };
+        let reply = EnableFlappingDetectReply { is_enable: false };
 
         // action
         match open_flapping_detect(request).await {
diff --git a/tests/tests/mqtt_protocol/connect34_test.rs b/tests/tests/mqtt_protocol/connect34_test.rs
index 61d7638ad..b201b7f0e 100644
--- a/tests/tests/mqtt_protocol/connect34_test.rs
+++ b/tests/tests/mqtt_protocol/connect34_test.rs
@@ -142,9 +142,9 @@ mod tests {
 
         let request = EnableFlappingDetectRequest {
             is_enable: true,
-            window_time: 20,
-            max_client_connections: 2,
-            ban_time: 5,
+            window_time: 60,
+            max_client_connections: 20,
+            ban_time: 1,
         };
 
         let _reply = mqtt_broker_enable_flapping_detect(&client_pool, &grpc_addr, request)
@@ -158,9 +158,9 @@ mod tests {
 
         let request = EnableFlappingDetectRequest {
             is_enable: false,
-            window_time: 20,
-            max_client_connections: 2,
-            ban_time: 5,
+            window_time: 60,
+            max_client_connections: 20,
+            ban_time: 1,
         };
 
         let _reply = mqtt_broker_enable_flapping_detect(&client_pool, &grpc_addr, request)
@@ -168,7 +168,6 @@ mod tests {
             .unwrap();
     }
 
-    #[ignore = "reason: flapping detect is not supported in v3"]
     #[tokio::test]
     async fn client_flapping_detect_test() {
         open_flapping_detect().await;
@@ -181,21 +180,17 @@ mod tests {
             ssl: false,
         };
 
-        let create_opts = build_create_pros(
-            &client_test_properties.client_id,
-            &client_test_properties.addr,
-        );
-        let cli = Client::new(create_opts).unwrap_or_else(|err| {
-            println!("Error creating the client: {:?}", err);
-            process::exit(1);
-        });
+        for _i in 0..20 {
+            test_correct_connect(&client_test_properties);
+        }
 
-        let conn_opts = build_conn_pros(client_test_properties.clone(), false);
+        test_correct_connect(&client_test_properties);
+        test_fail_connect(&client_test_properties);
 
-        assert!(cli.connect(conn_opts.clone()).is_ok());
-
-        distinct_conn(cli);
+        close_flapping_detect().await;
+    }
 
+    fn test_fail_connect(client_test_properties: &ClientTestProperties) {
         let create_opts = build_create_pros(
             &client_test_properties.client_id,
             &client_test_properties.addr,
@@ -206,11 +201,10 @@ mod tests {
         });
 
         let conn_opts = build_conn_pros(client_test_properties.clone(), false);
+        assert!(cli.connect(conn_opts.clone()).is_err());
+    }
 
-        assert!(cli.connect(conn_opts.clone()).is_ok());
-
-        distinct_conn(cli);
-
+    fn test_correct_connect(client_test_properties: &ClientTestProperties) {
         let create_opts = build_create_pros(
             &client_test_properties.client_id,
             &client_test_properties.addr,
@@ -225,19 +219,5 @@ mod tests {
         assert!(cli.connect(conn_opts.clone()).is_ok());
 
         distinct_conn(cli);
-
-        let create_opts = build_create_pros(
-            &client_test_properties.client_id,
-            &client_test_properties.addr,
-        );
-        let cli = Client::new(create_opts).unwrap_or_else(|err| {
-            println!("Error creating the client: {:?}", err);
-            process::exit(1);
-        });
-
-        let conn_opts = build_conn_pros(client_test_properties.clone(), false);
-        assert!(cli.connect(conn_opts.clone()).is_err());
-
-        close_flapping_detect().await;
     }
 }