Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add datasource to rest for primary sql instance #10153

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }}"
xin-hedera marked this conversation as resolved.
Show resolved Hide resolved
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/__tests__/integrationDbOps.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ const createPool = async () => {
password: readOnlyPassword,
user: readOnlyUser,
});
global.primaryPool = global.pool;
};

/**
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: ""
steven-sheehy marked this conversation as resolved.
Show resolved Hide resolved
name: mirror_node
password: mirror_api_pass
pool:
Expand Down
15 changes: 15 additions & 0 deletions hedera-mirror-rest/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,26 @@

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 primaryPoolConfig = {...poolConfig};
primaryPoolConfig.host = config.db.primaryHost;

Check warning on line 96 in hedera-mirror-rest/server.js

View check run for this annotation

Codecov / codecov/patch

hedera-mirror-rest/server.js#L95-L96

Added lines #L95 - L96 were not covered by tests

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

Check warning on line 100 in hedera-mirror-rest/server.js

View check run for this annotation

Codecov / codecov/patch

hedera-mirror-rest/server.js#L98-L100

Added lines #L98 - L100 were not covered by tests
steven-sheehy marked this conversation as resolved.
Show resolved Hide resolved
steven-sheehy marked this conversation as resolved.
Show resolved Hide resolved
});
global.primaryPool = primaryPool;

Check warning on line 102 in hedera-mirror-rest/server.js

View check run for this annotation

Codecov / codecov/patch

hedera-mirror-rest/server.js#L102

Added line #L102 was not covered by tests
} 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) {
xin-hedera marked this conversation as resolved.
Show resolved Hide resolved
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);

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
Loading