Permalink
Browse files

DCO use 12 bytes header, more simple

  • Loading branch information...
1 parent 35ab1b5 commit ab7729dc861c65bad89ddc281db12566c3d709db @kaven276 committed Oct 10, 2012
Showing with 161 additions and 197 deletions.
  1. +22 −20 lib/dco_proxy.js
  2. +30 −81 lib/ext_hub.js
  3. +10 −0 oracle/psp/k_dco_adm.bdy
  4. +6 −0 oracle/psp/k_dco_adm.spc
  5. +92 −95 oracle/psp/k_ext_call.bdy
  6. +1 −1 oracle/psp/k_ext_call.spc
View
@@ -34,9 +34,10 @@ Counter.prototype.finish = function(){
utl.gracefulExit(function(){
// stop accept new request
// detect end of current request and then quit
- var askClosePDU = new Buffer(6);
+ var askClosePDU = new Buffer(12);
askClosePDU.writeInt32BE(6, 0);
- askClosePDU.writeUInt16BE(0, 4);
+ askClosePDU.writeInt32BE(0, 4);
+ askClosePDU.writeInt32BE(0, 8);
extHubSocks.forEach(function(sock){
// tell ext-hub to half close the tcp connection
sock.write(askClosePDU);
@@ -59,6 +60,7 @@ exports.createServer = function(handler){
extHubSock.once('data', function onHandshake(data){
+ // for begin of connection
try {
var ptoken = data.readInt32BE(0);
} catch (e) {
@@ -71,26 +73,28 @@ exports.createServer = function(handler){
return;
}
- extHubSocks.push(extHubSock);
- (function(extHubID){
+ // for end of connection
+ (function(){
+ /** @const */
+ var extHubID = extHubSocks.length;
+ extHubSocks.push(extHubSock);
extHubSock.on('close', function(){
console.log('ext-hub(%d) will not send new request, in-bound socket half closed', extHubID);
- delete extHubSocks[extHubID - 1];
+ delete extHubSocks[extHubID];
});
- })(extHubSocks.length);
- new StreamSpliter(extHubSock, 'readInt32BE', onRequest);
- function onRequest(pdu){
+ })();
+
+ new StreamSpliter(extHubSock, 'readInt32BE', function onRequest(pdu){
counter.start();
if (pdu.readInt32BE(0) > 0) {
- handler(new Request(pdu, true), new Response(extHubSock, pdu.readUInt16BE(4)));
+ handler(new Request(pdu, true), new Response(extHubSock, pdu.slice(0, 12)));
} else {
handler(new Request(pdu, false), new DummyResponse());
}
- }
+ });
- if (data.length !== 4) {
- console.warn('first chunk is not 4 bytes length.');
- extHubSock.emit('data', data.slice(4));
+ if (data.length > 12) {
+ extHubSock.emit('data', data.slice(12));
}
});
});
@@ -107,15 +111,15 @@ DcoWorkerProxy.prototype.listen = function(port, host){
};
function Request(pdu, sync){
- this.content = pdu.slice(6);
+ this.content = pdu.slice(12);
this.sync = sync;
}
-function Response(extHubSock, traceBackSeq){
+function Response(extHubSock, header){
this._extHubSock = extHubSock;
- this._traceBackSeq = traceBackSeq;
+ this._header = header;
this._buffer = [];
- this._length = 6;
+ this._length = 12;
}
Response.prototype.write = function(data){
if (!data) return;
@@ -127,10 +131,8 @@ Response.prototype.write = function(data){
};
Response.prototype.end = function(data){
if (data) this.write(data);
- var header = new Buffer(6);
+ var header = this._header;
header.writeInt32BE(this._length, 0);
- header.writeUInt16BE(this._traceBackSeq, 4);
-
var extHubSock = this._extHubSock;
extHubSock.write(header);
this._buffer.forEach(function(buf){
View
@@ -7,25 +7,15 @@
var net = require('net')
, StreamSpliter = require('./StreamSpliter').Class
- , oraSocks = []
+ , oraSocks = {}
, wpSocks = []
, utl = require('./util.js')
, logConnWP = utl.dummy
, logConnOra = console.log
, logPDU = console.log
- , logSeqQ = utl.dummy
, pendingReturn = 0
;
-/**
- * A TraceBack record.
- * @constructor
- */
-function TraceBack(oraSock, oraSeq){
- this.oraSock = oraSock;
- this.oraSeq = oraSeq;
-}
-
function gracefulQuit(){
console.log('ext-hub is exiting !');
console.log('current connection is %d', server.connections);
@@ -57,16 +47,18 @@ var onDataCount = 0;
var server = net.createServer(function(oraSock){
logConnOra('oracle connected');
+ console.log('oracle connected');
oraSock.on('data', function(data){
- logConnOra('NO.%d data arrived from oracle(%d,%d) len=%d', oraSock.sid || 0, oraSock.serial || 0, ++onDataCount, data.length);
+ logConnOra('NO.%d data arrived from oracle(%d,%d) len=%d', ++onDataCount, oraSock.sid || 0, oraSock.serial || 0, data.length);
});
- var sid, serial;
+ var sid, serial, asid;
oraSock.once('data', onHandshake);
oraSock.on('close', function(){
+ delete oraSocks[oraSock.asid];
logConnOra('connection closed, now have %d', server.connections);
});
@@ -86,63 +78,51 @@ var server = net.createServer(function(oraSock){
oraSock.sid = sid = data.readInt32BE(4);
oraSock.serial = serial = data.readInt32BE(8);
- oraSock.oraSeq = data.readInt32BE(12);
- oraSocks[sid] = oraSock;
- logConnOra('oracle connected sid = %d, serial = %d, initial oraSeq = %d', sid, serial, oraSock.oraSeq);
+ oraSock.asid = asid = data.readUInt32BE(12);
+ oraSock.oraSeq = data.readInt32BE(16);
+ oraSocks[asid] = oraSock;
+ logConnOra('oracle connected asid=%d, sid = %d, serial = %d, initial oraSeq = %d', asid, sid, serial, oraSock.oraSeq);
new StreamSpliter(oraSock, 'readInt32BE', onOracleRequest);
- if (data.length !== 16) {
- logConnOra('first chunk is not 16 bytes length.');
- oraSock.emit('data', data.slice(16));
+ if (data.length !== 20) {
+ logConnOra('first chunk is not 20 bytes length.');
+ oraSock.emit('data', data.slice(20));
}
}
function onOracleRequest(oraReq){
- var proxyID = oraReq.readUInt16BE(4)
+ var proxyID = oraReq.readInt32BE(4)
, wpw = wpSocks[proxyID]
- , oraSeq = oraSock.oraSeq++
;
if (!wpw) {
console.warn('%s, proxy %d is not exists', new Date(), proxyID);
return;
}
- wpw.whenHaveFreeTraceBackID(oraReq.readInt32BE(0) > 0, function(rpcSeq){
- if (rpcSeq === 0) {
- // for no reply or no rpc requests
- oraReq.writeUInt16BE(0, 4);
- } else {
- oraReq.writeUInt16BE(rpcSeq, 4);
- logPDU('> ora(%d,%d,%d) - wp(%d,%d)', sid, serial, oraSeq, proxyID, rpcSeq);
- wpw.rpcLog[rpcSeq] = new TraceBack(oraSock, oraSeq);
- }
- (wpw.sts === 'opened') ? wpw.wpSock.write(oraReq) : wpw.queue.push(oraReq);
- });
+ /* replace proxyID field with oraSeq */
+ logPDU('> ora(%d,%d,%d,%d) - wp(%d)', asid, sid, serial, oraSock.oraSeq, proxyID);
+
+ oraReq.writeInt32BE(oraSock.oraSeq++, 4);
+ (wpw.sts === 'opened') ? wpw.wpSock.write(oraReq) : wpw.queue.push(oraReq);
}
});
/**
* A worker proxy client wrapper class
* @constructor
- * @param wpSock worker proxy client socket.
* @param {number} id worker proxy client socket slot, start at 0
* @param {string }hostp worker proxy server address, format as "host:port"
- * @param {number} maxPendingRpc worker proxy request queue pending requests limit.
* @param {string} desc worker proxy description.
*/
-function WPWrapper(id, hostp, maxPendingRpc, desc){
+function WPWrapper(id, hostp, desc){
var wpw = this;
var wpSock = this.wpSock = new net.Socket({allowHalfOpen : true});
this.desc = desc;
this.hostp = hostp.split(':');
this.id = id;
this.sts = 'close';
this.queue = [];
- this.rpcLog = [];
- this.freeList = utl.makeArray(maxPendingRpc || 256);
- this.freeList.shift();
- this.noRpcSeqQueue = [];
wpSock.on('end', function(){
// worker proxy real closed
@@ -171,27 +151,24 @@ function WPWrapper(id, hostp, maxPendingRpc, desc){
function onWorkerProxyReply(proxyResp){
var len = proxyResp.readInt32BE(0);
- if (len === 6) {
+ if (len === 12) {
wpw.setStatus('exiting', 'wp said he will quit');
wpSock.end();
return;
}
- var rpcSeq = proxyResp.readUInt16BE(4);
- var traceBack = wpw.rpcLog[rpcSeq];
+ var oraSeq = proxyResp.readInt32BE(4)
+ , asid = proxyResp.readInt32BE(8)
+ , oraSock = oraSocks[asid]
+ ;
// logPDU('reply for proxySeq=%d, %s', proxySeq, proxyResp.slice(0, 6).toString('hex'));
- if (!traceBack) {
- console.warn('proxySeq:%d have no trace back, may ext-hub restarted and proxy send reply in that break', rpcSeq);
+ if (!oraSock) {
+ console.warn('can not find the sending oracle connection, reply will lose');
return;
}
- var oraSock = traceBack.oraSock
- , oraSeq = traceBack.oraSeq
- ;
- wpw.recycleRpcSeq(rpcSeq);
- proxyResp.writeUInt16BE(oraSeq, 4);
oraSock.write(proxyResp);
- logPDU('< ora(%d,%d,%d) - wp(%d,%d)', oraSock.sid, oraSock.serial, oraSeq, wpw.id, rpcSeq);
+ logPDU('< ora(%d,%d,%d,%d) - wp(%d)', oraSock.asid, oraSock.sid, oraSock.serial, oraSeq, wpw.id);
}
}
@@ -205,43 +182,15 @@ WPWrapper.prototype.setStatus = function(sts, env){
this.sts = sts;
};
-WPWrapper.prototype.whenHaveFreeTraceBackID = function(isRPC, callback){
- if (!isRPC) {
- callback(0);
- } else {
- pendingReturn++;
- var rpcSeq = this.freeList.shift();
- if (rpcSeq === undefined) {
- this.noRpcSeqQueue.push(callback);
- logSeqQ('no free rpc trace back sequence available.');
- } else {
- callback(rpcSeq);
- }
- }
-};
-
-WPWrapper.prototype.recycleRpcSeq = function(rpcSeq){
- pendingReturn--;
- var callback = this.noRpcSeqQueue.shift();
- if (callback) {
- callback(rpcSeq);
- logSeqQ('new free rpc trace back sequence available, and used for queued request.');
- } else {
- delete this.rpcLog[rpcSeq];
- this.freeList.push(rpcSeq);
- // logSeqQ('new free rpc trace back sequence available, just recycled.');
- }
-};
-
-exports.addWorkerProxy = function(slot, addr, desc, maxPendingRpc){
+exports.addWorkerProxy = function(slot, addr, desc){
if (wpSocks[slot]) {
console.warn('The NO.%d slot is already used by %s', wpSocks[slot].desc);
return;
}
- var wpw = new WPWrapper(slot, addr, maxPendingRpc, desc);
+ var wpw = new WPWrapper(slot, addr, desc);
wpSocks[slot] = wpw;
wpw.connect();
- console.log('set NO.%d worker proxy.', slot);
+ console.log('set NO.%d worker proxy : %s', slot, desc);
};
exports.run = function(port){
server.listen(parseInt(port || 1523));
View
@@ -1,5 +1,15 @@
create or replace package body k_dco_adm is
+ procedure signal_reconnect
+ (
+ host varchar2,
+ port number
+ ) is
+ begin
+ dbms_alert.signal('Noradle-DCO-EXTHUB-QUIT', host || ':' || port);
+ commit;
+ end;
+
procedure wait_reconnect_exthub
(
host varchar2,
View
@@ -1,5 +1,11 @@
create or replace package k_dco_adm is
+ procedure signal_reconnect
+ (
+ host varchar2,
+ port number
+ );
+
procedure wait_reconnect_exthub
(
host varchar2,
Oops, something went wrong.

0 comments on commit ab7729d

Please sign in to comment.