use of org.apache.ignite.internal.processors.platform.cache.expiry.PlatformExpiryPolicy in project ignite by apache.
the class IgnitePersistentStoreCacheGroupsTest method _testExpiryPolicy.
/**
* @throws Exception If failed.
*/
public void _testExpiryPolicy() throws Exception {
long ttl = 10000;
CacheConfiguration[] ccfgs1 = new CacheConfiguration[5];
ccfgs1[0] = cacheConfiguration(GROUP1, "c1", PARTITIONED, ATOMIC, 1);
ccfgs1[1] = cacheConfiguration(GROUP1, "c2", PARTITIONED, TRANSACTIONAL, 1);
ccfgs1[2] = cacheConfiguration(GROUP2, "c3", PARTITIONED, ATOMIC, 1);
ccfgs1[3] = cacheConfiguration(GROUP2, "c4", PARTITIONED, TRANSACTIONAL, 1);
ccfgs1[4] = cacheConfiguration(null, "c5", PARTITIONED, ATOMIC, 1);
String[] caches = { "c1", "c2", "c3", "c4", "c5" };
startGrids(3);
Ignite node = ignite(0);
node.active(true);
node.createCaches(Arrays.asList(ccfgs1));
ExpiryPolicy plc = new PlatformExpiryPolicy(ttl, -2, -2);
for (String cacheName : caches) {
IgniteCache<Object, Object> cache = node.cache(cacheName).withExpiryPolicy(plc);
for (int i = 0; i < entriesCount(); i++) cache.put(i, cacheName + i);
}
long deadline = System.currentTimeMillis() + (long) (ttl * 1.2);
stopAllGrids();
startGrids(3);
node = ignite(0);
node.active(true);
for (String cacheName : caches) {
IgniteCache<Object, Object> cache = node.cache(cacheName);
for (int i = 0; i < entriesCount(); i++) assertEquals(cacheName + i, cache.get(i));
assertEquals(entriesCount(), cache.size());
}
// Wait for expiration.
Thread.sleep(Math.max(deadline - System.currentTimeMillis(), 0));
for (String cacheName : caches) {
IgniteCache<Object, Object> cache = node.cache(cacheName);
assertEquals(0, cache.size());
}
}
use of org.apache.ignite.internal.processors.platform.cache.expiry.PlatformExpiryPolicy in project ignite by apache.
the class JavaThinCompatibilityTest method testCacheConfiguration.
/**
*/
private void testCacheConfiguration(boolean checkFieldsPrecessionAndScale, boolean checkExpiryPlc) throws Exception {
X.println(">>>> Testing cache configuration");
try (IgniteClient client = Ignition.startClient(new ClientConfiguration().setAddresses(ADDR))) {
String cacheName = "testCacheConfiguration";
ClientCacheConfiguration ccfg = new ClientCacheConfiguration();
ccfg.setName(cacheName);
ccfg.setBackups(3);
ccfg.setGroupName("cache");
ccfg.setCacheMode(CacheMode.PARTITIONED);
QueryEntity qryEntity = new QueryEntity(int.class.getName(), "Entity").setTableName("ENTITY").setFields(new LinkedHashMap<>(F.asMap("id", Integer.class.getName(), "rate", Double.class.getName())));
if (checkFieldsPrecessionAndScale) {
qryEntity.setFieldsPrecision(F.asMap("rate", 5));
qryEntity.setFieldsScale(F.asMap("rate", 2));
}
ccfg.setQueryEntities(qryEntity);
if (checkExpiryPlc)
ccfg.setExpiryPolicy(new PlatformExpiryPolicy(10, 20, 30));
client.createCache(ccfg);
ClientCacheConfiguration ccfg1 = client.cache(cacheName).getConfiguration();
assertEquals(ccfg.getName(), ccfg1.getName());
assertEquals(ccfg.getBackups(), ccfg1.getBackups());
assertEquals(ccfg.getGroupName(), ccfg1.getGroupName());
assertEquals(ccfg.getCacheMode(), ccfg1.getCacheMode());
assertEquals(ccfg.getQueryEntities().length, ccfg1.getQueryEntities().length);
assertEquals(ccfg.getQueryEntities()[0].getTableName(), ccfg1.getQueryEntities()[0].getTableName());
assertEquals(ccfg.getQueryEntities()[0].getFields(), ccfg1.getQueryEntities()[0].getFields());
if (checkFieldsPrecessionAndScale) {
assertEquals(ccfg.getQueryEntities()[0].getFieldsPrecision(), ccfg1.getQueryEntities()[0].getFieldsPrecision());
assertEquals(ccfg.getQueryEntities()[0].getFieldsScale(), ccfg1.getQueryEntities()[0].getFieldsScale());
}
if (checkExpiryPlc) {
assertEquals(ccfg.getExpiryPolicy().getExpiryForCreation(), ccfg1.getExpiryPolicy().getExpiryForCreation());
assertEquals(ccfg.getExpiryPolicy().getExpiryForAccess(), ccfg1.getExpiryPolicy().getExpiryForAccess());
assertEquals(ccfg.getExpiryPolicy().getExpiryForUpdate(), ccfg1.getExpiryPolicy().getExpiryForUpdate());
}
}
}
use of org.apache.ignite.internal.processors.platform.cache.expiry.PlatformExpiryPolicy in project ignite by apache.
the class FunctionalTest method testCacheConfiguration.
/**
* Tested API:
* <ul>
* <li>{@link ClientCache#getName()}</li>
* <li>{@link ClientCache#getConfiguration()}</li>
* </ul>
*/
@Test
public void testCacheConfiguration() throws Exception {
final String dataRegionName = "functional-test-data-region";
IgniteConfiguration cfg = Config.getServerConfiguration().setDataStorageConfiguration(new DataStorageConfiguration().setDefaultDataRegionConfiguration(new DataRegionConfiguration().setName(dataRegionName)));
try (Ignite ignored = Ignition.start(cfg);
IgniteClient client = Ignition.startClient(getClientConfiguration())) {
final String CACHE_NAME = "testCacheConfiguration";
ClientCacheConfiguration cacheCfgTemplate = new ClientCacheConfiguration().setName(CACHE_NAME).setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL).setBackups(3).setCacheMode(CacheMode.PARTITIONED).setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC).setEagerTtl(false).setGroupName("FunctionalTest").setDefaultLockTimeout(12345).setPartitionLossPolicy(PartitionLossPolicy.READ_WRITE_SAFE).setReadFromBackup(true).setRebalanceBatchSize(67890).setRebalanceBatchesPrefetchCount(102938).setRebalanceDelay(54321).setRebalanceMode(CacheRebalanceMode.SYNC).setRebalanceOrder(2).setRebalanceThrottle(564738).setRebalanceTimeout(142536).setKeyConfiguration(new CacheKeyConfiguration("Employee", "orgId")).setQueryEntities(new QueryEntity(int.class.getName(), "Employee").setTableName("EMPLOYEE").setFields(Stream.of(new SimpleEntry<>("id", Integer.class.getName()), new SimpleEntry<>("orgId", Integer.class.getName())).collect(Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue, (a, b) -> a, LinkedHashMap::new))).setKeyFields(Collections.emptySet()).setKeyFieldName("id").setNotNullFields(Collections.singleton("id")).setDefaultFieldValues(Collections.singletonMap("id", 0)).setIndexes(Collections.singletonList(new QueryIndex("id", true, "IDX_EMPLOYEE_ID"))).setAliases(Stream.of("id", "orgId").collect(Collectors.toMap(f -> f, String::toUpperCase)))).setExpiryPolicy(new PlatformExpiryPolicy(10, 20, 30)).setCopyOnRead(!CacheConfiguration.DFLT_COPY_ON_READ).setDataRegionName(dataRegionName).setMaxConcurrentAsyncOperations(4).setMaxQueryIteratorsCount(4).setOnheapCacheEnabled(true).setQueryDetailMetricsSize(1024).setQueryParallelism(4).setSqlEscapeAll(true).setSqlIndexMaxInlineSize(1024).setSqlSchema("functional-test-schema").setStatisticsEnabled(true);
ClientCacheConfiguration cacheCfg = new ClientCacheConfiguration(cacheCfgTemplate);
ClientCache<Object, Object> cache = client.createCache(cacheCfg);
assertEquals(CACHE_NAME, cache.getName());
assertTrue(Comparers.equal(cacheCfgTemplate, cache.getConfiguration()));
}
}
use of org.apache.ignite.internal.processors.platform.cache.expiry.PlatformExpiryPolicy in project ignite by apache.
the class ClientUtils method cacheConfiguration.
/**
* Deserialize configuration from stream.
*/
ClientCacheConfiguration cacheConfiguration(BinaryInputStream in, ProtocolContext protocolCtx) throws IOException {
try (BinaryReaderExImpl reader = createBinaryReader(in)) {
// Do not need length to read data. The protocol defines fixed configuration layout.
reader.readInt();
return // cache name is to be assigned later
new ClientCacheConfiguration().setName("TBD").setAtomicityMode(CacheAtomicityMode.fromOrdinal(reader.readInt())).setBackups(reader.readInt()).setCacheMode(CacheMode.fromOrdinal(reader.readInt())).setCopyOnRead(reader.readBoolean()).setDataRegionName(reader.readString()).setEagerTtl(reader.readBoolean()).setStatisticsEnabled(reader.readBoolean()).setGroupName(reader.readString()).setDefaultLockTimeout(reader.readLong()).setMaxConcurrentAsyncOperations(reader.readInt()).setMaxQueryIteratorsCount(reader.readInt()).setName(reader.readString()).setOnheapCacheEnabled(reader.readBoolean()).setPartitionLossPolicy(PartitionLossPolicy.fromOrdinal((byte) reader.readInt())).setQueryDetailMetricsSize(reader.readInt()).setQueryParallelism(reader.readInt()).setReadFromBackup(reader.readBoolean()).setRebalanceBatchSize(reader.readInt()).setRebalanceBatchesPrefetchCount(reader.readLong()).setRebalanceDelay(reader.readLong()).setRebalanceMode(CacheRebalanceMode.fromOrdinal(reader.readInt())).setRebalanceOrder(reader.readInt()).setRebalanceThrottle(reader.readLong()).setRebalanceTimeout(reader.readLong()).setSqlEscapeAll(reader.readBoolean()).setSqlIndexMaxInlineSize(reader.readInt()).setSqlSchema(reader.readString()).setWriteSynchronizationMode(CacheWriteSynchronizationMode.fromOrdinal(reader.readInt())).setKeyConfiguration(ClientUtils.collection(in, unused -> new CacheKeyConfiguration(reader.readString(), reader.readString())).toArray(new CacheKeyConfiguration[0])).setQueryEntities(ClientUtils.collection(in, unused -> {
QueryEntity qryEntity = new QueryEntity(reader.readString(), reader.readString()).setTableName(reader.readString()).setKeyFieldName(reader.readString()).setValueFieldName(reader.readString());
boolean isPrecisionAndScaleSupported = protocolCtx.isFeatureSupported(QUERY_ENTITY_PRECISION_AND_SCALE);
Collection<QueryField> qryFields = ClientUtils.collection(in, unused2 -> {
String name = reader.readString();
String typeName = reader.readString();
boolean isKey = reader.readBoolean();
boolean isNotNull = reader.readBoolean();
Object dfltVal = reader.readObject();
int precision = isPrecisionAndScaleSupported ? reader.readInt() : -1;
int scale = isPrecisionAndScaleSupported ? reader.readInt() : -1;
return new QueryField(name, typeName, isKey, isNotNull, dfltVal, precision, scale);
});
return qryEntity.setFields(qryFields.stream().collect(Collectors.toMap(QueryField::getName, QueryField::getTypeName, (a, b) -> a, LinkedHashMap::new))).setKeyFields(qryFields.stream().filter(QueryField::isKey).map(QueryField::getName).collect(Collectors.toCollection(LinkedHashSet::new))).setNotNullFields(qryFields.stream().filter(QueryField::isNotNull).map(QueryField::getName).collect(Collectors.toSet())).setDefaultFieldValues(qryFields.stream().filter(f -> f.getDefaultValue() != null).collect(Collectors.toMap(QueryField::getName, QueryField::getDefaultValue))).setFieldsPrecision(qryFields.stream().filter(f -> f.getPrecision() != -1).collect(Collectors.toMap(QueryField::getName, QueryField::getPrecision))).setFieldsScale(qryFields.stream().filter(f -> f.getScale() != -1).collect(Collectors.toMap(QueryField::getName, QueryField::getScale))).setAliases(ClientUtils.collection(in, unused3 -> new SimpleEntry<>(reader.readString(), reader.readString())).stream().collect(Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue))).setIndexes(ClientUtils.collection(in, unused4 -> {
String name = reader.readString();
QueryIndexType type = QueryIndexType.fromOrdinal(reader.readByte());
int inlineSize = reader.readInt();
LinkedHashMap<String, Boolean> fields = ClientUtils.collection(in, unused5 -> new SimpleEntry<>(reader.readString(), reader.readBoolean())).stream().collect(Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue, (a, b) -> a, LinkedHashMap::new));
return new QueryIndex(fields, type).setName(name).setInlineSize(inlineSize);
}));
}).toArray(new QueryEntity[0])).setExpiryPolicy(!protocolCtx.isFeatureSupported(EXPIRY_POLICY) ? null : reader.readBoolean() ? new PlatformExpiryPolicy(reader.readLong(), reader.readLong(), reader.readLong()) : null);
}
}
use of org.apache.ignite.internal.processors.platform.cache.expiry.PlatformExpiryPolicy in project ignite by apache.
the class IgnitePersistentStoreCacheGroupsTest method testExpiryPolicy.
/**
* @throws Exception If failed.
*/
@Test
public void testExpiryPolicy() throws Exception {
long ttl = 10 * 60000;
CacheConfiguration[] ccfgs1 = new CacheConfiguration[5];
ccfgs1[0] = cacheConfiguration(GROUP1, "c1", PARTITIONED, ATOMIC, 1);
ccfgs1[1] = cacheConfiguration(GROUP1, "c2", PARTITIONED, TRANSACTIONAL, 1);
ccfgs1[2] = cacheConfiguration(GROUP2, "c3", PARTITIONED, ATOMIC, 1);
ccfgs1[3] = cacheConfiguration(GROUP2, "c4", PARTITIONED, TRANSACTIONAL, 1);
ccfgs1[4] = cacheConfiguration(null, "c5", PARTITIONED, ATOMIC, 1);
String[] caches = { "c1", "c2", "c3", "c4", "c5" };
startGrids(3);
Ignite node = ignite(0);
node.cluster().active(true);
node.createCaches(Arrays.asList(ccfgs1));
ExpiryPolicy plc = new PlatformExpiryPolicy(ttl, -2, -2);
Map<String, Map<Integer, Long>> expTimes = new HashMap<>();
for (String cacheName : caches) {
Map<Integer, Long> cacheExpTimes = new HashMap<>();
expTimes.put(cacheName, cacheExpTimes);
IgniteCache<Object, Object> cache = node.cache(cacheName).withExpiryPolicy(plc);
for (int i = 0; i < entriesCount(); i++) {
Integer key = i;
cache.put(key, cacheName + i);
IgniteKernal primaryNode = (IgniteKernal) primaryCache(i, cacheName).unwrap(Ignite.class);
GridCacheEntryEx entry = primaryNode.internalCache(cacheName).entryEx(key);
entry.unswap();
assertTrue(entry.expireTime() > 0);
cacheExpTimes.put(key, entry.expireTime());
}
}
stopAllGrids();
startGrids(3);
node = ignite(0);
node.cluster().active(true);
for (String cacheName : caches) {
IgniteCache<Object, Object> cache = node.cache(cacheName);
for (int i = 0; i < entriesCount(); i++) {
Integer key = i;
assertEquals(cacheName + i, cache.get(i));
IgniteKernal primaryNode = (IgniteKernal) primaryCache(i, cacheName).unwrap(Ignite.class);
GridCacheEntryEx entry = primaryNode.internalCache(cacheName).entryEx(key);
entry.unswap();
assertEquals(expTimes.get(cacheName).get(key), (Long) entry.expireTime());
}
}
}
Aggregations