Skip to content

Commit

Permalink
VSYM-14090: Enumeration deletes resources with no endpointLinks.
Browse files Browse the repository at this point in the history
Change-Id: I272fee3e1040c150521ec2b4643018d3279cc583
  • Loading branch information
Sarvesh Sharma authored and Gerrit Code Review committed Apr 27, 2018
1 parent c2d10ab commit c384343
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import com.vmware.photon.controller.model.PhotonModelServices;
import com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryAdapters;
import com.vmware.photon.controller.model.query.QueryUtils;
import com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType;
import com.vmware.photon.controller.model.resources.ComputeService.ComputeState;
import com.vmware.photon.controller.model.resources.DiskService.DiskState;
import com.vmware.photon.controller.model.resources.EndpointService.EndpointState;
Expand Down Expand Up @@ -590,29 +591,23 @@ private <T> void storeEnumeratedDocumentIdsByQueryingLinks (Set<String> selfLink
* associated with those ids should be the same.
*/
public void assertDocumentCounts() {
Assert.assertTrue("Compute document count mismatch during enumeration",
this.instanceIds.size() == this.computeStateLinks.size());
// If asserting after deleting provisioned instances, there should be compute state links
// with 0 endpoints.
if (this.postDeletion) {
for (String computeLink : this.computeStateLinks) {
verifyEndpointLinksCount(computeLink, 0);
}
} else {
if (!this.postDeletion) {
Assert.assertTrue("Compute document count mismatch during enumeration",
this.instanceIds.size() == this.computeStateLinks.size());
for (String computeLink : this.computeStateLinks) {
verifyEndpointLinksCount(computeLink, 1);
}
}

Assert.assertTrue("Resource pool document count mismatch during enumeration",
getEnumeratedDocumentCountByQueryingIds(this.resourcePoolIds,
ResourcePoolState.class) == this.resourcePoolIds.size());
Assert.assertTrue("Security group document count mismatch during enumeration",
getEnumeratedDocumentCountByQueryingIds(this.securityGroupIds,
SecurityGroupState.class) == this.securityGroupIds.size());
Assert.assertTrue("Subnet document count mismatch during enumeration",
getEnumeratedDocumentCountByQueryingIds(this.subnetIds,
SubnetState.class) == this.subnetIds.size());
Assert.assertTrue("Resource pool document count mismatch during enumeration",
getEnumeratedDocumentCountByQueryingIds(this.resourcePoolIds,
ResourcePoolState.class) == this.resourcePoolIds.size());
Assert.assertTrue("Security group document count mismatch during enumeration",
getEnumeratedDocumentCountByQueryingIds(this.securityGroupIds,
SecurityGroupState.class) == this.securityGroupIds.size());
Assert.assertTrue("Subnet document count mismatch during enumeration",
getEnumeratedDocumentCountByQueryingIds(this.subnetIds,
SubnetState.class) == this.subnetIds.size());
}
}

private void verifyEndpointLinksCount(String selfLink, int endpointCount) {
Expand Down Expand Up @@ -713,12 +708,15 @@ private void validateComputeDescForEndpoint() {
ComputeState computeState = response
.getBody(ComputeState.class);

String descLink = computeState.descriptionLink;
op = Operation.createGet(UriUtils.buildUri(this.host.getUri(), descLink))
.setReferer(this.host.getUri());
response = this.host.waitForResponse(op);
Assert.assertTrue("Error retrieving compute description",
response.getStatusCode() == 200);
// TODO: Compute description links for Zone computes will be missing after one of the endpoints belonging to same HOST is deleted.
if (computeState.type != ComputeType.ZONE) {
String descLink = computeState.descriptionLink;
op = Operation.createGet(UriUtils.buildUri(this.host.getUri(), descLink))
.setReferer(this.host.getUri());
response = this.host.waitForResponse(op);
Assert.assertTrue("Error retrieving compute description",
response.getStatusCode() == 200);
}
}
this.host.log(Level.INFO, "All compute states have valid compute description links");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand Down Expand Up @@ -1140,12 +1141,10 @@ private void validateS3Enumeration(int expectedDiskCount, int expectedTagsCount)
DiskState diskState = Utils.fromJson(response.results.documents
.get(response.results.documentLinks.get(0)), DiskState.class);
assertEquals(expectedTagsCount, diskState.tagLinks.size());
assertEquals(expectedDiskCount, diskState.endpointLinks.size());
} else {
assertEquals(expectedDiskCount, response.results.documents.size());
}

DiskState diskState = Utils.fromJson(response.results.documents
.get(response.results.documentLinks.get(0)), DiskState.class);

assertEquals(expectedDiskCount, diskState.endpointLinks.size());
}

