Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/iceberg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ set(ICEBERG_SOURCES
manifest/v3_metadata.cc
metadata_columns.cc
metrics_config.cc
metrics/commit_report.cc
metrics/counter.cc
metrics/json_serde.cc
metrics/metrics_context.cc
metrics/metrics_reporters.cc
metrics/scan_report.cc
metrics/timer.cc
name_mapping.cc
partition_field.cc
partition_spec.cc
Expand Down Expand Up @@ -219,6 +226,7 @@ add_subdirectory(puffin)
add_subdirectory(row)
add_subdirectory(update)
add_subdirectory(util)
add_subdirectory(metrics)

if(ICEBERG_BUILD_BUNDLE)
set(ICEBERG_BUNDLE_SOURCES
Expand Down
1 change: 1 addition & 0 deletions src/iceberg/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace iceberg {
constexpr std::string_view kParquetFieldIdKey = "PARQUET:field_id";
constexpr int64_t kInvalidSnapshotId = -1;
constexpr int64_t kInvalidSequenceNumber = -1;
constexpr int64_t kInvalidSchemaId = -1;
/// \brief Stand-in for the current sequence number that will be assigned when the commit
/// is successful. This is replaced when writing a manifest list by the ManifestFile
/// adapter.
Expand Down
8 changes: 8 additions & 0 deletions src/iceberg/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ iceberg_sources = files(
'manifest/v2_metadata.cc',
'manifest/v3_metadata.cc',
'metadata_columns.cc',
'metrics/commit_report.cc',
'metrics/counter.cc',
'metrics/json_serde.cc',
'metrics/metrics_context.cc',
'metrics/metrics_reporters.cc',
'metrics/scan_report.cc',
'metrics/timer.cc',
'metrics_config.cc',
'name_mapping.cc',
'partition_field.cc',
Expand Down Expand Up @@ -273,6 +280,7 @@ subdir('data')
subdir('deletes')
subdir('expression')
subdir('manifest')
subdir('metrics')
subdir('puffin')
subdir('row')
subdir('update')
Expand Down
18 changes: 18 additions & 0 deletions src/iceberg/metrics/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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.

iceberg_install_all_headers(iceberg/metrics)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Missing meson.build equivalent for this subdirectory

106 changes: 106 additions & 0 deletions src/iceberg/metrics/commit_report.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* 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.
*/

#include "iceberg/metrics/commit_report.h"

#include "iceberg/snapshot.h"
#include "iceberg/util/string_util.h"

namespace iceberg {

CommitMetrics CommitMetrics::Of(MetricsContext& context) {
CommitMetrics m;
m.total_duration = context.GetTimer("total-duration");
m.attempts = context.GetCounter("attempts");
return m;
}

CommitMetrics CommitMetrics::Noop() { return CommitMetrics::Of(MetricsContext::Null()); }

void CommitMetrics::PopulateResult(CommitMetricsResult& result) const {
result.total_duration =
total_duration ? TimerResult{.unit = std::string(total_duration->Unit()),
.count = total_duration->Count(),
.total_duration = total_duration->TotalDuration()}
: TimerResult{};
result.attempts =
attempts ? CounterResult{.unit = attempts->Unit(), .value = attempts->Value()}
: CounterResult{};
}

CommitMetricsResult CommitMetricsResult::From(
const CommitMetrics& live_metrics,
const std::unordered_map<std::string, std::string>& snapshot_summary) {
CommitMetricsResult result;
live_metrics.PopulateResult(result);

// Helpers: parse a summary key and wrap as a typed CounterResult.
auto count_field = [&snapshot_summary](const std::string& key) -> CounterResult {
auto it = snapshot_summary.find(key);
if (it == snapshot_summary.end()) return {};
auto parsed = StringUtils::ParseNumber<int64_t>(it->second);
return {.unit = CounterUnit::kCount,
.value = parsed.has_value() ? parsed.value() : 0};
};
auto bytes_field = [&snapshot_summary](const std::string& key) -> CounterResult {
auto it = snapshot_summary.find(key);
if (it == snapshot_summary.end()) return {.unit = CounterUnit::kBytes};
auto parsed = StringUtils::ParseNumber<int64_t>(it->second);
return {.unit = CounterUnit::kBytes,
.value = parsed.has_value() ? parsed.value() : 0};
};

result.added_data_files = count_field(SnapshotSummaryFields::kAddedDataFiles);
result.removed_data_files = count_field(SnapshotSummaryFields::kDeletedDataFiles);
result.total_data_files = count_field(SnapshotSummaryFields::kTotalDataFiles);
result.added_delete_files = count_field(SnapshotSummaryFields::kAddedDeleteFiles);
result.added_equality_delete_files =
count_field(SnapshotSummaryFields::kAddedEqDeleteFiles);
result.added_positional_delete_files =
count_field(SnapshotSummaryFields::kAddedPosDeleteFiles);
result.added_dvs = count_field(SnapshotSummaryFields::kAddedDVs);
result.removed_positional_delete_files =
count_field(SnapshotSummaryFields::kRemovedPosDeleteFiles);
result.removed_dvs = count_field(SnapshotSummaryFields::kRemovedDVs);
result.removed_equality_delete_files =
count_field(SnapshotSummaryFields::kRemovedEqDeleteFiles);
result.removed_delete_files = count_field(SnapshotSummaryFields::kRemovedDeleteFiles);
result.total_delete_files = count_field(SnapshotSummaryFields::kTotalDeleteFiles);
result.added_records = count_field(SnapshotSummaryFields::kAddedRecords);
result.removed_records = count_field(SnapshotSummaryFields::kDeletedRecords);
result.total_records = count_field(SnapshotSummaryFields::kTotalRecords);
result.added_files_size_bytes = bytes_field(SnapshotSummaryFields::kAddedFileSize);
result.removed_files_size_bytes = bytes_field(SnapshotSummaryFields::kRemovedFileSize);
result.total_files_size_bytes = bytes_field(SnapshotSummaryFields::kTotalFileSize);
result.added_positional_deletes = count_field(SnapshotSummaryFields::kAddedPosDeletes);
result.removed_positional_deletes =
count_field(SnapshotSummaryFields::kRemovedPosDeletes);
result.total_positional_deletes = count_field(SnapshotSummaryFields::kTotalPosDeletes);
result.added_equality_deletes = count_field(SnapshotSummaryFields::kAddedEqDeletes);
result.removed_equality_deletes = count_field(SnapshotSummaryFields::kRemovedEqDeletes);
result.total_equality_deletes = count_field(SnapshotSummaryFields::kTotalEqDeletes);
result.kept_manifest_count = count_field(SnapshotSummaryFields::kManifestsKept);
result.created_manifest_count = count_field(SnapshotSummaryFields::kManifestsCreated);
result.replaced_manifest_count = count_field(SnapshotSummaryFields::kManifestsReplaced);
result.processed_manifest_entries_count =
count_field(SnapshotSummaryFields::kEntriesProcessed);
return result;
}

} // namespace iceberg
155 changes: 155 additions & 0 deletions src/iceberg/metrics/commit_report.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* 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.
*/

#pragma once

#include <memory>
#include <string>
#include <unordered_map>

#include "iceberg/constants.h"
#include "iceberg/iceberg_export.h"
#include "iceberg/metrics/metrics_context.h"
#include "iceberg/metrics/metrics_types.h"
#include "iceberg/metrics/timer.h"

namespace iceberg {

// Forward declaration: CommitMetricsResult is defined later in this header.
struct CommitMetricsResult;

/// \brief Live commit metrics collected during a table commit operation.
///
/// Tracks the overall commit duration and retry count. File/record counts come
/// from the snapshot summary after the commit succeeds and are stored separately
/// in CommitMetricsResult.
class ICEBERG_EXPORT CommitMetrics {
public:
/// \brief Create a CommitMetrics instance backed by the given MetricsContext.
static CommitMetrics Of(MetricsContext& context);

/// \brief Create a CommitMetrics instance with all-noop timer and counter.
static CommitMetrics Noop();

/// \brief Snapshot timer and counter values into the corresponding fields of result.
///
/// Only total_duration and attempts are written; the caller is responsible for
/// populating the remaining snapshot-summary fields.
void PopulateResult(CommitMetricsResult& result) const;

/// \brief Timer measuring total wall-clock time of the commit call.
std::shared_ptr<Timer> total_duration;

/// \brief Counter for the number of commit attempts (including retries).
std::shared_ptr<Counter> attempts;
};

/// \brief Immutable snapshot of commit metrics for use in CommitReport.
struct ICEBERG_EXPORT CommitMetricsResult {
/// \brief Total wall-clock duration of the commit attempt.
TimerResult total_duration;
/// \brief Number of commit attempts (1 on success without retries).
CounterResult attempts;
/// \brief Number of data files added in this commit.
CounterResult added_data_files;
/// \brief Number of data files removed in this commit.
CounterResult removed_data_files;
/// \brief Total live data files after this commit.
CounterResult total_data_files;
/// \brief Number of delete files added in this commit.
CounterResult added_delete_files;
/// \brief Equality delete files added.
CounterResult added_equality_delete_files;
/// \brief Positional delete files added.
CounterResult added_positional_delete_files;
/// \brief Deletion vectors added.
CounterResult added_dvs;
/// \brief Positional delete files removed.
CounterResult removed_positional_delete_files;
/// \brief Deletion vectors removed.
CounterResult removed_dvs;
/// \brief Equality delete files removed.
CounterResult removed_equality_delete_files;
/// \brief Number of delete files removed in this commit.
CounterResult removed_delete_files;
/// \brief Total live delete files after this commit.
CounterResult total_delete_files;
/// \brief Number of records added in this commit.
CounterResult added_records;
/// \brief Number of records removed in this commit.
CounterResult removed_records;
/// \brief Total live records after this commit.
CounterResult total_records;
/// \brief Total byte size of files added.
CounterResult added_files_size_bytes;
/// \brief Total byte size of files removed.
CounterResult removed_files_size_bytes;
/// \brief Total byte size of all live files after this commit.
CounterResult total_files_size_bytes;
/// \brief Positional delete records added.
CounterResult added_positional_deletes;
/// \brief Positional delete records removed.
CounterResult removed_positional_deletes;
/// \brief Total positional delete records after this commit.
CounterResult total_positional_deletes;
/// \brief Equality delete records added.
CounterResult added_equality_deletes;
/// \brief Equality delete records removed.
CounterResult removed_equality_deletes;
/// \brief Total equality delete records after this commit.
CounterResult total_equality_deletes;
/// \brief Manifest files kept unchanged in this commit.
CounterResult kept_manifest_count;
/// \brief Manifest files created in this commit.
CounterResult created_manifest_count;
/// \brief Manifest files replaced in this commit.
CounterResult replaced_manifest_count;
/// \brief Manifest entries processed in this commit.
CounterResult processed_manifest_entries_count;

bool operator==(const CommitMetricsResult&) const = default;

/// \brief Build a CommitMetricsResult from live metrics and a snapshot summary map.
///
/// Combines timer/retry measurements from \p live_metrics with records parsed
/// from \p snapshot_summary. Missing or unparseable summary keys default to 0.
static CommitMetricsResult From(
const CommitMetrics& live_metrics,
const std::unordered_map<std::string, std::string>& snapshot_summary);
};

/// \brief Report generated after a commit operation.
///
/// Contains metrics about the changes made in a commit.
struct ICEBERG_EXPORT CommitReport {
/// \brief The fully qualified name of the table that was modified.
std::string table_name;
/// \brief The snapshot ID created by this commit.
int64_t snapshot_id = kInvalidSnapshotId;
/// \brief The sequence number assigned to this commit.
int64_t sequence_number = kInvalidSequenceNumber;
/// \brief The operation that was performed (write, delete, etc.).
std::string operation;
/// \brief Metrics collected during the commit operation.
CommitMetricsResult commit_metrics;
/// \brief Additional key-value metadata.
std::unordered_map<std::string, std::string> metadata;
};

} // namespace iceberg
51 changes: 51 additions & 0 deletions src/iceberg/metrics/counter.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.
*/

#include "iceberg/metrics/counter.h"

namespace iceberg {

namespace {

class NoopCounter final : public Counter {
public:
void Increment() override {}
void Increment(int64_t) override {}
int64_t Value() const override { return 0; }
bool IsNoop() const override { return true; }
};

} // namespace

Counter& Counter::Noop() {
static NoopCounter instance;
return instance;
}

DefaultCounter::DefaultCounter(CounterUnit unit) : unit_(unit) {}

void DefaultCounter::Increment() { count_.fetch_add(1, std::memory_order_relaxed); }

void DefaultCounter::Increment(int64_t amount) {
count_.fetch_add(amount, std::memory_order_relaxed);
}

int64_t DefaultCounter::Value() const { return count_.load(std::memory_order_relaxed); }

} // namespace iceberg
Loading
Loading