Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/v1/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import Record from './record';
import {Driver, READ, WRITE} from './driver';
import RoutingDriver from './routing-driver';
import VERSION from '../version';
import {parseScheme, parseUrl} from "./internal/connector";
import {assertString} from "./internal/util";
import {parseScheme, parseUrl, parseRoutingContext} from "./internal/connector";
import {assertString, isEmptyObjectOrNull} from "./internal/util";


const auth ={
Expand Down Expand Up @@ -120,13 +120,17 @@ let USER_AGENT = "neo4j-javascript/" + VERSION;
function driver(url, authToken, config = {}) {
assertString(url, 'Bolt URL');
const scheme = parseScheme(url);
const routingContext = parseRoutingContext(url);
if (scheme === "bolt+routing://") {
return new RoutingDriver(parseUrl(url), USER_AGENT, authToken, config);
return new RoutingDriver(parseUrl(url), routingContext, USER_AGENT, authToken, config);
} else if (scheme === "bolt://") {
if(!isEmptyObjectOrNull(routingContext))
{
throw new Error("Routing context are not supported with scheme 'bolt'. Given URI: '" + url + "'");
}
return new Driver(parseUrl(url), USER_AGENT, authToken, config);
} else {
throw new Error("Unknown scheme: " + scheme);

}
}

Expand Down
5 changes: 3 additions & 2 deletions src/v1/internal/connection-providers.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import RoutingTable from './routing-table';
import Rediscovery from './rediscovery';
import hasFeature from './features';
import {DnsHostNameResolver, DummyHostNameResolver} from './host-name-resolvers';
import GetServersUtil from './get-servers-util';

class ConnectionProvider {

Expand Down Expand Up @@ -61,11 +62,11 @@ export class DirectConnectionProvider extends ConnectionProvider {

export class LoadBalancer extends ConnectionProvider {

constructor(address, connectionPool, driverOnErrorCallback) {
constructor(address, routingContext, connectionPool, driverOnErrorCallback) {
super();
this._seedRouter = address;
this._routingTable = new RoutingTable(new RoundRobinArray([this._seedRouter]));
this._rediscovery = new Rediscovery();
this._rediscovery = new Rediscovery(new GetServersUtil(routingContext));
this._connectionPool = connectionPool;
this._driverOnErrorCallback = driverOnErrorCallback;
this._hostNameResolver = LoadBalancer._createHostNameResolver();
Expand Down
22 changes: 20 additions & 2 deletions src/v1/internal/connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ MAGIC_PREAMBLE = 0x6060B017,
DEBUG = false;

let URLREGEX = new RegExp([
"([^/]+//)?", // scheme
"([^/]+//)?", // scheme
"(([^:/?#]*)", // hostname
"(?::([0-9]+))?)", // port (optional)
".*"].join("")); // everything else
"([^?]*)?", // everything else
"(\\?(.+))?" // query
].join(""));

function parseScheme( url ) {
let scheme = url.match(URLREGEX)[1] || '';
Expand All @@ -82,6 +84,21 @@ function parsePort( url ) {
return url.match( URLREGEX )[4];
}

function parseRoutingContext(url) {
const query = url.match(URLREGEX)[7] || '';
const map = {};
if (query.length !== 0) {
query.split("&").forEach(val => {
const keyValue = val.split("=");
if (keyValue.length !== 2) {
throw new Error("Invalid parameters: '" + keyValue + "' in url '" + url + "'.");
}
map[keyValue[0]] = keyValue[1];
});
}
return map;
}

/**
* Very rudimentary log handling, should probably be replaced by something proper at some point.
* @param actor the part that sent the message, 'S' for server and 'C' for client
Expand Down Expand Up @@ -495,5 +512,6 @@ export {
parseUrl,
parseHost,
parsePort,
parseRoutingContext,
Connection
}
43 changes: 31 additions & 12 deletions src/v1/internal/get-servers-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,44 @@
import RoundRobinArray from './round-robin-array';
import {newError, PROTOCOL_ERROR, SERVICE_UNAVAILABLE} from '../error';
import Integer, {int} from '../integer';
import {ServerVersion, VERSION3_2} from './server-version-util'

const PROCEDURE_CALL = 'CALL dbms.cluster.routing.getServers';
const CALL_GET_SERVERS = 'CALL dbms.cluster.routing.getServers';
const GET_ROUTING_TABLE_PARAM = "context";
const CALL_GET_ROUTING_TABLE = "CALL dbms.cluster.routing.getRoutingTable({"
+ GET_ROUTING_TABLE_PARAM + "})";
const PROCEDURE_NOT_FOUND_CODE = 'Neo.ClientError.Procedure.ProcedureNotFound';

export default class GetServersUtil {

constructor(routingContext={}) {
this._routingContext = routingContext;
}

callGetServers(session, routerAddress) {
return session.run(PROCEDURE_CALL).then(result => {
session.close();
return result.records;
}).catch(error => {
if (error.code === PROCEDURE_NOT_FOUND_CODE) {
// throw when getServers procedure not found because this is clearly a configuration issue
throw newError('Server ' + routerAddress + ' could not perform routing. ' +
'Make sure you are connecting to a causal cluster', SERVICE_UNAVAILABLE);
session.run("RETURN 1").then(result=>{
let statement = {text:CALL_GET_SERVERS};

if(ServerVersion.fromString(result.summary.server.version).compare(VERSION3_2)>=0)
{
statement = {
text:CALL_GET_ROUTING_TABLE,
parameters:{GET_ROUTING_TABLE_PARAM: this._routingContext}};
}
// return nothing when failed to connect because code higher in the callstack is still able to retry with a
// different session towards a different router
return null;

return session.run(statement).then(result => {
session.close();
return result.records;
}).catch(error => {
if (error.code === PROCEDURE_NOT_FOUND_CODE) {
// throw when getServers procedure not found because this is clearly a configuration issue
throw newError('Server ' + routerAddress + ' could not perform routing. ' +
'Make sure you are connecting to a causal cluster', SERVICE_UNAVAILABLE);
}
// return nothing when failed to connect because code higher in the callstack is still able to retry with a
// different session towards a different router
return null;
});
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/v1/internal/rediscovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {newError, PROTOCOL_ERROR} from "../error";
export default class Rediscovery {

constructor(getServersUtil) {
this._getServersUtil = getServersUtil || new GetServersUtil();
this._getServersUtil = getServersUtil;
}

lookupRoutingTableOnRouter(session, routerAddress) {
Expand Down
73 changes: 73 additions & 0 deletions src/v1/internal/server-version-util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Copyright (c) 2002-2017 "Neo Technology,","
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

let SERVER_VERSION_REGEX = new RegExp("(Neo4j/)?(\\d+)\\.(\\d+)(?:\\.)?(\\d*)(\\.|-|\\+)?([0-9A-Za-z-.]*)?");

class ServerVersion {
constructor(major, minor, patch) {
this._major = major;
this._minor = minor;
this._patch = patch;
}

static fromString(versionStr) {
if (!versionStr) {
return new ServerVersion(3, 0, 0);
}
else {
const version = versionStr.match(SERVER_VERSION_REGEX);
return new ServerVersion(version[2], version[3], version[4]);
}
}

compare(other) {
const version = this._parseToNumber();
const otherVersion = other._parseToNumber();

if (version == otherVersion) {
return 0;
}
if (version > otherVersion) {
return 1;
}
else {
return -1;
}
}

_parseToNumber() {
let value = 0;
value += parseInt(this._major) * 100 + parseInt(this._minor) * 10;
if (!isEmptyObjectOrNull(this._patch)) {
value += parseInt(this._patch);
}
return value;
}
}

const VERSION3_2 = new ServerVersion(3, 2, 0);

export{
ServerVersion,
VERSION3_2
}




5 changes: 3 additions & 2 deletions src/v1/routing-driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ import {LoadBalancer} from './internal/connection-providers';
*/
class RoutingDriver extends Driver {

constructor(url, userAgent, token = {}, config = {}) {
constructor(url, routingContext, userAgent, token = {}, config = {}) {
super(url, userAgent, token, RoutingDriver._validateConfig(config));
this._routingContext = routingContext;
}

_createConnectionProvider(address, connectionPool, driverOnErrorCallback) {
return new LoadBalancer(address, connectionPool, driverOnErrorCallback);
return new LoadBalancer(address, this._routingContext, connectionPool, driverOnErrorCallback);
}

_createSession(mode, connectionProvider, bookmark, config) {
Expand Down
4 changes: 2 additions & 2 deletions test/internal/connection-providers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('LoadBalancer', () => {
});

it('initializes routing table with the given router', () => {
const loadBalancer = new LoadBalancer('server-ABC', newPool(), NO_OP_DRIVER_CALLBACK);
const loadBalancer = new LoadBalancer('server-ABC', {}, newPool(), NO_OP_DRIVER_CALLBACK);

expectRoutingTable(loadBalancer,
['server-ABC'],
Expand Down Expand Up @@ -1040,7 +1040,7 @@ function newLoadBalancerWithSeedRouter(seedRouter, seedRouterResolved,
expirationTime = Integer.MAX_VALUE,
routerToRoutingTable = {},
connectionPool = null) {
const loadBalancer = new LoadBalancer(seedRouter, connectionPool || newPool(), NO_OP_DRIVER_CALLBACK);
const loadBalancer = new LoadBalancer(seedRouter, {}, connectionPool || newPool(), NO_OP_DRIVER_CALLBACK);
loadBalancer._routingTable = new RoutingTable(
new RoundRobinArray(routers),
new RoundRobinArray(readers),
Expand Down
7 changes: 4 additions & 3 deletions test/internal/get-servers-util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,10 @@ describe('get-servers-util', () => {

class FakeSession {

constructor(runResponse) {
this._runResponse = runResponse;
constructor(runResponses) {
this._runResponses = runResponses;
this._closed = false;
this._runCounter = 0;
}

static successful(result) {
Expand All @@ -259,7 +260,7 @@ describe('get-servers-util', () => {
}

run() {
return this._runResponse;
return this._runResponses[this._runCounter ++];
}

close() {
Expand Down
33 changes: 32 additions & 1 deletion test/internal/host-name-resolvers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,38 @@

import {DnsHostNameResolver, DummyHostNameResolver} from '../../src/v1/internal/host-name-resolvers';
import hasFeature from '../../src/v1/internal/features';
import {parseHost, parsePort, parseScheme} from '../../src/v1/internal/connector';
import {parseHost, parsePort, parseScheme, parseRoutingContext} from '../../src/v1/internal/connector';

describe('RoutingContextParser', ()=>{

it('should parse routing context', done => {
const url = "bolt://localhost:7687/cat?name=molly&age=1&color=white";
const context = parseRoutingContext(url);
expect(context).toEqual({name:"molly", age:"1", color:"white"});

done();
});

it('should return empty routing context', done =>{
const url1 = "bolt://localhost:7687/cat?";
const context1 = parseRoutingContext(url1);
expect(context1).toEqual({});

const url2 = "bolt://localhost:7687/lalala";
const context2 = parseRoutingContext(url2);
expect(context2).toEqual({});

done();
});

it('should error for unmatched pair', done=>{
const url = "bolt://localhost?cat";
expect(()=>parseRoutingContext(url)).toThrow(
new Error("Invalid parameters: 'cat' in url 'bolt://localhost?cat'."));

done();
});
});

describe('DummyHostNameResolver', () => {

Expand Down