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);
}
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;
}
}
}
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();
}
}
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());
}
}
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());
}
Aggregations