Copilot commented on code in PR #6535:
URL: https://github.com/apache/ignite-3/pull/6535#discussion_r2325119651
##########
modules/table/src/main/java/org/apache/ignite/internal/table/distributed/TableManager.java:
##########
@@ -3134,16 +3152,47 @@ private static PartitionUpdateHandlers
createPartitionUpdateHandlers(
GcUpdateHandler gcUpdateHandler = new
GcUpdateHandler(partitionDataStorage, safeTimeTracker, indexUpdateHandler);
+ LongSupplier partSizeSupplier = () ->
partitionDataStorage.getStorage().estimatedSize();
+ PartitionModificationCounter modificationCounter =
partitionModificationCounterFactory.create(partSizeSupplier);
+ reegisterPartitionModificationCounterMetrics(table.tableId(),
partitionId, modificationCounter);
+
StorageUpdateHandler storageUpdateHandler = new StorageUpdateHandler(
partitionId,
partitionDataStorage,
indexUpdateHandler,
- replicationConfiguration
+ replicationConfiguration,
+ modificationCounter
);
return new PartitionUpdateHandlers(storageUpdateHandler,
indexUpdateHandler, gcUpdateHandler);
}
+ private void reegisterPartitionModificationCounterMetrics(
Review Comment:
Method name has a typo - "reegister" should be "register"
##########
modules/sql-engine/src/integrationTest/java/org/apache/ignite/internal/sql/engine/ItPartitionModificationCounterMetricsTest.java:
##########
@@ -0,0 +1,382 @@
+/*
+ * 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.sql.engine;
+
+import static org.apache.ignite.internal.TestWrappers.unwrapIgniteImpl;
+import static org.apache.ignite.internal.lang.IgniteStringFormatter.format;
+import static
org.apache.ignite.internal.sql.engine.statistic.PartitionModificationCounterFactoryImpl.DEFAULT_MIN_STALE_ROWS_COUNT;
+import static
org.apache.ignite.internal.table.partition.PartitionModificationCounterMetricSource.METRIC_COUNTER;
+import static
org.apache.ignite.internal.table.partition.PartitionModificationCounterMetricSource.METRIC_THRESHOLD_TIMESTAMP;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.greaterThan;
+
+import java.util.Map;
+import java.util.Objects;
+import org.apache.ignite.internal.catalog.Catalog;
+import org.apache.ignite.internal.catalog.CatalogManager;
+import org.apache.ignite.internal.catalog.descriptors.CatalogTableDescriptor;
+import org.apache.ignite.internal.metrics.LongMetric;
+import org.apache.ignite.internal.metrics.MetricManager;
+import org.apache.ignite.internal.metrics.MetricSet;
+import org.apache.ignite.internal.sql.BaseSqlIntegrationTest;
+import
org.apache.ignite.internal.table.distributed.PartitionModificationCounter;
+import
org.apache.ignite.internal.table.partition.PartitionModificationCounterMetricSource;
+import org.apache.ignite.table.KeyValueView;
+import org.apache.ignite.table.QualifiedName;
+import org.apache.ignite.tx.Transaction;
+import org.awaitility.Awaitility;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests for {@link PartitionModificationCounter partition modification
counter} metrics.
+ */
+public class ItPartitionModificationCounterMetricsTest extends
BaseSqlIntegrationTest {
+ private static final String ZONE_1_PART_NO_REPLICAS =
"zone_single_partition_no_replicas";
+ private static final String ZONE_1_PART_REPLICAS = "zone_single_partition";
+ private static final String ZONE_8_PART_NO_REPLICAS =
"zone_multi_partition";
+
+ private static final int UNDEFINED_METRIC_VALUE = -1;
+
+ @BeforeAll
+ void setupDistributionZones() {
+ sqlScript(
+ format("CREATE ZONE {} (PARTITIONS 1, REPLICAS {}) storage
profiles ['default'];", ZONE_1_PART_REPLICAS, initialNodes()),
+ format("CREATE ZONE {} (PARTITIONS 1, REPLICAS 1) storage
profiles ['default'];", ZONE_1_PART_NO_REPLICAS),
+ format("CREATE ZONE {} (PARTITIONS 8, REPLICAS 1) storage
profiles ['default'];", ZONE_8_PART_NO_REPLICAS)
+ );
+ }
+
+ @BeforeEach
+ void dropTables() {
+ dropAllTables();
+ }
+
+ /**
+ * Tests that counters are updated independently for tables in the same
zone.
+ */
+ @Test
+ void twoTablesInTheSameZone() {
+ String tab1 = "T1";
+ String tab2 = "T2";
+
+ sqlScript(
+ format("CREATE TABLE {}(id INT PRIMARY KEY, val INT) ZONE
{};", tab1, ZONE_1_PART_REPLICAS),
+ format("CREATE TABLE {}(id INT PRIMARY KEY, val INT) ZONE
{};", tab2, ZONE_1_PART_REPLICAS)
+ );
+
+ sql(format("INSERT INTO {} VALUES(0, 0), (1, 1);", tab1));
+
+ Awaitility.await().untilAsserted(
+ () -> assertThat(metricFromAnyNode(tab1, 0, METRIC_COUNTER),
is(2L))
+ );
+
+ sql(format("INSERT INTO {} VALUES(0, 0), (1, 1), (2, 2);", tab2));
+
+ Awaitility.await().untilAsserted(
+ () -> assertThat(metricFromAnyNode(tab2, 0, METRIC_COUNTER),
is(3L))
+ );
+
+ assertThat(metricFromAnyNode(tab1, 0, METRIC_COUNTER), is(2L));
+ }
+
+ /**
+ * Tests that dropping and creating a table with the same name resets the
counter value.
+ */
+ @Test
+ void recreateTableWithTheSameName() {
+ String table = "test_table";
+
+ sqlScript(
+ format("CREATE TABLE {}(id INT PRIMARY KEY, val INT) ZONE
{};", table, ZONE_1_PART_NO_REPLICAS),
+ "INSERT INTO test_table VALUES(0, 0), (1, 1);"
+ );
+ expectModsCount(table, 2);
+
+ sqlScript(
+ format("DROP TABLE {}", table),
+ format("CREATE TABLE {}(id INT PRIMARY KEY, val INT) ZONE
{};", table, ZONE_1_PART_NO_REPLICAS)
+ );
+ expectModsCount(table, 0);
+
+ sql("INSERT INTO test_table VALUES(0, 0), (1, 1);");
+ expectModsCount(table, 2);
+ }
+
+ /**
+ * Tests that different types of updates are counted.
+ */
+ @Test
+ void differentUpdateTypes() {
+ String tabName = "test_table";
+ int partsCount = 8;
+ sql(format("CREATE TABLE {}(id INT PRIMARY KEY, val INT) ZONE {};",
tabName, ZONE_8_PART_NO_REPLICAS));
+ KeyValueView<Integer, Integer> keyValueView =
CLUSTER.aliveNode().tables().table("test_table")
+ .keyValueView(Integer.class, Integer.class);
+
+ int expectedMods = 0;
+
+ // Implicit transaction.
+ {
+ sql("INSERT INTO test_table VALUES(0, 0);");
+ expectModsCount(tabName, ++expectedMods);
+
+ sql("UPDATE test_table SET val=1 WHERE id=0");
+ expectModsCount(tabName, ++expectedMods);
+
+ keyValueView.put(null, 0, 2);
+ expectModsCount(tabName, ++expectedMods);
+
+ sql("INSERT INTO test_table VALUES(1, 1), (2, 2);");
+ expectedMods += 2;
+ expectModsCount(tabName, expectedMods);
+
+ keyValueView.putAll(null, Map.of(3, 3, 4, 4, 5, 5));
+ expectedMods += 3;
+ expectModsCount(tabName, expectedMods);
+
+ sql("UPDATE test_table SET val=20 WHERE val = 2");
+ expectedMods += 2;
+ expectModsCount(tabName, expectedMods);
+
+ sql("DELETE FROM test_table");
+ expectedMods += 6;
+ expectModsCount(tabName, expectedMods);
+ }
+
+ // Explicit transaction.
+ {
+ {
+ Transaction tx = CLUSTER.aliveNode().transactions().begin();
+ sql(tx, "INSERT INTO test_table VALUES(0, 0);");
+ expectModsCount(tabName, expectedMods);
+ tx.commit();
+ expectModsCount(tabName, ++expectedMods);
+ }
+
+ {
+ Transaction tx = CLUSTER.aliveNode().transactions().begin();
+
+ sql(tx, "UPDATE test_table SET val=1 WHERE id=0");
+ keyValueView.put(tx, 0, 2);
+ sql(tx, "INSERT INTO test_table VALUES(1, 1), (2, 2);");
+ keyValueView.putAll(tx, Map.of(3, 3, 4, 4, 5, 5));
+ expectModsCount(tabName, expectedMods);
+
+ tx.commit();
+ expectedMods += 6;
+ expectModsCount(tabName, expectedMods);
+ }
+
+ {
+ Transaction tx = CLUSTER.aliveNode().transactions().begin();
+
+ sql(tx, "UPDATE test_table SET val=20 WHERE val = 2");
+ sql(tx, "DELETE FROM test_table");
+ expectModsCount(tabName, expectedMods);
+
+ tx.commit();
+ expectedMods += 6;
+ expectModsCount(tabName, expectedMods);
+ }
+ }
+ }
+
+ /**
+ * Tests that the milestone timestamp is updated only when
+ * the number of modifications reaches the configured threshold.
+ */
+ @Test
+ void reachMilestoneUpdateTest() {
+ String tableWithReplicas = "TEST_TABLE";
+ String tableNoReplicas = "TEST_TABLE_NO_REPLICAS";
+
+ sqlScript(
+ format("CREATE TABLE {}(id INT PRIMARY KEY, val INT) ZONE
{};", tableWithReplicas, ZONE_1_PART_REPLICAS),
+ format("CREATE TABLE {}(id INT PRIMARY KEY, val INT) ZONE
{};", tableNoReplicas, ZONE_1_PART_NO_REPLICAS),
+ format("INSERT INTO {} VALUES(0, 0);", tableWithReplicas),
+ format("INSERT INTO {} VALUES(0, 0);", tableNoReplicas)
+ );
+
+ Awaitility.await().untilAsserted(() ->
+ assertThat(sumNodeMetric(tableNoReplicas, 0, METRIC_COUNTER),
is(1L)));
+ Awaitility.await().untilAsserted(() ->
+ assertThat(sumNodeMetric(tableWithReplicas, 0,
METRIC_COUNTER), is((long) initialNodes())));
+
+ long initTsNoReplicas = metricFromAnyNode(tableNoReplicas, 0,
METRIC_THRESHOLD_TIMESTAMP);
+ long initTsWithReplicas = metricFromAnyNode(tableWithReplicas, 0,
METRIC_THRESHOLD_TIMESTAMP);
+
+ long modsCount = DEFAULT_MIN_STALE_ROWS_COUNT / 2;
+
+ // Perform a bunch of modifications.
+ {
+ for (int i = 0; i < modsCount; i++) {
+ sql(format("UPDATE {} SET VAL=?", tableWithReplicas), i);
+ sql(format("UPDATE {} SET VAL=?", tableNoReplicas), i);
+ }
+
+ long expectedModsCount = 1 + modsCount;
+
+ Awaitility.await().untilAsserted(() ->
+ assertThat(sumNodeMetric(tableNoReplicas, 0,
METRIC_COUNTER), is(expectedModsCount)));
+ Awaitility.await().untilAsserted(() ->
+ assertThat(sumNodeMetric(tableWithReplicas, 0,
METRIC_COUNTER), is(expectedModsCount * initialNodes())));
+
+ // Timestamp should not change as we did not reach the threshold.
+ assertThat(metricFromAnyNode(tableNoReplicas, 0,
METRIC_THRESHOLD_TIMESTAMP), is(initTsNoReplicas));
+ assertThat(metricFromAnyNode(tableWithReplicas, 0,
METRIC_THRESHOLD_TIMESTAMP), is(initTsWithReplicas));
+ }
+
+ // Perform another bunch of modifications to reach the milestone.
+ {
+ for (int i = 0; i < modsCount; i++) {
+ sql(format("UPDATE {} SET VAL=?", tableWithReplicas), i);
+ sql(format("UPDATE {} SET VAL=?", tableNoReplicas), i);
+ }
+
+ long expectedModsCount = 1 + modsCount + modsCount;
+
+ Awaitility.await().untilAsserted(() ->
+ assertThat(sumNodeMetric(tableNoReplicas, 0,
METRIC_COUNTER), is(expectedModsCount)));
+ Awaitility.await().untilAsserted(() ->
+ assertThat(sumNodeMetric(tableWithReplicas, 0,
METRIC_COUNTER), is(expectedModsCount * initialNodes())));
+
+ // Timestamp should change because we reached the threshold.
+ Awaitility.await().untilAsserted(() ->
+ assertThat(metricFromAnyNode(tableNoReplicas, 0,
METRIC_THRESHOLD_TIMESTAMP), greaterThan(initTsNoReplicas))
+ );
+ Awaitility.await().untilAsserted(() ->
+ assertThat(metricFromAnyNode(tableWithReplicas, 0,
METRIC_THRESHOLD_TIMESTAMP), greaterThan(initTsWithReplicas))
+ );
+ }
+ }
+
+ private void expectModsCount(String tableName, long value) {
+ QualifiedName qualifiedName = QualifiedName.parse(tableName);
+ CatalogManager manager = unwrapIgniteImpl(node(0)).catalogManager();
+ Catalog catalog = manager.catalog(manager.latestCatalogVersion());
+ CatalogTableDescriptor tableDesc =
catalog.table(qualifiedName.schemaName(), qualifiedName.objectName());
+ int partsCount = catalog.zone(tableDesc.zoneId()).partitions();
+
+ Awaitility.await().untilAsserted(() -> {
+ long allPartsValue = 0;
+
+ for (int part = 0; part < partsCount; part++) {
+ allPartsValue += sumNodeMetric(tableName, part,
METRIC_COUNTER);
+ }
+
+ assertThat(allPartsValue, is(value));
+ });
+ }
+
+ private int tableIdByName(QualifiedName qualifiedName) {
+ CatalogManager manager = unwrapIgniteImpl(node(0)).catalogManager();
+ Catalog catalog = manager.catalog(manager.latestCatalogVersion());
+ CatalogTableDescriptor tableDesc =
catalog.table(qualifiedName.schemaName(), qualifiedName.objectName());
+
+ assert tableDesc != null;
+
+ return tableDesc.id();
+ }
+
+ private long metricFromAnyNode(String tableName, int partId, String
metricName) {
+ for (int i = 0; i < CLUSTER.nodes().size(); i++) {
+ long value = metricFromNode(i, tableName, partId, metricName);
+
+ if (value >= 0) {
+ return value;
+ }
+ }
+
+ return UNDEFINED_METRIC_VALUE;
+ }
+
+ private long metricFromNode(int nodeIdx, String tableName, int partId,
String metricName) {
+ int tableId = tableIdByName(QualifiedName.parse(tableName));
+
+ String metricSourceName =
+
PartitionModificationCounterMetricSource.formatSourceName(tableId, partId);
+
+ MetricManager metricManager =
unwrapIgniteImpl(node(nodeIdx)).metricManager();
+
+ MetricSet metrics =
metricManager.metricSnapshot().metrics().get(metricSourceName);
+
+ if (metrics != null) {
+ LongMetric metric = metrics.get(metricName);
+ Objects.requireNonNull(metric, "metric does not exist: " +
metricName);
+
+ return metric.value();
+ }
+
+ return UNDEFINED_METRIC_VALUE;
+ }
+
+ private long sumNodeMetric(String tableName, int partId, String
metricName) {
+ int tableId = tableIdByName(QualifiedName.parse(tableName));
+ long result = 0;
+
+ String metricSourceName =
+
PartitionModificationCounterMetricSource.formatSourceName(tableId, partId);
+
+ boolean notFound = true;
+
+ for (int i = 0; i < CLUSTER.nodes().size(); i++) {
+ MetricManager metricManager =
unwrapIgniteImpl(node(i)).metricManager();
+
+ MetricSet metrics =
metricManager.metricSnapshot().metrics().get(metricSourceName);
+
+ if (metrics != null) {
+ LongMetric metric = metrics(tableId, partId).get(metricName);
Review Comment:
This line uses `metrics(tableId, partId)` but should use the `metrics`
variable from the previous line to avoid potential inconsistency
```suggestion
LongMetric metric = metrics.get(metricName);
```
--
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]