Server: Remove USERS_WITH_REPLICATION env variable

pull/10646/head
Laurent Cozic 2024-06-19 23:34:00 +01:00
parent b4ef5abb88
commit 9eb4944614
8 changed files with 8 additions and 61 deletions

View File

@ -15,7 +15,6 @@
# SLAVE_POSTGRES_USER=joplin # SLAVE_POSTGRES_USER=joplin
# SLAVE_POSTGRES_PORT=5433 # SLAVE_POSTGRES_PORT=5433
# SLAVE_POSTGRES_HOST=localhost # SLAVE_POSTGRES_HOST=localhost
# USERS_WITH_REPLICATION=ID1,ID2,...
version: '2' version: '2'

View File

@ -336,7 +336,6 @@ async function main() {
if (slaveConnectionCheck) { if (slaveConnectionCheck) {
printConnectionCheckInfo(slaveConnectionCheck); printConnectionCheckInfo(slaveConnectionCheck);
appLogger().info(`Users with replication: ${config().USERS_WITH_REPLICATION}`);
} else { } else {
appLogger().info('Not using database replication...'); appLogger().info('Not using database replication...');
} }

View File

@ -100,7 +100,6 @@ describe('db.replication', () => {
const { session, user } = await createUserAndSession(1, true); const { session, user } = await createUserAndSession(1, true);
const changeModel = models().change(); const changeModel = models().change();
changeModel.usersWithReplication_ = [user.id];
const folder = { const folder = {
id: '000000000000000000000000000000F1', id: '000000000000000000000000000000F1',

View File

@ -22,7 +22,6 @@ const defaultEnvValues: EnvVariables = {
ERROR_STACK_TRACES: false, ERROR_STACK_TRACES: false,
COOKIES_SECURE: false, COOKIES_SECURE: false,
RUNNING_IN_DOCKER: false, RUNNING_IN_DOCKER: false,
USERS_WITH_REPLICATION: '', // Temporary
HEARTBEAT_MESSAGE_SCHEDULE: '* * * * *', HEARTBEAT_MESSAGE_SCHEDULE: '* * * * *',
// The admin panel is accessible only if this is an admin instance. // The admin panel is accessible only if this is an admin instance.
@ -152,7 +151,6 @@ export interface EnvVariables {
ERROR_STACK_TRACES: boolean; ERROR_STACK_TRACES: boolean;
COOKIES_SECURE: boolean; COOKIES_SECURE: boolean;
RUNNING_IN_DOCKER: boolean; RUNNING_IN_DOCKER: boolean;
USERS_WITH_REPLICATION: string;
HEARTBEAT_MESSAGE_SCHEDULE: string; HEARTBEAT_MESSAGE_SCHEDULE: string;
MAX_TIME_DRIFT: number; MAX_TIME_DRIFT: number;

View File

@ -1,30 +0,0 @@
import { beforeAllDb, afterAllTests, beforeEachDb, models } from '../utils/testing/testUtils';
describe('BaseModel', () => {
beforeAll(async () => {
await beforeAllDb('BaseModel');
});
afterAll(async () => {
await afterAllTests();
});
beforeEach(async () => {
await beforeEachDb();
});
test('should check if a user has replication', async () => {
// cSpell:disable
const itemModel = models().item();
itemModel.usersWithReplication_ = ['A', 'B', 'EYE1m66mGmA01sDDDDKE19'];
expect(itemModel.isUserWithReplication('AAAAAAAAAAAA')).toBe(true);
expect(itemModel.isUserWithReplication('AAAAAAAAAAAAEEEEEEE')).toBe(true);
expect(itemModel.isUserWithReplication('bbbbbbbb')).toBe(false);
expect(itemModel.isUserWithReplication('BBBBBBBBBB')).toBe(true);
expect(itemModel.isUserWithReplication('')).toBe(false);
expect(itemModel.isUserWithReplication('EYE1m66mGmA01sDDDDKE19')).toBe(true);
// cSpell:enable
});
});

View File

@ -69,14 +69,12 @@ export default abstract class BaseModel<T> {
private modelFactory_: NewModelFactoryHandler; private modelFactory_: NewModelFactoryHandler;
private config_: Config; private config_: Config;
private savePoints_: SavePoint[] = []; private savePoints_: SavePoint[] = [];
public usersWithReplication_: string[] = [];
public constructor(db: DbConnection, dbSlave: DbConnection, modelFactory: NewModelFactoryHandler, config: Config) { public constructor(db: DbConnection, dbSlave: DbConnection, modelFactory: NewModelFactoryHandler, config: Config) {
this.db_ = db; this.db_ = db;
this.dbSlave_ = dbSlave; this.dbSlave_ = dbSlave;
this.modelFactory_ = modelFactory; this.modelFactory_ = modelFactory;
this.config_ = config; this.config_ = config;
this.usersWithReplication_ = config.USERS_WITH_REPLICATION ? config.USERS_WITH_REPLICATION.split(',') : [];
this.transactionHandler_ = new TransactionHandler(db); this.transactionHandler_ = new TransactionHandler(db);
} }
@ -117,24 +115,8 @@ export default abstract class BaseModel<T> {
return this.db_; return this.db_;
} }
public isUserWithReplication(userId: Uuid) { public get dbSlave(): DbConnection {
if (!userId) return false; return this.dbSlave_;
for (const id of this.usersWithReplication_) {
if (id === userId) return true;
if (id.length < 22 && userId.startsWith(id)) return true;
}
return false;
}
public dbSlave(userId: Uuid = ''): DbConnection {
if (this.isUserWithReplication(userId)) {
logger.info(`Using slave database for user: ${userId}`);
return this.dbSlave_;
}
return this.db_;
} }
protected get defaultFields(): string[] { protected get defaultFields(): string[] {

View File

@ -199,8 +199,8 @@ export default class ChangeModel extends BaseModel<Change> {
if (!doCountQuery) { if (!doCountQuery) {
finalParams.push(limit); finalParams.push(limit);
if (isPostgres(this.dbSlave(userId))) { if (isPostgres(this.dbSlave)) {
query = this.dbSlave(userId).raw(` query = this.dbSlave.raw(`
WITH cte1 AS MATERIALIZED ( WITH cte1 AS MATERIALIZED (
${subQuery1} ${subQuery1}
) )
@ -214,7 +214,7 @@ export default class ChangeModel extends BaseModel<Change> {
LIMIT ? LIMIT ?
`, finalParams); `, finalParams);
} else { } else {
query = this.dbSlave(userId).raw(` query = this.dbSlave.raw(`
SELECT ${fieldsSql} FROM (${subQuery1}) as sub1 SELECT ${fieldsSql} FROM (${subQuery1}) as sub1
UNION ALL UNION ALL
SELECT ${fieldsSql} FROM (${subQuery2}) as sub2 SELECT ${fieldsSql} FROM (${subQuery2}) as sub2
@ -223,7 +223,7 @@ export default class ChangeModel extends BaseModel<Change> {
`, finalParams); `, finalParams);
} }
} else { } else {
query = this.dbSlave(userId).raw(` query = this.dbSlave.raw(`
SELECT count(*) as total SELECT count(*) as total
FROM ( FROM (
(${subQuery1}) (${subQuery1})

View File

@ -102,7 +102,7 @@ export default class ItemModel extends BaseModel<Item> {
let driver = ItemModel.storageDrivers_.get(config); let driver = ItemModel.storageDrivers_.get(config);
if (!driver) { if (!driver) {
driver = await loadStorageDriver(config, this.db, this.dbSlave()); driver = await loadStorageDriver(config, this.db, this.dbSlave);
ItemModel.storageDrivers_.set(config, driver); ItemModel.storageDrivers_.set(config, driver);
} }
@ -331,7 +331,7 @@ export default class ItemModel extends BaseModel<Item> {
let fromDriver: StorageDriverBase = drivers[item.content_storage_id]; let fromDriver: StorageDriverBase = drivers[item.content_storage_id];
if (!fromDriver) { if (!fromDriver) {
fromDriver = await loadStorageDriver(item.content_storage_id, this.db, this.dbSlave()); fromDriver = await loadStorageDriver(item.content_storage_id, this.db, this.dbSlave);
drivers[item.content_storage_id] = fromDriver; drivers[item.content_storage_id] = fromDriver;
} }