Search in sources :

Example 1 with IgnitePair

use of org.apache.ignite.internal.util.lang.IgnitePair in project ignite by apache.

the class IgniteIndexReader method getCacheAndTypeId.

/**
 * Tries to get cache id and type id from index tree name.
 *
 * @param name Index name.
 * @return Pair of cache id and type id.
 */
public static IgnitePair<Integer> getCacheAndTypeId(String name) {
    return CACHE_TYPE_IDS.computeIfAbsent(name, k -> {
        Matcher mId = CACHE_TYPE_ID_SEACH_PATTERN.matcher(k);
        if (mId.find()) {
            String id = mId.group("id");
            String typeId = mId.group("typeId");
            return new IgnitePair<>(parseInt(id), parseInt(typeId));
        } else {
            Matcher cId = CACHE_ID_SEACH_PATTERN.matcher(k);
            if (cId.find()) {
                String id = cId.group("id");
                return new IgnitePair<>(parseInt(id), 0);
            }
        }
        return new IgnitePair<>(0, 0);
    });
}
Also used : IgnitePair(org.apache.ignite.internal.util.lang.IgnitePair) Matcher(java.util.regex.Matcher)

Example 2 with IgnitePair

use of org.apache.ignite.internal.util.lang.IgnitePair in project ignite by apache.

the class SparseBlockDistributedMatrix method times.

/**
 * {@inheritDoc}
 */
@SuppressWarnings({ "unchecked" })
@Override
public Matrix times(final Matrix mtx) {
    if (mtx == null)
        throw new IllegalArgumentException("The matrix should be not null.");
    if (columnSize() != mtx.rowSize())
        throw new CardinalityException(columnSize(), mtx.rowSize());
    SparseBlockDistributedMatrix matrixA = this;
    SparseBlockDistributedMatrix matrixB = (SparseBlockDistributedMatrix) mtx;
    String cacheName = this.storage().cacheName();
    SparseBlockDistributedMatrix matrixC = new SparseBlockDistributedMatrix(matrixA.rowSize(), matrixB.columnSize());
    CacheUtils.bcast(cacheName, () -> {
        Ignite ignite = Ignition.localIgnite();
        Affinity<MatrixBlockKey> affinity = ignite.affinity(cacheName);
        IgniteCache<MatrixBlockKey, MatrixBlockEntry> cache = ignite.getOrCreateCache(cacheName);
        ClusterNode locNode = ignite.cluster().localNode();
        BlockMatrixStorage storageC = matrixC.storage();
        Map<ClusterNode, Collection<MatrixBlockKey>> keysCToNodes = affinity.mapKeysToNodes(storageC.getAllKeys());
        Collection<MatrixBlockKey> locKeys = keysCToNodes.get(locNode);
        if (locKeys == null)
            return;
        // compute Cij locally on each node
        // TODO: IGNITE:5114, exec in parallel
        locKeys.forEach(key -> {
            long newBlockIdRow = key.blockRowId();
            long newBlockIdCol = key.blockColId();
            IgnitePair<Long> newBlockId = new IgnitePair<>(newBlockIdRow, newBlockIdCol);
            MatrixBlockEntry blockC = null;
            List<MatrixBlockEntry> aRow = matrixA.storage().getRowForBlock(newBlockId);
            List<MatrixBlockEntry> bCol = matrixB.storage().getColForBlock(newBlockId);
            for (int i = 0; i < aRow.size(); i++) {
                MatrixBlockEntry blockA = aRow.get(i);
                MatrixBlockEntry blockB = bCol.get(i);
                MatrixBlockEntry tmpBlock = new MatrixBlockEntry(blockA.times(blockB));
                blockC = blockC == null ? tmpBlock : new MatrixBlockEntry(blockC.plus(tmpBlock));
            }
            cache.put(storageC.getCacheKey(newBlockIdRow, newBlockIdCol), blockC);
        });
    });
    return matrixC;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) BlockMatrixStorage(org.apache.ignite.ml.math.impls.storage.matrix.BlockMatrixStorage) MatrixBlockKey(org.apache.ignite.ml.math.distributed.keys.impl.MatrixBlockKey) IgnitePair(org.apache.ignite.internal.util.lang.IgnitePair) Collection(java.util.Collection) Ignite(org.apache.ignite.Ignite) CardinalityException(org.apache.ignite.ml.math.exceptions.CardinalityException)

