|
16 | 16 | * See the License for the specific language governing permissions and
|
17 | 17 | * limitations under the License.
|
18 | 18 | */
|
19 |
| - |
20 | 19 | import RoutingTable from './routing-table'
|
21 |
| -import RoutingUtil from './routing-util' |
22 |
| -import { newError, PROTOCOL_ERROR } from '../error' |
| 20 | +import Session from '../session' |
| 21 | +import { RoutingTableGetterFactory } from './routing-table-getter' |
| 22 | +import ServerAddress from './server-address' |
23 | 23 |
|
24 | 24 | export default class Rediscovery {
|
25 | 25 | /**
|
26 | 26 | * @constructor
|
27 |
| - * @param {RoutingUtil} routingUtil the util to use. |
| 27 | + * @param {RoutingTableGetterFactory} routingTableGetterFactory the util to use. |
28 | 28 | */
|
29 |
| - constructor (routingUtil) { |
30 |
| - this._routingUtil = routingUtil |
| 29 | + constructor (routingTableGetterFactory) { |
| 30 | + this._routingTableGetterFactory = routingTableGetterFactory |
31 | 31 | }
|
32 | 32 |
|
33 | 33 | /**
|
34 | 34 | * Try to fetch new routing table from the given router.
|
35 | 35 | * @param {Session} session the session to use.
|
36 | 36 | * @param {string} database the database for which to lookup routing table.
|
37 |
| - * @param {string} routerAddress the URL of the router. |
| 37 | + * @param {ServerAddress} routerAddress the URL of the router. |
38 | 38 | * @return {Promise<RoutingTable>} promise resolved with new routing table or null when connection error happened.
|
39 | 39 | */
|
40 |
| - async lookupRoutingTableOnRouter (session, database, routerAddress) { |
41 |
| - const records = await this._routingUtil.callRoutingProcedure( |
42 |
| - session, |
43 |
| - database, |
44 |
| - routerAddress |
45 |
| - ) |
46 |
| - if (records === null) { |
47 |
| - // connection error happened, unable to retrieve routing table from this router, next one should be queried |
48 |
| - return null |
49 |
| - } |
50 |
| - |
51 |
| - if (records.length !== 1) { |
52 |
| - throw newError( |
53 |
| - 'Illegal response from router "' + |
54 |
| - routerAddress + |
55 |
| - '". ' + |
56 |
| - 'Received ' + |
57 |
| - records.length + |
58 |
| - ' records but expected only one.\n' + |
59 |
| - JSON.stringify(records), |
60 |
| - PROTOCOL_ERROR |
| 40 | + lookupRoutingTableOnRouter (session, database, routerAddress) { |
| 41 | + return session._acquireConnection(connection => { |
| 42 | + const routingTableGetter = this._routingTableGetterFactory.create( |
| 43 | + connection |
61 | 44 | )
|
62 |
| - } |
63 |
| - |
64 |
| - const record = records[0] |
65 |
| - |
66 |
| - const expirationTime = this._routingUtil.parseTtl(record, routerAddress) |
67 |
| - const { routers, readers, writers } = this._routingUtil.parseServers( |
68 |
| - record, |
69 |
| - routerAddress |
70 |
| - ) |
71 |
| - |
72 |
| - Rediscovery._assertNonEmpty(routers, 'routers', routerAddress) |
73 |
| - Rediscovery._assertNonEmpty(readers, 'readers', routerAddress) |
74 |
| - // case with no writers is processed higher in the promise chain because only RoutingDriver knows |
75 |
| - // how to deal with such table and how to treat router that returned such table |
76 |
| - |
77 |
| - return new RoutingTable({ |
78 |
| - database, |
79 |
| - routers, |
80 |
| - readers, |
81 |
| - writers, |
82 |
| - expirationTime |
83 |
| - }) |
84 |
| - } |
85 |
| - |
86 |
| - static _assertNonEmpty (serverAddressesArray, serversName, routerAddress) { |
87 |
| - if (serverAddressesArray.length === 0) { |
88 |
| - throw newError( |
89 |
| - 'Received no ' + serversName + ' from router ' + routerAddress, |
90 |
| - PROTOCOL_ERROR |
| 45 | + return routingTableGetter.get( |
| 46 | + connection, |
| 47 | + database, |
| 48 | + routerAddress, |
| 49 | + session |
91 | 50 | )
|
92 |
| - } |
| 51 | + }) |
93 | 52 | }
|
94 | 53 | }
|
0 commit comments