Skip to content

Commit 5774b96

Browse files
committed
Merge pull request apache#1209 from ustcweizhou/free-deviceid
CLOUDSTACK-9134: set device_id as the first device_id not in use instead of nic count when we restart vpc tiers, the old nics will be removed, and create a new nic. however, the device_id was set to the nic count, which may be already used. this commit get the first device_id not in use as the device_id of new nic. This issue also happen when we add multiple networks to a vm and remove them. * pr/1209: CLOUDSTACK-9134: set device_id as the first device_id not in use instead of nic count Signed-off-by: Daan Hoogland <daan@onecht.net>
2 parents d7b7171 + acfc19d commit 5774b96

3 files changed

Lines changed: 22 additions & 13 deletions

File tree

engine/orchestration/src/org/apache/cloudstack/engine/orchestration/NetworkOrchestrator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3150,7 +3150,7 @@ public NicProfile createNicForVm(Network network, NicProfile requested, Reservat
31503150

31513151
//1) allocate nic (if needed) Always allocate if it is a user vm
31523152
if (nic == null || (vmProfile.getType() == VirtualMachine.Type.User)) {
3153-
int deviceId = _nicDao.countNics(vm.getId());
3153+
int deviceId = _nicDao.getFreeDeviceId(vm.getId());
31543154

31553155
nic = allocateNic(requested, network, false, deviceId, vmProfile).first();
31563156

engine/schema/src/com/cloud/vm/dao/NicDao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public interface NicDao extends GenericDao<NicVO, Long> {
5555

5656
String getIpAddress(long networkId, long instanceId);
5757

58-
int countNics(long instanceId);
58+
int getFreeDeviceId(long instanceId);
5959

6060
NicVO findByNetworkIdInstanceIdAndBroadcastUri(long networkId, long instanceId, String broadcastUri);
6161

engine/schema/src/com/cloud/vm/dao/NicDaoImpl.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import org.springframework.stereotype.Component;
2727

28+
import com.cloud.utils.db.Filter;
2829
import com.cloud.utils.db.GenericDaoBase;
2930
import com.cloud.utils.db.GenericSearchBuilder;
3031
import com.cloud.utils.db.JoinBuilder;
@@ -44,7 +45,7 @@ public class NicDaoImpl extends GenericDaoBase<NicVO, Long> implements NicDao {
4445
private SearchBuilder<NicVO> AllFieldsSearch;
4546
private GenericSearchBuilder<NicVO, String> IpSearch;
4647
private SearchBuilder<NicVO> NonReleasedSearch;
47-
private GenericSearchBuilder<NicVO, Integer> CountBy;
48+
private GenericSearchBuilder<NicVO, Integer> deviceIdSearch;
4849
private GenericSearchBuilder<NicVO, Integer> CountByForStartingVms;
4950

5051
@Inject
@@ -81,11 +82,10 @@ protected void init() {
8182
NonReleasedSearch.and("state", NonReleasedSearch.entity().getState(), Op.NOTIN);
8283
NonReleasedSearch.done();
8384

84-
CountBy = createSearchBuilder(Integer.class);
85-
CountBy.select(null, Func.COUNT, CountBy.entity().getId());
86-
CountBy.and("vmId", CountBy.entity().getInstanceId(), Op.EQ);
87-
CountBy.and("removed", CountBy.entity().getRemoved(), Op.NULL);
88-
CountBy.done();
85+
deviceIdSearch = createSearchBuilder(Integer.class);
86+
deviceIdSearch.select(null, Func.DISTINCT, deviceIdSearch.entity().getDeviceId());
87+
deviceIdSearch.and("instance", deviceIdSearch.entity().getInstanceId(), Op.EQ);
88+
deviceIdSearch.done();
8989

9090
CountByForStartingVms = createSearchBuilder(Integer.class);
9191
CountByForStartingVms.select(null, Func.COUNT, CountByForStartingVms.entity().getId());
@@ -222,11 +222,20 @@ public String getIpAddress(long networkId, long instanceId) {
222222
}
223223

224224
@Override
225-
public int countNics(long instanceId) {
226-
SearchCriteria<Integer> sc = CountBy.create();
227-
sc.setParameters("vmId", instanceId);
228-
List<Integer> results = customSearch(sc, null);
229-
return results.get(0);
225+
public int getFreeDeviceId(long instanceId) {
226+
Filter searchFilter = new Filter(NicVO.class, "deviceId", true, null, null);
227+
SearchCriteria<Integer> sc = deviceIdSearch.create();
228+
sc.setParameters("instance", instanceId);
229+
List<Integer> deviceIds = customSearch(sc, searchFilter);
230+
231+
int freeDeviceId = 0;
232+
for (int deviceId : deviceIds) {
233+
if (deviceId > freeDeviceId)
234+
break;
235+
freeDeviceId ++;
236+
}
237+
238+
return freeDeviceId;
230239
}
231240

232241
@Override

0 commit comments

Comments
 (0)