use of org.apache.commons.lang3.tuple.MutablePair in project apex-core by apache.
the class BlacklistBasedResourceRequestHandler method reissueContainerRequests.
@Override
public void reissueContainerRequests(AMRMClient<ContainerRequest> amRmClient, Map<StreamingContainerAgent.ContainerStartRequest, MutablePair<Integer, ContainerRequest>> requestedResources, int loopCounter, ResourceRequestHandler resourceRequestor, List<ContainerRequest> containerRequests, List<ContainerRequest> removedContainerRequests) {
if (!requestedResources.isEmpty()) {
// Check if any requests timed out, create new requests in that case
recreateContainerRequest(requestedResources, loopCounter, resourceRequestor, removedContainerRequests);
}
// Issue all host specific requests first
if (!hostSpecificRequestsMap.isEmpty()) {
LOG.info("Issue Host specific requests first");
// Blacklist all the nodes and issue request for host specific
Entry<String, List<ContainerRequest>> set = hostSpecificRequestsMap.entrySet().iterator().next();
List<ContainerRequest> requests = set.getValue();
List<String> blacklistNodes = resourceRequestor.getNodesExceptHost(requests.get(0).getNodes());
amRmClient.updateBlacklist(blacklistNodes, requests.get(0).getNodes());
blacklistedNodesForHostSpecificRequests = blacklistNodes;
LOG.info("Sending {} request(s) after blacklisting all nodes other than {}", requests.size(), requests.get(0).getNodes());
for (ContainerRequest cr : requests) {
ContainerStartRequest csr = hostSpecificRequests.get(cr);
ContainerRequest newCr = new ContainerRequest(cr.getCapability(), null, null, cr.getPriority());
MutablePair<Integer, ContainerRequest> pair = new MutablePair<>(loopCounter, newCr);
requestedResources.put(csr, pair);
containerRequests.add(newCr);
hostSpecificRequests.remove(cr);
}
hostSpecificRequestsMap.remove(set.getKey());
} else {
if (blacklistedNodesForHostSpecificRequests != null) {
// Remove the blacklisted nodes during host specific requests
LOG.debug("All requests done.. Removing nodes from blacklist {}", blacklistedNodesForHostSpecificRequests);
amRmClient.updateBlacklist(null, blacklistedNodesForHostSpecificRequests);
blacklistedNodesForHostSpecificRequests = null;
}
// Proceed with other requests after host specific requests are done
if (!otherContainerRequests.isEmpty()) {
for (Entry<ContainerRequest, ContainerStartRequest> entry : otherContainerRequests.entrySet()) {
ContainerRequest cr = entry.getKey();
ContainerStartRequest csr = entry.getValue();
MutablePair<Integer, ContainerRequest> pair = new MutablePair<>(loopCounter, cr);
requestedResources.put(csr, pair);
containerRequests.add(cr);
}
otherContainerRequests.clear();
}
}
}
use of org.apache.commons.lang3.tuple.MutablePair in project apex-core by apache.
the class BlacklistBasedResourceRequestHandler method recreateContainerRequest.
private void recreateContainerRequest(Map<StreamingContainerAgent.ContainerStartRequest, MutablePair<Integer, ContainerRequest>> requestedResources, int loopCounter, ResourceRequestHandler resourceRequestor, List<ContainerRequest> removedContainerRequests) {
for (Map.Entry<StreamingContainerAgent.ContainerStartRequest, MutablePair<Integer, ContainerRequest>> entry : requestedResources.entrySet()) {
if ((loopCounter - entry.getValue().getKey()) > NUMBER_MISSED_HEARTBEATS) {
StreamingContainerAgent.ContainerStartRequest csr = entry.getKey();
removedContainerRequests.add(entry.getValue().getRight());
ContainerRequest cr = resourceRequestor.createContainerRequest(csr, false);
if (cr.getNodes() != null && !cr.getNodes().isEmpty()) {
addHostSpecificRequest(csr, cr);
} else {
otherContainerRequests.put(cr, csr);
}
}
}
}
use of org.apache.commons.lang3.tuple.MutablePair in project PneumaticCraft by MineMaarten.
the class TileEntityPneumaticBase method disperseAir.
private void disperseAir(List<Pair<ForgeDirection, IAirHandler>> teList) {
boolean shouldRepeat = false;
List<Pair<Integer, Integer>> dispersion = new ArrayList<Pair<Integer, Integer>>();
do {
shouldRepeat = false;
// Add up every volume and air.
int totalVolume = getVolume();
int totalAir = currentAir;
for (Pair<ForgeDirection, IAirHandler> entry : teList) {
IAirHandler airHandler = entry.getValue();
totalVolume += airHandler.getVolume();
totalAir += airHandler.getCurrentAir(entry.getKey().getOpposite());
}
// Only go push based, ignore any machines that have a higher pressure than this block.
Iterator<Pair<ForgeDirection, IAirHandler>> iterator = teList.iterator();
while (iterator.hasNext()) {
Pair<ForgeDirection, IAirHandler> entry = iterator.next();
IAirHandler airHandler = entry.getValue();
// Calculate the total air the machine is going to get.
int totalMachineAir = (int) ((long) totalAir * airHandler.getVolume() / totalVolume);
int airDispersed = totalMachineAir - airHandler.getCurrentAir(entry.getKey().getOpposite());
if (airDispersed < 0) {
iterator.remove();
shouldRepeat = true;
dispersion.clear();
break;
} else {
dispersion.add(new MutablePair(getMaxDispersion(entry.getKey()), airDispersed));
}
}
} while (shouldRepeat);
int toBeDivided = 0;
int receivers = dispersion.size();
for (Pair<Integer, Integer> disp : dispersion) {
if (disp.getValue() > disp.getKey()) {
// Any air that wants to go to a neighbor, but can't (because of regulator module) gives back its air.
toBeDivided += disp.getValue() - disp.getKey();
disp.setValue(disp.getKey());
receivers--;
}
}
while (toBeDivided >= receivers && receivers > 0) {
// try to give every receiver an equal part of the to be divided air.
int dividedValue = toBeDivided / receivers;
for (Pair<Integer, Integer> disp : dispersion) {
int maxTransfer = disp.getKey() - disp.getValue();
if (maxTransfer > 0) {
if (maxTransfer <= dividedValue) {
// next step this receiver won't be able to receive any air.
receivers--;
}
// cap it at the max it can have.
int transfered = Math.min(dividedValue, maxTransfer);
disp.setValue(disp.getValue() + transfered);
toBeDivided -= transfered;
} else {
receivers--;
}
}
}
for (int i = 0; i < teList.size(); i++) {
IAirHandler neighbor = teList.get(i).getValue();
int transferedAir = dispersion.get(i).getValue();
onAirDispersion(transferedAir, teList.get(i).getKey());
neighbor.addAir(transferedAir, teList.get(i).getKey().getOpposite());
addAir(-transferedAir, teList.get(i).getKey());
}
}
use of org.apache.commons.lang3.tuple.MutablePair in project shifu by ShifuML.
the class ShifuCLI method analysisModelFi.
public static int analysisModelFi(String modelPath) {
File modelFile = new File(modelPath);
if (!modelFile.exists() || !(modelPath.toUpperCase().endsWith("." + CommonConstants.GBT_ALG_NAME) || modelPath.toUpperCase().endsWith("." + CommonConstants.RF_ALG_NAME))) {
log.error("The model {} doesn't exist or it isn't GBT/RF model.", modelPath);
return 1;
}
FileInputStream inputStream = null;
String fiFileName = modelFile.getName() + ".fi";
try {
inputStream = new FileInputStream(modelFile);
BasicML basicML = TreeModel.loadFromStream(inputStream);
Map<Integer, MutablePair<String, Double>> featureImportances = CommonUtils.computeTreeModelFeatureImportance(Arrays.asList(new BasicML[] { basicML }));
CommonUtils.writeFeatureImportance(fiFileName, featureImportances);
} catch (IOException e) {
log.error("Fail to analysis model FI for {}", modelPath);
return 1;
} finally {
IOUtils.closeQuietly(inputStream);
}
return 0;
}
use of org.apache.commons.lang3.tuple.MutablePair in project azure-iot-sdk-java by Azure.
the class MqttDeviceMethodTest method receiveThrowsIfMethodNameCouldNotBeParsed.
// Tests_SRS_MQTTDEVICEMETHOD_25_029: [**If method name not found or is null then receive shall throw TransportException **]**
@Test(expected = TransportException.class)
public void receiveThrowsIfMethodNameCouldNotBeParsed() throws TransportException {
// arrange
String topic = "$iothub/methods/POST/";
byte[] actualPayload = "TestPayload".getBytes(StandardCharsets.UTF_8);
testreceivedMessages.add(new MutablePair<>(topic, actualPayload));
MqttDeviceMethod testMethod = new MqttDeviceMethod("", mockConnectOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
Deencapsulation.setField(testMethod, "receivedMessages", testreceivedMessages);
testMethod.start();
// act
testMethod.receive();
}
Aggregations