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

add logs with request latency for json readData/metaData API #749

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import static java.lang.Math.min;
import static java.lang.Math.toIntExact;
import static java.util.Arrays.stream;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toMap;

Expand Down Expand Up @@ -66,6 +67,7 @@
import com.google.cloud.hadoop.util.RetryDeterminer;
import com.google.cloud.hadoop.util.RetryHttpInitializer;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Stopwatch;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
Expand Down Expand Up @@ -2139,13 +2141,28 @@ private StorageObject getObject(StorageResourceId resourceId) throws IOException
// Request only fields used in GoogleCloudStorageItemInfo:
// https://cloud.google.com/storage/docs/json_api/v1/objects#resource-representations
.setFields(OBJECT_FIELDS);
Stopwatch metadataStopwatch = Stopwatch.createStarted();

try {
return getObject.execute();
StorageObject object = getObject.execute();

logger.atFinest().log(
"GoogleCloudStorageImpl:getMetadata complete context:%d,time:%d,resource:%s,requestId:%s",
Thread.currentThread().getId(),
metadataStopwatch.elapsed(MILLISECONDS),
resourceId,
getObject.getLastResponseHeaders().getFirstHeaderStringValue("x-guploader-uploadid"));
return object;
} catch (IOException e) {
if (errorExtractor.itemNotFound(e)) {
logger.atFiner().withCause(e).log("getObject(%s): not found", resourceId);
logger.atFiner().withCause(e).log(
"getObject(%s): not found, context:%d,time:%d",
resourceId, Thread.currentThread().getId(), metadataStopwatch.elapsed(MILLISECONDS));
return null;
}
logger.atSevere().withCause(e).log(
"GoogleCloudStorageImpl:getMetadata exception context:%d,time:%d,resource:%s",
Thread.currentThread().getId(), metadataStopwatch.elapsed(MILLISECONDS), resourceId);
throw new IOException("Error accessing " + resourceId, e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static com.google.common.base.Strings.nullToEmpty;
import static java.lang.Math.min;
import static java.lang.Math.toIntExact;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

import com.google.api.client.http.HttpHeaders;
import com.google.api.client.http.HttpResponse;
Expand All @@ -39,6 +40,7 @@
import com.google.cloud.hadoop.util.ResilientOperation;
import com.google.cloud.hadoop.util.RetryDeterminer;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Stopwatch;
import com.google.common.base.Suppliers;
import com.google.common.flogger.GoogleLogger;
import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -66,6 +68,8 @@ public class GoogleCloudStorageReadChannel implements SeekableByteChannel {

private static final String GZIP_ENCODING = "gzip";

private static final String GCS_REQUEST_ID_HEADER = "x-guploader-uploadid";

// GCS access instance.
private final Storage gcs;

Expand Down Expand Up @@ -104,6 +108,8 @@ public class GoogleCloudStorageReadChannel implements SeekableByteChannel {
// Size of the contentChannel.
private long contentChannelEnd = -1;

private Stopwatch stopwatch;

// Whether to use bounded range requests or streaming requests.
@VisibleForTesting boolean randomAccess;

Expand Down Expand Up @@ -229,6 +235,7 @@ protected GoogleCloudStorageItemInfo getInitialMetadata() throws IOException {
/** Returns {@link GoogleCloudStorageItemInfo} used to initialize metadata in constructor. */
private GoogleCloudStorageItemInfo fetchInitialMetadata() throws IOException {
StorageObject object;
Stopwatch metadataStopwatch = Stopwatch.createStarted();
try {
// Request only fields that are used for metadata initialization
Storage.Objects.Get getObject = createRequest().setFields("contentEncoding,generation,size");
Expand All @@ -239,11 +246,26 @@ private GoogleCloudStorageItemInfo fetchInitialMetadata() throws IOException {
RetryDeterminer.SOCKET_ERRORS,
IOException.class,
sleeper);

logger.atFinest().log(
"GoogleCloudStorageReadChannel:getMetadata complete context:%d,time:%d,resource:%s,requestId:%s",
Copy link
Contributor

Choose a reason for hiding this comment

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

we don't need to log the class name GoogleCloudStorageReadChannel logging pattern have the fully qualified class name

Thread.currentThread().getId(),
metadataStopwatch.elapsed(MILLISECONDS),
resourceId,
getObject.getLastResponseHeaders().getFirstHeaderStringValue(GCS_REQUEST_ID_HEADER));
} catch (IOException e) {
throw errorExtractor.itemNotFound(e)
? createFileNotFoundException(resourceId, e)
: new IOException("Error reading " + resourceId, e);
if (errorExtractor.itemNotFound(e)) {
throw createFileNotFoundException(resourceId, e);
Copy link
Contributor

Choose a reason for hiding this comment

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

We need to add a log here as well

} else {
logger.atSevere().withCause(e).log(
"GoogleCloudStorageReadChannel:getMetadata exception context:%d,time:%d,resource:%s",
Thread.currentThread().getId(), metadataStopwatch.elapsed(MILLISECONDS), resourceId);
throw new IOException("Error reading " + resourceId, e);
}
} catch (InterruptedException e) {
logger.atSevere().withCause(e).log(
"GoogleCloudStorageReadChannel:getMetadata exception context:%d,time:%d,resource:%s",
Thread.currentThread().getId(), metadataStopwatch.elapsed(MILLISECONDS), resourceId);
Thread.currentThread().interrupt();
throw new IOException("Thread interrupt received.", e);
}
Expand Down Expand Up @@ -398,8 +420,12 @@ public int read(ByteBuffer buffer) throws IOException {

++retriesAttempted;
logger.atWarning().withCause(ioe).log(
"Failed read retry #%s/%s for '%s'. Sleeping...",
retriesAttempted, maxRetries, resourceId);
"Failed read context:%d,retry:%s/%s,time:%d,resource:%s",
Thread.currentThread().getId(),
retriesAttempted,
maxRetries,
stopwatch.elapsed(MILLISECONDS),
resourceId);
try {
boolean backOffSuccessful = BackOffUtils.next(sleeper, readBackOff.get());
if (!backOffSuccessful) {
Expand Down Expand Up @@ -930,7 +956,17 @@ protected InputStream openStream(long bytesToRead) throws IOException {
Storage.Objects.Get getObject = createDataRequest(rangeHeader);
HttpResponse response;
try {
stopwatch = Stopwatch.createStarted();
response = getObject.executeMedia();

logger.atFinest().log(
"openStream complete context:%d,time:%d,bytesToRead:%d,rangeSize:%d,resource:%s,requestId:%s",
Thread.currentThread().getId(),
stopwatch.elapsed(MILLISECONDS),
bytesToRead,
contentChannelEnd - contentChannelPosition,
resourceId,
response.getHeaders().getFirstHeaderStringValue(GCS_REQUEST_ID_HEADER));
// TODO(b/110832992): validate response range header against expected/request range
} catch (IOException e) {
if (!metadataInitialized && errorExtractor.rangeNotSatisfiable(e) && currentPosition == 0) {
Expand Down Expand Up @@ -995,13 +1031,21 @@ protected InputStream openStream(long bytesToRead) throws IOException {
cacheFooter(response);
if (retriesCount != 0) {
logger.atInfo().log(
"Successfully cached footer after %s retries for '%s'", retriesCount, resourceId);
"Successfully cached footer context:%d,retries:%s,time:%d,resource:%s",
Thread.currentThread().getId(),
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we need to log the threadId, it should already be part of the logging pattern (thread Id/name)

retriesCount,
stopwatch.elapsed(MILLISECONDS),
resourceId);
}
break;
} catch (IOException footerException) {
logger.atInfo().withCause(footerException).log(
"Failed to prefetch footer (retry #%s/%s) for '%s'",
retriesCount + 1, maxRetries, resourceId);
logger.atWarning().withCause(footerException).log(
"Failed to prefetch footer context:%d,retry:%s/%s,time:%d,resource:%s",
Thread.currentThread().getId(),
retriesCount + 1,
maxRetries,
stopwatch.elapsed(MILLISECONDS),
resourceId);
if (retriesCount == 0) {
readBackOff.get().reset();
}
Expand Down