Search in sources :

Example 6 with GridUnsafeMemory

use of org.apache.ignite.internal.util.offheap.unsafe.GridUnsafeMemory in project ignite by apache.

the class GridH2KeyValueRowOffheap method decrementRefCount.

/** {@inheritDoc} */
@Override
public void decrementRefCount() {
    long p = ptr;
    assert p > 0 : p;
    GridUnsafeMemory mem = desc.memory();
    for (; ; ) {
        int cnt = mem.readIntVolatile(p);
        assert cnt > 0 : cnt;
        if (cnt == 1)
            break;
        if (mem.casInt(p, cnt, cnt - 1))
            return;
    }
    desc.uncache(p);
    // Deallocate off-heap memory.
    long valPtr = mem.readLongVolatile(p + OFFSET_VALUE_REF);
    assert valPtr >= 0 : valPtr;
    if (valPtr != 0)
        mem.release(valPtr, mem.readInt(valPtr) + OFFSET_VALUE);
    mem.release(p, mem.readInt(p + OFFSET_KEY_SIZE) + OFFSET_KEY);
}
Also used : GridUnsafeMemory(org.apache.ignite.internal.util.offheap.unsafe.GridUnsafeMemory)

Example 7 with GridUnsafeMemory

use of org.apache.ignite.internal.util.offheap.unsafe.GridUnsafeMemory in project ignite by apache.

the class GridH2KeyValueRowOffheap method incrementRefCount.

/** {@inheritDoc} */
@SuppressWarnings({ "NonPrivateFieldAccessedInSynchronizedContext" })
@Override
public void incrementRefCount() {
    long p = ptr;
    GridUnsafeMemory mem = desc.memory();
    if (p == 0) {
        // Serialize data to offheap memory.
        Value key = peekValue(KEY_COL);
        Value val = peekValue(VAL_COL);
        assert key != null;
        assert val != null;
        Data data = Data.create(null, new byte[SIZE_CALCULATOR.getValueLen(key)]);
        data.writeValue(key);
        int keySize = data.length();
        p = mem.allocate(keySize + OFFSET_KEY);
        // We don't need any synchronization or volatility here because we publish via
        // volatile write to tree node.
        mem.writeInt(p, 1);
        mem.writeLong(p + OFFSET_EXPIRATION, expirationTime);
        mem.writeInt(p + OFFSET_KEY_SIZE, keySize);
        mem.writeBytes(p + OFFSET_KEY, data.getBytes(), 0, keySize);
        data = Data.create(null, new byte[SIZE_CALCULATOR.getValueLen(val)]);
        data.writeValue(val);
        int valSize = data.length();
        long valPtr = mem.allocate(valSize + OFFSET_VALUE);
        mem.writeInt(valPtr, valSize);
        mem.writeBytes(valPtr + OFFSET_VALUE, data.getBytes(), 0, valSize);
        mem.writeLongVolatile(p + OFFSET_VALUE_REF, valPtr);
        ptr = p;
        desc.cache(this);
    } else {
        for (; ; ) {
            int cnt = mem.readIntVolatile(p);
            assert cnt > 0 : cnt;
            if (mem.casInt(p, cnt, cnt + 1))
                break;
        }
    }
}
Also used : Value(org.h2.value.Value) Data(org.h2.store.Data) GridUnsafeMemory(org.apache.ignite.internal.util.offheap.unsafe.GridUnsafeMemory)

Example 8 with GridUnsafeMemory

use of org.apache.ignite.internal.util.offheap.unsafe.GridUnsafeMemory in project ignite by apache.

the class GridH2Table method doUpdate.

/**
     * For testing only.
     *
     * @param row Row.
     * @param del If given row should be deleted from table.
     * @return {@code True} if operation succeeded.
     * @throws IgniteCheckedException If failed.
     */
