Skip to content

Commit

Permalink
Merge pull request #348 from mp-access/hotfix/cleanup-in-memory-repo
Browse files Browse the repository at this point in the history
Hotfix/cleanup in memory repo
  • Loading branch information
Haeri authored Oct 9, 2019
2 parents 8588930 + 640ebdb commit 4223464
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 0 deletions.
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class EvalMachineFactory {
public static final String EXTENDED_VAR_SUBMISSION_ID = "submissionId";
public static final String EXTENDED_VAR_NEXT_STEP = "nextStep";
public static final String EXTENDED_VAR_NEXT_STEP_DELAY = "nextStepDelay";
public static final String EXTENDED_VAR_COMPLETION_TIME = "completionTime";

public static StateMachine<EvalMachine.States, EvalMachine.Events> initSMForSubmission(String submissionId) throws Exception {

Expand Down Expand Up @@ -57,6 +58,7 @@ public static StateMachine<EvalMachine.States, EvalMachine.Events> initSMForSubm

StateMachine<EvalMachine.States, EvalMachine.Events> machine = builder.build();
machine.getExtendedState().getVariables().put(EXTENDED_VAR_SUBMISSION_ID, submissionId);
machine.addStateListener(new StateMachineEventListener(machine));
return machine;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.statemachine.StateMachine;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

Expand All @@ -27,4 +28,15 @@ public void store(String key, StateMachine machine) {
machines.put(key, machine);
}

public long count() {
return machines.size();
}

public void removeMachinesOlderThan(Instant threshold) {
machines.entrySet().removeIf(entry -> {
StateMachine machine = entry.getValue();
Instant completionTime = (Instant) machine.getExtendedState().getVariables().get(EvalMachineFactory.EXTENDED_VAR_COMPLETION_TIME);
return completionTime.isBefore(threshold);
});
}
}
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());
}
}
}
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();
}
}

0 comments on commit 4223464

Please sign in to comment.