Search in sources :

Example 11 with IgniteBiInClosure

use of org.apache.ignite.lang.IgniteBiInClosure in project ignite by apache.

the class CacheAffinitySharedManager method initCoordinatorCaches.

/**
 * @param fut Exchange future.
 * @param newAff {@code True} if there are no older nodes with affinity info available.
 * @throws IgniteCheckedException If failed.
 * @return Future completed when caches initialization is done.
 */
public IgniteInternalFuture<?> initCoordinatorCaches(final GridDhtPartitionsExchangeFuture fut, final boolean newAff) throws IgniteCheckedException {
    final List<IgniteInternalFuture<AffinityTopologyVersion>> futs = new ArrayList<>();
    final AffinityTopologyVersion topVer = fut.initialVersion();
    forAllRegisteredCacheGroups(new IgniteInClosureX<CacheGroupDescriptor>() {

        @Override
        public void applyx(CacheGroupDescriptor desc) throws IgniteCheckedException {
            CacheGroupHolder grpHolder = grpHolders.get(desc.groupId());
            if (grpHolder != null)
                return;
            // Need initialize holders and affinity if this node became coordinator during this exchange.
            int grpId = desc.groupId();
            CacheGroupContext grp = cctx.cache().cacheGroup(grpId);
            if (grp == null) {
                cctx.io().addCacheGroupHandler(desc.groupId(), GridDhtAffinityAssignmentResponse.class, new IgniteBiInClosure<UUID, GridDhtAffinityAssignmentResponse>() {

                    @Override
                    public void apply(UUID nodeId, GridDhtAffinityAssignmentResponse res) {
                        processAffinityAssignmentResponse(nodeId, res);
                    }
                });
                grpHolder = CacheGroupHolder2.create(cctx, desc, topVer, null);
                final GridAffinityAssignmentCache aff = grpHolder.affinity();
                if (newAff) {
                    if (!aff.lastVersion().equals(topVer))
                        calculateAndInit(fut.events(), aff, topVer);
                    grpHolder.topology(fut.context().events().discoveryCache()).beforeExchange(fut, true, false);
                } else {
                    List<GridDhtPartitionsExchangeFuture> exchFuts = cctx.exchange().exchangeFutures();
                    int idx = exchFuts.indexOf(fut);
                    assert idx >= 0 && idx < exchFuts.size() - 1 : "Invalid exchange futures state [cur=" + idx + ", total=" + exchFuts.size() + ']';
                    final GridDhtPartitionsExchangeFuture prev = exchFuts.get(idx + 1);
                    assert prev.isDone() && prev.topologyVersion().compareTo(topVer) < 0 : prev;
                    if (log.isDebugEnabled()) {
                        log.debug("Need initialize affinity on coordinator [" + "cacheGrp=" + desc.cacheOrGroupName() + "prevAff=" + prev.topologyVersion() + ']');
                    }
                    GridDhtAssignmentFetchFuture fetchFut = new GridDhtAssignmentFetchFuture(cctx, desc.groupId(), prev.topologyVersion(), prev.events().discoveryCache());
                    fetchFut.init(false);
                    final GridFutureAdapter<AffinityTopologyVersion> affFut = new GridFutureAdapter<>();
                    fetchFut.listen(new IgniteInClosureX<IgniteInternalFuture<GridDhtAffinityAssignmentResponse>>() {

                        @Override
                        public void applyx(IgniteInternalFuture<GridDhtAffinityAssignmentResponse> fetchFut) throws IgniteCheckedException {
                            fetchAffinity(prev.topologyVersion(), prev.events().lastEvent(), prev.events().discoveryCache(), aff, (GridDhtAssignmentFetchFuture) fetchFut);
                            aff.calculate(topVer, fut.events().lastEvent(), fut.events().discoveryCache());
                            affFut.onDone(topVer);
                        }
                    });
                    futs.add(affFut);
                }
            } else {
                grpHolder = new CacheGroupHolder1(grp, null);
                if (newAff) {
                    GridAffinityAssignmentCache aff = grpHolder.affinity();
                    if (!aff.lastVersion().equals(topVer))
                        calculateAndInit(fut.events(), aff, topVer);
                    grpHolder.topology(fut.context().events().discoveryCache()).beforeExchange(fut, true, false);
                }
            }
            CacheGroupHolder old = grpHolders.put(grpHolder.groupId(), grpHolder);
            assert old == null : old;
        }
    });
    if (!futs.isEmpty()) {
        GridCompoundFuture<AffinityTopologyVersion, ?> affFut = new GridCompoundFuture<>();
        for (IgniteInternalFuture<AffinityTopologyVersion> f : futs) affFut.add(f);
        affFut.markInitialized();
        return affFut;
    }
    return null;
}
Also used : GridDhtPartitionsExchangeFuture(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsExchangeFuture) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) ArrayList(java.util.ArrayList) IgniteBiInClosure(org.apache.ignite.lang.IgniteBiInClosure) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteInClosureX(org.apache.ignite.internal.util.lang.IgniteInClosureX) GridCompoundFuture(org.apache.ignite.internal.util.future.GridCompoundFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridAffinityAssignmentCache(org.apache.ignite.internal.processors.affinity.GridAffinityAssignmentCache) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) GridDhtAffinityAssignmentResponse(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtAffinityAssignmentResponse) GridDhtAssignmentFetchFuture(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtAssignmentFetchFuture) GridLongList(org.apache.ignite.internal.util.GridLongList) List(java.util.List) ArrayList(java.util.ArrayList) UUID(java.util.UUID)

