Skip to content
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

HDDS-12165. Refactor VolumeInfoMetrics to use getCurrentUsage #7784

Merged
merged 1 commit into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,18 @@

package org.apache.hadoop.ozone.container.common.volume;

import org.apache.hadoop.hdds.fs.SpaceUsageSource;
import org.apache.hadoop.metrics2.MetricsCollector;
import org.apache.hadoop.metrics2.MetricsInfo;
import org.apache.hadoop.metrics2.MetricsRecordBuilder;
import org.apache.hadoop.metrics2.MetricsSource;
import org.apache.hadoop.metrics2.MetricsSystem;
import org.apache.hadoop.metrics2.annotation.Metric;
import org.apache.hadoop.metrics2.annotation.Metrics;
import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
import org.apache.hadoop.metrics2.lib.MutableRate;
import org.apache.hadoop.metrics2.lib.Interns;
import org.apache.hadoop.metrics2.lib.MetricsRegistry;
import org.apache.hadoop.ozone.OzoneConsts;


Expand All @@ -31,20 +38,37 @@
*/
@Metrics(about = "Ozone Volume Information Metrics",
context = OzoneConsts.OZONE)
public class VolumeInfoMetrics {

private String metricsSourceName = VolumeInfoMetrics.class.getSimpleName();
public class VolumeInfoMetrics implements MetricsSource {

private static final String SOURCE_BASENAME =
VolumeInfoMetrics.class.getSimpleName();

private static final MetricsInfo CAPACITY =
Interns.info("Capacity", "Capacity");
private static final MetricsInfo AVAILABLE =
Interns.info("Available", "Available Space");
private static final MetricsInfo USED =
Interns.info("Used", "Used Space");
private static final MetricsInfo RESERVED =
Interns.info("Reserved", "Reserved Space");
private static final MetricsInfo TOTAL_CAPACITY =
Interns.info("TotalCapacity", "Total Capacity");

private final MetricsRegistry registry;
private final String metricsSourceName;
private final HddsVolume volume;
@Metric("Returns the RocksDB compact times of the Volume")
private MutableRate dbCompactLatency;
private long containers;

/**
* @param identifier Typically, path to volume root. E.g. /data/hdds
*/
public VolumeInfoMetrics(String identifier, HddsVolume ref) {
this.metricsSourceName += '-' + identifier;
this.volume = ref;
public VolumeInfoMetrics(String identifier, HddsVolume volume) {
this.volume = volume;

metricsSourceName = SOURCE_BASENAME + '-' + identifier;
registry = new MetricsRegistry(metricsSourceName);

init();
}

Expand Down Expand Up @@ -88,63 +112,6 @@ public String getVolumeType() {
return volume.getType().name();
}

public String getMetricsSourceName() {
return metricsSourceName;
}

/**
* Test conservative avail space.
* |----used----| (avail) |++++++++reserved++++++++|
* |<------- capacity ------->|
* |<------------------- Total capacity -------------->|
* A) avail = capacity - used
* B) capacity = used + avail
* C) Total capacity = used + avail + reserved
*/

/**
* Return the Storage type for the Volume.
*/
@Metric("Returns the Used space")
public long getUsed() {
return volume.getVolumeInfo().map(VolumeInfo::getScmUsed)
.orElse(0L);
}

/**
* Return the Total Available capacity of the Volume.
*/
@Metric("Returns the Available space")
public long getAvailable() {
return volume.getVolumeInfo().map(VolumeInfo::getAvailable)
.orElse(0L);
}

/**
* Return the Total Reserved of the Volume.
*/
@Metric("Fetches the Reserved Space")
public long getReserved() {
return volume.getVolumeInfo().map(VolumeInfo::getReservedInBytes)
.orElse(0L);
}

/**
* Return the Total capacity of the Volume.
*/
@Metric("Returns the Capacity of the Volume")
public long getCapacity() {
return getUsed() + getAvailable();
}

/**
* Return the Total capacity of the Volume.
*/
@Metric("Returns the Total Capacity of the Volume")
public long getTotalCapacity() {
return (getUsed() + getAvailable() + getReserved());
}

@Metric("Returns the Committed bytes of the Volume")
public long getCommitted() {
return volume.getCommittedBytes();
Expand All @@ -161,4 +128,20 @@ public void dbCompactTimesNanoSecondsIncr(long time) {
public long getContainers() {
return volume.getContainers();
}

@Override
public void getMetrics(MetricsCollector collector, boolean all) {
MetricsRecordBuilder builder = collector.addRecord(metricsSourceName);
registry.snapshot(builder, all);
volume.getVolumeInfo().ifPresent(volumeInfo -> {
SpaceUsageSource usage = volumeInfo.getCurrentUsage();
long reserved = volumeInfo.getReservedInBytes();
builder
.addGauge(CAPACITY, usage.getCapacity())
.addGauge(AVAILABLE, usage.getAvailable())
.addGauge(USED, usage.getUsedSpace())
.addGauge(RESERVED, reserved)
.addGauge(TOTAL_CAPACITY, usage.getCapacity() + reserved);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import org.apache.hadoop.hdds.fs.SpaceUsageSource;
import org.apache.hadoop.hdds.scm.ScmConfigKeys;
import org.apache.hadoop.hdfs.server.datanode.checker.VolumeCheckResult;
import org.apache.hadoop.metrics2.MetricsCollector;
import org.apache.hadoop.metrics2.impl.MetricsCollectorImpl;
import org.apache.hadoop.ozone.OzoneConfigKeys;

import static org.apache.hadoop.hdds.fs.MockSpaceUsagePersistence.inMemory;
Expand Down Expand Up @@ -504,13 +506,9 @@ public void testFailedVolumeSpace() throws IOException {
VolumeInfoMetrics volumeInfoMetrics = volume.getVolumeInfoStats();

try {
// In case of failed volume all stats should return 0.
assertEquals(0, volumeInfoMetrics.getUsed());
assertEquals(0, volumeInfoMetrics.getAvailable());
assertEquals(0, volumeInfoMetrics.getCapacity());
assertEquals(0, volumeInfoMetrics.getReserved());
assertEquals(0, volumeInfoMetrics.getTotalCapacity());
assertEquals(0, volumeInfoMetrics.getCommitted());
// In case of failed volume, metrics should not throw
MetricsCollector collector = new MetricsCollectorImpl();
volumeInfoMetrics.getMetrics(collector, true);
} finally {
// Shutdown the volume.
volume.shutdown();
Expand Down