|
| 1 | +/** |
| 2 | + * Copyright (c) 2002-2017 "Neo Technology,"," |
| 3 | + * Network Engine for Objects in Lund AB [http://neotechnology.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import RoundRobinArray from "./round-robin-array"; |
| 21 | +import {newError, PROTOCOL_ERROR, SERVICE_UNAVAILABLE} from "../error"; |
| 22 | +import Integer, {int} from "../integer"; |
| 23 | + |
| 24 | +const PROCEDURE_CALL = 'CALL dbms.cluster.routing.getServers'; |
| 25 | +const PROCEDURE_NOT_FOUND_CODE = 'Neo.ClientError.Procedure.ProcedureNotFound'; |
| 26 | + |
| 27 | +export default class GetServersUtil { |
| 28 | + |
| 29 | + callGetServers(session, routerAddress) { |
| 30 | + return session.run(PROCEDURE_CALL).then(result => { |
| 31 | + session.close(); |
| 32 | + return result.records; |
| 33 | + }).catch(error => { |
| 34 | + if (this._isProcedureNotFoundError(error)) { |
| 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 | + } |
| 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; |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + parseTtl(record, routerAddress) { |
| 46 | + try { |
| 47 | + const now = int(Date.now()); |
| 48 | + const expires = record.get('ttl').multiply(1000).add(now); |
| 49 | + // if the server uses a really big expire time like Long.MAX_VALUE this may have overflowed |
| 50 | + if (expires.lessThan(now)) { |
| 51 | + return Integer.MAX_VALUE; |
| 52 | + } |
| 53 | + return expires; |
| 54 | + } catch (error) { |
| 55 | + throw newError( |
| 56 | + 'Unable to parse TTL entry from router ' + routerAddress + ' from record:\n' + JSON.stringify(record), |
| 57 | + PROTOCOL_ERROR); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + parseServers(record, routerAddress) { |
| 62 | + try { |
| 63 | + const servers = record.get('servers'); |
| 64 | + |
| 65 | + const routers = new RoundRobinArray(); |
| 66 | + const readers = new RoundRobinArray(); |
| 67 | + const writers = new RoundRobinArray(); |
| 68 | + |
| 69 | + servers.forEach(server => { |
| 70 | + const role = server['role']; |
| 71 | + const addresses = server['addresses']; |
| 72 | + |
| 73 | + if (role === 'ROUTE') { |
| 74 | + routers.pushAll(addresses); |
| 75 | + } else if (role === 'WRITE') { |
| 76 | + writers.pushAll(addresses); |
| 77 | + } else if (role === 'READ') { |
| 78 | + readers.pushAll(addresses); |
| 79 | + } else { |
| 80 | + throw newError('Unknown server role "' + role + '"', PROTOCOL_ERROR); |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + return { |
| 85 | + routers: routers, |
| 86 | + readers: readers, |
| 87 | + writers: writers |
| 88 | + } |
| 89 | + } catch (ignore) { |
| 90 | + throw newError( |
| 91 | + 'Unable to parse servers entry from router ' + routerAddress + ' from record:\n' + JSON.stringify(record), |
| 92 | + PROTOCOL_ERROR); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + _isProcedureNotFoundError(error) { |
| 97 | + let errorCode = error.code; |
| 98 | + if (!errorCode) { |
| 99 | + try { |
| 100 | + errorCode = error.fields[0].code; |
| 101 | + } catch (e) { |
| 102 | + errorCode = 'UNKNOWN'; |
| 103 | + } |
| 104 | + } |
| 105 | + return errorCode === PROCEDURE_NOT_FOUND_CODE; |
| 106 | + } |
| 107 | +} |
0 commit comments