Search in sources :

Example 6 with CacheObjectImpl

use of org.apache.ignite.internal.processors.cache.CacheObjectImpl in project ignite by apache.

the class IgniteCacheContinuousQueryImmutableEntryTest method testCacheContinuousQueryEntrySerialization.

/**
 */
@Test
public void testCacheContinuousQueryEntrySerialization() {
    CacheContinuousQueryEntry e0 = new CacheContinuousQueryEntry(1, EventType.UPDATED, new KeyCacheObjectImpl(1, new byte[] { 0, 0, 0, 1 }, 1), new CacheObjectImpl(2, new byte[] { 0, 0, 0, 2 }), new CacheObjectImpl(2, new byte[] { 0, 0, 0, 3 }), true, 1, 1L, new AffinityTopologyVersion(1L), (byte) 0);
    e0.markFiltered();
    ByteBuffer buf = ByteBuffer.allocate(4096);
    DirectMessageWriter writer = new DirectMessageWriter((byte) 1);
    // Skip write class header.
    writer.onHeaderWritten();
    e0.writeTo(buf, writer);
    CacheContinuousQueryEntry e1 = new CacheContinuousQueryEntry();
    IgniteMessageFactoryImpl msgFactory = new IgniteMessageFactoryImpl(new MessageFactory[] { new GridIoMessageFactory() });
    e1.readFrom(ByteBuffer.wrap(buf.array()), new DirectMessageReader(msgFactory, (byte) 1));
    assertEquals(e0.cacheId(), e1.cacheId());
    assertEquals(e0.eventType(), e1.eventType());
    assertEquals(e0.isFiltered(), e1.isFiltered());
    assertEquals(e0.isBackup(), e1.isBackup());
    assertEquals(e0.isKeepBinary(), e1.isKeepBinary());
    assertEquals(e0.partition(), e1.partition());
    assertEquals(e0.updateCounter(), e1.updateCounter());
    // Key and value shouldn't be serialized in case an event is filtered.
    assertNull(e1.key());
    assertNotNull(e0.key());
    assertNull(e1.oldValue());
    assertNotNull(e0.oldValue());
    assertNull(e1.value());
    assertNotNull(e0.value());
}
Also used : IgniteMessageFactoryImpl(org.apache.ignite.internal.managers.communication.IgniteMessageFactoryImpl) DirectMessageReader(org.apache.ignite.internal.direct.DirectMessageReader) DirectMessageWriter(org.apache.ignite.internal.direct.DirectMessageWriter) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) KeyCacheObjectImpl(org.apache.ignite.internal.processors.cache.KeyCacheObjectImpl) CacheObjectImpl(org.apache.ignite.internal.processors.cache.CacheObjectImpl) GridIoMessageFactory(org.apache.ignite.internal.managers.communication.GridIoMessageFactory) KeyCacheObjectImpl(org.apache.ignite.internal.processors.cache.KeyCacheObjectImpl) ByteBuffer(java.nio.ByteBuffer) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 7 with CacheObjectImpl

use of org.apache.ignite.internal.processors.cache.CacheObjectImpl in project ignite by apache.

the class AbstractReadRepairTest method setDifferentValuesForSameKey.

/**
 */
