Skip to content

Commit

Permalink
add datasource pointing to primary instance
Browse files Browse the repository at this point in the history
Signed-off-by: Jesse Nelson <[email protected]>
  • Loading branch information
jnels124 committed Jan 16, 2025
1 parent 498782b commit 6a44cb4
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 10 deletions.
1 change: 1 addition & 0 deletions charts/hedera-mirror/templates/secret-passwords.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ stringData:
HEDERA_MIRROR_REST_DB_HOST: "{{ $dbHost }}"
HEDERA_MIRROR_REST_DB_NAME: "{{ $dbName }}"
HEDERA_MIRROR_REST_DB_PASSWORD: "{{ $restPassword }}"
HEDERA_MIRROR_REST_DB_PRIMARYHOST: "{{ ternary $dbHostPrimary $dbHost .Values.stackgres.enabled }}"
HEDERA_MIRROR_REST_DB_USERNAME: "{{ $restUsername }}"
HEDERA_MIRROR_RESTJAVA_DB_HOST: "{{ $dbHost }}"
HEDERA_MIRROR_RESTJAVA_DB_NAME: "{{ $dbName }}"
Expand Down
1 change: 1 addition & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ value, it is recommended to only populate overridden properties in the custom `a
| `hedera.mirror.rest.db.pool.maxConnections` | 10 | The maximum number of clients the database pool can contain |
| `hedera.mirror.rest.db.pool.statementTimeout` | 20000 | The number of milliseconds to wait before timing out a query statement |
| `hedera.mirror.rest.db.port` | 5432 | The port used to connect to the database |
| `hedera.mirror.rest.db.primaryHost` | "" | Optional IP or hostname used to connect to the primary instance |
| `hedera.mirror.rest.db.sslMode` | DISABLE | The ssl level of protection against Eavesdropping, Man-in-the-middle (MITM) and Impersonation on the db connection. Accepts either DISABLE, ALLOW, PREFER, REQUIRE, VERIFY_CA or VERIFY_FULL. |
| `hedera.mirror.rest.db.tls.ca` | "" | The path to the certificate authority used by the database for secure connections |
| `hedera.mirror.rest.db.tls.cert` | "" | The path to the public key the client should use to securely connect to the database |
Expand Down
1 change: 1 addition & 0 deletions hedera-mirror-rest/config/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ hedera:
maxSize: 100000
db:
host: 127.0.0.1
primaryHost: ""
name: mirror_node
password: mirror_api_pass
pool:
Expand Down
14 changes: 14 additions & 0 deletions hedera-mirror-rest/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,27 @@ if (config.db.tls.enabled) {
};
}

const primaryPoolConfig = {...poolConfig};

const Pool = getPoolClass();
const pool = new Pool(poolConfig);

pool.on('error', (error) => {
logger.error(`error event emitted on pg pool. ${error.stack}`);
});

global.pool = pool;

if (config.db.primaryHost) {
const primaryPool = new Pool(primaryPoolConfig);
primaryPool.on('error', (error) => {
logger.error(`error event emitted on pg primary pool. ${error.stack}`);
});
global.primaryPool = primaryPool;
} else {
global.primaryPool = pool;
}

// Express configuration. Prior to v0.5 all sets should be configured before use or they won't be picked up
const app = addAsync(express());
const {apiPrefix} = constants;
Expand Down
8 changes: 4 additions & 4 deletions hedera-mirror-rest/service/baseService.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ class BaseService {
};
}

async getRows(query, params) {
return (await pool.queryQuietly(query, params)).rows;
async getRows(query, params, dbPool = pool) {
return (await dbPool.queryQuietly(query, params)).rows;
}

async getSingleRow(query, params) {
const rows = await this.getRows(query, params);
async getSingleRow(query, params, dbPool = pool) {
const rows = await this.getRows(query, params, dbPool);
if (_.isEmpty(rows) || rows.length > 1) {
return null;
}
Expand Down
21 changes: 15 additions & 6 deletions hedera-mirror-rest/service/recordFileService.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ class RecordFileService extends BaseService {
* @return {Promise<RecordFile>} recordFile subset
*/
async getRecordFileBlockDetailsFromTimestamp(timestamp) {
const row = await super.getSingleRow(RecordFileService.recordFileBlockDetailsFromTimestampQuery, [timestamp]);
const row = await super.getSingleRow(
RecordFileService.recordFileBlockDetailsFromTimestampQuery,
[timestamp],
primaryPool
);

return _.isNull(row) ? null : new RecordFile(row);
}
Expand All @@ -116,7 +120,7 @@ class RecordFileService extends BaseService {
order by consensus_end ${order}`;
const params = [timestamps, minTimestamp, BigInt(maxTimestamp) + config.query.maxRecordFileCloseIntervalNs];

const rows = await super.getRows(query, params);
const rows = await super.getRows(query, params, primaryPool);

Check failure on line 123 in hedera-mirror-rest/service/recordFileService.js

View workflow job for this annotation

GitHub Actions / Build (rest, v1)

ContractService.getContractLogs tests › Row match

