-
Notifications
You must be signed in to change notification settings - Fork 477
/
DefaultBootstrapManager.java
230 lines (209 loc) · 9.72 KB
/
DefaultBootstrapManager.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
/**
* 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.bootstrap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.kaaproject.kaa.client.channel.BootstrapTransport;
import org.kaaproject.kaa.client.channel.failover.FailoverDecision;
import org.kaaproject.kaa.client.channel.failover.FailoverManager;
import org.kaaproject.kaa.client.channel.failover.FailoverStatus;
import org.kaaproject.kaa.client.channel.GenericTransportInfo;
import org.kaaproject.kaa.client.channel.KaaInternalChannelManager;
import org.kaaproject.kaa.client.channel.ServerType;
import org.kaaproject.kaa.client.channel.TransportConnectionInfo;
import org.kaaproject.kaa.client.channel.TransportProtocolId;
import org.kaaproject.kaa.client.context.ExecutorContext;
import org.kaaproject.kaa.client.transport.TransportException;
import org.kaaproject.kaa.common.TransportType;
import org.kaaproject.kaa.common.endpoint.gen.ProtocolMetaData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Default {@link BootstrapManager} implementation
*
* @author Yaroslav Zeygerman
*
*/
public class DefaultBootstrapManager implements BootstrapManager {
/** The Constant LOG. */
private static final Logger LOG = LoggerFactory.getLogger(DefaultBootstrapManager.class);
private static final int EXIT_FAILURE = 1;
private BootstrapTransport transport;
private List<ProtocolMetaData> operationsServerList;
private KaaInternalChannelManager channelManager;
private FailoverManager failoverManager;
private Integer serverToApply;
private ExecutorContext executorContext;
private final Map<TransportProtocolId, List<ProtocolMetaData>> mappedOperationServerList = new HashMap<>();
private final Map<TransportProtocolId, Iterator<ProtocolMetaData>> mappedIterators = new HashMap<>();
public DefaultBootstrapManager(BootstrapTransport transport, ExecutorContext executorContext) {
this.transport = transport;
this.executorContext = executorContext;
}
@Override
public void receiveOperationsServerList() throws TransportException {
LOG.debug("Going to invoke sync method of assigned transport");
transport.sync();
}
@Override
public void useNextOperationsServer(TransportProtocolId transportId, FailoverStatus status) {
if (mappedOperationServerList != null && !mappedOperationServerList.isEmpty()) {
if (mappedIterators.get(transportId).hasNext()) {
ProtocolMetaData nextOperationsServer = mappedIterators.get(transportId).next();
LOG.debug("New server [{}] will be user for [{}]",
nextOperationsServer.getAccessPointId(), transportId);
if (channelManager != null) {
channelManager.onTransportConnectionInfoUpdated(
new GenericTransportInfo(ServerType.OPERATIONS, nextOperationsServer));
} else {
LOG.error("Can not process server change. Channel manager was not specified");
}
} else {
LOG.warn("Failed to find server for channel [{}]", transportId);
resolveFailoverStatus(status);
}
} else {
throw new BootstrapRuntimeException("Operations Server list is empty");
}
}
@Override
public synchronized void setTransport(BootstrapTransport transport) {
this.transport = transport;
}
@Override
public synchronized void useNextOperationsServerByAccessPointId(int accessPointId) {
List<ProtocolMetaData> servers = getTransportsByAccessPointId(accessPointId);
if (servers != null && !servers.isEmpty()) {
notifyChannelManagerAboutServer(servers);
} else {
serverToApply = accessPointId;
transport.sync();
}
}
private void notifyChannelManagerAboutServer(List<ProtocolMetaData> transports) {
for (ProtocolMetaData transport : transports) {
LOG.debug("Applying new transport {}", transports);
channelManager.onTransportConnectionInfoUpdated(new GenericTransportInfo(ServerType.OPERATIONS, transport));
}
}
private List<ProtocolMetaData> getTransportsByAccessPointId(int accessPointId) {
if (operationsServerList == null || operationsServerList.isEmpty()) {
throw new BootstrapRuntimeException("Operations Server list is empty");
}
List<ProtocolMetaData> result = new ArrayList<>();
for (ProtocolMetaData transport : operationsServerList) {
if (transport.getAccessPointId().intValue() == accessPointId) {
result.add(transport);
}
}
return result;
}
@Override
public synchronized void setChannelManager(KaaInternalChannelManager manager) {
this.channelManager = manager;
}
@Override
public synchronized void setFailoverManager(FailoverManager failoverManager) {
this.failoverManager = failoverManager;
}
@Override
public synchronized void onProtocolListUpdated(List<ProtocolMetaData> list) {
LOG.trace("Protocol list was updated");
operationsServerList = list;
mappedOperationServerList.clear();
mappedIterators.clear();
if (operationsServerList == null || operationsServerList.isEmpty()) {
LOG.trace("Received empty operations server list");
resolveFailoverStatus(FailoverStatus.NO_OPERATION_SERVERS_RECEIVED);
return;
}
for (ProtocolMetaData server : operationsServerList) {
TransportProtocolId transportId = new TransportProtocolId(server.getProtocolVersionInfo().getId(), server.getProtocolVersionInfo().getVersion());
List<ProtocolMetaData> servers = mappedOperationServerList.get(transportId);
if (servers == null) {
servers = new LinkedList<>();
mappedOperationServerList.put(transportId, servers);
}
servers.add(server);
}
for (Map.Entry<TransportProtocolId, List<ProtocolMetaData>> entry : mappedOperationServerList.entrySet()) {
Collections.shuffle(entry.getValue());
mappedIterators.put(entry.getKey(), entry.getValue().iterator());
}
if (serverToApply != null) {
List<ProtocolMetaData> servers = getTransportsByAccessPointId(serverToApply);
if (servers != null && !servers.isEmpty()) {
notifyChannelManagerAboutServer(servers);
serverToApply = null;
}
} else {
for (Map.Entry<TransportProtocolId, Iterator<ProtocolMetaData>> entry : mappedIterators.entrySet()) {
TransportConnectionInfo info = new GenericTransportInfo(ServerType.OPERATIONS, entry.getValue().next());
channelManager.onTransportConnectionInfoUpdated(info);
}
}
}
private void resolveFailoverStatus(FailoverStatus status) {
FailoverDecision decision = failoverManager.onFailover(status);
switch (decision.getAction()) {
case NOOP:
LOG.warn("No operation is performed according to failover strategy decision");
break;
case RETRY:
long retryPeriod = decision.getRetryPeriod();
LOG.warn("Attempt to receive operations server list will be made in {} ms, " +
"according to failover strategy decision", retryPeriod);
executorContext.getScheduledExecutor().schedule(new Runnable() {
@Override
public void run() {
try {
receiveOperationsServerList();
} catch (TransportException e) {
LOG.error("Error while receiving operations server list", e);
}
}
}, retryPeriod, TimeUnit.MILLISECONDS);
break;
case USE_NEXT_BOOTSTRAP:
LOG.warn("Trying to switch to the next bootstrap server according to failover strategy decision");
retryPeriod = decision.getRetryPeriod();
failoverManager.onServerFailed(channelManager.getActiveServer(TransportType.BOOTSTRAP), status);
executorContext.getScheduledExecutor().schedule(new Runnable() {
@Override
public void run() {
try {
receiveOperationsServerList();
} catch (TransportException e) {
LOG.error("Error while receiving operations server list", e);
}
}
}, retryPeriod, TimeUnit.MILLISECONDS);
break;
case STOP_APP:
LOG.warn("Stopping application according to failover strategy decision!");
System.exit(EXIT_FAILURE); //NOSONAR
break;
default:
break;
}
}
}