Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
9bd4f1e
upgrade: move 4.19.0->4.20.0 to 4.19.1->4.20.0
weizhouapache Apr 8, 2024
af9d68c
api,server: purge expunged resources
shwstppr Mar 8, 2024
8ee1a15
fix
shwstppr Apr 30, 2024
971991d
refactor
shwstppr Apr 30, 2024
3103f85
fix
shwstppr Apr 30, 2024
f8fc404
another fix
shwstppr Apr 30, 2024
420babb
defensive checks, test fix
shwstppr May 1, 2024
f0c04cb
fixes
shwstppr May 5, 2024
73770a5
test fix
shwstppr May 5, 2024
36d0cad
Merge branch 'main' into 4.20-fix-upgrade-path-from-4191
weizhouapache May 7, 2024
7fd0c4e
Update api/src/main/java/org/apache/cloudstack/api/command/admin/offe…
shwstppr May 8, 2024
00ca064
Update api/src/main/java/org/apache/cloudstack/api/command/admin/reso…
shwstppr May 8, 2024
8bca921
Update api/src/main/java/org/apache/cloudstack/api/response/ServiceOf…
shwstppr May 8, 2024
dc593c5
Merge remote-tracking branch 'apache/main' into 4.20-fix-upgrade-path…
weizhouapache May 30, 2024
e61cc85
Merge remote-tracking branch 'apache/main' into feature-purgedb
shwstppr Jun 2, 2024
ff35da9
rephrase global config descriptions
shwstppr Jun 2, 2024
6fbd01c
Merge remote-tracking branch 'wei/4.20-fix-upgrade-path-from-4191' in…
shwstppr Jun 2, 2024
d17678a
Merge remote-tracking branch 'apache/main' into feature-purgedb
shwstppr Jun 3, 2024
ced6e6d
code coverage
shwstppr Jun 7, 2024
2a078c1
eof
shwstppr Jun 7, 2024
90c584a
imports
shwstppr Jun 7, 2024
195a904
Merge remote-tracking branch 'apache/main' into feature-purgedb
shwstppr Jun 10, 2024
7ef4c4b
Merge branch 'main' into feature-purgedb
shwstppr Jun 11, 2024
93edde5
Merge remote-tracking branch 'apache/main' into feature-purgedb
shwstppr Jun 18, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ jobs:
smoke/test_multipleips_per_nic
smoke/test_nested_virtualization
smoke/test_set_sourcenat
smoke/test_webhook_lifecycle",
smoke/test_webhook_lifecycle
smoke/test_purge_expunged_vms",
"smoke/test_network
smoke/test_network_acl
smoke/test_network_ipv6
Expand Down
2 changes: 2 additions & 0 deletions api/src/main/java/com/cloud/event/EventTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,8 @@ public class EventTypes {

// SystemVM
public static final String EVENT_LIVE_PATCH_SYSTEMVM = "LIVE.PATCH.SYSTEM.VM";
//Purge resources
public static final String EVENT_PURGE_EXPUNGED_RESOURCES = "PURGE.EXPUNGED.RESOURCES";

// OBJECT STORE
public static final String EVENT_OBJECT_STORE_CREATE = "OBJECT.STORE.CREATE";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,7 @@ public interface LoadBalancingServiceProvider extends NetworkElement, IpDeployin
List<LoadBalancerTO> updateHealthChecks(Network network, List<LoadBalancingRule> lbrules);

boolean handlesOnlyRulesInTransitionState();

default void expungeLbVmRefs(List<Long> vmIds, Long batchSize) {
}
}
3 changes: 3 additions & 0 deletions api/src/main/java/com/cloud/offering/ServiceOffering.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public interface ServiceOffering extends InfrastructureEntity, InternalIdentity,
static final String internalLbVmDefaultOffUniqueName = "Cloud.Com-InternalLBVm";
// leaving cloud.com references as these are identifyers and no real world addresses (check against DB)