ReferenceError: primaryPool is not defined at RecordFileService.primaryPool [as getRecordFileBlockDetailsFromTimestampArray] (service/recordFileService.js:123:53) at ContractService.getRecordFileBlockDetailsFromTimestampArray [as getContractLogs] (service/contractService.js:437:51) at Object.<anonymous> (__tests__/service/contractService.test.js:759:22)

Check failure on line 123 in hedera-mirror-rest/service/recordFileService.js

View workflow job for this annotation

GitHub Actions / Build (rest, v1)

ContractService.getContractLogs tests › Id match

ReferenceError: primaryPool is not defined at RecordFileService.primaryPool [as getRecordFileBlockDetailsFromTimestampArray] (service/recordFileService.js:123:53) at ContractService.getRecordFileBlockDetailsFromTimestampArray [as getContractLogs] (service/contractService.js:437:51) at Object.<anonymous> (__tests__/service/contractService.test.js:815:22)

Check failure on line 123 in hedera-mirror-rest/service/recordFileService.js

View workflow job for this annotation

GitHub Actions / Build (rest, v1)

ContractService.getContractLogs tests › All params match

ReferenceError: primaryPool is not defined at RecordFileService.primaryPool [as getRecordFileBlockDetailsFromTimestampArray] (service/recordFileService.js:123:53) at ContractService.getRecordFileBlockDetailsFromTimestampArray [as getContractLogs] (service/contractService.js:437:51) at Object.<anonymous> (__tests__/service/contractService.test.js:859:22)

Check failure on line 123 in hedera-mirror-rest/service/recordFileService.js

View workflow job for this annotation

GitHub Actions / Build (rest, v2)

ContractService.getContractLogs tests › Row match

ReferenceError: primaryPool is not defined at RecordFileService.primaryPool [as getRecordFileBlockDetailsFromTimestampArray] (service/recordFileService.js:123:53) at ContractService.getRecordFileBlockDetailsFromTimestampArray [as getContractLogs] (service/contractService.js:437:51) at Object.<anonymous> (__tests__/service/contractService.test.js:759:22)

Check failure on line 123 in hedera-mirror-rest/service/recordFileService.js

View workflow job for this annotation

GitHub Actions / Build (rest, v2)

ContractService.getContractLogs tests › Id match

ReferenceError: primaryPool is not defined at RecordFileService.primaryPool [as getRecordFileBlockDetailsFromTimestampArray] (service/recordFileService.js:123:53) at ContractService.getRecordFileBlockDetailsFromTimestampArray [as getContractLogs] (service/contractService.js:437:51) at Object.<anonymous> (__tests__/service/contractService.test.js:815:22)

Check failure on line 123 in hedera-mirror-rest/service/recordFileService.js

View workflow job for this annotation

GitHub Actions / Build (rest, v2)

ContractService.getContractLogs tests › All params match

ReferenceError: primaryPool is not defined at RecordFileService.primaryPool [as getRecordFileBlockDetailsFromTimestampArray] (service/recordFileService.js:123:53) at ContractService.getRecordFileBlockDetailsFromTimestampArray [as getContractLogs] (service/contractService.js:437:51) at Object.<anonymous> (__tests__/service/contractService.test.js:859:22)

let index = 0;
for (const row of rows) {
Expand Down Expand Up @@ -145,7 +149,7 @@ class RecordFileService extends BaseService {
* @return {Promise<RecordFile>} recordFile subset
*/
async getRecordFileBlockDetailsFromIndex(index) {
const row = await super.getSingleRow(RecordFileService.recordFileBlockDetailsFromIndexQuery, [index]);
const row = await super.getSingleRow(RecordFileService.recordFileBlockDetailsFromIndexQuery, [index], primaryPool);

return _.isNull(row) ? null : new RecordFile(row);
}
Expand All @@ -157,7 +161,11 @@ class RecordFileService extends BaseService {
* @return {Promise<RecordFile>} recordFile subset
*/
async getRecordFileBlockDetailsFromHash(hash) {
const row = await super.getSingleRow(RecordFileService.recordFileBlockDetailsFromHashQuery, [`${hash}%`]);
const row = await super.getSingleRow(
RecordFileService.recordFileBlockDetailsFromHashQuery,
[`${hash}%`],
primaryPool
);

return _.isNull(row) ? null : new RecordFile(row);
}
Expand All @@ -172,7 +180,8 @@ class RecordFileService extends BaseService {
order by ${filters.orderBy} ${filters.order}
limit ${filters.limit}
`;
const rows = await super.getRows(query, params);

const rows = await super.getRows(query, params, primaryPool);
return rows.map((recordFile) => new RecordFile(recordFile));
}

Expand All @@ -189,7 +198,7 @@ class RecordFileService extends BaseService {
}

const query = `${RecordFileService.blocksQuery} where ${whereStatement}`;
const row = await super.getSingleRow(query, params);
const row = await super.getSingleRow(query, params, primaryPool);
return row ? new RecordFile(row) : null;
}

Expand Down

0 comments on commit 6a44cb4

Please sign in to comment.