-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add read/write bytes metrics #803
Merged
ykmr1224
merged 3 commits into
opensearch-project:main
from
ykmr1224:metrics/read_write_bytes
Oct 25, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
flint-core/src/main/java/org/opensearch/flint/core/metrics/HistoricGauge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.flint.core.metrics; | ||
|
||
import com.codahale.metrics.Gauge; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Value; | ||
|
||
/** | ||
* Gauge which stores historic data points with timestamps. | ||
* This is used for emitting separate data points per request, instead of single aggregated metrics. | ||
*/ | ||
public class HistoricGauge implements Gauge<Long> { | ||
@AllArgsConstructor | ||
@Value | ||
public static class DataPoint { | ||
Long value; | ||
long timestamp; | ||
} | ||
|
||
private final List<DataPoint> dataPoints = Collections.synchronizedList(new LinkedList<>()); | ||
|
||
/** | ||
* This method will just return first value. | ||
* @return first value | ||
*/ | ||
@Override | ||
public Long getValue() { | ||
if (!dataPoints.isEmpty()) { | ||
return dataPoints.get(0).value; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Add new data point. Current time stamp will be attached to the data point. | ||
* @param value metric value | ||
*/ | ||
public void addDataPoint(Long value) { | ||
dataPoints.add(new DataPoint(value, System.currentTimeMillis())); | ||
} | ||
|
||
/** | ||
* Return copy of dataPoints and remove them from internal list | ||
* @return copy of the data points | ||
*/ | ||
public List<DataPoint> pollDataPoints() { | ||
int size = dataPoints.size(); | ||
List<DataPoint> result = new ArrayList<>(dataPoints.subList(0, size)); | ||
if (size > 0) { | ||
dataPoints.subList(0, size).clear(); | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...t-core/src/main/scala/org/opensearch/flint/core/metrics/ReadWriteBytesSparkListener.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.flint.core.metrics | ||
|
||
import org.apache.spark.internal.Logging | ||
import org.apache.spark.scheduler.{SparkListener, SparkListenerTaskEnd} | ||
import org.apache.spark.sql.SparkSession | ||
|
||
/** | ||
* Collect and emit bytesRead/Written and recordsRead/Written metrics | ||
*/ | ||
class ReadWriteBytesSparkListener extends SparkListener with Logging { | ||
var bytesRead: Long = 0 | ||
var recordsRead: Long = 0 | ||
var bytesWritten: Long = 0 | ||
var recordsWritten: Long = 0 | ||
|
||
override def onTaskEnd(taskEnd: SparkListenerTaskEnd): Unit = { | ||
val inputMetrics = taskEnd.taskMetrics.inputMetrics | ||
val outputMetrics = taskEnd.taskMetrics.outputMetrics | ||
val ids = s"(${taskEnd.taskInfo.taskId}, ${taskEnd.taskInfo.partitionId})" | ||
logInfo( | ||
s"${ids} Input: bytesRead=${inputMetrics.bytesRead}, recordsRead=${inputMetrics.recordsRead}") | ||
logInfo( | ||
s"${ids} Output: bytesWritten=${outputMetrics.bytesWritten}, recordsWritten=${outputMetrics.recordsWritten}") | ||
|
||
bytesRead += inputMetrics.bytesRead | ||
recordsRead += inputMetrics.recordsRead | ||
bytesWritten += outputMetrics.bytesWritten | ||
recordsWritten += outputMetrics.recordsWritten | ||
} | ||
|
||
def emitMetrics(): Unit = { | ||
logInfo(s"Input: totalBytesRead=${bytesRead}, totalRecordsRead=${recordsRead}") | ||
logInfo(s"Output: totalBytesWritten=${bytesWritten}, totalRecordsWritten=${recordsWritten}") | ||
MetricsUtil.addHistoricGauge(MetricConstants.INPUT_TOTAL_BYTES_READ, bytesRead) | ||
MetricsUtil.addHistoricGauge(MetricConstants.INPUT_TOTAL_RECORDS_READ, recordsRead) | ||
MetricsUtil.addHistoricGauge(MetricConstants.OUTPUT_TOTAL_BYTES_WRITTEN, bytesWritten) | ||
MetricsUtil.addHistoricGauge(MetricConstants.OUTPUT_TOTAL_RECORDS_WRITTEN, recordsWritten) | ||
} | ||
} | ||
|
||
object ReadWriteBytesSparkListener { | ||
def withMetrics[T](spark: SparkSession, lambda: () => T): T = { | ||
val listener = new ReadWriteBytesSparkListener() | ||
spark.sparkContext.addSparkListener(listener) | ||
|
||
val result = lambda() | ||
|
||
spark.sparkContext.removeSparkListener(listener) | ||
listener.emitMetrics() | ||
|
||
result | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
flint-core/src/test/java/org/opensearch/flint/core/metrics/HistoricGaugeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.flint.core.metrics; | ||
|
||
import org.junit.Test; | ||
import static org.junit.Assert.*; | ||
import org.opensearch.flint.core.metrics.HistoricGauge.DataPoint; | ||
|
||
import java.util.List; | ||
|
||
public class HistoricGaugeTest { | ||
|
||
@Test | ||
public void testGetValue_EmptyGauge_ShouldReturnNull() { | ||
HistoricGauge gauge= new HistoricGauge(); | ||
assertNull(gauge.getValue()); | ||
} | ||
|
||
@Test | ||
public void testGetValue_WithSingleDataPoint_ShouldReturnFirstValue() { | ||
HistoricGauge gauge= new HistoricGauge(); | ||
Long value = 100L; | ||
gauge.addDataPoint(value); | ||
|
||
assertEquals(value, gauge.getValue()); | ||
} | ||
|
||
@Test | ||
public void testGetValue_WithMultipleDataPoints_ShouldReturnFirstValue() { | ||
HistoricGauge gauge= new HistoricGauge(); | ||
Long firstValue = 100L; | ||
Long secondValue = 200L; | ||
gauge.addDataPoint(firstValue); | ||
gauge.addDataPoint(secondValue); | ||
|
||
assertEquals(firstValue, gauge.getValue()); | ||
} | ||
|
||
@Test | ||
public void testPollDataPoints_WithMultipleDataPoints_ShouldReturnAndClearDataPoints() { | ||
HistoricGauge gauge= new HistoricGauge(); | ||
gauge.addDataPoint(100L); | ||
gauge.addDataPoint(200L); | ||
gauge.addDataPoint(300L); | ||
|
||
List<DataPoint> dataPoints = gauge.pollDataPoints(); | ||
|
||
assertEquals(3, dataPoints.size()); | ||
assertEquals(Long.valueOf(100L), dataPoints.get(0).getValue()); | ||
assertEquals(Long.valueOf(200L), dataPoints.get(1).getValue()); | ||
assertEquals(Long.valueOf(300L), dataPoints.get(2).getValue()); | ||
|
||
assertTrue(gauge.pollDataPoints().isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testAddDataPoint_ShouldAddDataPointWithCorrectValueAndTimestamp() { | ||
HistoricGauge gauge= new HistoricGauge(); | ||
Long value = 100L; | ||
gauge.addDataPoint(value); | ||
|
||
List<DataPoint> dataPoints = gauge.pollDataPoints(); | ||
|
||
assertEquals(1, dataPoints.size()); | ||
assertEquals(value, dataPoints.get(0).getValue()); | ||
assertTrue(dataPoints.get(0).getTimestamp() > 0); | ||
} | ||
|
||
@Test | ||
public void testPollDataPoints_EmptyGauge_ShouldReturnEmptyList() { | ||
HistoricGauge gauge= new HistoricGauge(); | ||
List<DataPoint> dataPoints = gauge.pollDataPoints(); | ||
|
||
assertTrue(dataPoints.isEmpty()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might be a trivial question..
what could go wrong if use a regular list instead of synchronized list?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As metric emit is run in separate thread, there could be concurrency issue.