2016-05-16 19:49:56 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var util = require('util');
|
|
|
|
var JobBase = require('./job_base');
|
2016-10-10 18:09:13 +08:00
|
|
|
var JobStatus = require('../job_status');
|
2016-06-01 16:39:01 +08:00
|
|
|
var QueryFallback = require('./query/query_fallback');
|
|
|
|
var MainFallback = require('./query/main_fallback');
|
|
|
|
var QueryFactory = require('./query/query_factory');
|
2016-05-16 19:49:56 +08:00
|
|
|
|
2016-05-23 17:37:09 +08:00
|
|
|
function JobFallback(jobDefinition) {
|
|
|
|
JobBase.call(this, jobDefinition);
|
2016-05-16 19:49:56 +08:00
|
|
|
|
|
|
|
this.init();
|
2016-06-01 16:39:01 +08:00
|
|
|
|
|
|
|
this.queries = [];
|
|
|
|
for (var i = 0; i < this.data.query.query.length; i++) {
|
|
|
|
this.queries[i] = QueryFactory.create(this.data, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MainFallback.is(this.data)) {
|
|
|
|
this.fallback = new MainFallback();
|
|
|
|
}
|
2016-05-16 19:49:56 +08:00
|
|
|
}
|
|
|
|
util.inherits(JobFallback, JobBase);
|
|
|
|
|
|
|
|
module.exports = JobFallback;
|
|
|
|
|
2016-06-03 00:02:25 +08:00
|
|
|
// 1. from user: {
|
2016-05-16 19:49:56 +08:00
|
|
|
// query: {
|
|
|
|
// query: [{
|
|
|
|
// query: 'select ...',
|
|
|
|
// onsuccess: 'select ..'
|
|
|
|
// }],
|
|
|
|
// onerror: 'select ...'
|
|
|
|
// }
|
|
|
|
// }
|
2016-06-03 00:02:25 +08:00
|
|
|
//
|
|
|
|
// 2. from redis: {
|
2016-05-23 21:05:14 +08:00
|
|
|
// status: 'pending',
|
|
|
|
// fallback_status: 'pending'
|
2016-05-16 19:49:56 +08:00
|
|
|
// query: {
|
|
|
|
// query: [{
|
|
|
|
// query: 'select ...',
|
|
|
|
// onsuccess: 'select ..'
|
2016-05-23 21:05:14 +08:00
|
|
|
// status: 'pending',
|
|
|
|
// fallback_status: 'pending',
|
2016-05-16 19:49:56 +08:00
|
|
|
// }],
|
|
|
|
// onerror: 'select ...'
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
JobFallback.is = function (query) {
|
|
|
|
if (!query.query) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Array.isArray(query.query)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < query.query.length; i++) {
|
2016-06-01 16:39:01 +08:00
|
|
|
if (!QueryFallback.is(query.query[i])) {
|
2016-05-16 19:49:56 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
JobFallback.prototype.init = function () {
|
|
|
|
for (var i = 0; i < this.data.query.query.length; i++) {
|
2016-06-02 20:28:35 +08:00
|
|
|
if (shouldInitStatus(this.data.query.query[i])){
|
2016-10-10 18:09:13 +08:00
|
|
|
this.data.query.query[i].status = JobStatus.PENDING;
|
2016-06-02 20:28:35 +08:00
|
|
|
}
|
|
|
|
if (shouldInitQueryFallbackStatus(this.data.query.query[i])) {
|
2016-10-10 18:09:13 +08:00
|
|
|
this.data.query.query[i].fallback_status = JobStatus.PENDING;
|
2016-05-16 19:49:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-02 20:28:35 +08:00
|
|
|
if (shouldInitStatus(this.data)) {
|
2016-10-10 18:09:13 +08:00
|
|
|
this.data.status = JobStatus.PENDING;
|
2016-05-16 19:49:56 +08:00
|
|
|
}
|
2016-06-02 20:28:35 +08:00
|
|
|
|
|
|
|
if (shouldInitFallbackStatus(this.data)) {
|
2016-10-10 18:09:13 +08:00
|
|
|
this.data.fallback_status = JobStatus.PENDING;
|
2016-06-02 20:28:35 +08:00
|
|
|
}
|
2016-05-16 19:49:56 +08:00
|
|
|
};
|
|
|
|
|
2016-06-02 20:28:35 +08:00
|
|
|
function shouldInitStatus(jobOrQuery) {
|
|
|
|
return !jobOrQuery.status;
|
|
|
|
}
|
|
|
|
|
|
|
|
function shouldInitQueryFallbackStatus(query) {
|
|
|
|
return (query.onsuccess || query.onerror) && !query.fallback_status;
|
|
|
|
}
|
|
|
|
|
|
|
|
function shouldInitFallbackStatus(job) {
|
|
|
|
return (job.query.onsuccess || job.query.onerror) && !job.fallback_status;
|
|
|
|
}
|
|
|
|
|
2016-06-01 16:39:01 +08:00
|
|
|
JobFallback.prototype.getNextQueryFromQueries = function () {
|
|
|
|
for (var i = 0; i < this.queries.length; i++) {
|
|
|
|
if (this.queries[i].hasNextQuery(this.data)) {
|
|
|
|
return this.queries[i].getNextQuery(this.data);
|
2016-05-16 19:49:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-03 01:39:48 +08:00
|
|
|
JobFallback.prototype.hasNextQueryFromQueries = function () {
|
|
|
|
return !!this.getNextQueryFromQueries();
|
|
|
|
};
|
|
|
|
|
2016-06-01 16:39:01 +08:00
|
|
|
JobFallback.prototype.getNextQueryFromFallback = function () {
|
|
|
|
if (this.fallback && this.fallback.hasNextQuery(this.data)) {
|
2016-05-18 00:51:02 +08:00
|
|
|
|
2016-06-01 16:39:01 +08:00
|
|
|
return this.fallback.getNextQuery(this.data);
|
|
|
|
}
|
2016-05-18 00:51:02 +08:00
|
|
|
};
|
|
|
|
|
2016-06-01 16:39:01 +08:00
|
|
|
JobFallback.prototype.getNextQuery = function () {
|
|
|
|
var query = this.getNextQueryFromQueries();
|
2016-05-18 00:51:02 +08:00
|
|
|
|
2016-06-01 16:39:01 +08:00
|
|
|
if (!query) {
|
|
|
|
query = this.getNextQueryFromFallback();
|
|
|
|
}
|
2016-05-18 00:51:02 +08:00
|
|
|
|
2016-06-01 16:39:01 +08:00
|
|
|
return query;
|
2016-05-18 00:51:02 +08:00
|
|
|
};
|
|
|
|
|
2016-05-16 19:49:56 +08:00
|
|
|
JobFallback.prototype.setQuery = function (query) {
|
|
|
|
if (!JobFallback.is(query)) {
|
|
|
|
throw new Error('You must indicate a valid SQL');
|
|
|
|
}
|
|
|
|
|
|
|
|
JobFallback.super_.prototype.setQuery.call(this, query);
|
|
|
|
};
|
|
|
|
|
2016-05-19 22:58:14 +08:00
|
|
|
JobFallback.prototype.setStatus = function (status, errorMesssage) {
|
2016-06-01 16:39:01 +08:00
|
|
|
var now = new Date().toISOString();
|
2016-05-26 23:37:37 +08:00
|
|
|
|
2016-06-03 01:39:48 +08:00
|
|
|
var hasChanged = this.setQueryStatus(status, this.data, errorMesssage);
|
|
|
|
hasChanged = this.setJobStatus(status, this.data, hasChanged, errorMesssage);
|
|
|
|
hasChanged = this.setFallbackStatus(status, this.data, hasChanged);
|
2016-05-26 23:37:37 +08:00
|
|
|
|
2016-06-01 16:39:01 +08:00
|
|
|
if (!hasChanged.isValid) {
|
|
|
|
throw new Error('Cannot set status to ' + status);
|
2016-05-26 23:37:37 +08:00
|
|
|
}
|
|
|
|
|
2016-06-01 16:39:01 +08:00
|
|
|
this.data.updated_at = now;
|
2016-05-19 20:10:18 +08:00
|
|
|
};
|
|
|
|
|
2016-06-03 01:39:48 +08:00
|
|
|
JobFallback.prototype.setQueryStatus = function (status, job, errorMesssage) {
|
|
|
|
return this.queries.reduce(function (hasChanged, query) {
|
|
|
|
var result = query.setStatus(status, this.data, hasChanged, errorMesssage);
|
|
|
|
return result.isValid ? result : hasChanged;
|
|
|
|
}.bind(this), { isValid: false, appliedToFallback: false });
|
|
|
|
};
|
|
|
|
|
2016-06-01 16:39:01 +08:00
|
|
|
JobFallback.prototype.setJobStatus = function (status, job, hasChanged, errorMesssage) {
|
2016-06-03 01:39:48 +08:00
|
|
|
var result = {
|
|
|
|
isValid: false,
|
|
|
|
appliedToFallback: false
|
|
|
|
};
|
2016-05-19 20:10:18 +08:00
|
|
|
|
2016-06-01 16:39:01 +08:00
|
|
|
status = this.shiftStatus(status, hasChanged);
|
2016-05-19 20:10:18 +08:00
|
|
|
|
2016-06-03 01:39:48 +08:00
|
|
|
result.isValid = this.isValidTransition(job.status, status);
|
|
|
|
if (result.isValid) {
|
2016-06-01 16:39:01 +08:00
|
|
|
job.status = status;
|
2016-10-10 18:09:13 +08:00
|
|
|
if (status === JobStatus.FAILED && errorMesssage && !hasChanged.appliedToFallback) {
|
2016-06-03 01:39:48 +08:00
|
|
|
job.failed_reason = errorMesssage;
|
|
|
|
}
|
2016-05-16 19:49:56 +08:00
|
|
|
}
|
|
|
|
|
2016-06-03 01:39:48 +08:00
|
|
|
return result.isValid ? result : hasChanged;
|
|
|
|
};
|
|
|
|
|
|
|
|
JobFallback.prototype.setFallbackStatus = function (status, job, hasChanged) {
|
|
|
|
var result = hasChanged;
|
|
|
|
|
|
|
|
if (this.fallback && !this.hasNextQueryFromQueries()) {
|
|
|
|
result = this.fallback.setStatus(status, job, hasChanged);
|
2016-05-19 22:58:14 +08:00
|
|
|
}
|
|
|
|
|
2016-06-03 01:39:48 +08:00
|
|
|
return result.isValid ? result : hasChanged;
|
2016-05-18 00:51:02 +08:00
|
|
|
};
|
|
|
|
|
2016-06-01 16:39:01 +08:00
|
|
|
JobFallback.prototype.shiftStatus = function (status, hasChanged) {
|
2016-05-24 17:26:23 +08:00
|
|
|
// jshint maxcomplexity: 7
|
2016-06-01 16:39:01 +08:00
|
|
|
if (hasChanged.appliedToFallback) {
|
2016-10-10 18:09:13 +08:00
|
|
|
if (!this.hasNextQueryFromQueries() && (status === JobStatus.DONE || status === JobStatus.FAILED)) {
|
2016-06-03 00:02:25 +08:00
|
|
|
status = this.getLastFinishedStatus();
|
2016-10-10 18:09:13 +08:00
|
|
|
} else if (status === JobStatus.DONE || status === JobStatus.FAILED){
|
|
|
|
status = JobStatus.PENDING;
|
2016-05-19 20:10:18 +08:00
|
|
|
}
|
2016-10-10 18:09:13 +08:00
|
|
|
} else if (this.hasNextQueryFromQueries() && status !== JobStatus.RUNNING) {
|
|
|
|
status = JobStatus.PENDING;
|
2016-05-19 20:10:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
};
|
|
|
|
|
2016-06-03 00:02:25 +08:00
|
|
|
JobFallback.prototype.getLastFinishedStatus = function () {
|
|
|
|
return this.queries.reduce(function (lastFinished, query) {
|
|
|
|
var status = query.getStatus(this.data);
|
|
|
|
return this.isFinalStatus(status) ? status : lastFinished;
|
2016-10-10 18:09:13 +08:00
|
|
|
}.bind(this), JobStatus.DONE);
|
2016-05-16 19:49:56 +08:00
|
|
|
};
|
2016-09-30 22:47:37 +08:00
|
|
|
|
|
|
|
JobFallback.prototype.log = function(logger) {
|
|
|
|
if (!isFinished(this)) {
|
2016-09-30 22:51:34 +08:00
|
|
|
return false;
|
2016-09-30 22:47:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var queries = this.data.query.query;
|
|
|
|
|
|
|
|
for (var i = 0; i < queries.length; i++) {
|
|
|
|
var query = queries[i];
|
|
|
|
|
2016-10-01 00:45:33 +08:00
|
|
|
var logEntry = {
|
2016-10-17 22:12:02 +08:00
|
|
|
created: this.data.created_at,
|
|
|
|
waiting: elapsedTime(this.data.created_at, query.started_at),
|
2016-09-30 22:53:00 +08:00
|
|
|
time: query.started_at,
|
|
|
|
endtime: query.ended_at,
|
2016-09-30 22:47:37 +08:00
|
|
|
username: this.data.user,
|
2016-10-06 01:09:10 +08:00
|
|
|
dbhost: this.data.host,
|
2016-09-30 22:47:37 +08:00
|
|
|
job: this.data.job_id,
|
2016-09-30 22:55:33 +08:00
|
|
|
elapsed: elapsedTime(query.started_at, query.ended_at)
|
2016-09-30 22:47:37 +08:00
|
|
|
};
|
|
|
|
|
2016-09-30 22:53:00 +08:00
|
|
|
var queryId = query.id;
|
|
|
|
|
2016-10-01 00:45:15 +08:00
|
|
|
var tag = 'query';
|
2016-09-30 22:53:00 +08:00
|
|
|
if (queryId) {
|
2016-10-01 00:45:33 +08:00
|
|
|
logEntry.query_id = queryId;
|
2016-09-30 22:53:00 +08:00
|
|
|
|
|
|
|
var node = parseQueryId(queryId);
|
|
|
|
if (node) {
|
2016-10-01 00:45:33 +08:00
|
|
|
logEntry.analysis = node.analysisId;
|
|
|
|
logEntry.node = node.nodeId;
|
|
|
|
logEntry.type = node.nodeType;
|
2016-10-01 00:45:15 +08:00
|
|
|
tag = 'analysis';
|
2016-09-30 22:53:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-01 00:45:33 +08:00
|
|
|
logger.info(logEntry, tag);
|
2016-09-30 22:47:37 +08:00
|
|
|
}
|
|
|
|
|
2016-09-30 22:51:34 +08:00
|
|
|
return true;
|
2016-09-30 22:47:37 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
function isFinished (job) {
|
2016-10-10 18:09:13 +08:00
|
|
|
return JobStatus.isFinal(job.data.status) &&
|
|
|
|
(!job.data.fallback_status || JobStatus.isFinal(job.data.fallback_status));
|
2016-09-30 22:47:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function parseQueryId (queryId) {
|
|
|
|
var data = queryId.split(':');
|
|
|
|
|
2016-09-30 22:53:00 +08:00
|
|
|
if (data.length === 3) {
|
|
|
|
return {
|
|
|
|
analysisId: data[0],
|
|
|
|
nodeId: data[1],
|
|
|
|
nodeType: data[2]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return null;
|
2016-09-30 22:47:37 +08:00
|
|
|
}
|
|
|
|
|
2016-09-30 22:55:33 +08:00
|
|
|
function elapsedTime (started_at, ended_at) {
|
2016-09-30 22:47:37 +08:00
|
|
|
if (!started_at || !ended_at) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var start = new Date(started_at);
|
|
|
|
var end = new Date(ended_at);
|
2016-09-30 22:55:33 +08:00
|
|
|
return end.getTime() - start.getTime();
|
2016-09-30 22:47:37 +08:00
|
|
|
}
|