From 1cf01197bb9f51405c1c4365a97c56ec430ff73a Mon Sep 17 00:00:00 2001 From: Kjell Winblad Date: Mon, 13 Mar 2023 14:21:51 +0100 Subject: [PATCH] fix: increase heartbeat time to avoid extreme MongoDB logging Our MongoDB driver creates a new temporary connection, for every active connection, to just do a single heartbeat test. There is configurable delay between every heartbeat test. When the user has an EMQX cluster with a MongoDB bridge (to a MongoDB replica set), there will be a lot of connections. Furthermore, as MongoDB creates a log entry every time a new connection is created, the log will be flooded with info about new connection. One user have reported more than 1MB of log data in a 10 minute period. This commit tries to fix this by increasing the default delay between heartbeats. A better fix would be to change the MongoDB driver so that it does not create a new connection just to do a heartbeat check, but this is more complicated so we leave this to the future. We might also swap out the current MongoDB driver to something better. Fixes: https://github.com/emqx/emqx/issues/9851 --- apps/emqx_connector/i18n/emqx_connector_mongo.conf | 4 ++-- apps/emqx_connector/src/emqx_connector_mongo.erl | 8 +++++++- changes/ce/fix-10124.en.md | 1 + changes/ce/fix-10124.zh.md | 1 + 4 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 changes/ce/fix-10124.en.md create mode 100644 changes/ce/fix-10124.zh.md diff --git a/apps/emqx_connector/i18n/emqx_connector_mongo.conf b/apps/emqx_connector/i18n/emqx_connector_mongo.conf index 1f00083a4..6a2511ec8 100644 --- a/apps/emqx_connector/i18n/emqx_connector_mongo.conf +++ b/apps/emqx_connector/i18n/emqx_connector_mongo.conf @@ -177,8 +177,8 @@ The MongoDB default port 27017 is used if `[:Port]` is not specified.""" heartbeat_period { desc { - en: "Controls when the driver checks the state of the MongoDB deployment. Specify the interval between checks, counted from the end of the previous check until the beginning of the next one." - zh: "控制驱动程序何时检查MongoDB部署的状态。指定检查的间隔时间,从上一次检查结束到下一次检查开始计算。" + en: "Controls when the driver checks the state of the MongoDB deployment. Specify the interval between checks, counted from the end of the previous check until the beginning of the next one. If the number of connections is increased (which will happen, for example, if you increase the pool size), you may need to increase this period as well to avoid creating too many log entries in the MongoDB log file." + zh: "控制驱动程序何时检查MongoDB部署的状态。指定检查的间隔时间,从上一次检查结束到下一次检查开始计算。如果连接数增加(例如,如果你增加池子的大小,就会发生这种情况),你可能也需要增加这个周期,以避免在MongoDB日志文件中创建太多的日志条目。" } label { en: "Heartbeat period" diff --git a/apps/emqx_connector/src/emqx_connector_mongo.erl b/apps/emqx_connector/src/emqx_connector_mongo.erl index 1b0bcf94d..8804ebaf2 100644 --- a/apps/emqx_connector/src/emqx_connector_mongo.erl +++ b/apps/emqx_connector/src/emqx_connector_mongo.erl @@ -106,7 +106,7 @@ fields(topology) -> {socket_timeout_ms, duration("socket_timeout")}, {server_selection_timeout_ms, duration("server_selection_timeout")}, {wait_queue_timeout_ms, duration("wait_queue_timeout")}, - {heartbeat_frequency_ms, duration("heartbeat_period")}, + {heartbeat_frequency_ms, fun heartbeat_frequency_ms/1}, {min_heartbeat_frequency_ms, duration("min_heartbeat_period")} ]. @@ -407,6 +407,12 @@ duration(Desc) -> desc => ?DESC(Desc) }. +heartbeat_frequency_ms(type) -> emqx_schema:duration_ms(); +heartbeat_frequency_ms(desc) -> ?DESC("heartbeat_period"); +heartbeat_frequency_ms(default) -> 200000; +heartbeat_frequency_ms(validator) -> [?MIN(1)]; +heartbeat_frequency_ms(_) -> undefined. + max_overflow(type) -> non_neg_integer(); max_overflow(desc) -> ?DESC("max_overflow"); max_overflow(default) -> 0; diff --git a/changes/ce/fix-10124.en.md b/changes/ce/fix-10124.en.md new file mode 100644 index 000000000..1a4aca3d9 --- /dev/null +++ b/changes/ce/fix-10124.en.md @@ -0,0 +1 @@ +The default heartbeat period for MongoDB has been increased to reduce the risk of too excessive logging to the MongoDB log file. diff --git a/changes/ce/fix-10124.zh.md b/changes/ce/fix-10124.zh.md new file mode 100644 index 000000000..7605f2da3 --- /dev/null +++ b/changes/ce/fix-10124.zh.md @@ -0,0 +1 @@ +增加了MongoDB的默认心跳周期,以减少对MongoDB日志文件的过多记录的风险。