private InconsistentMapping setDifferentValuesForSameKey(int key, boolean misses, boolean nulls, ReadRepairStrategy strategy) throws Exception {
    List<Ignite> nodes = new ArrayList<>();
    Map<Ignite, T2<Integer, GridCacheVersion>> mapping = new HashMap<>();
    Ignite primary = primaryNode(key, DEFAULT_CACHE_NAME);
    ThreadLocalRandom rnd = ThreadLocalRandom.current();
    if (rnd.nextBoolean()) {
        // Reversed order.
        nodes.addAll(backupNodes(key, DEFAULT_CACHE_NAME));
        nodes.add(primary);
    } else {
        nodes.add(primary);
        nodes.addAll(backupNodes(key, DEFAULT_CACHE_NAME));
    }
    if (// Random order.
    rnd.nextBoolean())
        Collections.shuffle(nodes);
    IgniteInternalCache<Integer, Integer> internalCache = (grid(1)).cachex(DEFAULT_CACHE_NAME);
    GridCacheVersionManager mgr = ((GridCacheAdapter) internalCache.cache()).context().shared().versions();
    int incVal = 0;
    Integer primVal = null;
    Collection<T2<Integer, GridCacheVersion>> vals = new ArrayList<>();
    if (misses) {
        List<Ignite> keeped = nodes.subList(0, rnd.nextInt(1, nodes.size()));
        nodes.stream().filter(node -> !keeped.contains(node)).forEach(node -> {
            T2<Integer, GridCacheVersion> nullT2 = new T2<>(null, null);
            vals.add(nullT2);
            mapping.put(node, nullT2);
        });
        // Recording nulls (missed values).
        nodes = keeped;
    }
    boolean rmvd = false;
    boolean incVer = rnd.nextBoolean();
    GridCacheVersion ver = null;
    for (Ignite node : nodes) {
        IgniteInternalCache<Integer, Integer> cache = ((IgniteEx) node).cachex(DEFAULT_CACHE_NAME);
        GridCacheAdapter<Integer, Integer> adapter = (GridCacheAdapter) cache.cache();
        GridCacheEntryEx entry = adapter.entryEx(key);
        if (ver == null || incVer)
            // Incremental version.
            ver = mgr.next(entry.context().kernalContext().discovery().topologyVersion());
        boolean rmv = nulls && (!rmvd || rnd.nextBoolean());
        Integer val = rmv ? null : rnd.nextBoolean() ? /*increment or same as previously*/
        ++incVal : incVal;
        T2<Integer, GridCacheVersion> valVer = new T2<>(val, val != null ? ver : null);
        vals.add(valVer);
        mapping.put(node, valVer);
        GridKernalContext kctx = ((IgniteEx) node).context();
        // Incremental value.
        byte[] bytes = kctx.cacheObjects().marshal(entry.context().cacheObjectContext(), rmv ? -1 : val);
        try {
            kctx.cache().context().database().checkpointReadLock();
            boolean init = entry.initialValue(new CacheObjectImpl(null, bytes), ver, 0, 0, false, AffinityTopologyVersion.NONE, GridDrType.DR_NONE, false, false);
            if (rmv) {
                if (cache.configuration().getAtomicityMode() == ATOMIC)
                    entry.innerUpdate(ver, ((IgniteEx) node).localNode().id(), ((IgniteEx) node).localNode().id(), GridCacheOperation.DELETE, null, null, false, false, false, false, null, false, false, false, false, AffinityTopologyVersion.NONE, null, GridDrType.DR_NONE, 0, 0, null, false, false, null, null, null, null, false);
                else
                    entry.innerRemove(null, ((IgniteEx) node).localNode().id(), ((IgniteEx) node).localNode().id(), false, false, false, false, false, null, AffinityTopologyVersion.NONE, CU.empty0(), GridDrType.DR_NONE, null, null, null, 1L);
                rmvd = true;
                assertFalse(entry.hasValue());
            } else
                assertTrue(entry.hasValue());
            assertTrue("iterableKey " + key + " already inited", init);
            if (node.equals(primary))
                primVal = val;
        } finally {
            ((IgniteEx) node).context().cache().context().database().checkpointReadUnlock();
        }
    }
    assertEquals(vals.size(), mapping.size());
    assertEquals(vals.size(), internalCache.configuration().getCacheMode() == REPLICATED ? serverNodesCount() : backupsCount() + 1);
    if (!misses && !nulls)
        // Primary value set.
        assertTrue(primVal != null);
    Integer fixed;
    boolean consistent;
    boolean repairable;
    if (vals.stream().distinct().count() == 1) {
        // Consistent value.
        consistent = true;
        repairable = true;
        fixed = vals.iterator().next().getKey();
    } else {
        consistent = false;
        // Currently, Atomic caches can not be repaired.
        repairable = atomicityMode() != ATOMIC;
        switch(strategy) {
            case LWW:
                if (misses || rmvd || !incVer) {
                    repairable = false;
                    // Should never be returned.
                    fixed = Integer.MIN_VALUE;
                } else
                    fixed = incVal;
                break;
            case PRIMARY:
                fixed = primVal;
                break;
            case RELATIVE_MAJORITY:
                // Should never be returned.
                fixed = Integer.MIN_VALUE;
                Map<T2<Integer, GridCacheVersion>, Integer> counts = new HashMap<>();
                for (T2<Integer, GridCacheVersion> val : vals) {
                    counts.putIfAbsent(val, 0);
                    counts.compute(val, (k, v) -> v + 1);
                }
                int[] sorted = counts.values().stream().sorted(Comparator.reverseOrder()).mapToInt(v -> v).toArray();
                int max = sorted[0];
                if (sorted.length > 1 && sorted[1] == max)
                    repairable = false;
                if (repairable)
                    for (Map.Entry<T2<Integer, GridCacheVersion>, Integer> count : counts.entrySet()) if (count.getValue().equals(max)) {
                        fixed = count.getKey().getKey();
                        break;
                    }
                break;
            case REMOVE:
                fixed = null;
                break;
            case CHECK_ONLY:
                repairable = false;
                // Should never be returned.
                fixed = Integer.MIN_VALUE;
                break;
            default:
                throw new UnsupportedOperationException(strategy.toString());
        }
    }
    return new InconsistentMapping(mapping, primVal, fixed, repairable, consistent);
}
Also used : CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) IgniteEx(org.apache.ignite.internal.IgniteEx) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) Map(java.util.Map) PARTITIONED(org.apache.ignite.cache.CacheMode.PARTITIONED) IgniteInternalCache(org.apache.ignite.internal.processors.cache.IgniteInternalCache) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) EventType(org.apache.ignite.events.EventType) Collection(java.util.Collection) Event(org.apache.ignite.events.Event) ReadRepairStrategy(org.apache.ignite.cache.ReadRepairStrategy) Collectors(java.util.stream.Collectors) IgniteCache(org.apache.ignite.IgniteCache) GridCacheOperation(org.apache.ignite.internal.processors.cache.GridCacheOperation) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) CU(org.apache.ignite.internal.util.typedef.internal.CU) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) GridCacheAdapter(org.apache.ignite.internal.processors.cache.GridCacheAdapter) ClusterState(org.apache.ignite.cluster.ClusterState) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GridKernalContext(org.apache.ignite.internal.GridKernalContext) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) IgniteIrreparableConsistencyViolationException(org.apache.ignite.internal.processors.cache.distributed.near.consistency.IgniteIrreparableConsistencyViolationException) GridDrType(org.apache.ignite.internal.processors.dr.GridDrType) DataStorageConfiguration(org.apache.ignite.configuration.DataStorageConfiguration) IgniteEvents(org.apache.ignite.IgniteEvents) G(org.apache.ignite.internal.util.typedef.G) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) Ignite(org.apache.ignite.Ignite) CacheConsistencyViolationEvent(org.apache.ignite.events.CacheConsistencyViolationEvent) CacheObjectImpl(org.apache.ignite.internal.processors.cache.CacheObjectImpl) FULL_SYNC(org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque) TRANSACTIONAL(org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL) T2(org.apache.ignite.internal.util.typedef.T2) REPLICATED(org.apache.ignite.cache.CacheMode.REPLICATED) Consumer(java.util.function.Consumer) GridCacheVersionManager(org.apache.ignite.internal.processors.cache.version.GridCacheVersionManager) TreeMap(java.util.TreeMap) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) ATOMIC(org.apache.ignite.cache.CacheAtomicityMode.ATOMIC) EVT_CONSISTENCY_VIOLATION(org.apache.ignite.events.EventType.EVT_CONSISTENCY_VIOLATION) Comparator(java.util.Comparator) Collections(java.util.Collections) CacheMode(org.apache.ignite.cache.CacheMode) HashMap(java.util.HashMap) GridKernalContext(org.apache.ignite.internal.GridKernalContext) ArrayList(java.util.ArrayList) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) GridCacheAdapter(org.apache.ignite.internal.processors.cache.GridCacheAdapter) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) T2(org.apache.ignite.internal.util.typedef.T2) CacheObjectImpl(org.apache.ignite.internal.processors.cache.CacheObjectImpl) GridCacheVersionManager(org.apache.ignite.internal.processors.cache.version.GridCacheVersionManager) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) IgniteEx(org.apache.ignite.internal.IgniteEx)

