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);
}
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);
}
Aggregations