4242
4343import javax .inject .Inject ;
4444import javax .naming .ConfigurationException ;
45- import javax .persistence .EntityExistsException ;
4645
4746import org .apache .cloudstack .affinity .dao .AffinityGroupVMMapDao ;
4847import org .apache .cloudstack .annotation .AnnotationService ;
@@ -4005,7 +4004,7 @@ public NicProfile addVmToNetwork(final VirtualMachine vm, final Network network,
40054004 if (jobContext .isJobDispatchedBy (VmWorkConstants .VM_WORK_JOB_DISPATCHER )) {
40064005 // avoid re-entrance
40074006 VmWorkJobVO placeHolder = null ;
4008- placeHolder = createPlaceHolderWork (vm .getId (), network . getUuid () );
4007+ placeHolder = createPlaceHolderWork (vm .getId ());
40094008 try {
40104009 return orchestrateAddVmToNetwork (vm , network , requested );
40114010 } finally {
@@ -4045,23 +4044,10 @@ public NicProfile addVmToNetwork(final VirtualMachine vm, final Network network,
40454044 }
40464045 }
40474046
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-
40604047 private NicProfile orchestrateAddVmToNetwork (final VirtualMachine vm , final Network network , final NicProfile requested ) throws ConcurrentOperationException , ResourceUnavailableException ,
40614048 InsufficientCapacityException {
40624049 final CallContext cctx = CallContext .current ();
40634050
4064- checkIfNetworkExistsForVM (vm , network );
40654051 s_logger .debug ("Adding vm " + vm + " to network " + network + "; requested nic profile " + requested );
40664052 final VMInstanceVO vmVO = _vmDao .findById (vm .getId ());
40674053 final ReservationContext context = new ReservationContextImpl (null , null , cctx .getCallingUser (), cctx .getCallingAccount ());
@@ -5412,7 +5398,7 @@ public Outcome<VirtualMachine> migrateVmThroughJobQueue(final String vmUuid, fin
54125398 Map <Volume , StoragePool > volumeStorageMap = dest .getStorageForDisks ();
54135399 if (volumeStorageMap != null ) {
54145400 for (Volume vol : volumeStorageMap .keySet ()) {
5415- checkConcurrentJobsPerDatastoreThreshold (volumeStorageMap .get (vol ));
5401+ checkConcurrentJobsPerDatastoreThreshhold (volumeStorageMap .get (vol ));
54165402 }
54175403 }
54185404
@@ -5577,7 +5563,7 @@ public Outcome<VirtualMachine> migrateVmForScaleThroughJobQueue(
55775563 return new VmJobVirtualMachineOutcome (workJob , vm .getId ());
55785564 }
55795565
5580- private void checkConcurrentJobsPerDatastoreThreshold (final StoragePool destPool ) {
5566+ private void checkConcurrentJobsPerDatastoreThreshhold (final StoragePool destPool ) {
55815567 final Long threshold = VolumeApiService .ConcurrentMigrationsThresholdPerDatastore .value ();
55825568 if (threshold != null && threshold > 0 ) {
55835569 long count = _jobMgr .countPendingJobs ("\" storageid\" :\" " + destPool .getUuid () + "\" " , MigrateVMCmd .class .getName (), MigrateVolumeCmd .class .getName (), MigrateVolumeCmdByAdmin .class .getName ());
@@ -5598,7 +5584,7 @@ public Outcome<VirtualMachine> migrateVmStorageThroughJobQueue(
55985584 Set <Long > uniquePoolIds = new HashSet <>(poolIds );
55995585 for (Long poolId : uniquePoolIds ) {
56005586 StoragePoolVO pool = _storagePoolDao .findById (poolId );
5601- checkConcurrentJobsPerDatastoreThreshold (pool );
5587+ checkConcurrentJobsPerDatastoreThreshhold (pool );
56025588 }
56035589
56045590 final VMInstanceVO vm = _vmDao .findByUuid (vmUuid );
@@ -5645,61 +5631,35 @@ public Outcome<VirtualMachine> addVmToNetworkThroughJobQueue(
56455631
56465632 final List <VmWorkJobVO > pendingWorkJobs = _workJobDao .listPendingWorkJobs (
56475633 VirtualMachine .Type .Instance , vm .getId (),
5648- VmWorkAddVmToNetwork .class .getName (), network . getUuid () );
5634+ VmWorkAddVmToNetwork .class .getName ());
56495635
56505636 VmWorkJobVO workJob = null ;
56515637 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 ;
56555639 workJob = pendingWorkJobs .get (0 );
56565640 } 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- }
56605641
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 ());
56775643
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 ());
56805646
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 ());
56875652
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 ));
56925657
5693- try {
56945658 _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 ;
57015659 }
5702- return workJob ;
5660+ AsyncJobExecutionContext .getCurrentExecutionContext ().joinJob (workJob .getId ());
5661+
5662+ return new VmJobVirtualMachineOutcome (workJob , vm .getId ());
57035663 }
57045664
57055665 public Outcome <VirtualMachine > removeNicFromVmThroughJobQueue (
@@ -6008,10 +5968,6 @@ public Pair<JobInfo.Status, String> handleVmWorkJob(final VmWork work) throws Ex
60085968 }
60095969
60105970 private VmWorkJobVO createPlaceHolderWork (final long instanceId ) {
6011- return createPlaceHolderWork (instanceId , null );
6012- }
6013-
6014- private VmWorkJobVO createPlaceHolderWork (final long instanceId , String secondaryObjectIdentifier ) {
60155971 final VmWorkJobVO workJob = new VmWorkJobVO ("" );
60165972
60175973 workJob .setDispatcher (VmWorkConstants .VM_WORK_JOB_PLACEHOLDER );
@@ -6023,9 +5979,6 @@ private VmWorkJobVO createPlaceHolderWork(final long instanceId, String secondar
60235979 workJob .setStep (VmWorkJobVO .Step .Starting );
60245980 workJob .setVmType (VirtualMachine .Type .Instance );
60255981 workJob .setVmInstanceId (instanceId );
6026- if (StringUtils .isNotBlank (secondaryObjectIdentifier )) {
6027- workJob .setSecondaryObjectIdentifier (secondaryObjectIdentifier );
6028- }
60295982 workJob .setInitMsid (ManagementServerNode .getManagementServerId ());
60305983
60315984 _workJobDao .persist (workJob );
0 commit comments