Skip to content

Commit

Permalink
Auto-delete the down state hosts (#945)
Browse files Browse the repository at this point in the history
* Add testDeleteDownHosts

* Add testDeleteDownHosts
  • Loading branch information
splhack committed Apr 6, 2021
1 parent a77c1ff commit 401a33b
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cuebot/src/main/java/com/imageworks/spcue/dao/HostDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public interface HostDao {
*/
void deleteHost(HostInterface host);

/**
* deletes the down state hosts
*/
void deleteDownHosts();

/**
* updates a host with the passed hardware state
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,11 @@ public void deleteHost(HostInterface host) {
"DELETE FROM host WHERE pk_host=?",host.getHostId());
}

@Override
public void deleteDownHosts() {
// Not implemented.
}

@Override
public void updateHostState(HostInterface host, HardwareState state) {
getJdbcTemplate().update(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,34 @@ public void deleteHost(HostInterface host) {
"DELETE FROM host WHERE pk_host=?",host.getHostId());
}

private static final String DELETE_DOWN_HOST_COMMENTS =
"DELETE " +
"FROM " +
"comments " +
"USING " +
"host_stat " +
"WHERE " +
"comments.pk_host = host_stat.pk_host " +
"AND " +
"host_stat.str_state = ?";

private static final String DELETE_DOWN_HOSTS =
"DELETE " +
"FROM " +
"host " +
"USING " +
"host_stat " +
"WHERE " +
"host.pk_host = host_stat.pk_host " +
"AND " +
"host_stat.str_state=?";

@Override
public void deleteDownHosts() {
getJdbcTemplate().update(DELETE_DOWN_HOST_COMMENTS, HardwareState.DOWN.toString());
getJdbcTemplate().update(DELETE_DOWN_HOSTS, HardwareState.DOWN.toString());
}

@Override
public void updateHostState(HostInterface host, HardwareState state) {
getJdbcTemplate().update(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.CannotGetJdbcConnectionException;

import com.imageworks.spcue.FrameInterface;
import com.imageworks.spcue.MaintenanceTask;
import com.imageworks.spcue.PointDetail;
import com.imageworks.spcue.VirtualProc;
import com.imageworks.spcue.dao.FrameDao;
import com.imageworks.spcue.dao.HostDao;
import com.imageworks.spcue.dao.MaintenanceDao;
import com.imageworks.spcue.dao.ProcDao;
import com.imageworks.spcue.dispatcher.DispatchSupport;
Expand All @@ -41,12 +44,17 @@ public class MaintenanceManagerSupport {

private static final Logger logger = Logger.getLogger(MaintenanceManagerSupport.class);

@Autowired
private Environment env;

private MaintenanceDao maintenanceDao;

private ProcDao procDao;

private FrameDao frameDao;

private HostDao hostDao;

private JobManager jobManager;

private DispatchSupport dispatchSupport;
Expand Down Expand Up @@ -90,6 +98,12 @@ public void checkHardwareState() {
int hosts = maintenanceDao.setUpHostsToDown();
if (hosts > 0) {
clearDownProcs();

boolean autoDeleteDownHosts = env.getProperty(
"maintenance.auto_delete_down_hosts", Boolean.class, false);
if (autoDeleteDownHosts) {
hostDao.deleteDownHosts();
}
}
clearOrphanedProcs();
} finally {
Expand Down Expand Up @@ -193,6 +207,10 @@ public void setFrameDao(FrameDao frameDao) {
this.frameDao = frameDao;
}

public void setHostDao(HostDao hostDao) {
this.hostDao = hostDao;
}

public DispatchSupport getDispatchSupport() {
return dispatchSupport;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@
<property name="dispatchSupport" ref="dispatchSupport" />
<property name="procDao" ref="procDao" />
<property name="frameDao" ref="frameDao" />
<property name="hostDao" ref="hostDao" />
<property name="maintenanceDao" ref="maintenanceDao" />
<property name="historicalSupport" ref="historicalSupport" />
<property name="departmentManager" ref="departmentManager" />
Expand Down
3 changes: 3 additions & 0 deletions cuebot/src/main/resources/opencue.properties
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,6 @@ dispatcher.host_frame_dispatch_max=12

# Jobs will be archived to the history tables after being completed for this long.
history.archive_jobs_cutoff_hours=72

# Delete down hosts automatically.
maintenance.auto_delete_down_hosts=false
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,30 @@ public void testDeleteHost() {
assertEquals(hostDao.hostExists(TEST_HOST),false);
}

@Test
@Transactional
@Rollback(true)
public void testDeleteDownHosts() {
for (int i = 0; i < 3; i++) {
String name = TEST_HOST + i;
hostDao.insertRenderHost(buildRenderHost(name),
hostManager.getDefaultAllocationDetail(),
false);
if (i != 1) {
HostEntity host = hostDao.findHostDetail(name);
assertEquals(name, host.name);
hostDao.updateHostState(host, HardwareState.DOWN);
}
}

hostDao.deleteDownHosts();

for (int i = 0; i < 3; i++) {
String name = TEST_HOST + i;
assertEquals(hostDao.hostExists(name), i == 1);
}
}

@Test
@Transactional
@Rollback(true)
Expand Down

0 comments on commit 401a33b

Please sign in to comment.