Skip to content

Commit

Permalink
Metrics master (#1117)
Browse files Browse the repository at this point in the history
* Instrumentation

* Updating comments

* Adding global statistics along with GCS Connector specific statistics

* testing

* Resolving comments

* Resolving comments
  • Loading branch information
guljain committed Mar 7, 2024
1 parent 453b2f6 commit b0fff1c
Show file tree
Hide file tree
Showing 29 changed files with 1,705 additions and 276 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@
import static com.google.cloud.hadoop.fs.gcs.GhfsStatistic.STREAM_WRITE_CLOSE_OPERATIONS;
import static com.google.cloud.hadoop.fs.gcs.GhfsStatistic.STREAM_WRITE_EXCEPTIONS;
import static com.google.cloud.hadoop.fs.gcs.GhfsStatistic.STREAM_WRITE_OPERATIONS;
import static com.google.cloud.hadoop.gcsio.GoogleCloudStorageStatusStatistics.GCS_CLIENT_RATE_LIMIT_COUNT;
import static org.apache.hadoop.fs.statistics.IOStatisticsSupport.snapshotIOStatistics;
import static org.apache.hadoop.fs.statistics.StoreStatisticNames.SUFFIX_FAILURES;
import static org.apache.hadoop.fs.statistics.impl.IOStatisticsBinding.iostatisticsStore;

import com.google.cloud.hadoop.gcsio.GoogleCloudStorageStatusStatistics;
import com.google.cloud.hadoop.gcsio.GoogleCloudStorageStatistics;
import com.google.cloud.hadoop.gcsio.StatisticTypeEnum;
import com.google.cloud.hadoop.util.GcsClientStatisticInterface;
import com.google.common.flogger.GoogleLogger;
import java.io.Closeable;
import java.net.URI;
Expand Down Expand Up @@ -64,18 +62,14 @@
* Instrumentation of GCS.
*
* <p>Counters and metrics are generally addressed in code by their name or {@link GhfsStatistic}
* and {@link GoogleCloudStorageStatusStatistics} key. There <i>may</i> be some Statistics which do
* not have an entry here. To avoid attempts to access such counters failing, the operations to
* increment/query metric values are designed to handle lookup failures.
* key. There <i>may</i> be some Statistics which do not have an entry here. To avoid attempts to
* access such counters failing, the operations to increment/query metric values are designed to
* handle lookup failures.
*
* <p>GoogleHadoopFileSystem StorageStatistics are dynamically derived from the IOStatistics.
*/
public class GhfsInstrumentation
implements Closeable,
MetricsSource,
IOStatisticsSource,
DurationTrackerFactory,
GcsClientStatisticInterface {
implements Closeable, MetricsSource, IOStatisticsSource, DurationTrackerFactory {

private static final String METRICS_SOURCE_BASENAME = "GCSMetrics";

Expand Down Expand Up @@ -214,7 +208,7 @@ public void incrementCounter(GhfsStatistic op, long count) {
*
* @param op operation
*/
private void incrementCounter(GoogleCloudStorageStatusStatistics op) {
private void incrementCounter(GoogleCloudStorageStatistics op) {

String name = op.getSymbol();
incrementMutableCounter(name, 1);
Expand Down Expand Up @@ -263,7 +257,7 @@ protected final MutableCounterLong counter(GhfsStatistic op) {
* @param op statistic to count
* @return a new counter
*/
private final MutableCounterLong counter(GoogleCloudStorageStatusStatistics op) {
private final MutableCounterLong counter(GoogleCloudStorageStatistics op) {
return counter(op.getSymbol(), op.getDescription());
}

Expand Down Expand Up @@ -353,20 +347,6 @@ private void incrementMutableCounter(String name, long count) {
}
}

/**
* Counter Metrics updation based on the Http response
*
* @param statusCode of ther Http response
*/
@Override
public void statusMetricsUpdation(int statusCode) {
switch (statusCode) {
case 429:
incrementCounter(GCS_CLIENT_RATE_LIMIT_COUNT);
break;
}
}

/**
* A duration tracker which updates a mutable counter with a metric. The metric is updated with
* the count on start; after a failure the failures count is incremented by one.
Expand Down Expand Up @@ -1006,14 +986,6 @@ private IOStatisticsStoreBuilder createStoreBuilder() {
}
});

// registering metrics of GoogleCloudStorageStatusStatistics which are all counters
EnumSet.allOf(GoogleCloudStorageStatusStatistics.class)
.forEach(
stat -> {
counter(stat);
storeBuilder.withCounters(stat.getSymbol());
});

return storeBuilder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,13 @@ public enum GhfsStatistic {
"files_delete_rejected",
"Total number of files whose delete request was rejected",
TYPE_COUNTER),
INVOCATION_COPY_FROM_LOCAL_FILE(
StoreStatisticNames.OP_COPY_FROM_LOCAL_FILE, "Calls of copyFromLocalFile()", TYPE_COUNTER),
INVOCATION_CREATE(StoreStatisticNames.OP_CREATE, "Calls of create()", TYPE_DURATION),
INVOCATION_CREATE_NON_RECURSIVE(
StoreStatisticNames.OP_CREATE_NON_RECURSIVE, "Calls of createNonRecursive()", TYPE_DURATION),
INVOCATION_DELETE(StoreStatisticNames.OP_DELETE, "Calls of delete()", TYPE_DURATION),
INVOCATION_EXISTS(StoreStatisticNames.OP_EXISTS, "Calls of exists()", TYPE_COUNTER),
INVOCATION_GET_DELEGATION_TOKEN(
StoreStatisticNames.OP_GET_DELEGATION_TOKEN, "Calls of getDelegationToken()", TYPE_COUNTER),
INVOCATION_GET_FILE_CHECKSUM(
StoreStatisticNames.OP_GET_FILE_CHECKSUM, "Calls of getFileChecksum()", TYPE_COUNTER),
INVOCATION_GET_FILE_STATUS(
StoreStatisticNames.OP_GET_FILE_STATUS, "Calls of getFileStatus()", TYPE_COUNTER),
INVOCATION_GET_FILE_CHECKSUM(
StoreStatisticNames.OP_GET_FILE_CHECKSUM, "Calls of getFileChecksum()", TYPE_COUNTER),
INVOCATION_GLOB_STATUS(StoreStatisticNames.OP_GLOB_STATUS, "Calls of globStatus()", TYPE_COUNTER),
INVOCATION_HFLUSH(StoreStatisticNames.OP_HFLUSH, "Calls of hflush()", TYPE_DURATION),
INVOCATION_HSYNC(StoreStatisticNames.OP_HSYNC, "Calls of hsync()", TYPE_DURATION),
Expand All @@ -102,6 +96,12 @@ public enum GhfsStatistic {
INVOCATION_MKDIRS(StoreStatisticNames.OP_MKDIRS, "Calls of mkdirs()", TYPE_COUNTER),
INVOCATION_OPEN(StoreStatisticNames.OP_OPEN, "Calls of open()", TYPE_DURATION),
INVOCATION_RENAME(StoreStatisticNames.OP_RENAME, "Calls of rename()", TYPE_DURATION),
INVOCATION_COPY_FROM_LOCAL_FILE(
StoreStatisticNames.OP_COPY_FROM_LOCAL_FILE, "Calls of copyFromLocalFile()", TYPE_COUNTER),
INVOCATION_CREATE_NON_RECURSIVE(
StoreStatisticNames.OP_CREATE_NON_RECURSIVE, "Calls of createNonRecursive()", TYPE_DURATION),
INVOCATION_GET_DELEGATION_TOKEN(
StoreStatisticNames.OP_GET_DELEGATION_TOKEN, "Calls of getDelegationToken()", TYPE_COUNTER),
INVOCATION_LIST_LOCATED_STATUS(
StoreStatisticNames.OP_LIST_LOCATED_STATUS, "Calls of listLocatedStatus()", TYPE_COUNTER),

Expand All @@ -120,6 +120,7 @@ public enum GhfsStatistic {
TYPE_COUNTER),
STREAM_READ_OPERATIONS(
StreamStatisticNames.STREAM_READ_OPERATIONS, "Calls of read()", TYPE_DURATION),

STREAM_READ_OPERATIONS_INCOMPLETE(
StreamStatisticNames.STREAM_READ_OPERATIONS_INCOMPLETE,
"Count of incomplete read() operations in an input stream",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
public class GhfsStorageStatistics extends StorageStatisticsFromIOStatistics {

/** {@value} The key that stores all the registered metrics */
public static final String NAME = "GhfsStorageStatistics";
public static final String NAME = "GhfsFileSystemBasedStorageStatistics";

/** Exention for minimum */
private static final String MINIMUM = StoreStatisticNames.SUFFIX_MIN;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2023 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.hadoop.fs.gcs;

import javax.annotation.Nonnull;

class GhfsStreamStats {
private final GhfsGlobalStorageStatistics storageStatistics;
private final GhfsStatistic durationStat;
private final Object context;
private long maxLatencyNs;
private int operationCount;
private long minLatencyNs;
private long totalNs;

GhfsStreamStats(
@Nonnull GhfsGlobalStorageStatistics storageStatistics,
GhfsStatistic durationStat,
Object context) {
this.storageStatistics = storageStatistics;
this.durationStat = durationStat;
this.context = context;
}

void close() {
if (operationCount == 0) {
return;
}

storageStatistics.updateStats(
durationStat,
toMillis(minLatencyNs),
toMillis(maxLatencyNs),
toMillis(totalNs),
operationCount,
context);
this.totalNs = 0;
this.operationCount = 0;
}

void updateWriteStreamStats(int len, long start) {
updateStats(start);
storageStatistics.streamWriteBytes(len);
}

void updateReadStreamSeekStats(long start) {
updateStats(start);
}

void updateReadStreamStats(int len, long start) {
updateStats(start);
storageStatistics.streamReadBytes(len);
}

private static long toMillis(long nano) {
return nano / 1000_000;
}

private void updateStats(long start) {
long latency = System.nanoTime() - start;
this.maxLatencyNs = Math.max(latency, this.maxLatencyNs);
if (operationCount == 0) {
this.minLatencyNs = latency;
} else {
this.minLatencyNs = Math.min(latency, this.minLatencyNs);
}

this.totalNs += latency;
this.operationCount++;
}
}
Loading

0 comments on commit b0fff1c

Please sign in to comment.