Skip to content

Commit

Permalink
FABN-1668 Add query timeout setting (#380)
Browse files Browse the repository at this point in the history
Add a Query Timeout and not use the commit timeout

Signed-off-by: Bret Harrison <beharrison@nc.rr.com>
  • Loading branch information
harrisob authored Nov 24, 2020
1 parent a788705 commit 6fe0b58
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 12 deletions.
4 changes: 4 additions & 0 deletions docs/tutorials/query-peers.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ by default. This will evaluate all transactions on the first peer from
which is can obtain a response, and only switch to another peer if this
peer fails.

The default query timeout may be changed along with the query strategy.
This value will be in seconds.

```javascript
const { Gateway, DefaultQueryHandlerStrategies } = require('fabric-network');

const connectOptions = {
queryHandlerOptions: {
timeout: 60,
strategy: DefaultQueryHandlerStrategies.MSPID_SCOPE_SINGLE
}
}
Expand Down
9 changes: 9 additions & 0 deletions fabric-network/lib/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ class Contract {
return this.gateway.getOptions().eventHandlerOptions;
}

/**
* Get query handler options specified by the user when creating the gateway.
* @private
* @returns {Object} Query handler options.
*/
getQueryHandlerOptions() {
return this.gateway.getOptions().queryHandlerOptions;
}

/**
* Create an object representing a specific invocation of a transaction
* function implemented by this contract, and provides more control over
Expand Down
5 changes: 4 additions & 1 deletion fabric-network/lib/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ const logger = require('./logger').getLogger('Gateway');

/**
* @typedef {Object} Gateway~DefaultQueryHandlerOptions
* @memberof module:fabric-network
* @memberof module:fabric-
* @property {number} [timeout = 30] The timeout period in seconds to wait for the query operation to
* complete.
* @property {module:fabric-network.Gateway~QueryHandlerFactory} [strategy=MSPID_SCOPE_SINGLE] Query handling strategy
* used to evaluate queries. The default is [MSPID_SCOPE_SINGLE]{@link module:fabric-network.DefaultQueryHandlerStrategies}.
*/
Expand Down Expand Up @@ -147,6 +149,7 @@ class Gateway {
// default options
this.options = {
queryHandlerOptions: {
timeout: 30, // 30 seconds
strategy: QueryStrategies.MSPID_SCOPE_SINGLE
},
eventHandlerOptions: {
Expand Down
8 changes: 3 additions & 5 deletions fabric-network/lib/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,9 @@ class Transaction {
const channel = this._contract.getNetwork().getChannel();
const request = this._buildRequest(args);

const commitTimeout = this._contract.getEventHandlerOptions().commitTimeout * 1000; // in ms
const timeout = this._contract.gateway.getClient().getConfigSetting('request-timeout', commitTimeout);
if (timeout < commitTimeout) {
request.request_timeout = commitTimeout;
}
// queryHandlerOption.timeout default 30 seconds
let timeout = this._contract.getQueryHandlerOptions().timeout ? this._contract.getQueryHandlerOptions().timeout : 30;
request.request_timeout = timeout * 1000; // in ms

const query = new Query(channel, request);

Expand Down
8 changes: 8 additions & 0 deletions fabric-network/test/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ describe('Contract', () => {
});
});

describe('#getQuertHandlerOptions', () => {
it('returns query handler options from the gateway', () => {
const result = contract.getQueryHandlerOptions();
const expected = mockGateway.getOptions().queryHandlerOptions;
result.should.deep.equal(expected);
});
});

describe('#createTransaction', () => {
it('returns a transaction with only a name', () => {
const name = 'name';
Expand Down
9 changes: 9 additions & 0 deletions fabric-network/test/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ describe('Gateway', () => {
commitTimeout: 300, // 5 minutes
strategy: strategy
},
queryHandlerOptions: {
timeout: 70,
strategy: strategy
},
discovery: {
enabled: true,
asLocalhost: true
Expand All @@ -157,6 +161,10 @@ describe('Gateway', () => {
commitTimeout: 300, // 5 minutes
strategy: strategy
},
queryHandlerOptions: {
timeout: 70,
strategy: strategy
},
discovery: {
enabled: true,
asLocalhost: false
Expand Down Expand Up @@ -353,6 +361,7 @@ describe('Gateway', () => {
wallet: mockWallet,
identity: 'admin',
queryHandlerOptions: {
timeout: 30,
strategy: QueryStrategies.MSPID_SCOPE_SINGLE
}
};
Expand Down
1 change: 1 addition & 0 deletions fabric-network/test/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ describe('Network', () => {
strategy: EventStrategies.MSPID_SCOPE_ALLFORTX
},
queryHandlerOptions: {
timeout: 30,
strategy: (theNetwork) => {
stubQueryHandler.network = theNetwork;
return stubQueryHandler;
Expand Down
14 changes: 8 additions & 6 deletions fabric-network/test/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ describe('Transaction', () => {

stubContract.getChaincodeId.returns(chaincodeId);
stubContract.getEventHandlerOptions.returns({commitTimeout: 418});
stubContract.getQueryHandlerOptions.returns({timeout: 30});

const mockClient = sinon.createStubInstance(Client);
const mockGateway = sinon.createStubInstance(Gateway);
Expand Down Expand Up @@ -433,8 +434,8 @@ describe('Transaction', () => {
return expect(promise).to.be.rejectedWith('Transaction has already been invoked');
});

it('builds correct request for invocation with long timeout', async () => {
stubContract.getEventHandlerOptions.returns({commitTimeout: 999});
it('builds correct request for invocation with non default timeout', async () => {
stubContract.getQueryHandlerOptions.returns({timeout: 999});

await transaction.evaluate();

Expand All @@ -444,13 +445,14 @@ describe('Transaction', () => {
});
});

it('builds correct request for invocation with short timeout', async () => {
stubContract.getEventHandlerOptions.returns({commitTimeout: 3});

it('builds correct request for invocation with default timeout', async () => {
stubContract.getQueryHandlerOptions.returns({});
await transaction.evaluate();

const query = stubQueryHandler.evaluate.lastArg;
expect(query._request.request_timeout).to.be.undefined;
expect(query._request).to.deep.include({
request_timeout: 30000
});
});

});
Expand Down
1 change: 1 addition & 0 deletions fabric-network/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export interface TxEventHandler {
}

export interface DefaultQueryHandlerOptions {
timeout?: number;
strategy?: QueryHandlerFactory;
}

Expand Down

0 comments on commit 6fe0b58

Please sign in to comment.