Example 12 with IgniteBiInClosure

use of org.apache.ignite.lang.IgniteBiInClosure in project ignite by apache.

the class GridCacheIoManager method addHandler.

/**
 * @param hndId Message handler ID.
 * @param type Type of message.
 * @param c Handler.
 * @param msgHandlers Message handlers.
 */
@SuppressWarnings({ "unchecked" })
private void addHandler(int hndId, Class<? extends GridCacheMessage> type, IgniteBiInClosure<UUID, ? extends GridCacheMessage> c, MessageHandlers msgHandlers) {
    int msgIdx = messageIndex(type);
    if (msgIdx != -1) {
        Map<Integer, IgniteBiInClosure[]> idxClsHandlers0 = msgHandlers.idxClsHandlers;
        IgniteBiInClosure[] cacheClsHandlers = idxClsHandlers0.get(hndId);
        if (cacheClsHandlers == null) {
            cacheClsHandlers = new IgniteBiInClosure[GridCacheMessage.MAX_CACHE_MSG_LOOKUP_INDEX];
            idxClsHandlers0.put(hndId, cacheClsHandlers);
        }
        if (cacheClsHandlers[msgIdx] != null)
            throw new IgniteException("Duplicate cache message ID found [hndId=" + hndId + ", type=" + type + ']');
        cacheClsHandlers[msgIdx] = c;
        msgHandlers.idxClsHandlers = idxClsHandlers0;
        return;
    } else {
        ListenerKey key = new ListenerKey(hndId, type);
        if (msgHandlers.clsHandlers.putIfAbsent(key, (IgniteBiInClosure<UUID, GridCacheMessage>) c) != null)
            assert false : "Handler for class already registered [hndId=" + hndId + ", cls=" + type + ", old=" + msgHandlers.clsHandlers.get(key) + ", new=" + c + ']';
    }
    IgniteLogger log0 = log;
    if (log0 != null && log0.isTraceEnabled())
        log0.trace("Registered cache communication handler [hndId=" + hndId + ", type=" + type + ", msgIdx=" + msgIdx + ", handler=" + c + ']');
}
Also used : IgniteException(org.apache.ignite.IgniteException) IgniteBiInClosure(org.apache.ignite.lang.IgniteBiInClosure) IgniteLogger(org.apache.ignite.IgniteLogger)

Example 13 with IgniteBiInClosure

use of org.apache.ignite.lang.IgniteBiInClosure in project ignite by apache.

the class IgniteWalReaderTest method testTxFillWalAndExtractDataRecords.

/**
 * Places records under transaction, checks its value using WAL.
 *
 * @throws Exception if failed.
 */
public void testTxFillWalAndExtractDataRecords() throws Exception {
    final int cntEntries = 1000;
    final int txCnt = 100;
    final Ignite ignite0 = startGrid("node0");
    ignite0.active(true);
    final IgniteCache<Object, Object> entries = txPutDummyRecords(ignite0, cntEntries, txCnt);
    final Map<Object, Object> ctrlMap = new HashMap<>();
    for (Cache.Entry<Object, Object> next : entries) ctrlMap.put(next.getKey(), next.getValue());
    final String subfolderName = genDbSubfolderName(ignite0, 0);
    stopGrid("node0");
    final String workDir = U.defaultWorkDirectory();
    final File binaryMeta = U.resolveWorkDirectory(workDir, "binary_meta", false);
    final File binaryMetaWithConsId = new File(binaryMeta, subfolderName);
    final File marshallerMapping = U.resolveWorkDirectory(workDir, "marshaller", false);
    final IgniteWalIteratorFactory factory = new IgniteWalIteratorFactory(log, PAGE_SIZE, binaryMetaWithConsId, marshallerMapping);
    final IgniteBiInClosure<Object, Object> objConsumer = new IgniteBiInClosure<Object, Object>() {

        @Override
        public void apply(Object key, Object val) {
            boolean rmv = remove(ctrlMap, key, val);
            if (!rmv)
                log.error("Unable to remove Key and value from control Map K:[" + key + "] V: [" + val + "]");
            if (val instanceof IndexedObject) {
                IndexedObject indexedObj = (IndexedObject) val;
                assertEquals(indexedObj.iVal, indexedObj.jVal);
                assertEquals(indexedObj.iVal, key);
                for (byte datum : indexedObj.getData()) assertTrue(datum >= 'A' && datum <= 'A' + 10);
            }
        }
    };
    scanIterateAndCount(factory, workDir, subfolderName, cntEntries, txCnt, objConsumer, null);
    assertTrue(" Control Map is not empty after reading entries " + ctrlMap, ctrlMap.isEmpty());
}
Also used : IgniteWalIteratorFactory(org.apache.ignite.internal.processors.cache.persistence.wal.reader.IgniteWalIteratorFactory) HashMap(java.util.HashMap) IgniteBiInClosure(org.apache.ignite.lang.IgniteBiInClosure) Ignite(org.apache.ignite.Ignite) BinaryObject(org.apache.ignite.binary.BinaryObject) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) File(java.io.File) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 14 with IgniteBiInClosure

use of org.apache.ignite.lang.IgniteBiInClosure in project ignite by apache.

the class IgfsLocalSecondaryFileSystemDualAbstractSelfTest method testUsedSpaceSize.

/**
 * @throws Exception If failed.
 */
public void testUsedSpaceSize() throws Exception {
    final int DIRS_COUNT = 5;
    final int DIRS_MAX_DEEP = 3;
    final int FILES_COUNT = 10;
    final AtomicLong totalSize = new AtomicLong();
    IgniteBiInClosure<Integer, IgfsPath> createHierarchy = new IgniteBiInClosure<Integer, IgfsPath>() {

        @Override
        public void apply(Integer level, IgfsPath levelDir) {
            try {
                for (int i = 0; i < FILES_COUNT; ++i) {
                    IgfsPath filePath = new IgfsPath(levelDir, "file" + Integer.toString(i));
                    createFile(igfs, filePath, true, chunk);
                    totalSize.getAndAdd(chunk.length);
                }
                if (level < DIRS_MAX_DEEP) {
                    for (int dir = 0; dir < DIRS_COUNT; dir++) {
                        IgfsPath dirPath = new IgfsPath(levelDir, "dir" + Integer.toString(dir));
                        igfs.mkdirs(dirPath);
                        apply(level + 1, dirPath);
                    }
                }
            } catch (Exception e) {
                fail(e.getMessage());
            }
        }
    };
    createHierarchy.apply(1, new IgfsPath("/dir"));
    assertEquals(totalSize.get(), igfs.metrics().secondarySpaceSize());
}
Also used : IgfsPath(org.apache.ignite.igfs.IgfsPath) AtomicLong(java.util.concurrent.atomic.AtomicLong) IgniteBiInClosure(org.apache.ignite.lang.IgniteBiInClosure) IOException(java.io.IOException)

Example 15 with IgniteBiInClosure

use of org.apache.ignite.lang.IgniteBiInClosure in project ignite by apache.

the class IgfsLocalSecondaryFileSystemProxySelfTest method testUsedSpaceSize.

/**
 * @throws Exception If failed.
 */
public void testUsedSpaceSize() throws Exception {
    final int DIRS_COUNT = 5;
    final int DIRS_MAX_DEEP = 3;
    final int FILES_COUNT = 10;
    final AtomicLong totalSize = new AtomicLong();
    IgniteBiInClosure<Integer, IgfsPath> createHierarchy = new IgniteBiInClosure<Integer, IgfsPath>() {

        @Override
        public void apply(Integer level, IgfsPath levelDir) {
            try {
                for (int i = 0; i < FILES_COUNT; ++i) {
                    IgfsPath filePath = new IgfsPath(levelDir, "file" + Integer.toString(i));
                    createFile(igfs, filePath, true, chunk);
                    totalSize.getAndAdd(chunk.length);
                }
                if (level < DIRS_MAX_DEEP) {
                    for (int dir = 0; dir < DIRS_COUNT; dir++) {
                        IgfsPath dirPath = new IgfsPath(levelDir, "dir" + Integer.toString(dir));
                        igfs.mkdirs(dirPath);
                        apply(level + 1, dirPath);
                    }
                }
            } catch (Exception e) {
                fail(e.getMessage());
            }
        }
    };
    createHierarchy.apply(1, new IgfsPath("/dir"));
    assertEquals(totalSize.get(), igfs.metrics().secondarySpaceSize());
}
Also used : IgfsPath(org.apache.ignite.igfs.IgfsPath) AtomicLong(java.util.concurrent.atomic.AtomicLong) IgniteBiInClosure(org.apache.ignite.lang.IgniteBiInClosure) IOException(java.io.IOException)

Aggregations

IgniteBiInClosure (org.apache.ignite.lang.IgniteBiInClosure)16 IgniteException (org.apache.ignite.IgniteException)5 IgniteLogger (org.apache.ignite.IgniteLogger)5 HashMap (java.util.HashMap)4 UUID (java.util.UUID)4 Ignite (org.apache.ignite.Ignite)4 IgniteCache (org.apache.ignite.IgniteCache)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 MutableEntry (javax.cache.processor.MutableEntry)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 File (java.io.File)2 IOException (java.io.IOException)2 Map (java.util.Map)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 Cache (javax.cache.Cache)2 CacheEntryEvent (javax.cache.event.CacheEntryEvent)2