Skip to content

Commit 5357496

Browse files
authored
Expose AllowListPolicy and deprecate WhiteListPolicy
1 parent 51f6a8b commit 5357496

File tree

12 files changed

+97
-79
lines changed

12 files changed

+97
-79
lines changed

doc/features/tuning-policies/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ hosts in the remote data centers, but those are always tried after the local nod
2020
- `RoundRobinPolicy`: a policy that yields nodes in a round-robin fashion.
2121
- `TokenAwarePolicy`: a policy that yields replica nodes for a given partition key and keyspace. The token-aware policy
2222
uses a child policy to retrieve the next nodes in case the replicas for a partition key are not available.
23-
- `WhiteListPolicy`: a policy that wraps the provided child policy but only "allow" hosts from the provided
24-
whilelist. Keep in mind however that this policy defeats somewhat the host auto-detection of the driver. As such, this
23+
- `AllowListPolicy`: a policy that wraps the provided child policy but only "allow" hosts from the provided
24+
list. Keep in mind however that this policy defeats somewhat the host auto-detection of the driver. As such, this
2525
policy is only useful in a few special cases or for testing, but is not optimal in general.
2626

2727
### Default load-balancing policy

lib/policies/index.d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@ export namespace policies {
6363
constructor(childPolicy: LoadBalancingPolicy);
6464
}
6565

