forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgentAttache.java
More file actions
executable file
·524 lines (450 loc) · 18.3 KB
/
AgentAttache.java
File metadata and controls
executable file
·524 lines (450 loc) · 18.3 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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 com.cloud.agent.manager;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
import com.cloud.agent.AgentManager;
import com.cloud.agent.Listener;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.CheckHealthCommand;
import com.cloud.agent.api.CheckNetworkCommand;
import com.cloud.agent.api.CheckVirtualMachineCommand;
import com.cloud.agent.api.CleanupNetworkRulesCmd;
import com.cloud.agent.api.ClusterSyncCommand;
import com.cloud.agent.api.Command;
import com.cloud.agent.api.MaintainCommand;
import com.cloud.agent.api.MigrateCommand;
import com.cloud.agent.api.PingTestCommand;
import com.cloud.agent.api.ReadyCommand;
import com.cloud.agent.api.SetupCommand;
import com.cloud.agent.api.ShutdownCommand;
import com.cloud.agent.api.StartCommand;
import com.cloud.agent.api.StopCommand;
import com.cloud.agent.api.storage.CreateCommand;
import com.cloud.agent.transport.Request;
import com.cloud.agent.transport.Response;
import com.cloud.exception.AgentUnavailableException;
import com.cloud.exception.OperationTimedoutException;
import com.cloud.host.Status;
import com.cloud.utils.concurrency.NamedThreadFactory;
/**
* AgentAttache provides basic commands to be implemented.
*/
public abstract class AgentAttache {
private static final Logger s_logger = Logger.getLogger(AgentAttache.class);
private static final ScheduledExecutorService s_listenerExecutor = Executors.newScheduledThreadPool(10, new NamedThreadFactory("ListenerTimer"));
private static final Random s_rand = new Random(System.currentTimeMillis());
protected static final Comparator<Request> s_reqComparator =
new Comparator<Request>() {
@Override
public int compare(Request o1, Request o2) {
long seq1 = o1.getSequence();
long seq2 = o2.getSequence();
if (seq1 < seq2) {
return -1;
} else if (seq1 > seq2) {
return 1;
} else {
return 0;
}
}
};
protected static final Comparator<Object> s_seqComparator =
new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
long seq1 = ((Request) o1).getSequence();
long seq2 = (Long) o2;
if (seq1 < seq2) {
return -1;
} else if (seq1 > seq2) {
return 1;
} else {
return 0;
}
}
};
protected final long _id;
protected final ConcurrentHashMap<Long, Listener> _waitForList;
protected final LinkedList<Request> _requests;
protected Long _currentSequence;
protected Status _status = Status.Connecting;
protected boolean _maintenance;
protected long _nextSequence;
protected AgentManagerImpl _agentMgr;
public final static String[] s_commandsAllowedInMaintenanceMode =
new String[] { MaintainCommand.class.toString(), MigrateCommand.class.toString(), StopCommand.class.toString(), CheckVirtualMachineCommand.class.toString(), PingTestCommand.class.toString(), CheckHealthCommand.class.toString(), ReadyCommand.class.toString(), ShutdownCommand.class.toString(), SetupCommand.class.toString(), ClusterSyncCommand.class.toString(), CleanupNetworkRulesCmd.class.toString(), CheckNetworkCommand.class.toString() };
protected final static String[] s_commandsNotAllowedInConnectingMode =
new String[] { StartCommand.class.toString(), CreateCommand.class.toString() };
static {
Arrays.sort(s_commandsAllowedInMaintenanceMode);
Arrays.sort(s_commandsNotAllowedInConnectingMode);
}
protected AgentAttache(AgentManagerImpl agentMgr, final long id, boolean maintenance) {
_id = id;
_waitForList = new ConcurrentHashMap<Long, Listener>();
_currentSequence = null;
_maintenance = maintenance;
_requests = new LinkedList<Request>();
_agentMgr = agentMgr;
_nextSequence = s_rand.nextInt(Short.MAX_VALUE) << 48;
}
public synchronized long getNextSequence() {
return ++_nextSequence;
}
public synchronized void setMaintenanceMode(final boolean value) {
_maintenance = value;
}
public void ready() {
_status = Status.Up;
}
public boolean isReady() {
return _status == Status.Up;
}
public boolean isConnecting() {
return _status == Status.Connecting;
}
public boolean forForward() {
return false;
}
protected void checkAvailability(final Command[] cmds) throws AgentUnavailableException {
if (!_maintenance && _status != Status.Connecting) {
return;
}
if (_maintenance) {
for (final Command cmd : cmds) {
if (Arrays.binarySearch(s_commandsAllowedInMaintenanceMode, cmd.getClass().toString()) < 0) {
throw new AgentUnavailableException("Unable to send " + cmd.getClass().toString() + " because agent is in maintenance mode", _id);
}
}
}
if (_status == Status.Connecting) {
for (final Command cmd : cmds) {
if (Arrays.binarySearch(s_commandsNotAllowedInConnectingMode, cmd.getClass().toString()) >= 0) {
throw new AgentUnavailableException("Unable to send " + cmd.getClass().toString() + " because agent is in connecting mode", _id);
}
}
}
}
protected synchronized void addRequest(Request req) {
int index = findRequest(req);
assert (index < 0) : "How can we get index again? " + index + ":" + req.toString();
_requests.add(-index - 1, req);
}
protected void cancel(Request req) {
long seq = req.getSequence();
cancel(seq);
}
protected synchronized void cancel(final long seq) {
if (s_logger.isDebugEnabled()) {
s_logger.debug(log(seq, "Cancelling."));
}
final Listener listener = _waitForList.remove(seq);
if (listener != null) {
listener.processDisconnect(_id, Status.Disconnected);
}
int index = findRequest(seq);
if (index >= 0) {
_requests.remove(index);
}
}
protected synchronized int findRequest(Request req) {
return Collections.binarySearch(_requests, req, s_reqComparator);
}
protected synchronized int findRequest(long seq) {
return Collections.binarySearch(_requests, seq, s_seqComparator);
}
protected String log(final long seq, final String msg) {
return "Seq " + _id + "-" + seq + ": " + msg;
}
protected void registerListener(final long seq, final Listener listener) {
if (s_logger.isTraceEnabled()) {
s_logger.trace(log(seq, "Registering listener"));
}
if (listener.getTimeout() != -1) {
s_listenerExecutor.schedule(new Alarm(seq), listener.getTimeout(), TimeUnit.SECONDS);
}
_waitForList.put(seq, listener);
}
protected Listener unregisterListener(final long sequence) {
if (s_logger.isTraceEnabled()) {
s_logger.trace(log(sequence, "Unregistering listener"));
}
return _waitForList.remove(sequence);
}
protected Listener getListener(final long sequence) {
return _waitForList.get(sequence);
}
public long getId() {
return _id;
}
public int getQueueSize() {
return _requests.size();
}
public int getNonRecurringListenersSize() {
List<Listener> nonRecurringListenersList = new ArrayList<Listener>();
if (_waitForList.isEmpty()) {
return 0;
} else {
final Set<Map.Entry<Long, Listener>> entries = _waitForList.entrySet();
final Iterator<Map.Entry<Long, Listener>> it = entries.iterator();
while (it.hasNext()) {
final Map.Entry<Long, Listener> entry = it.next();
final Listener monitor = entry.getValue();
if (!monitor.isRecurring()) {
//TODO - remove this debug statement later
s_logger.debug("Listener is " + entry.getValue() + " waiting on " + entry.getKey());
nonRecurringListenersList.add(monitor);
}
}
}
return nonRecurringListenersList.size();
}
public boolean processAnswers(final long seq, final Response resp) {
resp.logD("Processing: ", true);
final Answer[] answers = resp.getAnswers();
boolean processed = false;
try {
Listener monitor = getListener(seq);
if (monitor == null) {
if ( answers[0] != null && answers[0].getResult() ) {
processed = true;
}
if (s_logger.isDebugEnabled()) {
s_logger.debug(log(seq, "Unable to find listener."));
}
} else {
processed = monitor.processAnswers(_id, seq, answers);
if (s_logger.isTraceEnabled()) {
s_logger.trace(log(seq, (processed ? "" : " did not ") + " processed "));
}
if (!monitor.isRecurring()) {
unregisterListener(seq);
}
}
_agentMgr.notifyAnswersToMonitors(_id, seq, answers);
} finally {
// we should always trigger next command execution, even in failure cases - otherwise in exception case all the remaining will be stuck in the sync queue forever
if (resp.executeInSequence()) {
sendNext(seq);
}
}
return processed;
}
protected void cancelAllCommands(final Status state, final boolean cancelActive) {
final Set<Map.Entry<Long, Listener>> entries = _waitForList.entrySet();
final Iterator<Map.Entry<Long, Listener>> it = entries.iterator();
while (it.hasNext()) {
final Map.Entry<Long, Listener> entry = it.next();
it.remove();
final Listener monitor = entry.getValue();
if (s_logger.isDebugEnabled()) {
s_logger.debug(log(entry.getKey(), "Sending disconnect to " + monitor.getClass()));
}
monitor.processDisconnect(_id, state);
}
}
public void cleanup(final Status state) {
cancelAllCommands(state, true);
_requests.clear();
}
@Override
public boolean equals(Object obj) {
try {
AgentAttache that = (AgentAttache) obj;
return this._id == that._id;
} catch (ClassCastException e) {
assert false : "Who's sending an " + obj.getClass().getSimpleName() + " to AgentAttache.equals()? ";
return false;
}
}
public void send(Request req, final Listener listener) throws AgentUnavailableException {
checkAvailability(req.getCommands());
long seq = req.getSequence();
if (listener != null) {
registerListener(seq, listener);
} else if (s_logger.isDebugEnabled()) {
s_logger.debug(log(seq, "Routed from " + req.getManagementServerId()));
}
synchronized(this) {
try {
if (isClosed()) {
throw new AgentUnavailableException("The link to the agent has been closed", _id);
}
if (req.executeInSequence() && _currentSequence != null) {
req.logD("Waiting for Seq " + _currentSequence + " Scheduling: ", true);
addRequest(req);
return;
}
// If we got to here either we're not suppose to set
// the _currentSequence or it is null already.
req.logD("Sending ", true);
send(req);
if (req.executeInSequence() && _currentSequence == null) {
_currentSequence = seq;
if (s_logger.isTraceEnabled()) {
s_logger.trace(log(seq, " is current sequence"));
}
}
} catch (AgentUnavailableException e) {
s_logger.info(log(seq, "Unable to send due to " + e.getMessage()));
cancel(seq);
throw e;
} catch (Exception e) {
s_logger.warn(log(seq, "Unable to send due to "), e);
cancel(seq);
throw new AgentUnavailableException("Problem due to other exception " + e.getMessage(), _id);
}
}
}
public Answer[] send(Request req, int wait) throws AgentUnavailableException, OperationTimedoutException {
SynchronousListener sl = new SynchronousListener(null);
long seq = req.getSequence();
send(req, sl);
try {
for (int i = 0; i < 2; i++) {
Answer[] answers = null;
try {
answers = sl.waitFor(wait);
} catch (final InterruptedException e) {
s_logger.debug(log(seq, "Interrupted"));
}
if (answers != null) {
if (s_logger.isDebugEnabled()) {
new Response(req, answers).logD("Received: ", false);
}
return answers;
}
answers = sl.getAnswers(); // Try it again.
if (answers != null) {
if (s_logger.isDebugEnabled()) {
new Response(req, answers).logD("Received after timeout: ", true);
}
_agentMgr.notifyAnswersToMonitors(_id, seq, answers);
return answers;
}
final Long current = _currentSequence;
if (current != null && seq != current) {
if (s_logger.isDebugEnabled()) {
s_logger.debug(log(seq, "Waited too long."));
}
throw new OperationTimedoutException(req.getCommands(), _id, seq, wait, false);
}
if (s_logger.isDebugEnabled()) {
s_logger.debug(log(seq, "Waiting some more time because this is the current command"));
}
}
throw new OperationTimedoutException(req.getCommands(), _id, seq, wait * 2, true);
} catch (OperationTimedoutException e) {
s_logger.warn(log(seq, "Timed out on " + req.toString()));
cancel(seq);
final Long current = _currentSequence;
if (req.executeInSequence() && (current != null && current == seq)) {
sendNext(seq);
}
throw e;
} catch (Exception e) {
s_logger.warn(log(seq, "Exception while waiting for answer"), e);
cancel(seq);
final Long current = _currentSequence;
if (req.executeInSequence() && (current != null && current == seq)) {
sendNext(seq);
}
throw new OperationTimedoutException(req.getCommands(), _id, seq, wait, false);
} finally {
unregisterListener(seq);
}
}
protected synchronized void sendNext(final long seq) {
_currentSequence = null;
if (_requests.isEmpty()) {
if (s_logger.isDebugEnabled()) {
s_logger.debug(log(seq, "No more commands found"));
}
return;
}
Request req = _requests.pop();
if (s_logger.isDebugEnabled()) {
s_logger.debug(log(req.getSequence(), "Sending now. is current sequence."));
}
try {
send(req);
} catch (AgentUnavailableException e) {
if (s_logger.isDebugEnabled()) {
s_logger.debug(log(req.getSequence(), "Unable to send the next sequence"));
}
cancel(req.getSequence());
}
_currentSequence = req.getSequence();
}
public void process(Answer[] answers) {
//do nothing
}
/**
* sends the request asynchronously.
*
* @param req
* @throws AgentUnavailableException
*/
public abstract void send(Request req) throws AgentUnavailableException;
/**
* Update password.
* @param new/changed password.
*/
public abstract void updatePassword(Command new_password);
/**
* Process disconnect.
* @param state state of the agent.
*/
public abstract void disconnect(final Status state);
/**
* Is the agent closed for more commands?
* @return true if unable to reach agent or false if reachable.
*/
protected abstract boolean isClosed();
protected class Alarm implements Runnable {
long _seq;
public Alarm(long seq) {
_seq = seq;
}
@Override
public void run() {
try {
Listener listener = unregisterListener(_seq);
if (listener != null) {
cancel(_seq);
listener.processTimeout(_id, _seq);
}
} catch (Exception e) {
s_logger.warn("Exception ", e);
}
}
}
}