From b9513071bf3a8fb5f56358f2ed23a2cd34046220 Mon Sep 17 00:00:00 2001 From: Florian Held Date: Wed, 10 Jul 2019 18:22:03 +0200 Subject: [PATCH] set correct id in evictBySolution and implemented sync checks --- .../matchmaking/ocl/ByonNodeCache.java | 58 +++++++++++++++++-- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/matchmaking-agent/src/main/java/org/cloudiator/matchmaking/ocl/ByonNodeCache.java b/matchmaking-agent/src/main/java/org/cloudiator/matchmaking/ocl/ByonNodeCache.java index e82689f..12f903c 100644 --- a/matchmaking-agent/src/main/java/org/cloudiator/matchmaking/ocl/ByonNodeCache.java +++ b/matchmaking-agent/src/main/java/org/cloudiator/matchmaking/ocl/ByonNodeCache.java @@ -12,6 +12,7 @@ import de.uniulm.omi.cloudiator.sword.domain.OfferQuota.OfferType; import de.uniulm.omi.cloudiator.sword.domain.Quota; import de.uniulm.omi.cloudiator.sword.domain.Quotas; +import io.github.cloudiator.domain.ByonIdCreator; import java.math.BigDecimal; import java.util.HashMap; import java.util.Map; @@ -20,6 +21,8 @@ import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; +import org.cloudiator.matchmaking.converters.GeoLocationConverter; +import org.cloudiator.matchmaking.converters.OperatingSystemConverter; import org.cloudiator.matchmaking.domain.NodeCandidate; import org.cloudiator.matchmaking.domain.Solution; import org.cloudiator.messages.Byon.ByonNode; @@ -30,6 +33,8 @@ public final class ByonNodeCache { private static final Logger LOGGER = LoggerFactory.getLogger(ByonNodeCache.class); + private static final OperatingSystemConverter OS_CONVERTER = new OperatingSystemConverter(); + private static final GeoLocationConverter GEO_LOCATION_CONVERTER = new GeoLocationConverter(); private final ByonUpdater updater; private final Set expirableSet; private volatile Map byonNodeCache = new HashMap<>(); @@ -66,10 +71,19 @@ public synchronized Optional add(ByonNode node) { return Optional.ofNullable(origByonNode); } - public void evictBySolution(Solution solution, String userId) { + public synchronized void evictBySolution(Solution solution, String userId) { + io.github.cloudiator.messaging.OperatingSystemConverter OS_DOMAIN_CONVERTER = + new io.github.cloudiator.messaging.OperatingSystemConverter(); + io.github.cloudiator.messaging.GeoLocationMessageToGeoLocationConverter GEO_LOC_DOMAIN_CONVERTER = + new io.github.cloudiator.messaging.GeoLocationMessageToGeoLocationConverter(); + for (NodeCandidate nodeCandidate : solution.getNodeCandidates()) { if (nodeCandidate.getType().equals(NodeType.BYON)) { - evictTemp(nodeCandidate.id(), userId); + String id = ByonIdCreator.createId(nodeCandidate.getHardware().getCores(), + nodeCandidate.getHardware().getRam(), nodeCandidate.getHardware().getDisk(), + OS_DOMAIN_CONVERTER.apply(OS_CONVERTER.applyBack(nodeCandidate.getImage().getOperatingSystem())), + GEO_LOC_DOMAIN_CONVERTER.apply(GEO_LOCATION_CONVERTER.applyBack(nodeCandidate.getLocation().getGeoLocation()))); + evictTemp(id, userId); } } } @@ -83,10 +97,16 @@ public synchronized Optional evictTemp(String id, String userId) { } public synchronized Optional evict(String id, String userId) { - ByonCacheKey key = new ByonCacheKey(id, userId); + boolean syncProblemPresent = checkForSyncProblems(key); + + if (syncProblemPresent) { + LOGGER.error(String.format("%s could not consistently evict byon with id %s from " + + "cache as there were sync problems", this, id)); + return null; + } - // if in temp remove + // if in temp remove & insert record back into byonCache tempCache.invalidate(key); if (!hit(key.getNodeId(), key.getUserId()).isPresent()) { @@ -105,6 +125,35 @@ public synchronized Optional evict(String id, String userId) { return Optional.of(evictNode); } + private synchronized boolean checkForSyncProblems(ByonCacheKey key) { + ByonNode inTmpCacheNode = tempCache.getIfPresent(key); + ByonNode inByonCacheNode = byonNodeCache.get(key); + + // Correct state + if (inTmpCacheNode != null && inByonCacheNode == null) { + return false; + } + + if (inTmpCacheNode == null) { + if (inByonCacheNode == null) { + LOGGER.error(String.format("Byon-Node with id %s that was assigned to " + + "a solution seems not to got cached in the temp-cache formerly", key.nodeId)); + } + if (inByonCacheNode != null) { + LOGGER.error(String.format("Severe problem encountered. Byon-Node with id %s that was assigned to " + + "a solution resides in byon-cache but not in temp-cache. Correct state would be the other" + + "way around.", key.nodeId)); + } + } + + if (inTmpCacheNode != null && inByonCacheNode != null) { + LOGGER.error(String.format("Byon-Node with id %s that was assigned to " + + "a solution seems not to got evicted temporarily from the byon-cache.", key.nodeId)); + } + + return true; + } + private synchronized void publishUpdate() { updater.update(this); } @@ -125,6 +174,7 @@ public synchronized Optional read(String id, String userId) { return Optional.empty(); } + return Optional.of(byonNodeCache.get(key)); }