66-
class WhiteListPolicy extends LoadBalancingPolicy {
67-
constructor(childPolicy: LoadBalancingPolicy, whiteList: string[]);
66+
class AllowListPolicy extends LoadBalancingPolicy {
67+
constructor(childPolicy: LoadBalancingPolicy, allowList: string[]);
68+
}
69+
70+
class WhiteListPolicy extends AllowListPolicy {
6871
}
6972

7073
class RoundRobinPolicy extends LoadBalancingPolicy {

lib/policies/load-balancing.js

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -366,15 +366,15 @@ TokenAwarePolicy.prototype.getOptions = function () {
366366

367367
/**
368368
* Create a new policy that wraps the provided child policy but only "allow" hosts
369-
* from the provided while list.
369+
* from the provided list.
370370
* @class
371371
* @classdesc
372372
* A load balancing policy wrapper that ensure that only hosts from a provided
373-
* white list will ever be returned.
373+
* allow list will ever be returned.
374374
* <p>
375375
* This policy wraps another load balancing policy and will delegate the choice
376376
* of hosts to the wrapped policy with the exception that only hosts contained
377-
* in the white list provided when constructing this policy will ever be
377+
* in the allow list provided when constructing this policy will ever be
378378
* returned. Any host not in the while list will be considered ignored
379379
* and thus will not be connected to.
380380
* <p>
@@ -386,39 +386,36 @@ TokenAwarePolicy.prototype.getOptions = function () {
386386
* data-center then you should use DCAwareRoundRobinPolicy and *not* this policy
387387
* in particular.
388388
* @param {LoadBalancingPolicy} childPolicy the wrapped policy.
389-
* @param {Array.<string>} whiteList the white listed hosts address in the format ipAddress:port.
389+
* @param {Array.<string>} allowList The hosts address in the format ipAddress:port.
390390
* Only hosts from this list may get connected
391391
* to (whether they will get connected to or not depends on the child policy).
392392
* @extends LoadBalancingPolicy
393393
* @constructor
394394
*/
395-
function WhiteListPolicy (childPolicy, whiteList) {
395+
function AllowListPolicy (childPolicy, allowList) {
396396
if (!childPolicy) {
397397
throw new Error("You must specify a child load balancing policy");
398398
}
399-
if (!Array.isArray(whiteList)) {
400-
throw new Error("You must provide the white list of host addresses");
399+
if (!Array.isArray(allowList)) {
400+
throw new Error("You must provide the list of allowed host addresses");
401401
}
402+
402403
this.childPolicy = childPolicy;
403-
const map = {};
404-
whiteList.forEach(function (address) {
405-
map[address] = true;
406-
});
407-
this.whiteList = map;
404+
this.allowList = new Map(allowList.map(address => [ address, true ]));
408405
}
409406

410-
util.inherits(WhiteListPolicy, LoadBalancingPolicy);
407+
util.inherits(AllowListPolicy, LoadBalancingPolicy);
411408

412-
WhiteListPolicy.prototype.init = function (client, hosts, callback) {
409+
AllowListPolicy.prototype.init = function (client, hosts, callback) {
413410
this.childPolicy.init(client, hosts, callback);
414411
};
415412

416413
/**
417-
* Uses the child policy to return the distance to the host if included in the white list.
414+
* Uses the child policy to return the distance to the host if included in the allow list.
418415
* Any host not in the while list will be considered ignored.
419416
* @param host
420417
*/
421-
WhiteListPolicy.prototype.getDistance = function (host) {
418+
AllowListPolicy.prototype.getDistance = function (host) {
422419
if (!this._contains(host)) {
423420
return types.distance.ignored;
424421
}
@@ -430,14 +427,14 @@ WhiteListPolicy.prototype.getDistance = function (host) {
430427
* @returns {boolean}
431428
* @private
432429
*/
433-
WhiteListPolicy.prototype._contains = function (host) {
434-
return !!this.whiteList[host.address];
430+
AllowListPolicy.prototype._contains = function (host) {
431+
return !!this.allowList.get(host.address);
435432
};
436433

437434
/**
438-
* Returns the hosts to use for a new query filtered by the white list.
435+
* Returns the hosts to use for a new query filtered by the allow list.
439436
*/
440-
WhiteListPolicy.prototype.newQueryPlan = function (keyspace, info, callback) {
437+
AllowListPolicy.prototype.newQueryPlan = function (keyspace, info, callback) {
441438
const self = this;
442439
this.childPolicy.newQueryPlan(keyspace, info, function (err, iterator) {
443440
if (err) {
@@ -447,7 +444,7 @@ WhiteListPolicy.prototype.newQueryPlan = function (keyspace, info, callback) {
447444
});
448445
};
449446

450-
WhiteListPolicy.prototype._filter = function (childIterator) {
447+
AllowListPolicy.prototype._filter = function (childIterator) {
451448
const self = this;
452449
return {
453450
next: function () {
@@ -463,13 +460,31 @@ WhiteListPolicy.prototype._filter = function (childIterator) {
463460
/**
464461
* Gets an associative array containing the policy options.
465462
*/
466-
WhiteListPolicy.prototype.getOptions = function () {
463+
AllowListPolicy.prototype.getOptions = function () {
467464
return new Map([
468465
['childPolicy', this.childPolicy.constructor !== undefined ? this.childPolicy.constructor.name : null ],
469-
['whitelist', Object.keys(this.whiteList)]
466+
['allowList', Array.from(this.allowList.keys())]
470467
]);
471468
};
472469

470+
/**
471+
* Creates a new instance of the policy.
472+
* @classdesc
473+
* Exposed for backward-compatibility only, it's recommended that you use {@link AllowListPolicy} instead.
474+
* @param {LoadBalancingPolicy} childPolicy the wrapped policy.
475+
* @param {Array.<string>} allowList The hosts address in the format ipAddress:port.
476+
* Only hosts from this list may get connected to (whether they will get connected to or not depends on the child
477+
* policy).
478+
* @extends AllowListPolicy
479+
* @deprecated Use allow-list instead. It will be removed in future major versions.
480+
* @constructor
481+
*/
482+
function WhiteListPolicy(childPolicy, allowList) {
483+
AllowListPolicy.call(this, childPolicy, allowList);
484+
}
485+
486+
util.inherits(WhiteListPolicy, AllowListPolicy);
487+
473488
/**
474489
* A load-balancing policy implementation that attempts to fairly distribute the load based on the amount of in-flight
475490
* request per hosts. The local replicas are initially shuffled and
@@ -856,9 +871,13 @@ function getDataCenters(hosts) {
856871
return new Set(hosts.values().map(h => h.datacenter));
857872
}
858873

859-
exports.DCAwareRoundRobinPolicy = DCAwareRoundRobinPolicy;
860-
exports.DefaultLoadBalancingPolicy = DefaultLoadBalancingPolicy;
861-
exports.LoadBalancingPolicy = LoadBalancingPolicy;
862-
exports.RoundRobinPolicy = RoundRobinPolicy;
863-
exports.TokenAwarePolicy = TokenAwarePolicy;
864-
exports.WhiteListPolicy = WhiteListPolicy;
874+
module.exports = {
875+
AllowListPolicy,
876+
DCAwareRoundRobinPolicy,
877+
DefaultLoadBalancingPolicy,
878+
LoadBalancingPolicy,
879+
RoundRobinPolicy,
880+
TokenAwarePolicy,
881+
// Deprecated: for backward compatibility only.
882+
WhiteListPolicy
883+
};

test/integration/short/client-execute-simulator-tests.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ const errors = require('../../../lib/errors');
2525

2626
const { responseErrorCodes } = require('../../../lib/types');
2727
const Client = require('../../../lib/client');
28-
const DCAwareRoundRobinPolicy = require('../../../lib/policies').loadBalancing.DCAwareRoundRobinPolicy;
29-
const WhiteListPolicy = require('../../../lib/policies').loadBalancing.WhiteListPolicy;
28+
const { AllowListPolicy, DCAwareRoundRobinPolicy } = require('../../../lib/policies').loadBalancing;
3029

3130
const query = "select * from data";
3231
const clusterSize = 3;
@@ -46,7 +45,7 @@ describe('Client', function() {
4645
localDataCenter: 'dc1',
4746
policies: {
4847
// define an LBP that includes all nodes except node 3
49-
loadBalancing: new WhiteListPolicy(new DCAwareRoundRobinPolicy(), [
48+
loadBalancing: new AllowListPolicy(new DCAwareRoundRobinPolicy(), [
5049
cluster.node(0).address,
5150
cluster.node(1).address,
5251
cluster.node(2).address

test/integration/short/execution-profile-tests.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
'use strict';
18+
1719
const assert = require('assert');
1820

19-
const helper = require('../../test-helper.js');
20-
const Client = require('../../../lib/client.js');
21+
const helper = require('../../test-helper');
22+
const Client = require('../../../lib/client');
2123
const types = require('../../../lib/types');
22-
const utils = require('../../../lib/utils.js');
24+
const utils = require('../../../lib/utils');
2325
const simulacron = require('../simulacron');
24-
const loadBalancing = require('../../../lib/policies/load-balancing.js');
25-
const DCAwareRoundRobinPolicy = loadBalancing.DCAwareRoundRobinPolicy;
26-
const WhiteListPolicy = loadBalancing.WhiteListPolicy;
27-
const ExecutionProfile = require('../../../lib/execution-profile.js').ExecutionProfile;
26+
const { AllowListPolicy, DCAwareRoundRobinPolicy } = require('../../../lib/policies/load-balancing');
27+
const { ExecutionProfile } = require('../../../lib/execution-profile');
2828

2929
describe('ProfileManager', function() {
3030
this.timeout(40000);
@@ -69,8 +69,8 @@ describe('ProfileManager', function() {
6969
loadBalancing: decorateInitWithCounter(new DCAwareRoundRobinPolicy('dc1'))
7070
});
7171
// A profile that targets 127.0.0.4 specifically.
72-
const wlProfile = new ExecutionProfile('whitelist', {
73-
loadBalancing: decorateInitWithCounter(new WhiteListPolicy(new DCAwareRoundRobinPolicy('dc2'), [ node4 ]))
72+
const wlProfile = new ExecutionProfile('allowlist', {
73+
loadBalancing: decorateInitWithCounter(new AllowListPolicy(new DCAwareRoundRobinPolicy('dc2'), [ node4 ]))
7474
});
7575
// A profile with no defined lbp, it should fallback on the default profile's lbp.
7676
const emptyProfile = new ExecutionProfile('empty');
@@ -143,7 +143,7 @@ describe('ProfileManager', function() {
143143
hosts.forEach(function(h) {
144144
const distance = client.profileManager.getDistance(h);
145145
// all hosts except 3 should be at a distance of local since a profile exists for all DCs
146-
// with DC2 white listing host 4. While host 5 is ignored in whitelist profile, it is remote in others
146+
// with DC2 allow-listing host 4. While host 5 is ignored in allowlist profile, it is remote in others
147147
// so it should be considered remote.
148148
const expectedDistance = h.address === node3 ? types.distance.ignored : types.distance.local;
149149
assert.strictEqual(distance, expectedDistance, "Expected distance of " + expectedDistance + " for host " + h.address);
@@ -157,7 +157,7 @@ describe('ProfileManager', function() {
157157
it('should only use hosts from the load balancing policy in the default profile', ensureOnlyHostsUsed([0, 1]));
158158
it('should only use hosts from the load balancing policy in the default profile when profile doesn\'t have policy', ensureOnlyHostsUsed([0, 1], 'empty'));
159159
it('should only use hosts from the load balancing policy in the default profile when specified', ensureOnlyHostsUsed([0, 1], 'default'));
160-
it('should only use hosts from the load balancing policy in whitelist profile', ensureOnlyHostsUsed([3], 'whitelist'));
160+
it('should only use hosts from the load balancing policy in allowlist profile', ensureOnlyHostsUsed([3], 'allowlist'));
161161
it('should fallback on client load balancing policy when default profile has no lbp', function (done) {
162162
const policy = new DCAwareRoundRobinPolicy('dc2');
163163
const profiles = [new ExecutionProfile('default'), new ExecutionProfile('empty')];

test/integration/short/load-balancing-tests.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ const helper = require('../../test-helper');
2121
const Client = require('../../../lib/client');
2222
const utils = require('../../../lib/utils');
2323
const types = require('../../../lib/types');
24-
const loadBalancing = require('../../../lib/policies/load-balancing');
25-
const RoundRobinPolicy = loadBalancing.RoundRobinPolicy;
26-
const TokenAwarePolicy = loadBalancing.TokenAwarePolicy;
27-
const WhiteListPolicy = loadBalancing.WhiteListPolicy;
24+
const { RoundRobinPolicy, AllowListPolicy, TokenAwarePolicy} = require('../../../lib/policies/load-balancing');
2825
const vdescribe = helper.vdescribe;
2926

3027
const maxInFlightRequests = 16;
@@ -112,9 +109,9 @@ context('with a reusable 3 node cluster', function () {
112109
'ALTER KEYSPACE system_traces WITH replication = {\'class\': \'SimpleStrategy\', \'replication_factor\': \'1\'}'
113110
]
114111
});
115-
vdescribe('2.0', 'WhiteListPolicy', function () {
116-
it('should use the hosts in the white list only', function (done) {
117-
const policy = new WhiteListPolicy(new RoundRobinPolicy(), ['127.0.0.1:9042', '127.0.0.2:9042']);
112+
vdescribe('2.0', 'AllowListPolicy', function () {
113+
it('should use the hosts in the allow list only', function (done) {
114+
const policy = new AllowListPolicy(new RoundRobinPolicy(), ['127.0.0.1:9042', '127.0.0.2:9042']);
118115
const client = newInstance(policy);
119116
utils.timesLimit(100, maxInFlightRequests, function (n, next) {
120117
client.execute(helper.queries.basic, function (err, result) {

test/integration/short/metadata-tests.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ describe('metadata @SERVER_API', function () {
325325
describe('#getTrace()', function () {
326326
it('should retrieve the trace immediately after', function (done) {
327327
// use a single node
328-
const lbp = new helper.WhiteListPolicy(['1']);
328+
const lbp = new helper.AllowListPolicy(['1']);
329329
const client = newInstance({policies: {loadBalancing: lbp}});
330330
let traceId;
331331
utils.series([
@@ -363,7 +363,7 @@ describe('metadata @SERVER_API', function () {
363363
});
364364
it('should retrieve the trace a few seconds after', function (done) {
365365
// use a single node
366-
const lbp = new helper.WhiteListPolicy(['2']);
366+
const lbp = new helper.AllowListPolicy(['2']);
367367
const client = newInstance({policies: {loadBalancing: lbp}});
368368
let traceId;
369369
utils.series([

test/test-helper.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ const helper = {
580580
},
581581
Map: MapPolyFill,
582582
Set: SetPolyFill,
583-
WhiteListPolicy: WhiteListPolicy,
583+
AllowListPolicy: AllowListPolicy,
584584
FallthroughRetryPolicy: FallthroughRetryPolicy,
585585
/**
586586
* Determines if test tracing is enabled
@@ -1601,18 +1601,18 @@ RetryMultipleTimes.prototype.onWriteTimeout = function (requestInfo) {
16011601
* @param [childPolicy]
16021602
* @constructor
16031603
*/
1604-
function WhiteListPolicy(list, childPolicy) {
1604+
function AllowListPolicy(list, childPolicy) {
16051605
this.list = list;
16061606
this.childPolicy = childPolicy || new policies.loadBalancing.RoundRobinPolicy();
16071607
}
16081608

1609-
util.inherits(WhiteListPolicy, policies.loadBalancing.LoadBalancingPolicy);
1609+
util.inherits(AllowListPolicy, policies.loadBalancing.LoadBalancingPolicy);
16101610

1611-
WhiteListPolicy.prototype.init = function (client, hosts, callback) {
1611+
AllowListPolicy.prototype.init = function (client, hosts, callback) {
16121612
this.childPolicy.init(client, hosts, callback);
16131613
};
16141614

1615-
WhiteListPolicy.prototype.newQueryPlan = function (keyspace, info, callback) {
1615+
AllowListPolicy.prototype.newQueryPlan = function (keyspace, info, callback) {
16161616
const list = this.list;
16171617
this.childPolicy.newQueryPlan(keyspace, info, function (err, iterator) {
16181618
callback(err, {

test/unit/api-tests.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ describe('API', function () {
104104
assert.ok(api.errors);
105105
assert.ok(api.policies);
106106
assert.ok(api.policies.loadBalancing);
107+
checkConstructor(api.policies.loadBalancing, 'AllowListPolicy');
108+
// For backward compatibility only
109+
checkConstructor(api.policies.loadBalancing, 'WhiteListPolicy');
107110
assert.ok(api.policies.retry);
108111
assert.ok(api.policies.reconnection);
109112
assert.ok(api.metadata);

test/unit/load-balancing-tests.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,13 @@ const assert = require('assert');
1919
const helper = require('../test-helper.js');
2020
const errors = require('../../lib/errors');
2121
const Client = require('../../lib/client.js');
22-
const clientOptions = require('../../lib/client-options.js');
23-
const Host = require('../../lib/host.js').Host;
24-
const HostMap = require('../../lib/host.js').HostMap;
22+
const clientOptions = require('../../lib/client-options');
23+
const { Host, HostMap } = require('../../lib/host');
2524
const types = require('../../lib/types');
26-
const utils = require('../../lib/utils.js');
27-
const loadBalancing = require('../../lib/policies/load-balancing.js');
28-
const ExecutionOptions = require('../../lib/execution-options').ExecutionOptions;
29-
const LoadBalancingPolicy = loadBalancing.LoadBalancingPolicy;
30-
const TokenAwarePolicy = loadBalancing.TokenAwarePolicy;
31-
const RoundRobinPolicy = loadBalancing.RoundRobinPolicy;
32-
const DCAwareRoundRobinPolicy = loadBalancing.DCAwareRoundRobinPolicy;
33-
const WhiteListPolicy = loadBalancing.WhiteListPolicy;
25+
const utils = require('../../lib/utils');
26+
const { ExecutionOptions } = require('../../lib/execution-options');
27+
const { AllowListPolicy, LoadBalancingPolicy, TokenAwarePolicy, RoundRobinPolicy, DCAwareRoundRobinPolicy } =
28+
require('../../lib/policies/load-balancing');
3429

3530
describe('RoundRobinPolicy', function () {
3631
it('should yield an error when the hosts are not set', function(done) {
@@ -447,7 +442,7 @@ describe('TokenAwarePolicy', function () {
447442
});
448443
});
449444
});
450-
describe('WhiteListPolicy', function () {
445+
describe('AllowListPolicy', function () {
451446
it('should use the childPolicy to determine the distance', function () {
452447
let getDistanceCalled = 0;
453448
const childPolicy = {
@@ -456,7 +451,7 @@ describe('WhiteListPolicy', function () {
456451
return types.distance.local;
457452
}
458453
};
459-
const policy = new WhiteListPolicy(childPolicy, ['h1:9042', 'h2:9042']);
454+
const policy = new AllowListPolicy(childPolicy, ['h1:9042', 'h2:9042']);
460455
assert.strictEqual(policy.getDistance({ address: 'h1:9042'}), types.distance.local);
461456
assert.strictEqual(getDistanceCalled, 1);
462457
assert.strictEqual(policy.getDistance({ address: 'h2:9042'}), types.distance.local);
@@ -471,7 +466,7 @@ describe('WhiteListPolicy', function () {
471466
cb(null, utils.arrayIterator([{ address: '1.1.1.1:9042'}, { address: '1.1.1.2:9042'}, { address: '1.1.1.3:9042'}]));
472467
}
473468
};
474-
const policy = new WhiteListPolicy(childPolicy, ['1.1.1.3:9042', '1.1.1.1:9042']);
469+
const policy = new AllowListPolicy(childPolicy, ['1.1.1.3:9042', '1.1.1.1:9042']);
475470
policy.newQueryPlan('ks1', {}, function (err, iterator) {
476471
assert.ifError(err);
477472
const hosts = helper.iteratorToArray(iterator);
@@ -484,8 +479,8 @@ describe('WhiteListPolicy', function () {
484479

485480
describe('#getOptions()', () => {
486481
it('should return a Map with the child policy name', () => {
487-
helper.assertMapEqual(new WhiteListPolicy(new RoundRobinPolicy(), ['a', 'b']).getOptions(),
488-
new Map([['childPolicy', 'RoundRobinPolicy'], ['whitelist', ['a', 'b']]]));
482+
helper.assertMapEqual(new AllowListPolicy(new RoundRobinPolicy(), ['a', 'b']).getOptions(),
483+
new Map([['childPolicy', 'RoundRobinPolicy'], ['allowList', ['a', 'b']]]));
489484
});
490485
});
491486
});

0 commit comments

Comments
 (0)