Skip to content

Commit 150f2c2

Browse files
committed
IGNITE-17587 Fixed the "io.datastorage.StorageSize" metric in case of multiple persistence dataregions (#10225)
(cherry picked from commit 15abfae)
1 parent a8f536b commit 150f2c2

6 files changed

Lines changed: 74 additions & 74 deletions

File tree

modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/DataStorageMetricsImpl.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,8 @@ public boolean metricsEnabled() {
739739
* @param totalPages Total number of all pages in checkpoint.
740740
* @param dataPages Total number of data pages in checkpoint.
741741
* @param cowPages Total number of COW-ed pages in checkpoint.
742+
* @param storageSize Storage space allocated, in bytes.
743+
* @param sparseStorageSize Storage space allocated adjusted for possible sparsity, in bytes.
742744
*/
743745
public void onCheckpoint(
744746
long beforeLockDuration,
@@ -755,7 +757,9 @@ public void onCheckpoint(
755757
long start,
756758
long totalPages,
757759
long dataPages,
758-
long cowPages
760+
long cowPages,
761+
long storageSize,
762+
long sparseStorageSize
759763
) {
760764
if (metricsEnabled) {
761765
lastCpBeforeLockDuration.value(beforeLockDuration);
@@ -773,6 +777,8 @@ public void onCheckpoint(
773777
lastCpTotalPages.value(totalPages);
774778
lastCpDataPages.value(dataPages);
775779
lastCpCowPages.value(cowPages);
780+
this.storageSize.value(storageSize);
781+
this.sparseStorageSize.value(sparseStorageSize);
776782

777783
totalCheckpointTime.add(duration);
778784

@@ -790,20 +796,6 @@ public void onCheckpoint(
790796
}
791797
}
792798

793-
/**
794-
* @param sparseStorageSize Sparse storage size.
795-
* @param storageSize Storage size.
796-
*/
797-
public void onStorageSizeChanged(
798-
long storageSize,
799-
long sparseStorageSize
800-
) {
801-
if (metricsEnabled) {
802-
this.storageSize.value(storageSize);
803-
this.sparseStorageSize.value(sparseStorageSize);
804-
}
805-
}
806-
807799
/**
808800
* Callback on logging a record to a WAL.
809801
*

modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/GridCacheDatabaseSharedManager.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,6 +2053,19 @@ public long forGroupPageStores(CacheGroupContext gctx, ToLongFunction<PageStore>
20532053
return res;
20542054
}
20552055

2056+
/**
2057+
* @param f Consumer.
2058+
* @return Accumulated result for all page stores.
2059+
*/
2060+
public long forAllPageStores(ToLongFunction<PageStore> f) {
2061+
long res = 0;
2062+
2063+
for (CacheGroupContext gctx : cacheGroupContexts())
2064+
res += forGroupPageStores(gctx, f);
2065+
2066+
return res;
2067+
}
2068+
20562069
/**
20572070
* Calculates tail pointer for WAL at the end of logical recovery.
20582071
*
@@ -3169,11 +3182,6 @@ public NodeFileLockHolder(String rootDir, @NotNull GridKernalContext ctx, Ignite
31693182
return new DataStorageMetricsSnapshot(dsMetrics);
31703183
}
31713184

3172-
/** {@inheritDoc} */
3173-
@Override public DataStorageMetricsImpl dataStorageMetricsImpl() {
3174-
return dsMetrics;
3175-
}
3176-
31773185
/** {@inheritDoc} */
31783186
@Override public MetaStorage metaStorage() {
31793187
return metaStorage;

modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/GridCacheOffheapManager.java

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import java.util.concurrent.Executor;
3434
import java.util.concurrent.atomic.AtomicBoolean;
3535
import java.util.concurrent.atomic.AtomicLong;
36-
import java.util.function.ToLongFunction;
3736
import java.util.stream.Stream;
3837
import javax.cache.processor.EntryProcessor;
3938
import org.apache.ignite.IgniteCheckedException;
@@ -50,7 +49,6 @@
5049
import org.apache.ignite.internal.pagemem.PageMemory;
5150
import org.apache.ignite.internal.pagemem.PageSupport;
5251
import org.apache.ignite.internal.pagemem.store.IgnitePageStoreManager;
53-
import org.apache.ignite.internal.pagemem.store.PageStore;
5452
import org.apache.ignite.internal.pagemem.wal.IgniteWriteAheadLogManager;
5553
import org.apache.ignite.internal.pagemem.wal.WALIterator;
5654
import org.apache.ignite.internal.pagemem.wal.record.DataEntry;
@@ -82,7 +80,6 @@
8280
import org.apache.ignite.internal.processors.cache.mvcc.MvccSnapshot;
8381
import org.apache.ignite.internal.processors.cache.mvcc.MvccVersion;
8482
import org.apache.ignite.internal.processors.cache.persistence.checkpoint.CheckpointListener;
85-
import org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager;
8683
import org.apache.ignite.internal.processors.cache.persistence.freelist.AbstractFreeList;
8784
import org.apache.ignite.internal.processors.cache.persistence.freelist.CacheFreeList;
8885
import org.apache.ignite.internal.processors.cache.persistence.freelist.SimpleDataRow;
@@ -126,6 +123,7 @@
126123
import org.apache.ignite.internal.util.typedef.internal.U;
127124
import org.apache.ignite.lang.IgniteBiTuple;
128125
import org.jetbrains.annotations.Nullable;
126+
129127
import static org.apache.ignite.failure.FailureType.CRITICAL_ERROR;
130128
import static org.apache.ignite.internal.processors.cache.GridCacheTtlManager.DFLT_UNWIND_THROTTLING_TIMEOUT;
131129
import static org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionState.EVICTED;
@@ -279,47 +277,6 @@ public IndexStorage getIndexStorage() {
279277
syncMetadata(ctx, ctx.executor(), false);
280278
}
281279

282-
/** {@inheritDoc} */
283-
@Override public void afterCheckpointEnd(Context ctx) throws IgniteCheckedException {
284-
persStoreMetrics.onStorageSizeChanged(
285-
forAllPageStores(PageStore::size),
286-
forAllPageStores(PageStore::getSparseSize)
287-
);
288-
}
289-
290-
/**
291-
* @param f Consumer.
292-
* @return Accumulated result for all page stores.
293-
*/
294-
private long forAllPageStores(ToLongFunction<PageStore> f) {
295-
return forGroupPageStores(grp, f);
296-
}
297-
298-
/**
299-
* @param gctx Group context.
300-
* @param f Consumer.
301-
* @return Accumulated result for all page stores.
302-
*/
303-
private long forGroupPageStores(CacheGroupContext gctx, ToLongFunction<PageStore> f) {
304-
int groupId = gctx.groupId();
305-
306-
long res = 0;
307-
308-
try {
309-
Collection<PageStore> stores = ((FilePageStoreManager)ctx.cache().context().pageStore()).getStores(groupId);
310-
311-
if (stores != null) {
312-
for (PageStore store : stores)
313-
res += f.applyAsLong(store);
314-
}
315-
}
316-
catch (IgniteCheckedException e) {
317-
throw new IgniteException(e);
318-
}
319-
320-
return res;
321-
}
322-
323280
/**
324281
* Syncs and saves meta-information of all data structures to page memory.
325282
*

modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/checkpoint/Checkpointer.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.apache.ignite.internal.processors.cache.GridCacheProcessor;
4545
import org.apache.ignite.internal.processors.cache.persistence.CheckpointState;
4646
import org.apache.ignite.internal.processors.cache.persistence.DataStorageMetricsImpl;
47+
import org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager;
4748
import org.apache.ignite.internal.processors.cache.persistence.GridCacheOffheapManager;
4849
import org.apache.ignite.internal.processors.cache.persistence.pagemem.CheckpointMetricsTracker;
4950
import org.apache.ignite.internal.processors.cache.persistence.pagemem.PageMemoryEx;
@@ -622,6 +623,8 @@ private void updateMetrics(Checkpoint chp, CheckpointMetricsTracker tracker) {
622623
}
623624

624625
if (persStoreMetrics.metricsEnabled()) {
626+
GridCacheDatabaseSharedManager dbMgr = (GridCacheDatabaseSharedManager)cacheProcessor.context().database();
627+
625628
persStoreMetrics.onCheckpoint(
626629
tracker.beforeLockDuration(),
627630
tracker.lockWaitDuration(),
@@ -637,7 +640,9 @@ private void updateMetrics(Checkpoint chp, CheckpointMetricsTracker tracker) {
637640
tracker.checkpointStartTime(),
638641
chp.pagesSize,
639642
tracker.dataPagesWritten(),
640-
tracker.cowPagesWritten()
643+
tracker.cowPagesWritten(),
644+
dbMgr.forAllPageStores(PageStore::size),
645+
dbMgr.forAllPageStores(PageStore::getSparseSize)
641646
);
642647
}
643648
}

modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/file/FilePageStoreManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,7 @@ private IgniteCheckedException shutdown(PageStore store, boolean cleanFile, Igni
10841084
}
10851085

10861086
/**
1087-
* Return cache store holedr.
1087+
* Return cache store holder.
10881088
*
10891089
* @param grpId Cache group ID.
10901090
* @return Cache store holder.

modules/core/src/test/java/org/apache/ignite/internal/processors/cache/persistence/IgniteDataStorageMetricsSelfTest.java

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
import java.io.File;
2121
import java.io.Serializable;
2222
import java.util.Arrays;
23+
import java.util.Collection;
2324
import java.util.Objects;
2425
import java.util.concurrent.ThreadLocalRandom;
2526
import java.util.concurrent.atomic.AtomicInteger;
2627
import java.util.concurrent.atomic.AtomicLong;
2728
import java.util.function.Consumer;
29+
import java.util.function.ToLongFunction;
2830
import java.util.function.UnaryOperator;
2931
import java.util.regex.Matcher;
3032
import java.util.regex.Pattern;
@@ -54,11 +56,13 @@
5456
import org.apache.ignite.internal.processors.metric.impl.LongAdderMetric;
5557
import org.apache.ignite.internal.processors.metric.impl.LongGauge;
5658
import org.apache.ignite.internal.util.tostring.GridToStringInclude;
59+
import org.apache.ignite.internal.util.typedef.F;
5760
import org.apache.ignite.internal.util.typedef.PAX;
5861
import org.apache.ignite.internal.util.typedef.internal.S;
5962
import org.apache.ignite.internal.util.typedef.internal.U;
6063
import org.apache.ignite.mxbean.DataStorageMetricsMXBean;
6164
import org.apache.ignite.spi.metric.HistogramMetric;
65+
import org.apache.ignite.spi.metric.LongMetric;
6266
import org.apache.ignite.testframework.ListeningTestLogger;
6367
import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
6468
import org.junit.Test;
@@ -68,8 +72,10 @@
6872
import static org.apache.ignite.cache.CacheMode.PARTITIONED;
6973
import static org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC;
7074
import static org.apache.ignite.cluster.ClusterState.ACTIVE;
75+
import static org.apache.ignite.internal.processors.cache.CacheGroupMetricsImpl.CACHE_GROUP_METRICS_PREFIX;
7176
import static org.apache.ignite.internal.processors.cache.persistence.DataStorageMetricsImpl.DATASTORAGE_METRIC_PREFIX;
7277
import static org.apache.ignite.internal.processors.cache.persistence.wal.serializer.RecordV1Serializer.HEADER_RECORD_SIZE;
78+
import static org.apache.ignite.internal.processors.metric.impl.MetricUtils.metricName;
7379
import static org.apache.ignite.testframework.GridTestUtils.setFieldValue;
7480
import static org.apache.ignite.testframework.GridTestUtils.waitForCondition;
7581

@@ -80,9 +86,18 @@ public class IgniteDataStorageMetricsSelfTest extends GridCommonAbstractTest {
8086
/** */
8187
private static final String GROUP1 = "grp1";
8288

89+
/** */
90+
private static final String GROUP2 = "grp2";
91+
8392
/** */
8493
private static final String NO_PERSISTENCE = "no-persistence";
8594

95+
/** */
96+
private static final String PERSISTENCE_REGION_1 = "persistence-1";
97+
98+
/** */
99+
private static final String PERSISTENCE_REGION_2 = "persistence-2";
100+
86101
/** */
87102
private final ListeningTestLogger listeningLog = new ListeningTestLogger(log);
88103

@@ -111,12 +126,18 @@ public class IgniteDataStorageMetricsSelfTest extends GridCommonAbstractTest {
111126
.setMaxSize(maxRegionSize)
112127
.setPersistenceEnabled(true)
113128
.setMetricsEnabled(true)
114-
.setName("dflt-plc"))
115-
.setDataRegionConfigurations(new DataRegionConfiguration()
116-
.setMaxSize(maxRegionSize)
117-
.setPersistenceEnabled(false)
118-
.setMetricsEnabled(true)
119-
.setName(NO_PERSISTENCE))
129+
.setName(PERSISTENCE_REGION_1))
130+
.setDataRegionConfigurations(
131+
new DataRegionConfiguration()
132+
.setMaxSize(maxRegionSize)
133+
.setPersistenceEnabled(true)
134+
.setMetricsEnabled(true)
135+
.setName(PERSISTENCE_REGION_2),
136+
new DataRegionConfiguration()
137+
.setMaxSize(maxRegionSize)
138+
.setPersistenceEnabled(false)
139+
.setMetricsEnabled(true)
140+
.setName(NO_PERSISTENCE))
120141
.setWalMode(WALMode.LOG_ONLY)
121142
.setMetricsEnabled(true);
122143

@@ -126,6 +147,7 @@ public class IgniteDataStorageMetricsSelfTest extends GridCommonAbstractTest {
126147

127148
cfg.setCacheConfiguration(
128149
cacheConfiguration(GROUP1, "cache", PARTITIONED, ATOMIC, 1, null),
150+
cacheConfiguration(GROUP2, "cache2", PARTITIONED, ATOMIC, 1, PERSISTENCE_REGION_2),
129151
cacheConfiguration(null, "cache-np", PARTITIONED, ATOMIC, 1, NO_PERSISTENCE));
130152

131153
cfg.setGridLogger(listeningLog);
@@ -186,16 +208,19 @@ public void testPersistenceMetrics() throws Exception {
186208

187209
try {
188210
IgniteCache<Object, Object> cache = ig.cache("cache");
211+
IgniteCache<Object, Object> cache2 = ig.cache("cache2");
189212

190-
for (int i = 0; i < 10; i++)
213+
for (int i = 0; i < 10; i++) {
191214
cache.put(i, new Person("first-" + i, "last-" + i));
215+
cache2.put(i, new Person("first-" + i, "last-" + i));
216+
}
192217

193218
IgniteCache<Object, Object> cacheNp = ig.cache("cache-np");
194219

195220
for (int i = 0; i < 10; i++)
196221
cacheNp.put(i, new Person("first-" + i, "last-" + i));
197222

198-
DataRegionMetrics memMetrics = ig.dataRegionMetrics("dflt-plc");
223+
DataRegionMetrics memMetrics = ig.dataRegionMetrics(PERSISTENCE_REGION_1);
199224

200225
assertNotNull(memMetrics);
201226
assertTrue(memMetrics.getDirtyPages() > 0);
@@ -219,6 +244,19 @@ public void testPersistenceMetrics() throws Exception {
219244
pMetrics.getLastCheckpointDataPagesNumber() != 0;
220245
}
221246
}, 10_000));
247+
248+
Collection<MetricRegistry> grpRegs = F.viewReadOnly(ig.context().cache().cacheGroups(),
249+
ctx -> ig.context().metric().registry(metricName(CACHE_GROUP_METRICS_PREFIX, ctx.cacheOrGroupName())));
250+
251+
ToLongFunction<String> sumByGroups = metric -> grpRegs.stream()
252+
.map(grpReg -> grpReg.<LongMetric>findMetric(metric).value()).mapToLong(v -> v)
253+
.sum();
254+
255+
long storageSize = dsMetricRegistry(ig).<LongMetric>findMetric("StorageSize").value();
256+
long sparseStorageSize = dsMetricRegistry(ig).<LongMetric>findMetric("SparseStorageSize").value();
257+
258+
assertEquals(sumByGroups.applyAsLong("StorageSize"), storageSize);
259+
assertEquals(sumByGroups.applyAsLong("SparseStorageSize"), sparseStorageSize);
222260
}
223261
finally {
224262
stopAllGrids();

0 commit comments

Comments
 (0)