forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecondaryStorageManagerImpl.java
More file actions
executable file
·1482 lines (1295 loc) · 62.1 KB
/
Copy pathSecondaryStorageManagerImpl.java
File metadata and controls
executable file
·1482 lines (1295 loc) · 62.1 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.storage.secondary;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.Local;
import javax.inject.Inject;
import javax.naming.ConfigurationException;
import org.apache.log4j.Logger;
import com.cloud.agent.AgentManager;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.Command;
import com.cloud.agent.api.RebootCommand;
import com.cloud.agent.api.SecStorageFirewallCfgCommand;
import com.cloud.agent.api.SecStorageSetupAnswer;
import com.cloud.agent.api.SecStorageSetupCommand;
import com.cloud.agent.api.SecStorageSetupCommand.Certificates;
import com.cloud.agent.api.SecStorageVMSetupCommand;
import com.cloud.agent.api.StartupCommand;
import com.cloud.agent.api.StartupSecondaryStorageCommand;
import com.cloud.agent.api.StartupStorageCommand;
import com.cloud.agent.api.StopAnswer;
import com.cloud.agent.api.check.CheckSshAnswer;
import com.cloud.agent.api.check.CheckSshCommand;
import com.cloud.agent.api.to.NicTO;
import com.cloud.agent.api.to.VirtualMachineTO;
import com.cloud.agent.manager.Commands;
import com.cloud.capacity.dao.CapacityDao;
import com.cloud.cluster.ClusterManager;
import com.cloud.cluster.ManagementServerNode;
import com.cloud.configuration.Config;
import com.cloud.configuration.ZoneConfig;
import com.cloud.configuration.dao.ConfigurationDao;
import com.cloud.consoleproxy.ConsoleProxyManager;
import com.cloud.dc.DataCenter;
import com.cloud.dc.DataCenter.NetworkType;
import com.cloud.dc.DataCenterVO;
import com.cloud.dc.dao.DataCenterDao;
import com.cloud.deploy.DataCenterDeployment;
import com.cloud.deploy.DeployDestination;
import com.cloud.exception.ConcurrentOperationException;
import com.cloud.exception.InsufficientCapacityException;
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.exception.StorageUnavailableException;
import com.cloud.host.Host;
import com.cloud.host.HostVO;
import com.cloud.host.dao.HostDao;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.info.RunningHostCountInfo;
import com.cloud.info.RunningHostInfoAgregator;
import com.cloud.info.RunningHostInfoAgregator.ZoneHostInfo;
import com.cloud.keystore.KeystoreManager;
import com.cloud.network.Network;
import com.cloud.network.NetworkManager;
import com.cloud.network.NetworkModel;
import com.cloud.network.Networks.TrafficType;
import com.cloud.network.dao.IPAddressDao;
import com.cloud.network.dao.IPAddressVO;
import com.cloud.network.dao.NetworkDao;
import com.cloud.network.dao.NetworkVO;
import com.cloud.network.rules.RulesManager;
import com.cloud.offering.NetworkOffering;
import com.cloud.offering.ServiceOffering;
import com.cloud.offerings.NetworkOfferingVO;
import com.cloud.offerings.dao.NetworkOfferingDao;
import com.cloud.resource.ResourceManager;
import com.cloud.resource.ResourceStateAdapter;
import com.cloud.resource.ServerResource;
import com.cloud.resource.UnableDeleteHostException;
import com.cloud.service.ServiceOfferingVO;
import com.cloud.service.dao.ServiceOfferingDao;
import com.cloud.storage.SnapshotVO;
import com.cloud.storage.Storage;
import com.cloud.storage.VMTemplateHostVO;
import com.cloud.storage.VMTemplateStorageResourceAssoc.Status;
import com.cloud.storage.VMTemplateVO;
import com.cloud.storage.dao.SnapshotDao;
import com.cloud.storage.dao.StoragePoolHostDao;
import com.cloud.storage.dao.VMTemplateDao;
import com.cloud.storage.dao.VMTemplateHostDao;
import com.cloud.storage.resource.DummySecondaryStorageResource;
import com.cloud.storage.swift.SwiftManager;
import com.cloud.storage.template.TemplateConstants;
import com.cloud.user.Account;
import com.cloud.user.AccountService;
import com.cloud.user.User;
import com.cloud.user.UserContext;
import com.cloud.utils.DateUtil;
import com.cloud.utils.NumbersUtil;
import com.cloud.utils.Pair;
import com.cloud.utils.component.ManagerBase;
import com.cloud.utils.db.GlobalLock;
import com.cloud.utils.db.SearchCriteria.Op;
import com.cloud.utils.db.SearchCriteria2;
import com.cloud.utils.db.SearchCriteriaService;
import com.cloud.utils.events.SubscriptionMgr;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.net.NetUtils;
import com.cloud.vm.Nic;
import com.cloud.vm.NicProfile;
import com.cloud.vm.ReservationContext;
import com.cloud.vm.SecondaryStorageVm;
import com.cloud.vm.SecondaryStorageVmVO;
import com.cloud.vm.SystemVmLoadScanHandler;
import com.cloud.vm.SystemVmLoadScanner;
import com.cloud.vm.SystemVmLoadScanner.AfterScanAction;
import com.cloud.vm.VirtualMachine;
import com.cloud.vm.VirtualMachine.State;
import com.cloud.vm.VirtualMachineGuru;
import com.cloud.vm.VirtualMachineManager;
import com.cloud.vm.VirtualMachineName;
import com.cloud.vm.VirtualMachineProfile;
import com.cloud.vm.dao.SecondaryStorageVmDao;
import com.cloud.vm.dao.UserVmDetailsDao;
import com.cloud.vm.dao.VMInstanceDao;
//
// Possible secondary storage vm state transition cases
// Creating -> Destroyed
// Creating -> Stopped --> Starting -> Running
// HA -> Stopped -> Starting -> Running
// Migrating -> Running (if previous state is Running before it enters into Migrating state
// Migrating -> Stopped (if previous state is not Running before it enters into Migrating state)
// Running -> HA (if agent lost connection)
// Stopped -> Destroyed
//
// Creating state indicates of record creating and IP address allocation are ready, it is a transient
// state which will soon be switching towards Running if everything goes well.
// Stopped state indicates the readiness of being able to start (has storage and IP resources allocated)
// Starting state can only be entered from Stopped states
//
// Starting, HA, Migrating, Creating and Running state are all counted as "Open" for available capacity calculation
// because sooner or later, it will be driven into Running state
//
@Local(value = { SecondaryStorageVmManager.class })
public class SecondaryStorageManagerImpl extends ManagerBase implements SecondaryStorageVmManager, VirtualMachineGuru<SecondaryStorageVmVO>, SystemVmLoadScanHandler<Long>, ResourceStateAdapter {
private static final Logger s_logger = Logger.getLogger(SecondaryStorageManagerImpl.class);
private static final int DEFAULT_CAPACITY_SCAN_INTERVAL = 30000; // 30
// seconds
private static final int ACQUIRE_GLOBAL_LOCK_TIMEOUT_FOR_SYNC = 180; // 3
// minutes
private static final int STARTUP_DELAY = 60000; // 60 seconds
private String _mgmt_host;
private int _mgmt_port = 8250;
@Inject
private List<SecondaryStorageVmAllocator> _ssVmAllocators;
@Inject
protected SecondaryStorageVmDao _secStorageVmDao;
@Inject
private DataCenterDao _dcDao;
@Inject
private VMTemplateDao _templateDao;
@Inject
private HostDao _hostDao;
@Inject
private StoragePoolHostDao _storagePoolHostDao;
@Inject
private VMTemplateHostDao _vmTemplateHostDao;
@Inject
private AgentManager _agentMgr;
@Inject
protected SwiftManager _swiftMgr;
@Inject
protected NetworkManager _networkMgr;
@Inject
protected NetworkModel _networkModel;
@Inject
protected SnapshotDao _snapshotDao;
@Inject
private ClusterManager _clusterMgr;
private SecondaryStorageListener _listener;
private ServiceOfferingVO _serviceOffering;
@Inject
protected ConfigurationDao _configDao;
@Inject
private ServiceOfferingDao _offeringDao;
@Inject
private AccountService _accountMgr;
@Inject
private VirtualMachineManager _itMgr;
@Inject
protected VMInstanceDao _vmDao;
@Inject
protected CapacityDao _capacityDao;
@Inject
UserVmDetailsDao _vmDetailsDao;
@Inject
protected ResourceManager _resourceMgr;
//@Inject // TODO this is a very strange usage, a singleton class need to inject itself?
protected SecondaryStorageVmManager _ssvmMgr;
@Inject
NetworkDao _networkDao;
@Inject
NetworkOfferingDao _networkOfferingDao;
@Inject
protected IPAddressDao _ipAddressDao = null;
@Inject
protected RulesManager _rulesMgr;
@Inject
KeystoreManager _keystoreMgr;
private long _capacityScanInterval = DEFAULT_CAPACITY_SCAN_INTERVAL;
private int _secStorageVmMtuSize;
private String _instance;
private boolean _useLocalStorage;
private boolean _useSSlCopy;
private String _httpProxy;
private String _allowedInternalSites;
protected long _nodeId = ManagementServerNode.getManagementServerId();
private SystemVmLoadScanner<Long> _loadScanner;
private Map<Long, ZoneHostInfo> _zoneHostInfoMap; // map <zone id, info about running host in zone>
private final GlobalLock _allocLock = GlobalLock.getInternLock(getAllocLockName());
public SecondaryStorageManagerImpl() {
_ssvmMgr = this;
}
@Override
public SecondaryStorageVmVO startSecStorageVm(long secStorageVmId) {
try {
SecondaryStorageVmVO secStorageVm = _secStorageVmDao.findById(secStorageVmId);
Account systemAcct = _accountMgr.getSystemAccount();
User systemUser = _accountMgr.getSystemUser();
return _itMgr.start(secStorageVm, null, systemUser, systemAcct);
} catch (StorageUnavailableException e) {
s_logger.warn("Exception while trying to start secondary storage vm", e);
return null;
} catch (InsufficientCapacityException e) {
s_logger.warn("Exception while trying to start secondary storage vm", e);
return null;
} catch (ResourceUnavailableException e) {
s_logger.warn("Exception while trying to start secondary storage vm", e);
return null;
} catch (Exception e) {
s_logger.warn("Exception while trying to start secondary storage vm", e);
return null;
}
}
SecondaryStorageVmVO getSSVMfromHost(HostVO ssAHost) {
if( ssAHost.getType() == Host.Type.SecondaryStorageVM ) {
return _secStorageVmDao.findByInstanceName(ssAHost.getName());
}
return null;
}
@Override
public boolean generateSetupCommand(Long ssHostId) {
HostVO cssHost = _hostDao.findById(ssHostId);
Long zoneId = cssHost.getDataCenterId();
if( cssHost.getType() == Host.Type.SecondaryStorageVM ) {
SecondaryStorageVmVO secStorageVm = _secStorageVmDao.findByInstanceName(cssHost.getName());
if (secStorageVm == null) {
s_logger.warn("secondary storage VM " + cssHost.getName() + " doesn't exist");
return false;
}
List<HostVO> ssHosts = _ssvmMgr.listSecondaryStorageHostsInOneZone(zoneId);
for( HostVO ssHost : ssHosts ) {
String secUrl = ssHost.getStorageUrl();
SecStorageSetupCommand setupCmd = null;
if (!_useSSlCopy) {
setupCmd = new SecStorageSetupCommand(secUrl, null);
} else {
Certificates certs = _keystoreMgr.getCertificates(ConsoleProxyManager.CERTIFICATE_NAME);
setupCmd = new SecStorageSetupCommand(secUrl, certs);
}
Answer answer = _agentMgr.easySend(ssHostId, setupCmd);
if (answer != null && answer.getResult()) {
SecStorageSetupAnswer an = (SecStorageSetupAnswer) answer;
ssHost.setParent(an.get_dir());
_hostDao.update(ssHost.getId(), ssHost);
if (s_logger.isDebugEnabled()) {
s_logger.debug("Successfully programmed secondary storage " + ssHost.getName() + " in secondary storage VM " + secStorageVm.getInstanceName());
}
} else {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Successfully programmed secondary storage " + ssHost.getName() + " in secondary storage VM " + secStorageVm.getInstanceName());
}
return false;
}
}
} else if( cssHost.getType() == Host.Type.SecondaryStorage ) {
List<SecondaryStorageVmVO> alreadyRunning = _secStorageVmDao.getSecStorageVmListInStates(SecondaryStorageVm.Role.templateProcessor, zoneId, State.Running);
String secUrl = cssHost.getStorageUrl();
SecStorageSetupCommand setupCmd = new SecStorageSetupCommand(secUrl, null);
for ( SecondaryStorageVmVO ssVm : alreadyRunning ) {
HostVO host = _resourceMgr.findHostByName(ssVm.getInstanceName());
Answer answer = _agentMgr.easySend(host.getId(), setupCmd);
if (answer != null && answer.getResult()) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Successfully programmed secondary storage " + host.getName() + " in secondary storage VM " + ssVm.getInstanceName());
}
} else {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Successfully programmed secondary storage " + host.getName() + " in secondary storage VM " + ssVm.getInstanceName());
}
return false;
}
}
}
return true;
}
@Override
public boolean deleteHost(Long hostId) {
List<SnapshotVO> snapshots = _snapshotDao.listByHostId(hostId);
if( snapshots != null && !snapshots.isEmpty()) {
throw new CloudRuntimeException("Can not delete this secondary storage since it contains atleast one or more snapshots ");
}
if (!_swiftMgr.isSwiftEnabled()) {
List<Long> list = _templateDao.listPrivateTemplatesByHost(hostId);
if (list != null && !list.isEmpty()) {
throw new CloudRuntimeException("Can not delete this secondary storage since it contains private templates ");
}
}
_vmTemplateHostDao.deleteByHost(hostId);
HostVO host = _hostDao.findById(hostId);
host.setGuid(null);
_hostDao.update(hostId, host);
_hostDao.remove(hostId);
return true;
}
@Override
public boolean generateVMSetupCommand(Long ssAHostId) {
HostVO ssAHost = _hostDao.findById(ssAHostId);
if( ssAHost.getType() != Host.Type.SecondaryStorageVM ) {
return false;
}
SecondaryStorageVmVO secStorageVm = _secStorageVmDao.findByInstanceName(ssAHost.getName());
if (secStorageVm == null) {
s_logger.warn("secondary storage VM " + ssAHost.getName() + " doesn't exist");
return false;
}
SecStorageVMSetupCommand setupCmd = new SecStorageVMSetupCommand();
if (_allowedInternalSites != null) {
List<String> allowedCidrs = new ArrayList<String>();
String[] cidrs = _allowedInternalSites.split(",");
for (String cidr : cidrs) {
if (NetUtils.isValidCIDR(cidr) || NetUtils.isValidIp(cidr) || !cidr.startsWith("0.0.0.0")) {
allowedCidrs.add(cidr);
}
}
List<? extends Nic> nics = _networkModel.getNicsForTraffic(secStorageVm.getId(), TrafficType.Management);
setupCmd.setAllowedInternalSites(allowedCidrs.toArray(new String[allowedCidrs.size()]));
}
String copyPasswd = _configDao.getValue("secstorage.copy.password");
setupCmd.setCopyPassword(copyPasswd);
setupCmd.setCopyUserName(TemplateConstants.DEFAULT_HTTP_AUTH_USER);
Answer answer = _agentMgr.easySend(ssAHostId, setupCmd);
if (answer != null && answer.getResult()) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Successfully programmed http auth into " + secStorageVm.getHostName());
}
return true;
} else {
if (s_logger.isDebugEnabled()) {
s_logger.debug("failed to program http auth into secondary storage vm : " + secStorageVm.getHostName());
}
return false;
}
}
@Override
public Pair<HostVO, SecondaryStorageVmVO> assignSecStorageVm(long zoneId, Command cmd) {
return null;
}
@Override
public boolean generateFirewallConfiguration(Long ssAHostId) {
if ( ssAHostId == null ) {
return true;
}
HostVO ssAHost = _hostDao.findById(ssAHostId);
SecondaryStorageVmVO thisSecStorageVm = _secStorageVmDao.findByInstanceName(ssAHost.getName());
if (thisSecStorageVm == null) {
s_logger.warn("secondary storage VM " + ssAHost.getName() + " doesn't exist");
return false;
}
String copyPort = _useSSlCopy? "443" : Integer.toString(TemplateConstants.DEFAULT_TMPLT_COPY_PORT);
SecStorageFirewallCfgCommand thiscpc = new SecStorageFirewallCfgCommand(true);
thiscpc.addPortConfig(thisSecStorageVm.getPublicIpAddress(), copyPort, true, TemplateConstants.DEFAULT_TMPLT_COPY_INTF);
SearchCriteriaService<HostVO, HostVO> sc = SearchCriteria2.create(HostVO.class);
sc.addAnd(sc.getEntity().getType(), Op.EQ, Host.Type.SecondaryStorageVM);
sc.addAnd(sc.getEntity().getStatus(), Op.IN, com.cloud.host.Status.Up, com.cloud.host.Status.Connecting);
List<HostVO> ssvms = sc.list();
for (HostVO ssvm : ssvms) {
if (ssvm.getId() == ssAHostId) {
continue;
}
Answer answer = _agentMgr.easySend(ssvm.getId(), thiscpc);
if (answer != null && answer.getResult()) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Successfully programmed firewall rules into SSVM " + ssvm.getName());
}
} else {
if (s_logger.isDebugEnabled()) {
s_logger.debug("failed to program firewall rules into secondary storage vm : " + ssvm.getName());
}
return false;
}
}
SecStorageFirewallCfgCommand allSSVMIpList = new SecStorageFirewallCfgCommand(false);
for (HostVO ssvm : ssvms) {
if (ssvm.getId() == ssAHostId) {
continue;
}
allSSVMIpList.addPortConfig(ssvm.getPublicIpAddress(), copyPort, true, TemplateConstants.DEFAULT_TMPLT_COPY_INTF);
}
Answer answer = _agentMgr.easySend(ssAHostId, allSSVMIpList);
if (answer != null && answer.getResult()) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Successfully programmed firewall rules into " + thisSecStorageVm.getHostName());
}
} else {
if (s_logger.isDebugEnabled()) {
s_logger.debug("failed to program firewall rules into secondary storage vm : " + thisSecStorageVm.getHostName());
}
return false;
}
return true;
}
protected boolean isSecondaryStorageVmRequired(long dcId) {
DataCenterVO dc = _dcDao.findById(dcId);
_dcDao.loadDetails(dc);
String ssvmReq = dc.getDetail(ZoneConfig.EnableSecStorageVm.key());
if (ssvmReq != null) {
return Boolean.parseBoolean(ssvmReq);
}
return true;
}
public SecondaryStorageVmVO startNew(long dataCenterId, SecondaryStorageVm.Role role) {
if (!isSecondaryStorageVmRequired(dataCenterId)) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Secondary storage vm not required in zone " + dataCenterId + " acc. to zone config");
}
return null;
}
if (s_logger.isDebugEnabled()) {
s_logger.debug("Assign secondary storage vm from a newly started instance for request from data center : " + dataCenterId);
}
Map<String, Object> context = createSecStorageVmInstance(dataCenterId, role);
long secStorageVmId = (Long) context.get("secStorageVmId");
if (secStorageVmId == 0) {
if (s_logger.isTraceEnabled()) {
s_logger.trace("Creating secondary storage vm instance failed, data center id : " + dataCenterId);
}
return null;
}
SecondaryStorageVmVO secStorageVm = _secStorageVmDao.findById(secStorageVmId);
// SecondaryStorageVmVO secStorageVm =
// allocSecStorageVmStorage(dataCenterId, secStorageVmId);
if (secStorageVm != null) {
SubscriptionMgr.getInstance().notifySubscribers(ALERT_SUBJECT, this,
new SecStorageVmAlertEventArgs(SecStorageVmAlertEventArgs.SSVM_CREATED, dataCenterId, secStorageVmId, secStorageVm, null));
return secStorageVm;
} else {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Unable to allocate secondary storage vm storage, remove the secondary storage vm record from DB, secondary storage vm id: " + secStorageVmId);
}
SubscriptionMgr.getInstance().notifySubscribers(ALERT_SUBJECT, this,
new SecStorageVmAlertEventArgs(SecStorageVmAlertEventArgs.SSVM_CREATE_FAILURE, dataCenterId, secStorageVmId, null, "Unable to allocate storage"));
}
return null;
}
protected Map<String, Object> createSecStorageVmInstance(long dataCenterId, SecondaryStorageVm.Role role) {
HostVO secHost = findSecondaryStorageHost(dataCenterId);
if (secHost == null) {
String msg = "No secondary storage available in zone " + dataCenterId + ", cannot create secondary storage vm";
s_logger.warn(msg);
throw new CloudRuntimeException(msg);
}
long id = _secStorageVmDao.getNextInSequence(Long.class, "id");
String name = VirtualMachineName.getSystemVmName(id, _instance, "s").intern();
Account systemAcct = _accountMgr.getSystemAccount();
DataCenterDeployment plan = new DataCenterDeployment(dataCenterId);
DataCenter dc = _dcDao.findById(plan.getDataCenterId());
NetworkVO defaultNetwork = null;
if (dc.getNetworkType() == NetworkType.Advanced && dc.isSecurityGroupEnabled()) {
List<NetworkVO> networks = _networkDao.listByZoneSecurityGroup(dataCenterId);
if (networks == null || networks.size() == 0) {
throw new CloudRuntimeException("Can not found security enabled network in SG Zone " + dc);
}
defaultNetwork = networks.get(0);
} else {
TrafficType defaultTrafficType = TrafficType.Public;
if (dc.getNetworkType() == NetworkType.Basic || dc.isSecurityGroupEnabled()) {
defaultTrafficType = TrafficType.Guest;
}
List<NetworkVO> defaultNetworks = _networkDao.listByZoneAndTrafficType(dataCenterId, defaultTrafficType);
// api should never allow this situation to happen
if (defaultNetworks.size() != 1) {
throw new CloudRuntimeException("Found " + defaultNetworks.size() + " networks of type "
+ defaultTrafficType + " when expect to find 1");
}
defaultNetwork = defaultNetworks.get(0);
}
List<? extends NetworkOffering> offerings = _networkModel.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemControlNetwork, NetworkOfferingVO.SystemManagementNetwork, NetworkOfferingVO.SystemStorageNetwork);
List<Pair<NetworkVO, NicProfile>> networks = new ArrayList<Pair<NetworkVO, NicProfile>>(offerings.size() + 1);
NicProfile defaultNic = new NicProfile();
defaultNic.setDefaultNic(true);
defaultNic.setDeviceId(2);
try {
networks.add(new Pair<NetworkVO, NicProfile>(_networkMgr.setupNetwork(systemAcct, _networkOfferingDao.findById(defaultNetwork.getNetworkOfferingId()), plan, null, null, false).get(0), defaultNic));
for (NetworkOffering offering : offerings) {
networks.add(new Pair<NetworkVO, NicProfile>(_networkMgr.setupNetwork(systemAcct, offering, plan, null, null, false).get(0), null));
}
} catch (ConcurrentOperationException e) {
s_logger.info("Unable to setup due to concurrent operation. " + e);
return new HashMap<String, Object>();
}
HypervisorType hypeType = _resourceMgr.getAvailableHypervisor(dataCenterId);
VMTemplateVO template = _templateDao.findSystemVMTemplate(dataCenterId, hypeType);
if (template == null) {
s_logger.debug("Can't find a template to start");
throw new CloudRuntimeException("Insufficient capacity exception");
}
SecondaryStorageVmVO secStorageVm = new SecondaryStorageVmVO(id, _serviceOffering.getId(), name, template.getId(), template.getHypervisorType(), template.getGuestOSId(), dataCenterId,
systemAcct.getDomainId(), systemAcct.getId(), role, _serviceOffering.getOfferHA());
try {
secStorageVm = _itMgr.allocate(secStorageVm, template, _serviceOffering, networks, plan, null, systemAcct);
} catch (InsufficientCapacityException e) {
s_logger.warn("InsufficientCapacity", e);
throw new CloudRuntimeException("Insufficient capacity exception", e);
}
Map<String, Object> context = new HashMap<String, Object>();
context.put("secStorageVmId", secStorageVm.getId());
return context;
}
private SecondaryStorageVmAllocator getCurrentAllocator() {
// for now, only one adapter is supported
if(_ssVmAllocators.size() > 0)
return _ssVmAllocators.get(0);
return null;
}
protected String connect(String ipAddress, int port) {
return null;
}
public SecondaryStorageVmVO assignSecStorageVmFromRunningPool(long dataCenterId, SecondaryStorageVm.Role role) {
if (s_logger.isTraceEnabled()) {
s_logger.trace("Assign secondary storage vm from running pool for request from data center : " + dataCenterId);
}
SecondaryStorageVmAllocator allocator = getCurrentAllocator();
assert (allocator != null);
List<SecondaryStorageVmVO> runningList = _secStorageVmDao.getSecStorageVmListInStates(role, dataCenterId, State.Running);
if (runningList != null && runningList.size() > 0) {
if (s_logger.isTraceEnabled()) {
s_logger.trace("Running secondary storage vm pool size : " + runningList.size());
for (SecondaryStorageVmVO secStorageVm : runningList) {
s_logger.trace("Running secStorageVm instance : " + secStorageVm.getHostName());
}
}
Map<Long, Integer> loadInfo = new HashMap<Long, Integer>();
return allocator.allocSecondaryStorageVm(runningList, loadInfo, dataCenterId);
} else {
if (s_logger.isTraceEnabled()) {
s_logger.trace("Empty running secStorageVm pool for now in data center : " + dataCenterId);
}
}
return null;
}
public SecondaryStorageVmVO assignSecStorageVmFromStoppedPool(long dataCenterId, SecondaryStorageVm.Role role) {
List<SecondaryStorageVmVO> l = _secStorageVmDao.getSecStorageVmListInStates(role, dataCenterId, State.Starting, State.Stopped, State.Migrating);
if (l != null && l.size() > 0) {
return l.get(0);
}
return null;
}
private void allocCapacity(long dataCenterId, SecondaryStorageVm.Role role) {
if (s_logger.isTraceEnabled()) {
s_logger.trace("Allocate secondary storage vm standby capacity for data center : " + dataCenterId);
}
if (!isSecondaryStorageVmRequired(dataCenterId)) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Secondary storage vm not required in zone " + dataCenterId + " acc. to zone config");
}
return;
}
boolean secStorageVmFromStoppedPool = false;
SecondaryStorageVmVO secStorageVm = assignSecStorageVmFromStoppedPool(dataCenterId, role);
if (secStorageVm == null) {
if (s_logger.isInfoEnabled()) {
s_logger.info("No stopped secondary storage vm is available, need to allocate a new secondary storage vm");
}
if (_allocLock.lock(ACQUIRE_GLOBAL_LOCK_TIMEOUT_FOR_SYNC)) {
try {
secStorageVm = startNew(dataCenterId, role);
} finally {
_allocLock.unlock();
}
} else {
if (s_logger.isInfoEnabled()) {
s_logger.info("Unable to acquire synchronization lock to allocate secStorageVm resource for standby capacity, wait for next scan");
}
return;
}
} else {
if (s_logger.isInfoEnabled()) {
s_logger.info("Found a stopped secondary storage vm, bring it up to running pool. secStorageVm vm id : " + secStorageVm.getId());
}
secStorageVmFromStoppedPool = true;
}
if (secStorageVm != null) {
long secStorageVmId = secStorageVm.getId();
GlobalLock secStorageVmLock = GlobalLock.getInternLock(getSecStorageVmLockName(secStorageVmId));
try {
if (secStorageVmLock.lock(ACQUIRE_GLOBAL_LOCK_TIMEOUT_FOR_SYNC)) {
try {
secStorageVm = startSecStorageVm(secStorageVmId);
} finally {
secStorageVmLock.unlock();
}
} else {
if (s_logger.isInfoEnabled()) {
s_logger.info("Unable to acquire synchronization lock to start secStorageVm for standby capacity, secStorageVm vm id : " + secStorageVm.getId());
}
return;
}
} finally {
secStorageVmLock.releaseRef();
}
if (secStorageVm == null) {
if (s_logger.isInfoEnabled()) {
s_logger.info("Unable to start secondary storage vm for standby capacity, secStorageVm vm Id : " + secStorageVmId + ", will recycle it and start a new one");
}
if (secStorageVmFromStoppedPool) {
destroySecStorageVm(secStorageVmId);
}
} else {
if (s_logger.isInfoEnabled()) {
s_logger.info("Secondary storage vm " + secStorageVm.getHostName() + " is started");
}
}
}
}
public boolean isZoneReady(Map<Long, ZoneHostInfo> zoneHostInfoMap, long dataCenterId) {
ZoneHostInfo zoneHostInfo = zoneHostInfoMap.get(dataCenterId);
if (zoneHostInfo != null && (zoneHostInfo.getFlags() & RunningHostInfoAgregator.ZoneHostInfo.ROUTING_HOST_MASK) != 0) {
VMTemplateVO template = _templateDao.findSystemVMTemplate(dataCenterId);
HostVO secHost = _ssvmMgr.findSecondaryStorageHost(dataCenterId);
if (secHost == null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("No secondary storage available in zone " + dataCenterId + ", wait until it is ready to launch secondary storage vm");
}
return false;
}
boolean templateReady = false;
if (template != null) {
VMTemplateHostVO templateHostRef = _vmTemplateHostDao.findByHostTemplate(secHost.getId(), template.getId());
templateReady = (templateHostRef != null) && (templateHostRef.getDownloadState() == Status.DOWNLOADED);
}
if (templateReady) {
List<Pair<Long, Integer>> l = _storagePoolHostDao.getDatacenterStoragePoolHostInfo(dataCenterId, !_useLocalStorage);
if (l != null && l.size() > 0 && l.get(0).second().intValue() > 0) {
return true;
} else {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Primary storage is not ready, wait until it is ready to launch secondary storage vm. dcId: " + dataCenterId + " system.vm.use.local.storage: " + _useLocalStorage +
"If you want to use local storage to start ssvm, need to set system.vm.use.local.storage to true");
}
}
} else {
if (s_logger.isDebugEnabled()) {
if (template == null) {
s_logger.debug("Zone host is ready, but secondary storage vm template does not exist");
} else {
s_logger.debug("Zone host is ready, but secondary storage vm template: " + template.getId() + " is not ready on secondary storage: " + secHost.getId());
}
}
}
}
return false;
}
private synchronized Map<Long, ZoneHostInfo> getZoneHostInfo() {
Date cutTime = DateUtil.currentGMTTime();
List<RunningHostCountInfo> l = _hostDao.getRunningHostCounts(new Date(cutTime.getTime() - _clusterMgr.getHeartbeatThreshold()));
RunningHostInfoAgregator aggregator = new RunningHostInfoAgregator();
if (l.size() > 0) {
for (RunningHostCountInfo countInfo : l) {
aggregator.aggregate(countInfo);
}
}
return aggregator.getZoneHostInfoMap();
}
@Override
public boolean start() {
if (s_logger.isInfoEnabled()) {
s_logger.info("Start secondary storage vm manager");
}
return true;
}
@Override
public boolean stop() {
_loadScanner.stop();
_allocLock.releaseRef();
_resourceMgr.unregisterResourceStateAdapter(this.getClass().getSimpleName());
return true;
}
@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
if (s_logger.isInfoEnabled()) {
s_logger.info("Start configuring secondary storage vm manager : " + name);
}
Map<String, String> configs = _configDao.getConfiguration("management-server", params);
_secStorageVmMtuSize = NumbersUtil.parseInt(configs.get("secstorage.vm.mtu.size"), DEFAULT_SS_VM_MTUSIZE);
String useServiceVM = _configDao.getValue("secondary.storage.vm");
boolean _useServiceVM = false;
if ("true".equalsIgnoreCase(useServiceVM)) {
_useServiceVM = true;
}
String sslcopy = _configDao.getValue("secstorage.encrypt.copy");
if ("true".equalsIgnoreCase(sslcopy)) {
_useSSlCopy = true;
}
_allowedInternalSites = _configDao.getValue("secstorage.allowed.internal.sites");
String value = configs.get("secstorage.capacityscan.interval");
_capacityScanInterval = NumbersUtil.parseLong(value, DEFAULT_CAPACITY_SCAN_INTERVAL);
_instance = configs.get("instance.name");
if (_instance == null) {
_instance = "DEFAULT";
}
Map<String, String> agentMgrConfigs = _configDao.getConfiguration("AgentManager", params);
_mgmt_host = agentMgrConfigs.get("host");
if (_mgmt_host == null) {
s_logger.warn("Critical warning! Please configure your management server host address right after you have started your management server and then restart it, otherwise you won't have access to secondary storage");
}
value = agentMgrConfigs.get("port");
_mgmt_port = NumbersUtil.parseInt(value, 8250);
_listener = new SecondaryStorageListener(this, _agentMgr);
_agentMgr.registerForHostEvents(_listener, true, false, true);
_itMgr.registerGuru(VirtualMachine.Type.SecondaryStorageVm, this);
//check if there is a default service offering configured
String ssvmSrvcOffIdStr = configs.get(Config.SecondaryStorageServiceOffering.key());
if (ssvmSrvcOffIdStr != null) {
Long ssvmSrvcOffId = Long.parseLong(ssvmSrvcOffIdStr);
_serviceOffering = _offeringDao.findById(ssvmSrvcOffId);
if (_serviceOffering == null || !_serviceOffering.getSystemUse()) {
String msg = "Can't find system service offering id=" + ssvmSrvcOffId + " for secondary storage vm";
s_logger.error(msg);
throw new ConfigurationException(msg);
}
} else {
int ramSize = NumbersUtil.parseInt(_configDao.getValue("ssvm.ram.size"), DEFAULT_SS_VM_RAMSIZE);
int cpuFreq = NumbersUtil.parseInt(_configDao.getValue("ssvm.cpu.mhz"), DEFAULT_SS_VM_CPUMHZ);
_useLocalStorage = Boolean.parseBoolean(configs.get(Config.SystemVMUseLocalStorage.key()));
_serviceOffering = new ServiceOfferingVO("System Offering For Secondary Storage VM", 1, ramSize, cpuFreq, null, null, false, null, _useLocalStorage, true, null, true, VirtualMachine.Type.SecondaryStorageVm, true);
_serviceOffering.setUniqueName(ServiceOffering.ssvmDefaultOffUniqueName);
_serviceOffering = _offeringDao.persistSystemServiceOffering(_serviceOffering);
// this can sometimes happen, if DB is manually or programmatically manipulated
if(_serviceOffering == null) {
String msg = "Data integrity problem : System Offering For Secondary Storage VM has been removed?";
s_logger.error(msg);
throw new ConfigurationException(msg);
}
}
if (_useServiceVM) {
_loadScanner = new SystemVmLoadScanner<Long>(this);
_loadScanner.initScan(STARTUP_DELAY, _capacityScanInterval);
}
_httpProxy = configs.get(Config.SecStorageProxy.key());
if (_httpProxy != null) {
boolean valid = true;
String errMsg = null;
try {
URI uri = new URI(_httpProxy);
if (!"http".equalsIgnoreCase(uri.getScheme())) {
errMsg = "Only support http proxy";
valid = false;
} else if (uri.getHost() == null) {
errMsg = "host can not be null";
valid = false;
} else if (uri.getPort() == -1) {
_httpProxy = _httpProxy + ":3128";
}
} catch (URISyntaxException e) {
errMsg = e.toString();
} finally {
if (!valid) {
s_logger.debug("ssvm http proxy " + _httpProxy + " is invalid: " + errMsg);
throw new ConfigurationException("ssvm http proxy " + _httpProxy + "is invalid: " + errMsg);
}
}
}
if (s_logger.isInfoEnabled()) {
s_logger.info("Secondary storage vm Manager is configured.");
}
_resourceMgr.registerResourceStateAdapter(this.getClass().getSimpleName(), this);
return true;
}
@Override
public Long convertToId(String vmName) {
if (!VirtualMachineName.isValidSystemVmName(vmName, _instance, "s")) {
return null;
}
return VirtualMachineName.getSystemVmId(vmName);
}
@Override
public boolean stopSecStorageVm(long secStorageVmId) {
SecondaryStorageVmVO secStorageVm = _secStorageVmDao.findById(secStorageVmId);
if (secStorageVm == null) {
String msg = "Stopping secondary storage vm failed: secondary storage vm " + secStorageVmId + " no longer exists";
if (s_logger.isDebugEnabled()) {
s_logger.debug(msg);
}
return false;
}
try {
if (secStorageVm.getHostId() != null) {
GlobalLock secStorageVmLock = GlobalLock.getInternLock(getSecStorageVmLockName(secStorageVm.getId()));
try {
if (secStorageVmLock.lock(ACQUIRE_GLOBAL_LOCK_TIMEOUT_FOR_SYNC)) {
try {
boolean result = _itMgr.stop(secStorageVm, _accountMgr.getSystemUser(), _accountMgr.getSystemAccount());
if (result) {
}
return result;
} finally {
secStorageVmLock.unlock();
}
} else {
String msg = "Unable to acquire secondary storage vm lock : " + secStorageVm.toString();
s_logger.debug(msg);
return false;
}
} finally {
secStorageVmLock.releaseRef();
}
}
// vm was already stopped, return true
return true;
} catch (ResourceUnavailableException e) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Stopping secondary storage vm " + secStorageVm.getHostName() + " faled : exception " + e.toString());
}
return false;
}
}
@Override
public boolean rebootSecStorageVm(long secStorageVmId) {
final SecondaryStorageVmVO secStorageVm = _secStorageVmDao.findById(secStorageVmId);
if (secStorageVm == null || secStorageVm.getState() == State.Destroyed) {
return false;
}
if (secStorageVm.getState() == State.Running && secStorageVm.getHostId() != null) {
final RebootCommand cmd = new RebootCommand(secStorageVm.getInstanceName());
final Answer answer = _agentMgr.easySend(secStorageVm.getHostId(), cmd);
if (answer != null && answer.getResult()) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Successfully reboot secondary storage vm " + secStorageVm.getHostName());
}
SubscriptionMgr.getInstance().notifySubscribers(ALERT_SUBJECT, this,
new SecStorageVmAlertEventArgs(SecStorageVmAlertEventArgs.SSVM_REBOOTED, secStorageVm.getDataCenterId(), secStorageVm.getId(), secStorageVm, null));
return true;
} else {
String msg = "Rebooting Secondary Storage VM failed - " + secStorageVm.getHostName();
if (s_logger.isDebugEnabled()) {
s_logger.debug(msg);
}
return false;
}
} else {
return startSecStorageVm(secStorageVmId) != null;
}
}
@Override
public boolean destroySecStorageVm(long vmId) {
SecondaryStorageVmVO ssvm = _secStorageVmDao.findById(vmId);
try {
boolean result = _itMgr.expunge(ssvm, _accountMgr.getSystemUser(), _accountMgr.getSystemAccount());
if (result) {
HostVO host = _hostDao.findByTypeNameAndZoneId(ssvm.getDataCenterId(), ssvm.getHostName(),
Host.Type.SecondaryStorageVM);
if (host != null) {