|
20 | 20 | import RoundRobinArray from './round-robin-array'; |
21 | 21 | import {newError, PROTOCOL_ERROR, SERVICE_UNAVAILABLE} from '../error'; |
22 | 22 | import Integer, {int} from '../integer'; |
| 23 | +import {ServerVersion, VERSION3_2} from './server-version-util' |
23 | 24 |
|
24 | | -const PROCEDURE_CALL = 'CALL dbms.cluster.routing.getServers'; |
| 25 | +const CALL_GET_SERVERS = 'CALL dbms.cluster.routing.getServers'; |
| 26 | +const GET_ROUTING_TABLE_PARAM = "context"; |
| 27 | +const CALL_GET_ROUTING_TABLE = "CALL dbms.cluster.routing.getRoutingTable({" |
| 28 | + + GET_ROUTING_TABLE_PARAM + "})"; |
25 | 29 | const PROCEDURE_NOT_FOUND_CODE = 'Neo.ClientError.Procedure.ProcedureNotFound'; |
26 | 30 |
|
27 | 31 | export default class GetServersUtil { |
28 | 32 |
|
| 33 | + constructor(routingContext={}) { |
| 34 | + this._routingContext = routingContext; |
| 35 | + } |
| 36 | + |
29 | 37 | callGetServers(session, routerAddress) { |
30 | | - return session.run(PROCEDURE_CALL).then(result => { |
31 | | - session.close(); |
32 | | - return result.records; |
33 | | - }).catch(error => { |
34 | | - if (error.code === PROCEDURE_NOT_FOUND_CODE) { |
35 | | - // throw when getServers procedure not found because this is clearly a configuration issue |
36 | | - throw newError('Server ' + routerAddress + ' could not perform routing. ' + |
37 | | - 'Make sure you are connecting to a causal cluster', SERVICE_UNAVAILABLE); |
| 38 | + session.run("RETURN 1").then(result=>{ |
| 39 | + let statement = {text:CALL_GET_SERVERS}; |
| 40 | + |
| 41 | + if(ServerVersion.fromString(result.summary.server.version).compare(VERSION3_2)>=0) |
| 42 | + { |
| 43 | + statement = { |
| 44 | + text:CALL_GET_ROUTING_TABLE, |
| 45 | + parameters:{GET_ROUTING_TABLE_PARAM: this._routingContext}}; |
38 | 46 | } |
39 | | - // return nothing when failed to connect because code higher in the callstack is still able to retry with a |
40 | | - // different session towards a different router |
41 | | - return null; |
| 47 | + |
| 48 | + return session.run(statement).then(result => { |
| 49 | + session.close(); |
| 50 | + return result.records; |
| 51 | + }).catch(error => { |
| 52 | + if (error.code === PROCEDURE_NOT_FOUND_CODE) { |
| 53 | + // throw when getServers procedure not found because this is clearly a configuration issue |
| 54 | + throw newError('Server ' + routerAddress + ' could not perform routing. ' + |
| 55 | + 'Make sure you are connecting to a causal cluster', SERVICE_UNAVAILABLE); |
| 56 | + } |
| 57 | + // return nothing when failed to connect because code higher in the callstack is still able to retry with a |
| 58 | + // different session towards a different router |
| 59 | + return null; |
| 60 | + }); |
42 | 61 | }); |
43 | 62 | } |
44 | 63 |
|
|
0 commit comments