@Override public synchronized void addElement(T val) { if (fStartTimesIndex.put(Long.valueOf(val.getStart()), val)) { fEndTimesIndex.put(Long.valueOf(val.getEnd()), val); fSize++; } }
@Override public Metric deserialize( JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { JsonObject jsonObject = jsonElement.getAsJsonObject(); String name = null; if (jsonObject.get("name") != null) name = jsonObject.get("name").getAsString(); boolean exclude_tags = false; if (jsonObject.get("exclude_tags") != null) exclude_tags = jsonObject.get("exclude_tags").getAsBoolean(); TreeMultimap<String, String> tags = TreeMultimap.create(); JsonElement jeTags = jsonObject.get("tags"); if (jeTags != null) { JsonObject joTags = jeTags.getAsJsonObject(); int count = 0; for (Map.Entry<String, JsonElement> tagEntry : joTags.entrySet()) { String context = "tags[" + count + "]"; if (tagEntry.getKey().isEmpty()) throw new ContextualJsonSyntaxException(context, "name must not be empty"); if (tagEntry.getValue().isJsonArray()) { for (JsonElement element : tagEntry.getValue().getAsJsonArray()) { if (element.isJsonNull() || element.getAsString().isEmpty()) throw new ContextualJsonSyntaxException( context + "." + tagEntry.getKey(), "value must not be null or empty"); tags.put(tagEntry.getKey(), element.getAsString()); } } else { if (tagEntry.getValue().isJsonNull() || tagEntry.getValue().getAsString().isEmpty()) throw new ContextualJsonSyntaxException( context + "." + tagEntry.getKey(), "value must not be null or empty"); tags.put(tagEntry.getKey(), tagEntry.getValue().getAsString()); } count++; } } Metric ret = new Metric(name, exclude_tags, tags); JsonElement limit = jsonObject.get("limit"); if (limit != null) ret.setLimit(limit.getAsInt()); return (ret); }
private void handleIncomingConfirmableCoapRequest(ChannelHandlerContext ctx, MessageEvent me) { InetSocketAddress remoteEndpoint = (InetSocketAddress) me.getRemoteAddress(); CoapMessage coapMessage = (CoapMessage) me.getMessage(); IncomingReliableMessageExchange newMessageExchange = new IncomingReliableMessageExchange(remoteEndpoint, coapMessage.getMessageID()); IncomingMessageExchange oldMessageExchange = ongoingMessageExchanges.get(remoteEndpoint, coapMessage.getMessageID()); // Check if there is an ongoing if (oldMessageExchange != null) { if (oldMessageExchange instanceof IncomingReliableMessageExchange) { // if the old message exchange is reliable and the empty ACK was already sent send another // empty ACK if (((IncomingReliableMessageExchange) oldMessageExchange).isAcknowledgementSent()) writeEmptyAcknowledgement(remoteEndpoint, coapMessage.getMessageID()); } // if the old message was unreliable and the duplicate message is confirmable send empty ACK else writeEmptyAcknowledgement(remoteEndpoint, coapMessage.getMessageID()); // As the message is already being processed there is nothing more to do return; } // try to add new reliable message exchange boolean added = false; synchronized (monitor) { Long time = System.currentTimeMillis() + MIN_EMPTY_ACK_DELAY_MILLIS; // Add message exchange to set of ongoing exchanges to detect duplicates if (!ongoingMessageExchanges.contains(remoteEndpoint, coapMessage.getMessageID())) { ongoingMessageExchanges.put(remoteEndpoint, coapMessage.getMessageID(), newMessageExchange); added = true; } // If the scheduling of the empty ACK does not work then it was already scheduled if (!emptyAcknowledgementSchedule.put(time, newMessageExchange)) { log.error("Could not schedule empty ACK for message: {}", coapMessage); ongoingMessageExchanges.remove(remoteEndpoint, coapMessage.getMessageID()); added = false; } } // everything is fine, so further process message if (added) ctx.sendUpstream(me); }
@Test public void buildInt() throws IOException { final TreeMultimap<Integer, Integer> elements = TreeMultimap.create(); for (int i = 0; i < DOCS; i++) { elements.put(i / 2, i); } final com.yandex.yoctodb.util.mutable.IndexToIndexMultiMap mutable = new com.yandex.yoctodb.util.mutable.impl.IntIndexToIndexMultiMap(elements.asMap().values()); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); mutable.writeTo(baos); final Buffer buf = Buffer.from(baos.toByteArray()); final IndexToIndexMultiMap result = IndexToIndexMultiMapReader.from(buf); assertTrue(result instanceof IntIndexToIndexMultiMap); }
private void identifyDuplicates(List<ModContainer> mods) { TreeMultimap<ModContainer, File> dupsearch = TreeMultimap.create(new ModIdComparator(), Ordering.arbitrary()); for (ModContainer mc : mods) { if (mc.getSource() != null) { dupsearch.put(mc, mc.getSource()); } } ImmutableMultiset<ModContainer> duplist = Multisets.copyHighestCountFirst(dupsearch.keys()); SetMultimap<ModContainer, File> dupes = LinkedHashMultimap.create(); for (Entry<ModContainer> e : duplist.entrySet()) { if (e.getCount() > 1) { FMLLog.severe( "Found a duplicate mod %s at %s", e.getElement().getModId(), dupsearch.get(e.getElement())); dupes.putAll(e.getElement(), dupsearch.get(e.getElement())); } } if (!dupes.isEmpty()) { throw new DuplicateModsFoundException(dupes); } }
/** * Returns a HashMap containing the scores of the hosts. The slowest *normal* host gets a score of * 1.0 and the fastest gets 0. If the host was a straggler, its score is set to * HostMetric.STRAGGLER. * * @return a HashMap of the host name and its score */ @Override public HashMap<String, Double> getHostsScores(String queryID) { HashMap<String, Double> scores = new HashMap<>(); List<String> hostNames = myMetadataTracker.getHostNamesInQuery(queryID); if (hostNames == null || hostNames.size() == 0) { // Unknown query ID return null; } // Order the hosts by their slowness and assign a slowness score for each starting from 0 to 1.0 TreeMultimap<Long, String> hostsDeltaTime = TreeMultimap.create(); double sum = 0; double sumSquared = 0; double numberOfSamples = 0; synchronized (hostNames) { for (String hostName : hostNames) { Long deltaTime = myMetadataTracker.getHostDeltaTime(queryID, hostName); if (deltaTime == null && !myMetadataTracker.isRusher(queryID, hostName)) { // That's not a rusher and didn't finish then it is a straggler scores.put(hostName, HostMetric.STRAGGLER); continue; } if (deltaTime != null) { sum += deltaTime; sumSquared += deltaTime * deltaTime; numberOfSamples++; hostsDeltaTime.put(deltaTime, hostName); } // Else this host is a rusher } } if (numberOfSamples == 0) { // We had one host only and it is a straggler return scores; } // n cannot be 0 from here on.... // Now mark the non-stragglers but slow hosts double mean = sum / numberOfSamples; double variance = (sumSquared - 2 * sum * mean + numberOfSamples * mean * mean) / numberOfSamples; double stdDevsAway = mean + multipleStdDevs * Math.sqrt(variance); if (log.isTraceEnabled()) { log.trace( "mean:" + mean + " stddev:" + Math.sqrt(variance) + " multiple:" + multipleStdDevs + " stdDevsAway:" + stdDevsAway); } ArrayList<Long> keysToDelete = new ArrayList<>(); for (Map.Entry<Long, String> e : hostsDeltaTime.entries()) { if (e.getKey() > stdDevsAway) { keysToDelete.add(e.getKey()); scores.put(e.getValue(), HostMetric.SLOW_HOST); if (log.isTraceEnabled()) { log.trace("Marking host:" + e.getValue() + " as slow"); } } } // Remove the hosts that needs to be remove for (Long key : keysToDelete) { hostsDeltaTime.removeAll(key); } // If we have only one host to determine score for, then put 0.5 if (hostsDeltaTime.size() == 1) { // Only one host responded scores.put(hostsDeltaTime.values().iterator().next(), 0.5); return scores; } // The number of segments is 1 less than the number of hosts to score. So if we have 3 hosts to // occupy // the score range from 0 -> 1, then we have two segments and their scores should be 0, 0.5 and // 1. double segmentSize = 1.0 / (hostsDeltaTime.size() - 1); // Since the data is sorting ascending in turnaround time, the first we meet are the fastest // hosts // and so they should get the lowest slowness score double score = 0; for (Map.Entry<Long, String> e : hostsDeltaTime.entries()) { scores.put(e.getValue(), score); score += segmentSize; } return scores; }