Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
SUBMARINE-1342. Remove experiment template cache in server (#1088)
Browse files Browse the repository at this point in the history
* Remove cache

* Fix test case check
  • Loading branch information
cdmikechen authored Jul 20, 2023
1 parent dbcf369 commit 6686f18
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -72,14 +70,6 @@ public class ExperimentTemplateManager {
.registerTypeAdapter(ExperimentId.class, new ExperimentIdDeserializer());
private static final Gson gson = gsonBuilder.setDateFormat("yyyy-MM-dd HH:mm:ss").create();



/**
* ExperimentTemplate Cache.
*/
private final ConcurrentMap<String, ExperimentTemplate> cachedExperimentTemplates =
new ConcurrentHashMap<>();

/**
* Get the singleton instance.
*
Expand Down Expand Up @@ -174,8 +164,6 @@ private ExperimentTemplate createOrUpdateExperimentTemplate(ExperimentTemplateSp
throw new SubmarineRuntimeException(Status.BAD_REQUEST.getStatusCode(),
"Unable to parse the experimentTemplate spec: " + e.getMessage());
}
// Update cache
cachedExperimentTemplates.put(spec.getName(), experimentTemplate);

return experimentTemplate;
}
Expand Down Expand Up @@ -206,8 +194,6 @@ public ExperimentTemplate deleteExperimentTemplate(String name) throws Submarine
experimentTemplateMapper.delete(name);
sqlSession.commit();

// Invalidate cache
cachedExperimentTemplates.remove(name);
return tpl;
} catch (Exception e) {
LOG.error(e.getMessage(), e);
Expand Down Expand Up @@ -239,12 +225,7 @@ public ExperimentTemplate getExperimentTemplate(String name) throws SubmarineRun
* @throws SubmarineRuntimeException the service error
*/
public List<ExperimentTemplate> listExperimentTemplates(String status) throws SubmarineRuntimeException {
List<ExperimentTemplate> tpls = new ArrayList<>(cachedExperimentTemplates.values());

// Is it available in cache?
if (tpls.size() != 0) {
return tpls;
}
List<ExperimentTemplate> tpls = new ArrayList<>();
try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
ExperimentTemplateMapper experimentTemplateMapper =
sqlSession.getMapper(ExperimentTemplateMapper.class);
Expand All @@ -253,12 +234,10 @@ public List<ExperimentTemplate> listExperimentTemplates(String status) throws Su
for (ExperimentTemplateEntity experimentTemplateEntity : experimentTemplateEntities) {
if (experimentTemplateEntity != null) {
ExperimentTemplate tpl = new ExperimentTemplate();

tpl.setExperimentTemplateSpec(
gson.fromJson(experimentTemplateEntity.getExperimentTemplateSpec(),
ExperimentTemplateSpec.class));
tpls.add(tpl);
cachedExperimentTemplates.put(tpl.getExperimentTemplateSpec().getName(), tpl);
}
}
} catch (Exception e) {
Expand All @@ -276,20 +255,18 @@ private void checkSpec(ExperimentTemplateSpec spec) throws SubmarineRuntimeExcep
}
}

/**
* Get experiment template by name
*
* @param name experiment template name
*/
private ExperimentTemplate getExperimentTemplateDetails(String name) throws SubmarineRuntimeException {

// Is it available in cache?
ExperimentTemplate tpl = cachedExperimentTemplates.get(name);
if (tpl != null) {
return tpl;
}
ExperimentTemplateEntity experimentTemplateEntity;
ExperimentTemplate tpl = null;
try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
ExperimentTemplateMapper experimentTemplateMapper =
sqlSession.getMapper(ExperimentTemplateMapper.class);

experimentTemplateEntity = experimentTemplateMapper.select(name);

ExperimentTemplateEntity experimentTemplateEntity = experimentTemplateMapper.select(name);
if (experimentTemplateEntity != null) {
tpl = new ExperimentTemplate();
tpl.setExperimentTemplateSpec(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ public void testListExperimentTemplates() throws Exception {
gson.fromJson(gson.toJson(jsonResponse.getResult()), new TypeToken<List<ExperimentTemplate>>() {
}.getType());

Assert.assertEquals(TPL_NAME, getExperimentTemplates.get(0).getExperimentTemplateSpec().getName());
Assert.assertTrue(getExperimentTemplates.stream()
.anyMatch(t -> TPL_NAME.equals(t.getExperimentTemplateSpec().getName())));

deleteExperimentTemplate();
}
Expand Down

0 comments on commit 6686f18

Please sign in to comment.