Search in sources :

Example 1 with Observation

use of org.spf4j.tsdb2.avro.Observation in project spf4j by zolyfarkas.

the class AvroMeasurementStoreReader method getObservations.

@Override
public AvroCloseableIterable<Observation> getObservations() throws IOException {
    Schema oSchema = Observation.getClassSchema();
    if (dataFiles.length == 0) {
        return AvroCloseableIterable.from(Collections.emptyList(), () -> {
        }, oSchema);
    }
    SpecificDatumReader<Observation> specificDatumReader = new SpecificDatumReader<>(Observation.class);
    Iterable<Observation>[] streams = new Iterable[dataFiles.length];
    Closeable[] closeables = new Closeable[dataFiles.length];
    for (int i = 0; i < dataFiles.length; i++) {
        Path dataFile = dataFiles[i];
        DataFileStream<Observation> ds;
        try {
            ds = new DataFileStream<Observation>(Files.newInputStream(dataFile), specificDatumReader);
        } catch (IOException ex) {
            IOException ex2 = Closeables.closeAll(closeables, 0, i);
            if (ex2 != null) {
                ex2.addSuppressed(ex);
                throw ex2;
            }
            throw ex;
        }
        long fileTimeRef = ds.getMetaLong("timeRef");
        streams[i] = Iterables.transform(ds, new TimeCalibrate(fileTimeRef));
        closeables[i] = ds;
    }
    Iterable<Observation> stream = Iterables.concat(streams);
    return AvroCloseableIterable.from(stream, () -> {
        IOException ex = Closeables.closeAll(closeables);
        if (ex != null) {
            throw new UncheckedIOException(ex);
        }
    }, oSchema);
}
Also used : Path(java.nio.file.Path) AvroCloseableIterable(org.spf4j.base.avro.AvroCloseableIterable) Schema(org.apache.avro.Schema) Closeable(java.io.Closeable) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Observation(org.spf4j.tsdb2.avro.Observation) SpecificDatumReader(org.apache.avro.specific.SpecificDatumReader)

Example 2 with Observation

use of org.spf4j.tsdb2.avro.Observation in project spf4j by zolyfarkas.

the class Charts2 method readToTs.

// false positive.
@SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE")
public static TimeSeries readToTs(final MeasurementStoreQuery query, final Schema table, final long startTime, final long endTime, final int aggTimeMillis) throws IOException {
    TLongList ts = new TLongArrayList();
    List<long[]> values = new ArrayList<>();
    try (AvroCloseableIterable<Observation> data = aggTimeMillis <= 0 ? query.getObservations(table, Instant.ofEpochMilli(startTime), Instant.ofEpochMilli(endTime)) : query.getAggregatedObservations(table, Instant.ofEpochMilli(startTime), Instant.ofEpochMilli(endTime), aggTimeMillis, TimeUnit.MILLISECONDS)) {
        for (Observation rec : data) {
            ts.add(rec.getRelTimeStamp());
            values.add(Longs.toArray(rec.getData()));
        }
    }
    return new TimeSeries(ts.toArray(), values.toArray(new long[values.size()][]));
}
Also used : TLongArrayList(gnu.trove.list.array.TLongArrayList) TLongList(gnu.trove.list.TLongList) ArrayList(java.util.ArrayList) TLongArrayList(gnu.trove.list.array.TLongArrayList) Observation(org.spf4j.tsdb2.avro.Observation) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 3 with Observation

use of org.spf4j.tsdb2.avro.Observation in project spf4j by zolyfarkas.

the class RecorderFactoryTest method assertData.

@SuppressFBWarnings("CLI_CONSTANT_LIST_INDEX")
public static void assertData(final String forWhat, final long expectedValue) throws IOException {
    MeasurementStore store = ProcessMeasurementStore.getMeasurementStore();
    store.flush();
    MeasurementStoreQuery query = store.query();
    Collection<Schema> schemas = query.getMeasurements((x) -> forWhat.equals(x));
    Schema schema = schemas.iterator().next();
    try (AvroCloseableIterable<Observation> observations = query.getObservations(schema, Instant.EPOCH, Instant.ofEpochMilli(Long.MAX_VALUE))) {
        long sum = 0;
        for (Observation o : observations) {
            sum += o.getData().get(0);
        }
        Assert.assertEquals(expectedValue, sum);
    }
}
Also used : Schema(org.apache.avro.Schema) Observation(org.spf4j.tsdb2.avro.Observation) ProcessMeasurementStore(org.spf4j.perf.impl.ProcessMeasurementStore) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 4 with Observation

use of org.spf4j.tsdb2.avro.Observation in project spf4j by zolyfarkas.

the class TimeSeriesRecord method accumulateObservations.

