Example usage for org.apache.commons.collections4.queue CircularFifoQueue add

List of usage examples for org.apache.commons.collections4.queue CircularFifoQueue add

Introduction

In this page you can find the example usage for org.apache.commons.collections4.queue CircularFifoQueue add.

Prototype

@Override
public boolean add(final E element) 

Source Link

Document

Adds the given element to this queue.

Usage

From source file:edu.iu.grnoc.flowspace_firewall.RateLimitTest.java

@Test
public void testRateLimitFast() {
    RateTracker tracker = new RateTracker(2000, 10000);

    CircularFifoQueue<Date> fifo = tracker.getFifo();
    Date now = new Date();
    for (int i = 0; i < 1000; i++) {
        fifo.add(now);
    }//ww  w  .jav a2  s  .  c om

    assertTrue("Tracker Rate is " + tracker.getRate(), tracker.getRate() == (1000 * 1000));

}

From source file:io.stallion.monitoring.HealthTracker.java

public void incrementQueue(CircularFifoQueue<MinuteInfo> queue) {
    ZonedDateTime now = MinuteInfo.getCurrentMinute();
    MinuteInfo minuteInfo = null;/*  w  ww  .j  av  a 2s.  c  o m*/
    if (!queue.isEmpty()) {
        minuteInfo = queue.get(queue.size() - 1);
        //minuteInfo = queue.get(0);
        //Log.info("first: {0}", queue.get(0).getMinute());
        //Log.info("last:  {0}", queue.get(queue.size() -1).getMinute());
        //Log.info("now:   {0}", now);
        if (!minuteInfo.getMinute().equals(now)) {
            //Log.info("Minutes do not matched, prepare for new minute");
            minuteInfo = null;
        }
    }
    if (minuteInfo == null) {
        minuteInfo = new MinuteInfo();
        minuteInfo.setMinute(now);
        queue.add(minuteInfo);
    }
    //Log.info("Increment minute {0} {1}", minuteInfo.getMinute().toString(), minuteInfo.getCount().get());
    minuteInfo.getCount().incrementAndGet();
}

From source file:eu.tango.energymodeller.datasourceclient.SlurmDataSourceAdaptor.java

/**
 * Parses a line from slurm and adds the data into the data source adaptor.
 *
 * @param line//from w  ww  .j  a  v a 2s  . com
 */
private void parse(String line) {
    try {
        boolean valid = true;
        GregorianCalendar calander = new GregorianCalendar();
        long clock = TimeUnit.MILLISECONDS.toSeconds(calander.getTimeInMillis());
        String[] values = line.split(";");
        String watts = getValue("CurrentWatts", values);
        String wattskwh = getValue("ConsumedJoules", values);
        String hostname = getValue("NodeName", values);
        String state = getValue("State", values);
        if (hostname.isEmpty()) {
            return;
        }
        String hostId = hostname.replaceAll("[^0-9]", "");
        CircularFifoQueue<SlurmDataSourceAdaptor.CPUUtilisation> lastCpuMeasurements = cpuMeasure.get(hostname);
        if (lastCpuMeasurements == null) {
            //Needs enough information to cover any recent queries of cpu utilisation, thus gather last 10mins of data
            lastCpuMeasurements = new CircularFifoQueue(
                    (int) TimeUnit.MINUTES.toSeconds(10) / poller.getPollRate());
            cpuMeasure.put(hostname, lastCpuMeasurements);
        }
        Host host = getHostByName(hostname);

        //Check for need to disover host
        if (host == null) {
            host = new Host(Integer.parseInt(hostId), hostname);
            hosts.put(hostname, host);
        }
        host.setAvailable(!state.isEmpty() && !state.equals("DOWN*"));
        /**
         * The further metrics from this host are not relevant and may cause
         * parse errors
         */
        if (!host.isAvailable()) {
            return;
        }

        //Note CPU Load = N/A when the node is down, but perhas might occur in some other case. The previous guard should prevent this error.
        String cpuLoad = getValue("CPULoad", values);
        if (!cpuLoad.equals("N/A") && cpuLoad.matches("-?\\d+(\\.\\d+)?")) {
            lastCpuMeasurements.add(new SlurmDataSourceAdaptor.CPUUtilisation(clock, hostname,
                    (Double.valueOf(cpuLoad)) * 100));
        }
        HostMeasurement measurement = new HostMeasurement(host, clock);
        measurement.addMetric(new MetricValue(KpiList.POWER_KPI_NAME, KpiList.POWER_KPI_NAME, watts, clock));
        measurement
                .addMetric(new MetricValue(KpiList.ENERGY_KPI_NAME, KpiList.ENERGY_KPI_NAME, wattskwh, clock));
        readGresString(values, measurement, clock);
        readGresUsedString(values, measurement, clock);
        readGenericMetrics(values, measurement, clock);
        double cpuUtil = Double.valueOf(cpuLoad) / Double.valueOf(getValue("CPUTot", values));
        valid = valid && validatedAddMetric(measurement, new MetricValue(KpiList.CPU_SPOT_USAGE_KPI_NAME,
                KpiList.CPU_SPOT_USAGE_KPI_NAME, cpuUtil * 100 + "", clock));
        valid = valid && validatedAddMetric(measurement, new MetricValue(KpiList.CPU_IDLE_KPI_NAME,
                KpiList.CPU_IDLE_KPI_NAME, ((1 - cpuUtil)) * 100 + "", clock));
        if (!valid) {
            System.out.println("The measurement taken was invalid");
            return;
        }

        valid = valid && validatedAddMetric(measurement,
                new MetricValue(KpiList.MEMORY_AVAILABLE_KPI_NAME, KpiList.MEMORY_AVAILABLE_KPI_NAME,
                        (int) (Double.valueOf(getValue("FreeMem", values)) / 1048576) + "", clock));
        valid = valid && validatedAddMetric(measurement,
                new MetricValue(KpiList.MEMORY_TOTAL_KPI_NAME, KpiList.MEMORY_TOTAL_KPI_NAME,
                        (int) (Double.valueOf(getValue("RealMemory", values)) / 1048576) + "", clock));

        if (!valid) {
            return;
        }
        current.put(hostname, measurement);
        if (lowest.get(hostname) == null || measurement.getPower() < lowest.get(hostname).getPower()) {
            lowest.put(hostname, measurement);
        }
        if (highest.get(hostname) == null || measurement.getPower() > highest.get(hostname).getPower()) {
            highest.put(hostname, measurement);
        }
    } catch (NumberFormatException ex) {
        //Ignore these errors and carry on. It may just be the header line.
        Logger.getLogger(SlurmDataSourceAdaptor.class.getName()).log(Level.SEVERE, "Unexpected number format",
                ex);
    }
}

From source file:org.apache.nifi.cluster.coordination.node.NodeClusterCoordinator.java

private void addNodeEvent(final NodeIdentifier nodeId, final Severity severity, final String message) {
    final NodeEvent event = new Event(nodeId.toString(), message, severity);
    final CircularFifoQueue<NodeEvent> eventQueue = nodeEvents.computeIfAbsent(nodeId,
            id -> new CircularFifoQueue<>());
    synchronized (eventQueue) {
        eventQueue.add(event);
    }/*from   w  w w .  ja v  a  2s.  c  om*/
}