Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mongodb执行时出现WARNING的优化 #1237

Closed
weideguo opened this issue Nov 17, 2021 · 4 comments
Closed

mongodb执行时出现WARNING的优化 #1237

weideguo opened this issue Nov 17, 2021 · 4 comments

Comments

@weideguo
Copy link
Collaborator

目前遇到的问题/使用障碍

使用mongo命令连接部分mongodb实例时会出现告警,从而导致部分函数执行失败如 get_slave

WARNING: slaveOk() is deprecated and may be removed in the next major release. Please use secondaryOk() instead.

希望如何解决/实现它

增加一个补丁 mongo.py.20211117.patch

diff --git a/sql/engines/mongo.py b/sql/engines/mongo.py
index 8e7143f..86e7c99 100644
--- a/sql/engines/mongo.py
+++ b/sql/engines/mongo.py
@@ -271,7 +271,18 @@ class MongoEngine(EngineBase):
                 re_msg = []
                 for line in iter(p.stdout.read, ''):
                     re_msg.append(line)
-                msg = '\n'.join(re_msg)
+
+                __msg = '\n'.join(re_msg)
+
+                _re_msg = []
+                for _line in __msg.split('\n'):
+                    if not _re_msg and re.match('WARNING.*', _line):
+                        # 第一行可能是WARNING语句,因此跳过
+                        continue
+                    _re_msg.append(_line)
+
+                msg = '\n'.join(_re_msg)
+
             except Exception as e:
                 logger.warning(f"mongo语句执行报错,语句:{sql},{e}错误信息{traceback.format_exc()}")
         return msg
 

应用补丁

patch -p1 < mongo.py.20211117.patch
@weideguo
Copy link
Collaborator Author

不应该贸然把 slaveOk() 改成 secondaryOk(),因为有些mongodb实例不支持后者

@peng19832
Copy link
Contributor

peng19832 commented Nov 19, 2021

目前遇到的问题/使用障碍

使用mongo命令连接部分mongodb实例时会出现告警,从而导致部分函数执行失败如 get_slave

WARNING: slaveOk() is deprecated and may be removed in the next major release. Please use secondaryOk() instead.

希望如何解决/实现它

增加一个补丁 mongo.py.20211117.patch

diff --git a/sql/engines/mongo.py b/sql/engines/mongo.py
index 8e7143f..86e7c99 100644
--- a/sql/engines/mongo.py
+++ b/sql/engines/mongo.py
@@ -271,7 +271,18 @@ class MongoEngine(EngineBase):
                 re_msg = []
                 for line in iter(p.stdout.read, ''):
                     re_msg.append(line)
-                msg = '\n'.join(re_msg)
+
+                __msg = '\n'.join(re_msg)
+
+                _re_msg = []
+                for _line in __msg.split('\n'):
+                    if not _re_msg and re.match('WARNING.*', _line):
+                        # 第一行可能是WARNING语句,因此跳过
+                        continue
+                    _re_msg.append(_line)
+
+                msg = '\n'.join(_re_msg)
+
             except Exception as e:
                 logger.warning(f"mongo语句执行报错,语句:{sql},{e}错误信息{traceback.format_exc()}")
         return msg
 

应用补丁

patch -p1 < mongo.py.20211117.patch

大佬,能加个版本判断不,老版本用slaveok,新版本用新的命令

@weideguo
Copy link
Collaborator Author

执行前加版本判断太不优雅

@hhyo
Copy link
Owner

hhyo commented Jan 8, 2022

可提pr,谢谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants