jankotek / mapdb

MapDB provides concurrent Maps, Sets and Queues backed by disk storage or off-heap-memory. It is a fast and easy to use embedded Java database engine.
https://mapdb.org
Apache License 2.0
4.9k stars 872 forks source link

concurrency reading problem ? #752

Open lkolek opened 8 years ago

lkolek commented 8 years ago

I have a task with a lot of lookups. They usually goes quite near in terms of keys.

The effect is I can not get more than about 5-6 cores working effectivly.

Other threads gets locked in the last line of code below (StoreDirectAbstract, lines 37+):

//TODO PERF indexPages are synchronized writes are protected by structural lock, but should it be read under locks?
protected val indexPages = if(isThreadSafe) LongArrayList().asSynchronized() else LongArrayList()

protected fun recidToOffset(recid2:Long):Long{
    var recid = recid2-1; //normalize recid so it starts from zero
    if(recid< StoreDirectJava.RECIDS_PER_ZERO_INDEX_PAGE){
        //zero index page
        return StoreDirectJava.HEAD_END + 16 + recid*8
    }
    //strip zero index page
    recid -= StoreDirectJava.RECIDS_PER_ZERO_INDEX_PAGE
    val pageNum = recid/ StoreDirectJava.RECIDS_PER_INDEX_PAGE
    return indexPages.get(pageNum.toInt()) + 16 + ((recid)% StoreDirectJava.RECIDS_PER_INDEX_PAGE)*8
}

seems that indexPages.get(pageNum.toInt()) causes the problem, as this call is synchronized.

below actual implementation of get (from eclipse collections):

public long get(int index)
{
    synchronized (this.getLock())
    {
        return this.getMutableLongList().get(index);
    }
}
jankotek commented 8 years ago

I was too paranoid in first version. Will be fixed in 3.1