-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #348 from mp-access/hotfix/cleanup-in-memory-repo
Hotfix/cleanup in memory repo
- Loading branch information
Showing
5 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
src/main/java/ch/uzh/ifi/access/student/config/SchedulingConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package ch.uzh.ifi.access.student.config; | ||
|
||
import ch.uzh.ifi.access.student.evaluation.process.EvalMachineRepoService; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.EnableScheduling; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
|
||
import java.time.Instant; | ||
import java.time.temporal.ChronoUnit; | ||
|
||
@Configuration | ||
@EnableScheduling | ||
public class SchedulingConfig { | ||
|
||
private static Logger logger = LoggerFactory.getLogger(SchedulingConfig.class); | ||
|
||
private static final long FIXED_DELAY_IN_MINUTES = 5; | ||
|
||
private EvalMachineRepoService machineRepository; | ||
|
||
public SchedulingConfig(EvalMachineRepoService machineRepository) { | ||
this.machineRepository = machineRepository; | ||
} | ||
|
||
@Scheduled(fixedDelay = FIXED_DELAY_IN_MINUTES * 60000) | ||
public void cleanUpRepo() { | ||
Instant threshold = Instant.now().minus(5, ChronoUnit.MINUTES); | ||
logger.debug("Starting state machine cleanup. Repository size {}, removing machine older than {}", machineRepository.count(), threshold); | ||
machineRepository.removeMachinesOlderThan(threshold); | ||
logger.debug("Completed cleanup. Repository size {}", machineRepository.count()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/java/ch/uzh/ifi/access/student/evaluation/process/StateMachineEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package ch.uzh.ifi.access.student.evaluation.process; | ||
|
||
import org.springframework.statemachine.StateMachine; | ||
import org.springframework.statemachine.listener.StateMachineListenerAdapter; | ||
import org.springframework.statemachine.state.State; | ||
|
||
import java.time.Instant; | ||
|
||
public class StateMachineEventListener | ||
extends StateMachineListenerAdapter<EvalMachine.States, EvalMachine.Events> { | ||
|
||
private StateMachine<EvalMachine.States, EvalMachine.Events> machine; | ||
|
||
public StateMachineEventListener(StateMachine<EvalMachine.States, EvalMachine.Events> machine) { | ||
this.machine = machine; | ||
} | ||
|
||
@Override | ||
public void stateEntered(State<EvalMachine.States, EvalMachine.Events> state) { | ||
if (EvalMachine.States.FINISHED.equals(state.getId())) { | ||
machine.getExtendedState().getVariables().put(EvalMachineFactory.EXTENDED_VAR_COMPLETION_TIME, Instant.now()); | ||
} | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/test/java/ch/uzh/ifi/access/student/evaluation/process/EvalMachineRepoServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package ch.uzh.ifi.access.student.evaluation.process; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.Test; | ||
import org.springframework.statemachine.StateMachine; | ||
|
||
import java.time.Instant; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.UUID; | ||
|
||
public class EvalMachineRepoServiceTest { | ||
|
||
@Test | ||
public void cleanUpNoMachines() { | ||
EvalMachineRepoService repo = new EvalMachineRepoService(); | ||
repo.removeMachinesOlderThan(Instant.now()); | ||
} | ||
|
||
@Test | ||
public void cleanUp() throws Exception { | ||
String id1 = UUID.randomUUID().toString(); | ||
String id2 = UUID.randomUUID().toString(); | ||
StateMachine<EvalMachine.States, EvalMachine.Events> m1 = EvalMachineFactory.initSMForSubmission("123"); | ||
StateMachine<EvalMachine.States, EvalMachine.Events> m2 = EvalMachineFactory.initSMForSubmission("345"); | ||
|
||
m1.getExtendedState().getVariables().put(EvalMachineFactory.EXTENDED_VAR_COMPLETION_TIME, Instant.now().minus(1, ChronoUnit.MINUTES)); | ||
m2.getExtendedState().getVariables().put(EvalMachineFactory.EXTENDED_VAR_COMPLETION_TIME, Instant.now().minus(30, ChronoUnit.MINUTES)); | ||
|
||
EvalMachineRepoService repo = new EvalMachineRepoService(); | ||
repo.store(id1, m1); | ||
repo.store(id2, m2); | ||
|
||
Assertions.assertThat(repo.get(id1)).isNotNull(); | ||
Assertions.assertThat(repo.get(id2)).isNotNull(); | ||
|
||
Instant fiveMinutesAgo = Instant.now().minus(5, ChronoUnit.MINUTES); | ||
repo.removeMachinesOlderThan(fiveMinutesAgo); | ||
|
||
Assertions.assertThat(repo.get(id1)).isNotNull(); | ||
Assertions.assertThat(repo.get(id2)).isNull(); | ||
} | ||
|
||
@Test | ||
public void noMachinesToClean() throws Exception { | ||
String id1 = UUID.randomUUID().toString(); | ||
String id2 = UUID.randomUUID().toString(); | ||
StateMachine<EvalMachine.States, EvalMachine.Events> m1 = EvalMachineFactory.initSMForSubmission("123"); | ||
StateMachine<EvalMachine.States, EvalMachine.Events> m2 = EvalMachineFactory.initSMForSubmission("345"); | ||
|
||
m1.getExtendedState().getVariables().put(EvalMachineFactory.EXTENDED_VAR_COMPLETION_TIME, Instant.now().minus(1, ChronoUnit.MINUTES)); | ||
m2.getExtendedState().getVariables().put(EvalMachineFactory.EXTENDED_VAR_COMPLETION_TIME, Instant.now().minus(1, ChronoUnit.MINUTES)); | ||
|
||
EvalMachineRepoService repo = new EvalMachineRepoService(); | ||
repo.store(id1, m1); | ||
repo.store(id2, m2); | ||
|
||
Assertions.assertThat(repo.get(id1)).isNotNull(); | ||
Assertions.assertThat(repo.get(id2)).isNotNull(); | ||
|
||
Instant fiveMinutesAgo = Instant.now().minus(5, ChronoUnit.MINUTES); | ||
repo.removeMachinesOlderThan(fiveMinutesAgo); | ||
|
||
Assertions.assertThat(repo.get(id1)).isNotNull(); | ||
Assertions.assertThat(repo.get(id2)).isNotNull(); | ||
} | ||
} |