Example 8 with CacheObjectImpl

use of org.apache.ignite.internal.processors.cache.CacheObjectImpl in project ignite by apache.

the class CdcCacheVersionTest method addConflictData.

/**
 */
private void addConflictData(IgniteEx cli, IgniteCache<Integer, User> cache, int from, int to, TransactionConcurrency concurrency, TransactionIsolation isolation) {
    try {
        IgniteInternalCache<Integer, User> intCache = cli.cachex(cache.getName());
        Map<KeyCacheObject, GridCacheDrInfo> drMap = new HashMap<>();
        for (int i = from; i < to; i++) {
            KeyCacheObject key = new KeyCacheObjectImpl(i, null, intCache.affinity().partition(i));
            CacheObject val = new CacheObjectImpl(createUser(i), null);
            val.prepareMarshal(intCache.context().cacheObjectContext());
            drMap.put(key, new GridCacheDrInfo(val, new GridCacheVersion(1, i, 1, OTHER_CLUSTER_ID)));
        }
        if (concurrency != null) {
            try (Transaction tx = cli.transactions().txStart(concurrency, isolation)) {
                intCache.putAllConflict(drMap);
                tx.commit();
            }
        } else
            intCache.putAllConflict(drMap);
    } catch (IgniteCheckedException e) {
        throw new IgniteException(e);
    }
}
Also used : HashMap(java.util.HashMap) KeyCacheObjectImpl(org.apache.ignite.internal.processors.cache.KeyCacheObjectImpl) CacheObjectImpl(org.apache.ignite.internal.processors.cache.CacheObjectImpl) GridCacheDrInfo(org.apache.ignite.internal.processors.cache.dr.GridCacheDrInfo) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Transaction(org.apache.ignite.transactions.Transaction) IgniteException(org.apache.ignite.IgniteException) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) KeyCacheObjectImpl(org.apache.ignite.internal.processors.cache.KeyCacheObjectImpl) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 9 with CacheObjectImpl

