Commit 62df35e0 authored by Julian Zobel's avatar Julian Zobel
Browse files

Merge branch 'jz/merge-disasterPrio' into jz/master

parents 5f338d24 851364a9
package de.tudarmstadt.maki.simonstrator.api;
/**
* A message that has some sort of disaster type with a priority.
*
* @author Simon Luser
*
*/
public interface MessageWithDisasterType extends Message {
public static enum MessageDisasterType {
Emergency(1), Warning(2), SearchFor(3), SimpleMsg(4);
private int priority;
private MessageDisasterType(int priority) {
this.priority = priority;
}
public int getPriorityValue() {
return priority;
}
}
public MessageDisasterType getDisasterType();
}
package de.tudarmstadt.maki.simonstrator.api.util;
import java.util.List;
import java.util.stream.Collectors;
import de.tudarmstadt.maki.simonstrator.api.Oracle;
import de.tudarmstadt.maki.simonstrator.api.common.graph.INodeID;
import de.tudarmstadt.maki.simonstrator.api.component.ComponentNotAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.overlay.OverlayComponent;
/**
* Node filtering methods.
*
* @author Simon Luser
*
*/
public final class Filter {
/**
* @return a list of node-IDs that are online
*/
public static List<INodeID> getActiveNodes() {
return Oracle.getAllHosts().stream().filter(host -> {
try {
return host.getComponent(OverlayComponent.class).isPresent();
} catch (ComponentNotAvailableException e) {
return false;
}
}).map(n -> n.getId()).collect(Collectors.toList());
}
/**
*
* @return a list of all Node IDs
*/
public static List<INodeID> getAllNodeIDs() {
return Oracle.getAllHosts().stream().map(node -> node.getId()).collect(Collectors.toList());
}
}
package de.tudarmstadt.maki.simonstrator.api.util.nodeselections;
import java.util.List;
import de.tudarmstadt.maki.simonstrator.api.common.graph.INodeID;
/**
* Abstract class for the node selection strategy. Defines which nodes are
* picked, in order to follow the given load distribution or trace.
*
* @author ConstiZ
*
*/
public abstract class AbstractNodeSelection {
protected List<INodeID> allNodeIDs;
public void setAllNodeIDs(List<INodeID> allIDs) {
this.allNodeIDs = allIDs;
}
/**
* Selects nodes ID's according to a strategy.
*
* @param nodeCount
* Amount of nodes that should be selected. (given by
* distribution)
* @param activeNodeIDs
* List of all active Nodes ID's to select from.
* @return List of selected Node ID's according to strategy
* @throws IllegalSelectionException
*/
public abstract List<INodeID> selectNodes(int nodeCount, List<INodeID> activeNodeIDs)
throws IllegalSelectionException;
/**
* Validates input for {@link #selectNodes(int, List)}
*
* @param nodeCount
* @param allNodeIDs
* @return
* @throws IllegalSelectionException
*/
protected static boolean validateSelection(int nodeCount, List<INodeID> allNodeIDs)
throws IllegalSelectionException {
if (nodeCount < 0)
throw new IllegalSelectionException("Negative nodeCount");
if (nodeCount > allNodeIDs.size())
throw new IllegalSelectionException("List too short");
return true;
}
public static final class IllegalSelectionException extends Exception {
private static final long serialVersionUID = 1L;
public IllegalSelectionException(String message) {
super(message);
}
}
}
package de.tudarmstadt.maki.simonstrator.api.util.nodeselections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import de.tudarmstadt.maki.simonstrator.api.Randoms;
import de.tudarmstadt.maki.simonstrator.api.common.graph.INodeID;
/**
* Selects random node ID's on every call.
*
* @author ConstiZ
*
*/
public final class FullyRandomNodeSelection extends AbstractNodeSelection {
private Random rnd = Randoms.getRandom(FullyRandomNodeSelection.class);
@Override
public List<INodeID> selectNodes(int nodeCount, List<INodeID> allNodeIDs) throws IllegalSelectionException {
validateSelection(nodeCount, allNodeIDs);
List<INodeID> selection = new ArrayList<INodeID>(allNodeIDs);
Collections.shuffle(selection, rnd);
selection.subList(nodeCount, allNodeIDs.size()).clear();
return selection;
}
}
package de.tudarmstadt.maki.simonstrator.api.util.nodeselections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import de.tudarmstadt.maki.simonstrator.api.Monitor;
import de.tudarmstadt.maki.simonstrator.api.Monitor.Level;
import de.tudarmstadt.maki.simonstrator.api.Randoms;
import de.tudarmstadt.maki.simonstrator.api.common.graph.INodeID;
/**
* Determines initially random order of nodes ID's and tries to select the same
* ones in the same order on consecutive calls. A node is skipped if it was
* offline, to follow the distribution.
*
* @author ConstiZ
*
*/
public final class InitialRandomNodeSelection extends AbstractNodeSelection {
private Random rnd;
private List<INodeID> initialSelection;
public InitialRandomNodeSelection() {
rnd = Randoms.getRandom(InitialRandomNodeSelection.class);
}
@Override
public void setAllNodeIDs(List<INodeID> allIDs) {
this.allNodeIDs = allIDs;
initialSelection = new ArrayList<INodeID>(allNodeIDs);
Collections.shuffle(initialSelection, rnd);
Monitor.log(InitialRandomNodeSelection.class, Level.DEBUG, "Initial node selection: " + initialSelection);
}
@Override
public List<INodeID> selectNodes(int nodeCount, List<INodeID> allNodeIDs) throws IllegalSelectionException {
validateSelection(nodeCount, allNodeIDs);
// Select nodes from initialSelection, if they are in allNodes
List<INodeID> selection = initialSelection.stream().filter(node -> allNodeIDs.contains(node))
.collect(Collectors.toList());
if (selection.size() < nodeCount)
Monitor.log(InitialRandomNodeSelection.class, Level.ERROR, "Initial selection too short!");
selection.subList(nodeCount, selection.size()).clear();
return selection;
}
}
package de.tudarmstadt.maki.simonstrator.api.util.nodeselections;
import java.util.ArrayList;
import java.util.List;
import de.tudarmstadt.maki.simonstrator.api.common.graph.INodeID;
/**
* Selects the first nodeCount nodes from AllNodeIDs.
*
* @author ConstiZ
*
*/
public final class StaticNodeSelection extends AbstractNodeSelection {
@Override
public List<INodeID> selectNodes(int nodeCount, List<INodeID> allNodeIDs) throws IllegalSelectionException {
validateSelection(nodeCount, allNodeIDs);
List<INodeID> selection = new ArrayList<INodeID>(allNodeIDs);
selection.subList(nodeCount, allNodeIDs.size()).clear();
return selection;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment