-
Notifications
You must be signed in to change notification settings - Fork 515
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FAB-1108: Sample query handler and tutorial
Change-Id: I52844ab283a9e8d5bd60ccb7cde2dd4e6822cfd2 Signed-off-by: Mark S. Lewis <[email protected]>
- Loading branch information
1 parent
07956d6
commit 309722a
Showing
6 changed files
with
213 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
This tutorial describes how peers are selected to evaluate transactions | ||
that will not then be written to the ledger, which may also be considered | ||
as queries. | ||
|
||
### Query handling strategies | ||
|
||
The SDK provides several selectable strategies for how it should evaluate | ||
transactions on peers in the network. The available strategies are defined | ||
in `DefaultQueryHandlerStrategies`. The desired strategy is (optionally) | ||
specified as an argument to `connect()` on the `Gateway`, and is used for | ||
all transaction evaluations on Contracts obtained from that Gateway | ||
instance. | ||
|
||
If no query handling strategy is specified, `MSPID_SCOPE_SINGLE` is used | ||
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. | ||
|
||
```javascript | ||
const { Gateway, DefaultQueryHandlerStrategies } = require('fabric-network'); | ||
|
||
const connectOptions = { | ||
queryHandlerOptions: { | ||
strategy: DefaultQueryHandlerStrategies.MSPID_SCOPE_SINGLE | ||
} | ||
} | ||
|
||
const gateway = new Gateway(); | ||
await gateway.connect(connectionProfile, connectOptions); | ||
``` | ||
|
||
### Plug-in query handlers | ||
|
||
If behavior is required that is not provided by the default query handling | ||
strategies, it is possible to implement your own query handling. This is | ||
achieved by specifying your own factory function as the query handling | ||
strategy. The factory function should return a *query handler* | ||
object and take one parameter: | ||
1. Blockchain network: `Network` | ||
|
||
The Network provides access to peers on which transactions should be | ||
evaluated. | ||
|
||
```javascript | ||
function createQueryHandler(network) { | ||
/* Your implementation here */ | ||
return new MyQueryHandler(peers); | ||
} | ||
|
||
const connectOptions = { | ||
queryHandlerOptions: { | ||
strategy: createQueryHandler | ||
} | ||
} | ||
|
||
const gateway = new Gateway(); | ||
await gateway.connect(connectionProfile, connectOptions); | ||
``` | ||
|
||
The *query handler* object returned must implement the following functions. | ||
|
||
```javascript | ||
class MyQueryHandler { | ||
/** | ||
* Evaluate the supplied query on appropriate peers. | ||
* @param {Query} query A query object that provides an evaluate() | ||
* function to invoke itself on specified peers. | ||
* @returns {Buffer} Query result. | ||
*/ | ||
async evaluate(query) { /* Your implementation here */ } | ||
} | ||
``` | ||
|
||
For a complete sample plug-in query handler implementation, see [sample-query-handler.ts](https://github.com/hyperledger/fabric-sdk-node/blob/master/test/typescript/integration/network-e2e/sample-query-handler.ts). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
test/typescript/integration/network-e2e/sample-query-handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* Copyright 2018 IBM All Rights Reserved. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
// Sample query handler that will use all queryable peers within the network to evaluate transactions, with preference | ||
// given to peers within the same organization. | ||
|
||
import { | ||
Network, | ||
Query, | ||
QueryHandler, | ||
QueryHandlerFactory, | ||
} from 'fabric-network'; | ||
|
||
import { | ||
ChannelPeer, | ||
} from 'fabric-client'; | ||
|
||
import util = require('util'); | ||
|
||
/** | ||
* Query handler implementation that simply tries all the peers it is given in order until it gets a result. | ||
*/ | ||
class SampleQueryHandler implements QueryHandler { | ||
private peers: ChannelPeer[]; | ||
|
||
constructor(peers: ChannelPeer[]) { | ||
this.peers = peers; | ||
} | ||
|
||
public async evaluate(query: Query): Promise<Buffer> { | ||
const errorMessages: string[] = []; | ||
|
||
for (const peer of this.peers) { | ||
const results = await query.evaluate([peer]); | ||
const result = results[peer.getName()]; | ||
|
||
if (result instanceof Error) { | ||
errorMessages.push(result.message); | ||
} else { | ||
return result; | ||
} | ||
} | ||
|
||
const message = util.format('Evaluate failed with the following errors: %j', errorMessages); | ||
throw new Error(message); | ||
} | ||
} | ||
|
||
function filterQueryablePeers(peers: ChannelPeer[]): ChannelPeer[] { | ||
return peers.filter((peer) => peer.isInRole('chaincodeQuery')); | ||
} | ||
|
||
/** | ||
* Factory function for creating sample query handlers. | ||
* @param {Network} network The network where transactions are to be evaluated. | ||
* @returns {QueryHandler} A query handler implementation. | ||
*/ | ||
const createQueryHandler: QueryHandlerFactory = (network: Network) => { | ||
const channel = network.getChannel(); | ||
const orgPeers = filterQueryablePeers(channel.getPeersForOrg()); | ||
const networkPeers = filterQueryablePeers(channel.getChannelPeers()) | ||
.filter((peer) => !orgPeers.includes(peer)); // Exclude peers already in the orgPeer array | ||
|
||
const allPeers = orgPeers.concat(networkPeers); // Peers in our organization first | ||
return new SampleQueryHandler(allPeers); | ||
}; | ||
|
||
export = createQueryHandler; // Plain JavaScript compatible node module export |