use of org.apache.ignite.internal.processors.cache.CacheObjectImpl in project ignite by apache.

the class IgniteWalConverterSensitiveDataTest method withSensitiveData.

/**
 * Creating {@link WALRecord} instances with sensitive data.
 *
 * @return {@link WALRecord} instances with sensitive data.
 */
private Collection<WALRecord> withSensitiveData() {
    List<WALRecord> walRecords = new ArrayList<>();
    int cacheId = CU.cacheId(DEFAULT_CACHE_NAME);
    DataEntry dataEntry = new DataEntry(cacheId, new KeyCacheObjectImpl(SENSITIVE_DATA_VALUE_PREFIX, null, 0), new CacheObjectImpl(SENSITIVE_DATA_VALUE_PREFIX, null), GridCacheOperation.CREATE, new GridCacheVersion(), new GridCacheVersion(), 0, 0, 0, DataEntry.EMPTY_FLAGS);
    byte[] sensitiveDataBytes = SENSITIVE_DATA_VALUE_PREFIX.getBytes(StandardCharsets.UTF_8);
    walRecords.add(new DataRecord(dataEntry));
    walRecords.add(new MetastoreDataRecord(SENSITIVE_DATA_VALUE_PREFIX, sensitiveDataBytes));
    return walRecords;
}
Also used : WALRecord(org.apache.ignite.internal.pagemem.wal.record.WALRecord) DataEntry(org.apache.ignite.internal.pagemem.wal.record.DataEntry) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) MetastoreDataRecord(org.apache.ignite.internal.pagemem.wal.record.MetastoreDataRecord) ArrayList(java.util.ArrayList) KeyCacheObjectImpl(org.apache.ignite.internal.processors.cache.KeyCacheObjectImpl) CacheObjectImpl(org.apache.ignite.internal.processors.cache.CacheObjectImpl) MetastoreDataRecord(org.apache.ignite.internal.pagemem.wal.record.MetastoreDataRecord) DataRecord(org.apache.ignite.internal.pagemem.wal.record.DataRecord) KeyCacheObjectImpl(org.apache.ignite.internal.processors.cache.KeyCacheObjectImpl)

Aggregations

CacheObjectImpl (org.apache.ignite.internal.processors.cache.CacheObjectImpl)9 KeyCacheObjectImpl (org.apache.ignite.internal.processors.cache.KeyCacheObjectImpl)5 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)4 ArrayList (java.util.ArrayList)3 IgniteEx (org.apache.ignite.internal.IgniteEx)3 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)3 GridCacheAdapter (org.apache.ignite.internal.processors.cache.GridCacheAdapter)3 GridCacheEntryEx (org.apache.ignite.internal.processors.cache.GridCacheEntryEx)3 GridCacheVersionManager (org.apache.ignite.internal.processors.cache.version.GridCacheVersionManager)3 HashMap (java.util.HashMap)2 Ignite (org.apache.ignite.Ignite)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 IgniteException (org.apache.ignite.IgniteException)2 RendezvousAffinityFunction (org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction)2 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)2 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)2 IgniteInternalCache (org.apache.ignite.internal.processors.cache.IgniteInternalCache)2 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)2 ByteBuffer (java.nio.ByteBuffer)1 Collection (java.util.Collection)1