Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<Expirable> expirableSet;
private volatile Map<ByonCacheKey, ByonNode> byonNodeCache = new HashMap<>();
Expand Down Expand Up @@ -67,10 +72,19 @@ public synchronized Optional<ByonNode> 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);
}
}
}
Expand All @@ -84,10 +98,16 @@ public synchronized Optional<ByonNode> evictTemp(String id, String userId) {
}

public synchronized Optional<ByonNode> 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()) {
Expand All @@ -106,6 +126,35 @@ public synchronized Optional<ByonNode> 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);
}
Expand All @@ -126,6 +175,7 @@ public synchronized Optional<ByonNode> read(String id, String userId) {
return Optional.empty();
}


return Optional.of(byonNodeCache.get(key));
}

Expand Down