private void validateS3TagStatesCreated() {
Expand Down Expand Up @@ -1402,8 +1401,7 @@ private void validateStaleResourceStateDeletion(String... staleResourceSelfLinks
// do nothing, expected is the resource not to be found
}
//the resourceState will be deleted by the groomer task after disassociation
assertTrue("Stale subnet state should have been disassociated.", resourceState
.endpointLinks.isEmpty());
assertNull("Stale subnet state should have been deleted.", resourceState);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,20 +363,15 @@ private void testImageEnumeration_delete(boolean isPublic, String imageFilter)

// Validate 1 image state is CREATED and the 2 vSphere are UNtouched
int postEnumCount = 1 + 2 + 2;
postEnumCount++; //since we are not deleting stale resource anymore, just disassociating
// them
ServiceDocumentQueryResult imagesAfterEnum = queryDocumentsAndAssertExpectedCount(
getHost(),
postEnumCount,
ImageService.FACTORY_LINK,
EXACT_COUNT);

// Validate 1 stale image state is DISASSOCIATED
ImageState staleImage = Utils.fromJson(
imagesAfterEnum.documents.get(staleImageState.documentSelfLink),
ImageState.class);
Assert.assertTrue("Dummy image should have been disassociated.",
staleImage.endpointLinks.isEmpty());
// Validate 1 stale image state is deleted
Assert.assertNull("Dummy image should have been deleted.",
imagesAfterEnum.documents.get(staleImageState.documentSelfLink));

// Validate vSphere images are untouched
Assert.assertTrue("Private images from other endpoints should not have been deleted.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ private void assertStaleResources() {
// Security Groups
for (int i = 0; i < STALE_SECURITY_GROUPS_COUNT; i++) {
assertResourceDisassociated(this.host, SecurityGroupService.FACTORY_LINK,
STALE_SG_NAME_PREFIX + i, false);
STALE_SG_NAME_PREFIX + i, true);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,20 +350,16 @@ public void testPublicImageEnumeration_delete() throws Throwable {
kickOffImageEnumeration(this.endpointState, PUBLIC, AZURE_SINGLE_IMAGE_FILTER);

// Validate 1 image state is CREATED and the 2 vSphere and 2 diff region are UNtouched
final int postEnumCount = 1 + 2 + 2 + 1; // plus 1 because we are not deleting the
// resource, only disassociating it.
final int postEnumCount = 1 + 2 + 2;
ServiceDocumentQueryResult imagesAfterEnum = queryDocumentsAndAssertExpectedCount(
getHost(),
postEnumCount,
ImageService.FACTORY_LINK,
EXACT_COUNT);

// Validate 1 stale image state is DISASSOCIATED
ImageState staleImage = Utils.fromJson(
imagesAfterEnum.documents.get(staleImageState.documentSelfLink),
ImageState.class);
Assert.assertTrue("Dummy image should have been disassociated.",
staleImage.endpointLinks.isEmpty());
// Validate 1 stale image state is deleted
Assert.assertNull("Dummy image should have been deleted.",
imagesAfterEnum.documents.get(staleImageState.documentSelfLink));

// Validate vSphere images are untouched
Assert.assertTrue("Private images from other endpoints should not have been deleted.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@ private void assertStaleResources() {
// Security Groups
for (int i = 0; i < STALE_SECURITY_GROUPS_COUNT; i++) {
assertResourceDisassociated(this.host, SecurityGroupService.FACTORY_LINK,
STALE_SG_NAME_PREFIX + i, false);
STALE_SG_NAME_PREFIX + i, true);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public void testResourceDeduplication() throws Throwable {
validateEndpointLinks("", this.computeHost.documentSelfLink);

this.endpointState2 = createEndpointState();
this.computeHost = createComputeHostWithDescription();
this.endpointState2.computeLink = this.computeHost.documentSelfLink;
this.host.waitForResponse(Operation.createPatch(this.host, this.endpointState2.documentSelfLink)
.setBody(this.endpointState2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1860,16 +1860,16 @@ public static void assertDiskExist(VerificationHost host, String factoryLink,
* Factory link to the stateful service which states to check.
* @param name
* name of the resource to assert if exists.
* @param isDisassociated
* @param isDeleted
* whether to assert if a resource exists or not.
*/
public static void assertResourceDisassociated(
VerificationHost host, String factoryLink, String name, boolean isDisassociated) {
VerificationHost host, String factoryLink, String name, boolean isDeleted) {

ServiceDocumentQueryResult result = host.getExpandedFactoryState(
UriUtils.buildUri(host, factoryLink));

boolean disassociated = false;
boolean deleted = true;

for (Object document : result.documents.values()) {
// Read doc as ServiceDocument to access its 'documentKind'
Expand All @@ -1889,14 +1889,14 @@ public static void assertResourceDisassociated(
String endpointLink = PhotonModelUtils.getEndpointLink(resource);

if (endpointLink == null || endpointLink.isEmpty()) {
disassociated = true;
deleted = false;
break;
}
}
}
}

assertEquals("isDisassociated", isDisassociated, disassociated);
assertEquals("isDisassociated", isDeleted, deleted);
}
/**
* Validate DiskStates are populated with the appropriate type tagLinks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,25 @@ public static <T extends ResourceState> Operation createRemoveEndpointLinksOpera
return null;
}

// Delete resource if it belongs to current endpoint only.
if (resource.endpointLinks.size() == 1 && resource.endpointLinks.contains(endpointLink)) {
return Operation
.createDelete(createInventoryUri(service.getHost(), resource.documentSelfLink))
.setReferer(service.getUri())
.setCompletion((deleteOp, failure) -> {
if (failure != null) {
service.getHost().log(Level.WARNING, () -> String.format("DELETE " +
"on resource %s for instance service %s, failed: %s",
resource.documentSelfLink, deleteOp.getUri(),
failure.toString()));
return;
}
service.getHost().log(Level.FINE, () -> String.format("DELETE on " +
"resource %s for instance service %s finished successfully",
resource.documentSelfLink, deleteOp.getUri()));
});
}

Map<String, Collection<Object>> endpointsToRemoveMap = Collections.singletonMap(
EndpointService.EndpointState.FIELD_NAME_ENDPOINT_LINKS,
Collections.singleton(endpointLink));
Expand Down

0 comments on commit c384343

Please sign in to comment.