Example 3 with IgnitePair

use of org.apache.ignite.internal.util.lang.IgnitePair in project ignite by apache.

the class SparseBlockDistributedMatrix method times.

/**
 * {@inheritDoc}
 */
@SuppressWarnings({ "unchecked" })
@Override
public Vector times(final Vector vec) {
    if (vec == null)
        throw new IllegalArgumentException("The vector should be not null.");
    if (columnSize() != vec.size())
        throw new CardinalityException(columnSize(), vec.size());
    SparseBlockDistributedMatrix matrixA = this;
    SparseBlockDistributedVector vectorB = (SparseBlockDistributedVector) vec;
    String cacheName = this.storage().cacheName();
    SparseBlockDistributedVector vectorC = new SparseBlockDistributedVector(matrixA.rowSize());
    CacheUtils.bcast(cacheName, () -> {
        Ignite ignite = Ignition.localIgnite();
        Affinity<VectorBlockKey> affinity = ignite.affinity(cacheName);
        IgniteCache<VectorBlockKey, VectorBlockEntry> cache = ignite.getOrCreateCache(cacheName);
        ClusterNode locNode = ignite.cluster().localNode();
        BlockVectorStorage storageC = vectorC.storage();
        Map<ClusterNode, Collection<VectorBlockKey>> keysCToNodes = affinity.mapKeysToNodes(storageC.getAllKeys());
        Collection<VectorBlockKey> locKeys = keysCToNodes.get(locNode);
        if (locKeys == null)
            return;
        // compute Cij locally on each node
        // TODO: IGNITE:5114, exec in parallel
        locKeys.forEach(key -> {
            long newBlockId = key.blockId();
            IgnitePair<Long> newBlockIdForMtx = new IgnitePair<>(newBlockId, 0L);
            VectorBlockEntry blockC = null;
            List<MatrixBlockEntry> aRow = matrixA.storage().getRowForBlock(newBlockIdForMtx);
            List<VectorBlockEntry> bCol = vectorB.storage().getColForBlock(newBlockId);
            for (int i = 0; i < aRow.size(); i++) {
                MatrixBlockEntry blockA = aRow.get(i);
                VectorBlockEntry blockB = bCol.get(i);
                VectorBlockEntry tmpBlock = new VectorBlockEntry(blockA.times(blockB));
                blockC = blockC == null ? tmpBlock : new VectorBlockEntry(blockC.plus(tmpBlock));
            }
            cache.put(storageC.getCacheKey(newBlockId), blockC);
        });
    });
    return vectorC;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) VectorBlockKey(org.apache.ignite.ml.math.distributed.keys.impl.VectorBlockKey) VectorBlockEntry(org.apache.ignite.ml.math.impls.vector.VectorBlockEntry) BlockVectorStorage(org.apache.ignite.ml.math.impls.storage.matrix.BlockVectorStorage) IgnitePair(org.apache.ignite.internal.util.lang.IgnitePair) Collection(java.util.Collection) SparseBlockDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVector) Ignite(org.apache.ignite.Ignite) CardinalityException(org.apache.ignite.ml.math.exceptions.CardinalityException)

Example 4 with IgnitePair

use of org.apache.ignite.internal.util.lang.IgnitePair in project ignite by apache.

the class IgniteIndexReader method printTraversalResults.

/**
 * Prints traversal info.
 *
 * @param treeInfos Tree traversal info.
 */
