Skip to content

Commit

Permalink
name-server-fault-tolerance
Browse files Browse the repository at this point in the history
  • Loading branch information
wujia authored and gxcsoccer committed Sep 14, 2018
1 parent 9fd4004 commit d1de082
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
11 changes: 10 additions & 1 deletion lib/mq_client_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,16 @@ class MQClientAPI extends RemotingClient {
const request = RemotingCommand.createRequestCommand(RequestCode.GET_ROUTEINTO_BY_TOPIC, {
topic,
});
const response = await this.invoke(null, request, timeoutMillis);
let response = {};
let count = this._namesrvAddrList.length;
while (count--) {
try {
response = await this.invoke(null, request, timeoutMillis);
break;
} catch (err) {
this.logger.warn(err);
}
}
switch (response.code) {
case ResponseCode.SUCCESS:
{
Expand Down
6 changes: 3 additions & 3 deletions lib/remoting_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class RemotingClient extends Base {
assert(options.onsAddr, '[RemotingClient] options.onsAddr is required');
assert(options.httpclient, '[RemotingClient] options.httpclient is required');
super(Object.assign({ initMethod: 'init' }, defaultOptions, options));

this._index = 0;
this._inited = false;
this._namesrvAddrList = [];
this._channels = new Map();
Expand Down Expand Up @@ -162,8 +162,8 @@ class RemotingClient extends Base {
*/
getAndCreateChannel(addr) {
if (!addr) {
const index = Math.floor(Math.random() * this._namesrvAddrList.length);
addr = this._namesrvAddrList[index];
this._index = ++this._index % this._namesrvAddrList.length;
addr = this._namesrvAddrList[this._index];
}
let channel = this._channels.get(addr);
if (channel && channel.isOK) {
Expand Down
25 changes: 25 additions & 0 deletions test/mq_client_api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,29 @@ describe('test/mq_client_api.test.js', function() {
}
assert(isError);
});

it('should getTopicRouteInfoFromNameServer ok', async () => {
const res = await client.getTopicRouteInfoFromNameServer('TEST_TOPIC', 3000);
assert(res);
});

it('should getTopicRouteInfoFromNameServer ok if old one is closed', async () => {
client._namesrvAddrList.unshift('1.2.3.4:80', '2.3.4.5:80');
client._namesrvAddrList.push('6.7.8.9:80');
const res = await client.getTopicRouteInfoFromNameServer('TEST_TOPIC', 3000);
assert(res);
});

it('should getTopicRouteInfoFromNameServer ok if old one is closed empty list', async () => {
client._namesrvAddrList = [];
let isError = false;
try {
await client.getTopicRouteInfoFromNameServer('TEST_TOPIC', 3000);
} catch (err) {
isError = true;
assert(err.name === 'MQClientException');
}
assert(isError);
});

});

0 comments on commit d1de082

Please sign in to comment.