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

Fix cacheMissPercentage metric #18208

Merged
merged 9 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -102,9 +102,7 @@ public ByteBuffer read(long offset, long length) throws IOException {

@Override
public int transferTo(ByteBuf buf) throws IOException {
int bytesReadFromCache = buf.writeBytes(mLocalFileChannel, buf.writableBytes());
MetricsSystem.counter(MetricKey.WORKER_BYTES_READ_CACHE.getName()).inc(bytesReadFromCache);
return bytesReadFromCache;
return buf.writeBytes(mLocalFileChannel, buf.writableBytes());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import alluxio.worker.block.annotator.BlockOrder;
import alluxio.worker.block.io.BlockReader;
import alluxio.worker.block.io.BlockWriter;
import alluxio.worker.block.io.DeStoreBlockReader;
import alluxio.worker.block.io.DelegatingBlockReader;
import alluxio.worker.block.io.StoreBlockReader;
import alluxio.worker.block.io.StoreBlockWriter;
Expand Down Expand Up @@ -222,9 +223,10 @@ public BlockReader createBlockReader(long sessionId, long blockId, long offset)

try {
BlockReader reader = new StoreBlockReader(sessionId, block);
BlockReader dereader = new DeStoreBlockReader(reader);
((FileChannel) reader.getChannel()).position(offset);
accessBlock(sessionId, blockId);
return new DelegatingBlockReader(reader, blockLock);
return new DelegatingBlockReader(dereader, blockLock);
} catch (Exception e) {
blockLock.close();
throw new IOException(format("Failed to get local block reader, sessionId=%d, "
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package alluxio.worker.block.io;

import alluxio.metrics.MetricKey;
import alluxio.metrics.MetricsSystem;

import io.netty.buffer.ByteBuf;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;

/**
* An reader class with metrics.
*/
public class DeStoreBlockReader extends BlockReader {
Copy link
Contributor

@dbw9580 dbw9580 Oct 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MetricAccountingLocalBlockReader is a better name.

private final BlockReader mDeBlockReader;

/**
* A decorator of BlockReader.
* @param deblockReader block reader
*/
public DeStoreBlockReader(BlockReader deblockReader) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requiring the reader to be a BlockReader is too general: UnderFileSystemBlockReader is also a BlockReader, but it's obviously wrong to use that with the metric MetricKey.WORKER_BYTES_READ_CACHE.

Since we are dealing with the bytes-read-cache metric here, requiring the argument to be of type LocalFileBlockReader makes sense.

mDeBlockReader = deblockReader;
}

@Override
public ByteBuffer read(long offset, long length) throws IOException {
return mDeBlockReader.read(offset, length);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also update the metric here

}

@Override
public long getLength() {
return mDeBlockReader.getLength();
}

@Override
public ReadableByteChannel getChannel() {
return mDeBlockReader.getChannel();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the channel also needs to be wrapped to add the metric updating logic.
something looks like

return new ReadableByteChannel() {
  private final ReadableByteChannel mDelegate = mDeBlockReader.getChannel();

  @Override
  public int read(ByteBuffer dst) throws IOException {
    int bytesRead = mDelegate.read(dst);
    // update metric here
    return bytesRead;
  }
}

}

@Override
public int transferTo(ByteBuf buf) throws IOException {
int bytesReadFromCache = mDeBlockReader.transferTo(buf);
MetricsSystem.counter(MetricKey.WORKER_BYTES_READ_CACHE.getName()).inc(bytesReadFromCache);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bytesReadFromCache may be -1 at EOF.

return bytesReadFromCache;
}

@Override
public boolean isClosed() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also override close() and close the wrapped reader

return mDeBlockReader.isClosed();
}

@Override
public String getLocation() {
return mDeBlockReader.getLocation();
}

@Override
public String toString() {
return mDeBlockReader.toString();
}
}
Loading