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不能执行长sql的问题 #1556

Merged
merged 5 commits into from
Jun 3, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 32 additions & 28 deletions sql/engines/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import subprocess
import simplejson as json
import datetime
import tempfile
from bson.son import SON
from bson import json_util
from pymongo.errors import OperationFailure
Expand Down Expand Up @@ -254,38 +255,41 @@ class MongoEngine(EngineBase):

def exec_cmd(self, sql, db_name=None, slave_ok=''):
"""审核时执行的语句"""


if self.user and self.password and self.port and self.host:
msg = ""
auth_db = self.instance.db_name or 'admin'
try:
if not sql.startswith('var host='): #在master节点执行的情况
cmd = "{mongo} --quiet -u {uname} -p '{password}' {host}:{port}/{auth_db} <<\\EOF\ndb=db.getSiblingDB(\"{db_name}\");{slave_ok}printjson({sql})\nEOF".format(
mongo=mongo, uname=self.user, password=self.password, host=self.host, port=self.port, db_name=db_name, sql=sql, auth_db=auth_db, slave_ok=slave_ok)
else:
cmd = "{mongo} --quiet -u {user} -p '{password}' {host}:{port}/{auth_db} <<\\EOF\nrs.slaveOk();{sql}\nEOF".format(
mongo=mongo, user=self.user, password=self.password, host=self.host, port=self.port, db_name=db_name, sql=sql, auth_db=auth_db)
logger.debug(cmd)
p = subprocess.Popen(cmd, shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
re_msg = []
for line in iter(p.stdout.read, ''):
re_msg.append(line)
# 因为返回的line中也有可能带有换行符,因此需要先全部转换成字符串
__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()}")
# 因为要知道具体的临时文件位置,所以用了NamedTemporaryFile模块
with tempfile.NamedTemporaryFile(suffix=".js", prefix="mongo_", dir='/tmp/', delete=True) as fp:
fp.write(sql.encode('utf-8'))
try:
if not sql.startswith('var host='): #在master节点执行的情况
cmd = "{mongo} --quiet -u {uname} -p '{password}' {host}:{port}/{auth_db} <<\\EOF\ndb=db.getSiblingDB(\"{db_name}\");{slave_ok}load('{tempfile}')\nEOF".format(
mongo=mongo, uname=self.user, password=self.password, host=self.host, port=self.port,
db_name=db_name, sql=sql, auth_db=auth_db, slave_ok=slave_ok, tempfile=fp.name)
else:
cmd = "{mongo} --quiet -u {user} -p '{password}' {host}:{port}/{auth_db} <<\\EOF\nrs.slaveOk();{sql}\nEOF".format(
mongo=mongo, user=self.user, password=self.password, host=self.host, port=self.port, db_name=db_name, sql=sql, auth_db=auth_db)
p = subprocess.Popen(cmd, shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
re_msg = []
for line in iter(p.stdout.read, ''):
re_msg.append(line)
# 因为返回的line中也有可能带有换行符,因此需要先全部转换成字符串
__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

def get_master(self):
Expand Down