|
17 | 17 | packageio.asyncer.r2dbc.mysql; |
18 | 18 |
|
19 | 19 | importio.asyncer.r2dbc.mysql.client.Client; |
| 20 | +importio.asyncer.r2dbc.mysql.client.FailoverClient; |
| 21 | +importio.asyncer.r2dbc.mysql.client.ReactorNettyClient; |
20 | 22 | importio.asyncer.r2dbc.mysql.constant.ProtocolDriver; |
21 | 23 | importio.asyncer.r2dbc.mysql.internal.NodeAddress; |
22 | 24 | importio.asyncer.r2dbc.mysql.internal.util.InternalArrays; |
23 | | -importio.netty.channel.ChannelOption; |
24 | 25 | importio.netty.resolver.DefaultNameResolver; |
25 | 26 | importio.netty.resolver.NameResolver; |
26 | 27 | importio.netty.util.concurrent.Future; |
27 | | -importio.r2dbc.spi.R2dbcNonTransientResourceException; |
28 | | -importorg.jetbrains.annotations.Nullable; |
29 | 28 | importreactor.core.publisher.Flux; |
30 | 29 | importreactor.core.publisher.Mono; |
31 | 30 | importreactor.netty.resources.LoopResources; |
32 | | -importreactor.netty.tcp.TcpClient; |
33 | 31 | importreactor.netty.tcp.TcpResources; |
34 | 32 |
|
35 | 33 | importjava.net.InetAddress; |
|
46 | 44 | */ |
47 | 45 | finalclassMultiHostsConnectionStrategyimplementsConnectionStrategy { |
48 | 46 |
|
49 | | -privatefinalMono<Client>client; |
| 47 | +privatefinalMono<?extendsClient>client; |
50 | 48 |
|
51 | 49 | MultiHostsConnectionStrategy( |
52 | | -TcpSocketConfigurationtcp, |
53 | 50 | MySqlConnectionConfigurationconfiguration, |
54 | | -booleanshuffle |
| 51 | +List<NodeAddress>addresses, |
| 52 | +ProtocolDriverdriver, |
| 53 | +intretriesAllDown, |
| 54 | +booleanshuffle, |
| 55 | +booleantcpKeepAlive, |
| 56 | +booleantcpNoDelay |
55 | 57 | ) { |
56 | | -this.client =Mono.defer(() -> { |
57 | | -if (ProtocolDriver.DNS_SRV.equals(tcp.getDriver())) { |
| 58 | +Mono<ReactorNettyClient>client =configuration.getCredential().flatMap(credential -> { |
| 59 | +if (ProtocolDriver.DNS_SRV.equals(driver)) { |
| 60 | +logger.debug("Resolve hosts via DNS SRV: {}",addresses); |
| 61 | + |
58 | 62 | LoopResourcesresources =configuration.getClient().getLoopResources(); |
59 | 63 | LoopResourcesloopResources =resources ==null ?TcpResources.get() :resources; |
60 | | - |
61 | | -returnresolveAllHosts(loopResources,tcp.getAddresses(),shuffle) |
62 | | - .flatMap(addresses ->connectHost(addresses,tcp,configuration,false,shuffle,0)); |
| 64 | +InetConnectFunctionlogin =newInetConnectFunction( |
| 65 | +false, |
| 66 | +tcpKeepAlive, |
| 67 | +tcpNoDelay, |
| 68 | +credential, |
| 69 | +configuration |
| 70 | + ); |
| 71 | + |
| 72 | +returnresolveAllHosts(loopResources,addresses,shuffle).flatMap(addrs -> { |
| 73 | +logger.debug("Connect to multiple addresses: {}",addrs); |
| 74 | + |
| 75 | +returnconnectHost( |
| 76 | +addrs, |
| 77 | +login, |
| 78 | +shuffle, |
| 79 | +0, |
| 80 | +retriesAllDown |
| 81 | + ); |
| 82 | + }); |
63 | 83 | }else { |
64 | | -List<NodeAddress>availableHosts =copyAvailableAddresses(tcp.getAddresses(),shuffle); |
| 84 | +List<NodeAddress>availableHosts =copyAvailableAddresses(addresses,shuffle); |
| 85 | +logger.debug("Connect to multiple hosts: {}",availableHosts); |
| 86 | + |
65 | 87 | intsize =availableHosts.size(); |
66 | | -InetSocketAddress[]addresses =newInetSocketAddress[availableHosts.size()]; |
| 88 | +InetSocketAddress[]array =newInetSocketAddress[availableHosts.size()]; |
67 | 89 |
|
68 | 90 | for (inti =0;i <size;i++) { |
69 | | -NodeAddressaddress =availableHosts.get(i); |
70 | | -addresses[i] =InetSocketAddress.createUnresolved(address.getHost(),address.getPort()); |
| 91 | +array[i] =availableHosts.get(i).toUnresolved(); |
71 | 92 | } |
72 | 93 |
|
73 | | -returnconnectHost(InternalArrays.asImmutableList(addresses),tcp,configuration,true,shuffle,0); |
| 94 | +List<InetSocketAddress>addrs =InternalArrays.asImmutableList(array); |
| 95 | +InetConnectFunctionlogin =newInetConnectFunction( |
| 96 | +true, |
| 97 | +tcpKeepAlive, |
| 98 | +tcpNoDelay, |
| 99 | +credential, |
| 100 | +configuration |
| 101 | + ); |
| 102 | + |
| 103 | +returnconnectHost( |
| 104 | +addrs, |
| 105 | +login, |
| 106 | +shuffle, |
| 107 | +0, |
| 108 | +retriesAllDown |
| 109 | + ); |
74 | 110 | } |
75 | 111 | }); |
| 112 | + |
| 113 | +this.client =client.map(c ->newFailoverClient(c,client)); |
76 | 114 | } |
77 | 115 |
|
78 | 116 | @Override |
79 | | -publicMono<Client>connect() { |
| 117 | +publicMono<?extendsClient>connect() { |
80 | 118 | returnclient; |
81 | 119 | } |
82 | 120 |
|
83 | | -privateMono<Client>connectHost( |
| 121 | +privatestaticMono<ReactorNettyClient>connectHost( |
84 | 122 | List<InetSocketAddress>addresses, |
85 | | -TcpSocketConfigurationtcp, |
86 | | -MySqlConnectionConfigurationconfiguration, |
87 | | -booleanbalancedDns, |
| 123 | +InetConnectFunctionlogin, |
88 | 124 | booleanshuffle, |
89 | | -intattempts |
| 125 | +intattempts, |
| 126 | +intmaxAttempts |
90 | 127 | ) { |
91 | 128 | Iterator<InetSocketAddress>iter =addresses.iterator(); |
92 | 129 |
|
93 | 130 | if (!iter.hasNext()) { |
94 | | -returnMono.error(fail("Fail to establish connection: no available host",null)); |
| 131 | +returnMono.error(ConnectionStrategy.retryFail("Fail to establish connection: no available host",null)); |
95 | 132 | } |
96 | 133 |
|
97 | | -returnattemptConnect(iter.next(),tcp,configuration,balancedDns).onErrorResume(t -> |
98 | | -resumeConnect(t,addresses,iter,tcp,configuration,balancedDns,shuffle,attempts)); |
| 134 | +InetSocketAddressaddress =iter.next(); |
| 135 | + |
| 136 | +returnlogin.apply(() ->address).onErrorResume(error ->resumeConnect( |
| 137 | +error, |
| 138 | +address, |
| 139 | +addresses, |
| 140 | +iter, |
| 141 | +login, |
| 142 | +shuffle, |
| 143 | +attempts, |
| 144 | +maxAttempts |
| 145 | + )); |
99 | 146 | } |
100 | 147 |
|
101 | | -privateMono<Client>resumeConnect( |
| 148 | +privatestaticMono<ReactorNettyClient>resumeConnect( |
102 | 149 | Throwablet, |
| 150 | +InetSocketAddressfailed, |
103 | 151 | List<InetSocketAddress>addresses, |
104 | 152 | Iterator<InetSocketAddress>iter, |
105 | | -TcpSocketConfigurationtcp, |
106 | | -MySqlConnectionConfigurationconfiguration, |
107 | | -booleanbalancedDns, |
| 153 | +InetConnectFunctionlogin, |
108 | 154 | booleanshuffle, |
109 | | -intattempts |
| 155 | +intattempts, |
| 156 | +intmaxAttempts |
110 | 157 | ) { |
| 158 | +logger.warn("Fail to connect to {}",failed,t); |
| 159 | + |
111 | 160 | if (!iter.hasNext()) { |
112 | 161 | // The last host failed to connect |
113 | | -if (attempts >=tcp.getRetriesAllDown()) { |
114 | | -returnMono.error(fail( |
115 | | -"Fail to establishconnection, retried " +attempts +" times: " +t.getMessage(),t)); |
| 162 | +if (attempts >=maxAttempts) { |
| 163 | +returnMono.error(ConnectionStrategy.retryFail( |
| 164 | +"Fail to establishconnections, retried " +attempts +" times",t)); |
116 | 165 | } |
117 | 166 |
|
118 | | -logger.warn("All hosts failed to establish connections, auto-try again after 250ms."); |
| 167 | +logger.warn("All hosts failed to establish connections, auto-try again after 250ms.",t); |
119 | 168 |
|
120 | 169 | // Ignore waiting error, e.g. interrupted, scheduler rejected |
121 | 170 | returnMono.delay(Duration.ofMillis(250)) |
122 | 171 | .onErrorComplete() |
123 | | - .then(Mono.defer(() ->connectHost(addresses,tcp,configuration,balancedDns,shuffle,attempts +1))); |
| 172 | + .then(Mono.defer(() ->connectHost( |
| 173 | +addresses, |
| 174 | +login, |
| 175 | +shuffle, |
| 176 | +attempts +1, |
| 177 | +maxAttempts |
| 178 | + ))); |
124 | 179 | } |
125 | 180 |
|
126 | | -returnattemptConnect(iter.next(),tcp,configuration,balancedDns).onErrorResume(tt -> |
127 | | -resumeConnect(tt,addresses,iter,tcp,configuration,balancedDns,shuffle,attempts)); |
128 | | - } |
129 | | - |
130 | | -privateMono<Client>attemptConnect( |
131 | | -InetSocketAddressaddress, |
132 | | -TcpSocketConfigurationtcp, |
133 | | -MySqlConnectionConfigurationconfiguration, |
134 | | -booleanbalancedDns |
135 | | - ) { |
136 | | -returnconfiguration.getCredential().flatMap(credential -> { |
137 | | -TcpClienttcpClient =ConnectionStrategy.createTcpClient(configuration.getClient(),balancedDns) |
138 | | - .option(ChannelOption.SO_KEEPALIVE,tcp.isTcpKeepAlive()) |
139 | | - .option(ChannelOption.TCP_NODELAY,tcp.isTcpNoDelay()) |
140 | | - .remoteAddress(() ->address); |
141 | | - |
142 | | -returnConnectionStrategy.login(tcpClient,credential,configuration); |
143 | | - }).doOnError(e ->logger.warn("Fail to connect: ",e)); |
| 181 | +InetSocketAddressaddress =iter.next(); |
| 182 | + |
| 183 | +returnlogin.apply(() ->address).onErrorResume(error ->resumeConnect( |
| 184 | +error, |
| 185 | +address, |
| 186 | +addresses, |
| 187 | +iter, |
| 188 | +login, |
| 189 | +shuffle, |
| 190 | +attempts, |
| 191 | +maxAttempts |
| 192 | + )); |
144 | 193 | } |
145 | 194 |
|
146 | 195 | privatestaticMono<List<InetSocketAddress>>resolveAllHosts( |
@@ -199,13 +248,4 @@ private static List<NodeAddress> copyAvailableAddresses(List<NodeAddress> addres |
199 | 248 |
|
200 | 249 | returnInternalArrays.asImmutableList(addresses.toArray(newNodeAddress[0])); |
201 | 250 | } |
202 | | - |
203 | | -privatestaticR2dbcNonTransientResourceExceptionfail(Stringmessage,@NullableThrowablecause) { |
204 | | -returnnewR2dbcNonTransientResourceException( |
205 | | -message, |
206 | | -"H1000", |
207 | | -9000, |
208 | | -cause |
209 | | - ); |
210 | | - } |
211 | 251 | } |