@SuppressWarnings("LockAcquiredButNotSafelyReleased")
boolean doUpdate(final GridH2Row row, boolean del) throws IgniteCheckedException {
    // Here we assume that each key can't be updated concurrently and case when different indexes
    // getting updated from different threads with different rows with the same key is impossible.
    GridUnsafeMemory mem = desc == null ? null : desc.memory();
    lock(false);
    if (mem != null)
        desc.guard().begin();
    try {
        ensureNotDestroyed();
        GridH2IndexBase pk = pk();
        if (!del) {
            assert rowFactory == null || row.link != 0 : row;
            // Put to PK.
            GridH2Row old = pk.put(row);
            if (old == null)
                size.increment();
            int len = idxs.size();
            int i = pkIndexPos;
            // Start from 3 because 0 - Scan (don't need to update), 1 - PK hash (already updated), 2 - PK (already updated).
            while (++i < len) {
                if (!(idxs.get(i) instanceof GridH2IndexBase))
                    continue;
                GridH2IndexBase idx = index(i);
                addToIndex(idx, pk, row, old, false);
            }
            for (GridH2IndexBase idx : tmpIdxs.values()) addToIndex(idx, pk, row, old, true);
        } else {
            //  index(1) is PK, get full row from there (search row here contains only key but no other columns).
            GridH2Row old = pk.remove(row);
            if (old != null) {
                // Start from 3 because 0 - Scan (don't need to update), 1 - PK hash (already updated), 2 - PK (already updated).
                for (int i = pkIndexPos + 1, len = idxs.size(); i < len; i++) {
                    if (!(idxs.get(i) instanceof GridH2IndexBase))
                        continue;
                    Row res = index(i).remove(old);
                    assert eq(pk, res, old) : "\n" + old + "\n" + res + "\n" + i + " -> " + index(i).getName();
                }
                for (GridH2IndexBase idx : tmpIdxs.values()) idx.remove(old);
                size.decrement();
            } else
                return false;
        }
        // The snapshot is not actual after update.
        if (actualSnapshot != null)
            actualSnapshot.set(pk.segmentForRow(row), null);
        return true;
    } finally {
        unlock(false);
        if (mem != null)
            desc.guard().end();
    }
}
Also used : Row(org.h2.result.Row) SearchRow(org.h2.result.SearchRow) GridUnsafeMemory(org.apache.ignite.internal.util.offheap.unsafe.GridUnsafeMemory)

Example 9 with GridUnsafeMemory

use of org.apache.ignite.internal.util.offheap.unsafe.GridUnsafeMemory in project ignite by apache.

the class HadoopSkipListSelfTest method testMultiThreaded.

/**
     * @throws Exception if failed.
     */
public void testMultiThreaded() throws Exception {
    GridUnsafeMemory mem = new GridUnsafeMemory(0);
    X.println("___ Started");
    Random rnd = new GridRandom();
    for (int i = 0; i < 20; i++) {
        HadoopJobInfo job = new JobInfo();
        final HadoopTaskContext taskCtx = new TaskContext();
        final HadoopMultimap m = new HadoopSkipList(job, mem);
        final ConcurrentMap<Integer, Collection<Integer>> mm = new ConcurrentHashMap<>();
        X.println("___ MT");
        multithreaded(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                X.println("___ TH in");
                Random rnd = new GridRandom();
                IntWritable key = new IntWritable();
                IntWritable val = new IntWritable();
                HadoopMultimap.Adder a = m.startAdding(taskCtx);
                for (int i = 0; i < 50000; i++) {
                    int k = rnd.nextInt(32000);
                    int v = rnd.nextInt();
                    key.set(k);
                    val.set(v);
                    a.write(key, val);
                    Collection<Integer> list = mm.get(k);
                    if (list == null) {
                        list = new ConcurrentLinkedQueue<>();
                        Collection<Integer> old = mm.putIfAbsent(k, list);
                        if (old != null)
                            list = old;
                    }
                    list.add(v);
                }
                a.close();
                X.println("___ TH out");
                return null;
            }
        }, 3 + rnd.nextInt(27));
        HadoopTaskInput in = m.input(taskCtx);
        int prevKey = Integer.MIN_VALUE;
        while (in.next()) {
            IntWritable key = (IntWritable) in.key();
            assertTrue(key.get() > prevKey);
            prevKey = key.get();
            Iterator<?> valsIter = in.values();
            Collection<Integer> vals = mm.remove(key.get());
            assertNotNull(vals);
            while (valsIter.hasNext()) {
                IntWritable val = (IntWritable) valsIter.next();
                assertTrue(vals.remove(val.get()));
            }
            assertTrue(vals.isEmpty());
        }
        in.close();
        m.close();
        assertEquals(0, mem.allocatedSize());
    }
}
Also used : HadoopJobInfo(org.apache.ignite.internal.processors.hadoop.HadoopJobInfo) HadoopTaskInput(org.apache.ignite.internal.processors.hadoop.HadoopTaskInput) HadoopTaskContext(org.apache.ignite.internal.processors.hadoop.HadoopTaskContext) HadoopMultimap(org.apache.ignite.internal.processors.hadoop.shuffle.collections.HadoopMultimap) IOException(java.io.IOException) GridRandom(org.apache.ignite.internal.util.GridRandom) Random(java.util.Random) GridRandom(org.apache.ignite.internal.util.GridRandom) HadoopJobInfo(org.apache.ignite.internal.processors.hadoop.HadoopJobInfo) HadoopSkipList(org.apache.ignite.internal.processors.hadoop.shuffle.collections.HadoopSkipList) HadoopTaskContext(org.apache.ignite.internal.processors.hadoop.HadoopTaskContext) Collection(java.util.Collection) GridUnsafeMemory(org.apache.ignite.internal.util.offheap.unsafe.GridUnsafeMemory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) IntWritable(org.apache.hadoop.io.IntWritable)

