use of io.datarouter.util.tuple.Pair in project datarouter by hotpads.
the class LatencyMonitoringService method getClientChecks.
public List<LatencyCheck> getClientChecks() {
List<LatencyCheck> checks = new ArrayList<>();
if (MAKE_GET_CHECK) {
for (ClientId clientId : clientInitializationTracker.getInitializedClients()) {
Collection<PhysicalNode<?, ?, ?>> nodesForClient = nodes.getPhysicalNodesForClient(clientId.getName());
Optional<PhysicalNode<?, ?, ?>> findFirst = nodesForClient.stream().findFirst();
if (findFirst.isPresent()) {
PhysicalNode<?, ?, ?> node = findFirst.get();
if (node instanceof PhysicalMapStorageNode) {
PhysicalMapStorageNode<?, ?, ?> ms = (PhysicalMapStorageNode<?, ?, ?>) node;
checks.add(new DatarouterClientLatencyCheck(LatencyMonitoringService.DR_CLIENT_PREFIX + clientId + LatencyMonitoringService.MS_CHECK_SUFIX, makeGet(ms), clientId));
}
}
}
}
Function<ClientId, Stream<Pair<ClientId, SortedStorage<?, ?>>>> mapClientIdToFirstSortedStorageNode = clientId -> nodes.getPhysicalNodesForClient(clientId.getName()).stream().filter(node -> node instanceof SortedStorage).limit(1).map(SortedStorage.class::cast).peek(sortedStorage -> logger.info("selected SortedStorage {}", sortedStorage)).map(sortedStorage -> new Pair<>(clientId, sortedStorage));
checks.addAll(clientInitializationTracker.getInitializedClients().stream().filter(clientId -> clients.getClientManager(clientId).monitorLatency()).flatMap(mapClientIdToFirstSortedStorageNode).map(pair -> new DatarouterClientLatencyCheck(getCheckNameForDatarouterClient(pair.getLeft()), () -> pair.getRight().scanKeys(ONLY_FIRST).findFirst(), pair.getLeft())).collect(Collectors.toList()));
return checks;
}
use of io.datarouter.util.tuple.Pair in project datarouter by hotpads.
the class BaseCacheRateLimiter method internalAllow.
@Override
protected Pair<Boolean, Instant> internalAllow(String key, boolean increment) {
Instant now = Instant.now();
Map<String, Long> results = readCounts(buildKeysToRead(key, now));
String currentMapKey = makeMapKey(key, getTimeStr(now));
int total = 0;
for (Entry<String, Long> entry : results.entrySet()) {
Long numRequests = entry.getValue() == null ? 0L : entry.getValue();
if (entry.getKey().equals(currentMapKey)) {
numRequests++;
}
// exceeded maxSpikeRequests
if (numRequests > config.maxSpikeRequests) {
Instant exceededInstant = getDateFromKey(entry.getKey());
Counters.inc(HIT_COUNTER_NAME);
return new Pair<>(false, exceededInstant.plusMillis(config.bucketIntervalMs * (config.numIntervals - 1)));
}
total += numRequests;
}
double avgRequests = total / (double) config.numIntervals;
// exceeded maxAvgRequests
if (avgRequests > config.maxAverageRequests) {
List<Instant> instants = Scanner.of(results.keySet()).map(this::getDateFromKey).list();
Instant lastTime = Instant.MIN;
for (Instant instant : instants) {
if (instant.isAfter(lastTime)) {
lastTime = instant;
}
}
Objects.requireNonNull(lastTime);
// add to get next available time
Counters.inc(HIT_COUNTER_NAME);
return new Pair<>(false, lastTime.plusMillis(config.bucketIntervalMs));
}
if (increment) {
increment(currentMapKey);
}
return new Pair<>(true, null);
}
use of io.datarouter.util.tuple.Pair in project datarouter by hotpads.
the class AwsMemcachedWebInspector method getDetails.
@Override
protected Pair<Integer, DivTag> getDetails(ClientId clientId) {
MemcachedClientMode mode = options.getClientMode(clientId.getName());
Pair<Integer, DivTag> nodeCountByNodeTag = new Pair<>();
if (mode == MemcachedClientMode.DYNAMIC) {
List<AwsMemcachedNodeEndpointDto> nodeEndpointDtos = Scanner.of(getSpyClient(clientId).getAllNodeEndPoints()).map(nodeEndPoint -> new AwsMemcachedNodeEndpointDto(nodeEndPoint.getHostName(), nodeEndPoint.getIpAddress(), nodeEndPoint.getPort())).list();
var table = new J2HtmlTable<AwsMemcachedNodeEndpointDto>().withClasses("sortable table table-sm table-striped my-4 border").withColumn("HostName", dto -> dto.hostName).withColumn("IpAddress", dto -> dto.ipAddress).withColumn("Port", dto -> dto.port).build(nodeEndpointDtos);
DivTag divTable = div(table).withClass("container-fluid my-4").withStyle("padding-left: 0px");
nodeCountByNodeTag.setLeft(nodeEndpointDtos.size());
nodeCountByNodeTag.setRight(divTable);
} else {
List<LiTag> socketAddresses = Scanner.of(getSpyClient(clientId).getAvailableServers()).map(Object::toString).map(TagCreator::li).list();
DivTag div = div(ul(socketAddresses.toArray(new ContainerTag[0])));
nodeCountByNodeTag.setLeft(socketAddresses.size());
nodeCountByNodeTag.setRight(div);
}
return nodeCountByNodeTag;
}
use of io.datarouter.util.tuple.Pair in project datarouter by hotpads.
the class BaseDatarouterServletContextListener method processListeners.
private void processListeners(OnAction onAction, boolean executeAllListenersSynchronously) {
ThreadFactory factory = new NamedThreadFactory("datarouterListenerExecutor", false);
ExecutorService executor = Executors.newFixedThreadPool(allListeners.size(), factory);
var timer = new PhaseTimer();
long shutdownStartMillis = System.currentTimeMillis();
for (Pair<ExecutionMode, List<DatarouterAppListener>> listenersByShutdownMode : listenersByExecutionMods) {
List<DatarouterAppListener> listeners = listenersByShutdownMode.getRight();
ExecutionMode executionMode = executeAllListenersSynchronously ? ExecutionMode.SYNCHRONOUS : listenersByShutdownMode.getLeft();
logger.warn("{} {}: [{}", onAction.display, executionMode.display, listeners.stream().map(listener -> listener.getClass().getSimpleName()).collect(Collectors.joining(", ")) + "]");
if (executionMode == ExecutionMode.SYNCHRONOUS) {
Scanner.of(listeners).map(executeOnAction(onAction)).forEach(timer::add);
} else if (executionMode == ExecutionMode.PARALLEL) {
long shutdownParallelStartMillis = System.currentTimeMillis();
Scanner.of(listeners).parallel(new ParallelScannerContext(executor, listeners.size(), true)).map(executeOnAction(onAction)).forEach(timer::add);
logger.info("Parallel {} total={}", onAction.display, System.currentTimeMillis() - shutdownParallelStartMillis);
}
}
logger.warn(String.format("%s [total=%d][%s]", onAction, System.currentTimeMillis() - shutdownStartMillis, timer.getPhaseNamesAndTimes().stream().map(pair -> pair.getLeft() + "=" + pair.getRight()).collect(Collectors.joining("]["))));
ExecutorServiceTool.shutdown(executor, Duration.ofSeconds(2));
}
use of io.datarouter.util.tuple.Pair in project datarouter by hotpads.
the class BaseDatarouterServletContextListener method buildExecuteOnActionsLists.
private void buildExecuteOnActionsLists(ServletContextEvent event) {
DatarouterInjector injector = getInjector(event.getServletContext());
Scanner.of(listenerClasses).map(injector::getInstance).forEach(allListeners::add);
Scanner.of(webListenerClasses).map(injector::getInstance).each(listener -> listener.setServletContext(event.getServletContext())).forEach(allListeners::add);
Scanner.of(allListeners).splitBy(DatarouterAppListener::safeToExecuteInParallel).map(Scanner::list).map(listeners -> new Pair<>(listeners.get(0).safeToExecuteInParallel() ? ExecutionMode.PARALLEL : ExecutionMode.SYNCHRONOUS, listeners)).forEach(listenersByExecutionMods::add);
}
Aggregations