static final String PURGE_DB_ENTITIES_KEY = "purge.db.entities";

enum State {
Inactive, Active,
}
Expand Down
2 changes: 2 additions & 0 deletions api/src/main/java/org/apache/cloudstack/api/ApiConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ public class ApiConstants {
public static final String PUBLIC_START_PORT = "publicport";
public static final String PUBLIC_END_PORT = "publicendport";
public static final String PUBLIC_ZONE = "publiczone";
public static final String PURGE_RESOURCES = "purgeresources";
public static final String RECEIVED_BYTES = "receivedbytes";
public static final String RECONNECT = "reconnect";
public static final String RECOVER = "recover";
Expand Down Expand Up @@ -909,6 +910,7 @@ public class ApiConstants {
public static final String AUTOSCALE_VMGROUP_NAME = "autoscalevmgroupname";
public static final String BAREMETAL_DISCOVER_NAME = "baremetaldiscovername";
public static final String BAREMETAL_RCT_URL = "baremetalrcturl";
public static final String BATCH_SIZE = "batchsize";
public static final String UCS_DN = "ucsdn";
public static final String GSLB_PROVIDER = "gslbprovider";
public static final String EXCLUSIVE_GSLB_PROVIDER = "isexclusivegslbprovider";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ public class CreateServiceOfferingCmd extends BaseCmd {
@Parameter(name = ApiConstants.ENCRYPT_ROOT, type = CommandType.BOOLEAN, description = "VMs using this offering require root volume encryption", since="4.18")
private Boolean encryptRoot;

@Parameter(name = ApiConstants.PURGE_RESOURCES, type = CommandType.BOOLEAN,
description = "Whether to cleanup instance and its associated resource from database upon expunge of the instance",
since="4.20")
private Boolean purgeResources;



/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
Expand Down Expand Up @@ -273,7 +279,7 @@ public Integer getMemory() {

public String getServiceOfferingName() {
if (StringUtils.isEmpty(serviceOfferingName)) {
throw new InvalidParameterValueException("Failed to create service offering because offering name has not been spified.");
throw new InvalidParameterValueException("Failed to create service offering because offering name has not been specified.");
}
return serviceOfferingName;
}
Expand Down Expand Up @@ -481,6 +487,10 @@ public boolean getEncryptRoot() {
return false;
}

public boolean isPurgeResources() {
return Boolean.TRUE.equals(purgeResources);
}

/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public class UpdateServiceOfferingCmd extends BaseCmd {
description = "state of the service offering")
private String serviceOfferingState;

@Parameter(name = ApiConstants.PURGE_RESOURCES, type = CommandType.BOOLEAN,
description = "Whether to cleanup VM and its associated resource upon expunge",
since="4.20")
private Boolean purgeResources;

/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
Expand Down Expand Up @@ -185,6 +190,10 @@ public State getState() {
return state;
}

public boolean isPurgeResources() {
return Boolean.TRUE.equals(purgeResources);
}

/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.cloudstack.api.command.admin.resource;


import java.util.Date;

import javax.inject.Inject;

import org.apache.cloudstack.acl.RoleType;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.ApiErrorCode;
import org.apache.cloudstack.api.BaseAsyncCmd;
import org.apache.cloudstack.api.BaseCmd;
import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.ResponseObject;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.PurgeExpungedResourcesResponse;
import org.apache.cloudstack.api.response.SuccessResponse;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.resource.ResourceCleanupService;

import com.cloud.event.EventTypes;

@APICommand(name = "purgeExpungedResources",
description = "Purge expunged resources",
responseObject = SuccessResponse.class,
responseView = ResponseObject.ResponseView.Full,
requestHasSensitiveInfo = false,
responseHasSensitiveInfo = false,
authorized = {RoleType.Admin},
since = "4.20")
public class PurgeExpungedResourcesCmd extends BaseAsyncCmd {

@Inject
ResourceCleanupService resourceCleanupService;

/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
/////////////////////////////////////////////////////

@Parameter(name = ApiConstants.RESOURCE_TYPE, type = BaseCmd.CommandType.STRING,
description = "The type of the resource which need to be purged. Supported types: " +
"VirtualMachine")
private String resourceType;

@Parameter(name = ApiConstants.BATCH_SIZE, type = CommandType.LONG,
description = "The size of batch used during purging")
private Long batchSize;

@Parameter(name = ApiConstants.START_DATE,
type = CommandType.DATE,
description = "The start date range of the expunged resources used for purging " +
"(use format \"yyyy-MM-dd\" or \"yyyy-MM-dd HH:mm:ss\")")
private Date startDate;

@Parameter(name = ApiConstants.END_DATE,
type = CommandType.DATE,
description = "The end date range of the expunged resources used for purging " +
"(use format \"yyyy-MM-dd\" or \"yyyy-MM-dd HH:mm:ss\")")
private Date endDate;

/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////


public String getResourceType() {
return resourceType;
}

public Long getBatchSize() {
return batchSize;
}

public Date getStartDate() {
return startDate;
}

public Date getEndDate() {
return endDate;
}

@Override
public long getEntityOwnerId() {
return CallContext.current().getCallingAccount().getId();
}

@Override
public String getEventType() {
return EventTypes.EVENT_PURGE_EXPUNGED_RESOURCES;
}

@Override
public String getEventDescription() {
return "Purging expunged resources";
}

/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////

@Override
public void execute() {
try {
long result = resourceCleanupService.purgeExpungedResources(this);
PurgeExpungedResourcesResponse response = new PurgeExpungedResourcesResponse();
response.setResourceCount(result);
response.setObjectName(getCommandName().toLowerCase());
setResponseObject(response);
} catch (Exception e) {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getLocalizedMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.cloudstack.api.response;

import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseResponse;

import com.cloud.serializer.Param;
import com.google.gson.annotations.SerializedName;

public class PurgeExpungedResourcesResponse extends BaseResponse {

@SerializedName(ApiConstants.RESOURCE_COUNT)
@Param(description = "The count of the purged expunged resources")
private Long resourceCount;

public Long getResourceCount() {
return resourceCount;
}

public void setResourceCount(Long resourceCount) {
this.resourceCount = resourceCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ public class ServiceOfferingResponse extends BaseResponseWithAnnotations {
@Param(description = "true if virtual machine root disk will be encrypted on storage", since = "4.18")
private Boolean encryptRoot;

@SerializedName(ApiConstants.PURGE_RESOURCES)
@Param(description = "Whether to cleanup VM and its associated resource upon expunge", since = "4.20")
private Boolean purgeResources;

public ServiceOfferingResponse() {
}

Expand Down Expand Up @@ -555,4 +559,8 @@ public String getDiskOfferingDisplayText() {
}

public void setEncryptRoot(Boolean encrypt) { this.encryptRoot = encrypt; }

public void setPurgeResources(Boolean purgeResources) {
this.purgeResources = purgeResources;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.cloudstack.resource;

import org.apache.cloudstack.api.command.admin.resource.PurgeExpungedResourcesCmd;
import org.apache.cloudstack.framework.config.ConfigKey;

import com.cloud.vm.VirtualMachine;

public interface ResourceCleanupService {
int MINIMUM_EXPUNGED_RESOURCE_PURGE_JOB_DELAY_IN_SECONDS = 3 * 60;
ConfigKey<Boolean> ExpungedResourcePurgeEnabled = new ConfigKey<>("Advanced", Boolean.class,
"expunged.resources.purge.enabled", "false",
"Whether to run a background task to purge the DB records of the expunged resources",
false, ConfigKey.Scope.Global);
ConfigKey<String> ExpungedResourcePurgeResources = new ConfigKey<>("Advanced", String.class,
"expunged.resources.purge.resources", "",
"A comma-separated list of resource types that will be considered by the background task " +
"to purge the DB records of the expunged resources. Currently only VirtualMachine is supported. " +
"An empty value will result in considering all resource types for purging",
false, ConfigKey.Scope.Global);
ConfigKey<Integer> ExpungedResourcesPurgeInterval = new ConfigKey<>("Advanced", Integer.class,
"expunged.resources.purge.interval", "86400",
"Interval (in seconds) for the background task to purge the DB records of the expunged resources",
false);
ConfigKey<Integer> ExpungedResourcesPurgeDelay = new ConfigKey<>("Advanced", Integer.class,
"expunged.resources.purge.delay", "300",
"Initial delay (in seconds) to start the background task to purge the DB records of the " +
"expunged resources task", false);
ConfigKey<Integer> ExpungedResourcesPurgeBatchSize = new ConfigKey<>("Advanced", Integer.class,
"expunged.resources.purge.batch.size", "50",
"Batch size to be used during purging of the DB records of the expunged resources",
true);
ConfigKey<String> ExpungedResourcesPurgeStartTime = new ConfigKey<>("Advanced", String.class,
"expunged.resources.purge.start.time", "",
"Start time to be used by the background task to purge the DB records of the expunged " +
"resources. Use format \"yyyy-MM-dd\" or \"yyyy-MM-dd HH:mm:ss\"", true);
ConfigKey<Integer> ExpungedResourcesPurgeKeepPastDays = new ConfigKey<>("Advanced", Integer.class,
"expunged.resources.purge.keep.past.days", "30",
"The number of days in the past from the execution time of the background task to purge " +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"The number of days in the past from the execution time of the background task to purge " +
"The number of days in the past from the execution time of the background task to keep " +

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the complete sentence makes sense as it is,

The number of days in the past from the execution time of the background task to purge the expunged resources for which the expunged resources must not be purged

If needed, we can rephrase the whole statement

"the DB records of the expunged resources for which the expunged resources must not be purged. " +
"To enable purging DB records of the expunged resource till the execution of the background " +
"task, set the value to zero.", true);
ConfigKey<Integer> ExpungedResourcePurgeJobDelay = new ConfigKey<>("Advanced", Integer.class,
"expunged.resource.purge.job.delay",
String.valueOf(MINIMUM_EXPUNGED_RESOURCE_PURGE_JOB_DELAY_IN_SECONDS),
String.format("Delay (in seconds) to execute the purging of the DB records of an expunged resource " +
"initiated by the configuration in the offering. Minimum value should be %d seconds " +
"and if a lower value is set then the minimum value will be used",
MINIMUM_EXPUNGED_RESOURCE_PURGE_JOB_DELAY_IN_SECONDS),
true);

enum ResourceType {
VirtualMachine
}

long purgeExpungedResources(PurgeExpungedResourcesCmd cmd);
void purgeExpungedVmResourcesLaterIfNeeded(VirtualMachine vm);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,22 @@ public void testGetDisplayTextWhenEmpty() {
Assert.assertEquals(createServiceOfferingCmd.getDisplayText(), netName);
}

@Test
public void testIsPurgeResourcesNoOrNullValue() {
Assert.assertFalse(createServiceOfferingCmd.isPurgeResources());
ReflectionTestUtils.setField(createServiceOfferingCmd, "purgeResources", false);
Assert.assertFalse(createServiceOfferingCmd.isPurgeResources());
}

@Test
public void testIsPurgeResourcesFalse() {
ReflectionTestUtils.setField(createServiceOfferingCmd, "purgeResources", false);
Assert.assertFalse(createServiceOfferingCmd.isPurgeResources());
}

@Test
public void testIsPurgeResourcesTrue() {
ReflectionTestUtils.setField(createServiceOfferingCmd, "purgeResources", true);
Assert.assertTrue(createServiceOfferingCmd.isPurgeResources());
}
}
Loading