rdblue commented on a change in pull request #4050: URL: https://github.com/apache/iceberg/pull/4050#discussion_r806008749
########## File path: core/src/main/java/org/apache/iceberg/hadoop/HadoopMetricsContext.java ########## @@ -0,0 +1,120 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.iceberg.hadoop; + +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.Map; +import java.util.function.Consumer; +import org.apache.hadoop.fs.FileSystem; +import org.apache.iceberg.exceptions.ValidationException; +import org.apache.iceberg.io.FileIOMetricsContext; + +/** + * FileIO Metrics implementation that delegates to Hadoop FileSystem + * statistics implementation using the provided scheme. + */ +public class HadoopMetricsContext implements FileIOMetricsContext { + public static final String SCHEME = "fileio.scheme"; + + private String scheme; + private transient FileSystem.Statistics statistics; + + @Override + public void initialize(Map<String, String> properties) { + ValidationException.check(properties.containsKey(SCHEME), + "Scheme is required for Hadoop FileSystem metrics reporting"); + + // FileIO has no specific implementation class, but Hadoop will + // still track and report for the provided scheme. + this.scheme = properties.get(SCHEME); + this.statistics = FileSystem.getStatistics(scheme, null); + } + + /** + * The Hadoop implementation delegates to the Hadoop delegates to the + * FileSystem.Statistics implementation and therefore does not require + * support for operations like unit() and count() as the counter + * values are not directly consumed. + * + * @param name name of the metric + * @param type numeric type of the counter value + * @param unit ignored + * @param <T> Counter numeric type + * @return counter + */ + @Override + @SuppressWarnings("unchecked") + public <T extends Number> Counter<T> counter(String name, Class<T> type, Unit unit) { + switch (name) { + case READ_BYTES: + ValidationException.check(type == Long.class, "'%s' requires Long type", READ_BYTES); + return (Counter<T>) longCounter(statistics::incrementBytesRead); + case READ_OPERATIONS: + ValidationException.check(type == Integer.class, "'%s' requires Integer type", READ_OPERATIONS); + return (Counter<T>) integerCounter(statistics::incrementReadOps); + case WRITE_BYTES: + ValidationException.check(type == Long.class, "'%s' requires Long type", WRITE_BYTES); + return (Counter<T>) longCounter(statistics::incrementBytesWritten); + case WRITE_OPERATIONS: + ValidationException.check(type == Integer.class, "'%s' requires Integer type", WRITE_OPERATIONS); + return (Counter<T>) integerCounter(statistics::incrementWriteOps); + default: + throw new IllegalArgumentException(String.format("Unsupported counter: '%s'", name)); + } + } + + private Counter<Long> longCounter(Consumer<Long> consumer) { + return new Counter<Long>() { + @Override + public void increment() { + increment(1L); + } + @Override Review comment: Style: missing newline between methods. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