@Beta
static void accumulateObservations(final Schema recSchema, final Observation r1, final Observation r2) {
    r1.setRelTimeStamp(r2.getRelTimeStamp());
    r1.setTableDefId(-1L);
    Iterator<Schema.Field> it = recSchema.getFields().iterator();
    it.next();
    List<Long> r1d = r1.getData();
    List<Long> r2d = r2.getData();
    while (it.hasNext()) {
        Schema.Field nf = it.next();
        int pos = nf.pos();
        Aggregation agg;
        String prop = nf.schema().getProp(AGGREGATION_TYPE_PROP);
        if (prop != null) {
            agg = Aggregation.valueOf(prop);
        } else {
            agg = inferAggregationFromName(nf, recSchema);
        }
        int apos = pos - 1;
        switch(agg) {
            case SUM:
                r1d.set(apos, r1d.get(apos) + r2d.get(apos));
                break;
            case MIN:
                r1d.set(apos, Math.min(r1d.get(apos), r2d.get(apos)));
                break;
            case MAX:
                r1d.set(apos, Math.max(r1d.get(apos), r2d.get(apos)));
                break;
            case FIRST:
                break;
            case LAST:
            case UNKNOWN:
                r1d.set(apos, r2d.get(apos));
                break;
            default:
                throw new UnsupportedOperationException("Unsupported aggregation: " + agg);
        }
    }
}
Also used : Aggregation(org.spf4j.tsdb2.avro.Aggregation) Schema(org.apache.avro.Schema) Beta(com.google.common.annotations.Beta)

Example 5 with Observation

use of org.spf4j.tsdb2.avro.Observation in project spf4j by zolyfarkas.

the class FileMonitorAspectTest method testFileIOMonitoring.

/**
 * Test of nioReadLong method, of class FileMonitorAspect.
 */
@Test
public void testFileIOMonitoring() throws Exception {
    System.setProperty("spf4j.perf.file.sampleTimeMillis", "1000");
    File tempFile = File.createTempFile("test", ".tmp");
    tempFile.deleteOnExit();
    try (Writer fw = new OutputStreamWriter(Files.newOutputStream(tempFile.toPath()), StandardCharsets.UTF_8)) {
        for (int i = 0; i < 10; i++) {
            fw.write("bla bla test\n");
            Thread.sleep(500);
        }
    }
    Thread.sleep(1000);
    try (BufferedReader fr = new BufferedReader(new InputStreamReader(Files.newInputStream(tempFile.toPath()), StandardCharsets.UTF_8))) {
        String line = fr.readLine();
        while (line != null) {
            LOG.debug("Read: {}", line);
            line = fr.readLine();
            Thread.sleep(500);
        }
    }
    RecorderFactory.MEASUREMENT_STORE.flush();
    MeasurementStoreQuery query = RecorderFactory.MEASUREMENT_STORE.query();
    Collection<Schema> measurements = query.getMeasurements((x) -> true);
    LOG.debug("Tables {}", measurements);
    THashSet<String> collect = measurements.stream().map((x) -> x.getProp(TimeSeriesRecord.RAW_NAME)).collect(Collectors.toCollection(() -> new THashSet<String>(measurements.size())));
    Assert.assertThat(collect, (Matcher) Matchers.hasItem("file-write,org.spf4j.perf.aspects.FileMonitorAspectTest"));
    Assert.assertThat(collect, (Matcher) Matchers.hasItem("file-read,org.spf4j.perf.aspects.FileMonitorAspectTest"));
    Collection<Schema> fmw = query.getMeasurements((x) -> "file_write_org_spf4j_perf_aspects_FileMonitorAspectTest".equals(x));
    try (AvroCloseableIterable<Observation> obs = query.getObservations(fmw.iterator().next(), Instant.EPOCH, Instant.now())) {
        Assert.assertTrue(obs.iterator().hasNext());
    }
    Assert.assertTrue(OperatingSystem.getOpenFileDescriptorCount() > 0);
}
Also used : AvroCloseableIterable(org.spf4j.base.avro.AvroCloseableIterable) LoggerFactory(org.slf4j.LoggerFactory) RecorderFactory(org.spf4j.perf.impl.RecorderFactory) OutputStreamWriter(java.io.OutputStreamWriter) Observation(org.spf4j.tsdb2.avro.Observation) Schema(org.apache.avro.Schema) Logger(org.slf4j.Logger) MeasurementStoreQuery(org.spf4j.perf.MeasurementStoreQuery) Files(java.nio.file.Files) Collection(java.util.Collection) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) Instant(java.time.Instant) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) OperatingSystem(org.spf4j.os.OperatingSystem) THashSet(gnu.trove.set.hash.THashSet) TimeSeriesRecord(org.spf4j.perf.TimeSeriesRecord) Matcher(org.hamcrest.Matcher) Writer(java.io.Writer) BufferedReader(java.io.BufferedReader) Assert(org.junit.Assert) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) InputStreamReader(java.io.InputStreamReader) Schema(org.apache.avro.Schema) THashSet(gnu.trove.set.hash.THashSet) MeasurementStoreQuery(org.spf4j.perf.MeasurementStoreQuery) BufferedReader(java.io.BufferedReader) Observation(org.spf4j.tsdb2.avro.Observation) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) Test(org.junit.Test)

Aggregations

Schema (org.apache.avro.Schema)5 Observation (org.spf4j.tsdb2.avro.Observation)5 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)3 Test (org.junit.Test)2 AvroCloseableIterable (org.spf4j.base.avro.AvroCloseableIterable)2 ProcessMeasurementStore (org.spf4j.perf.impl.ProcessMeasurementStore)2 Beta (com.google.common.annotations.Beta)1 TLongList (gnu.trove.list.TLongList)1 TLongArrayList (gnu.trove.list.array.TLongArrayList)1 THashSet (gnu.trove.set.hash.THashSet)1 BufferedReader (java.io.BufferedReader)1 Closeable (java.io.Closeable)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 UncheckedIOException (java.io.UncheckedIOException)1 Writer (java.io.Writer)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Files (java.nio.file.Files)1