This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git
The following commit(s) were added to refs/heads/master by this push:
new 619093b091f IGNITE-29028 Implement multi-page in-place row update -
Fixes #13560.
619093b091f is described below
commit 619093b091fb71475e78ea7ebc55d7ad91a562e9
Author: Aleksey Plekhanov <[email protected]>
AuthorDate: Thu Oct 22 21:11:59 2026 +0300
IGNITE-29028 Implement multi-page in-place row update - Fixes #13560.
Signed-off-by: Aleksey Plekhanov <[email protected]>
---
.../jmh/cache/JmhCacheInPlaceUpdateBenchmark.java | 160 +++++
.../wal/record/delta/DataPageUpdateRecord.java | 2 +-
.../processors/cache/GridCacheMapEntry.java | 8 +-
.../cache/IgniteCacheOffheapManager.java | 28 +-
.../cache/IgniteCacheOffheapManagerImpl.java | 168 +++---
.../processors/cache/persistence/CacheDataRow.java | 5 +
.../cache/persistence/CacheDataRowAdapter.java | 157 +++--
.../cache/persistence/DataRowCacheAware.java | 68 ---
.../GridCacheDatabaseSharedManager.java | 3 +-
.../cache/persistence/GridCacheOffheapManager.java | 28 +-
.../processors/cache/persistence/RowStore.java | 11 +-
.../persistence/freelist/AbstractFreeList.java | 258 +++++++-
.../cache/persistence/freelist/FreeList.java | 11 +-
.../persistence/tree/io/AbstractDataPageIO.java | 105 +++-
.../cache/persistence/tree/io/DataPageIO.java | 12 +-
.../persistence/tree/io/DataPageUpdateResult.java | 72 +++
.../internal/processors/cache/tree/DataRow.java | 15 +-
.../db/IgnitePdsCheckpointRecoveryTest.java | 76 ++-
.../processors/database/CacheFreeListSelfTest.java | 5 +
.../database/MultiPageInPlaceUpdateTest.java | 649 +++++++++++++++++++++
.../ignite/testsuites/IgniteBasicTestSuite2.java | 2 +
.../org/apache/ignite/util/TestStorageUtils.java | 3 +-
.../processors/query/h2/opt/H2CacheRow.java | 5 +
23 files changed, 1568 insertions(+), 283 deletions(-)
diff --git
a/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/cache/JmhCacheInPlaceUpdateBenchmark.java
b/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/cache/JmhCacheInPlaceUpdateBenchmark.java
new file mode 100644
index 00000000000..f927f4d9fa9
--- /dev/null
+++
b/modules/benchmarks/src/main/java/org/apache/ignite/internal/benchmarks/jmh/cache/JmhCacheInPlaceUpdateBenchmark.java
@@ -0,0 +1,160 @@
+/*
+ * 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.benchmarks.jmh.cache;
+
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.TimeUnit;
+import javax.cache.expiry.CreatedExpiryPolicy;
+import javax.cache.expiry.Duration;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.Ignition;
+import org.apache.ignite.cluster.ClusterState;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.DataRegionConfiguration;
+import org.apache.ignite.configuration.DataStorageConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.benchmarks.jmh.runner.JmhIdeBenchmarkRunner;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Level;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Param;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.Setup;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.TearDown;
+import org.openjdk.jmh.annotations.Warmup;
+
+/**
+ * Compare put with in-place update and without in-place update.
+ */
+@State(Scope.Benchmark)
+@BenchmarkMode(Mode.Throughput)
+@OutputTimeUnit(TimeUnit.MILLISECONDS)
+@Warmup(iterations = 3, time = 3)
+@Measurement(iterations = 3, time = 10)
+public class JmhCacheInPlaceUpdateBenchmark {
+ /** Items count. */
+ private static final int CNT = 100;
+
+ /** Entry size. */
+ private static final int ENTRY_SIZE = 100 * 1024;
+
+ /** Ignite. */
+ private Ignite ignite;
+
+ /** Cache with in-place updates. */
+ private IgniteCache<Integer, byte[]> cache0;
+
+ /** Cache without in-place updates. */
+ private IgniteCache<Integer, byte[]> cache1;
+
+ /** Entry payloads for cache 0. */
+ private final byte[][] payloads0 = new byte[CNT][ENTRY_SIZE];
+
+ /** Entry payloads for cache 1. */
+ private final byte[][] payloads1 = new byte[CNT][ENTRY_SIZE];
+
+ /** Persistence enabled. */
+ @Param({"FALSE", "TRUE"})
+ private String persistence;
+
+ /** */
+ @Benchmark
+ public void putWithInPlaceUpdate() {
+ int key = ThreadLocalRandom.current().nextInt(CNT);
+
+ changeAndPutPayload(cache0, key, payloads0[key]);
+ }
+
+ /** */
+ @Benchmark
+ public void putWithoutInPlaceUpdate() {
+ int key = ThreadLocalRandom.current().nextInt(CNT);
+
+ changeAndPutPayload(cache1, key, payloads1[key]);
+ }
+
+ /** */
+ private void changeAndPutPayload(IgniteCache<Integer, byte[]> cache, int
key, byte[] payload) {
+ // Change 1 byte.
+ payload[ThreadLocalRandom.current().nextInt(payload.length)] =
(byte)ThreadLocalRandom.current().nextInt(256);
+
+ cache.put(key, payload);
+ }
+
+ /**
+ * Initiate Ignite and caches.
+ */
+ @Setup(Level.Trial)
+ public void setup() {
+ ignite = Ignition.start(new
IgniteConfiguration().setIgniteInstanceName("test")
+ .setDataStorageConfiguration(new
DataStorageConfiguration().setDefaultDataRegionConfiguration(
+ new
DataRegionConfiguration().setPersistenceEnabled(Boolean.parseBoolean(persistence))
+ ))
+ );
+
+ ignite.cluster().state(ClusterState.ACTIVE);
+
+ cache0 = ignite.getOrCreateCache(new CacheConfiguration<>("CACHE0"));
+
+ // Enable expiration for second cache, but set eager ttl to false,
this will disable in-place updates,
+ // but without performance overhead to maintain expiration.
+ cache1 = ignite.getOrCreateCache(
+ new CacheConfiguration<Integer, byte[]>("CACHE1")
+ .setEagerTtl(false)
+
.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(Duration.ONE_DAY))
+ );
+ }
+
+ /**
+ * Clear caches.
+ */
+ @Setup(Level.Iteration)
+ public void setupIteration() {
+ for (int i = 0; i < CNT; i++) {
+ ThreadLocalRandom.current().nextBytes(payloads0[i]);
+ ThreadLocalRandom.current().nextBytes(payloads1[i]);
+ cache0.put(i, payloads0[i]);
+ cache1.put(i, payloads1[i]);
+ }
+ }
+
+ /**
+ * Stop Ignite instance.
+ */
+ @TearDown
+ public void tearDown() {
+ ignite.close();
+ }
+
+ /**
+ * Run benchmarks.
+ *
+ * @param args Args.
+ * @throws Exception Exception.
+ */
+ public static void main(String[] args) throws Exception {
+ JmhIdeBenchmarkRunner.create()
+ .benchmarks(JmhCacheInPlaceUpdateBenchmark.class.getSimpleName())
+ .run();
+ }
+}
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/pagemem/wal/record/delta/DataPageUpdateRecord.java
b/modules/core/src/main/java/org/apache/ignite/internal/pagemem/wal/record/delta/DataPageUpdateRecord.java
index 6f5d8fd8640..3a38455e11e 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/pagemem/wal/record/delta/DataPageUpdateRecord.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/pagemem/wal/record/delta/DataPageUpdateRecord.java
@@ -71,7 +71,7 @@ public class DataPageUpdateRecord extends PageDeltaRecord {
AbstractDataPageIO io = PageIO.getPageIO(pageAddr);
- io.updateRow(pageAddr, itemId, pageMem.realPageSize(groupId()),
payload, null, 0);
+ io.updateRow(pageAddr, itemId, pageMem.realPageSize(groupId()),
payload);
}
/** {@inheritDoc} */
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheMapEntry.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheMapEntry.java
index 6ee75f06d90..816a66f2505 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheMapEntry.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheMapEntry.java
@@ -4297,7 +4297,7 @@ public abstract class GridCacheMapEntry extends
GridMetadataAwareAdapter impleme
if (val != null) {
if (newRow == null) {
- newRow =
entry.cctx.offheap().dataStore(entry.localPartition()).createRow(
+ newRow =
entry.cctx.offheap().dataStore(entry.localPartition()).updateRow(
entry.cctx,
entry.key,
val,
@@ -4819,7 +4819,7 @@ public abstract class GridCacheMapEntry extends
GridMetadataAwareAdapter impleme
}
if (needUpdate) {
- newRow = entry.localPartition().dataStore().createRow(
+ newRow = entry.localPartition().dataStore().updateRow(
entry.cctx,
entry.key,
storeLoadedVal,
@@ -4983,7 +4983,7 @@ public abstract class GridCacheMapEntry extends
GridMetadataAwareAdapter impleme
entry.logUpdate(op, updated, newVer, newExpireTime, updateCntr0,
primary);
if (!entry.isNear()) {
- newRow = entry.localPartition().dataStore().createRow(
+ newRow = entry.localPartition().dataStore().updateRow(
entry.cctx,
entry.key,
updated,
@@ -4992,7 +4992,7 @@ public abstract class GridCacheMapEntry extends
GridMetadataAwareAdapter impleme
oldRow);
treeOp = oldRow != null && oldRow.link() == newRow.link() ?
- IgniteTree.OperationType.NOOP :
IgniteTree.OperationType.PUT;
+ IgniteTree.OperationType.IN_PLACE :
IgniteTree.OperationType.PUT;
}
else
treeOp = IgniteTree.OperationType.PUT;
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheOffheapManager.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheOffheapManager.java
index 3247e718ad6..54884f20bb7 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheOffheapManager.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheOffheapManager.java
@@ -26,13 +26,13 @@ import
org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
import
org.apache.ignite.internal.processors.cache.distributed.dht.preloader.IgniteDhtDemandedPartitionsMap;
import
org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition;
import org.apache.ignite.internal.processors.cache.persistence.CacheDataRow;
-import
org.apache.ignite.internal.processors.cache.persistence.DataRowCacheAware;
import org.apache.ignite.internal.processors.cache.persistence.RootPage;
import org.apache.ignite.internal.processors.cache.persistence.RowStore;
import
org.apache.ignite.internal.processors.cache.persistence.freelist.SimpleDataRow;
import
org.apache.ignite.internal.processors.cache.persistence.partstorage.PartitionMetaStorage;
import
org.apache.ignite.internal.processors.cache.persistence.tree.reuse.ReuseList;
import org.apache.ignite.internal.processors.cache.tree.CacheDataTree;
+import org.apache.ignite.internal.processors.cache.tree.DataRow;
import org.apache.ignite.internal.processors.cache.tree.PendingEntriesTree;
import org.apache.ignite.internal.processors.cache.version.GridCacheVersion;
import org.apache.ignite.internal.processors.query.GridQueryRowCacheCleaner;
@@ -194,18 +194,16 @@ public interface IgniteCacheOffheapManager {
* @param val Value.
* @param ver Version.
* @param expireTime Expire time.
- * @param oldRow Old row if available.
* @param part Partition.
* @throws IgniteCheckedException If failed.
*/
public void update(
- GridCacheContext cctx,
+ GridCacheContext<?, ?> cctx,
KeyCacheObject key,
CacheObject val,
GridCacheVersion ver,
long expireTime,
- GridDhtLocalPartition part,
- @Nullable CacheDataRow oldRow
+ GridDhtLocalPartition part
) throws IgniteCheckedException;
/**
@@ -542,13 +540,14 @@ public interface IgniteCacheOffheapManager {
* @return New row.
* @throws IgniteCheckedException If failed.
*/
- CacheDataRow createRow(
- GridCacheContext cctx,
+ CacheDataRow updateRow(
+ GridCacheContext<?, ?> cctx,
KeyCacheObject key,
CacheObject val,
GridCacheVersion ver,
long expireTime,
- @Nullable CacheDataRow oldRow) throws IgniteCheckedException;
+ @Nullable CacheDataRow oldRow
+ ) throws IgniteCheckedException;
/**
* Insert rows into page memory.
@@ -557,8 +556,10 @@ public interface IgniteCacheOffheapManager {
* @param initPred Applied to all rows. Each row that not matches the
predicate is removed.
* @throws IgniteCheckedException If failed.
*/
- public void insertRows(Collection<DataRowCacheAware> rows,
- IgnitePredicateX<CacheDataRow> initPred) throws
IgniteCheckedException;
+ public void insertRows(
+ Collection<DataRow> rows,
+ IgnitePredicateX<CacheDataRow> initPred
+ ) throws IgniteCheckedException;
/**
* @param cctx Cache context.
@@ -566,16 +567,15 @@ public interface IgniteCacheOffheapManager {
* @param val Value.
* @param ver Version.
* @param expireTime Expire time.
- * @param oldRow Old row if available.
* @throws IgniteCheckedException If failed.
*/
void update(
- GridCacheContext cctx,
+ GridCacheContext<?, ?> cctx,
KeyCacheObject key,
CacheObject val,
GridCacheVersion ver,
- long expireTime,
- @Nullable CacheDataRow oldRow) throws IgniteCheckedException;
+ long expireTime
+ ) throws IgniteCheckedException;
/**
* @param cctx Cache context.
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheOffheapManagerImpl.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheOffheapManagerImpl.java
index c97aefd6821..78d8b651359 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheOffheapManagerImpl.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheOffheapManagerImpl.java
@@ -54,7 +54,6 @@ import
org.apache.ignite.internal.processors.cache.distributed.dht.topology.Grid
import org.apache.ignite.internal.processors.cache.persistence.CacheDataRow;
import
org.apache.ignite.internal.processors.cache.persistence.CacheDataRowAdapter;
import org.apache.ignite.internal.processors.cache.persistence.CacheSearchRow;
-import
org.apache.ignite.internal.processors.cache.persistence.DataRowCacheAware;
import org.apache.ignite.internal.processors.cache.persistence.RootPage;
import org.apache.ignite.internal.processors.cache.persistence.RowStore;
import
org.apache.ignite.internal.processors.cache.persistence.freelist.SimpleDataRow;
@@ -404,17 +403,16 @@ public class IgniteCacheOffheapManagerImpl implements
IgniteCacheOffheapManager
/** {@inheritDoc} */
@Override public void update(
- GridCacheContext cctx,
+ GridCacheContext<?, ?> cctx,
KeyCacheObject key,
CacheObject val,
GridCacheVersion ver,
long expireTime,
- GridDhtLocalPartition part,
- @Nullable CacheDataRow oldRow
+ GridDhtLocalPartition part
) throws IgniteCheckedException {
assert expireTime >= 0;
- dataStore(part).update(cctx, key, val, ver, expireTime, oldRow);
+ dataStore(part).update(cctx, key, val, ver, expireTime);
}
/** {@inheritDoc} */
@@ -988,7 +986,7 @@ public class IgniteCacheOffheapManagerImpl implements
IgniteCacheOffheapManager
IgnitePredicateX<CacheDataRow> initPred) throws IgniteCheckedException
{
CacheDataStore dataStore = dataStore(part);
- List<DataRowCacheAware> batch = new
ArrayList<>(PRELOAD_SIZE_UNDER_CHECKPOINT_LOCK);
+ List<DataRow> batch = new
ArrayList<>(PRELOAD_SIZE_UNDER_CHECKPOINT_LOCK);
while (infos.hasNext()) {
GridCacheEntryInfo info = infos.next();
@@ -996,7 +994,7 @@ public class IgniteCacheOffheapManagerImpl implements
IgniteCacheOffheapManager
assert info.ttl() == TTL_ETERNAL : info.ttl();
try {
- batch.add(new DataRowCacheAware(info.key(),
+ batch.add(new DataRow(info.key(),
info.value(),
info.version(),
part.id(),
@@ -1236,9 +1234,6 @@ public class IgniteCacheOffheapManagerImpl implements
IgniteCacheOffheapManager
IgniteSystemProperties.IGNITE_FAIL_NODE_ON_UNRECOVERABLE_PARTITION_INCONSISTENCY
);
- /** */
- private final int updateValSizeThreshold;
-
/** */
private volatile GridQueryRowCacheCleaner rowCacheCleaner;
@@ -1272,8 +1267,6 @@ public class IgniteCacheOffheapManagerImpl implements
IgniteCacheOffheapManager
pCntr =
grp.shared().logger(PartitionUpdateCounterDebugWrapper.class).isDebugEnabled() ?
new PartitionUpdateCounterDebugWrapper(partId, delegate) : new
PartitionUpdateCounterErrorWrapper(partId, delegate);
- updateValSizeThreshold = grp.shared().database().pageSize() / 2;
-
if (cleaner == null)
rowStore.setRowCacheCleaner(() -> rowCacheCleaner);
else
@@ -1437,28 +1430,73 @@ public class IgniteCacheOffheapManagerImpl implements
IgniteCacheOffheapManager
}
/**
+ * Checks if in-place row update is possible.
+ *
* @param cctx Cache context.
* @param oldRow Old row.
* @param dataRow New row.
* @return {@code True} if it is possible to update old row data.
* @throws IgniteCheckedException If failed.
*/
- private boolean canUpdateOldRow(GridCacheContext cctx, @Nullable
CacheDataRow oldRow, DataRow dataRow)
- throws IgniteCheckedException {
- if (oldRow == null || cctx.queries().enabled())
+ private boolean canUpdateOldRow(
+ GridCacheContext<?, ?> cctx,
+ @Nullable CacheDataRow oldRow,
+ DataRow dataRow
+ ) throws IgniteCheckedException {
+ if (oldRow == null)
return false;
+ // In-place update is not possible when queries (indexes) are
enabled. Index entries reference the row by
+ // link and are built from the row fields, so after the in-place
update, but before the index update,
+ // index entries would point to the row with fields that may not
match the index. Without in-place update
+ // there is no such window:
+ //
+ // 1. The new row is inserted to the row store first.
+ // 2. Then the index entries are updated:
+ // 2a. The new entry, linked to the new row is added.
+ // 2b. The old one, linked to the old row is removed under the
index leaf page lock.
+ // 3. Only after that the old row is removed from the row store.
+ //
+ // The index reader holds the index leaf page read lock while it
reads the row by link, and the updater has
+ // to acquire the write lock on the same leaf page to remove the
old entry before it removes the old row,
+ // so the reader always reads the row consistent with the index
entry it was found by, and the row
+ // can't be removed under the reader.
+ if (cctx.queries().enabled())
+ return false;
+
+ // Pending tree entries reference the row by link and store its
expire time. In-place update keeps the
+ // link and doesn't touch the pending tree, so it's possible only
when the expire time is not changed,
+ // otherwise the pending tree entry would become inconsistent with
the row: the entry would be processed
+ // by the TTL cleanup worker at the old expire time, while the row
has the new one. Without in-place update
+ // the pending tree is kept consistent as follows:
+ //
+ // 1. The new row is inserted to the row store.
+ // 2. Then the pending tree entries are updated:
+ // 2a. The old link is removed from the pending tree (under
the pending tree leaf page lock)
+ // 2b. The new one is added,
+ // 3. Only after that the old row is removed from the row store.
+ //
+ // The TTL cleanup worker holds the pending tree leaf page write
lock while it reads the row by link,
+ // and the updater has to acquire the write lock on the same leaf
page to remove the old link before
+ // it removes the old row, so the worker always reads the row
consistent with the pending tree entry,
+ // and the row can't be removed under the worker.
if (oldRow.expireTime() != dataRow.expireTime())
return false;
int oldLen = oldRow.size();
- // Use grp.sharedGroup() flag since it is possible cacheId is not
yet set here.
- if (!grp.storeCacheIdInDataPage() && grp.sharedGroup() &&
oldRow.cacheId() != CU.UNDEFINED_CACHE_ID)
- oldLen -= 4;
-
- if (oldLen > updateValSizeThreshold)
- return false;
+ // Paths that read the row by link hold different locks:
+ // *KV API* - the data tree leaf page lock.
+ // *TTL cleanup* - the pending tree leaf page lock.
+ // *page eviction* - the read lock of the evicted data page.
+ // *indexes* - the index tree leaf page lock.
+ // The only lock they have in common is the lock on the data row
pages, which are read one by one.
+ // In-place update is performed by the data tree invoke closure
under the entry lock, but without the
+ // data tree leaf page lock. Atomicity of read / in-place update
of a single-page row is provided by
+ // the lock on the single data row page. Atomicity of reads
concurrent with the multi-page in-place update
+ // is provided by lock coupling on the data row pages (see
AbstractFreeList#updateFragmentedRow
+ // and CacheDataRowAdapter#doInitFromLink), which doesn't allow a
reader to overtake the updater and
+ // vice versa, so all readers are protected regardless of the
locks they hold.
int newLen = dataRow.size();
@@ -1507,17 +1545,13 @@ public class IgniteCacheOffheapManagerImpl implements
IgniteCacheOffheapManager
case PUT: {
assert c.newRow() != null : c;
- CacheDataRow oldRow = c.oldRow();
-
- finishUpdate(cctx, c.newRow(), oldRow,
c.oldRowExpiredFlag());
+ finishUpdate(cctx, c.newRow(), c.oldRow(),
c.oldRowExpiredFlag());
break;
}
case REMOVE: {
- CacheDataRow oldRow = c.oldRow();
-
- finishRemove(cctx, row.key(), oldRow);
+ finishRemove(cctx, row.key(), c.oldRow());
break;
}
@@ -1532,19 +1566,23 @@ public class IgniteCacheOffheapManagerImpl implements
IgniteCacheOffheapManager
}
/** {@inheritDoc} */
- @Override public CacheDataRow createRow(
- GridCacheContext cctx,
+ @Override public CacheDataRow updateRow(
+ GridCacheContext<?, ?> cctx,
KeyCacheObject key,
CacheObject val,
GridCacheVersion ver,
long expireTime,
- @Nullable CacheDataRow oldRow) throws IgniteCheckedException {
+ @Nullable CacheDataRow oldRow
+ ) throws IgniteCheckedException {
int cacheId = grp.storeCacheIdInDataPage() ? cctx.cacheId() :
CU.UNDEFINED_CACHE_ID;
DataRow dataRow = makeDataRow(key, val, ver, expireTime, cacheId);
- if (canUpdateOldRow(cctx, oldRow, dataRow) &&
rowStore.updateRow(oldRow.link(), dataRow, grp.statisticsHolderData()))
+ if (canUpdateOldRow(cctx, oldRow, dataRow)) {
+ rowStore.updateRow(oldRow, dataRow,
grp.statisticsHolderData());
+
dataRow.link(oldRow.link());
+ }
else {
CacheObjectContext coCtx = cctx.cacheObjectContext();
@@ -1563,19 +1601,17 @@ public class IgniteCacheOffheapManagerImpl implements
IgniteCacheOffheapManager
}
/** {@inheritDoc} */
- @Override public void insertRows(Collection<DataRowCacheAware> rows,
- IgnitePredicateX<CacheDataRow> initPred) throws
IgniteCheckedException {
+ @Override public void insertRows(
+ Collection<DataRow> rows,
+ IgnitePredicateX<CacheDataRow> initPred
+ ) throws IgniteCheckedException {
if (!busyLock.enterBusy())
throw operationCancelledException();
try {
rowStore.addRows(F.view(rows, row -> row.value() != null),
grp.statisticsHolderData());
- boolean cacheIdAwareGrp = grp.sharedGroup() ||
grp.storeCacheIdInDataPage();
-
- for (DataRowCacheAware row : rows) {
- row.storeCacheId(cacheIdAwareGrp);
-
+ for (DataRow row : rows) {
if (!initPred.applyx(row) && row.value() != null)
rowStore.removeRow(row.link(),
grp.statisticsHolderData());
}
@@ -1598,27 +1634,25 @@ public class IgniteCacheOffheapManagerImpl implements
IgniteCacheOffheapManager
if (key.partition() == -1)
key.partition(partId);
- return new DataRow(key, val, ver, partId, expireTime, cacheId);
+ assert val != null;
+
+ return new DataRow(key, val, ver, partId, expireTime, cacheId,
grp.storeCacheIdInDataPage());
}
/** {@inheritDoc} */
- @Override public void update(GridCacheContext cctx,
+ @Override public void update(
+ GridCacheContext<?, ?> cctx,
KeyCacheObject key,
CacheObject val,
GridCacheVersion ver,
- long expireTime,
- @Nullable CacheDataRow oldRow
+ long expireTime
) throws IgniteCheckedException {
- assert oldRow == null || oldRow.link() != 0L : oldRow;
-
if (!busyLock.enterBusy())
throw operationCancelledException();
try {
int cacheId = grp.storeCacheIdInDataPage() ? cctx.cacheId() :
CU.UNDEFINED_CACHE_ID;
- assert oldRow == null || oldRow.cacheId() == cacheId : oldRow;
-
DataRow dataRow = makeDataRow(key, val, ver, expireTime,
cacheId);
CacheObjectContext coCtx = cctx.cacheObjectContext();
@@ -1627,31 +1661,16 @@ public class IgniteCacheOffheapManagerImpl implements
IgniteCacheOffheapManager
key.valueBytes(coCtx);
val.valueBytes(coCtx);
- CacheDataRow old;
-
assert cctx.shared().database().checkpointLockIsHeldByThread();
- if (canUpdateOldRow(cctx, oldRow, dataRow) &&
rowStore.updateRow(oldRow.link(), dataRow, grp.statisticsHolderData())) {
- old = oldRow;
-
- dataRow.link(oldRow.link());
- }
- else {
- rowStore.addRow(dataRow, grp.statisticsHolderData());
+ rowStore.addRow(dataRow, grp.statisticsHolderData());
- assert dataRow.link() != 0 : dataRow;
+ assert dataRow.link() != 0 : dataRow;
- if (grp.sharedGroup() && dataRow.cacheId() ==
CU.UNDEFINED_CACHE_ID)
- dataRow.cacheId(cctx.cacheId());
+ if (grp.sharedGroup() && dataRow.cacheId() ==
CU.UNDEFINED_CACHE_ID)
+ dataRow.cacheId(cctx.cacheId());
- if (oldRow != null) {
- old = oldRow;
-
- dataTree.putx(dataRow);
- }
- else
- old = dataTree.put(dataRow);
- }
+ CacheDataRow old = dataTree.put(dataRow);
finishUpdate(cctx, dataRow, old);
}
@@ -1666,8 +1685,11 @@ public class IgniteCacheOffheapManagerImpl implements
IgniteCacheOffheapManager
* @param oldRow Old row if available.
* @throws IgniteCheckedException If failed.
*/
- private void finishUpdate(GridCacheContext cctx, CacheDataRow newRow,
@Nullable CacheDataRow oldRow)
- throws IgniteCheckedException {
+ private void finishUpdate(
+ GridCacheContext<?, ?> cctx,
+ CacheDataRow newRow,
+ @Nullable CacheDataRow oldRow
+ ) throws IgniteCheckedException {
finishUpdate(cctx, newRow, oldRow, false);
}
@@ -1678,12 +1700,16 @@ public class IgniteCacheOffheapManagerImpl implements
IgniteCacheOffheapManager
* @param oldRowExpired Old row expiration flag
* @throws IgniteCheckedException If failed.
*/
- private void finishUpdate(GridCacheContext cctx, CacheDataRow newRow,
@Nullable CacheDataRow oldRow, boolean oldRowExpired)
- throws IgniteCheckedException {
+ private void finishUpdate(
+ GridCacheContext<?, ?> cctx,
+ CacheDataRow newRow,
+ @Nullable CacheDataRow oldRow,
+ boolean oldRowExpired
+ ) throws IgniteCheckedException {
if (oldRow == null && !oldRowExpired)
incrementSize(cctx.cacheId());
- GridCacheQueryManager qryMgr = cctx.queries();
+ GridCacheQueryManager<?, ?> qryMgr = cctx.queries();
if (qryMgr.enabled())
qryMgr.store(newRow, oldRow, true);
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/CacheDataRow.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/CacheDataRow.java
index 647e68dbf51..970aa992c82 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/CacheDataRow.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/CacheDataRow.java
@@ -58,6 +58,11 @@ public interface CacheDataRow extends CacheSearchRow,
Storable {
*/
public void key(KeyCacheObject key);
+ /**
+ * @return {@code True} if store cache ID.
+ */
+ public boolean storeCacheId();
+
/** {@inheritDoc} */
@Override public default IOVersions<? extends AbstractDataPageIO>
ioVersions() {
return DataPageIO.VERSIONS;
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/CacheDataRowAdapter.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/CacheDataRowAdapter.java
index 810423838a3..66059d557c9 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/CacheDataRowAdapter.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/CacheDataRowAdapter.java
@@ -58,6 +58,15 @@ import static
org.apache.ignite.internal.util.GridUnsafe.wrapPointer;
* Cache data row adapter.
*/
public class CacheDataRowAdapter implements CacheDataRow {
+ /** Version is ready flag. */
+ protected static final byte FLAG_VER_READY = 0x01;
+
+ /** Store cache ID flag. */
+ protected static final byte FLAG_STORE_CACHE_ID = 0x02;
+
+ /** Allow null as value. */
+ protected static final byte FLAG_ALLOW_NULL_VAL = 0x04;
+
/** */
@GridToStringExclude
protected long link;
@@ -68,7 +77,7 @@ public class CacheDataRowAdapter implements CacheDataRow {
/** */
@GridToStringInclude
- protected CacheObject val;
+ @Nullable protected CacheObject val;
/** */
@GridToStringInclude
@@ -78,8 +87,8 @@ public class CacheDataRowAdapter implements CacheDataRow {
@GridToStringInclude
protected GridCacheVersion ver;
- /** Whether version is ready. */
- protected boolean verReady;
+ /** */
+ protected byte flags;
/** */
@GridToStringInclude
@@ -105,7 +114,7 @@ public class CacheDataRowAdapter implements CacheDataRow {
this.ver = ver;
this.expireTime = expireTime;
- verReady = true;
+ flags = FLAG_VER_READY;
}
/**
@@ -158,6 +167,9 @@ public class CacheDataRowAdapter implements CacheDataRow {
int grpId = grp != null ? grp.groupId() : 0;
IoStatisticsHolder statHolder = grp != null ?
grp.statisticsHolderData() : IoStatisticsHolderNoOp.INSTANCE;
+ if (readCacheId)
+ flags |= FLAG_STORE_CACHE_ID;
+
doInitFromLink(link, sharedCtx, coctx, pageMem, grpId, statHolder,
readCacheId, rowData, null, skipVer);
}
@@ -187,6 +199,9 @@ public class CacheDataRowAdapter implements CacheDataRow {
ByteBuffer buff = pageBuff;
IncompleteObject<?> incomplete = null;
+ if (readCacheId)
+ flags |= FLAG_STORE_CACHE_ID;
+
for (;;) {
long pageAddr = GridUnsafe.bufferAddress(buff);
@@ -240,6 +255,9 @@ public class CacheDataRowAdapter implements CacheDataRow {
int grpId = grp != null ? grp.groupId() : 0;
IoStatisticsHolder statHolder = grp != null ?
grp.statisticsHolderData() : IoStatisticsHolderNoOp.INSTANCE;
+ if (readCacheId)
+ flags |= FLAG_STORE_CACHE_ID;
+
IncompleteObject<?> incomplete = readIncomplete(null, sharedCtx,
coctx, pageMem.pageSize(),
pageMem.realPageSize(grpId), pageAddr, itemId, io, rowData,
readCacheId, skipVer);
@@ -292,61 +310,105 @@ public class CacheDataRowAdapter implements CacheDataRow
{
assert key == null : "key";
long nextLink = link;
+ long pageId = pageId(link);
+ long page = 0L;
+ long pageAddr = 0L;
- do {
- final long pageId = pageId(nextLink);
-
+ try {
try {
- final long page = pageMem.acquirePage(grpId, pageId,
statHolder);
+ page = pageMem.acquirePage(grpId, pageId, statHolder);
+ pageAddr = readLockFragmentPage(pageMem, grpId, pageId, page,
link);
- try {
- long pageAddr = pageMem.readLock(grpId, pageId, page); //
Non-empty data page must not be recycled.
+ while (true) {
+ DataPageIO io = DataPageIO.VERSIONS.forPage(pageAddr);
- assert pageAddr != 0L : "Cannot lock page [link=" +
U.hexLong(nextLink) +
- ", tag=" + PageIdUtils.tag(pageId) + ",
pageLockState=[" + pageMem.pageLockStateInfo(page) + "]]";
+ int itemId = itemId(nextLink);
- try {
- DataPageIO io = DataPageIO.VERSIONS.forPage(pageAddr);
+ incomplete = readIncomplete(incomplete, sharedCtx, coctx,
pageMem.pageSize(),
+ pageMem.realPageSize(grpId), pageAddr, itemId, io,
rowData, readCacheId, skipVer);
- int itemId = itemId(nextLink);
+ if (incomplete == null || (rowData == KEY_ONLY && key !=
null))
+ return;
- incomplete = readIncomplete(incomplete, sharedCtx,
coctx, pageMem.pageSize(),
- pageMem.realPageSize(grpId), pageAddr, itemId, io,
rowData, readCacheId, skipVer);
+ nextLink = incomplete.getNextLink();
- if (incomplete == null || (rowData == KEY_ONLY && key
!= null))
- return;
+ if (nextLink == 0)
+ break;
- nextLink = incomplete.getNextLink();
+ // Lock coupling: acquire the read lock on the next
fragment page before releasing the current one.
+ // In-place update of the fragmented row traverses
fragments in the same order with the same lock
+ // coupling (see AbstractFreeList#updateFragmentedRow), so
it can't overtake this read and the row
+ // is read either entirely old or entirely new.
+ long nextPageId = pageId(nextLink);
+ long nextPage = pageMem.acquirePage(grpId, nextPageId,
statHolder);
+ long nextPageAddr;
+
+ try {
+ nextPageAddr = readLockFragmentPage(pageMem, grpId,
nextPageId, nextPage, nextLink);
}
- finally {
+ catch (Throwable e) {
+ pageMem.releasePage(grpId, nextPageId, nextPage);
+
+ throw e;
+ }
+
+ try {
pageMem.readUnlock(grpId, pageId, page);
+ pageMem.releasePage(grpId, pageId, page);
+ }
+ finally {
+ pageId = nextPageId;
+ page = nextPage;
+ pageAddr = nextPageAddr;
}
- }
- finally {
- pageMem.releasePage(grpId, pageId, page);
}
}
- catch (RuntimeException | AssertionError e) {
- // Collect all pages from first link to pageId.
- long[] pageIds;
-
- try {
- pageIds = relatedPageIds(grpId, link, pageId, pageMem,
statHolder);
+ finally {
+ if (pageAddr != 0L)
+ pageMem.readUnlock(grpId, pageId, page);
- }
- catch (IgniteCheckedException e0) {
- // Ignore exception if failed to resolve related page ids.
- pageIds = new long[] {pageId};
- }
+ if (page != 0L)
+ pageMem.releasePage(grpId, pageId, page);
+ }
+ }
+ catch (RuntimeException | AssertionError e) {
+ // Collect all pages from first link to pageId.
+ long[] pageIds;
- throw new BPlusTreeRuntimeException(e, grpId, pageIds);
+ try {
+ pageIds = relatedPageIds(grpId, link, pageId, pageMem,
statHolder);
+ }
+ catch (IgniteCheckedException e0) {
+ // Ignore exception if failed to resolve related page ids.
+ pageIds = new long[] {pageId};
}
+
+ throw new BPlusTreeRuntimeException(e, grpId, pageIds);
}
- while (nextLink != 0);
assert isReady() : "ready";
}
+ /**
+ * Acquires the read lock on the fragment page of the row.
+ *
+ * @param pageMem Page memory.
+ * @param grpId Cache group ID.
+ * @param pageId Page ID.
+ * @param page Page pointer.
+ * @param link Fragment link (for diagnostics).
+ * @return Page address.
+ */
+ private static long readLockFragmentPage(PageMemory pageMem, int grpId,
long pageId, long page, long link) {
+ long pageAddr = pageMem.readLock(grpId, pageId, page);
+
+ // Non-empty data page must not be recycled.
+ assert pageAddr != 0L : "Cannot lock page [link=" + U.hexLong(link) +
+ ", tag=" + PageIdUtils.tag(pageId) + ", pageLockState=[" +
pageMem.pageLockStateInfo(page) + "]]";
+
+ return pageAddr;
+ }
+
/**
* @param incomplete Incomplete object.
* @param sharedCtx Cache shared context.
@@ -483,7 +545,7 @@ public class CacheDataRowAdapter implements CacheDataRow {
}
// Read version.
- if (!verReady) {
+ if ((flags & FLAG_VER_READY) == 0) {
incomplete = readIncompleteVersion(buf, incomplete, skipVer);
assert skipVer || ver != null || incomplete != null;
@@ -562,7 +624,7 @@ public class CacheDataRowAdapter implements CacheDataRow {
verLen = CacheVersionIO.size(ver, false);
}
- verReady = true;
+ flags |= FLAG_VER_READY;
off += verLen;
@@ -737,7 +799,7 @@ public class CacheDataRowAdapter implements CacheDataRow {
assert ver != null;
}
- verReady = true;
+ flags |= FLAG_VER_READY;
return null;
}
@@ -759,7 +821,7 @@ public class CacheDataRowAdapter implements CacheDataRow {
assert ver != null;
}
- verReady = true;
+ flags |= FLAG_VER_READY;
}
assert !buf.hasRemaining();
@@ -825,7 +887,7 @@ public class CacheDataRowAdapter implements CacheDataRow {
* @return {@code True} if entry is ready.
*/
public boolean isReady() {
- return verReady && val != null && key != null;
+ return ((flags & FLAG_VER_READY) != 0) && val != null && key != null;
}
/** {@inheritDoc} */
@@ -844,6 +906,11 @@ public class CacheDataRowAdapter implements CacheDataRow {
this.key = key;
}
+ /** {@inheritDoc} */
+ @Override public boolean storeCacheId() {
+ return (flags & FLAG_STORE_CACHE_ID) != 0;
+ }
+
/** {@inheritDoc} */
@Override public int cacheId() {
return cacheId;
@@ -851,14 +918,14 @@ public class CacheDataRowAdapter implements CacheDataRow {
/** {@inheritDoc} */
@Override public CacheObject value() {
- assert val != null : "Value is not ready: " + this;
+ assert val != null || (flags & FLAG_ALLOW_NULL_VAL) != 0 : "Value is
not ready: " + this;
return val;
}
/** {@inheritDoc} */
@Override public GridCacheVersion version() {
- assert verReady : "Version is not ready: " + this;
+ assert (flags & FLAG_VER_READY) != 0 : "Version is not ready: " + this;
return ver;
}
@@ -894,7 +961,7 @@ public class CacheDataRowAdapter implements CacheDataRow {
len += value().valueBytesLength(null) + CacheVersionIO.size(version(),
false) + 8;
- return len + (cacheId() != 0 ? 4 : 0);
+ return len + (storeCacheId() ? 4 : 0);
}
/**
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/DataRowCacheAware.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/DataRowCacheAware.java
deleted file mode 100644
index baee4013208..00000000000
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/DataRowCacheAware.java
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * 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.processors.cache.persistence;
-
-import org.apache.ignite.internal.processors.cache.CacheObject;
-import org.apache.ignite.internal.processors.cache.KeyCacheObject;
-import org.apache.ignite.internal.processors.cache.tree.DataRow;
-import org.apache.ignite.internal.processors.cache.version.GridCacheVersion;
-import org.apache.ignite.internal.util.typedef.internal.CU;
-import org.jetbrains.annotations.Nullable;
-
-/**
- * Data row implementation that can optionally hide the cache identifier and
can set {@code null} as value.<br> It is
- * used to simplify storing a data row into page memory, because in some cases
the cache identifier is not stored on the
- * data pages, but is required to link this data row in {@code BPlusTree}.
- */
-public class DataRowCacheAware extends DataRow {
- /** Flag indicates that cacheId should be stored in data page. */
- private boolean storeCacheId;
-
- /**
- * @param key Key.
- * @param val Value.
- * @param ver Version.
- * @param part Partition.
- * @param expireTime Expire time.
- * @param cacheId Cache ID.
- * @param storeCacheId Flag indicates that cacheId should be stored in
data page.
- */
- public DataRowCacheAware(KeyCacheObject key, @Nullable CacheObject val,
GridCacheVersion ver, int part,
- long expireTime, int cacheId, boolean storeCacheId) {
- super(key, val, ver, part, expireTime, cacheId);
-
- storeCacheId(storeCacheId);
- }
-
- /**
- * @param storeCacheId Flag indicates that cacheId should be stored in
data page.
- */
- public void storeCacheId(boolean storeCacheId) {
- this.storeCacheId = storeCacheId;
- }
-
- /** {@inheritDoc} */
- @Override public int cacheId() {
- return storeCacheId ? cacheId : CU.UNDEFINED_CACHE_ID;
- }
-
- /** {@inheritDoc} */
- @Override public @Nullable CacheObject value() {
- return val;
- }
-}
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/GridCacheDatabaseSharedManager.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/GridCacheDatabaseSharedManager.java
index 6ba634cecd0..84246064796 100755
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/GridCacheDatabaseSharedManager.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/GridCacheDatabaseSharedManager.java
@@ -2880,8 +2880,7 @@ public class GridCacheDatabaseSharedManager extends
IgniteCacheDatabaseSharedMan
dataEntry.value(),
dataEntry.writeVersion(),
dataEntry.expireTime(),
- locPart,
- null);
+ locPart);
return true;
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/GridCacheOffheapManager.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/GridCacheOffheapManager.java
index 5e3ff63d3dd..e9580a2cf3e 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/GridCacheOffheapManager.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/GridCacheOffheapManager.java
@@ -96,6 +96,7 @@ import
org.apache.ignite.internal.processors.cache.persistence.tree.util.PageHan
import org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer;
import org.apache.ignite.internal.processors.cache.tree.CacheDataRowStore;
import org.apache.ignite.internal.processors.cache.tree.CacheDataTree;
+import org.apache.ignite.internal.processors.cache.tree.DataRow;
import org.apache.ignite.internal.processors.cache.tree.PendingEntriesTree;
import org.apache.ignite.internal.processors.cache.tree.PendingRow;
import org.apache.ignite.internal.processors.cache.version.GridCacheVersion;
@@ -1546,6 +1547,11 @@ public class GridCacheOffheapManager extends
IgniteCacheOffheapManagerImpl imple
throw new UnsupportedOperationException();
}
+ /** {@inheritDoc} */
+ @Override public boolean storeCacheId() {
+ throw new UnsupportedOperationException();
+ }
+
/** {@inheritDoc} */
@Override public long link() {
return 0;
@@ -2374,38 +2380,40 @@ public class GridCacheOffheapManager extends
IgniteCacheOffheapManagerImpl imple
/** {@inheritDoc} */
@Override public void update(
- GridCacheContext cctx,
+ GridCacheContext<?, ?> cctx,
KeyCacheObject key,
CacheObject val,
GridCacheVersion ver,
- long expireTime,
- @Nullable CacheDataRow oldRow
+ long expireTime
) throws IgniteCheckedException {
assert grp.shared().database().checkpointLockIsHeldByThread();
CacheDataStore delegate = init0(false);
- delegate.update(cctx, key, val, ver, expireTime, oldRow);
+ delegate.update(cctx, key, val, ver, expireTime);
}
/** {@inheritDoc} */
- @Override public CacheDataRow createRow(
- GridCacheContext cctx,
+ @Override public CacheDataRow updateRow(
+ GridCacheContext<?, ?> cctx,
KeyCacheObject key,
CacheObject val,
GridCacheVersion ver,
long expireTime,
- @Nullable CacheDataRow oldRow) throws IgniteCheckedException {
+ @Nullable CacheDataRow oldRow
+ ) throws IgniteCheckedException {
assert grp.shared().database().checkpointLockIsHeldByThread();
CacheDataStore delegate = init0(false);
- return delegate.createRow(cctx, key, val, ver, expireTime, oldRow);
+ return delegate.updateRow(cctx, key, val, ver, expireTime, oldRow);
}
/** {@inheritDoc} */
- @Override public void insertRows(Collection<DataRowCacheAware> rows,
- IgnitePredicateX<CacheDataRow> initPred) throws
IgniteCheckedException {
+ @Override public void insertRows(
+ Collection<DataRow> rows,
+ IgnitePredicateX<CacheDataRow> initPred
+ ) throws IgniteCheckedException {
CacheDataStore delegate = init0(false);
delegate.insertRows(rows, initPred);
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/RowStore.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/RowStore.java
index cffcf9b1e5b..a6c50b20da9 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/RowStore.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/RowStore.java
@@ -140,20 +140,19 @@ public class RowStore {
}
/**
- * @param link Row link.
- * @param row New row data.
- * @return {@code True} if was able to update row.
+ * @param oldRow Old row data.
+ * @param newRow New row data.
* @throws IgniteCheckedException If failed.
*/
- public boolean updateRow(long link, CacheDataRow row, IoStatisticsHolder
statHolder) throws IgniteCheckedException {
+ public void updateRow(CacheDataRow oldRow, CacheDataRow newRow,
IoStatisticsHolder statHolder) throws IgniteCheckedException {
assert !persistenceEnabled ||
ctx.database().checkpointLockIsHeldByThread();
GridQueryRowCacheCleaner rowCacheCleaner0 = rowCacheCleaner.get();
if (rowCacheCleaner0 != null)
- rowCacheCleaner0.remove(link);
+ rowCacheCleaner0.remove(oldRow.link());
- return freeList.updateDataRow(link, row, statHolder);
+ freeList.updateDataRow(oldRow, newRow, statHolder);
}
/**
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/freelist/AbstractFreeList.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/freelist/AbstractFreeList.java
index 83e05562a4d..48055a157a2 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/freelist/AbstractFreeList.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/freelist/AbstractFreeList.java
@@ -39,8 +39,10 @@ import
org.apache.ignite.internal.processors.cache.persistence.DataRegionMetrics
import org.apache.ignite.internal.processors.cache.persistence.Storable;
import
org.apache.ignite.internal.processors.cache.persistence.diagnostic.pagelocktracker.PageLockTrackerManager;
import
org.apache.ignite.internal.processors.cache.persistence.evict.PageEvictionTracker;
+import
org.apache.ignite.internal.processors.cache.persistence.pagemem.PageMemoryEx;
import
org.apache.ignite.internal.processors.cache.persistence.tree.io.AbstractDataPageIO;
import
org.apache.ignite.internal.processors.cache.persistence.tree.io.DataPagePayload;
+import
org.apache.ignite.internal.processors.cache.persistence.tree.io.DataPageUpdateResult;
import org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO;
import
org.apache.ignite.internal.processors.cache.persistence.tree.reuse.LongListReuseBag;
import
org.apache.ignite.internal.processors.cache.persistence.tree.reuse.ReuseBag;
@@ -90,7 +92,7 @@ public abstract class AbstractFreeList<T extends Storable>
extends PagesList imp
private final int MIN_SIZE_FOR_DATA_PAGE;
/** */
- private final PageHandler<T, Boolean> updateRow = new UpdateRowHandler();
+ private final PageHandler<T, Boolean> updateSinglePageRow = new
UpdateSinglePageRowHandler();
/** */
private final DataRegionMetricsImpl memMetrics;
@@ -104,7 +106,7 @@ public abstract class AbstractFreeList<T extends Storable>
extends PagesList imp
/**
*
*/
- private final class UpdateRowHandler extends PageHandler<T, Boolean> {
+ private final class UpdateSinglePageRowHandler extends PageHandler<T,
Boolean> {
/** {@inheritDoc} */
@Override public Boolean run(
int cacheId,
@@ -115,40 +117,62 @@ public abstract class AbstractFreeList<T extends
Storable> extends PagesList imp
Boolean walPlc,
T row,
int itemId,
- IoStatisticsHolder statHolder)
- throws IgniteCheckedException {
+ IoStatisticsHolder statHolder
+ ) throws IgniteCheckedException {
AbstractDataPageIO<T> io = (AbstractDataPageIO<T>)iox;
int rowSize = row.size();
- boolean updated = io.updateRow(pageAddr, itemId, pageSize(), null,
row, rowSize);
+ boolean updated = io.updateRow(pageAddr, itemId, pageSize(), row,
rowSize);
evictionTracker.touchPage(pageId);
- if (updated && needWalDeltaRecord(pageId, page, walPlc)) {
- // TODO This record must contain only a reference to a logical
WAL record with the actual data.
- byte[] payload = new byte[rowSize];
+ if (updated) {
+ statHolder.trackPageRemoveData(rowSize);
+ statHolder.trackPageInsertData(rowSize);
- DataPagePayload data = io.readPayload(pageAddr, itemId,
pageSize());
+ if (needWalDeltaRecord(pageId, page, walPlc)) {
+ // TODO IGNITE-5829 This record must contain only a
reference to a logical WAL record with the actual data.
+ byte[] payload = new byte[rowSize];
- assert data.payloadSize() == rowSize;
+ DataPagePayload data = io.readPayload(pageAddr, itemId,
pageSize());
- PageUtils.getBytes(pageAddr, data.offset(), payload, 0,
rowSize);
+ assert data.payloadSize() == rowSize;
- statHolder.trackPageRemoveData(rowSize);
- statHolder.trackPageInsertData(rowSize);
+ PageUtils.getBytes(pageAddr, data.offset(), payload, 0,
rowSize);
- wal.log(new DataPageUpdateRecord(
- cacheId,
- pageId,
- itemId,
- payload));
+ wal.log(new DataPageUpdateRecord(
+ cacheId,
+ pageId,
+ itemId,
+ payload));
+ }
}
return updated;
}
}
+ /** Current state (for last processed page) of partially written row. */
+ private final class PartiallyWritten {
+ /** */
+ private final T row;
+
+ /** */
+ private long nextLink;
+
+ /** */
+ private int written;
+
+ /** */
+ private boolean modified;
+
+ /** */
+ public PartiallyWritten(T row) {
+ this.row = row;
+ }
+ }
+
/** Write a single row on a single page. */
private final WriteRowHandler writeRowHnd = new WriteRowHandler();
@@ -209,6 +233,13 @@ public abstract class AbstractFreeList<T extends Storable>
extends PagesList imp
written = (written == 0 && oldFreeSpace >= rowSize) ?
addRowFull(pageId, page, pageAddr, io, row, rowSize) :
addRowFragment(pageId, page, pageAddr, io, row, written,
rowSize);
+ // A non-head fragment (the row is not complete after the fragment
write) always occupies the whole
+ // initially empty page (see takePage), so a page can be shared
between rows only as a head fragment page.
+ // Deadlock freedom of the fragmented row lock coupling (see
updateFragmentedRow) relies on this.
+ assert written == rowSize || io.getDirectCount(pageAddr) == 1 :
+ "Non-head fragment is written to a shared page [pageId=" +
U.hexLong(pageId) +
+ ", items=" + io.getDirectCount(pageAddr) + ']';
+
if (written == rowSize)
evictionTracker.touchPage(pageId);
@@ -760,19 +791,43 @@ public abstract class AbstractFreeList<T extends
Storable> extends PagesList imp
}
/** {@inheritDoc} */
- @Override public boolean updateDataRow(long link, T row,
- IoStatisticsHolder statHolder) throws IgniteCheckedException {
+ @Override public void updateDataRow(
+ T oldRow,
+ T newRow,
+ IoStatisticsHolder statHolder
+ ) throws IgniteCheckedException {
+ long link = oldRow.link();
+ int size = newRow.size();
+
assert link != 0;
+ assert oldRow.size() == size :
+ "Unexpected row size on update [oldSize=" + oldRow.size() + ",
newSize=" + size + ']';
try {
long pageId = PageIdUtils.pageId(link);
int itemId = PageIdUtils.itemId(link);
- Boolean updated = write(pageId, updateRow, row, itemId, null,
statHolder);
+ if (size <= pageSize() -
AbstractDataPageIO.MIN_DATA_PAGE_OVERHEAD) {
+ Boolean updated = write(pageId, updateSinglePageRow, newRow,
itemId, null, statHolder);
- assert updated != null; // Can't fail here.
+ assert updated != null; // Can't fail here.
- return updated;
+ if (updated)
+ return;
+
+ // Fallback if row is fragmented.
+ }
+
+ PartiallyWritten updateRes = updateFragmentedRow(link, newRow,
statHolder);
+
+ statHolder.trackPageRemoveData(size);
+ statHolder.trackPageInsertData(size);
+
+ assert updateRes.written == size :
+ "Unexpected written row size [written=" + updateRes.written +
", rowSize=" + size + ']';
+
+ assert updateRes.nextLink == 0 :
+ "Unexpected next page link [nextLink=" +
Long.toHexString(updateRes.nextLink) + ']';
}
catch (AssertionError e) {
throw corruptedFreeListException(e);
@@ -785,6 +840,163 @@ public abstract class AbstractFreeList<T extends
Storable> extends PagesList imp
}
}
+ /**
+ * Updates fragmented row in-place.
+ * <p>
+ * Fragments are updated in the order of fragment links (from the head
fragment to the tail one) using lock
+ * coupling: the write lock on the next fragment page is acquired before
the write lock on the current fragment
+ * page is released. Readers of fragmented rows traverse fragments in the
same order with the same lock coupling
+ * (see {@code CacheDataRowAdapter#doInitFromLink}), so a concurrent
update can neither overtake a reader in the
+ * middle of the row nor be overtaken by it, and every reader observes
either the entirely old or the entirely new
+ * row.
+ * <p>
+ * Lock coupling is deadlock-free: a page can be shared between rows only
as a head fragment page, since non-head
+ * fragments always occupy the whole page (see {@link #takePage}, {@code
WriteRowHandler#addRow}), and the head
+ * page is never requested while a lock on another page of the same or
another row is held.
+ *
+ * @param link Row link.
+ * @param row Row with the new data.
+ * @param statHolder Statistics holder.
+ * @return Final state of the update.
+ * @throws IgniteCheckedException If failed.
+ */
+ private PartiallyWritten updateFragmentedRow(
+ long link, T row,
+ IoStatisticsHolder statHolder
+ ) throws IgniteCheckedException {
+ PartiallyWritten state = new PartiallyWritten(row);
+
+ int size = row.size();
+
+ long pageId = PageIdUtils.pageId(link);
+ int itemId = PageIdUtils.itemId(link);
+ long page = 0L;
+ long pageAddr = 0L;
+ boolean dirty = false;
+
+ try {
+ page = acquirePage(pageId, statHolder);
+ pageAddr = lockFragmentPage(pageId, page, link);
+
+ while (true) {
+ updateFragmentPage(pageId, page, pageAddr,
pageIoRslvr.resolve(pageAddr), state, itemId);
+
+ dirty = state.modified;
+
+ assert PageIO.getCrc(pageAddr) == 0; // See
PageHandler#writePage.
+
+ if (state.written >= size)
+ return state;
+
+ assert state.nextLink != 0L : "Unexpected end of the
fragmented row [link=" + U.hexLong(link) +
+ ", written=" + state.written + ", rowSize=" + size + ']';
+
+ long nextPageId = PageIdUtils.pageId(state.nextLink);
+ int nextItemId = PageIdUtils.itemId(state.nextLink);
+
+ // Lock coupling: acquire the write lock on the next fragment
page before releasing the current one.
+ long nextPage = acquirePage(nextPageId, statHolder);
+ long nextPageAddr;
+
+ try {
+ nextPageAddr = lockFragmentPage(nextPageId, nextPage,
state.nextLink);
+ }
+ catch (Throwable t) {
+ releasePage(nextPageId, nextPage);
+
+ throw t;
+ }
+
+ try {
+ writeUnlock(pageId, page, pageAddr, null, dirty);
+ releasePage(pageId, page);
+ }
+ finally {
+ pageId = nextPageId;
+ itemId = nextItemId;
+ page = nextPage;
+ pageAddr = nextPageAddr;
+ dirty = false;
+ }
+ }
+ }
+ finally {
+ if (pageAddr != 0L)
+ writeUnlock(pageId, page, pageAddr, null, dirty);
+
+ if (page != 0L)
+ releasePage(pageId, page);
+ }
+ }
+
+ /**
+ * Acquires the write lock on the fragment page of the live (referenced
from the data tree) row.
+ *
+ * @param pageId Page ID.
+ * @param page Page pointer.
+ * @param link Fragment link (for diagnostics).
+ * @return Page address.
+ */
+ private long lockFragmentPage(long pageId, long page, long link) {
+ long pageAddr = writeLock(pageId, page);
+
+ // Pages of the live row can't be recycled, so the lock can't fail
here.
+ assert pageAddr != 0L : "Failed to lock fragment page of the live row
[link=" + U.hexLong(link) +
+ ", tag=" + PageIdUtils.tag(pageId) + ']';
+
+ return pageAddr;
+ }
+
+ /**
+ * Updates locked fragment page of the row.
+ *
+ * @param pageId Page ID.
+ * @param page Page pointer.
+ * @param pageAddr Page address.
+ * @param iox Page IO
+ * @param fragment Partially written fragment state.
+ * @param itemId Item ID.
+ */
+ private void updateFragmentPage(
+ long pageId,
+ long page,
+ long pageAddr,
+ PageIO iox,
+ PartiallyWritten fragment,
+ int itemId
+ ) throws IgniteCheckedException {
+ AbstractDataPageIO<T> io = (AbstractDataPageIO<T>)iox;
+
+ boolean walEnabled = wal != null && !wal.pageRecordsDisabled(grpId,
pageId);
+ // Never need payload for in-memory. For persistence need payload if
wal is enabled and need to compare
+ // page to mark it dirty if it's not dirty yet even if wal is disabled.
+ boolean needPayload = pageMem instanceof PageMemoryEx && (walEnabled
|| !pageMem.isDirty(grpId, pageId, page));
+
+ DataPageUpdateResult updateRes = io.updateRowFragment(pageMem,
pageAddr, itemId, pageSize(),
+ fragment.row, fragment.written, needPayload);
+
+ evictionTracker.touchPage(pageId);
+
+ if (updateRes != null) {
+ if (updateRes.modifiedPayload() != null && walEnabled &&
needWalDeltaRecord(pageId, page, null)) {
+ wal.log(new DataPageUpdateRecord(
+ grpId,
+ pageId,
+ itemId,
+ updateRes.modifiedPayload()));
+ }
+
+ fragment.modified = !needPayload || updateRes.modifiedPayload() !=
null;
+ fragment.nextLink = updateRes.nextLink();
+ fragment.written += updateRes.payloadSize();
+ }
+ else {
+ fragment.modified = true;
+ fragment.nextLink = 0L;
+ fragment.written = fragment.row.size();
+ }
+ }
+
/** {@inheritDoc} */
@Override public void removeDataRowByLink(long link, IoStatisticsHolder
statHolder) throws IgniteCheckedException {
assert link != 0;
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/freelist/FreeList.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/freelist/FreeList.java
index 7d492fd46f8..3db46ed6a30 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/freelist/FreeList.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/freelist/FreeList.java
@@ -41,13 +41,16 @@ public interface FreeList<T extends Storable> {
public void insertDataRows(Collection<T> rows, IoStatisticsHolder
statHolder) throws IgniteCheckedException;
/**
- * @param link Row link.
- * @param row New row data.
+ * @param oldRow Old row data.
+ * @param newRow New row data.
* @param statHolder Statistics holder to track IO operations.
- * @return {@code True} if was able to update row.
* @throws IgniteCheckedException If failed.
*/
- public boolean updateDataRow(long link, T row, IoStatisticsHolder
statHolder) throws IgniteCheckedException;
+ public void updateDataRow(
+ T oldRow,
+ T newRow,
+ IoStatisticsHolder statHolder
+ ) throws IgniteCheckedException;
/**
* @param link Row link.
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/tree/io/AbstractDataPageIO.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/tree/io/AbstractDataPageIO.java
index 9802a6ece19..bfa9e630f76 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/tree/io/AbstractDataPageIO.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/tree/io/AbstractDataPageIO.java
@@ -776,10 +776,11 @@ public abstract class AbstractDataPageIO<T extends
Storable> extends PageIO impl
}
/**
+ * In-place single-page row update.
+ *
* @param pageAddr Page address.
* @param itemId Item ID.
* @param pageSize Page size.
- * @param payload Row data.
* @param row Row.
* @param rowSize Row size.
* @return {@code True} if entry is not fragmented.
@@ -789,11 +790,10 @@ public abstract class AbstractDataPageIO<T extends
Storable> extends PageIO impl
final long pageAddr,
int itemId,
int pageSize,
- @Nullable byte[] payload,
- @Nullable T row,
- final int rowSize) throws IgniteCheckedException {
+ T row,
+ final int rowSize
+ ) throws IgniteCheckedException {
assert checkIndex(itemId) : itemId;
- assert row != null ^ payload != null;
assertPageType(pageAddr);
final int dataOff = getDataOffset(pageAddr, itemId, pageSize);
@@ -801,14 +801,99 @@ public abstract class AbstractDataPageIO<T extends
Storable> extends PageIO impl
if (isFragmented(pageAddr, dataOff))
return false;
- if (row != null)
- writeRowData(pageAddr, dataOff, rowSize, row, false);
- else
- writeRowData(pageAddr, dataOff, payload);
+ writeRowData(pageAddr, dataOff, rowSize, row, false);
return true;
}
+ /**
+ * In-place row update. Modifies only entry payload, keeps size and next
fragment link.
+ *
+ * @param pageAddr Page address.
+ * @param itemId Item ID.
+ * @param pageSize Page size.
+ * @param payload Row data.
+ */
+ public void updateRow(
+ final long pageAddr,
+ int itemId,
+ int pageSize,
+ byte[] payload
+ ) {
+ assert checkIndex(itemId) : itemId;
+ assertPageType(pageAddr);
+
+ int dataOff = getDataOffset(pageAddr, itemId, pageSize);
+
+ boolean fragmented = isFragmented(pageAddr, dataOff);
+
+ assert getPageEntrySize(pageAddr, dataOff, 0) == payload.length :
"Unexpected payload length [" +
+ "stored=" + getPageEntrySize(pageAddr, dataOff, 0) + ", updated="
+ payload.length + ']';
+
+ PageUtils.putBytes(pageAddr, dataOff + PAYLOAD_LEN_SIZE + (fragmented
? LINK_SIZE : 0), payload);
+ }
+
+ /**
+ * In-place fragmented row update.
+ *
+ * @param pageAddr Page address.
+ * @param itemId Item ID.
+ * @param pageSize Page size.
+ * @param row Row.
+ * @param needPayload If modified payload required (to write WAL records).
+ * @return Next page and payload if needed, or {@code null} for last page
if no payload needed.
+ * @throws IgniteCheckedException If failed.
+ */
+ public @Nullable DataPageUpdateResult updateRowFragment(
+ PageMemory pageMem,
+ long pageAddr,
+ int itemId,
+ int pageSize,
+ T row,
+ int written,
+ boolean needPayload
+ ) throws IgniteCheckedException {
+ assert checkIndex(itemId) : itemId;
+ assertPageType(pageAddr);
+
+ int dataOff = getDataOffset(pageAddr, itemId, pageSize);
+
+ // Due to different write formats this method can't be used for
non-fragmented rows.
+ assert isFragmented(pageAddr, dataOff);
+
+ long nextLink = getNextFragmentLink(pageAddr, dataOff);
+ int payloadSize = getPageEntrySize(pageAddr, dataOff, 0);
+
+ ByteBuffer pageBuf = pageMem.pageBuffer(pageAddr);
+ pageBuf.position(dataOff + PAYLOAD_LEN_SIZE + LINK_SIZE);
+ pageBuf.limit(pageBuf.position() + payloadSize);
+
+ byte[] modifiedPayload = null;
+
+ if (needPayload) {
+ ByteBuffer buf =
ByteBuffer.allocate(payloadSize).order(pageBuf.order());
+
+ writeFragmentData(row, buf, written, payloadSize);
+
+ buf.rewind();
+
+ boolean modified = buf.compareTo(pageBuf) != 0;
+
+ if (modified) {
+ pageBuf.put(buf);
+ modifiedPayload = buf.array();
+ }
+ }
+ else {
+ writeFragmentData(row, pageBuf, written, payloadSize);
+
+ if (nextLink == 0L)
+ return null;
+ }
+
+ return new DataPageUpdateResult(payloadSize, modifiedPayload,
nextLink);
+ }
+
/**
* @param pageAddr Page address.
* @param itemId Fixed item ID (the index used for referencing an entry
from the outside).
@@ -1157,6 +1242,8 @@ public abstract class AbstractDataPageIO<T extends
Storable> extends PageIO impl
int rowOff = rowSize - written - payloadSize;
+ assertPageType(buf);
+
writeFragmentData(row, buf, rowOff, payloadSize);
}
else {
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/tree/io/DataPageIO.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/tree/io/DataPageIO.java
index aaf744969e8..0547d059387 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/tree/io/DataPageIO.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/tree/io/DataPageIO.java
@@ -54,7 +54,7 @@ public class DataPageIO extends
AbstractDataPageIO<CacheDataRow> {
long addr = pageAddr + dataOff;
- int cacheIdSize = row.cacheId() != 0 ? 4 : 0;
+ int cacheIdSize = row.storeCacheId() ? 4 : 0;
if (newRow) {
PageUtils.putShort(addr, 0, (short)payloadSize);
@@ -82,8 +82,6 @@ public class DataPageIO extends
AbstractDataPageIO<CacheDataRow> {
/** {@inheritDoc} */
@Override protected void writeFragmentData(CacheDataRow row, ByteBuffer
buf, int rowOff,
int payloadSize) throws IgniteCheckedException {
- assertPageType(buf);
-
final int keySize = row.key().valueBytesLength(null);
final int valSize = row.value().valueBytesLength(null);
@@ -130,7 +128,7 @@ public class DataPageIO extends
AbstractDataPageIO<CacheDataRow> {
final int prevLen;
final int curLen;
- int cacheIdSize = row.cacheId() == 0 ? 0 : 4;
+ int cacheIdSize = row.storeCacheId() ? 4 : 0;
switch (type) {
case CACHE_ID:
@@ -174,8 +172,10 @@ public class DataPageIO extends
AbstractDataPageIO<CacheDataRow> {
if (type == EXPIRE_TIME)
writeExpireTimeFragment(buf, row.expireTime(), rowOff, len,
prevLen);
- else if (type == CACHE_ID)
- writeCacheIdFragment(buf, row.cacheId(), rowOff, len, prevLen);
+ else if (type == CACHE_ID) {
+ if (cacheIdSize != 0)
+ writeCacheIdFragment(buf, row.cacheId(), rowOff, len, prevLen);
+ }
else if (type != VERSION) {
// Write key or value.
final CacheObject co = type == KEY ? row.key() : row.value();
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/tree/io/DataPageUpdateResult.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/tree/io/DataPageUpdateResult.java
new file mode 100644
index 00000000000..255565d87fc
--- /dev/null
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/tree/io/DataPageUpdateResult.java
@@ -0,0 +1,72 @@
+/*
+ * 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.processors.cache.persistence.tree.io;
+
+import org.apache.ignite.internal.util.typedef.internal.S;
+
+/**
+ * Data page update result.
+ */
+@SuppressWarnings("AssignmentOrReturnOfFieldWithMutableType")
+public class DataPageUpdateResult {
+ /** */
+ private final int payloadSize;
+
+ /** */
+ private final byte[] payload;
+
+ /** */
+ private final long nextLink;
+
+ /**
+ * @param payloadSize Payload size.
+ * @param payload Payload, if it was modified or {@code null}.
+ * @param nextLink Next link.
+ */
+ DataPageUpdateResult(int payloadSize, byte[] payload, long nextLink) {
+ this.payloadSize = payloadSize;
+ this.payload = payload;
+ this.nextLink = nextLink;
+ }
+
+ /**
+ * @return Modified payload.
+ */
+ public byte[] modifiedPayload() {
+ return payload;
+ }
+
+ /**
+ * @return Link to the next fragment or {@code 0} if it is the last
fragment or the data row is not fragmented.
+ */
+ public long nextLink() {
+ return nextLink;
+ }
+
+ /**
+ * @return Payload size.
+ */
+ public int payloadSize() {
+ return payloadSize;
+ }
+
+ /** {@inheritDoc} */
+ @Override public String toString() {
+ return S.toString(DataPageUpdateResult.class, this);
+ }
+}
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/tree/DataRow.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/tree/DataRow.java
index 362a0d9e15a..b06767ead44 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/tree/DataRow.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/tree/DataRow.java
@@ -24,6 +24,7 @@ import
org.apache.ignite.internal.processors.cache.CacheObject;
import org.apache.ignite.internal.processors.cache.KeyCacheObject;
import
org.apache.ignite.internal.processors.cache.persistence.CacheDataRowAdapter;
import org.apache.ignite.internal.processors.cache.version.GridCacheVersion;
+import org.jetbrains.annotations.Nullable;
/**
*
@@ -69,7 +70,15 @@ public class DataRow extends CacheDataRowAdapter {
* @param expireTime Expire time.
* @param cacheId Cache ID.
*/
- public DataRow(KeyCacheObject key, CacheObject val, GridCacheVersion ver,
int part, long expireTime, int cacheId) {
+ public DataRow(
+ KeyCacheObject key,
+ @Nullable CacheObject val,
+ GridCacheVersion ver,
+ int part,
+ long expireTime,
+ int cacheId,
+ boolean storeCacheId
+ ) {
super(0);
this.hash = key.hashCode();
@@ -80,7 +89,9 @@ public class DataRow extends CacheDataRowAdapter {
this.expireTime = expireTime;
this.cacheId = cacheId;
- verReady = true;
+ flags = (byte)(FLAG_VER_READY +
+ (storeCacheId ? FLAG_STORE_CACHE_ID : 0) +
+ (val == null ? FLAG_ALLOW_NULL_VAL : 0));
}
/**
diff --git
a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/persistence/db/IgnitePdsCheckpointRecoveryTest.java
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/persistence/db/IgnitePdsCheckpointRecoveryTest.java
index afe8d8dfa96..ec253ea6425 100644
---
a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/persistence/db/IgnitePdsCheckpointRecoveryTest.java
+++
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/persistence/db/IgnitePdsCheckpointRecoveryTest.java
@@ -40,6 +40,7 @@ import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.failure.StopNodeFailureHandler;
import org.apache.ignite.internal.IgniteEx;
import org.apache.ignite.internal.IgniteInternalFuture;
+import org.apache.ignite.internal.IgniteInterruptedCheckedException;
import org.apache.ignite.internal.encryption.AbstractEncryptionTest;
import
org.apache.ignite.internal.processors.cache.persistence.checkpoint.CheckpointRecoveryFileStorage;
import org.apache.ignite.internal.processors.cache.persistence.file.FileIO;
@@ -142,6 +143,64 @@ public class IgnitePdsCheckpointRecoveryTest extends
GridCommonAbstractTest {
cache.put(ThreadLocalRandom.current().nextInt(KEYS_CNT),
val.incrementAndGet());
});
+ failOnCheckpoint(ignite, fut);
+
+ ignite = startGrid(0);
+ IgniteCache<Integer, Integer> cache0 =
ignite.cache(DEFAULT_CACHE_NAME);
+
+ int max = 0;
+ for (int i = 0; i < KEYS_CNT; i++)
+ max = Math.max(max, cache0.get(i));
+
+ // There are two cases possible:
+ // 1. Failure during put before writting cache entry to WAL, in this
case, after restore we will get last value
+ // in cache: val.get() - 1
+ // 2. Failure during put after writting cache entry to WAL, in this
case, after restore we will get last value
+ // in cache: val.get()
+ assertTrue("Expected value between " + (val.get() - 1) + " and " +
val.get() + ", actual value: " + max,
+ max >= val.get() - 1 && max <= val.get());
+ }
+
+ /** */
+ @Test
+ public void
testRecoverFromCheckpointRecoveryFilesForMultiPageInPlaceUpdate() throws
Exception {
+ spoilFilePattern = FileTreeTestUtils.partitionFilePattern();
+
+ IgniteEx ignite = initIgnite();
+ IgniteCache<Integer, byte[]> cache = ignite.cache(DEFAULT_CACHE_NAME);
+
+ byte[][] payloads = new byte[100][10_000];
+
+ for (int i = 0; i < payloads.length; i++) {
+ ThreadLocalRandom.current().nextBytes(payloads[i]);
+ payloads[i][0] = (byte)i;
+ }
+
+ for (int i = 0; i < KEYS_CNT; i++)
+ cache.put(i, payloads[i % payloads.length]);
+
+ IgniteInternalFuture<?> fut = GridTestUtils.runAsync(() -> {
+ while (true) {
+ int key = ThreadLocalRandom.current().nextInt(KEYS_CNT);
+ byte[] val =
payloads[ThreadLocalRandom.current().nextInt(payloads.length)];
+ cache.put(key, val);
+ }
+ });
+
+ failOnCheckpoint(ignite, fut);
+
+ ignite = startGrid(0);
+ IgniteCache<Integer, byte[]> cache0 = ignite.cache(DEFAULT_CACHE_NAME);
+
+ for (int i = 0; i < KEYS_CNT; i++) {
+ byte[] val = cache0.get(i);
+ assertTrue(val[0] < payloads.length);
+ assertEqualsArraysAware(val, payloads[val[0]]);
+ }
+ }
+
+ /** */
+ private void failOnCheckpoint(IgniteEx ignite, IgniteInternalFuture<?>
futToWait) throws IgniteInterruptedCheckedException {
File cpDir =
ignite.context().pdsFolderResolver().fileTree().checkpoint();
spoiledPageLimit.set(10);
@@ -155,7 +214,7 @@ public class IgnitePdsCheckpointRecoveryTest extends
GridCommonAbstractTest {
}
try {
- fut.get(10_000);
+ futToWait.get(10_000);
}
catch (Throwable ignore) {
// Expected.
@@ -169,21 +228,6 @@ public class IgnitePdsCheckpointRecoveryTest extends
GridCommonAbstractTest {
fail.set(false);
assertTrue(cpDir.listFiles(((dir, name) ->
FILE_NAME_PATTERN.matcher(name).matches())).length > 0);
-
- ignite = startGrid(0);
- IgniteCache<Integer, Integer> cache0 =
ignite.cache(DEFAULT_CACHE_NAME);
-
- int max = 0;
- for (int i = 0; i < KEYS_CNT; i++)
- max = Math.max(max, cache0.get(i));
-
- // There are two cases possible:
- // 1. Failure during put before writting cache entry to WAL, in this
case, after restore we will get last value
- // in cache: val.get() - 1
- // 2. Failure during put after writting cache entry to WAL, in this
case, after restore we will get last value
- // in cache: val.get()
- assertTrue("Expected value between " + (val.get() - 1) + " and " +
val.get() + ", actual value: " + max,
- max >= val.get() - 1 && max <= val.get());
}
/** */
diff --git
a/modules/core/src/test/java/org/apache/ignite/internal/processors/database/CacheFreeListSelfTest.java
b/modules/core/src/test/java/org/apache/ignite/internal/processors/database/CacheFreeListSelfTest.java
index 3b317c5db38..0b0d253edc4 100644
---
a/modules/core/src/test/java/org/apache/ignite/internal/processors/database/CacheFreeListSelfTest.java
+++
b/modules/core/src/test/java/org/apache/ignite/internal/processors/database/CacheFreeListSelfTest.java
@@ -607,6 +607,11 @@ public class CacheFreeListSelfTest extends
GridCommonAbstractTest {
return len + (cacheId() != 0 ? 4 : 0);
}
+ /** {@inheritDoc} */
+ @Override public boolean storeCacheId() {
+ return cacheId() != 0;
+ }
+
/** {@inheritDoc} */
@Override public long link() {
return link;
diff --git
a/modules/core/src/test/java/org/apache/ignite/internal/processors/database/MultiPageInPlaceUpdateTest.java
b/modules/core/src/test/java/org/apache/ignite/internal/processors/database/MultiPageInPlaceUpdateTest.java
new file mode 100644
index 00000000000..80ac9eac3d1
--- /dev/null
+++
b/modules/core/src/test/java/org/apache/ignite/internal/processors/database/MultiPageInPlaceUpdateTest.java
@@ -0,0 +1,649 @@
+/*
+ * 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.processors.database;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.OpenOption;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.atomic.AtomicBoolean;
+import javax.cache.Cache;
+import javax.cache.expiry.CreatedExpiryPolicy;
+import javax.cache.expiry.Duration;
+import javax.cache.expiry.ModifiedExpiryPolicy;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.cache.query.ScanQuery;
+import org.apache.ignite.cluster.ClusterState;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.DataPageEvictionMode;
+import org.apache.ignite.configuration.DataRegionConfiguration;
+import org.apache.ignite.configuration.DataStorageConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.IgniteInternalFuture;
+import org.apache.ignite.internal.processors.cache.KeyCacheObject;
+import org.apache.ignite.internal.processors.cache.persistence.CacheDataRow;
+import org.apache.ignite.internal.processors.cache.persistence.file.FileIO;
+import
org.apache.ignite.internal.processors.cache.persistence.file.FileIOFactory;
+import
org.apache.ignite.internal.processors.cache.persistence.file.RandomAccessFileIOFactory;
+import org.apache.ignite.internal.processors.cache.tree.CacheDataTree;
+import org.apache.ignite.internal.processors.cache.tree.SearchRow;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.internal.CU;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.spi.metric.LongMetric;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.Test;
+
+import static
org.apache.ignite.configuration.DataStorageConfiguration.DFLT_DATA_REG_DEFAULT_NAME;
+import static
org.apache.ignite.internal.processors.cache.persistence.DataRegionMetricsImpl.DATAREGION_METRICS_PREFIX;
+import static
org.apache.ignite.internal.processors.metric.impl.MetricUtils.metricName;
+import static org.junit.Assert.assertNotEquals;
+
+/**
+ * Tests for multi-page in-place update.
+ */
+public class MultiPageInPlaceUpdateTest extends GridCommonAbstractTest {
+ /** */
+ private boolean pds;
+
+ /** {@inheritDoc} */
+ @Override protected IgniteConfiguration getConfiguration(String
igniteInstanceName) throws Exception {
+ IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
+
+ cfg.setDataStorageConfiguration(new DataStorageConfiguration()
+ .setFileIOFactory(new FailingFileIOFactory())
+ .setDefaultDataRegionConfiguration(new DataRegionConfiguration()
+ .setPersistenceEnabled(pds)
+ .setMetricsEnabled(true)
+ )
+ );
+
+ return cfg;
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void beforeTest() throws Exception {
+ super.beforeTest();
+
+ cleanPersistenceDir();
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void afterTest() throws Exception {
+ super.afterTest();
+
+ stopAllGrids();
+ }
+
+ /** */
+ @Test
+ public void testInPlaceUpdateInMemory() throws Exception {
+ checkInPlaceUpdate(false);
+ }
+
+ /** */
+ @Test
+ public void testInPlaceUpdatePersistence() throws Exception {
+ checkInPlaceUpdate(true);
+ }
+
+ /** */
+ private void checkInPlaceUpdate(boolean pds) throws Exception {
+ this.pds = pds;
+
+ int entrySize = 100 * 1024;
+
+ IgniteEx ignite = startGrid(0);
+
+ if (pds)
+ ignite.cluster().state(ClusterState.ACTIVE);
+
+ IgniteCache<Integer, byte[]> cache =
ignite.getOrCreateCache(DEFAULT_CACHE_NAME);
+
+ byte[] payload = new byte[entrySize];
+
+ ThreadLocalRandom.current().nextBytes(payload);
+
+ int key = 0;
+
+ cache.put(key, payload);
+
+ long link = link(ignite, key);
+
+ for (int i = 0; i < 100; i++) {
+ payload[ThreadLocalRandom.current().nextInt(entrySize)] = (byte)i;
+
+ cache.put(key, payload);
+
+ assertEqualsArraysAware(payload, cache.get(0));
+
+ assertEquals(link, link(ignite, key));
+ }
+
+ // Size changed, can't do in-place update.
+ cache.put(key, new byte[payload.length + 1]);
+
+ assertNotEquals(link, link(ignite, key));
+ }
+
+ /** */
+ @Test
+ public void testUpdateDifferentSizesInMemory() throws Exception {
+ checkUpdateDifferentSizes(false);
+ }
+
+ /** */
+ @Test
+ public void testUpdateDifferentSizesPersistence() throws Exception {
+ checkUpdateDifferentSizes(true);
+ }
+
+ /** */
+ public void checkUpdateDifferentSizes(boolean pds) throws Exception {
+ this.pds = pds;
+
+ IgniteEx ignite = startGrid(0);
+
+ if (pds)
+ ignite.cluster().state(ClusterState.ACTIVE);
+
+ IgniteCache<Integer, byte[]> cache =
ignite.getOrCreateCache(DEFAULT_CACHE_NAME);
+
+ int pageSize =
ignite.context().cache().context().database().pageSize();
+
+ for (int i = 1000; i < pageSize; i++)
+ checkLinkChange(ignite, cache, i, true, false);
+ }
+
+ /** */
+ @Test
+ public void testDirtyPagesCountAfterUpdateWithPhysicalWal() throws
Exception {
+ pds = true;
+
+ IgniteConfiguration cfg =
getConfiguration(getTestIgniteInstanceName(0));
+
+ checkDirtyPagesCountAfterUpdate(cfg);
+ }
+
+ /** */
+ @Test
+ public void testDirtyPagesCountAfterUpdateWithCheckpoinRecoveryFiles()
throws Exception {
+ pds = true;
+
+ IgniteConfiguration cfg =
getConfiguration(getTestIgniteInstanceName(0));
+
cfg.getDataStorageConfiguration().setWriteRecoveryDataOnCheckpoint(true);
+
+ checkDirtyPagesCountAfterUpdate(cfg);
+ }
+
+ /** */
+ private void checkDirtyPagesCountAfterUpdate(IgniteConfiguration cfg)
throws Exception {
+ int entrySize = 100 * 1024;
+ int entryCnt = 100;
+
+ IgniteEx ignite = startGrid(cfg);
+
+ ignite.cluster().state(ClusterState.ACTIVE);
+ IgniteCache<Integer, byte[]> cache =
ignite.getOrCreateCache(DEFAULT_CACHE_NAME);
+
+ LongMetric dirtyPages =
ignite.context().metric().registry(metricName(DATAREGION_METRICS_PREFIX,
+ DFLT_DATA_REG_DEFAULT_NAME)).findMetric("DirtyPages");
+
+ int pageSize =
ignite.context().cache().context().database().pageSize();
+
+ byte[] payload = new byte[entrySize];
+
+ for (int i = 0; i < entryCnt; i++)
+ cache.put(i, payload);
+
+ assertTrue(dirtyPages.value() >= entryCnt * entrySize / pageSize);
+
+ forceCheckpoint();
+
+ // Update only first page.
+ payload[0] = 1;
+
+ for (int i = 0; i < entryCnt; i++)
+ cache.put(i, payload);
+
+ assertTrue(dirtyPages.value() < entryCnt * entrySize / pageSize);
+
+ for (int i = 0; i < entryCnt; i++)
+ assertEqualsArraysAware(payload, cache.get(i));
+
+ forceCheckpoint();
+
+ // Update only last page.
+ payload[entrySize - 1] = 1;
+
+ for (int i = 0; i < entryCnt; i++)
+ cache.put(i, payload);
+
+ assertTrue(dirtyPages.value() < entryCnt * entrySize / pageSize);
+
+ for (int i = 0; i < entryCnt; i++)
+ assertEqualsArraysAware(payload, cache.get(i));
+
+ // Update some intermediate page.
+ payload[entrySize / 2] = 1;
+
+ for (int i = 0; i < entryCnt; i++)
+ cache.put(i, payload);
+
+ assertTrue(dirtyPages.value() < entryCnt * entrySize / pageSize);
+
+ for (int i = 0; i < entryCnt; i++)
+ assertEqualsArraysAware(payload, cache.get(i));
+ }
+
+ /** */
+ @Test
+ public void testApplyInPlaceUpdateDeltaRecordsAfterCrash() throws
Exception {
+ pds = true;
+ int entrySize = 100 * 1024;
+ int entryCnt = 100;
+
+ IgniteEx ignite = startGrid(0);
+
+ ignite.cluster().state(ClusterState.ACTIVE);
+ IgniteCache<Integer, byte[]> cache =
ignite.getOrCreateCache(DEFAULT_CACHE_NAME);
+
+ byte[] payload = new byte[entrySize];
+ long[] links = new long[entryCnt];
+
+ for (int i = 0; i < entryCnt; i++) {
+ cache.put(i, payload);
+ links[i] = link(ignite, i);
+ }
+
+ forceCheckpoint();
+
+ byte[][] payloads = new byte[entryCnt][entrySize];
+
+ for (int i = 0; i < entryCnt / 2; i++) {
+ // First change of the page - produce page snapshot WAL record.
+ ThreadLocalRandom.current().nextBytes(payloads[i]);
+ cache.put(i, payloads[i]);
+ assertEquals(links[i], link(ignite, i));
+ // Second change of the page - produce delta WAL record.
+ ThreadLocalRandom.current().nextBytes(payloads[i]);
+ cache.put(i, payloads[i]);
+ assertEquals(links[i], link(ignite, i));
+ }
+
+ int pageSize =
ignite.context().cache().context().database().pageSize();
+
+ for (int i = entryCnt / 2; i < entryCnt; i++) {
+ // Randomly modify pages of entry.
+ // - Some pages can produce only page snapshot.
+ // - Some pages can produce both page snapshot and delta pages.
+ // - Some pages can be untouched.
+ for (int j = 0; j < entrySize / pageSize; j++) {
+ payloads[i][ThreadLocalRandom.current().nextInt(entrySize)] =
(byte)j;
+ cache.put(i, payloads[i]);
+ assertEquals(links[i], link(ignite, i));
+ }
+ }
+
+ FailingFileIOFactory failingFactory =
(FailingFileIOFactory)ignite.configuration()
+ .getDataStorageConfiguration().getFileIOFactory();
+
+ failingFactory.failFlag.set(true);
+
+ try {
+ forceCheckpoint();
+
+ fail("Expected failure on checkpoint");
+ }
+ catch (Exception ignore) {
+ // Expected.
+ }
+
+ stopGrid(0);
+ ignite = startGrid(0);
+
+ cache = ignite.cache(DEFAULT_CACHE_NAME);
+
+ for (int i = 0; i < entryCnt; i++) {
+ assertEqualsArraysAware(payloads[i], cache.get(i));
+ assertEquals(links[i], link(ignite, i));
+ }
+ }
+
+ /** */
+ @Test
+ public void testLogicalRecoveryInPlaceUpdatedEntriesAfterCrash() throws
Exception {
+ pds = true;
+ int entrySize = 100 * 1024;
+ int entryCnt = 100;
+
+ IgniteEx ignite = startGrid(0);
+
+ ignite.cluster().state(ClusterState.ACTIVE);
+ IgniteCache<Integer, byte[]> cache =
ignite.getOrCreateCache(DEFAULT_CACHE_NAME);
+
+ byte[] payload = new byte[entrySize];
+ long[] links = new long[entryCnt];
+
+ for (int i = 0; i < entryCnt; i++) {
+ cache.put(i, payload);
+ links[i] = link(ignite, i);
+ }
+
+ forceCheckpoint();
+
+ byte[][] payloads = new byte[entryCnt][entrySize];
+ for (int i = 0; i < entryCnt; i++) {
+ ThreadLocalRandom.current().nextBytes(payloads[i]);
+ cache.put(i, payloads[i]);
+ assertEquals(links[i], link(ignite, i));
+ }
+
+ stopGrid(0, true);
+ ignite = startGrid(0);
+
+ cache = ignite.cache(DEFAULT_CACHE_NAME);
+
+ for (int i = 0; i < entryCnt; i++) {
+ assertEqualsArraysAware(payloads[i], cache.get(i));
+ // Entry has the same payload, but link can be changed, since
logical recovery applies records never using
+ // in-place update.
+ assertNotEquals(links[i], link(ignite, i));
+ }
+ }
+
+ /** */
+ @Test
+ public void testInPlaceUpdateWithTtl() throws Exception {
+ IgniteEx ignite = startGrid(0);
+
+ IgniteCache<Integer, byte[]> cache =
ignite.getOrCreateCache(DEFAULT_CACHE_NAME);
+ cache = cache.withExpiryPolicy(new
CreatedExpiryPolicy(Duration.ONE_DAY));
+
+ // In-place update is enabled when TTL is not changed and entry
occupies only one page.
+ checkLinkChange(ignite, cache, 100, true, false);
+
+ // In-place update is enabled when TTL is not changed and entry
occupies more than one page.
+ checkLinkChange(ignite, cache,
ignite.context().cache().context().database().pageSize(), true, false);
+
+ cache = cache.withExpiryPolicy(new
ModifiedExpiryPolicy(Duration.ONE_DAY));
+
+ // In-place update is disabled when TTL is changed.
+ checkLinkChange(ignite, cache, 100, false, true);
+ }
+
+ /** */
+ private void checkLinkChange(
+ IgniteEx ignite,
+ IgniteCache<Integer, byte[]> cache,
+ int payloadSize,
+ boolean expectInPlaceUpdate,
+ boolean ensureTtlChanged
+ ) {
+ int key = 0;
+
+ byte[] payload = new byte[payloadSize];
+ ThreadLocalRandom.current().nextBytes(payload);
+
+ cache.put(key, payload);
+ long link = link(ignite, key);
+
+ if (ensureTtlChanged) {
+ long ts = U.currentTimeMillis();
+
+ while (ts == U.currentTimeMillis())
+ doSleep(10);
+ }
+
+ ThreadLocalRandom.current().nextBytes(payload);
+ cache.put(key, payload);
+
+ assertEquals(expectInPlaceUpdate, link == link(ignite, key));
+ assertEqualsArraysAware(payload, cache.get(key));
+ }
+
+ /** */
+ @Test
+ public void testInPlaceUpdateRowSizeCalculation() throws Exception {
+ CacheConfiguration<Integer, byte[]> plainCacheCfg = new
CacheConfiguration<>(DEFAULT_CACHE_NAME);
+ // When cache belongs to shared group - cacheId is stored in
CacheDataRow (but not always in the page store).
+ CacheConfiguration<Integer, byte[]> cacheCfgWithGrp = new
CacheConfiguration<>(DEFAULT_CACHE_NAME);
+ cacheCfgWithGrp.setGroupName("grp");
+
+ for (CacheConfiguration<Integer, byte[]> ccfg :
F.asList(plainCacheCfg, cacheCfgWithGrp)) {
+ IgniteConfiguration plainCfg =
getConfiguration(getTestIgniteInstanceName(0));
+ // When eviction is enabled - cacheId is always stored in the page
store.
+ IgniteConfiguration cfgWithEviction =
getConfiguration(getTestIgniteInstanceName(0))
+ .setDataStorageConfiguration(new DataStorageConfiguration()
+ .setDefaultDataRegionConfiguration(new
DataRegionConfiguration()
+
.setPageEvictionMode(DataPageEvictionMode.RANDOM_LRU)));
+
+ checkInPlaceUpdateRowSizeCalculation(plainCfg, ccfg);
+ checkInPlaceUpdateRowSizeCalculation(cfgWithEviction, ccfg);
+ }
+ }
+
+ /** */
+ private void checkInPlaceUpdateRowSizeCalculation(
+ IgniteConfiguration cfg,
+ CacheConfiguration<Integer, byte[]> ccfg
+ ) throws Exception {
+ try (IgniteEx ignite = startGrid(cfg)) {
+ IgniteCache<Integer, byte[]> cache = ignite.createCache(ccfg);
+
+ checkLinkChange(ignite, cache, 10_000, true, false);
+ }
+ }
+
+ /** */
+ @Test
+ public void testConcurrentInPlaceUpdate() throws Exception {
+ IgniteEx ignite = startGrid(0);
+
+ IgniteCache<Integer, byte[]> cache =
ignite.getOrCreateCache(DEFAULT_CACHE_NAME);
+
+ int entrySize = 100 * 1024;
+ int entryCnt = 100;
+ byte[][] payloads = new byte[entryCnt][entrySize];
+
+ for (int i = 0; i < entryCnt; i++) {
+ ThreadLocalRandom.current().nextBytes(payloads[i]);
+
+ payloads[i][0] = (byte)i; // Store index of payload as first
element.
+
+ cache.put(i, payloads[i]);
+ }
+
+ AtomicBoolean end = new AtomicBoolean();
+ IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(
+ () -> {
+ while (!end.get()) {
+ int key = ThreadLocalRandom.current().nextInt(entryCnt);
+
+ long link = link(ignite, key);
+
+ cache.put(key,
payloads[ThreadLocalRandom.current().nextInt(payloads.length)]);
+
+ assertEquals(link, link(ignite, key));
+ }
+ }, 10, "workload");
+
+ doSleep(5_000L);
+
+ end.set(true);
+
+ fut.get(5_000L);
+
+ for (int i = 0; i < entryCnt; i++) {
+ byte[] val = cache.get(i);
+
+ int payloadIdx = val[0];
+
+ assertEqualsArraysAware(val, payloads[payloadIdx]); // Check that
value is consistent.
+ }
+ }
+
+ /** */
+ @Test
+ public void testConcurrentReadInPlaceUpdateInMemory() throws Exception {
+ checkConcurrentReadInPlaceUpdate(false);
+ }
+
+ /** */
+ @Test
+ public void testConcurrentReadInPlaceUpdatePersistence() throws Exception {
+ checkConcurrentReadInPlaceUpdate(true);
+ }
+
+ /**
+ * Checks that concurrent readers never observe partially updated (torn)
multi-page row: each reader sees either
+ * the entirely old or the entirely new value.
+ */
+ private void checkConcurrentReadInPlaceUpdate(boolean pds) throws
Exception {
+ this.pds = pds;
+
+ IgniteEx ignite = startGrid(0);
+
+ if (pds)
+ ignite.cluster().state(ClusterState.ACTIVE);
+
+ IgniteCache<Integer, byte[]> cache =
ignite.getOrCreateCache(DEFAULT_CACHE_NAME);
+
+ int entrySize = 100 * 1024;
+ int keysCnt = 4;
+ int payloadsCnt = 10;
+
+ // Each payload is filled with a single byte value, so a torn read is
detected by a non-uniform value.
+ byte[][] payloads = new byte[payloadsCnt][entrySize];
+
+ for (int i = 0; i < payloadsCnt; i++)
+ Arrays.fill(payloads[i], (byte)i);
+
+ long[] links = new long[keysCnt];
+
+ for (int key = 0; key < keysCnt; key++) {
+ cache.put(key, payloads[0]);
+
+ links[key] = link(ignite, key);
+ }
+
+ AtomicBoolean end = new AtomicBoolean();
+
+ IgniteInternalFuture<?> updFut =
GridTestUtils.runMultiThreadedAsync(() -> {
+ while (!end.get()) {
+ cache.put(ThreadLocalRandom.current().nextInt(keysCnt),
+
payloads[ThreadLocalRandom.current().nextInt(payloadsCnt)]);
+ }
+ }, 2, "updater");
+
+ IgniteInternalFuture<?> readFut =
GridTestUtils.runMultiThreadedAsync(() -> {
+ int iter = 0;
+
+ while (!end.get()) {
+ if (iter++ % 100 == 0) {
+ // Scan query reads rows by the data tree cursor.
+ List<Cache.Entry<Integer, byte[]>> entries =
cache.query(new ScanQuery<Integer, byte[]>()).getAll();
+
+ assertEquals(keysCnt, entries.size());
+
+ for (Cache.Entry<Integer, byte[]> entry : entries)
+ checkUniform(entry.getValue(), entrySize);
+ }
+ else
+
checkUniform(cache.get(ThreadLocalRandom.current().nextInt(keysCnt)),
entrySize);
+ }
+ }, 4, "reader");
+
+ try {
+ doSleep(5_000L);
+ }
+ finally {
+ end.set(true);
+ }
+
+ readFut.get(10_000L);
+ updFut.get(10_000L);
+
+ // All updates must be in-place.
+ for (int key = 0; key < keysCnt; key++)
+ assertEquals(links[key], link(ignite, key));
+ }
+
+ /** */
+ private static void checkUniform(byte[] val, int expSize) {
+ assertNotNull(val);
+ assertEquals(expSize, val.length);
+
+ byte b = val[0];
+
+ for (int i = 1; i < val.length; i++) {
+ if (val[i] != b)
+ fail("Torn read detected [pos=" + i + ", expected=" + b + ",
actual=" + val[i] + ']');
+ }
+ }
+
+ /** */
+ private long link(IgniteEx ignite, Object key) {
+ KeyCacheObject keyCacheObj =
ignite.cachex(DEFAULT_CACHE_NAME).context().toCacheKeyObject(key);
+ SearchRow searchRow = new SearchRow(CU.cacheId(DEFAULT_CACHE_NAME),
keyCacheObj);
+
+ CacheDataTree tree =
ignite.cachex(DEFAULT_CACHE_NAME).context().topology()
+ .localPartition(keyCacheObj.partition()).dataStore().tree();
+
+ assertNotNull(tree);
+
+ try {
+ CacheDataRow row = tree.findOne(searchRow);
+ assertNotNull(row);
+
+ return row.link();
+ }
+ catch (IgniteCheckedException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ /** */
+ private static final class FailingFileIOFactory implements FileIOFactory {
+ /** */
+ private final FileIOFactory delegateFactory;
+
+ /** */
+ private final AtomicBoolean failFlag = new AtomicBoolean();
+
+ /** */
+ FailingFileIOFactory() {
+ delegateFactory = new RandomAccessFileIOFactory();
+ }
+
+ /** {@inheritDoc} */
+ @Override public FileIO create(File file, OpenOption... modes) throws
IOException {
+ FileIO delegate = delegateFactory.create(file, modes);
+
+ if (failFlag.get() && file.getName().contains("END.bin"))
+ throw new IOException("Test exception");
+
+ return delegate;
+ }
+ }
+}
diff --git
a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBasicTestSuite2.java
b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBasicTestSuite2.java
index a6f9e966907..4f8889ef5c7 100644
---
a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBasicTestSuite2.java
+++
b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBasicTestSuite2.java
@@ -74,6 +74,7 @@ import
org.apache.ignite.internal.processors.database.CacheFreeListSelfTest;
import
org.apache.ignite.internal.processors.database.DataRegionMetricsSelfTest;
import
org.apache.ignite.internal.processors.database.FreeListCutTailDifferentGcTest;
import org.apache.ignite.internal.processors.database.IndexStorageSelfTest;
+import
org.apache.ignite.internal.processors.database.MultiPageInPlaceUpdateTest;
import
org.apache.ignite.internal.processors.database.SwapPathConstructionSelfTest;
import
org.apache.ignite.internal.processors.failure.FailureProcessorLoggingTest;
import
org.apache.ignite.internal.processors.failure.FailureProcessorMetricsTest;
@@ -234,6 +235,7 @@ import org.junit.runners.Suite;
MdcCacheReadRequestsRoutingTest.class,
IgniteConfigurationTest.class,
+ MultiPageInPlaceUpdateTest.class,
})
public class IgniteBasicTestSuite2 {
}
diff --git
a/modules/core/src/test/java/org/apache/ignite/util/TestStorageUtils.java
b/modules/core/src/test/java/org/apache/ignite/util/TestStorageUtils.java
index 9c696e81355..4ba17f4dd6b 100644
--- a/modules/core/src/test/java/org/apache/ignite/util/TestStorageUtils.java
+++ b/modules/core/src/test/java/org/apache/ignite/util/TestStorageUtils.java
@@ -80,8 +80,7 @@ public class TestStorageUtils {
dataEntry.value(),
dataEntry.writeVersion(),
dataEntry.expireTime(),
- locPart,
- null);
+ locPart);
ctx.offheap().dataStore(locPart).updateInitialCounter(dataEntry.partitionCounter()
- 1, 1);
}
diff --git
a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/opt/H2CacheRow.java
b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/opt/H2CacheRow.java
index c9b4db13be7..f1ad9b41ffc 100644
---
a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/opt/H2CacheRow.java
+++
b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/opt/H2CacheRow.java
@@ -263,6 +263,11 @@ public class H2CacheRow extends H2Row implements
CacheDataRow {
throw new UnsupportedOperationException();
}
+ /** {@inheritDoc} */
+ @Override public boolean storeCacheId() {
+ throw new UnsupportedOperationException();
+ }
+
/** {@inheritDoc} */
@Override public String toString() {
SB sb = new SB("Row@");