Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public class RestoreBackupCommand extends Command {
private List<String> backupVolumesUUIDs;
private List<PrimaryDataStoreTO> restoreVolumePools;
private List<String> restoreVolumePaths;
private List<String> backupFiles;
private String diskType;
private Boolean vmExists;
private String restoreVolumeUUID;
private VirtualMachine.State vmState;
private Integer mountTimeout;

Expand Down Expand Up @@ -92,6 +92,14 @@ public void setRestoreVolumePaths(List<String> restoreVolumePaths) {
this.restoreVolumePaths = restoreVolumePaths;
}

public List<String> getBackupFiles() {
return backupFiles;
}

public void setBackupFiles(List<String> backupFiles) {
this.backupFiles = backupFiles;
}

public Boolean isVmExists() {
return vmExists;
}
Expand All @@ -116,14 +124,6 @@ public void setMountOptions(String mountOptions) {
this.mountOptions = mountOptions;
}

public String getRestoreVolumeUUID() {
return restoreVolumeUUID;
}

public void setRestoreVolumeUUID(String restoreVolumeUUID) {
this.restoreVolumeUUID = restoreVolumeUUID;
}

public VirtualMachine.State getVmState() {
return vmState;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ public interface ResourceManager extends ResourceService, Configurable {

public HostVO findHostByGuid(String guid);

HostVO findHostByGuidPrefix(String guid);

public HostVO findHostByName(String name);

HostStats getHostStatistics(Host host);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public Scope getScope() {
@Override
public String getConfigValue(long id, String key) {
ClusterDetailsVO vo = findDetail(id, key);
return vo == null ? null : vo.getValue();
return vo == null ? null : getActualValue(vo);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public Scope getScope() {
@Override
public String getConfigValue(long id, String key) {
ResourceDetail vo = findDetail(id, key);
return vo == null ? null : vo.getValue();
return vo == null ? null : getActualValue(vo);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1412,7 +1412,7 @@ public HostVO findAnyStateHypervisorHostInCluster(long clusterId) {
SearchCriteria<HostVO> sc = TypeStatusStateSearch.create();
sc.setParameters("type", Host.Type.Routing);
sc.setParameters("cluster", clusterId);
List<HostVO> list = listBy(sc, new Filter(1));
List<HostVO> list = listBy(sc, new Filter(1, true));
return list.isEmpty() ? null : list.get(0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public ImageStoreVO findOneByZoneAndProtocol(long dataCenterId, String protocol)
sc.setParameters("dataCenterId", dataCenterId);
sc.setParameters("protocol", protocol);
sc.setParameters("role", DataStoreRole.Image);
Filter filter = new Filter(1);
Filter filter = new Filter(1, true);
List<ImageStoreVO> results = listBy(sc, filter);
return results.size() == 0 ? null : results.get(0);
}
Expand Down
13 changes: 12 additions & 1 deletion framework/db/src/main/java/com/cloud/utils/db/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,18 @@ public Filter(Class<?> clazz, String field, boolean ascending) {
}

public Filter(long limit) {
_orderBy = " ORDER BY RAND()";
this(limit, false);
}

/**
* Constructor for creating a filter with random ordering
* @param limit the maximum number of results to return
* @param randomize if true, orders results randomly
*/
public Filter(long limit, boolean randomize) {
if (randomize) {
_orderBy = " ORDER BY RAND()" ;
}
_limit = limit;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ public List<T> lockRows(final SearchCriteria<T> sc, final Filter filter, final b
@Override
@DB()
public T lockOneRandomRow(final SearchCriteria<T> sc, final boolean exclusive) {
final Filter filter = new Filter(1);
final Filter filter = new Filter(1, true);
final List<T> beans = search(sc, filter, exclusive, true);
return beans.isEmpty() ? null : beans.get(0);
}
Expand Down Expand Up @@ -927,7 +927,7 @@ public Class<T> getEntityBeanType() {

@DB()
protected T findOneIncludingRemovedBy(final SearchCriteria<T> sc) {
Filter filter = new Filter(1);
Filter filter = new Filter(1, true);
List<T> results = searchIncludingRemoved(sc, filter, null, false);
assert results.size() <= 1 : "Didn't the limiting worked?";
return results.size() == 0 ? null : results.get(0);
Expand Down Expand Up @@ -1335,7 +1335,7 @@ public int batchExpunge(final SearchCriteria<T> sc, final Long batchSize) {
Filter filter = null;
final long batchSizeFinal = ObjectUtils.defaultIfNull(batchSize, 0L);
if (batchSizeFinal > 0) {
filter = new Filter(null, batchSizeFinal);
filter = new Filter(batchSizeFinal);
}
int expunged = 0;
int currentExpunged = 0;
Expand Down
58 changes: 58 additions & 0 deletions framework/db/src/test/java/com/cloud/utils/db/FilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,62 @@ public void testAddOrderBy() {
Assert.assertTrue(filter.getOrderBy().split(",").length == 3);
Assert.assertTrue(filter.getOrderBy().split(",")[2].trim().toLowerCase().equals("test.fld_int asc"));
}


@Test
public void testFilterWithLimitOnly() {
Filter filter = new Filter(5);

Assert.assertEquals(Long.valueOf(5), filter.getLimit());
Assert.assertNull(filter.getOrderBy());
Assert.assertNull(filter.getOffset());
}

@Test
public void testFilterWithLimitAndRandomizeFalse() {
Filter filter = new Filter(10, false);

Assert.assertEquals(Long.valueOf(10), filter.getLimit());
Assert.assertNull(filter.getOrderBy());
Assert.assertNull(filter.getOffset());
}

@Test
public void testFilterWithLimitAndRandomizeTrue() {
Filter filter = new Filter(3, true);

Assert.assertNull(filter.getLimit());
Assert.assertNotNull(filter.getOrderBy());
Assert.assertTrue(filter.getOrderBy().contains("ORDER BY RAND()"));
Assert.assertTrue(filter.getOrderBy().contains("LIMIT 3"));
Assert.assertEquals(" ORDER BY RAND() LIMIT 3", filter.getOrderBy());
}

@Test
public void testFilterRandomizeWithDifferentLimits() {
Filter filter1 = new Filter(1, true);
Filter filter10 = new Filter(10, true);
Filter filter100 = new Filter(100, true);

Assert.assertEquals(" ORDER BY RAND() LIMIT 1", filter1.getOrderBy());
Assert.assertEquals(" ORDER BY RAND() LIMIT 10", filter10.getOrderBy());
Assert.assertEquals(" ORDER BY RAND() LIMIT 100", filter100.getOrderBy());
}

@Test
public void testFilterConstructorBackwardsCompatibility() {
// Test that Filter(long) behaves differently now (no ORDER BY RAND())
// compared to Filter(long, true) which preserves old behavior
Filter simpleLimitFilter = new Filter(1);
Filter randomFilter = new Filter(1, true);

// Simple limit filter should just set limit
Assert.assertEquals(Long.valueOf(1), simpleLimitFilter.getLimit());
Assert.assertNull(simpleLimitFilter.getOrderBy());

// Random filter should set orderBy with RAND()
Assert.assertNull(randomFilter.getLimit());
Assert.assertNotNull(randomFilter.getOrderBy());
Assert.assertTrue(randomFilter.getOrderBy().contains("RAND()"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.junit.Assert;
import org.junit.Before;
Expand Down Expand Up @@ -263,4 +264,71 @@ public void multiJoinSameTableTest() {
" INNER JOIN tableA tableA2Alias ON tableC.column3=tableA2Alias.column2 " +
" INNER JOIN tableA tableA3Alias ON tableD.column4=tableA3Alias.column3 AND tableD.column5=? ", joinString.toString());
}


@Test
public void testLockOneRandomRowUsesRandomFilter() {
// Create a mock DAO to test lockOneRandomRow behavior
GenericDaoBase<DbTestVO, Long> testDao = Mockito.mock(GenericDaoBase.class);

// Capture the filter passed to the search method
final Filter[] capturedFilter = new Filter[1];

Mockito.when(testDao.lockOneRandomRow(Mockito.any(SearchCriteria.class), Mockito.anyBoolean()))
.thenCallRealMethod();

Mockito.when(testDao.search(Mockito.any(SearchCriteria.class), Mockito.any(Filter.class),
Mockito.anyBoolean(), Mockito.anyBoolean()))
.thenAnswer(invocation -> {
capturedFilter[0] = invocation.getArgument(1);
return new ArrayList<DbTestVO>();
});

SearchCriteria<DbTestVO> sc = Mockito.mock(SearchCriteria.class);
testDao.lockOneRandomRow(sc, true);

// Verify that the filter uses random ordering
Assert.assertNotNull(capturedFilter[0]);
Assert.assertNotNull(capturedFilter[0].getOrderBy());
Assert.assertTrue(capturedFilter[0].getOrderBy().contains("ORDER BY RAND()"));
Assert.assertTrue(capturedFilter[0].getOrderBy().contains("LIMIT 1"));
}

@Test
public void testLockOneRandomRowReturnsNullOnEmptyResult() {
GenericDaoBase<DbTestVO, Long> testDao = Mockito.mock(GenericDaoBase.class);

Mockito.when(testDao.lockOneRandomRow(Mockito.any(SearchCriteria.class), Mockito.anyBoolean()))
.thenCallRealMethod();

Mockito.when(testDao.search(Mockito.any(SearchCriteria.class), Mockito.any(Filter.class),
Mockito.anyBoolean(), Mockito.anyBoolean()))
.thenReturn(new ArrayList<DbTestVO>());

SearchCriteria<DbTestVO> sc = Mockito.mock(SearchCriteria.class);
DbTestVO result = testDao.lockOneRandomRow(sc, true);

Assert.assertNull(result);
}

@Test
public void testLockOneRandomRowReturnsFirstElement() {
GenericDaoBase<DbTestVO, Long> testDao = Mockito.mock(GenericDaoBase.class);
DbTestVO expectedResult = new DbTestVO();
List<DbTestVO> resultList = new ArrayList<>();
resultList.add(expectedResult);

Mockito.when(testDao.lockOneRandomRow(Mockito.any(SearchCriteria.class), Mockito.anyBoolean()))
.thenCallRealMethod();

Mockito.when(testDao.search(Mockito.any(SearchCriteria.class), Mockito.any(Filter.class),
Mockito.anyBoolean(), Mockito.anyBoolean()))
.thenReturn(resultList);

SearchCriteria<DbTestVO> sc = Mockito.mock(SearchCriteria.class);
DbTestVO result = testDao.lockOneRandomRow(sc, true);

Assert.assertNotNull(result);
Assert.assertEquals(expectedResult, result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ protected BigDecimal getQuotaTariffValueToBeApplied(QuotaTariffVO quotaTariff, J
}

injectPresetVariablesIntoJsInterpreter(jsInterpreter, presetVariables);
jsInterpreter.injectVariable("lastTariffs", lastAppliedTariffsList.toString());
jsInterpreter.injectVariable("lastTariffs", lastAppliedTariffsList);

String scriptResult = jsInterpreter.executeScript(activationRule).toString();

Expand Down Expand Up @@ -459,12 +459,12 @@ protected BigDecimal getQuotaTariffValueToBeApplied(QuotaTariffVO quotaTariff, J
protected void injectPresetVariablesIntoJsInterpreter(JsInterpreter jsInterpreter, PresetVariables presetVariables) {
jsInterpreter.discardCurrentVariables();

jsInterpreter.injectVariable("account", presetVariables.getAccount().toString());
jsInterpreter.injectVariable("domain", presetVariables.getDomain().toString());
jsInterpreter.injectVariable("account", presetVariables.getAccount());
jsInterpreter.injectVariable("domain", presetVariables.getDomain());

GenericPresetVariable project = presetVariables.getProject();
if (project != null) {
jsInterpreter.injectVariable("project", project.toString());
jsInterpreter.injectVariable("project", project);

}

Expand All @@ -474,8 +474,8 @@ protected void injectPresetVariablesIntoJsInterpreter(JsInterpreter jsInterprete
}

jsInterpreter.injectVariable("resourceType", presetVariables.getResourceType());
jsInterpreter.injectVariable("value", presetVariables.getValue().toString());
jsInterpreter.injectVariable("zone", presetVariables.getZone().toString());
jsInterpreter.injectVariable("value", presetVariables.getValue());
jsInterpreter.injectVariable("zone", presetVariables.getZone());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public Role getRole() {

public void setRole(Role role) {
this.role = role;
fieldNamesToIncludeInToString.add("role");
}

public String getCreated() {
Expand All @@ -45,6 +44,5 @@ public String getCreated() {

public void setCreated(Date created) {
this.created = DateUtil.displayDateInTimezone(TimeZone.getTimeZone("GMT"), created);
fieldNamesToIncludeInToString.add("created");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,5 @@ public String getExternalId() {

public void setExternalId(String externalId) {
this.externalId = externalId;
fieldNamesToIncludeInToString.add("externalId");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public boolean isCustomized() {

public void setCustomized(boolean customized) {
this.customized = customized;
fieldNamesToIncludeInToString.add("customized");
}

public boolean offerHa() {
Expand All @@ -41,7 +40,6 @@ public boolean offerHa() {

public void setOfferHa(boolean offerHa) {
this.offerHa = offerHa;
fieldNamesToIncludeInToString.add("offerHa");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,5 @@ public boolean getForceHa() {

public void setForceHa(boolean forceHa) {
this.forceHa = forceHa;
fieldNamesToIncludeInToString.add("forceHa");
}
}
Loading
Loading