private void printTraversalResults(String prefix, Map<String, TreeTraversalInfo> treeInfos) {
    print("\n" + prefix + "Tree traversal results");
    Map<Class, Long> totalStat = new HashMap<>();
    AtomicInteger totalErr = new AtomicInteger(0);
    // Map (cacheId, typeId) -> (map idxName -> size))
    Map<IgnitePair<Integer>, Map<String, Long>> cacheIdxSizes = new HashMap<>();
    treeInfos.forEach((idxName, validationInfo) -> {
        print(prefix + "-----");
        print(prefix + "Index tree: " + idxName);
        print(prefix + "-- Page stat:");
        validationInfo.ioStat.forEach((cls, cnt) -> {
            print(prefix + cls.getSimpleName() + ": " + cnt);
            totalStat.compute(cls, (k, v) -> v == null ? 1 : v + 1);
        });
        print(prefix + "-- Count of items found in leaf pages: " + validationInfo.itemStorage.size());
        printErrors(prefix, "Errors:", "No errors occurred while traversing.", "Page id=%s, exceptions:", true, validationInfo.errors);
        totalErr.addAndGet(validationInfo.errors.size());
        cacheIdxSizes.computeIfAbsent(getCacheAndTypeId(idxName), k -> new HashMap<>()).put(idxName, validationInfo.itemStorage.size());
    });
    print(prefix + "---");
    printPageStat(prefix, "Total page stat collected during trees traversal:", totalStat);
    print("");
    AtomicBoolean sizeConsistencyErrorsFound = new AtomicBoolean(false);
    cacheIdxSizes.forEach((cacheTypeId, idxSizes) -> {
        if (idxSizes.values().stream().distinct().count() > 1) {
            sizeConsistencyErrorsFound.set(true);
            totalErr.incrementAndGet();
            printErr("Index size inconsistency: cacheId=" + cacheTypeId.get1() + ", typeId=" + cacheTypeId.get2());
            idxSizes.forEach((name, size) -> printErr("     Index name: " + name + ", size=" + size));
        }
    });
    if (!sizeConsistencyErrorsFound.get())
        print(prefix + "No index size consistency errors found.");
    print("");
    print(prefix + "Total trees: " + treeInfos.keySet().size());
    print(prefix + "Total pages found in trees: " + totalStat.values().stream().mapToLong(a -> a).sum());
    print(prefix + "Total errors during trees traversal: " + totalErr.get());
    print("");
    print("------------------");
}
Also used : Arrays(java.util.Arrays) PAGE_SIZE(org.apache.ignite.internal.commandline.indexreader.IgniteIndexReader.Args.PAGE_SIZE) PART_CNT(org.apache.ignite.internal.commandline.indexreader.IgniteIndexReader.Args.PART_CNT) IgnitePair(org.apache.ignite.internal.util.lang.IgnitePair) MvccInnerIO(org.apache.ignite.internal.cache.query.index.sorted.inline.io.MvccInnerIO) Collections.singletonList(java.util.Collections.singletonList) Matcher(java.util.regex.Matcher) PagePartitionMetaIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.PagePartitionMetaIO) Arrays.asList(java.util.Arrays.asList) PageIdUtils.itemId(org.apache.ignite.internal.pagemem.PageIdUtils.itemId) Map(java.util.Map) PageUtils(org.apache.ignite.internal.pagemem.PageUtils) Objects.isNull(java.util.Objects.isNull) GridStringBuilder(org.apache.ignite.internal.util.GridStringBuilder) IgniteDataIntegrityViolationException(org.apache.ignite.internal.processors.cache.persistence.wal.crc.IgniteDataIntegrityViolationException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Set(java.util.Set) CLIArgument.optionalArg(org.apache.ignite.internal.commandline.argument.parser.CLIArgument.optionalArg) StringBuilderOutputStream(org.apache.ignite.internal.commandline.StringBuilderOutputStream) Collectors.joining(java.util.stream.Collectors.joining) PageIO.getType(org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO.getType) INDEX_PARTITION(org.apache.ignite.internal.pagemem.PageIdAllocator.INDEX_PARTITION) GridUnsafe.allocateBuffer(org.apache.ignite.internal.util.GridUnsafe.allocateBuffer) CLIArgument.mandatoryArg(org.apache.ignite.internal.commandline.argument.parser.CLIArgument.mandatoryArg) PageIdUtils(org.apache.ignite.internal.pagemem.PageIdUtils) PageIdUtils.pageIndex(org.apache.ignite.internal.pagemem.PageIdUtils.pageIndex) Supplier(java.util.function.Supplier) PagesListNodeIO(org.apache.ignite.internal.processors.cache.persistence.freelist.io.PagesListNodeIO) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) PageIO.getVersion(org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO.getVersion) PagesListMetaIO(org.apache.ignite.internal.processors.cache.persistence.freelist.io.PagesListMetaIO) AbstractInlineInnerIO(org.apache.ignite.internal.cache.query.index.sorted.inline.io.AbstractInlineInnerIO) PageIdUtils.partId(org.apache.ignite.internal.pagemem.PageIdUtils.partId) LongStream(java.util.stream.LongStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) InnerIO(org.apache.ignite.internal.cache.query.index.sorted.inline.io.InnerIO) File(java.io.File) BPlusMetaIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusMetaIO) AtomicLong(java.util.concurrent.atomic.AtomicLong) PageIdUtils.flag(org.apache.ignite.internal.pagemem.PageIdUtils.flag) PageIdUtils.pageId(org.apache.ignite.internal.pagemem.PageIdUtils.pageId) StorageException(org.apache.ignite.internal.processors.cache.persistence.StorageException) FLAG_DATA(org.apache.ignite.internal.pagemem.PageIdAllocator.FLAG_DATA) MvccLeafIO(org.apache.ignite.internal.cache.query.index.sorted.inline.io.MvccLeafIO) ByteBuffer(java.nio.ByteBuffer) GridLongList(org.apache.ignite.internal.util.GridLongList) PAGE_STORE_VER(org.apache.ignite.internal.commandline.indexreader.IgniteIndexReader.Args.PAGE_STORE_VER) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MvccDataLeafIO(org.apache.ignite.internal.processors.cache.tree.mvcc.data.MvccDataLeafIO) INDEXES(org.apache.ignite.internal.commandline.indexreader.IgniteIndexReader.Args.INDEXES) PendingRowIO(org.apache.ignite.internal.processors.cache.tree.PendingRowIO) FilePageStore(org.apache.ignite.internal.processors.cache.persistence.file.FilePageStore) BPlusLeafIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusLeafIO) Predicate(java.util.function.Predicate) IgniteException(org.apache.ignite.IgniteException) FilePageStoreManager(org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) DEST_FILE(org.apache.ignite.internal.commandline.indexreader.IgniteIndexReader.Args.DEST_FILE) String.format(java.lang.String.format) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Objects(java.util.Objects) GridUnsafe.freeBuffer(org.apache.ignite.internal.util.GridUnsafe.freeBuffer) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Pattern(java.util.regex.Pattern) AbstractInlineLeafIO(org.apache.ignite.internal.cache.query.index.sorted.inline.io.AbstractInlineLeafIO) CLIArgumentParser(org.apache.ignite.internal.commandline.argument.parser.CLIArgumentParser) Objects.nonNull(java.util.Objects.nonNull) InlineIO(org.apache.ignite.internal.cache.query.index.sorted.inline.io.InlineIO) FLAG_IDX(org.apache.ignite.internal.pagemem.PageIdAllocator.FLAG_IDX) IntStream(java.util.stream.IntStream) ProgressPrinter(org.apache.ignite.internal.commandline.ProgressPrinter) DataPagePayload(org.apache.ignite.internal.processors.cache.persistence.tree.io.DataPagePayload) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) HashSet(java.util.HashSet) PageMetaIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.PageMetaIO) IndexStorageImpl(org.apache.ignite.internal.processors.cache.persistence.IndexStorageImpl) GridUnsafe.bufferAddress(org.apache.ignite.internal.util.GridUnsafe.bufferAddress) PageIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO) DIR(org.apache.ignite.internal.commandline.indexreader.IgniteIndexReader.Args.DIR) LinkedList(java.util.LinkedList) AbstractDataLeafIO(org.apache.ignite.internal.processors.cache.tree.AbstractDataLeafIO) OutputStream(java.io.OutputStream) PrintStream(java.io.PrintStream) RowLinkIO(org.apache.ignite.internal.processors.cache.tree.RowLinkIO) F(org.apache.ignite.internal.util.typedef.F) BPlusIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusIO) GridClosure3(org.apache.ignite.internal.util.lang.GridClosure3) Integer.parseInt(java.lang.Integer.parseInt) AbstractDataPageIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.AbstractDataPageIO) BPlusInnerIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusInnerIO) LeafIO(org.apache.ignite.internal.cache.query.index.sorted.inline.io.LeafIO) CLIArgument(org.apache.ignite.internal.commandline.argument.parser.CLIArgument) Collectors.toList(java.util.stream.Collectors.toList) INDEX_FILE_NAME(org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.INDEX_FILE_NAME) FileChannel(java.nio.channels.FileChannel) Collections(java.util.Collections) CHECK_PARTS(org.apache.ignite.internal.commandline.indexreader.IgniteIndexReader.Args.CHECK_PARTS) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgnitePair(org.apache.ignite.internal.util.lang.IgnitePair) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicLong(java.util.concurrent.atomic.AtomicLong) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap)

