Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimized the locking on the database #300

Merged
merged 30 commits into from
Mar 29, 2018
Merged

Optimized the locking on the database #300

merged 30 commits into from
Mar 29, 2018

Conversation

AlexandraRoatis
Copy link
Contributor

@AlexandraRoatis AlexandraRoatis commented Mar 28, 2018

  • removed locks from the database implementations
  • added a new database that can add read-write locks on top of any IByteArrayKeyValueDatabase
  • added locks and cleaned up synchronization inside the:
    • TransactionStore
    • AionBlockStore
    • AionRepositoryImpl
  • improved the functionality in the DatabaseFactory
  • removed synchronization from ObjectDataSource
  • removed the inheritance between TransactionStore and ObjectDataSource
  • minor code and documentation cleanup

…inter exceptions when the properties are not specified
@@ -84,14 +88,17 @@ public AionBlock deserialize(byte[] bytes) {
}

public AionBlock getBestBlock() {
lock.readLock().lock();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest a:

lock()
try {
} finally {
 unlock() 
}

Pattern here, just in case any exceptions occur

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going to refer to this as the deferred unlock pattern, from go:

c.mutex.Lock()
defer c.mutex.Unlock()

return bestBlock;
}

public byte[] getBlockHashByNumber(long blockNumber) {
lock.readLock().lock();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest a deferred unlock pattern here

addInternalBlock(block, cummDifficulty, mainChain);
} finally {
lock.writeLock().unlock();
}
}

private void addInternalBlock(AionBlock block, BigInteger cummDifficulty, boolean mainChain) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Document that this method is guarded by lock

@@ -162,10 +189,12 @@ private void addInternalBlock(AionBlock block, BigInteger cummDifficulty, boolea
}

public List<Map.Entry<AionBlock, Map.Entry<BigInteger, Boolean>>> getBlocksByNumber(long number) {
lock.readLock().lock();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest a deferred unlock pattern here

return result;
}

@Override
public AionBlock getChainBlockByNumber(long number) {
lock.readLock().lock();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest a deferred unlock pattern here, would also simplify the code!

@@ -428,19 +419,28 @@ public synchronized void loadAccountState(Address address, Map<Address, AccountS
}

@Override
public synchronized byte[] getRoot() {
return worldState.getRootHash();
public byte[] getRoot() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest deferred unlock pattern

@Override
public boolean isCreatedOnDisk() {
// acquire read lock
lock.readLock().lock();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be simplified to:

lock();
try {
  return database.isCreatedOnDisk();
} finally {
  unlock();
}

}
}

@Override
public void compact() {
// acquire read lock
lock.writeLock().lock();

LOG.info("Compacting " + this.toString() + ".");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor point, not sure if this messages needs to be logged to info, could become noisy


public boolean put(INFO tx) {
lock.writeLock().lock();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest deferred unlock pattern here

return true;
}

public INFO get(byte[] txHash, byte[] blockHash) {
List<INFO> existingInfos = get(txHash);
lock.readLock().lock();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest deferred unlock pattern here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants