Skip to content

Commit

Permalink
Removed unused lock
Browse files Browse the repository at this point in the history
  • Loading branch information
spoto committed May 1, 2024
1 parent d2d7552 commit dc15455
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ class DiskStore extends AbstractStore<DiskStore> {
* @param toClone the store to clone
*/
private DiskStore(DiskStore toClone) {
super(toClone);

this.dir = toClone.dir;
this.histories = toClone.histories;
this.errors = toClone.errors;
Expand Down Expand Up @@ -157,7 +155,7 @@ public Optional<TransactionRequest<?>> getRequest(TransactionReference reference

@Override
public StoreTransaction<DiskStore> beginTransaction() {
return new DiskStoreTransaction(this, lock);
return new DiskStoreTransaction(this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
import io.hotmoka.stores.StoreException;

public class DiskStoreTransaction extends AbstractStoreTransaction<DiskStore> {

private final DiskStore store;

private final ConcurrentMap<TransactionReference, TransactionRequest<?>> requests = new ConcurrentHashMap<>();
private final ConcurrentMap<TransactionReference, TransactionResponse> responses = new ConcurrentHashMap<>();

Expand All @@ -37,9 +35,7 @@ public class DiskStoreTransaction extends AbstractStoreTransaction<DiskStore> {
*/
private final AtomicReference<StorageReference> manifest = new AtomicReference<>();

public DiskStoreTransaction(DiskStore store, Object lock) {
super(lock);

public DiskStoreTransaction(DiskStore store) {
this.store = store;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,12 @@ public Optional<TransactionRequest<?>> getRequest(TransactionReference reference
*/
byte[] getHash() {
try {
synchronized (lock) {
return isEmpty() ?
new byte[0] : // Tendermint requires an empty array at the beginning, for consensus
// we do not use the info part of the hash, so that the hash
// remains stable when the responses and the histories are stable,
// although the info part has changed for the update of the number of commits
hasherOfHashes.hash(mergeRootsOfTriesWithoutInfo()); // we hash the result into 32 bytes
}
return isEmpty() ?
new byte[0] : // Tendermint requires an empty array at the beginning, for consensus
// we do not use the info part of the hash, so that the hash
// remains stable when the responses and the histories are stable,
// although the info part has changed for the update of the number of commits
hasherOfHashes.hash(mergeRootsOfTriesWithoutInfo()); // we hash the result into 32 bytes
}
catch (StoreException e) {
throw new RuntimeException(e); // TODO
Expand Down Expand Up @@ -149,6 +147,6 @@ protected TendermintStore mkClone(Optional<byte[]> rootOfResponses, Optional<byt

@Override
protected TendermintStoreTransaction mkTransaction(Transaction txn) throws StoreException {
return new TendermintStoreTransaction(this, lock, txn);
return new TendermintStoreTransaction(this, txn);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

public class TendermintStoreTransaction extends AbstractTrieBasedStoreTransaction<TendermintStore> {

protected TendermintStoreTransaction(TendermintStore store, Object lock, Transaction txn) throws StoreException {
super(store, lock, txn);
protected TendermintStoreTransaction(TendermintStore store, Transaction txn) throws StoreException {
super(store, txn);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 Fausto Spoto
Copyright 2024 Fausto Spoto
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -25,26 +25,10 @@
@ThreadSafe
public abstract class AbstractStore<T extends AbstractStore<T>> implements Store<T> {

/**
* The lock for modifications of the store.
*/
protected final Object lock;

/**
* Builds the store for a node.
*/
protected AbstractStore() {
this.lock = new Object();
}

/**
* Creates a clone of the given store.
*
* @param toClone the store to clone
*/
protected AbstractStore(AbstractStore<T> toClone) {
this.lock = toClone.lock;
}
protected AbstractStore() {}

protected abstract T mkClone();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import io.hotmoka.annotations.ThreadSafe;
import io.hotmoka.node.api.requests.InitializationTransactionRequest;
import io.hotmoka.node.api.requests.TransactionRequest;
import io.hotmoka.node.api.responses.GameteCreationTransactionResponse;
Expand All @@ -41,58 +40,48 @@
* its hash is held in the node, if consensus is needed. Stores must be thread-safe, since they can
* be used concurrently for executing more requests.
*/
@ThreadSafe
public abstract class AbstractStoreTransaction<T extends Store<T>> implements StoreTransaction<T> {
private final Object lock;
private final static Logger LOGGER = Logger.getLogger(AbstractStoreTransaction.class.getName());

protected AbstractStoreTransaction(Object lock) {
this.lock = lock;
}
protected AbstractStoreTransaction() {}

@Override
public final void push(TransactionReference reference, TransactionRequest<?> request, TransactionResponse response) throws StoreException {
synchronized (lock) {
if (response instanceof TransactionResponseWithUpdates trwu) {
setRequest(reference, request);
setResponse(reference, response);
expandHistory(reference, trwu);
if (response instanceof TransactionResponseWithUpdates trwu) {
setRequest(reference, request);
setResponse(reference, response);
expandHistory(reference, trwu);

if (response instanceof GameteCreationTransactionResponse gctr)
LOGGER.info(gctr.getGamete() + ": created as gamete");
}
else if (response instanceof InitializationTransactionResponse) {
if (request instanceof InitializationTransactionRequest itr) {
setRequest(reference, request);
setResponse(reference, response);
StorageReference manifest = itr.getManifest();
setManifest(manifest);
LOGGER.info(manifest + ": set as manifest");
LOGGER.info("the node has been initialized");
}
else
throw new StoreException("Trying to initialize the node with a request of class " + request.getClass().getSimpleName());
}
else {
if (response instanceof GameteCreationTransactionResponse gctr)
LOGGER.info(gctr.getGamete() + ": created as gamete");
}
else if (response instanceof InitializationTransactionResponse) {
if (request instanceof InitializationTransactionRequest itr) {
setRequest(reference, request);
setResponse(reference, response);
StorageReference manifest = itr.getManifest();
setManifest(manifest);
LOGGER.info(manifest + ": set as manifest");
LOGGER.info("the node has been initialized");
}
else
throw new StoreException("Trying to initialize the node with a request of class " + request.getClass().getSimpleName());
}
else {
setRequest(reference, request);
setResponse(reference, response);
}
}

@Override
public final void push(TransactionReference reference, TransactionRequest<?> request, String errorMessage) throws StoreException {
synchronized (lock) {
setRequest(reference, request);
setError(reference, errorMessage);
}
setRequest(reference, request);
setError(reference, errorMessage);
}

@Override
public final void replace(TransactionReference reference, TransactionRequest<?> request, TransactionResponse response) throws StoreException {
synchronized (lock) {
setResponse(reference, response);
}
setResponse(reference, response);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,54 +200,44 @@ protected AbstractTrieBasedStore(Path dir) {
}

protected AbstractTrieBasedStore(AbstractTrieBasedStore<T> toClone) {
super(toClone);

this.env = toClone.env;
this.storeOfResponses = toClone.storeOfResponses;
this.storeOfInfo = toClone.storeOfInfo;
this.storeOfErrors = toClone.storeOfErrors;
this.storeOfHistories = toClone.storeOfHistories;
this.storeOfRequests = toClone.storeOfRequests;

synchronized (toClone.lock) {
this.rootOfResponses = toClone.rootOfResponses;
this.rootOfInfo = toClone.rootOfInfo;
this.rootOfErrors = toClone.rootOfErrors;
this.rootOfHistories = toClone.rootOfHistories;
this.rootOfRequests = toClone.rootOfRequests;
this.txn = toClone.txn;
}
this.storeOfHistories = toClone.storeOfHistories;
this.storeOfRequests = toClone.storeOfRequests;
this.rootOfResponses = toClone.rootOfResponses;
this.rootOfInfo = toClone.rootOfInfo;
this.rootOfErrors = toClone.rootOfErrors;
this.rootOfHistories = toClone.rootOfHistories;
this.rootOfRequests = toClone.rootOfRequests;
this.txn = toClone.txn;
}

protected AbstractTrieBasedStore(AbstractTrieBasedStore<T> toClone, Optional<byte[]> rootOfResponses, Optional<byte[]> rootOfInfo, Optional<byte[]> rootOfErrors, Optional<byte[]> rootOfHistories, Optional<byte[]> rootOfRequests) {
super(toClone);

this.env = toClone.env;
this.storeOfResponses = toClone.storeOfResponses;
this.storeOfInfo = toClone.storeOfInfo;
this.storeOfErrors = toClone.storeOfErrors;
this.storeOfHistories = toClone.storeOfHistories;
this.storeOfRequests = toClone.storeOfRequests;

synchronized (toClone.lock) {
this.rootOfResponses = rootOfResponses;
this.rootOfInfo = rootOfInfo;
this.rootOfErrors = rootOfErrors;
this.rootOfHistories = rootOfHistories;
this.rootOfRequests = rootOfRequests;
this.txn = toClone.txn;
}
this.storeOfHistories = toClone.storeOfHistories;
this.storeOfRequests = toClone.storeOfRequests;
this.rootOfResponses = rootOfResponses;
this.rootOfInfo = rootOfInfo;
this.rootOfErrors = rootOfErrors;
this.rootOfHistories = rootOfHistories;
this.rootOfRequests = rootOfRequests;
this.txn = toClone.txn;
}

protected abstract T mkClone(Optional<byte[]> rootOfResponses, Optional<byte[]> rootOfInfo, Optional<byte[]> rootOfErrors, Optional<byte[]> rootOfHistories, Optional<byte[]> rootOfRequests);

@Override
public void close() {
if (txn != null && !txn.isFinished()) {
/*if (txn != null && !txn.isFinished()) {
// store closed with yet uncommitted transactions: we abort them
LOGGER.log(Level.WARNING, "store closed with uncommitted transactions: they are being aborted");
txn.abort();
}
}*/

try {
env.close();
Expand All @@ -261,19 +251,15 @@ public void close() {

@Override
public Optional<TransactionResponse> getResponse(TransactionReference reference) {
synchronized (lock) {
return env.computeInReadonlyTransaction // TODO: recheck
(UncheckFunction.uncheck(txn -> mkTrieOfResponses(txn).get(reference)));
}
return env.computeInReadonlyTransaction // TODO: recheck
(UncheckFunction.uncheck(txn -> mkTrieOfResponses(txn).get(reference)));
}

@Override
public Optional<StorageReference> getManifest() throws StoreException {
try {
synchronized (lock) {
return CheckSupplier.check(TrieException.class, () ->
env.computeInReadonlyTransaction(UncheckFunction.uncheck(txn -> mkTrieOfInfo(txn).getManifest())));
}
return CheckSupplier.check(TrieException.class, () -> env.computeInReadonlyTransaction
(UncheckFunction.uncheck(txn -> mkTrieOfInfo(txn).getManifest())));
}
catch (ExodusException | TrieException e) {
throw new StoreException(e);
Expand All @@ -282,34 +268,28 @@ public Optional<StorageReference> getManifest() throws StoreException {

@Override
public Optional<String> getError(TransactionReference reference) throws StoreException {
synchronized (lock) {
try {
return CheckSupplier.check(TrieException.class, () -> env.computeInReadonlyTransaction
try {
return CheckSupplier.check(TrieException.class, () -> env.computeInReadonlyTransaction
(UncheckFunction.uncheck(txn -> mkTrieOfErrors(txn).get(reference))));
}
catch (TrieException e) {
throw new StoreException(e);
}
}
}
catch (ExodusException | TrieException e) {
throw new StoreException(e);
}
}

@Override
public Optional<TransactionRequest<?>> getRequest(TransactionReference reference) {
synchronized (lock) {
return env.computeInReadonlyTransaction // TODO: recheck
(UncheckFunction.uncheck(txn -> mkTrieOfRequests(txn).get(reference)));
}
return env.computeInReadonlyTransaction // TODO: recheck
(UncheckFunction.uncheck(txn -> mkTrieOfRequests(txn).get(reference)));
}

@Override
public Stream<TransactionReference> getHistory(StorageReference object) throws StoreException {
try {
synchronized (lock) {
return CheckSupplier.check(TrieException.class, () -> env.computeInReadonlyTransaction
(UncheckFunction.uncheck(txn -> mkTrieOfHistories(txn).get(object))).orElse(Stream.empty()));
}
return CheckSupplier.check(TrieException.class, () -> env.computeInReadonlyTransaction
(UncheckFunction.uncheck(txn -> mkTrieOfHistories(txn).get(object))).orElse(Stream.empty()));
}
catch (TrieException e) {
catch (ExodusException | TrieException e) {
throw new StoreException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ public abstract class AbstractTrieBasedStoreTransaction<T extends AbstractTrieBa
*/
private volatile TrieOfRequests trieOfRequests;

protected AbstractTrieBasedStoreTransaction(T store, Object lock, Transaction txn) throws StoreException {
super(lock);

protected AbstractTrieBasedStoreTransaction(T store, Transaction txn) throws StoreException {
this.txn = txn;
this.store = store;
this.trieOfResponses = store.mkTrieOfResponses(txn);
Expand Down

0 comments on commit dc15455

Please sign in to comment.