Skip to content

Commit

Permalink
修复jly8866#26 中提到的4个BUG,修复osc进度条更新时潜在的BUG
Browse files Browse the repository at this point in the history
  • Loading branch information
liujing committed Jun 30, 2017
1 parent 1a68fbb commit a12b876
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 113 deletions.
34 changes: 28 additions & 6 deletions sql/inception.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def criticalDDL(self, sqlContent):
else:
return None

def sqlautoReview(self, sqlContent, clusterName, isBackup='否'):
def sqlautoReview(self, sqlContent, clusterName, isSplit="no"):
'''
将sql交给inception进行自动审核,并返回审核结果。
'''
Expand All @@ -59,11 +59,33 @@ def sqlautoReview(self, sqlContent, clusterName, isBackup='否'):
if criticalDDL_check is not None:
result = criticalDDL_check
else:
sql="/*--user=%s;--password=%s;--host=%s;--enable-check=1;--port=%s;*/\
inception_magic_start;\
%s\
inception_magic_commit;" % (masterUser, masterPassword, masterHost, str(masterPort), sqlContent)
result = self._fetchall(sql, self.inception_host, self.inception_port, '', '', '')
if isSplit == "yes":
# 这种场景只给osc进度功能使用
# 如果一个工单中同时包含DML和DDL,那么执行时被split后的SQL与提交的SQL会不一样(会在每条语句前面加use database;),导致osc进度更新取不到正确的SHA1值。
# 请参考inception文档中--enable-split参数的说明
sqlSplit = "/*--user=%s; --password=%s; --host=%s; --enable-execute;--port=%s; --enable-ignore-warnings;--enable-split;*/\
inception_magic_start;\
%s\
inception_magic_commit;" % (masterUser, masterPassword, masterHost, str(masterPort), sqlContent)
splitResult = self._fetchall(sqlSplit, self.inception_host, self.inception_port, '', '', '')
tmpList = []
for splitRow in splitResult:
sqlTmp = splitRow[1]
sql = "/*--user=%s;--password=%s;--host=%s;--enable-check=1;--port=%s; --enable-ignore-warnings;*/\
inception_magic_start;\
%s\
inception_magic_commit;" % (masterUser, masterPassword, masterHost, str(masterPort), sqlTmp)
reviewResult = self._fetchall(sql, self.inception_host, self.inception_port, '', '', '')
for row in reviewResult:
tmpList.append(list(row))
result = tmpList
else:
# 工单审核使用
sql="/*--user=%s;--password=%s;--host=%s;--enable-check=1;--port=%s;*/\
inception_magic_start;\
%s\
inception_magic_commit;" % (masterUser, masterPassword, masterHost, str(masterPort), sqlContent)
result = self._fetchall(sql, self.inception_host, self.inception_port, '', '', '')
return result

def executeFinal(self, workflowDetail, dictConn):
Expand Down
8 changes: 6 additions & 2 deletions sql/static/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,11 @@ <h4>单子名称:<span id="editWorkflowNname">{{workflowDetail.workflow_name}}
<input type="hidden" name="workflowid" value="{{workflowDetail.id}}">
<input type="submit" id="btnRollback" type="button" class="btn btn-default" data-loading-text="Loading..." value="查看回滚SQL" />
</form>
{% elif workflowDetail.status == '自动审核不通过' or workflowDetail.status == '人工终止流程' or workflowDetail.status == '执行中' or workflowDetail.status == '执行有异常' %}
<a type='button' id="btnEditSql" class='btn btn-warning' href="/editsql/">重新修改</a>
{% elif workflowDetail.status == '自动审核不通过' or workflowDetail.status == '人工终止流程' or workflowDetail.status == '执行有异常' %}
{% if workflowDetail.engineer == loginUser %}
<!--只允许发起人修改工单-->
{% csrf_token %}
<a type='button' id="btnEditSql" class='btn btn-warning' href="/editsql/">重新修改</a>
{% endif %}
{% endif %}
{% endblock content%}
235 changes: 156 additions & 79 deletions sql/static/user/js/detail.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,90 +22,144 @@ var workflowid = $("#workflowDetail_id").val();
var sqlMaxRowNumber = parseInt($("#sqlMaxRowNumber").val());
var key;
var isStoped = 0;
var wfStatus = -1;
var retryCnt = 1;
var executing = 0;

