Search in sources :

Example 1 with AvroCloseableIterable

use of org.spf4j.base.avro.AvroCloseableIterable 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 AvroCloseableIterable

use of org.spf4j.base.avro.AvroCloseableIterable 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)2 AvroCloseableIterable (org.spf4j.base.avro.AvroCloseableIterable)2 Observation (org.spf4j.tsdb2.avro.Observation)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)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 Path (java.nio.file.Path)1 Instant (java.time.Instant)1 Collection (java.util.Collection)1 Collectors (java.util.stream.Collectors)1 SpecificDatumReader (org.apache.avro.specific.SpecificDatumReader)1