Skip to content

Commit a0f7ee5

Browse files
authored
Set up state store if configured in d4all mode (#753)
* Set up state store if configured in d4all mode Signed-off-by: MTRNord <[email protected]> * Ensure the dataPath is set in both the example and harness appservice config Signed-off-by: MTRNord <[email protected]> * Move the SqliteRoomStateBackingStore creation to a static method instead Signed-off-by: MTRNord <[email protected]> * Make the storage path canonicalization less confusing --------- Signed-off-by: MTRNord <[email protected]>
1 parent 04d21a2 commit a0f7ee5

File tree

7 files changed

+60
-12
lines changed

7 files changed

+60
-12
lines changed

src/appservice/AppService.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { Api } from "./Api";
2424
import { IConfig } from "./config/config";
2525
import { AccessControl } from "./AccessControl";
2626
import { AppserviceCommandHandler } from "./bot/AppserviceCommandHandler";
27-
import { SOFTWARE_VERSION } from "../config";
27+
import { getStoragePath, SOFTWARE_VERSION } from "../config";
2828
import { Registry } from "prom-client";
2929
import {
3030
ClientCapabilityFactory,
@@ -36,6 +36,7 @@ import {
3636
ClientsInRoomMap,
3737
DefaultEventDecoder,
3838
EventDecoder,
39+
RoomStateBackingStore,
3940
StandardClientsInRoomMap,
4041
Task,
4142
isError,
@@ -48,6 +49,7 @@ import {
4849
isStringRoomAlias,
4950
isStringRoomID,
5051
} from "@the-draupnir-project/matrix-basic-types";
52+
import { SqliteRoomStateBackingStore } from "../backingstore/better-sqlite3/SqliteRoomStateBackingStore";
5153

5254
const log = new Logger("AppService");
5355
/**
@@ -98,7 +100,8 @@ export class MjolnirAppService {
98100
config: IConfig,
99101
dataStore: DataStore,
100102
eventDecoder: EventDecoder,
101-
registrationFilePath: string
103+
registrationFilePath: string,
104+
backingStore?: RoomStateBackingStore
102105
) {
103106
const bridge = new Bridge({
104107
homeserverUrl: config.homeserver.url,
@@ -143,7 +146,8 @@ export class MjolnirAppService {
143146
const roomStateManagerFactory = new RoomStateManagerFactory(
144147
clientsInRoomMap,
145148
clientProvider,
146-
eventDecoder
149+
eventDecoder,
150+
backingStore
147151
);
148152
const clientCapabilityFactory = new ClientCapabilityFactory(
149153
clientsInRoomMap,
@@ -226,11 +230,17 @@ export class MjolnirAppService {
226230
Logger.configure(config.logging ?? { console: "debug" });
227231
const dataStore = new PgDataStore(config.db.connectionString);
228232
await dataStore.init();
233+
const eventDecoder = DefaultEventDecoder;
234+
const storagePath = getStoragePath(config.dataPath);
235+
const backingStore = config.roomStateBackingStore.enabled
236+
? SqliteRoomStateBackingStore.create(storagePath, eventDecoder)
237+
: undefined;
229238
const service = await MjolnirAppService.makeMjolnirAppService(
230239
config,
231240
dataStore,
232241
DefaultEventDecoder,
233-
registrationFilePath
242+
registrationFilePath,
243+
backingStore
234244
);
235245
// The call to `start` MUST happen last. As it needs the datastore, and the mjolnir manager to be initialized before it can process events from the homeserver.
236246
await service.start(port);

src/appservice/config/config.example.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,9 @@ adminRoom: "#draupnir-admin:localhost:9999"
1616
# This is a web api that the widget connects to in order to interact with the appservice.
1717
webAPI:
1818
port: 9001
19+
20+
# The directory the bot should store various bits of information in
21+
dataPath: "./test/harness/mjolnir-data/"
22+
23+
roomStateBackingStore:
24+
enabled: false

src/appservice/config/config.harness.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ adminRoom: "#draupnir-admin:localhost:9999"
1010

1111
webAPI:
1212
port: 9001
13+
14+
# The directory the bot should store various bits of information in
15+
dataPath: "./test/harness/mjolnir-data/"
16+
17+
roomStateBackingStore:
18+
enabled: false

src/appservice/config/config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ export interface IConfig {
3333
adminRoom: string;
3434
/** configuration for matrix-appservice-bridge's Logger */
3535
logging?: LoggingOpts;
36+
37+
dataPath: string;
38+
39+
// Store room state using sqlite to improve startup time when Synapse responds
40+
// slowly to requests for `/state`.
41+
roomStateBackingStore: {
42+
enabled?: boolean;
43+
};
3644
}
3745

3846
export function read(configPath: string): IConfig {

src/backingstore/better-sqlite3/SqliteRoomStateBackingStore.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
import { BetterSqliteStore, flatTransaction } from "./BetterSqliteStore";
1919
import { jsonReviver } from "../../utils";
2020
import { StringRoomID } from "@the-draupnir-project/matrix-basic-types";
21+
import path from "path";
2122

2223
const log = new Logger("SqliteRoomStateBackingStore");
2324

@@ -68,6 +69,16 @@ export class SqliteRoomStateBackingStore
6869
this.ensureSchema();
6970
}
7071

72+
public static create(
73+
storagePath: string,
74+
eventDecoder: EventDecoder
75+
): SqliteRoomStateBackingStore {
76+
return new SqliteRoomStateBackingStore(
77+
path.join(storagePath, "room-state-backing-store.db"),
78+
eventDecoder
79+
);
80+
}
81+
7182
private updateBackingStore(
7283
revision: RoomStateRevision,
7384
changes: StateChange[]

src/config.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,3 +540,15 @@ export function getUnknownConfigPropertyPaths(config: unknown): string[] {
540540
[]
541541
);
542542
}
543+
544+
/**
545+
* Ensures we have an absolute path for storage
546+
*
547+
* @param dataPath The dataPath from either the bot or appservice configs
548+
* @returns The storagePath used for the databases
549+
*/
550+
export function getStoragePath(dataPath: string) {
551+
return path.isAbsolute(dataPath)
552+
? dataPath
553+
: path.join(__dirname, "../", dataPath);
554+
}

src/index.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
RustSdkCryptoStorageProvider,
2020
} from "matrix-bot-sdk";
2121
import { StoreType } from "@matrix-org/matrix-sdk-crypto-nodejs";
22-
import { configRead as configRead } from "./config";
22+
import { configRead as configRead, getStoragePath } from "./config";
2323
import { initializeSentry, patchMatrixClient } from "./utils";
2424
import { DraupnirBotModeToggle } from "./DraupnirBotMode";
2525
import { SafeMatrixEmitterWrapper } from "matrix-protection-suite-for-matrix-bot-sdk";
@@ -46,9 +46,7 @@ void (async function () {
4646
let bot: DraupnirBotModeToggle | null = null;
4747
let client: MatrixClient;
4848
try {
49-
const storagePath = path.isAbsolute(config.dataPath)
50-
? config.dataPath
51-
: path.join(__dirname, "../", config.dataPath);
49+
const storagePath = getStoragePath(config.dataPath);
5250
const storage = new SimpleFsStorageProvider(
5351
path.join(storagePath, "bot.json")
5452
);
@@ -86,10 +84,7 @@ void (async function () {
8684
patchMatrixClient();
8785
const eventDecoder = DefaultEventDecoder;
8886
const store = config.roomStateBackingStore.enabled
89-
? new SqliteRoomStateBackingStore(
90-
path.join(storagePath, "room-state-backing-store.db"),
91-
eventDecoder
92-
)
87+
? SqliteRoomStateBackingStore.create(storagePath, eventDecoder)
9388
: undefined;
9489
bot = await DraupnirBotModeToggle.create(
9590
client,

0 commit comments

Comments
 (0)