function get_pct(wid, sqlNum){
if(sqlNum > sqlMaxRowNumber){
getWorkflowStatus(wid); //最后一条SQL的进度刷新完后,请求后端接口获取整个工单的状态,如果不为“执行中”状态,则提示刷新当前页面;如果是“执行中”,则每隔1秒查询工单的状态,共重试60次
// console.log('finish1');
var returned = confirm("执行完毕,请确认是否刷新当前页面?");
if (returned){
window.location.reload(true);
}
return;
}
if (isStoped == 1){
document.getElementById("btnstop_" + sqlNum).style.display="none";
return false;
}
var pct;
$.ajax({
type: "post",
async:false,
url: "/getOscPercent/",
dataType: "json",
data: {
workflowid: wid,
sqlID: sqlNum
},
complete: function () {
},
success: function (data) {
// console.log(data);
if (data.status == 4){
// 不是由pt-OSC执行的
pct = "N/A";
document.getElementById("td_" + sqlNum).innerHTML = "不由pt-OSC执行";
itemIndex++;
clearTimeout(key);
key = setTimeout(function(){
get_pct(wid,itemIndex);
},500)
}
else if (data.status == 1){
// 该行SQL有应对的SHA1值,说明是pt-OSC执行的,但是到inception查询不到它的进度信息,可以判定为执行完毕
document.getElementById("btnstop_" + sqlNum).style.display="none";
itemIndex++;
// console.log('===>' + itemIndex);
pct = 100;
document.getElementById("div_" + sqlNum).style.width = pct + "%";
document.getElementById("span_" + sqlNum).innerHTML = pct + "%";
// console.log('per:' + pct);
clearTimeout(key);
key = setTimeout(function(){
get_pct(wid,itemIndex);
},500)
}
else if(data.status == 0){
// 在inception能查询到它的进度信息,说明正在执行
pct = data.data.percent;
// console.log('per:' + pct);
document.getElementById("div_" + sqlNum).style.width = pct + "%";
document.getElementById("span_" + sqlNum).innerHTML = pct + "%";
if(pct == 100){
document.getElementById("btnstop_" + sqlNum).style.display="none";
itemIndex++;
clearTimeout(key);
key = setTimeout(function(){
get_pct(wid,itemIndex);
},500)
}else{
document.getElementById("btnstop_" + sqlNum).style.display="";
clearTimeout(key);
key = setTimeout(function(){
get_pct(wid,sqlNum);
},500)
}

}
else{
alert(data.msg);
return;
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
if (wfStatus != -1 && wfStatus != "执行中") {
var returned = confirm("执行完毕,请确认是否刷新当前页面?");
if (returned) {
window.location.reload(true);
}
});
return pct;
}else {
document.getElementById("workflowDetail_status").innerHTML = "确认中...";
if (retryCnt <= 60){
clearTimeout(key);
key = setTimeout(function () {
get_pct(wid, sqlNum);
}, 1000);
retryCnt++;
}
else {
retryCnt = 1;
alert("该工单一分钟仍然未执行完毕,请稍后尝试手动刷新本页面");
}

}
}

else {

if (isStoped == 1) {
document.getElementById("btnstop_" + sqlNum).style.display = "none";
return false;
}
var pct;
$.ajax({
type: "post",
async: false,
url: "/getOscPercent/",
dataType: "json",
data: {
workflowid: wid,
sqlID: sqlNum
},
complete: function () {
},
success: function (data) {
//console.log(data);
if (data.status == 4) {
// 不是由pt-OSC执行的,比如DML语句
pct = "N/A";
document.getElementById("td_" + sqlNum).innerHTML = "不由pt-OSC执行";
itemIndex++;
executing = 0;
clearTimeout(key);
key = setTimeout(function () {
get_pct(wid, itemIndex);
}, 1000)
}
else if (data.status == 1) {
// 该行SQL有应对的SHA1值,说明该SQL会由pt-OSC执行,但是到inception查询不到它的进度信息;有2种情况:
// 1.正在执行DML语句,inception实际还未执行到这一行,只是本js轮循到这一行了而已
// 2.已执行完毕
if (executing == 0) {
//第1种情况,这里进行5次重试,每次间隔1秒
if (retryCnt <= 5) {
clearTimeout(key);
//document.getElementById("span_" + sqlNum).style.color = "#F00";
document.getElementById("div_" + sqlNum).className = "progress";
document.getElementById("div_" + sqlNum).style.width = "100%";
document.getElementById("span_" + sqlNum).innerHTML = "查询中...";
key = setTimeout(function () {
get_pct(wid, itemIndex);
}, 1000);
//console.log("retryCnt: " + retryCnt + ",itemIndex: " + itemIndex);
retryCnt++;
} else {
retryCnt = 1;
document.getElementById("div_" + sqlNum).className = "progress-bar";
itemIndex++;
executing = 0;
// console.log('===>' + itemIndex);
pct = 100;
document.getElementById("div_" + sqlNum).style.width = pct + "%";
document.getElementById("span_" + sqlNum).innerHTML = pct + "%";
get_pct(wid, itemIndex);
// console.log('per:' + pct);
}
}else{
//第2种情况
document.getElementById("btnstop_" + sqlNum).style.display = "none";
itemIndex++;
executing = 0;
pct = 100;
document.getElementById("div_" + sqlNum).style.width = pct + "%";
document.getElementById("span_" + sqlNum).innerHTML = pct + "%";
get_pct(wid, itemIndex);
}
}
else if (data.status == 0) {
// 在inception能查询到它的进度信息,说明正在执行
pct = data.data.percent;
// console.log('per:' + pct);
document.getElementById("div_" + sqlNum).className = "progress-bar";
document.getElementById("div_" + sqlNum).style.width = pct + "%";
document.getElementById("span_" + sqlNum).innerHTML = pct + "%";
executing = 1;
if (pct == 100) {
document.getElementById("btnstop_" + sqlNum).style.display = "none";
itemIndex++;
executing = 0;
clearTimeout(key);
key = setTimeout(function () {
get_pct(wid, itemIndex);
}, 800)
} else {
document.getElementById("btnstop_" + sqlNum).style.display = "";
clearTimeout(key);
key = setTimeout(function () {
get_pct(wid, sqlNum);
}, 800)
}

}
else {
alert(data.msg);
return;
}

},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
return pct;
}
}

function stopOsc(wid, sqlNum){
Expand Down Expand Up @@ -137,6 +191,29 @@ function stopOsc(wid, sqlNum){
}
}

function getWorkflowStatus(wid){
if (wid > 0) {
// console.log('get workflow status');
$.ajax({
type: "post",
async: false,
url: "/getWorkflowStatus/",
dataType: "json",
data: {
workflowid: wid
},
complete: function () {
},
success: function (data) {
wfStatus = data.status;
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
}
}

function execute(){
//点击执行之后,刷新当前页面,以显示执行进度
clearTimeout(key);
Expand Down
1 change: 1 addition & 0 deletions sql/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
url(r'^getMonthCharts/$', views_ajax.getMonthCharts, name='getMonthCharts'),
url(r'^getPersonCharts/$', views_ajax.getPersonCharts, name='getPersonCharts'),
url(r'^getOscPercent/$', views_ajax.getOscPercent, name='getOscPercent'),
url(r'^getWorkflowStatus/$', views_ajax.getWorkflowStatus, name='getWorkflowStatus'),
url(r'^stopOscProgress/$', views_ajax.stopOscProgress, name='stopOscProgress'),
]
Loading

0 comments on commit a12b876

Please sign in to comment.