Skip to content

Commit

Permalink
HDDS-10046. Replace PrecomputedVolumeSpace with SpaceUsageSource.Fixed (
Browse files Browse the repository at this point in the history
  • Loading branch information
adoroszlai authored Jan 5, 2024
1 parent 741b113 commit 2066868
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,47 @@ public interface SpaceUsageSource {
long getCapacity();

long getAvailable();

default SpaceUsageSource snapshot() {
return new Fixed(getCapacity(), getAvailable(), getUsedSpace());
}

SpaceUsageSource UNKNOWN = new Fixed(0, 0, 0);

/**
* A static source of space usage. Can be a point in time snapshot of a
* real volume usage, or can be used for testing.
*/
final class Fixed implements SpaceUsageSource {

private final long capacity;
private final long available;
private final long used;

Fixed(long capacity, long available, long used) {
this.capacity = capacity;
this.available = available;
this.used = used;
}

@Override
public long getCapacity() {
return capacity;
}

@Override
public long getAvailable() {
return available;
}

@Override
public long getUsedSpace() {
return used;
}

@Override
public SpaceUsageSource snapshot() {
return this; // immutable
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,7 @@ public static SpaceUsageSource fixed(long capacity, long available) {

public static SpaceUsageSource fixed(long capacity, long available,
long used) {
return new Fixed(capacity, available, used);
}

private static final class Fixed implements SpaceUsageSource {

private final long capacity;
private final long available;
private final long used;

Fixed(long capacity, long available, long used) {
this.capacity = capacity;
this.available = available;
this.used = used;
}

@Override
public long getCapacity() {
return capacity;
}

@Override
public long getAvailable() {
return available;
}

@Override
public long getUsedSpace() {
return used;
}
return new SpaceUsageSource.Fixed(capacity, available, used);
}

private MockSpaceUsageSource() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.hadoop.hdds.HddsUtils;
import org.apache.hadoop.hdds.client.BlockID;
import org.apache.hadoop.hdds.conf.ConfigurationSource;
import org.apache.hadoop.hdds.fs.SpaceUsageSource;
import org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos;
import org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.ContainerCommandRequestProto;
import org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.ContainerCommandResponseProto;
Expand Down Expand Up @@ -78,7 +79,6 @@
import static org.apache.hadoop.hdds.scm.protocolPB.ContainerCommandResponseBuilders.malformedRequest;
import static org.apache.hadoop.hdds.scm.protocolPB.ContainerCommandResponseBuilders.unsupportedRequest;
import static org.apache.hadoop.ozone.container.common.interfaces.Container.ScanResult;
import static org.apache.hadoop.ozone.container.common.volume.VolumeUsage.PrecomputedVolumeSpace;

/**
* Ozone Container dispatcher takes a call from the netty server and routes it
Expand Down Expand Up @@ -585,8 +585,8 @@ private boolean isVolumeFull(Container container) {
.orElse(Boolean.FALSE);
if (isOpen) {
HddsVolume volume = container.getContainerData().getVolume();
PrecomputedVolumeSpace precomputedVolumeSpace =
volume.getPrecomputedVolumeSpace();
SpaceUsageSource precomputedVolumeSpace =
volume.getCurrentUsage();
long volumeCapacity = precomputedVolumeSpace.getCapacity();
long volumeFreeSpaceToSpare =
VolumeUsage.getMinVolumeFreeSpace(conf, volumeCapacity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.hadoop.fs.StorageType;
import org.apache.hadoop.hdds.conf.ConfigurationSource;
import org.apache.hadoop.hdds.fs.SpaceUsageCheckFactory;
import org.apache.hadoop.hdds.fs.SpaceUsageSource;
import org.apache.hadoop.hdfs.server.datanode.StorageLocation;
import org.apache.hadoop.hdfs.server.datanode.checker.Checkable;
import org.apache.hadoop.hdfs.server.datanode.checker.VolumeCheckResult;
Expand Down Expand Up @@ -50,7 +51,6 @@
import java.util.stream.Stream;

import static org.apache.hadoop.ozone.container.common.HDDSVolumeLayoutVersion.getLatestVersion;
import static org.apache.hadoop.ozone.container.common.volume.VolumeUsage.PrecomputedVolumeSpace;


/**
Expand Down Expand Up @@ -456,14 +456,14 @@ public long getAvailable() {

}

public long getAvailable(PrecomputedVolumeSpace precomputedVolumeSpace) {
public long getAvailable(SpaceUsageSource precomputedVolumeSpace) {
return volumeInfo.map(info -> info.getAvailable(precomputedVolumeSpace))
.orElse(0L);
}

public PrecomputedVolumeSpace getPrecomputedVolumeSpace() {
return volumeInfo.map(VolumeInfo::getPrecomputedVolumeSpace)
.orElse(new PrecomputedVolumeSpace(0L, 0L));
public SpaceUsageSource getCurrentUsage() {
return volumeInfo.map(VolumeInfo::getCurrentUsage)
.orElse(SpaceUsageSource.UNKNOWN);
}

public long getUsedSpace() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
import org.apache.hadoop.hdds.fs.SpaceUsageCheckParams;

import com.google.common.annotations.VisibleForTesting;
import org.apache.hadoop.hdds.fs.SpaceUsageSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.apache.hadoop.ozone.container.common.volume.VolumeUsage.PrecomputedVolumeSpace;
import static org.apache.hadoop.hdds.scm.ScmConfigKeys.HDDS_DATANODE_DIR_DU_RESERVED;
import static org.apache.hadoop.hdds.scm.ScmConfigKeys.HDDS_DATANODE_DIR_DU_RESERVED_PERCENT;
import static org.apache.hadoop.hdds.scm.ScmConfigKeys.HDDS_DATANODE_DIR_DU_RESERVED_PERCENT_DEFAULT;
Expand Down Expand Up @@ -240,13 +240,13 @@ public long getAvailable() {
return Math.max(Math.min(avail, usage.getAvailable()), 0);
}

public long getAvailable(PrecomputedVolumeSpace precomputedValues) {
public long getAvailable(SpaceUsageSource precomputedValues) {
long avail = precomputedValues.getCapacity() - usage.getUsedSpace();
return Math.max(Math.min(avail, usage.getAvailable(precomputedValues)), 0);
}

public PrecomputedVolumeSpace getPrecomputedVolumeSpace() {
return usage.getPrecomputedVolumeSpace();
public SpaceUsageSource getCurrentUsage() {
return usage.snapshot();
}

public void incrementUsedSpace(long usedSpace) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public long getAvailable() {
return source.getAvailable() - getRemainingReserved();
}

public long getAvailable(PrecomputedVolumeSpace precomputedVolumeSpace) {
public long getAvailable(SpaceUsageSource precomputedVolumeSpace) {
long available = precomputedVolumeSpace.getAvailable();
return available - getRemainingReserved(precomputedVolumeSpace);
}
Expand All @@ -75,6 +75,11 @@ public long getUsedSpace() {
return source.getUsedSpace();
}

@Override
public SpaceUsageSource snapshot() {
return source.snapshot();
}

public void incrementUsedSpace(long usedSpace) {
source.incrementUsedSpace(usedSpace);
}
Expand All @@ -94,7 +99,7 @@ private long getOtherUsed() {
return Math.max(totalUsed - source.getUsedSpace(), 0L);
}

private long getOtherUsed(PrecomputedVolumeSpace precomputedVolumeSpace) {
private long getOtherUsed(SpaceUsageSource precomputedVolumeSpace) {
long totalUsed = precomputedVolumeSpace.getCapacity() -
precomputedVolumeSpace.getAvailable();
return Math.max(totalUsed - source.getUsedSpace(), 0L);
Expand All @@ -105,7 +110,7 @@ private long getRemainingReserved() {
}

private long getRemainingReserved(
PrecomputedVolumeSpace precomputedVolumeSpace) {
SpaceUsageSource precomputedVolumeSpace) {
return Math.max(reservedInBytes - getOtherUsed(precomputedVolumeSpace), 0L);
}

Expand Down Expand Up @@ -169,33 +174,4 @@ public static boolean hasVolumeEnoughSpace(long volumeAvailableSpace,
return (volumeAvailableSpace - volumeCommittedBytesCount) >
Math.max(requiredSpace, volumeFreeSpaceToSpare);
}

/**
* Class representing precomputed space values of a volume.
* This class is intended to store precomputed values, such as capacity
* and available space of a volume, to avoid recalculating these
* values multiple times and to make method signatures simpler.
*/
public static class PrecomputedVolumeSpace {
private final long capacity;
private final long available;

public PrecomputedVolumeSpace(long capacity, long available) {
this.capacity = capacity;
this.available = available;
}

public long getCapacity() {
return capacity;
}

public long getAvailable() {
return available;
}
}

public PrecomputedVolumeSpace getPrecomputedVolumeSpace() {
return new PrecomputedVolumeSpace(source.getCapacity(),
source.getAvailable());
}
}

0 comments on commit 2066868

Please sign in to comment.