Set sqlite soft_heap_limit via pragma

This commit is contained in:
Shane Kilkelly 2021-06-25 09:46:49 +01:00
parent 048571a3f6
commit 899e781b33
8 changed files with 47 additions and 7 deletions

View file

@ -28,5 +28,6 @@
"highGiB": ${GIT_BRIDGE_SWAPJOB_HIGH_GIB:-256},
"intervalMillis": ${GIT_BRIDGE_SWAPJOB_INTERVAL_MILLIS:-3600000},
"compressionMethod": "${GIT_BRIDGE_SWAPJOB_COMPRESSION_METHOD:-gzip}"
}
},
"sqliteHeapLimitBytes": ${GIT_BRIDGE_SQLITE_HEAP_LIMIT_BYTES:-0}
}

View file

@ -28,5 +28,6 @@
"highGiB": 256,
"intervalMillis": 3600000,
"compressionMethod": "gzip"
}
},
"sqliteHeapLimitBytes": 512000000
}

View file

@ -33,7 +33,8 @@ public class Config implements JSONSource {
Oauth2.asSanitised(config.oauth2),
config.repoStore,
SwapStoreConfig.sanitisedCopy(config.swapStore),
config.swapJob
config.swapJob,
config.sqliteHeapLimitBytes
);
}
@ -52,6 +53,7 @@ public class Config implements JSONSource {
private SwapStoreConfig swapStore;
@Nullable
private SwapJobConfig swapJob;
private int sqliteHeapLimitBytes = 0;
public Config(
String configFilePath
@ -75,7 +77,8 @@ public class Config implements JSONSource {
Oauth2 oauth2,
RepoStoreConfig repoStore,
SwapStoreConfig swapStore,
SwapJobConfig swapJob
SwapJobConfig swapJob,
int sqliteHeapLimitBytes
) {
this.port = port;
this.bindIp = bindIp;
@ -88,6 +91,7 @@ public class Config implements JSONSource {
this.repoStore = repoStore;
this.swapStore = swapStore;
this.swapJob = swapJob;
this.sqliteHeapLimitBytes = sqliteHeapLimitBytes;
}
@Override
@ -124,6 +128,9 @@ public class Config implements JSONSource {
configObject.get("swapJob"),
SwapJobConfig.class
);
if (configObject.has("sqliteHeapLimitBytes")) {
sqliteHeapLimitBytes = getElement(configObject, "sqliteHeapLimitBytes").getAsInt();
}
}
public String getSanitisedString() {
@ -146,6 +153,10 @@ public class Config implements JSONSource {
return rootGitDirectory;
}
public int getSqliteHeapLimitBytes() {
return this.sqliteHeapLimitBytes;
}
public String getAPIBaseURL() {
return apiBaseURL;
}

View file

@ -21,8 +21,14 @@ import java.util.stream.Stream;
public class SqliteDBStore implements DBStore {
private final Connection connection;
private int heapLimitBytes = 0;
public SqliteDBStore(File dbFile) {
this(dbFile, 0);
}
public SqliteDBStore(File dbFile, int heapLimitBytes) {
this.heapLimitBytes = heapLimitBytes;
try {
connection = openConnectionTo(dbFile);
createTables();
@ -144,6 +150,7 @@ public class SqliteDBStore implements DBStore {
private void createTables() {
/* Migrations */
/* We need to eat exceptions from here */
try { doUpdate(new SetSoftHeapLimitPragma(this.heapLimitBytes)); } catch (SQLException ignore) {}
try { doUpdate(new ProjectsAddLastAccessed()); } catch (SQLException ignore) {}
try { doUpdate(new ProjectsAddSwapTime()); } catch (SQLException ignore) {}
try { doUpdate(new ProjectsAddRestoreTime()); } catch (SQLException ignore) {}

View file

@ -0,0 +1,17 @@
package uk.ac.ic.wlgitbridge.bridge.db.sqlite.update.alter;
import uk.ac.ic.wlgitbridge.bridge.db.sqlite.SQLUpdate;
public class SetSoftHeapLimitPragma implements SQLUpdate {
private int heapLimitBytes = 0;
public SetSoftHeapLimitPragma(int heapLimitBytes) {
this.heapLimitBytes = heapLimitBytes;
}
@Override
public String getSQL() {
return "PRAGMA soft_heap_limit="+this.heapLimitBytes+";";
}
}

View file

@ -61,7 +61,8 @@ public class GitBridgeServer {
DBStore dbStore = new SqliteDBStore(
Paths.get(
repoStore.getRootDirectory().getAbsolutePath()
).resolve(".wlgb").resolve("wlgb.db").toFile()
).resolve(".wlgb").resolve("wlgb.db").toFile(),
config.getSqliteHeapLimitBytes()
);
SwapStore swapStore = SwapStore.fromConfig(config.getSwapStore());
SnapshotApi snapshotApi = new NetSnapshotApi();

View file

@ -94,7 +94,8 @@ public class ConfigTest {
" },\n" +
" \"repoStore\": null,\n" +
" \"swapStore\": null,\n" +
" \"swapJob\": null\n" +
" \"swapJob\": null,\n" +
" \"sqliteHeapLimitBytes\": 0\n" +
"}";
assertEquals(
"sanitised config did not hide sensitive fields",

View file

@ -63,7 +63,8 @@ public class BridgeTest {
null,
null,
null,
null),
null,
0),
lock,
repoStore,
dbStore,