Skip to content

Commit 8092131

Browse files
committed
use read only pool - db cleaner
Signed-off-by: Amit Prinz Setter <alphaprinz@gmail.com>
1 parent 2367740 commit 8092131

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

src/sdk/nb.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ interface DBCollection {
766766

767767
validate(doc: object, warn?: 'warn'): object;
768768

769-
executeSQL<T>(query: string, params: Array<any>, options?: { query_name?: string }): Promise<sqlResult<T>>;
769+
executeSQL<T>(query: string, params: Array<any>, options?: { query_name?: string, preferred_pool?: string }): Promise<sqlResult<T>>;
770770
name: any;
771771
}
772772

src/server/bg_services/db_cleaner.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,19 @@ async function clean_md_store(last_date_to_remove) {
6262
}
6363
dbg.log0('DB_CLEANER: checking md-store for documents deleted before', new Date(last_date_to_remove));
6464
const objects_to_remove = await MDStore.instance().find_deleted_objects(last_date_to_remove, config.DB_CLEANER_DOCS_LIMIT);
65-
dbg.log2('DB_CLEANER: list objects:', objects_to_remove);
65+
dbg.log0('DB_CLEANER: list objects:', objects_to_remove);
6666
if (objects_to_remove.length) {
6767
await P.map_with_concurrency(10, objects_to_remove, obj => db_delete_object_parts(obj));
6868
await MDStore.instance().db_delete_objects(objects_to_remove);
6969
}
7070
const blocks_to_remove = await MDStore.instance().find_deleted_blocks(last_date_to_remove, config.DB_CLEANER_DOCS_LIMIT);
71-
dbg.log2('DB_CLEANER: list blocks:', blocks_to_remove);
71+
dbg.log0('DB_CLEANER: list blocks:', blocks_to_remove);
7272
if (blocks_to_remove.length) await MDStore.instance().db_delete_blocks(blocks_to_remove);
7373
const chunks_to_remove = await MDStore.instance().find_deleted_chunks(last_date_to_remove, config.DB_CLEANER_DOCS_LIMIT);
7474
const filtered_chunks = chunks_to_remove.filter(async chunk =>
7575
!(await MDStore.instance().has_any_blocks_for_chunk(chunk)) &&
7676
!(await MDStore.instance().has_any_parts_for_chunk(chunk)));
77-
dbg.log2('DB_CLEANER: list chunks with no blocks and no parts to be removed from DB', filtered_chunks);
77+
dbg.log0('DB_CLEANER: list chunks with no blocks and no parts to be removed from DB', filtered_chunks);
7878
if (filtered_chunks.length) await MDStore.instance().db_delete_chunks(filtered_chunks);
7979
dbg.log0(`DB_CLEANER: removed ${objects_to_remove.length + blocks_to_remove.length + filtered_chunks.length} documents from md-store`);
8080
}

src/server/object_services/md_store.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,7 @@ class MDStore {
11231123
FROM ${this._objects.name}
11241124
WHERE (to_ts(data->>'deleted')<to_ts($1) and data ? 'deleted' and data ? 'reclaimed')
11251125
LIMIT ${query_limit};`;
1126-
const result = await this._objects.executeSQL(query, [new Date(max_delete_time).toISOString()]);
1126+
const result = await this._objects.executeSQL(query, [new Date(max_delete_time).toISOString()], {preferred_pool: 'read_only'});
11271127
return db_client.instance().uniq_ids(result.rows, '_id');
11281128
}
11291129

@@ -1773,21 +1773,26 @@ class MDStore {
17731773
projection: {
17741774
_id: 1,
17751775
deleted: 1
1776-
}
1776+
},
1777+
preferred_pool: 'read_only'
17771778
})
17781779
.then(objects => db_client.instance().uniq_ids(objects, '_id'));
17791780
}
17801781

17811782
has_any_blocks_for_chunk(chunk_id) {
17821783
return this._blocks.findOne({
17831784
chunk: { $eq: chunk_id, $exists: true },
1785+
}, {
1786+
preferred_pool: 'read_only'
17841787
})
17851788
.then(obj => Boolean(obj));
17861789
}
17871790

17881791
has_any_parts_for_chunk(chunk_id) {
17891792
return this._parts.findOne({
17901793
chunk: { $eq: chunk_id, $exists: true },
1794+
}, {
1795+
preferred_pool: 'read_only'
17911796
})
17921797
.then(obj => Boolean(obj));
17931798
}
@@ -2029,7 +2034,8 @@ class MDStore {
20292034
projection: {
20302035
_id: 1,
20312036
deleted: 1
2032-
}
2037+
},
2038+
preferred_pool: 'read_only'
20332039
})
20342040
.then(objects => db_client.instance().uniq_ids(objects, '_id'));
20352041
}

src/util/postgres_client.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,9 @@ function convert_timestamps(where_clause) {
248248

249249
async function _do_query(pg_client, q, transaction_counter) {
250250
query_counter += 1;
251+
252+
dbg.log3("pg_client.options?.host =", pg_client.options?.host, ", q =", q);
253+
251254
const tag = `T${_.padStart(transaction_counter, 8, '0')}|Q${_.padStart(query_counter.toString(), 8, '0')}`;
252255
try {
253256
// dbg.log0(`postgres_client: ${tag}: ${q.text}`, util.inspect(q.values, { depth: 6 }));
@@ -629,6 +632,10 @@ class PostgresTable {
629632
get_pool(key = this.pool_key) {
630633
const pool = this.client.get_pool(key);
631634
if (!pool) {
635+
//if original get_pool was no for the default this.pool_key, try also this.pool_key
636+
if (key && key !== this.pool_key) {
637+
return this.get_pool();
638+
}
632639
throw new Error(`The postgres clients pool ${key} disconnected`);
633640
}
634641
return pool;
@@ -716,13 +723,14 @@ class PostgresTable {
716723
* @param {Array<any>} params
717724
* @param {{
718725
* query_name?: string,
726+
* preferred_pool?: string,
719727
* }} [options = {}]
720728
*
721729
* @returns {Promise<import('pg').QueryResult<T>>}
722730
*/
723731
async executeSQL(query, params, options = {}) {
724732
/** @type {Pool} */
725-
const pool = this.get_pool();
733+
const pool = this.get_pool(options.preferred_pool);
726734
const client = await pool.connect();
727735

728736
const q = {
@@ -926,7 +934,7 @@ class PostgresTable {
926934
query_string += ` OFFSET ${sql_query.offset}`;
927935
}
928936
try {
929-
const res = await this.single_query(query_string);
937+
const res = await this.single_query(query_string, undefined, this.get_pool(options.preferred_pool));
930938
return res.rows.map(row => decode_json(this.schema, row.data));
931939
} catch (err) {
932940
dbg.error('find failed', query, options, query_string, err);
@@ -943,7 +951,7 @@ class PostgresTable {
943951
}
944952
query_string += ' LIMIT 1';
945953
try {
946-
const res = await this.single_query(query_string);
954+
const res = await this.single_query(query_string, undefined, this.get_pool(options.preferred_pool));
947955
if (res.rowCount === 0) return null;
948956
return res.rows.map(row => decode_json(this.schema, row.data))[0];
949957
} catch (err) {

0 commit comments

Comments
 (0)