Search in sources :

Example 1 with LongArray

use of io.mycat.memory.unsafe.array.LongArray in project Mycat-Server by MyCATApache.

the class MemoryConsumer method allocateLongArray.

/**
   * Allocates a LongArray of `size`.
   */
public LongArray allocateLongArray(long size) {
    long required = size * 8L;
    MemoryBlock page = dataNodeMemoryManager.allocatePage(required, this);
    if (page == null || page.size() < required) {
        long got = 0;
        if (page != null) {
            got = page.size();
            dataNodeMemoryManager.freePage(page, this);
        }
        dataNodeMemoryManager.showMemoryUsage();
        throw new OutOfMemoryError("Unable to acquire " + required + " bytes of memory, got " + got);
    }
    used += required;
    return new LongArray(page);
}
Also used : LongArray(io.mycat.memory.unsafe.array.LongArray) MemoryBlock(io.mycat.memory.unsafe.memory.MemoryBlock)

Example 2 with LongArray

use of io.mycat.memory.unsafe.array.LongArray in project Mycat-Server by MyCATApache.

the class UnsafeExternalSorter method growPointerArrayIfNecessary.

/**
   * Checks whether there is enough space to insert an additional record in to the sort pointer
   * array and grows the array if additional space is required. If the required space cannot be
   * obtained, then the in-memory data will be spilled to disk.
   */
private void growPointerArrayIfNecessary() throws IOException {
    assert (inMemSorter != null);
    if (!inMemSorter.hasSpaceForAnotherRecord()) {
        long used = inMemSorter.getMemoryUsage();
        LongArray array;
        try {
            // could trigger spilling
            array = allocateLongArray(used / 8 * 2);
        } catch (OutOfMemoryError e) {
            // should have trigger spilling
            if (!inMemSorter.hasSpaceForAnotherRecord()) {
                logger.error("Unable to grow the pointer array");
                throw e;
            }
            return;
        }
        // check if spilling is triggered or not
        if (inMemSorter.hasSpaceForAnotherRecord()) {
            freeLongArray(array);
        } else {
            inMemSorter.expandPointerArray(array);
        }
    }
}
Also used : LongArray(io.mycat.memory.unsafe.array.LongArray)

Example 3 with LongArray

use of io.mycat.memory.unsafe.array.LongArray in project Mycat-Server by MyCATApache.

the class BytesToBytesMap method growAndRehash.

/**
   * Grows the size of the hash table and re-hash everything.
   */
@VisibleForTesting
void growAndRehash() {
    assert (longArray != null);
    long resizeStartTime = -1;
    if (enablePerfMetrics) {
        resizeStartTime = System.nanoTime();
    }
    // Store references to the old data structures to be used when we re-hash
    final LongArray oldLongArray = longArray;
    final int oldCapacity = (int) oldLongArray.size() / 2;
    // Allocate the new data structures
    allocate(Math.min(growthStrategy.nextCapacity(oldCapacity), MAX_CAPACITY));
    // Re-mask (we don't recompute the hashcode because we stored all 32 bits of it)
    for (int i = 0; i < oldLongArray.size(); i += 2) {
        final long keyPointer = oldLongArray.get(i);
        if (keyPointer == 0) {
            continue;
        }
        final int hashcode = (int) oldLongArray.get(i + 1);
        int newPos = hashcode & mask;
        int step = 1;
        while (longArray.get(newPos * 2) != 0) {
            newPos = (newPos + step) & mask;
            step++;
        }
        longArray.set(newPos * 2, keyPointer);
        longArray.set(newPos * 2 + 1, hashcode);
    }
    freeLongArray(oldLongArray);
    if (enablePerfMetrics) {
        timeSpentResizingNs += System.nanoTime() - resizeStartTime;
    }
}
Also used : LongArray(io.mycat.memory.unsafe.array.LongArray) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

LongArray (io.mycat.memory.unsafe.array.LongArray)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 MemoryBlock (io.mycat.memory.unsafe.memory.MemoryBlock)1