/
client_region.test.js
391 lines (336 loc) · 11.6 KB
/
client_region.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/**!
* node-hbase-client - test/client_region.test.js
*
* Copyright(c) 2014 Alibaba Group Holding Limited.
*
* Authors:
* 苏千 <suqian.yf@taobao.com> (http://fengmk2.github.com)
*/
"use strict";
/**
* Module dependencies.
*/
var async = require('async');
var should = require('should');
var mm = require('mm');
var pedding = require('pedding');
var hbase = require('../');
var config = require('./config');
describe('client_region.test.js', function () {
var client = null;
before(function () {
client = hbase.Client.create(config);
});
after(function (done) {
setTimeout(done, 500);
});
afterEach(mm.restore);
before(function (done) {
done = pedding(2, done);
client.putRow(config.tableUser,
'0338472dd25d0faeacbef9b957950961',
{'cf1:history': '0338472dd25d0faeacbef9b957950961 histry'}, done);
client.putRow(config.tableUser,
'fdbf2da2cc85e1c79f953a3d8f482edf',
{'cf1:history': 'fdbf2da2cc85e1c79f953a3d8f482edf histry'}, done);
});
describe('processBatch()', function () {
it('should mget from wrong region', function (done) {
var table = config.tableUser;
var get = new hbase.Get('0338472dd25d0faeacbef9b957950961');
get.addColumn('cf1', 'history');
client.processBatch(table, [get], true, 0, function (err, results) {
should.not.exist(err);
results.should.length(1);
var location = client.getCachedLocation(table, get.getRow());
mm(client, 'locateRegion', function () {
var cb = arguments[arguments.length - 1];
cb(null, location);
});
client.mget(table, ['0338472dd25d0faeacbef9b957950961', 'fdbf2da2cc85e1c79f953a3d8f482edf'], ['cf1:history'],
function (err, results) {
should.exist(err);
err.name.should.equal('org.apache.hadoop.hbase.regionserver.WrongRegionException');
done();
});
});
});
it('should batch process from wrong region', function (done) {
var table = config.tableUser;
var get = new hbase.Get('0338472dd25d0faeacbef9b957950961');
get.addColumn('cf1', 'history');
var get2 = new hbase.Get('fdbf2da2cc85e1c79f953a3d8f482edf');
get2.addColumn('cf1', 'history');
client.processBatch(table, [get], true, 0, function (err, results) {
should.not.exist(err);
results.should.length(1);
var location = client.getCachedLocation(table, get.getRow());
mm(client, 'locateRegion', function () {
var cb = arguments[arguments.length - 1];
cb(null, location);
});
client.processBatch(table, [get2], true, 0, function (err, results) {
should.exist(err);
err.name.should.equal('org.apache.hadoop.hbase.regionserver.WrongRegionException');
done();
});
});
});
it('should mget fail first on wrong region and retry clean region caches', function (done) {
var table = config.tableUser;
var get = new hbase.Get('0338472dd25d0faeacbef9b957950961');
get.addColumn('cf1', 'history');
var get2 = new hbase.Get('fdbf2da2cc85e1c79f953a3d8f482edf');
get2.addColumn('cf1', 'history');
client.processBatch(table, [get], true, 0, function (err, results) {
should.not.exist(err);
results.should.length(1);
var location = client.getCachedLocation(table, get.getRow());
// console.log('location: ', location.toString());
mm(client, 'locateRegion', function () {
var cb = arguments[arguments.length - 1];
mm.restore();
cb(null, location);
});
client.processBatch(table, [get2], true, 0, function (err, results) {
should.not.exist(err);
results.should.length(1);
done();
});
});
});
});
it('should get rowkey from wrong region', function (done) {
var get = new hbase.Get('0338472dd25d0faeacbef9b957950961');
get.addColumn('cf1', 'history');
client.get(config.tableUser, get, function (err, result) {
should.not.exist(err);
var kvs = result.raw();
// if (kvs.length > 0) {
// for (var i = 0; i < kvs.length; i++) {
// var kv = kvs[i];
// console.log('kv: %s: %s', kv.getFamily().toString() + ':' + kv.getQualifier().toString(), kv.getValue().toString());
// }
// }
var location = client.getCachedLocation(config.tableUser, get.getRow());
//console.log(location.toString());
client.getRegionConnection(location.getHostname(), location.getPort(), function (err, server) {
should.not.exist(err);
var get2 = new hbase.Get('fdbf2da2cc85e1c79f953a3d8f482edf');
get2.addColumn('cf1', 'history');
server.get(location.getRegionInfo().getRegionName(), get2, function (err, result) {
should.exist(err);
err.name.should.equal('org.apache.hadoop.hbase.regionserver.WrongRegionException');
should.not.exist(result);
done();
});
});
});
});
it('should get wrong region and retry from new region', function (done) {
var get = new hbase.Get('0338472dd25d0faeacbef9b957950961');
get.addColumn('cf1', 'history');
client.get(config.tableUser, get, function (err, result) {
should.not.exist(err);
var kvs = result.raw();
// if (kvs.length > 0) {
// for (var i = 0; i < kvs.length; i++) {
// var kv = kvs[i];
// console.log('kv: %s: %s', kv.getFamily().toString() + ':' + kv.getQualifier().toString(), kv.getValue().toString());
// }
// }
var location = client.getCachedLocation(config.tableUser, get.getRow());
var get2 = new hbase.Get('fdbf2da2cc85e1c79f953a3d8f482edf');
get2.addColumn('cf1', 'history');
//console.log(location.toString());
mm(client, 'locateRegion', function (tableName, row, useCache, callback) {
callback(null, location);
mm.restore();
});
client.get(config.tableUser, get2, function (err, result) {
should.not.exist(err);
should.exist(result);
var kvs = result.raw();
// if (kvs.length > 0) {
// for (var i = 0; i < kvs.length; i++) {
// var kv = kvs[i];
// console.log('kv: %s: %s', kv.getFamily().toString() + ':' + kv.getQualifier().toString(), kv.getValue().toString());
// }
// }
done();
});
});
});
it('should error when retry over 3 times', function (done) {
var get = new hbase.Get('0338472dd25d0faeacbef9b957950961');
get.addColumn('cf1', 'w02');
client.get(config.tableUser, get, function (err, result) {
should.not.exist(err);
should.exist(result);
var location = client.getCachedLocation(config.tableUser, get.getRow());
var get2 = new hbase.Get('fdbf2da2cc85e1c79f953a3d8f482edf');
get2.addColumn('cf1', 'w02');
mm(client, 'locateRegion', function (tableName, row, useCache, callback) {
callback(null, location);
});
client.get(config.tableUser, get2, function (err, result) {
should.exist(err);
err.name.should.equal('org.apache.hadoop.hbase.regionserver.WrongRegionException');
should.not.exist(result);
done();
});
});
});
it('should refused error', function (done) {
var get = new hbase.Get('0338472dd25d0faeacbef9b957950961');
get.addColumn('cf1', 'w02');
client.get(config.tableUser, get, function (err, result) {
should.not.exist(err);
should.exist(result);
var location = client.getCachedLocation(config.tableUser, get.getRow());
mm(location, 'getPort', function () {
return 64401;
});
var get2 = new hbase.Get('fdbf2da2cc85e1c79f953a3d8f482edf');
get2.addColumn('cf1', 'w02');
mm(client, 'locateRegion', function (tableName, row, useCache, callback) {
callback(null, location);
});
client.get(config.tableUser, get2, function (err, result) {
should.exist(err);
err.name.should.equal('ConnectionRefusedException');
should.not.exist(result);
done();
});
});
});
describe('scan through regions', function() {
var Scan = require('../lib/scan');
it('should through regions', function(done) {
var scan = new Scan();
var rows = [];
var count = function() {
rows.length.should.equal(49);
done();
};
client.getScanner(config.tableUser, scan, function(err, scanner) {
var next = function() {
scanner.next(function(err, row) {
should.ifError(err);
if (!row) {
return count();
}
rows.push(row);
next();
});
};
next();
});
});
it('should through regions with cache 10', function(done) {
var scan = new Scan();
scan.caching = 10;
var rows = [];
var count = function() {
rows.length.should.equal(49);
done();
};
client.getScanner(config.tableUser, scan, function(err, scanner) {
var next = function() {
scanner.next(3, function(err, ret) {
should.ifError(err);
if (!ret.length) {
return count();
}
rows = Array.prototype.concat.apply(rows, ret);
next();
});
};
next();
});
});
it('should cache even it\'s closed', function(done) {
var scan = new Scan();
scan.caching = 10;
var rows = [];
var count = function() {
rows.length.should.equal(10);
done();
};
client.getScanner(config.tableUser, scan, function(err, scanner) {
var next = function() {
scanner.next(function(err, row) {
should.ifError(err);
if (!rows.length) {
return scanner.close(function() {
rows.push(row);
next();
});
}
if (!row) {
return count();
}
rows.push(row);
next();
});
};
next();
});
});
it('should scan through regions with stop row', function(done) {
var scan = new Scan(null, '7fffffdd');
scan.caching = 10;
var rows = [];
var count = function() {
rows.length.should.equal(24);
done();
};
client.getScanner(config.tableUser, scan, function(err, scanner) {
var next = function() {
scanner.next(function(err, row) {
should.ifError(err);
if (!row) {
return count();
}
rows.push(row);
next();
});
};
next();
});
});
it('should scan error', function(done) {
var scan = new Scan(null, '7fffffdd');
scan.caching = 10;
var rows = [];
var count = function() {
rows.length.should.equal(10);
done();
};
var times = 0;
client.getScanner(config.tableUser, scan, function(err, scanner) {
var next = function() {
scanner.next(function(err, row) {
times++;
if (times < 11) {
should.ifError(err);
} else {
err.message.should.startWith(
'java.io.IOException: java.lang.NoSuchMethodException: ' +
'org.apache.hadoop.hbase.ipc.HRegionInterface.next(int, int)');
return count();
}
if (!rows.length) {
rows.push(row);
scanner.id = -1;
return next();
}
rows.push(row);
next();
});
};
next();
});
});
});
});