Skip to content

Commit

Permalink
Merge pull request #9623 from MishaDemianenko/3.2-replicated-id-gener…
Browse files Browse the repository at this point in the history
…ator-locks

More fine grain locking for replicated id generator.
  • Loading branch information
MishaDemianenko committed Jul 5, 2017
2 parents bf81beb + d5f29ea commit 47e2af3
Showing 1 changed file with 78 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.neo4j.causalclustering.core.state.machines.id;

import java.io.File;
import java.util.concurrent.locks.ReentrantLock;

import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.kernel.impl.store.id.IdContainer;
Expand All @@ -38,10 +39,10 @@ class ReplicatedIdGenerator implements IdGenerator
private final IdType idType;
private final Log log;
private final ReplicatedIdRangeAcquirer acquirer;
private long highId;
private IdRangeIterator idQueue = EMPTY_ID_RANGE_ITERATOR;

private volatile long highId;
private volatile IdRangeIterator idQueue = EMPTY_ID_RANGE_ITERATOR;
private IdContainer idContainer;
private final ReentrantLock idContainerLock = new ReentrantLock();

ReplicatedIdGenerator( FileSystemAbstraction fs, File file, IdType idType, long highId,
ReplicatedIdRangeAcquirer acquirer, LogProvider logProvider, int grabSize, boolean aggressiveReuse )
Expand All @@ -55,45 +56,61 @@ class ReplicatedIdGenerator implements IdGenerator
}

@Override
public synchronized void close()
public void close()
{
idContainer.close( highId );
idContainerLock.lock();
try
{
idContainer.close( highId );
}
finally
{
idContainerLock.unlock();
}
}

@Override
public synchronized void freeId( long id )
public void freeId( long id )
{
idContainer.freeId( id );
idContainerLock.lock();
try
{
idContainer.freeId( id );
}
finally
{
idContainerLock.unlock();
}
}

@Override
public synchronized long getHighId()
public long getHighId()
{
return highId;
}

@Override
public synchronized void setHighId( long id )
public void setHighId( long id )
{
this.highId = max( this.highId, id );
}

@Override
public synchronized long getHighestPossibleIdInUse()
public long getHighestPossibleIdInUse()
{
return highId - 1;
}

@Override
public synchronized long getNumberOfIdsInUse()
public long getNumberOfIdsInUse()
{
return highId - getDefragCount();
}

@Override
public synchronized long nextId()
{
long id = idContainer.getReusableId();
long id = getReusableId();
if ( id != IdContainer.NO_RESULT )
{
return id;
Expand All @@ -113,11 +130,58 @@ public synchronized long nextId()
}

@Override
public synchronized IdRange nextIdBatch( int size )
public IdRange nextIdBatch( int size )
{
throw new UnsupportedOperationException( "Should never be called" );
}

@Override
public long getDefragCount()
{
idContainerLock.lock();
try
{
return idContainer.getFreeIdCount();
}
finally
{
idContainerLock.unlock();
}
}

@Override
public void delete()
{
idContainerLock.lock();
try
{
idContainer.delete();
}
finally
{
idContainerLock.unlock();
}
}

@Override
public String toString()
{
return getClass().getSimpleName() + "[" + this.idQueue + "]";
}

private long getReusableId()
{
idContainerLock.lock();
try
{
return idContainer.getReusableId();
}
finally
{
idContainerLock.unlock();
}
}

private long storeLocally( IdAllocation allocation )
{
setHighId( allocation.getHighestIdInUse() + 1 ); // high id is certainly bigger than the highest id in use
Expand All @@ -139,26 +203,8 @@ private IdRange respectingHighId( IdRange idRange )
{
throw new IllegalStateException(
"IdAllocation state is probably corrupted or out of sync with the cluster. " +
"Local highId is " + highId + " and allocation range is " + idRange );
"Local highId is " + highId + " and allocation range is " + idRange );
}
return new IdRange( idRange.getDefragIds(), rangeStart, rangeLength );
}

@Override
public synchronized long getDefragCount()
{
return idContainer.getFreeIdCount();
}

@Override
public synchronized void delete()
{
idContainer.delete();
}

@Override
public synchronized String toString()
{
return getClass().getSimpleName() + "[" + this.idQueue + "]";
}
}

0 comments on commit 47e2af3

Please sign in to comment.