-
Notifications
You must be signed in to change notification settings - Fork 477
/
DefaultFailoverManager.java
280 lines (236 loc) · 12.1 KB
/
DefaultFailoverManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/**
* Copyright 2014-2016 CyberVision, Inc.
*
* 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.
*/
package org.kaaproject.kaa.client.channel.failover;
import org.kaaproject.kaa.client.channel.KaaChannelManager;
import org.kaaproject.kaa.client.channel.ServerType;
import org.kaaproject.kaa.client.channel.TransportConnectionInfo;
import org.kaaproject.kaa.client.channel.failover.FailoverDecision.FailoverAction;
import org.kaaproject.kaa.client.channel.failover.strategies.DefaultFailoverStrategy;
import org.kaaproject.kaa.client.channel.failover.strategies.FailoverStrategy;
import org.kaaproject.kaa.client.context.ExecutorContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
public class DefaultFailoverManager implements FailoverManager {
private static final Logger LOG = LoggerFactory.getLogger(DefaultFailoverManager.class);
private static final long DEFAULT_FAILURE_RESOLUTION_TIMEOUT = 10;
private static final TimeUnit DEFAULT_TIME_UNIT = TimeUnit.SECONDS;
private long failureResolutionTimeout;
private TimeUnit timeUnit = DEFAULT_TIME_UNIT;
private FailoverStrategy failoverStrategy;
private final KaaChannelManager channelManager;
private final ExecutorContext context;
private Map<ServerType, AccessPointIdResolution> resolutionProgressMap = new HashMap<>();
public DefaultFailoverManager(KaaChannelManager channelManager, ExecutorContext context) {
this(channelManager, context, new DefaultFailoverStrategy(), DEFAULT_FAILURE_RESOLUTION_TIMEOUT, DEFAULT_TIME_UNIT);
}
public DefaultFailoverManager(KaaChannelManager channelManager,
ExecutorContext context,
FailoverStrategy failoverStrategy,
long failureResolutionTimeout,
TimeUnit timeUnit) {
this.channelManager = channelManager;
this.context = context;
this.failoverStrategy = failoverStrategy;
this.failureResolutionTimeout = failureResolutionTimeout;
this.timeUnit = timeUnit;
}
@Override
public synchronized void onServerFailed(final TransportConnectionInfo connectionInfo, FailoverStatus status) {
if (connectionInfo == null) {
LOG.warn("Server failed, but connection info is null, can't resolve");
return;
} else {
LOG.info("Server [{}, {}] failed", connectionInfo.getServerType(), connectionInfo.getAccessPointId());
}
long currentResolutionTime = -1;
AccessPointIdResolution currentAccessPointIdResolution = resolutionProgressMap.get(connectionInfo.getServerType());
if (currentAccessPointIdResolution != null) {
currentResolutionTime = currentAccessPointIdResolution.getResolutionTime();
if (currentAccessPointIdResolution.getAccessPointId() == connectionInfo.getAccessPointId()
&& currentAccessPointIdResolution.getCurResolution() != null
&& System.currentTimeMillis() < currentResolutionTime) {
LOG.debug("Resolution is in progress for {} server", connectionInfo);
return;
} else {
if (currentAccessPointIdResolution.getCurResolution() != null) {
LOG.trace("Cancelling old resolution: {}", currentAccessPointIdResolution.getCurResolution());
cancelCurrentFailResolution(currentAccessPointIdResolution);
}
}
}
LOG.trace("Next fail resolution will be available in {} {}", failureResolutionTimeout, timeUnit.toString());
Future<?> currentResolution = context.getScheduledExecutor().schedule(new Runnable() {
@Override
public void run() {
LOG.debug("Removing server {} from resolution map for type: {}", connectionInfo, connectionInfo.getServerType());
resolutionProgressMap.remove(connectionInfo.getServerType());
}
}, failureResolutionTimeout, timeUnit);
channelManager.onServerFailed(connectionInfo, status);
long updatedResolutionTime = currentAccessPointIdResolution != null ? currentAccessPointIdResolution.getResolutionTime() : currentResolutionTime;
AccessPointIdResolution newAccessPointIdResolution =
new AccessPointIdResolution(connectionInfo.getAccessPointId(), currentResolution);
if (updatedResolutionTime != currentResolutionTime) {
newAccessPointIdResolution.setResolutionTime(updatedResolutionTime);
}
resolutionProgressMap.put(connectionInfo.getServerType(), newAccessPointIdResolution);
}
@Override
public synchronized void onServerChanged(TransportConnectionInfo connectionInfo) {
if (connectionInfo == null) {
LOG.warn("Server has changed, but its connection info is null, can't resolve");
return;
} else {
LOG.trace("Server [{}, {}] has changed", connectionInfo.getServerType(), connectionInfo.getAccessPointId());
}
AccessPointIdResolution currentAccessPointIdResolution = resolutionProgressMap.get(connectionInfo.getServerType());
if (currentAccessPointIdResolution == null) {
AccessPointIdResolution newResolution = new AccessPointIdResolution(connectionInfo.getAccessPointId(), null);
resolutionProgressMap.put(connectionInfo.getServerType(), newResolution);
} else if (currentAccessPointIdResolution.getAccessPointId() != connectionInfo.getAccessPointId()) {
if (currentAccessPointIdResolution.getCurResolution() != null) {
LOG.trace("Cancelling fail resolution, as server [{}] has changed: {}", connectionInfo, currentAccessPointIdResolution);
cancelCurrentFailResolution(currentAccessPointIdResolution);
}
AccessPointIdResolution newResolution = new AccessPointIdResolution(connectionInfo.getAccessPointId(), null);
resolutionProgressMap.put(connectionInfo.getServerType(), newResolution);
} else {
LOG.debug("Same server [{}] is used, nothing has changed", connectionInfo);
}
}
@Override
public synchronized void onServerConnected(TransportConnectionInfo connectionInfo) {
LOG.trace("Server {} has connected", connectionInfo);
if (connectionInfo == null) {
LOG.warn("Server connection info is null, can't resolve");
return;
}
failoverStrategy.onRecover(connectionInfo);
AccessPointIdResolution accessPointIdResolution = resolutionProgressMap.get(connectionInfo.getServerType());
if (accessPointIdResolution == null) {
LOG.trace("Server hasn't been set yet (failover resolution has happened), so a new server: {} can't be connected", connectionInfo);
} else if (accessPointIdResolution.getCurResolution() != null
&& connectionInfo.getAccessPointId() == accessPointIdResolution.getAccessPointId()) {
LOG.trace("Cancelling fail resolution: {}", accessPointIdResolution);
cancelCurrentFailResolution(accessPointIdResolution);
} else if (accessPointIdResolution.getCurResolution() != null) {
LOG.debug("Connection for outdated accessPointId: {} was received, ignoring. The new accessPointId is: {}",
connectionInfo.getAccessPointId(), accessPointIdResolution.getAccessPointId());
} else {
LOG.trace("There is no current resolution in progress, connected to the same server: {}", connectionInfo);
}
}
@Override
public void setFailoverStrategy(FailoverStrategy failoverStrategy) {
if (failoverStrategy == null) {
throw new IllegalArgumentException("Failover strategy can't be null");
}
this.failoverStrategy = failoverStrategy;
}
@Override
public synchronized FailoverDecision onFailover(FailoverStatus failoverStatus) {
AccessPointIdResolution accessPointIdResolution = null;
long resolutionTime = System.currentTimeMillis();
switch (failoverStatus) {
case ENDPOINT_VERIFICATION_FAILED:
return new FailoverDecision(FailoverAction.STOP_APP);
case BOOTSTRAP_SERVERS_NA:
case CURRENT_BOOTSTRAP_SERVER_NA:
accessPointIdResolution = resolutionProgressMap.get(ServerType.BOOTSTRAP);
resolutionTime += failoverStrategy.getTimeUnit().toMillis(failoverStrategy.getBootstrapServersRetryPeriod());
break;
case NO_OPERATION_SERVERS_RECEIVED:
accessPointIdResolution = resolutionProgressMap.get(ServerType.BOOTSTRAP);
break;
case OPERATION_SERVERS_NA:
accessPointIdResolution = resolutionProgressMap.get(ServerType.OPERATIONS);
resolutionTime += failoverStrategy.getTimeUnit().toMillis(failoverStrategy.getOperationServersRetryPeriod());
break;
}
if (accessPointIdResolution != null) {
accessPointIdResolution.setResolutionTime(resolutionTime);
}
return failoverStrategy.onFailover(failoverStatus);
}
private void cancelCurrentFailResolution(AccessPointIdResolution accessPointIdResolution) {
if (accessPointIdResolution.getCurResolution() != null) {
accessPointIdResolution.getCurResolution().cancel(true);
accessPointIdResolution.setCurResolution(null);
} else {
LOG.trace("Current resolution is null, can't cancel");
}
}
static class AccessPointIdResolution {
private int accessPointId;
private long resolutionTimeMillis;
private Future<?> curResolution;
public AccessPointIdResolution(int accessPointId, Future<?> curResolution) {
this.accessPointId = accessPointId;
this.curResolution = curResolution;
this.resolutionTimeMillis = Long.MAX_VALUE;
}
public int getAccessPointId() {
return accessPointId;
}
public Future<?> getCurResolution() {
return curResolution;
}
public void setCurResolution(Future<?> curResolution) {
this.curResolution = curResolution;
}
public long getResolutionTime() {
return resolutionTimeMillis;
}
public void setResolutionTime(long resolutionTimeMillis) {
this.resolutionTimeMillis = resolutionTimeMillis;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AccessPointIdResolution that = (AccessPointIdResolution) o;
if (accessPointId != that.accessPointId) {
return false;
}
if (curResolution != null ? !curResolution.equals(that.curResolution) : that.curResolution != null) {
return false;
}
return true;
}
@Override
public int hashCode() {
int result = accessPointId;
result = 31 * result + (curResolution != null ? curResolution.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "AccessPointIdResolution{" +
"accessPointId=" + accessPointId +
", resolutionTime=" + resolutionTimeMillis +
", curResolution=" + curResolution +
'}';
}
}
}