DB cleaner counts transactions rather than bytes stored. Dev task #55.

This commit is contained in:
akwizgran
2014-01-25 13:39:55 +00:00
parent c67869dac1
commit 905eaa5c69
4 changed files with 30 additions and 27 deletions

View File

@@ -77,6 +77,15 @@ interface Database<T> {
*/ */
void commitTransaction(T txn) throws DbException; void commitTransaction(T txn) throws DbException;
/**
* Returns the number of transactions started since the transaction count
* was last reset.
*/
int getTransactionCount();
/** Resets the transaction count. */
void resetTransactionCount();
/** /**
* Stores a contact associated with the given local and remote pseudonyms, * Stores a contact associated with the given local and remote pseudonyms,
* and returns an ID for the contact. * and returns an ID for the contact.

View File

@@ -4,9 +4,8 @@ import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
import static org.briarproject.db.DatabaseConstants.BYTES_PER_SWEEP; import static org.briarproject.db.DatabaseConstants.BYTES_PER_SWEEP;
import static org.briarproject.db.DatabaseConstants.CRITICAL_FREE_SPACE; import static org.briarproject.db.DatabaseConstants.CRITICAL_FREE_SPACE;
import static org.briarproject.db.DatabaseConstants.MAX_BYTES_BETWEEN_SPACE_CHECKS;
import static org.briarproject.db.DatabaseConstants.MAX_MS_BETWEEN_SPACE_CHECKS;
import static org.briarproject.db.DatabaseConstants.MAX_OFFERED_MESSAGES; import static org.briarproject.db.DatabaseConstants.MAX_OFFERED_MESSAGES;
import static org.briarproject.db.DatabaseConstants.MAX_TRANSACTIONS_BETWEEN_SPACE_CHECKS;
import static org.briarproject.db.DatabaseConstants.MIN_FREE_SPACE; import static org.briarproject.db.DatabaseConstants.MIN_FREE_SPACE;
import java.io.IOException; import java.io.IOException;
@@ -120,10 +119,6 @@ DatabaseCleaner.Callback {
private final Collection<EventListener> listeners = private final Collection<EventListener> listeners =
new CopyOnWriteArrayList<EventListener>(); new CopyOnWriteArrayList<EventListener>();
private final Object spaceLock = new Object();
private long bytesStoredSinceLastCheck = 0; // Locking: spaceLock
private long timeOfLastCheck = 0; // Locking: spaceLock
private final Object openCloseLock = new Object(); private final Object openCloseLock = new Object();
private boolean open = false; // Locking: openCloseLock; private boolean open = false; // Locking: openCloseLock;
private int shutdownHandle = -1; // Locking: openCloseLock; private int shutdownHandle = -1; // Locking: openCloseLock;
@@ -403,10 +398,6 @@ DatabaseCleaner.Callback {
db.addStatus(txn, c, m.getId(), false, false); db.addStatus(txn, c, m.getId(), false, false);
} }
} }
// Count the bytes stored
synchronized(spaceLock) {
bytesStoredSinceLastCheck += m.getSerialised().length;
}
} }
public void addSecrets(Collection<TemporarySecret> secrets) public void addSecrets(Collection<TemporarySecret> secrets)
@@ -1975,14 +1966,9 @@ DatabaseCleaner.Callback {
} }
public boolean shouldCheckFreeSpace() { public boolean shouldCheckFreeSpace() {
synchronized(spaceLock) { if(db.getTransactionCount() > MAX_TRANSACTIONS_BETWEEN_SPACE_CHECKS) {
long now = clock.currentTimeMillis(); db.resetTransactionCount();
if(bytesStoredSinceLastCheck > MAX_BYTES_BETWEEN_SPACE_CHECKS return true;
|| now - timeOfLastCheck > MAX_MS_BETWEEN_SPACE_CHECKS) {
bytesStoredSinceLastCheck = 0;
timeOfLastCheck = now;
return true;
}
} }
return false; return false;
} }

View File

@@ -26,16 +26,12 @@ interface DatabaseConstants {
long CRITICAL_FREE_SPACE = 10 * 1024 * 1024; // 10 MiB long CRITICAL_FREE_SPACE = 10 * 1024 * 1024; // 10 MiB
/** /**
* The amount of free space will be checked whenever this many bytes of * The amount of free space will be checked whenever this many transactions
* messages have been added to the database since the last check. * have been started since the last check.
* <p>
* FIXME: Increase this after implementing BTPv2 (smaller packets)?
*/ */
int MAX_BYTES_BETWEEN_SPACE_CHECKS = 1024 * 1024; // 1 MiB int MAX_TRANSACTIONS_BETWEEN_SPACE_CHECKS = 10;
/**
* The amount of free space will be checked whenever this many milliseconds
* have passed since the last check.
*/
long MAX_MS_BETWEEN_SPACE_CHECKS = 60 * 1000; // 1 min
/** /**
* Up to this many bytes of messages will be expired from the database each * Up to this many bytes of messages will be expired from the database each

View File

@@ -22,6 +22,7 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.briarproject.api.Author; import org.briarproject.api.Author;
@@ -339,6 +340,8 @@ abstract class JdbcDatabase implements Database<Connection> {
private final LinkedList<Connection> connections = private final LinkedList<Connection> connections =
new LinkedList<Connection>(); // Locking: self new LinkedList<Connection>(); // Locking: self
private final AtomicInteger transactionCount = new AtomicInteger(0);
private int openConnections = 0; // Locking: connections private int openConnections = 0; // Locking: connections
private boolean closed = false; // Locking: connections private boolean closed = false; // Locking: connections
@@ -498,6 +501,7 @@ abstract class JdbcDatabase implements Database<Connection> {
} catch(SQLException e) { } catch(SQLException e) {
throw new DbException(e); throw new DbException(e);
} }
transactionCount.incrementAndGet();
return txn; return txn;
} }
@@ -541,6 +545,14 @@ abstract class JdbcDatabase implements Database<Connection> {
} }
} }
public int getTransactionCount() {
return transactionCount.get();
}
public void resetTransactionCount() {
transactionCount.set(0);
}
protected void closeAllConnections() throws SQLException { protected void closeAllConnections() throws SQLException {
boolean interrupted = false; boolean interrupted = false;
synchronized(connections) { synchronized(connections) {