Skip to content

Commit

Permalink
Merge pull request #142 from Tencent/dev
Browse files Browse the repository at this point in the history
v1.1.4-alpha
  • Loading branch information
RobinzZH authored Jun 25, 2018
2 parents 06fa5b4 + 53ec520 commit 466d32c
Show file tree
Hide file tree
Showing 46 changed files with 1,321 additions and 637 deletions.
10 changes: 1 addition & 9 deletions bin/proxy/admin.actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ module.exports = {
res.end('done!\r\n');
},

'/heapdump': function (req, res) {
process.emit('sendCmd2workerOnce', {
CMD: 'heapdump',
GET: req.GET
});
res.writeHead(200, { 'Content-Type': 'text/plain; charset=UTF-8' });
res.end('done!\r\n');
},

'/profiler': function (req, res) {
process.emit('sendCmd2workerOnce', {
Expand All @@ -55,7 +47,7 @@ module.exports = {

'/reload': function (req, res) {

cp.exec('./check.js', {
cp.exec('node --no-warnings ./check.js', {
timeout: 5000,
encoding: 'utf8',
cwd: __dirname
Expand Down
7 changes: 0 additions & 7 deletions bin/proxy/dump.heap.sh

This file was deleted.

139 changes: 61 additions & 78 deletions bin/proxy/http.proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,6 @@ methodMap.reload = function() {
process.emit('reload');
};

// heapdump
methodMap.heapdump = function(message) {
process.emit('heapdump', message.GET);
};

// profiler
methodMap.profiler = function(message) {
process.emit('profiler', message.GET);
Expand All @@ -130,24 +125,6 @@ process.on('top100', function(e) {
global.top100 = [];
});


process.on('heapdump', function(e) {
if (isWin32Like) {
return;
}

require('heapdump').writeSnapshot(__dirname + '/cpu' + serverInfo.cpu + '.' + Date.now() + '.heapsnapshot', function(err, filename) {
if (err) {
logger.error(`dump heap error ${err.message}`);
return;
}
logger.info('dump written to ${filename}', {
filename: filename
});
});
});


process.on('profiler', function(data = {}) {
logger.info('profiler time: ${time}', data);
if (isWin32Like) {
Expand Down Expand Up @@ -390,61 +367,7 @@ function heartBeat() {

// 高负载告警
if (global.cpuUsed80 === 4 && !config.isTest && !isWin32Like) {
// 取进程快照
cp.exec('top -bcn1', {
env: {
COLUMNS: 200
},
encoding: 'utf8',
timeout: 5000
}, function(err, data, errData) { // eslint-disable-line handle-callback-err
const key = `cpu80.v4:${serverInfo.intranetIp}`;
let content = `<strong>单核CPU${serverInfo.cpu}使用率为:${cpuUsed},超过80%, 最近5秒钟CPU Profiler见附件</strong>`;
let str = '';

if (data) {
str = data;
str = str.replace(/</g, '&gt;');
str = str.replace(/\r\n|\r|\n/g, '<br>');

content += '<p><strong>进程快照:</strong></p><pre style="font-size:12px">' + str + '</pre>';
}


// 获取本机信息,用来分组
require('api/cmdb').GetDeviceThisServer().done(function(data) {
data = data || {};
const business = data.business && data.business[0] || {};
let owner = '';

if (data.ownerMain) {
owner = [owner, data.ownerMain].join(';');
}

if (data.ownerBack) {
owner = [owner, data.ownerBack].join(';');
}

// 再抓一份CPU Profiler
require('util/v8-profiler.js').getProfiler({
recordTime: 5000
}, result => {
mail.SendMail(key, 'js', 600, {
'to': config.mailTo,
'cc': owner,
'msgInfo': business.module + '[CPU]' + serverInfo.intranetIp + '单核CPU' + serverInfo.cpu + '使用率为:' + cpuUsed + ',超过80%',
'title': business.module + '[CPU]' + serverInfo.intranetIp + '单核CPU' + serverInfo.cpu + '使用率为:' + cpuUsed + ',超过80%',
'content': content,
'attachment': result ? {
fileType: true,
dispositionType: 'attachment',
fileName: 'cpu-profiler.cpuprofile',
content: result
} : ''
});
});
});
});
afterCpu80(cpuUsed);
}

const currMemory = process.memoryUsage();
Expand All @@ -453,3 +376,63 @@ function heartBeat() {
tnm2.Attr_API_Set('AVG_TSW_MEMORY_HEAP', currMemory.heapTotal);
tnm2.Attr_API_Set('AVG_TSW_MEMORY_EXTERNAL', currMemory.external);
}


function afterCpu80(cpuUsed) {
// 取进程快照
cp.exec('top -bcn1', {
env: {
COLUMNS: 200
},
encoding: 'utf8',
timeout: 5000
}, function(err, data, errData) { // eslint-disable-line handle-callback-err
const key = `cpu80.v4:${serverInfo.intranetIp}`;
let content = `<strong>单核CPU${serverInfo.cpu}使用率为:${cpuUsed},超过80%, 最近5秒钟CPU Profiler见附件</strong>`;
let str = '';

if (data) {
str = data;
str = str.replace(/</g, '&gt;');
str = str.replace(/\r\n|\r|\n/g, '<br>');

content += '<p><strong>进程快照:</strong></p><pre style="font-size:12px">' + str + '</pre>';
}


// 获取本机信息,用来分组
require('api/cmdb').GetDeviceThisServer().done(function(data) {
data = data || {};
const business = data.business && data.business[0] || {};
const profCallback = (result) => {
mail.SendMail(key, 'js', 600, {
'to': config.mailTo,
'cc': config.mailCC,
'msgInfo': `${business.module}[CPU]${serverInfo.intranetIp}单核CPU${serverInfo.cpu}使用率为:${cpuUsed},超过80%`,
'title': `${business.module}[CPU]${serverInfo.intranetIp}单核CPU${serverInfo.cpu}使用率为:${cpuUsed},超过80%`,
'content': content,
'attachment': result ? {
fileType: true,
dispositionType: 'attachment',
fileName: 'cpu-profiler.cpuprofile',
content: result
} : ''
});
};

// 再抓一份CPU Profiler
let profiler;
try {
profiler = require('util/v8-profiler.js');
} catch (err) {
logger.info(err.stack);
}

if (profiler) {
profiler.getProfiler({ recordTime: 5000 }, profCallback);
} else {
profCallback();
}
});
});
}
25 changes: 18 additions & 7 deletions bin/proxy/http.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ module.exports = function(req, res) {
}

req.removeAllListeners('fail');
req.removeAllListeners('reportLog');
res.removeAllListeners('timeout');
res.removeAllListeners('close');
res.removeAllListeners('finish');
Expand Down Expand Up @@ -469,7 +470,7 @@ function doRoute(req, res) {
}

// 抓回包
httpUtil.captureBody(this);
httpUtil.captureServerResponseBody(this);
}

logger.debug('response ${statusCode}', {
Expand All @@ -488,6 +489,8 @@ function doRoute(req, res) {
logger.getLog().showLineNumber = true;
logger.debug('showLineNumber on');
}

httpUtil.captureIncomingMessageBody(req);
}

logger.debug('node-${version}, name: ${name}, appid: ${appid}', {
Expand Down Expand Up @@ -536,10 +539,18 @@ function doRoute(req, res) {

if (modulePath && typeof modulePath.callback === 'function') {
const app = modulePath;

modulePath = function(req, res, plug) {
return app.callback()(req, res);
};
// if beforeStart exists
if (typeof app.beforeStart === 'function') {
modulePath = (req, res, plug) => {
return app.beforeStart(() => {
app.callback()(req, res);
});
};
} else {
modulePath = (req, res, plug) => {
return app.callback()(req, res);
};
}
}

if (typeof modulePath !== 'function') {
Expand Down Expand Up @@ -581,8 +592,8 @@ function doRoute(req, res) {

const blackIpMap = TSW.getBlockIpMapSync() || {};

if (blackIpMap[clientIp] || blackIpMap[userIp24] || !clientIp) {
logger.debug('连接已断开');
if (!clientIp) {
logger.debug('client ip has been empty');

tnm2.Attr_API('SUM_TSW_IP_EMPTY', 1);
res.writeHead(403, { 'Content-Type': 'text/plain; charset=UTF-8' });
Expand Down
10 changes: 6 additions & 4 deletions bin/proxy/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ process.on('warning', function(warning) {
const errStr = warning && warning.stack || String(warning);
const content = `<p><strong>错误堆栈</strong></p><p><pre><code>${errStr}</code></pre></p>`;

logger.error(errStr);
if (warning.message && warning.message.indexOf('N-API') > -1) {
logger.warn(warning.message);
return;
}

logger.warn(errStr);

setImmediate(function() {
require('util/mail/mail.js').SendMail(key, 'js', 600, {
'title': key,
'runtimeType': 'warning',
'content': content
});
});
Expand Down Expand Up @@ -71,8 +75,6 @@ process.on('unhandledRejection', (errorOrReason, currPromise) => {
}
});

process.noProcessWarnings = true;


startServer();

Expand Down
4 changes: 2 additions & 2 deletions bin/proxy/startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ pwd
IS_DOCKER=${IS_DOCKER:=0}
if [ ${IS_DOCKER} = 1 ]
then
./TSW --expose_internals $CUR_ARGS ./proxy >> ../log/run.log.0 2>&1
./TSW --expose_internals --no-deprecation $CUR_ARGS ./proxy >> ../log/run.log.0 2>&1
else
./TSW --expose_internals $CUR_ARGS ./proxy >> ../log/run.log.0 2>&1 &
./TSW --expose_internals --no-deprecation $CUR_ARGS ./proxy >> ../log/run.log.0 2>&1 &
echo start down
fi

Loading

0 comments on commit 466d32c

Please sign in to comment.