Skip to content

Commit d6df2fd

Browse files
author
Alena Prokharchyk
committed
VPC: createPrivateGateway - added cleanup part happening in case when the gateway fails to be applied on the backend
Conflicts: api/src/com/cloud/api/commands/CreatePrivateGatewayCmd.java api/src/com/cloud/api/response/AsyncJobResponse.java server/src/com/cloud/network/vpc/VpcManagerImpl.java
1 parent 0588c97 commit d6df2fd

4 files changed

Lines changed: 43 additions & 24 deletions

File tree

‎api/src/com/cloud/api/commands/CreatePrivateGatewayCmd.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ public void create() throws ResourceAllocationException {
129129

130130
@Override
131131
public void execute() throws InsufficientCapacityException, ConcurrentOperationException,
132-
ResourceAllocationException, ResourceUnavailableException {
133-
PrivateGateway result = _vpcService.applyVpcPrivateGateway(getEntityId());
132+
ResourceAllocationException, ResourceUnavailableException {
133+
PrivateGateway result = _vpcService.applyVpcPrivateGateway(getEntityId(), true);
134134
if (result != null) {
135135
PrivateGatewayResponse response = _responseGenerator.createPrivateGatewayResponse(result);
136136
response.setResponseName(getCommandName());

‎api/src/com/cloud/api/response/AsyncJobResponse.java‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818

1919
import java.util.Date;
2020

21-
import org.apache.log4j.Logger;
22-
2321
import com.cloud.api.ApiConstants;
24-
import com.cloud.utils.IdentityProxy;
2522
import com.cloud.api.ResponseObject;
2623
import com.cloud.async.AsyncJob;
2724
import com.cloud.serializer.Param;
25+
import com.cloud.utils.IdentityProxy;
2826
import com.google.gson.annotations.SerializedName;
2927

3028
@SuppressWarnings("unused")
@@ -129,6 +127,10 @@ public void setJobInstanceType(String jobInstanceType) {
129127
this.jobInstanceId.setTableName("account");
130128
} else if (jobInstanceType.equalsIgnoreCase(AsyncJob.Type.User.toString())) {
131129
this.jobInstanceId.setTableName("user");
130+
} else if (jobInstanceType.equalsIgnoreCase(AsyncJob.Type.StaticRoute.toString())) {
131+
this.jobInstanceId.setTableName("static_routes");
132+
} else if (jobInstanceType.equalsIgnoreCase(AsyncJob.Type.PrivateGateway.toString())) {
133+
this.jobInstanceId.setTableName("vpc_gateways");
132134
} else if (!jobInstanceType.equalsIgnoreCase(AsyncJob.Type.None.toString())){
133135
// TODO : when we hit here, we need to add instanceType -> UUID entity table mapping
134136
assert(false);

‎api/src/com/cloud/network/vpc/VpcService.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,19 +169,20 @@ public PrivateGateway createVpcPrivateGateway(long vpcId, Long physicalNetworkId
169169

170170
/**
171171
* @param gatewayId
172+
* @param destroyOnFailure TODO
172173
* @return
173174
* @throws ResourceUnavailableException
174175
* @throws ConcurrentOperationException
175176
*/
176-
public PrivateGateway applyVpcPrivateGateway(Long gatewayId) throws ConcurrentOperationException, ResourceUnavailableException;
177+
public PrivateGateway applyVpcPrivateGateway(long gatewayId, boolean destroyOnFailure) throws ConcurrentOperationException, ResourceUnavailableException;
177178

178179
/**
179180
* @param id
180181
* @return
181182
* @throws ResourceUnavailableException
182183
* @throws ConcurrentOperationException
183184
*/
184-
boolean deleteVpcPrivateGateway(Long gatewayId) throws ConcurrentOperationException, ResourceUnavailableException;
185+
boolean deleteVpcPrivateGateway(long gatewayId) throws ConcurrentOperationException, ResourceUnavailableException;
185186

186187
/**
187188
* @param listPrivateGatewaysCmd

‎server/src/com/cloud/network/vpc/VpcManagerImpl.java‎

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,11 @@ public void validateNtkwOffForVpc(long ntwkOffId, String cidr, String networkDom
10011001

10021002
}
10031003

1004+
@DB
10041005
protected void validateNewVpcGuestNetwork(String cidr, String gateway, Account networkOwner, Vpc vpc, String networkDomain) {
1006+
1007+
Transaction txn = Transaction.currentTxn();
1008+
txn.start();
10051009
Vpc locked = _vpcDao.acquireInLockTable(vpc.getId());
10061010
if (locked == null) {
10071011
throw new CloudRuntimeException("Unable to acquire lock on " + vpc);
@@ -1053,7 +1057,8 @@ protected void validateNewVpcGuestNetwork(String cidr, String gateway, Account n
10531057
if (NetUtils.getCidrSubNet(cidr).equalsIgnoreCase(gateway)) {
10541058
throw new InvalidParameterValueException("Invalid gateway specified. It should never be equal to the cidr subnet value");
10551059
}
1056-
1060+
1061+
txn.commit();
10571062
} finally {
10581063
s_logger.debug("Releasing lock for " + locked);
10591064
_vpcDao.releaseFromLockTable(locked.getId());
@@ -1243,16 +1248,14 @@ public PrivateGateway createVpcPrivateGateway(long vpcId, Long physicalNetworkId
12431248

12441249

12451250
@Override
1246-
@DB
1247-
public PrivateGateway applyVpcPrivateGateway(Long gatewayId) throws ConcurrentOperationException, ResourceUnavailableException {
1248-
VpcGatewayVO vo = _vpcGatewayDao.acquireInLockTable(gatewayId);
1249-
if (vo == null) {
1250-
throw new ConcurrentOperationException("Unable to lock gateway " + gatewayId);
1251-
}
1252-
1251+
public PrivateGateway applyVpcPrivateGateway(long gatewayId, boolean destroyOnFailure) throws ConcurrentOperationException, ResourceUnavailableException {
1252+
VpcGatewayVO vo = _vpcGatewayDao.findById(gatewayId);
1253+
1254+
boolean success = false;
12531255
try {
12541256
PrivateGateway gateway = getVpcPrivateGateway(gatewayId);
1255-
if (getVpcElement().createPrivateGateway(gateway)) {
1257+
success = getVpcElement().createPrivateGateway(gateway);
1258+
if (success) {
12561259
s_logger.debug("Private gateway " + gateway + " was applied succesfully on the backend");
12571260
if (vo.getState() != VpcGateway.State.Ready) {
12581261
vo.setState(VpcGateway.State.Ready);
@@ -1265,24 +1268,33 @@ public PrivateGateway applyVpcPrivateGateway(Long gatewayId) throws ConcurrentOp
12651268
return null;
12661269
}
12671270
} finally {
1268-
if (vo != null) {
1269-
_vpcGatewayDao.releaseFromLockTable(gatewayId);
1271+
//do cleanup
1272+
if (!success) {
1273+
if (destroyOnFailure) {
1274+
s_logger.debug("Destroying private gateway " + vo + " that failed to start");
1275+
if (deleteVpcPrivateGateway(gatewayId)) {
1276+
s_logger.warn("Successfully destroyed vpc " + vo + " that failed to start");
1277+
} else {
1278+
s_logger.warn("Failed to destroy vpc " + vo + " that failed to start");
1279+
}
1280+
}
12701281
}
1271-
}
1282+
}
12721283
}
12731284

12741285
@Override
12751286
@ActionEvent(eventType = EventTypes.EVENT_PRIVATE_GATEWAY_DELETE, eventDescription = "deleting private gateway")
12761287
@DB
1277-
public boolean deleteVpcPrivateGateway(Long gatewayId) throws ConcurrentOperationException, ResourceUnavailableException {
1288+
public boolean deleteVpcPrivateGateway(long gatewayId) throws ConcurrentOperationException, ResourceUnavailableException {
1289+
1290+
Transaction txn = Transaction.currentTxn();
1291+
txn.start();
12781292
VpcGatewayVO gatewayVO = _vpcGatewayDao.acquireInLockTable(gatewayId);
12791293
if (gatewayVO == null || gatewayVO.getType() != VpcGateway.Type.Private) {
12801294
throw new ConcurrentOperationException("Unable to lock gateway " + gatewayId);
12811295
}
1282-
1283-
try {
1284-
_vpcGatewayDao.update(gatewayVO.getId(), gatewayVO);
1285-
s_logger.debug("Marked gateway " + gatewayVO + " with state " + VpcGateway.State.Deleting);
1296+
1297+
try {
12861298
//don't allow to remove gateway when there are static routes associated with it
12871299
long routeCount = _staticRouteDao.countRoutesByGateway(gatewayVO.getId());
12881300
if (routeCount > 0) {
@@ -1291,7 +1303,11 @@ public boolean deleteVpcPrivateGateway(Long gatewayId) throws ConcurrentOperatio
12911303
}
12921304

12931305
gatewayVO.setState(VpcGateway.State.Deleting);
1306+
_vpcGatewayDao.update(gatewayVO.getId(), gatewayVO);
1307+
s_logger.debug("Marked gateway " + gatewayVO + " with state " + VpcGateway.State.Deleting);
12941308

1309+
txn.commit();
1310+
12951311
//1) delete the gateway on the backend
12961312
PrivateGateway gateway = getVpcPrivateGateway(gatewayId);
12971313
if (getVpcElement().deletePrivateGateway(gateway)) {

0 commit comments

Comments
 (0)