42
42
43
43
import javax .inject .Inject ;
44
44
import javax .naming .ConfigurationException ;
45
- import javax .persistence .EntityExistsException ;
46
45
47
46
import org .apache .cloudstack .affinity .dao .AffinityGroupVMMapDao ;
48
47
import org .apache .cloudstack .annotation .AnnotationService ;
@@ -4005,7 +4004,7 @@ public NicProfile addVmToNetwork(final VirtualMachine vm, final Network network,
4005
4004
if (jobContext .isJobDispatchedBy (VmWorkConstants .VM_WORK_JOB_DISPATCHER )) {
4006
4005
// avoid re-entrance
4007
4006
VmWorkJobVO placeHolder = null ;
4008
- placeHolder = createPlaceHolderWork (vm .getId (), network . getUuid () );
4007
+ placeHolder = createPlaceHolderWork (vm .getId ());
4009
4008
try {
4010
4009
return orchestrateAddVmToNetwork (vm , network , requested );
4011
4010
} finally {
@@ -4045,23 +4044,10 @@ public NicProfile addVmToNetwork(final VirtualMachine vm, final Network network,
4045
4044
}
4046
4045
}
4047
4046
4048
- /**
4049
- * duplicated in {@see UserVmManagerImpl} for a {@see UserVmVO}
4050
- */
4051
- private void checkIfNetworkExistsForVM (VirtualMachine virtualMachine , Network network ) {
4052
- List <NicVO > allNics = _nicsDao .listByVmId (virtualMachine .getId ());
4053
- for (NicVO nic : allNics ) {
4054
- if (nic .getNetworkId () == network .getId ()) {
4055
- throw new CloudRuntimeException ("A NIC already exists for VM:" + virtualMachine .getInstanceName () + " in network: " + network .getUuid ());
4056
- }
4057
- }
4058
- }
4059
-
4060
4047
private NicProfile orchestrateAddVmToNetwork (final VirtualMachine vm , final Network network , final NicProfile requested ) throws ConcurrentOperationException , ResourceUnavailableException ,
4061
4048
InsufficientCapacityException {
4062
4049
final CallContext cctx = CallContext .current ();
4063
4050
4064
- checkIfNetworkExistsForVM (vm , network );
4065
4051
s_logger .debug ("Adding vm " + vm + " to network " + network + "; requested nic profile " + requested );
4066
4052
final VMInstanceVO vmVO = _vmDao .findById (vm .getId ());
4067
4053
final ReservationContext context = new ReservationContextImpl (null , null , cctx .getCallingUser (), cctx .getCallingAccount ());
@@ -5412,7 +5398,7 @@ public Outcome<VirtualMachine> migrateVmThroughJobQueue(final String vmUuid, fin
5412
5398
Map <Volume , StoragePool > volumeStorageMap = dest .getStorageForDisks ();
5413
5399
if (volumeStorageMap != null ) {
5414
5400
for (Volume vol : volumeStorageMap .keySet ()) {
5415
- checkConcurrentJobsPerDatastoreThreshold (volumeStorageMap .get (vol ));
5401
+ checkConcurrentJobsPerDatastoreThreshhold (volumeStorageMap .get (vol ));
5416
5402
}
5417
5403
}
5418
5404
@@ -5577,7 +5563,7 @@ public Outcome<VirtualMachine> migrateVmForScaleThroughJobQueue(
5577
5563
return new VmJobVirtualMachineOutcome (workJob , vm .getId ());
5578
5564
}
5579
5565
5580
- private void checkConcurrentJobsPerDatastoreThreshold (final StoragePool destPool ) {
5566
+ private void checkConcurrentJobsPerDatastoreThreshhold (final StoragePool destPool ) {
5581
5567
final Long threshold = VolumeApiService .ConcurrentMigrationsThresholdPerDatastore .value ();
5582
5568
if (threshold != null && threshold > 0 ) {
5583
5569
long count = _jobMgr .countPendingJobs ("\" storageid\" :\" " + destPool .getUuid () + "\" " , MigrateVMCmd .class .getName (), MigrateVolumeCmd .class .getName (), MigrateVolumeCmdByAdmin .class .getName ());
@@ -5598,7 +5584,7 @@ public Outcome<VirtualMachine> migrateVmStorageThroughJobQueue(
5598
5584
Set <Long > uniquePoolIds = new HashSet <>(poolIds );
5599
5585
for (Long poolId : uniquePoolIds ) {
5600
5586
StoragePoolVO pool = _storagePoolDao .findById (poolId );
5601
- checkConcurrentJobsPerDatastoreThreshold (pool );
5587
+ checkConcurrentJobsPerDatastoreThreshhold (pool );
5602
5588
}
5603
5589
5604
5590
final VMInstanceVO vm = _vmDao .findByUuid (vmUuid );
@@ -5645,61 +5631,35 @@ public Outcome<VirtualMachine> addVmToNetworkThroughJobQueue(
5645
5631
5646
5632
final List <VmWorkJobVO > pendingWorkJobs = _workJobDao .listPendingWorkJobs (
5647
5633
VirtualMachine .Type .Instance , vm .getId (),
5648
- VmWorkAddVmToNetwork .class .getName (), network . getUuid () );
5634
+ VmWorkAddVmToNetwork .class .getName ());
5649
5635
5650
5636
VmWorkJobVO workJob = null ;
5651
5637
if (pendingWorkJobs != null && pendingWorkJobs .size () > 0 ) {
5652
- if (pendingWorkJobs .size () > 1 ) {
5653
- s_logger .warn (String .format ("The number of jobs to add network %s to vm %s are %d" , network .getUuid (), vm .getInstanceName (), pendingWorkJobs .size ()));
5654
- }
5638
+ assert pendingWorkJobs .size () == 1 ;
5655
5639
workJob = pendingWorkJobs .get (0 );
5656
5640
} else {
5657
- if (s_logger .isTraceEnabled ()) {
5658
- s_logger .trace (String .format ("no jobs to add network %s for vm %s yet" , network , vm ));
5659
- }
5660
5641
5661
- workJob = createVmWorkJobToAddNetwork (vm , network , requested , context , user , account );
5662
- }
5663
- AsyncJobExecutionContext .getCurrentExecutionContext ().joinJob (workJob .getId ());
5664
-
5665
- return new VmJobVirtualMachineOutcome (workJob , vm .getId ());
5666
- }
5667
-
5668
- private VmWorkJobVO createVmWorkJobToAddNetwork (
5669
- VirtualMachine vm ,
5670
- Network network ,
5671
- NicProfile requested ,
5672
- CallContext context ,
5673
- User user ,
5674
- Account account ) {
5675
- VmWorkJobVO workJob ;
5676
- workJob = new VmWorkJobVO (context .getContextId ());
5642
+ workJob = new VmWorkJobVO (context .getContextId ());
5677
5643
5678
- workJob .setDispatcher (VmWorkConstants .VM_WORK_JOB_DISPATCHER );
5679
- workJob .setCmd (VmWorkAddVmToNetwork .class .getName ());
5644
+ workJob .setDispatcher (VmWorkConstants .VM_WORK_JOB_DISPATCHER );
5645
+ workJob .setCmd (VmWorkAddVmToNetwork .class .getName ());
5680
5646
5681
- workJob .setAccountId (account .getId ());
5682
- workJob .setUserId (user .getId ());
5683
- workJob .setVmType (VirtualMachine .Type .Instance );
5684
- workJob .setVmInstanceId (vm .getId ());
5685
- workJob .setRelated (AsyncJobExecutionContext .getOriginJobId ());
5686
- workJob .setSecondaryObjectIdentifier (network .getUuid ());
5647
+ workJob .setAccountId (account .getId ());
5648
+ workJob .setUserId (user .getId ());
5649
+ workJob .setVmType (VirtualMachine .Type .Instance );
5650
+ workJob .setVmInstanceId (vm .getId ());
5651
+ workJob .setRelated (AsyncJobExecutionContext .getOriginJobId ());
5687
5652
5688
- // save work context info (there are some duplications)
5689
- final VmWorkAddVmToNetwork workInfo = new VmWorkAddVmToNetwork (user .getId (), account .getId (), vm .getId (),
5690
- VirtualMachineManagerImpl .VM_WORK_JOB_HANDLER , network .getId (), requested );
5691
- workJob .setCmdInfo (VmWorkSerializer .serialize (workInfo ));
5653
+ // save work context info (there are some duplications)
5654
+ final VmWorkAddVmToNetwork workInfo = new VmWorkAddVmToNetwork (user .getId (), account .getId (), vm .getId (),
5655
+ VirtualMachineManagerImpl .VM_WORK_JOB_HANDLER , network .getId (), requested );
5656
+ workJob .setCmdInfo (VmWorkSerializer .serialize (workInfo ));
5692
5657
5693
- try {
5694
5658
_jobMgr .submitAsyncJob (workJob , VmWorkConstants .VM_WORK_QUEUE , vm .getId ());
5695
- } catch (CloudRuntimeException e ) {
5696
- if (e .getCause () instanceof EntityExistsException ) {
5697
- String msg = String .format ("A job to add a nic for network %s to vm %s already exists" , network .getUuid (), vm .getUuid ());
5698
- s_logger .warn (msg , e );
5699
- }
5700
- throw e ;
5701
5659
}
5702
- return workJob ;
5660
+ AsyncJobExecutionContext .getCurrentExecutionContext ().joinJob (workJob .getId ());
5661
+
5662
+ return new VmJobVirtualMachineOutcome (workJob , vm .getId ());
5703
5663
}
5704
5664
5705
5665
public Outcome <VirtualMachine > removeNicFromVmThroughJobQueue (
@@ -6008,10 +5968,6 @@ public Pair<JobInfo.Status, String> handleVmWorkJob(final VmWork work) throws Ex
6008
5968
}
6009
5969
6010
5970
private VmWorkJobVO createPlaceHolderWork (final long instanceId ) {
6011
- return createPlaceHolderWork (instanceId , null );
6012
- }
6013
-
6014
- private VmWorkJobVO createPlaceHolderWork (final long instanceId , String secondaryObjectIdentifier ) {
6015
5971
final VmWorkJobVO workJob = new VmWorkJobVO ("" );
6016
5972
6017
5973
workJob .setDispatcher (VmWorkConstants .VM_WORK_JOB_PLACEHOLDER );
@@ -6023,9 +5979,6 @@ private VmWorkJobVO createPlaceHolderWork(final long instanceId, String secondar
6023
5979
workJob .setStep (VmWorkJobVO .Step .Starting );
6024
5980
workJob .setVmType (VirtualMachine .Type .Instance );
6025
5981
workJob .setVmInstanceId (instanceId );
6026
- if (StringUtils .isNotBlank (secondaryObjectIdentifier )) {
6027
- workJob .setSecondaryObjectIdentifier (secondaryObjectIdentifier );
6028
- }
6029
5982
workJob .setInitMsid (ManagementServerNode .getManagementServerId ());
6030
5983
6031
5984
_workJobDao .persist (workJob );
0 commit comments