Skip to content

Commit b57994e

Browse files
author
Daan Hoogland
committed
Merge branch '4.20'
2 parents 65d3592 + 0d65c8c commit b57994e

File tree

7 files changed

+88
-43
lines changed

7 files changed

+88
-43
lines changed

engine/schema/src/main/java/com/cloud/usage/dao/UsageJobDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,6 @@ public interface UsageJobDao extends GenericDao<UsageJobVO, Long> {
3737
UsageJobVO isOwner(String hostname, int pid);
3838

3939
void updateJobSuccess(Long jobId, long startMillis, long endMillis, long execTime, boolean success);
40+
41+
void removeLastOpenJobsOwned(String hostname, int pid);
4042
}

engine/schema/src/main/java/com/cloud/usage/dao/UsageJobDaoImpl.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.List;
2323

2424

25+
import org.apache.commons.collections.CollectionUtils;
2526
import org.springframework.stereotype.Component;
2627

2728
import com.cloud.usage.UsageJobVO;
@@ -114,7 +115,7 @@ public Long checkHeartbeat(String hostname, int pid, int aggregationDuration) {
114115
public UsageJobVO isOwner(String hostname, int pid) {
115116
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
116117
try {
117-
if ((hostname == null) || (pid <= 0)) {
118+
if (hostname == null || pid <= 0) {
118119
return null;
119120
}
120121

@@ -174,7 +175,7 @@ public UsageJobVO getNextImmediateJob() {
174175
SearchCriteria<UsageJobVO> sc = createSearchCriteria();
175176
sc.addAnd("endMillis", SearchCriteria.Op.EQ, Long.valueOf(0));
176177
sc.addAnd("jobType", SearchCriteria.Op.EQ, Integer.valueOf(UsageJobVO.JOB_TYPE_SINGLE));
177-
sc.addAnd("scheduled", SearchCriteria.Op.EQ, Integer.valueOf(0));
178+
sc.addAnd("scheduled", SearchCriteria.Op.EQ, Integer.valueOf(UsageJobVO.JOB_NOT_SCHEDULED));
178179
List<UsageJobVO> jobs = search(sc, filter);
179180

180181
if ((jobs == null) || jobs.isEmpty()) {
@@ -194,4 +195,36 @@ public Date getLastHeartbeat() {
194195
}
195196
return jobs.get(0).getHeartbeat();
196197
}
198+
199+
private List<UsageJobVO> getLastOpenJobsOwned(String hostname, int pid) {
200+
SearchCriteria<UsageJobVO> sc = createSearchCriteria();
201+
sc.addAnd("endMillis", SearchCriteria.Op.EQ, Long.valueOf(0));
202+
sc.addAnd("host", SearchCriteria.Op.EQ, hostname);
203+
if (pid > 0) {
204+
sc.addAnd("pid", SearchCriteria.Op.EQ, Integer.valueOf(pid));
205+
}
206+
return listBy(sc);
207+
}
208+
209+
@Override
210+
public void removeLastOpenJobsOwned(String hostname, int pid) {
211+
if (hostname == null) {
212+
return;
213+
}
214+
215+
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
216+
try {
217+
List<UsageJobVO> jobs = getLastOpenJobsOwned(hostname, pid);
218+
if (CollectionUtils.isNotEmpty(jobs)) {
219+
logger.info("Found {} opens job, to remove", jobs.size());
220+
for (UsageJobVO job : jobs) {
221+
logger.debug("Removing job - id: {}, pid: {}, job type: {}, scheduled: {}, heartbeat: {}",
222+
job.getId(), job.getPid(), job.getJobType(), job.getScheduled(), job.getHeartbeat());
223+
remove(job.getId());
224+
}
225+
}
226+
} finally {
227+
txn.close();
228+
}
229+
}
197230
}

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/ScaleIOStorageAdaptor.java

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -653,24 +653,10 @@ public Ternary<Boolean, Map<String, String>, String> prepareStorageClient(String
653653
if (!ScaleIOUtil.startSDCService()) {
654654
return new Ternary<>(false, null, "Couldn't start SDC service on host");
655655
}
656-
}
657-
658-
if (MapUtils.isNotEmpty(details) && details.containsKey(ScaleIOGatewayClient.STORAGE_POOL_MDMS)) {
659-
// Assuming SDC service is started, add mdms
660-
String mdms = details.get(ScaleIOGatewayClient.STORAGE_POOL_MDMS);
661-
String[] mdmAddresses = mdms.split(",");
662-
if (mdmAddresses.length > 0) {
663-
if (ScaleIOUtil.isMdmPresent(mdmAddresses[0])) {
664-
return new Ternary<>(true, getSDCDetails(details), "MDM added, no need to prepare the SDC client");
665-
}
666-
667-
ScaleIOUtil.addMdms(mdmAddresses);
668-
if (!ScaleIOUtil.isMdmPresent(mdmAddresses[0])) {
669-
return new Ternary<>(false, null, "Failed to add MDMs");
670-
} else {
671-
logger.debug(String.format("MDMs %s added to storage pool %s", mdms, uuid));
672-
applyMdmsChangeWaitTime(details);
673-
}
656+
} else {
657+
logger.debug("SDC service is active on host, re-starting it");
658+
if (!ScaleIOUtil.restartSDCService()) {
659+
return new Ternary<>(false, null, "Couldn't restart SDC service on host");
674660
}
675661
}
676662

@@ -798,12 +784,12 @@ private Map<String, String> getSDCDetails(Map<String, String> details) {
798784
if (sdcId != null) {
799785
sdcDetails.put(ScaleIOGatewayClient.SDC_ID, sdcId);
800786
return sdcDetails;
801-
}
802-
803-
String sdcGuId = ScaleIOUtil.getSdcGuid();
804-
if (sdcGuId != null) {
805-
sdcDetails.put(ScaleIOGatewayClient.SDC_GUID, sdcGuId);
806-
return sdcDetails;
787+
} else {
788+
String sdcGuId = ScaleIOUtil.getSdcGuid();
789+
if (sdcGuId != null) {
790+
sdcDetails.put(ScaleIOGatewayClient.SDC_GUID, sdcGuId);
791+
return sdcDetails;
792+
}
807793
}
808794

809795
try {

plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/storage/ScaleIOStorageAdaptorTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,34 @@ public void testPrepareStorageClient_SDCServiceNotEnabled() {
9292
Assert.assertEquals("SDC service not enabled on host", result.third());
9393
}
9494

95+
@Test
96+
public void testPrepareStorageClient_SDCServiceNotRestarted() {
97+
when(Script.runSimpleBashScriptForExitValue(Mockito.eq("systemctl status scini"))).thenReturn(3);
98+
when(Script.runSimpleBashScriptForExitValue(Mockito.eq("systemctl is-enabled scini"))).thenReturn(0);
99+
when(Script.runSimpleBashScriptForExitValue(Mockito.eq("systemctl is-active scini"))).thenReturn(0);
100+
when(Script.runSimpleBashScriptForExitValue(Mockito.eq("systemctl restart scini"))).thenReturn(1);
101+
102+
Ternary<Boolean, Map<String, String>, String> result = scaleIOStorageAdaptor.prepareStorageClient(poolUuid, new HashMap<>());
103+
104+
Assert.assertFalse(result.first());
105+
Assert.assertNull(result.second());
106+
Assert.assertEquals("Couldn't restart SDC service on host", result.third());
107+
}
108+
109+
@Test
110+
public void testPrepareStorageClient_SDCServiceRestarted() {
111+
when(Script.runSimpleBashScriptForExitValue(Mockito.eq("systemctl status scini"))).thenReturn(3);
112+
when(Script.runSimpleBashScriptForExitValue(Mockito.eq("systemctl is-enabled scini"))).thenReturn(0);
113+
when(Script.runSimpleBashScriptForExitValue(Mockito.eq("systemctl is-active scini"))).thenReturn(0);
114+
when(Script.runSimpleBashScriptForExitValue(Mockito.eq("systemctl restart scini"))).thenReturn(0);
115+
116+
Ternary<Boolean, Map<String, String>, String> result = scaleIOStorageAdaptor.prepareStorageClient(poolUuid, new HashMap<>());
117+
118+
Assert.assertFalse(result.first());
119+
Assert.assertNull(result.second());
120+
Assert.assertEquals("Couldn't get the SDC details on the host", result.third());
121+
}
122+
95123
@Test
96124
public void testPrepareStorageClient_SDCServiceNotStarted() {
97125
when(Script.runSimpleBashScriptForExitValue(Mockito.eq("systemctl status scini"))).thenReturn(3);

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
5151
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
5252
<project.systemvm.template.location>https://download.cloudstack.org/systemvm</project.systemvm.template.location>
53-
<project.systemvm.template.version>4.20.1.0</project.systemvm.template.version>
53+
<project.systemvm.template.version>4.20.2.0</project.systemvm.template.version>
5454
<sonar.organization>apache</sonar.organization>
5555
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
5656

tools/appliance/systemvmtemplate/scripts/finalize.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ function zero_disk() {
6868
}
6969

7070
function finalize() {
71+
depmod -a
7172
configure_misc
7273
configure_rundisk_size
7374
configure_sudoers

usage/src/main/java/com/cloud/usage/UsageManagerImpl.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,9 @@ public boolean start() {
312312
logger.info("Starting Usage Manager");
313313
}
314314

315+
_usageJobDao.removeLastOpenJobsOwned(_hostname, 0);
316+
Runtime.getRuntime().addShutdownHook(new AbandonJob());
317+
315318
// use the configured exec time and aggregation duration for scheduling the job
316319
_scheduledFuture =
317320
_executor.scheduleAtFixedRate(this, _jobExecTime.getTimeInMillis() - System.currentTimeMillis(), _aggregationDuration * 60 * 1000, TimeUnit.MILLISECONDS);
@@ -324,7 +327,6 @@ public boolean start() {
324327
_sanity = _sanityExecutor.scheduleAtFixedRate(new SanityCheck(), 1, _sanityCheckInterval, TimeUnit.DAYS);
325328
}
326329

327-
Runtime.getRuntime().addShutdownHook(new AbandonJob());
328330
TransactionLegacy usageTxn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
329331
try {
330332
if (_heartbeatLock.lock(3)) { // 3 second timeout
@@ -2148,19 +2150,17 @@ protected void runInContext() {
21482150
// the aggregation range away from executing the next job
21492151
long now = System.currentTimeMillis();
21502152
long timeToJob = _jobExecTime.getTimeInMillis() - now;
2151-
long timeSinceJob = 0;
2153+
long timeSinceLastSuccessJob = 0;
21522154
long aggregationDurationMillis = _aggregationDuration * 60L * 1000L;
21532155
long lastSuccess = _usageJobDao.getLastJobSuccessDateMillis();
21542156
if (lastSuccess > 0) {
2155-
timeSinceJob = now - lastSuccess;
2157+
timeSinceLastSuccessJob = now - lastSuccess;
21562158
}
21572159

2158-
if ((timeSinceJob > 0) && (timeSinceJob > (aggregationDurationMillis - 100))) {
2160+
if ((timeSinceLastSuccessJob > 0) && (timeSinceLastSuccessJob > (aggregationDurationMillis - 100))) {
21592161
if (timeToJob > (aggregationDurationMillis / 2)) {
2160-
if (logger.isDebugEnabled()) {
2161-
logger.debug("it's been " + timeSinceJob + " ms since last usage job and " + timeToJob +
2162-
" ms until next job, scheduling an immediate job to catch up (aggregation duration is " + _aggregationDuration + " minutes)");
2163-
}
2162+
logger.debug("it's been {} ms since last usage job and {} ms until next job, scheduling an immediate job to catch up (aggregation duration is {} minutes)"
2163+
, timeSinceLastSuccessJob, timeToJob, _aggregationDuration);
21642164
scheduleParse();
21652165
}
21662166
}
@@ -2245,17 +2245,12 @@ protected void runInContext() {
22452245
}
22462246
}
22472247
}
2248+
22482249
private class AbandonJob extends Thread {
22492250
@Override
22502251
public void run() {
2251-
logger.info("exitting Usage Manager");
2252-
deleteOpenjob();
2253-
}
2254-
private void deleteOpenjob() {
2255-
UsageJobVO job = _usageJobDao.isOwner(_hostname, _pid);
2256-
if (job != null) {
2257-
_usageJobDao.remove(job.getId());
2258-
}
2252+
logger.info("exiting Usage Manager");
2253+
_usageJobDao.removeLastOpenJobsOwned(_hostname, _pid);
22592254
}
22602255
}
22612256
}

0 commit comments

Comments
 (0)