Example 5 with IgnitePair

use of org.apache.ignite.internal.util.lang.IgnitePair in project ignite by apache.

the class BinaryMarshallerSelfTest method handleToCollection.

/**
 * Checks {@link BinaryBuilderReader#parseValue()} for object that contains handles to collection.
 *
 * @throws Exception If failed.
 */
@Test
public void handleToCollection() throws Exception {
    final IgnitePair<String>[] fieldsColAndHandle = new IgnitePair[] { new IgnitePair<>("lst", "hndLst"), new IgnitePair<>("linkedLst", "hndLinkedLst"), new IgnitePair<>("map", "hndMap"), new IgnitePair<>("linkedMap", "hndLinkedMap") };
    BinaryMarshaller m = binaryMarshaller();
    HandleToCollections obj = new HandleToCollections();
    BinaryObject bo = marshal(obj, m);
    for (int i = 0; i < 10; ++i) {
        BinaryObjectBuilder bob = bo.toBuilder();
        if (i > 0)
            assertEquals(i - 1, (int) bo.field("a"));
        bob.setField("a", i);
        for (IgnitePair<String> flds : fieldsColAndHandle) {
            // Different orders to read collection and handle to collection.
            Object col;
            Object colHnd;
            if (i % 2 == 0) {
                col = bob.getField(flds.get1());
                colHnd = bob.getField(flds.get2());
            } else {
                colHnd = bob.getField(flds.get2());
                col = bob.getField(flds.get1());
            }
            // Must be assertSame but now BinaryObjectBuilder doesn't support handle to collection.
            // Now we check only that BinaryObjectBuilder#getField doesn't crash and returns valid collection.
            assertEquals("Check: " + flds, col, colHnd);
        }
        bo = bob.build();
    }
}
Also used : IgnitePair(org.apache.ignite.internal.util.lang.IgnitePair) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder) Test(org.junit.Test)

Aggregations

IgnitePair (org.apache.ignite.internal.util.lang.IgnitePair)9 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Ignite (org.apache.ignite.Ignite)2 IgniteException (org.apache.ignite.IgniteException)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2 CardinalityException (org.apache.ignite.ml.math.exceptions.CardinalityException)2 Transaction (org.apache.ignite.transactions.Transaction)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 PrintStream (java.io.PrintStream)1 Integer.parseInt (java.lang.Integer.parseInt)1 String.format (java.lang.String.format)1 ByteBuffer (java.nio.ByteBuffer)1 FileChannel (java.nio.channels.FileChannel)1 SQLException (java.sql.SQLException)1