Example 10 with GridUnsafeMemory

use of org.apache.ignite.internal.util.offheap.unsafe.GridUnsafeMemory in project ignite by apache.

the class HadoopHashMapSelfTest method testMapSimple.

/**
     * Test simple map.
     *
     * @throws Exception If failed.
     */
public void testMapSimple() throws Exception {
    GridUnsafeMemory mem = new GridUnsafeMemory(0);
    Random rnd = new Random();
    int mapSize = 16 << rnd.nextInt(3);
    HadoopTaskContext taskCtx = new TaskContext();
    final HadoopHashMultimap m = new HadoopHashMultimap(new JobInfo(), mem, mapSize);
    HadoopMultimap.Adder a = m.startAdding(taskCtx);
    Multimap<Integer, Integer> mm = ArrayListMultimap.create();
    for (int i = 0, vals = 4 * mapSize + rnd.nextInt(25); i < vals; i++) {
        int key = rnd.nextInt(mapSize);
        int val = rnd.nextInt();
        a.write(new IntWritable(key), new IntWritable(val));
        mm.put(key, val);
        X.println("k: " + key + " v: " + val);
        a.close();
        check(m, mm, taskCtx);
        a = m.startAdding(taskCtx);
    }
    //        a.add(new IntWritable(10), new IntWritable(2));
    //        mm.put(10, 2);
    //        check(m, mm);
    a.close();
    X.println("Alloc: " + mem.allocatedSize());
    m.close();
    assertEquals(0, mem.allocatedSize());
}
Also used : HadoopTaskContext(org.apache.ignite.internal.processors.hadoop.HadoopTaskContext) Random(java.util.Random) HadoopMultimap(org.apache.ignite.internal.processors.hadoop.shuffle.collections.HadoopMultimap) HadoopTaskContext(org.apache.ignite.internal.processors.hadoop.HadoopTaskContext) GridUnsafeMemory(org.apache.ignite.internal.util.offheap.unsafe.GridUnsafeMemory) HadoopHashMultimap(org.apache.ignite.internal.processors.hadoop.shuffle.collections.HadoopHashMultimap) IntWritable(org.apache.hadoop.io.IntWritable)

Aggregations

GridUnsafeMemory (org.apache.ignite.internal.util.offheap.unsafe.GridUnsafeMemory)11 Random (java.util.Random)5 IntWritable (org.apache.hadoop.io.IntWritable)5 HadoopTaskContext (org.apache.ignite.internal.processors.hadoop.HadoopTaskContext)5 HadoopJobInfo (org.apache.ignite.internal.processors.hadoop.HadoopJobInfo)4 GridRandom (org.apache.ignite.internal.util.GridRandom)4 HadoopMultimap (org.apache.ignite.internal.processors.hadoop.shuffle.collections.HadoopMultimap)3 IOException (java.io.IOException)2 Collection (java.util.Collection)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)2 HadoopTaskInput (org.apache.ignite.internal.processors.hadoop.HadoopTaskInput)2 HadoopConcurrentHashMultimap (org.apache.ignite.internal.processors.hadoop.shuffle.collections.HadoopConcurrentHashMultimap)2 HadoopSkipList (org.apache.ignite.internal.processors.hadoop.shuffle.collections.HadoopSkipList)2 HadoopDataInStream (org.apache.ignite.internal.processors.hadoop.shuffle.streams.HadoopDataInStream)2 HadoopDataOutStream (org.apache.ignite.internal.processors.hadoop.shuffle.streams.HadoopDataOutStream)2 Data (org.h2.store.Data)2 Lock (java.util.concurrent.locks.Lock)1 HadoopHashMultimap (org.apache.ignite.internal.processors.hadoop.shuffle.collections.HadoopHashMultimap)1 GridStripedLock (org.apache.ignite.internal.util.GridStripedLock)1