Fix issues in data archival feature

There were few issues regarding the data retreiving SQL query and archived config operations
4.x.x
Milan Perera 7 years ago
parent 6b99197b54
commit 415aac1ce0

@ -111,6 +111,12 @@ public class ArchivalServiceImpl implements ArchivalService {
}
archivalDAO.moveProfileOperations();
//Purge the config operation table, DM_CONFIG_OPERATION
if (log.isDebugEnabled()) {
log.debug("## Purging config operations");
}
archivalDAO.moveConfigOperations();
//Purge the enrolment mappings table, DM_ENROLMENT_OP_MAPPING
if (log.isDebugEnabled()) {
log.debug("## Purging enrolment mappings");

@ -41,6 +41,8 @@ public interface ArchivalDAO {
void moveProfileOperations() throws ArchivalDAOException;
void moveConfigOperations() throws ArchivalDAOException;
void moveEnrolmentMappings() throws ArchivalDAOException;
void moveOperations() throws ArchivalDAOException;

@ -56,8 +56,8 @@ public class ArchivalDAOImpl implements ArchivalDAO {
try {
Connection conn = ArchivalSourceDAOFactory.getConnection();
String sql = "SELECT DISTINCT OPERATION_ID FROM DM_ENROLMENT_OP_MAPPING " +
"WHERE CREATED_TIMESTAMP BETWEEN DATE_SUB(NOW(), INTERVAL " +
this.retentionPeriod + " DAY) AND NOW()";
"WHERE CREATED_TIMESTAMP BETWEEN DATE(TIMESTAMPADD(DAY, " +
this.retentionPeriod + ", NOW())) AND NOW()";
stmt = this.createMemoryEfficientStatement(conn);
rs = stmt.executeQuery(sql);
while (rs.next()) {
@ -83,7 +83,8 @@ public class ArchivalDAOImpl implements ArchivalDAO {
Connection conn = ArchivalSourceDAOFactory.getConnection();
String sql = "SELECT DISTINCT OPERATION_ID " +
" FROM DM_ENROLMENT_OP_MAPPING WHERE STATUS IN('PENDING', 'IN_PROGRESS') " +
" AND CREATED_TIMESTAMP BETWEEN DATE_SUB(NOW(), INTERVAL " + this.retentionPeriod +" DAY) AND NOW()";
" AND CREATED_TIMESTAMP BETWEEN DATE(TIMESTAMPADD(DAY, " + this.retentionPeriod +", NOW())) " +
"AND NOW()";
stmt = this.createMemoryEfficientStatement(conn);
rs = stmt.executeQuery(sql);
while (rs.next()) {
@ -337,6 +338,56 @@ public class ArchivalDAOImpl implements ArchivalDAO {
}
}
@Override
public void moveConfigOperations() throws ArchivalDAOException {
Statement stmt = null;
PreparedStatement stmt2 = null;
Statement stmt3 = null;
ResultSet rs = null;
try {
Connection conn = ArchivalSourceDAOFactory.getConnection();
String sql = "SELECT * FROM DM_CONFIG_OPERATION WHERE OPERATION_ID IN " +
"(SELECT ID FROM DM_ARCHIVED_OPERATIONS)";
stmt = this.createMemoryEfficientStatement(conn);
rs = stmt.executeQuery(sql);
Connection conn2 = ArchivalDestinationDAOFactory.getConnection();
sql = "INSERT INTO DM_CONFIG_OPERATION_ARCH VALUES(?, ?, ?, ?)";
stmt2 = conn2.prepareStatement(sql);
int count = 0;
while (rs.next()) {
stmt2.setInt(1, rs.getInt("OPERATION_ID"));
stmt2.setBytes(2, rs.getBytes("OPERATION_CONFIG"));
stmt2.setInt(3, rs.getInt("ENABLED"));
stmt2.setTimestamp(4,this.currentTimestamp );
stmt2.addBatch();
if (++count % batchSize == 0) {
stmt2.executeBatch();
}
}
stmt2.executeBatch();
if (log.isDebugEnabled()) {
log.debug(count + " [CONFIG_OPERATION] Records copied to the archival table. Starting deletion");
}
sql = "DELETE FROM DM_CONFIG_OPERATION" +
" WHERE OPERATION_ID IN (SELECT ID FROM DM_ARCHIVED_OPERATIONS)";
stmt3 = conn.createStatement();
int affected = stmt3.executeUpdate(sql);
if (log.isDebugEnabled()) {
log.debug(affected + " Rows deleted");
}
} catch (SQLException e) {
throw new ArchivalDAOException("Error occurred while moving config operations", e);
} finally {
ArchivalDAOUtil.cleanupResources(stmt, rs);
ArchivalDAOUtil.cleanupResources(stmt2);
ArchivalDAOUtil.cleanupResources(stmt3);
}
}
@Override
public void moveEnrolmentMappings() throws ArchivalDAOException {
Statement stmt = null;

@ -29,6 +29,7 @@ public class ArchivalTaskConfiguration {
private int retentionPeriod;
private int batchSize;
private PurgingTaskConfiguration purgingTaskConfiguration;
private final int MULTIPLIER = -1;
@XmlElement(name = "Enabled", required = true)
public boolean isEnabled() {
@ -59,7 +60,8 @@ public class ArchivalTaskConfiguration {
@XmlElement(name = "RetentionPeriod", required = true)
public int getRetentionPeriod() {
return retentionPeriod;
// multiply by -1 to get the diff
return retentionPeriod * MULTIPLIER;
}
public void setRetentionPeriod(int retentionPeriod) {

@ -52,6 +52,14 @@ CREATE TABLE IF NOT EXISTS DM_COMMAND_OPERATION_ARCH (
PRIMARY KEY (OPERATION_ID)
)ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS DM_CONFIG_OPERATION_ARCH (
OPERATION_ID INTEGER NOT NULL,
OPERATION_CONFIG BLOB DEFAULT NULL,
ENABLED BOOLEAN NOT NULL DEFAULT FALSE,
ARCHIVED_AT TIMESTAMP DEFAULT NOW(),
PRIMARY KEY (OPERATION_ID)
)ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS DM_PROFILE_OPERATION_ARCH (
OPERATION_ID INTEGER NOT NULL,
ENABLED INTEGER NOT NULL DEFAULT 0,

Loading…
Cancel
Save