diff --git a/src/main/java/org/opensearch/performanceanalyzer/rca/framework/metrics/WriterMetrics.java b/src/main/java/org/opensearch/performanceanalyzer/rca/framework/metrics/WriterMetrics.java index b730a27a7..b24b0ff58 100644 --- a/src/main/java/org/opensearch/performanceanalyzer/rca/framework/metrics/WriterMetrics.java +++ b/src/main/java/org/opensearch/performanceanalyzer/rca/framework/metrics/WriterMetrics.java @@ -155,6 +155,10 @@ public enum WriterMetrics implements MeasurementSet { STALE_METRICS("StaleMetrics", "count", Arrays.asList(Statistics.COUNT)), + /** This metric indicates that the writer file creation was skipped. */ + WRITER_FILE_CREATION_SKIPPED( + "WriterFileCreationSkipped", "count", Arrays.asList(Statistics.COUNT)), + METRICS_WRITE_ERROR( "MetricsWriteError", "namedCount", diff --git a/src/main/java/org/opensearch/performanceanalyzer/reader_writer_shared/EventLogFileHandler.java b/src/main/java/org/opensearch/performanceanalyzer/reader_writer_shared/EventLogFileHandler.java index 53494030e..8370d06e7 100644 --- a/src/main/java/org/opensearch/performanceanalyzer/reader_writer_shared/EventLogFileHandler.java +++ b/src/main/java/org/opensearch/performanceanalyzer/reader_writer_shared/EventLogFileHandler.java @@ -82,8 +82,11 @@ public void writeTmpFile(List dataEntries, long epoch) { * data. * *

If any of the above steps fail, then the tmp file is not deleted from the filesystem. This - * is fine as the {@link org.opensearch.performanceanalyzer.reader_writer_shared.EventLogFileHandler#deleteFiles}, - * will eventually clean it. The copies are atomic and therefore the reader never reads incompletely written file. + * is fine as the {@link + * org.opensearch.performanceanalyzer.reader_writer_shared.EventLogFileHandler#deleteFiles}, + * will eventually clean it. The copies are atomic and therefore the reader never reads + * incompletely written file. + * * @param dataEntries The metrics to be written to file. * @param epoch The epoch all the metrics belong to. */ @@ -116,11 +119,19 @@ public void renameFromTmp(long epoch) { public void renameFromTmpWithPrivilege(long epoch) { Path path = Paths.get(metricsLocation, String.valueOf(epoch)); Path tmpPath = Paths.get(path.toString() + TMP_FILE_EXT); - // This is done only when no exception is thrown. - try { - Files.move(tmpPath, path, REPLACE_EXISTING, ATOMIC_MOVE); - } catch (IOException e) { - LOG.error("Error moving file {} to {}.", tmpPath.toString(), path.toString(), e); + File tempFile = new File(tmpPath.toString()); + // Only if the tmp file is present, we want to create the writer file. + // If not, we will publish a metric. + if (tempFile.exists()) { + // This is done only when no exception is thrown. + try { + Files.move(tmpPath, path, REPLACE_EXISTING, ATOMIC_MOVE); + } catch (IOException e) { + LOG.error("Error moving file {} to {}.", tmpPath.toString(), path.toString(), e); + } + } else { + PerformanceAnalyzerApp.WRITER_METRICS_AGGREGATOR.updateStat( + WriterMetrics.WRITER_FILE_CREATION_SKIPPED, "", 1); } } @@ -134,7 +145,7 @@ public void read(long timestamp, EventDispatcher processor) { File tempFile = new File(pathToFile.toString()); if (!tempFile.exists()) { long mCurrT = System.currentTimeMillis(); - LOG.info("Didnt find {} at {}", filename, mCurrT); + LOG.debug("Didnt find {} at {}", filename, mCurrT); return; } readInternal(pathToFile, BUFFER_SIZE, processor);