forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgent.java
More file actions
1436 lines (1281 loc) · 59.2 KB
/
Agent.java
File metadata and controls
1436 lines (1281 loc) · 59.2 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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.channels.ClosedChannelException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import javax.naming.ConfigurationException;
import org.apache.cloudstack.agent.lb.SetupMSListAnswer;
import org.apache.cloudstack.agent.lb.SetupMSListCommand;
import org.apache.cloudstack.ca.PostCertificateRenewalCommand;
import org.apache.cloudstack.ca.SetupCertificateAnswer;
import org.apache.cloudstack.ca.SetupCertificateCommand;
import org.apache.cloudstack.ca.SetupKeyStoreCommand;
import org.apache.cloudstack.ca.SetupKeystoreAnswer;
import org.apache.cloudstack.managed.context.ManagedContextTimerTask;
import org.apache.cloudstack.utils.security.KeyStoreUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.ThreadContext;
import com.cloud.agent.api.AgentControlAnswer;
import com.cloud.agent.api.AgentControlCommand;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.Command;
import com.cloud.agent.api.CronCommand;
import com.cloud.agent.api.MaintainAnswer;
import com.cloud.agent.api.MaintainCommand;
import com.cloud.agent.api.MigrateAgentConnectionAnswer;
import com.cloud.agent.api.MigrateAgentConnectionCommand;
import com.cloud.agent.api.PingAnswer;
import com.cloud.agent.api.PingCommand;
import com.cloud.agent.api.ReadyCommand;
import com.cloud.agent.api.ShutdownCommand;
import com.cloud.agent.api.StartupAnswer;
import com.cloud.agent.api.StartupCommand;
import com.cloud.agent.transport.Request;
import com.cloud.agent.transport.Response;
import com.cloud.exception.AgentControlChannelException;
import com.cloud.host.Host;
import com.cloud.resource.AgentStatusUpdater;
import com.cloud.resource.ResourceStatusUpdater;
import com.cloud.resource.ServerResource;
import com.cloud.utils.NumbersUtil;
import com.cloud.utils.PropertiesUtil;
import com.cloud.utils.StringUtils;
import com.cloud.utils.concurrency.NamedThreadFactory;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.exception.NioConnectionException;
import com.cloud.utils.exception.TaskExecutionException;
import com.cloud.utils.nio.HandlerFactory;
import com.cloud.utils.nio.Link;
import com.cloud.utils.nio.NioClient;
import com.cloud.utils.nio.NioConnection;
import com.cloud.utils.nio.Task;
import com.cloud.utils.script.Script;
/**
* @config
* {@table
* || Param Name | Description | Values | Default ||
* || type | Type of server | Storage / Computing / Routing | No Default ||
* || workers | # of workers to process the requests | int | 1 ||
* || host | host to connect to | ip address | localhost ||
* || port | port to connect to | port number | 8250 ||
* || instance | Used to allow multiple agents running on the same host | String | none || * }
*
* For more configuration options, see the individual types.
*
**/
public class Agent implements HandlerFactory, IAgentControl, AgentStatusUpdater {
protected Logger logger = LogManager.getLogger(getClass());
public enum ExitStatus {
Normal(0), // Normal status = 0.
Upgrade(65), // Exiting for upgrade.
Configuration(66), // Exiting due to configuration problems.
Error(67); // Exiting because of error.
final int value;
ExitStatus(final int value) {
this.value = value;
}
public int value() {
return value;
}
}
CopyOnWriteArrayList<IAgentControlListener> controlListeners = new CopyOnWriteArrayList<>();
IAgentShell shell;
NioConnection connection;
ServerResource serverResource;
Link link;
Long id;
String _uuid;
String _name;
ScheduledExecutorService selfTaskExecutor;
ScheduledExecutorService certExecutor;
ScheduledExecutorService hostLbCheckExecutor;
CopyOnWriteArrayList<ScheduledFuture<?>> watchList = new CopyOnWriteArrayList<>();
AtomicLong sequence = new AtomicLong(0);
AtomicLong lastPingResponseTime = new AtomicLong(0L);
long pingInterval = 0;
AtomicInteger commandsInProgress = new AtomicInteger(0);
private final AtomicReference<StartupTask> startupTask = new AtomicReference<>();
private static final long DEFAULT_STARTUP_WAIT = 180;
long startupWait = DEFAULT_STARTUP_WAIT;
boolean reconnectAllowed = true;
//For time sensitive task, e.g. PingTask
ThreadPoolExecutor outRequestHandler;
ExecutorService requestHandler;
Thread shutdownThread = new ShutdownThread(this);
private String keystoreSetupSetupPath;
private String keystoreCertImportScriptPath;
private String hostname;
protected String getLinkLog(final Link link) {
if (link == null) {
return "";
}
StringBuilder str = new StringBuilder();
if (logger.isTraceEnabled()) {
str.append(System.identityHashCode(link)).append("-");
}
str.append(link.getSocketAddress());
return str.toString();
}
protected String getAgentName() {
return (serverResource != null && serverResource.isAppendAgentNameToLogs() &&
StringUtils.isNotBlank(serverResource.getName())) ?
serverResource.getName() :
"Agent";
}
protected void setupShutdownHookAndInitExecutors() {
logger.trace("Adding shutdown hook");
Runtime.getRuntime().addShutdownHook(shutdownThread);
selfTaskExecutor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("Agent-SelfTask"));
outRequestHandler = new ThreadPoolExecutor(shell.getPingRetries(), 2 * shell.getPingRetries(), 10, TimeUnit.MINUTES,
new SynchronousQueue<>(), new NamedThreadFactory("AgentOutRequest-Handler"));
requestHandler = new ThreadPoolExecutor(shell.getWorkers(), 5 * shell.getWorkers(), 1, TimeUnit.DAYS,
new LinkedBlockingQueue<>(), new NamedThreadFactory("AgentRequest-Handler"));
}
/**
* Constructor for the {@code Agent} class, intended for simulator use only.
*
* <p>This constructor initializes the agent with a provided {@link IAgentShell}.
* It sets up the necessary NIO client connection, establishes a shutdown hook,
* and initializes the thread executors.
*
* @param shell the {@link IAgentShell} instance that provides agent configuration and runtime information.
*/
public Agent(final IAgentShell shell) {
this.shell = shell;
this.link = null;
this.connection = new NioClient(
getAgentName(),
this.shell.getNextHost(),
this.shell.getPort(),
this.shell.getWorkers(),
this.shell.getSslHandshakeTimeout(),
this
);
setupShutdownHookAndInitExecutors();
}
public Agent(final IAgentShell shell, final int localAgentId, final ServerResource resource) throws ConfigurationException {
this.shell = shell;
serverResource = resource;
link = null;
resource.setAgentControl(this);
final String value = shell.getPersistentProperty(getResourceName(), "id");
_uuid = shell.getPersistentProperty(getResourceName(), "uuid");
_name = shell.getPersistentProperty(getResourceName(), "name");
id = value != null ? Long.parseLong(value) : null;
logger.info("Initialising agent [id: {}, uuid: {}, name: {}]", ObjectUtils.defaultIfNull(id, ""), _uuid, _name);
final Map<String, Object> params = new HashMap<>();
// merge with properties from command line to let resource access command line parameters
for (final Map.Entry<String, Object> cmdLineProp : this.shell.getCmdLineProperties().entrySet()) {
params.put(cmdLineProp.getKey(), cmdLineProp.getValue());
}
if (!serverResource.configure(getResourceName(), params)) {
throw new ConfigurationException("Unable to configure " + serverResource.getName());
}
ThreadContext.put("agentname", getAgentName());
final String host = this.shell.getNextHost();
connection = new NioClient(getAgentName(), host, this.shell.getPort(), this.shell.getWorkers(),
this.shell.getSslHandshakeTimeout(), this);
setupShutdownHookAndInitExecutors();
logger.info("{} with host = {}, local id = {}", this, host, localAgentId);
}
@Override
public String toString() {
return String.format("Agent [id = %s, uuid = %s, name = %s, type = %s, zone = %s, pod = %s, workers = %d, port = %d]",
ObjectUtils.defaultIfNull(id, "new"),
_uuid,
_name,
getResourceName(),
this.shell.getZone(),
this.shell.getPod(),
this.shell.getWorkers(),
this.shell.getPort());
}
public String getVersion() {
return shell.getVersion();
}
public String getResourceGuid() {
final String guid = shell.getGuid();
return guid + "-" + getResourceName();
}
public String getZone() {
return shell.getZone();
}
public String getPod() {
return shell.getPod();
}
protected void setLink(final Link link) {
this.link = link;
}
public ServerResource getResource() {
return serverResource;
}
public String getResourceName() {
return serverResource.getClass().getSimpleName();
}
/**
* In case of a software based agent restart, this method
* can help to perform explicit garbage collection of any old
* agent instances and its inner objects.
*/
private void scavengeOldAgentObjects() {
requestHandler.submit(() -> {
try {
Thread.sleep(2000L);
} catch (final InterruptedException ignored) {
} finally {
System.gc();
}
});
}
public void start() {
if (!serverResource.start()) {
String msg = String.format("Unable to start the resource: %s", serverResource.getName());
logger.error(msg);
throw new CloudRuntimeException(msg);
}
keystoreSetupSetupPath = Script.findScript("scripts/util/", KeyStoreUtils.KS_SETUP_SCRIPT);
if (keystoreSetupSetupPath == null) {
throw new CloudRuntimeException(String.format("Unable to find the '%s' script", KeyStoreUtils.KS_SETUP_SCRIPT));
}
keystoreCertImportScriptPath = Script.findScript("scripts/util/", KeyStoreUtils.KS_IMPORT_SCRIPT);
if (keystoreCertImportScriptPath == null) {
throw new CloudRuntimeException(String.format("Unable to find the '%s' script", KeyStoreUtils.KS_IMPORT_SCRIPT));
}
try {
connection.start();
} catch (final NioConnectionException e) {
logger.warn("Attempt to connect to server generated NIO Connection Exception {}, trying again", e.getLocalizedMessage());
}
while (!connection.isStartup()) {
final String host = shell.getNextHost();
shell.getBackoffAlgorithm().waitBeforeRetry();
connection = new NioClient(getAgentName(), host, shell.getPort(), shell.getWorkers(),
shell.getSslHandshakeTimeout(), this);
logger.info("Connecting to host: {}", host);
try {
connection.start();
} catch (final NioConnectionException e) {
stopAndCleanupConnection(false);
logger.info("Attempted to connect to the server, but received an unexpected exception, trying again...", e);
}
}
shell.updateConnectedHost(((NioClient)connection).getHost());
scavengeOldAgentObjects();
}
public void stop(final String reason, final String detail) {
logger.info("Stopping the agent: Reason = {}{}", reason, (detail != null ? ": Detail = " + detail : ""));
reconnectAllowed = false;
if (connection != null) {
final ShutdownCommand cmd = new ShutdownCommand(reason, detail);
try {
if (link != null) {
final Request req = new Request(id != null ? id : -1, -1, cmd, false);
link.send(req.toBytes());
}
} catch (final ClosedChannelException e) {
logger.warn("Unable to send: {}", cmd.toString());
} catch (final Exception e) {
logger.warn("Unable to send: {} due to exception: {}", cmd.toString(), e);
}
logger.debug("Sending shutdown to management server");
try {
Thread.sleep(1000);
} catch (final InterruptedException e) {
logger.debug("Who the heck interrupted me here?");
}
connection.stop();
connection = null;
link = null;
}
if (serverResource != null) {
serverResource.stop();
serverResource = null;
}
if (startupTask.get() != null) {
startupTask.set(null);
}
if (outRequestHandler != null) {
outRequestHandler.shutdownNow();
outRequestHandler = null;
}
if (requestHandler != null) {
requestHandler.shutdown();
requestHandler = null;
}
if (selfTaskExecutor != null) {
selfTaskExecutor.shutdown();
selfTaskExecutor = null;
}
if (hostLbCheckExecutor != null) {
hostLbCheckExecutor.shutdown();
hostLbCheckExecutor = null;
}
if (certExecutor != null) {
certExecutor.shutdown();
certExecutor = null;
}
}
public Long getId() {
return id;
}
public void setId(final Long id) {
logger.debug("Set agent id {}", id);
this.id = id;
shell.setPersistentProperty(getResourceName(), "id", Long.toString(id));
}
public String getUuid() {
return _uuid;
}
public void setUuid(String uuid) {
this._uuid = uuid;
shell.setPersistentProperty(getResourceName(), "uuid", uuid);
}
public String getName() {
return _name;
}
public void setName(String name) {
this._name = name;
shell.setPersistentProperty(getResourceName(), "name", name);
}
private void scheduleCertificateRenewalTask() {
String name = "CertificateRenewalTask";
if (certExecutor != null && !certExecutor.isShutdown()) {
certExecutor.shutdown();
try {
if (!certExecutor.awaitTermination(1, TimeUnit.SECONDS)) {
certExecutor.shutdownNow();
}
} catch (InterruptedException e) {
logger.debug("Forcing {} shutdown as it did not shutdown in the desired time due to: {}",
name, e.getMessage());
certExecutor.shutdownNow();
}
}
certExecutor = Executors.newSingleThreadScheduledExecutor((new NamedThreadFactory(name)));
certExecutor.schedule(new PostCertificateRenewalTask(this), 5, TimeUnit.SECONDS);
}
private void scheduleHostLBCheckerTask(final String lbAlgorithm, final long checkInterval) {
String name = "HostLBCheckerTask";
if (hostLbCheckExecutor != null && !hostLbCheckExecutor.isShutdown()) {
logger.info("Shutting down the preferred host checker task {}", name);
hostLbCheckExecutor.shutdown();
try {
if (!hostLbCheckExecutor.awaitTermination(1, TimeUnit.SECONDS)) {
hostLbCheckExecutor.shutdownNow();
}
} catch (InterruptedException e) {
logger.debug("Forcing the preferred host checker task {} shutdown as it did not shutdown in the desired time due to: {}",
name, e.getMessage());
hostLbCheckExecutor.shutdownNow();
}
}
if (checkInterval > 0L) {
if ("shuffle".equalsIgnoreCase(lbAlgorithm)) {
logger.info("Scheduling the preferred host checker task to trigger once (to apply lb algorithm '{}') after host.lb.interval={} ms", lbAlgorithm, checkInterval);
hostLbCheckExecutor = Executors.newSingleThreadScheduledExecutor((new NamedThreadFactory(name)));
hostLbCheckExecutor.schedule(new PreferredHostCheckerTask(), checkInterval, TimeUnit.MILLISECONDS);
return;
}
logger.info("Scheduling a recurring preferred host checker task with host.lb.interval={} ms", checkInterval);
hostLbCheckExecutor = Executors.newSingleThreadScheduledExecutor((new NamedThreadFactory(name)));
hostLbCheckExecutor.scheduleAtFixedRate(new PreferredHostCheckerTask(), checkInterval, checkInterval,
TimeUnit.MILLISECONDS);
}
}
public void scheduleWatch(final Link link, final Request request, final long delay, final long period) {
logger.debug("Adding a watch list");
final WatchTask task = new WatchTask(link, request, this);
final ScheduledFuture<?> future = selfTaskExecutor.scheduleAtFixedRate(task, delay, period, TimeUnit.MILLISECONDS);
watchList.add(future);
}
public void triggerUpdate() {
PingCommand command = serverResource.getCurrentStatus(getId());
command.setOutOfBand(true);
logger.debug("Sending out of band ping");
final Request request = new Request(id, -1, command, false);
request.setSequence(getNextSequence());
try {
link.send(request.toBytes());
} catch (final ClosedChannelException e) {
logger.warn("Unable to send ping update: {}", request.toString());
}
}
protected void cancelTasks() {
for (final ScheduledFuture<?> task : watchList) {
task.cancel(true);
}
logger.debug("Clearing watch list: {}", () -> watchList.size());
watchList.clear();
}
/**
* Cleanup agent zone properties.
*
* Unset zone, cluster and pod values so that host is not added back
* when service is restarted. This will be set to proper values
* when host is added back
*/
protected void cleanupAgentZoneProperties() {
shell.setPersistentProperty(null, "zone", "");
shell.setPersistentProperty(null, "cluster", "");
shell.setPersistentProperty(null, "pod", "");
}
public void lockStartupTask(final Link link) {
logger.debug("Creating startup task for link: {}", () -> getLinkLog(link));
StartupTask currentTask = startupTask.get();
if (currentTask != null) {
logger.warn("A Startup task is already locked or in progress, cannot create for link {}",
getLinkLog(link));
return;
}
currentTask = new StartupTask(link);
if (startupTask.compareAndSet(null, currentTask)) {
selfTaskExecutor.schedule(currentTask, startupWait, TimeUnit.SECONDS);
return;
}
logger.warn("Failed to lock a StartupTask for link: {}", getLinkLog(link));
}
protected boolean cancelStartupTask() {
StartupTask task = startupTask.getAndSet(null);
if (task != null) {
task.cancel();
return true;
}
return false;
}
public void sendStartup(final Link link) {
sendStartup(link, false);
}
public void sendStartup(final Link link, boolean transfer) {
final StartupCommand[] startup = serverResource.initialize();
if (startup != null) {
final String msHostList = shell.getPersistentProperty(null, "host");
final Command[] commands = new Command[startup.length];
for (int i = 0; i < startup.length; i++) {
setupStartupCommand(startup[i]);
startup[i].setMSHostList(msHostList);
startup[i].setConnectionTransferred(transfer);
commands[i] = startup[i];
}
final Request request = new Request(id != null ? id : -1, -1, commands, false, false);
request.setSequence(getNextSequence());
logger.debug("Sending Startup: {}", request.toString());
lockStartupTask(link);
try {
link.send(request.toBytes());
} catch (final ClosedChannelException e) {
logger.warn("Unable to send request to {} due to '{}', request: {}",
getLinkLog(link), e.getMessage(), request);
}
if (serverResource instanceof ResourceStatusUpdater) {
((ResourceStatusUpdater) serverResource).registerStatusUpdater(this);
}
}
}
protected String retrieveHostname() {
logger.trace("Retrieving hostname with resource={}", () -> serverResource.getClass().getSimpleName());
final String result = Script.runSimpleBashScript(Script.getExecutableAbsolutePath("hostname"), 500);
if (StringUtils.isNotBlank(result)) {
return result;
}
try {
InetAddress address = InetAddress.getLocalHost();
return address.toString();
} catch (final UnknownHostException e) {
logger.warn("unknown host? ", e);
throw new CloudRuntimeException("Cannot get local IP address");
}
}
protected void setupStartupCommand(final StartupCommand startup) {
startup.setId(getId());
if (StringUtils.isBlank(startup.getName())) {
if (StringUtils.isBlank(hostname)) {
hostname = retrieveHostname();
}
startup.setName(hostname);
}
startup.setDataCenter(getZone());
startup.setPod(getPod());
startup.setGuid(getResourceGuid());
startup.setResourceName(getResourceName());
startup.setVersion(getVersion());
startup.setArch(getAgentArch());
}
protected String getAgentArch() {
String arch = Script.runSimpleBashScript(Script.getExecutableAbsolutePath("arch"), 2000);
logger.debug("Arch for agent: {} found: {}", _name, arch);
return arch;
}
@Override
public Task create(final Task.Type type, final Link link, final byte[] data) {
return new ServerHandler(type, link, data);
}
protected void reconnect(final Link link) {
reconnect(link, null, false);
}
protected void reconnect(final Link link, String preferredMSHost, boolean forTransfer) {
if (!(forTransfer || reconnectAllowed)) {
logger.debug("Reconnect requested but it is not allowed {}", () -> getLinkLog(link));
return;
}
cancelStartupTask();
closeAndTerminateLink(link);
closeAndTerminateLink(this.link);
setLink(null);
cancelTasks();
serverResource.disconnected();
logger.info("Lost connection to host: {}. Attempting reconnection while we still have {} commands in progress.", shell.getConnectedHost(), commandsInProgress.get());
stopAndCleanupConnection(true);
String host = preferredMSHost;
if (org.apache.commons.lang3.StringUtils.isBlank(host)) {
host = shell.getNextHost();
}
List<String> avoidMSHostList = shell.getAvoidHosts();
do {
if (CollectionUtils.isEmpty(avoidMSHostList) || !avoidMSHostList.contains(host)) {
connection = new NioClient(getAgentName(), host, shell.getPort(), shell.getWorkers(), shell.getSslHandshakeTimeout(), this);
logger.info("Reconnecting to host: {}", host);
try {
connection.start();
} catch (final NioConnectionException e) {
logger.info("Attempted to re-connect to the server, but received an unexpected exception, trying again...", e);
stopAndCleanupConnection(false);
}
}
shell.getBackoffAlgorithm().waitBeforeRetry();
host = shell.getNextHost();
} while (!connection.isStartup());
shell.updateConnectedHost(((NioClient)connection).getHost());
logger.info("Connected to the host: {}", shell.getConnectedHost());
}
protected void closeAndTerminateLink(final Link link) {
if (link == null) {
return;
}
link.close();
link.terminated();
}
protected void stopAndCleanupConnection(boolean waitForStop) {
if (connection == null) {
return;
}
connection.stop();
try {
connection.cleanUp();
} catch (final IOException e) {
logger.warn("Fail to clean up old connection. {}", e);
}
if (!waitForStop) {
return;
}
do {
shell.getBackoffAlgorithm().waitBeforeRetry();
} while (connection.isStartup());
}
public void processStartupAnswer(final Answer answer, final Response response, final Link link) {
boolean answerValid = cancelStartupTask();
final StartupAnswer startup = (StartupAnswer)answer;
if (!startup.getResult()) {
logger.error("Not allowed to connect to the server: {}", answer.getDetails());
if (serverResource != null && !serverResource.isExitOnFailures()) {
logger.trace("{} does not allow exit on failure, reconnecting",
serverResource.getClass().getSimpleName());
reconnect(link);
return;
}
System.exit(1);
}
if (!answerValid) {
logger.warn("Threw away a startup answer because we're reconnecting.");
return;
}
logger.info("Process agent startup answer, agent [id: {}, uuid: {}, name: {}] connected to the server",
startup.getHostId(), startup.getHostUuid(), startup.getHostName());
setId(startup.getHostId());
setUuid(startup.getHostUuid());
setName(startup.getHostName());
pingInterval = startup.getPingInterval() * 1000L; // change to ms.
updateLastPingResponseTime();
scheduleWatch(link, response, pingInterval, pingInterval);
outRequestHandler.setKeepAliveTime(2 * pingInterval, TimeUnit.MILLISECONDS);
logger.info("Startup Response Received: agent [id: {}, uuid: {}, name: {}]",
startup.getHostId(), startup.getHostUuid(), startup.getHostName());
}
protected void processRequest(final Request request, final Link link) {
boolean requestLogged = false;
Response response = null;
try {
final Command[] cmds = request.getCommands();
final Answer[] answers = new Answer[cmds.length];
for (int i = 0; i < cmds.length; i++) {
final Command cmd = cmds[i];
Answer answer;
try {
if (cmd.getContextParam("logid") != null) {
ThreadContext.put("logcontextid", cmd.getContextParam("logid"));
}
if (logger.isDebugEnabled()) {
if (!requestLogged) // ensures request is logged only once per method call
{
final String requestMsg = request.toString();
if (requestMsg != null) {
logger.debug("Request:{}",requestMsg);
}
requestLogged = true;
}
logger.debug("Processing command: {}", cmd.toString());
}
if (cmd instanceof CronCommand) {
final CronCommand watch = (CronCommand)cmd;
scheduleWatch(link, request, watch.getInterval() * 1000L, watch.getInterval() * 1000L);
answer = new Answer(cmd, true, null);
} else if (cmd instanceof ShutdownCommand) {
final ShutdownCommand shutdown = (ShutdownCommand)cmd;
logger.debug("Received shutdownCommand, due to: {}", shutdown.getReason());
cancelTasks();
if (shutdown.isRemoveHost()) {
cleanupAgentZoneProperties();
}
reconnectAllowed = false;
answer = new Answer(cmd, true, null);
} else if (cmd instanceof ReadyCommand && ((ReadyCommand)cmd).getDetails() != null) {
logger.debug("Not ready to connect to mgt server: {}", ((ReadyCommand)cmd).getDetails());
if (serverResource != null && !serverResource.isExitOnFailures()) {
logger.trace("{} does not allow exit on failure, reconnecting",
serverResource.getClass().getSimpleName());
reconnect(link);
return;
}
System.exit(1);
return;
} else if (cmd instanceof MaintainCommand) {
logger.debug("Received maintainCommand, do not cancel current tasks");
answer = new MaintainAnswer((MaintainCommand)cmd);
} else if (cmd instanceof AgentControlCommand) {
answer = null;
for (final IAgentControlListener listener : controlListeners) {
answer = listener.processControlRequest(request, (AgentControlCommand)cmd);
if (answer != null) {
break;
}
}
if (answer == null) {
logger.warn("No handler found to process cmd: {}", cmd.toString());
answer = new AgentControlAnswer(cmd);
}
} else if (cmd instanceof SetupKeyStoreCommand && ((SetupKeyStoreCommand) cmd).isHandleByAgent()) {
answer = setupAgentKeystore((SetupKeyStoreCommand) cmd);
} else if (cmd instanceof SetupCertificateCommand && ((SetupCertificateCommand) cmd).isHandleByAgent()) {
answer = setupAgentCertificate((SetupCertificateCommand) cmd);
if (Host.Type.Routing.equals(serverResource.getType())) {
scheduleCertificateRenewalTask();
}
} else if (cmd instanceof SetupMSListCommand) {
answer = setupManagementServerList((SetupMSListCommand) cmd);
} else if (cmd instanceof MigrateAgentConnectionCommand) {
answer = migrateAgentToOtherMS((MigrateAgentConnectionCommand) cmd);
} else {
if (cmd instanceof ReadyCommand) {
processReadyCommand(cmd);
}
commandsInProgress.incrementAndGet();
try {
if (cmd.isReconcile()) {
cmd.setRequestSequence(request.getSequence());
}
answer = serverResource.executeRequest(cmd);
} finally {
commandsInProgress.decrementAndGet();
}
if (answer == null) {
logger.debug("Response: unsupported command {}", cmd.toString());
answer = Answer.createUnsupportedCommandAnswer(cmd);
}
}
} catch (final Throwable th) {
logger.warn("Caught: ", th);
final StringWriter writer = new StringWriter();
th.printStackTrace(new PrintWriter(writer));
answer = new Answer(cmd, false, writer.toString());
}
answers[i] = answer;
if (!answer.getResult() && request.stopOnError()) {
for (i++; i < cmds.length; i++) {
answers[i] = new Answer(cmds[i], false, "Stopped by previous failure");
}
break;
}
}
response = new Response(request, answers);
} finally {
if (logger.isDebugEnabled()) {
final String responseMsg = response.toString();
if (responseMsg != null) {
logger.debug(response.toString());
}
}
if (response != null) {
try {
link.send(response.toBytes());
} catch (final ClosedChannelException e) {
logger.warn("Unable to send response: {}", response.toString());
}
}
}
}
public Answer setupAgentKeystore(final SetupKeyStoreCommand cmd) {
final String keyStorePassword = cmd.getKeystorePassword();
final long validityDays = cmd.getValidityDays();
logger.debug("Setting up agent keystore file and generating CSR");
final File agentFile = PropertiesUtil.findConfigFile("agent.properties");
if (agentFile == null) {
return new Answer(cmd, false, "Failed to find agent.properties file");
}
final String keyStoreFile = agentFile.getParent() + "/" + KeyStoreUtils.KS_FILENAME;
final String csrFile = agentFile.getParent() + "/" + KeyStoreUtils.CSR_FILENAME;
String storedPassword = shell.getPersistentProperty(null, KeyStoreUtils.KS_PASSPHRASE_PROPERTY);
if (StringUtils.isEmpty(storedPassword)) {
storedPassword = keyStorePassword;
shell.setPersistentProperty(null, KeyStoreUtils.KS_PASSPHRASE_PROPERTY, storedPassword);
}
Script script = new Script(keystoreSetupSetupPath, 300000, logger);
script.add(agentFile.getAbsolutePath());
script.add(keyStoreFile);
script.add(storedPassword);
script.add(String.valueOf(validityDays));
script.add(csrFile);
String result = script.execute();
if (result != null) {
throw new CloudRuntimeException("Unable to setup keystore file");
}
final String csrString;
try {
csrString = FileUtils.readFileToString(new File(csrFile), Charset.defaultCharset());
} catch (IOException e) {
throw new CloudRuntimeException("Unable to read generated CSR file", e);
}
return new SetupKeystoreAnswer(csrString);
}
private Answer setupAgentCertificate(final SetupCertificateCommand cmd) {
final String certificate = cmd.getCertificate();
final String privateKey = cmd.getPrivateKey();
final String caCertificates = cmd.getCaCertificates();
logger.debug("Importing received certificate to agent's keystore");
final File agentFile = PropertiesUtil.findConfigFile("agent.properties");
if (agentFile == null) {
return new Answer(cmd, false, "Failed to find agent.properties file");
}
final String keyStoreFile = agentFile.getParent() + "/" + KeyStoreUtils.KS_FILENAME;
final String certFile = agentFile.getParent() + "/" + KeyStoreUtils.CERT_FILENAME;
final String privateKeyFile = agentFile.getParent() + "/" + KeyStoreUtils.PKEY_FILENAME;
final String caCertFile = agentFile.getParent() + "/" + KeyStoreUtils.CACERT_FILENAME;
try {
FileUtils.writeStringToFile(new File(certFile), certificate, Charset.defaultCharset());
FileUtils.writeStringToFile(new File(caCertFile), caCertificates, Charset.defaultCharset());
logger.debug("Saved received client certificate to: {}", certFile);
} catch (IOException e) {
throw new CloudRuntimeException("Unable to save received agent client and ca certificates", e);
}
String ksPassphrase = shell.getPersistentProperty(null, KeyStoreUtils.KS_PASSPHRASE_PROPERTY);
Script script = new Script(keystoreCertImportScriptPath, 300000, logger);
script.add(agentFile.getAbsolutePath());
script.add(ksPassphrase);
script.add(keyStoreFile);
script.add(KeyStoreUtils.AGENT_MODE);
script.add(certFile);
script.add("");
script.add(caCertFile);
script.add("");
script.add(privateKeyFile);
script.add(privateKey);
String result = script.execute();
if (result != null) {
throw new CloudRuntimeException("Unable to import certificate into keystore file");
}
return new SetupCertificateAnswer(true);
}
private void processManagementServerList(final List<String> msList, final List<String> avoidMsList, final String lbAlgorithm, final Long lbCheckInterval, final boolean triggerHostLB) {
if (CollectionUtils.isNotEmpty(msList) && StringUtils.isNotEmpty(lbAlgorithm)) {
try {
final String newMSHosts = String.format("%s%s%s", com.cloud.utils.StringUtils.toCSVList(msList), IAgentShell.hostLbAlgorithmSeparator, lbAlgorithm);
shell.setPersistentProperty(null, "host", newMSHosts);
shell.setHosts(newMSHosts);
shell.resetHostCounter();
logger.info("Processed new management server list: {}", newMSHosts);
} catch (final Exception e) {
throw new CloudRuntimeException("Could not persist received management servers list", e);
}
}
shell.setAvoidHosts(avoidMsList);
if (triggerHostLB) {
logger.info("Triggering the preferred host checker task now");
ScheduledExecutorService hostLbExecutor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("HostLB-Executor"));
hostLbExecutor.schedule(new PreferredHostCheckerTask(), 0, TimeUnit.MILLISECONDS);
hostLbExecutor.shutdown();
}
scheduleHostLBCheckerTask(lbAlgorithm, shell.getLbCheckerInterval(lbCheckInterval));
}
private Answer setupManagementServerList(final SetupMSListCommand cmd) {
processManagementServerList(cmd.getMsList(), cmd.getAvoidMsList(), cmd.getLbAlgorithm(), cmd.getLbCheckInterval(), cmd.getTriggerHostLb());
return new SetupMSListAnswer(true);
}
private Answer migrateAgentToOtherMS(final MigrateAgentConnectionCommand cmd) {
try {
if (CollectionUtils.isNotEmpty(cmd.getMsList())) {
processManagementServerList(cmd.getMsList(), cmd.getAvoidMsList(), cmd.getLbAlgorithm(), cmd.getLbCheckInterval(), false);
}
ScheduledExecutorService migrateAgentConnectionService = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("MigrateAgentConnection-Job"));
migrateAgentConnectionService.schedule(() -> {
migrateAgentConnection(cmd.getAvoidMsList());
}, 3, TimeUnit.SECONDS);
migrateAgentConnectionService.shutdown();
} catch (Exception e) {
String errMsg = "Migrate agent connection failed, due to " + e.getMessage();
logger.debug(errMsg, e);
return new MigrateAgentConnectionAnswer(errMsg);
}
return new MigrateAgentConnectionAnswer(true);
}
private void migrateAgentConnection(List<String> avoidMsList) {
final String[] msHosts = shell.getHosts();
if (msHosts == null || msHosts.length < 1) {
throw new CloudRuntimeException("Management Server hosts empty, not properly configured in agent");
}
List<String> msHostsList = new ArrayList<>(Arrays.asList(msHosts));
msHostsList.removeAll(avoidMsList);
if (msHostsList.isEmpty() || StringUtils.isEmpty(msHostsList.get(0))) {
throw new CloudRuntimeException("No other Management Server hosts to migrate");
}
String preferredMSHost = null;
for (String msHost : msHostsList) {
try (final Socket socket = new Socket()) {
socket.connect(new InetSocketAddress(msHost, shell.getPort()), 5000);
preferredMSHost = msHost;
break;