* Validates and sets the local data center to be used. * @param {LoadBalancingPolicy} lbp * @param {Client} client * @param {HostMap} hosts * @private
(lbp, client, hosts)
| 841 | * @private |
| 842 | */ |
| 843 | function setLocalDc(lbp, client, hosts) { |
| 844 | if (!(lbp instanceof LoadBalancingPolicy)) { |
| 845 | throw new errors.DriverInternalError('LoadBalancingPolicy instance was not provided'); |
| 846 | } |
| 847 | |
| 848 | if (client && client.options) { |
| 849 | if (lbp.localDc && !client.options.localDataCenter) { |
| 850 | client.log('info', `Local data center '${lbp.localDc}' was provided as an argument to the load-balancing` + |
| 851 | ` policy. It is preferable to specify the local data center using 'localDataCenter' in Client` + |
| 852 | ` options instead when your application is targeting a single data center.`); |
| 853 | } |
| 854 | |
| 855 | // If localDc is unset, use value set in client options. |
| 856 | lbp.localDc = lbp.localDc || client.options.localDataCenter; |
| 857 | } |
| 858 | |
| 859 | const dcs = getDataCenters(hosts); |
| 860 | |
| 861 | if (!lbp.localDc) { |
| 862 | throw new errors.ArgumentError( |
| 863 | `'localDataCenter' is not defined in Client options and also was not specified in constructor.` + |
| 864 | ` At least one is required. Available DCs are: [${Array.from(dcs)}]`); |
| 865 | } |
| 866 | |
| 867 | if (!dcs.has(lbp.localDc)) { |
| 868 | throw new errors.ArgumentError(`Datacenter ${lbp.localDc} was not found. Available DCs are: [${Array.from(dcs)}]`); |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | function getDataCenters(hosts) { |
| 873 | return new Set(hosts.values().map(h => h.datacenter)); |
no test coverage detected