sk0x50 commented on code in PR #6206: URL: https://github.com/apache/ignite-3/pull/6206#discussion_r2207335942
########## modules/transactions/src/main/java/org/apache/ignite/internal/tx/metrics/TransactionMetricsSource.java: ########## @@ -0,0 +1,220 @@ +/* + * 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.ignite.internal.tx.metrics; + +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.apache.ignite.internal.tx.TransactionIds.beginTimestamp; + +import java.util.List; +import java.util.UUID; +import org.apache.ignite.internal.hlc.ClockService; +import org.apache.ignite.internal.metrics.AbstractMetricSource; +import org.apache.ignite.internal.metrics.DistributionMetric; +import org.apache.ignite.internal.metrics.LongAdderMetric; +import org.apache.ignite.internal.metrics.Metric; +import org.apache.ignite.internal.tx.metrics.TransactionMetricsSource.Holder; +import org.jetbrains.annotations.TestOnly; + +/** + * Transaction metric source, that contains a set of transaction metrics. + **/ +public class TransactionMetricsSource extends AbstractMetricSource<Holder> { + /** Histogram buckets for duration metrics in milliseconds. */ + private static final long[] HISTOGRAM_BUCKETS = new long[] { + MILLISECONDS.convert(1, MILLISECONDS), + MILLISECONDS.convert(10, MILLISECONDS), + MILLISECONDS.convert(100, MILLISECONDS), + MILLISECONDS.convert(250, MILLISECONDS), + MILLISECONDS.convert(500, MILLISECONDS), + MILLISECONDS.convert(1, SECONDS), + MILLISECONDS.convert(5, SECONDS) + }; + + /** Source name. */ + public static final String SOURCE_NAME = "transactions"; + + /** Clock service to calculate a timestamp for rolled back transactions. */ + private final ClockService clockService; + + /** + * Creates a new instance of {@link TransactionMetricsSource}. + */ + public TransactionMetricsSource(ClockService clockService) { + super(SOURCE_NAME, "Transaction metrics."); + + this.clockService = clockService; + } + + /** + * Updates read-write related metrics. + * + * @param transactionId Transaction identifier. + * @param commit {@code true} if a transaction was committed, and {@code false} otherwise. + */ + public void onReadWriteTransactionFinished(UUID transactionId, boolean commit) { + Holder holder = holder(); + + if (holder != null) { + holder.rwDuration.add(calculateTransactionDuration(transactionId)); + + holder.activeTransactions.decrement(); + + if (commit) { + holder.totalCommits.increment(); + holder.rwCommits.increment(); + } else { + holder.totalRollbacks.increment(); + holder.rwRollbacks.increment(); + } + } + } + + /** + * Updates read-only related metrics. + * + * @param transactionId Transaction identifier. + * @param commit {@code true} if a transaction was committed, and {@code false} otherwise. + */ + public void onReadOnlyTransactionFinished(UUID transactionId, boolean commit) { + Holder holder = holder(); + + if (holder != null) { + holder.roDuration.add(calculateTransactionDuration(transactionId)); + + holder.activeTransactions.decrement(); + + if (commit) { + holder.totalCommits.increment(); + holder.roCommits.increment(); + } else { + holder.totalRollbacks.increment(); + holder.roRollbacks.increment(); + } + } + } + + /** + * Tracks a number of active transactions. + */ + public void onTransactionStarted() { + Holder holder = holder(); + + if (holder != null) { + holder.activeTransactions.increment(); + } + } + + /** + * Returns a number of active transactions. + * If this metric source is not enabled, then always returns {@code 0}. + * + * @return Number of active transactions. + */ + public long activeTransactions() { + Holder holder = holder(); + + if (holder != null) { + return holder.activeTransactions.value(); + } + + return 0L; + } + + /** + * Returns a number of finished transactions. + * If this metric source is not enabled, then always returns {@code 0}. + * + * @return Number of finished transactions. + */ + public long finishedTransactions() { + Holder holder = holder(); + + if (holder != null) { + return holder.totalCommits.value() + holder.totalRollbacks.value(); + } + + return 0L; + } + + @Override + protected Holder createHolder() { + return new Holder(); + } + + private long calculateTransactionDuration(UUID transactionId) { + return clockService.currentLong() - beginTimestamp(transactionId).getPhysical(); + } + + /** Holder. */ + protected static class Holder implements AbstractMetricSource.Holder<Holder> { + private final LongAdderMetric totalCommits = new LongAdderMetric( + "TotalCommits", + "Total number of commits."); + + private final LongAdderMetric totalRollbacks = new LongAdderMetric( + "TotalRollbacks", + "Total number of rollbacks."); + + private final LongAdderMetric rwCommits = new LongAdderMetric( + "RwCommits", + "Total number of read-write transaction commits."); + + private final LongAdderMetric roCommits = new LongAdderMetric( + "RoCommits", + "Total number of read-only transaction commits."); + + private final LongAdderMetric rwRollbacks = new LongAdderMetric( + "RwRollbacks", + "Total number of rolled-back read-write transactions."); Review Comment: I would say that it still makes sense. First of all, it is about API symmetry. Read-write transactions have commit and rollback metrics, so why should we exclude them for read-only transactions? For example, a long-running scan that was rolled back due to a timeout. -- 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: notifications-unsubscr...@ignite.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org