Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IGNITE-17449 Support expire time in CDC #10583

Merged
merged 23 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
IGNITE-17449 TTL support for CDC
  • Loading branch information
nizhikov committed Mar 10, 2023
commit a91d48ea551422d998e9a6ce7ba67012b1a8bad8
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.apache.ignite.internal.util.typedef.internal.CU;
import org.apache.ignite.internal.util.typedef.internal.U;

import static org.apache.ignite.internal.processors.cache.GridCacheUtils.EXPIRE_TIME_CALCULATE;
import static org.apache.ignite.internal.processors.cache.GridCacheUtils.TTL_NOT_CHANGED;
import static org.apache.ignite.internal.processors.platform.utils.PlatformUtils.readCacheObject;

/**
Expand All @@ -51,6 +53,8 @@ public class ClientCachePutAllConflictRequest extends ClientCacheDataRequest imp
public ClientCachePutAllConflictRequest(BinaryReaderExImpl reader, ClientConnectionContext ctx) {
super(reader);

boolean expPlc = cachex(ctx).configuration().getExpiryPolicyFactory() != null;

int cnt = reader.readInt();

map = new LinkedHashMap<>(cnt);
Expand All @@ -59,11 +63,19 @@ public ClientCachePutAllConflictRequest(BinaryReaderExImpl reader, ClientConnect
KeyCacheObject key = readCacheObject(reader, true);
CacheObject val = readCacheObject(reader, false);
GridCacheVersion ver = (GridCacheVersion)reader.readObjectDetached();
Long expireTime = reader.readLong();
long expireTime = reader.readLong();
long ttl = expireTime == CU.EXPIRE_TIME_ETERNAL
? CU.TTL_ETERNAL
: (expireTime - U.currentTimeMillis());

if (ttl < 0) // Happens if replication lag more than TTL.
ttl = CU.TTL_ZERO;

GridCacheDrInfo info = expireTime != CU.EXPIRE_TIME_ETERNAL ?
new GridCacheDrExpirationInfo(val, ver, CU.TTL_ETERNAL, expireTime) : //TODO: check params here.
new GridCacheDrInfo(val, ver);
new GridCacheDrExpirationInfo(val, ver, ttl, expireTime) :
(expPlc
? new GridCacheDrExpirationInfo(val, ver, TTL_NOT_CHANGED, EXPIRE_TIME_CALCULATE)
: new GridCacheDrInfo(val, ver));

map.put(key, info);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.ignite.client.IgniteClient;
import org.apache.ignite.client.Person;
import org.apache.ignite.internal.processors.cache.version.GridCacheVersion;
import org.apache.ignite.internal.processors.platform.cache.expiry.PlatformExpiryPolicy;
import org.apache.ignite.internal.util.typedef.F;
import org.apache.ignite.internal.util.typedef.T3;
import org.apache.ignite.internal.util.typedef.internal.CU;
Expand All @@ -42,6 +43,9 @@ public class DataReplicationOperationsTest extends AbstractThinClientTest {
/** Keys count. */
private static final int KEYS_CNT = 10;

/** TTL. */
public static final int TTL = 1000;

/** */
private static IgniteClient client;

Expand Down Expand Up @@ -121,39 +125,57 @@ public void testRemoveAllConflict() {
/** @throws Exception If fails. */
@Test
public void testWithExpiryPolicy() throws Exception {
long expireTime = System.currentTimeMillis() + 1000;
PlatformExpiryPolicy expPlc = new PlatformExpiryPolicy(TTL, TTL, TTL);

ClientCacheConfiguration ccfgWithTtlEntries = new ClientCacheConfiguration()
.setName("cache-with-ttl-entries");
ClientCacheConfiguration ccfgWithExpPlc = new ClientCacheConfiguration()
.setName("cache-with-expiry-policy")
.setExpiryPolicy(expPlc);

TcpClientCache<Object, Object> cache =
(TcpClientCache<Object, Object>)client.getOrCreateCache(ccfgWithTtlEntries);
TcpClientCache<Object, Object> cache = (TcpClientCache<Object, Object>)client.getOrCreateCache(ccfgWithExpPlc);

TcpClientCache<Object, Object> cacheWithTtlEntries = binary ?
TcpClientCache<Object, Object> cacheWithExpPlc = binary ?
(TcpClientCache<Object, Object>)cache.withKeepBinary() : cache;

Map<Object, T3<Object, GridCacheVersion, Long>> data = createPutAllData(expireTime);
Map<Object, T3<Object, GridCacheVersion, Long>> data = createPutAllData(CU.EXPIRE_TIME_ETERNAL);

cacheWithTtlEntries.putAllConflict(data);
cacheWithExpPlc.putAllConflict(data);

assertTrue(cacheWithTtlEntries.containsKeys(data.keySet()));
assertTrue(cacheWithExpPlc.containsKeys(data.keySet()));

assertTrue(waitForCondition(
() -> data.keySet().stream().noneMatch(cacheWithTtlEntries::containsKey),
2 * expireTime
() -> data.keySet().stream().noneMatch(cacheWithExpPlc::containsKey),
2 * expPlc.getExpiryForCreation().getDurationAmount()
));
}

/** @throws Exception If fails. */
@Test
public void testWithPerEntryExpiry() throws Exception {
TcpClientCache<Object, Object> cache0 =
(TcpClientCache<Object, Object>)client.getOrCreateCache(DEFAULT_CACHE_NAME);

TcpClientCache<Object, Object> cache = binary ?
(TcpClientCache<Object, Object>)cache0.withKeepBinary() : cache0;

Map<Object, T3<Object, GridCacheVersion, Long>> data = createPutAllData(System.currentTimeMillis() + TTL);

cache.putAllConflict(data);

assertTrue(cache.containsKeys(data.keySet()));

assertTrue(waitForCondition(() -> data.keySet().stream().noneMatch(cache::containsKey), 2 * TTL));
}

/** */
private Map<Object, T3<Object, GridCacheVersion, Long>> createPutAllData(long ttl) {
private Map<Object, T3<Object, GridCacheVersion, Long>> createPutAllData(long expireTime) {
Map<Object, T3<Object, GridCacheVersion, Long>> map = new HashMap<>();

for (int i = 0; i < KEYS_CNT; i++) {
Person key = new Person(i, "Person-" + i);
Person val = new Person(i, "Person-" + i);

map.put(binary ? client.binary().toBinary(key) : key,
new T3<>(binary ? client.binary().toBinary(val) : val, otherVer, ttl));
new T3<>(binary ? client.binary().toBinary(val) : val, otherVer, expireTime));
}

return map;
Expand Down