-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fab-1475 make CC fmk allow concurrent invokes
https://jira.hyperledger.org/browse/FAB-1475 Summary ======= With pre-consensus simulation, multiple chains and relaxation by the ledger to simulate versions of chaincode state concurrently, we can now allow chaincode framework to execute invokes concurrently. This CR enables this. This CR enables concurrency basically by removing the FSM states that enforced serialization (so basically all the FSM changes in chaincode/hander.go and chaincode/shim/handler.go). The CR also has a "Chaincode Checker" program which has the potential for much bigger things . the tooling test their chaincodes for consistency . the tooling for stressing the fabric The concurrency enablement was tested with the "ccchecker". Details ======= The submit will basically have 4 things . changes to 3 chaincode framework files handler.go files to enable concurrency . concurrency_test.go to run 100 concurrent invokes followed by 100 concurrent queries . a complete "ccchecker" example framework for testing and validating chaincodes . exports some functions under fabric/peer/chaincode CLI for use by the above ccchecker example framework "ccchecker" comes with a sample "newkeyperinvoke" chaincode that should NEVER fail ledger consistency checks. To test simply follow these steps . vagrant window 1 - start orderer ./orderer . vagrant window 2 - start peer peer node start . vagrant window 3 - bring up chaincode for test cd peer //deploy the chaincode used by ccchecker out of the box peer chaincode deploy -n mycc -p github.com/hyperledger/fabric/examples/ccchecker/chaincodes/newkeyperinvoke -c '{"Args":[""]}' //wait for commit say for about 10 secs and then issue a query to bring the CC up peer chaincode query -n mycc -c '{"Args":["get","a"]}' //verify the chaincode is up docker ps . vagrant window 4 - run test cd examples/ccchecker go build ./ccchecker The above reads from ccchecker.json and executes tests concurrently. Change-Id: I5267b19f03ed10003eb28facf87693525f0dcd1a Signed-off-by: Srinivasan Muralidharan <muralisr@us.ibm.com>
- Loading branch information
Srinivasan Muralidharan
committed
Dec 29, 2016
1 parent
defb65b
commit 5bdca86
Showing
19 changed files
with
1,345 additions
and
291 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
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,141 @@ | ||
/* | ||
Copyright IBM Corp. 2016 All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package chaincode | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/hyperledger/fabric/core/util" | ||
pb "github.com/hyperledger/fabric/protos/peer" | ||
|
||
"golang.org/x/net/context" | ||
) | ||
|
||
//TestExecuteConcurrentInvokes deploys newkeyperinvoke and runs 100 concurrent invokes | ||
//followed by concurrent 100 queries to validate | ||
func TestExecuteConcurrentInvokes(t *testing.T) { | ||
chainID := util.GetTestChainID() | ||
|
||
lis, err := initPeer(chainID) | ||
if err != nil { | ||
t.Fail() | ||
t.Logf("Error creating peer: %s", err) | ||
} | ||
|
||
defer finitPeer(lis, chainID) | ||
|
||
var ctxt = context.Background() | ||
|
||
url := "github.com/hyperledger/fabric/examples/ccchecker/chaincodes/newkeyperinvoke" | ||
|
||
chaincodeID := &pb.ChaincodeID{Name: "nkpi", Path: url} | ||
|
||
args := util.ToChaincodeArgs("init", "") | ||
|
||
spec := &pb.ChaincodeSpec{Type: 1, ChaincodeID: chaincodeID, CtorMsg: &pb.ChaincodeInput{Args: args}} | ||
|
||
cccid := NewCCContext(chainID, "nkpi", "0", "", false, nil) | ||
|
||
defer theChaincodeSupport.Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec}) | ||
|
||
_, err = deploy(ctxt, cccid, spec) | ||
if err != nil { | ||
t.Fail() | ||
t.Logf("Error initializing chaincode %s(%s)", chaincodeID, err) | ||
return | ||
} | ||
|
||
var wg sync.WaitGroup | ||
|
||
//run 100 invokes in parallel | ||
numTrans := 100 | ||
|
||
results := make([][]byte, numTrans) | ||
errs := make([]error, numTrans) | ||
|
||
e := func(inv bool, qnum int) { | ||
defer wg.Done() | ||
|
||
newkey := fmt.Sprintf("%d", qnum) | ||
|
||
var args [][]byte | ||
if inv { | ||
args = util.ToChaincodeArgs("put", newkey, newkey) | ||
} else { | ||
args = util.ToChaincodeArgs("get", newkey) | ||
} | ||
|
||
spec = &pb.ChaincodeSpec{Type: 1, ChaincodeID: chaincodeID, CtorMsg: &pb.ChaincodeInput{Args: args}} | ||
|
||
//start with a new background | ||
_, _, results[qnum], err = invoke(context.Background(), chainID, spec) | ||
|
||
if err != nil { | ||
errs[qnum] = fmt.Errorf("Error executing <%s>: %s", chaincodeID.Name, err) | ||
return | ||
} | ||
} | ||
|
||
wg.Add(numTrans) | ||
|
||
//execute transactions concurrently. | ||
for i := 0; i < numTrans; i++ { | ||
go e(true, i) | ||
} | ||
|
||
wg.Wait() | ||
|
||
for i := 0; i < numTrans; i++ { | ||
if errs[i] != nil { | ||
t.Fail() | ||
t.Logf("Error invoking chaincode iter %d %s(%s)", i, chaincodeID.Name, errs[i]) | ||
} | ||
if results[i] == nil || string(results[i]) != "OK" { | ||
t.Fail() | ||
t.Logf("Error concurrent invoke %d %s", i, chaincodeID.Name) | ||
return | ||
} | ||
} | ||
|
||
wg.Add(numTrans) | ||
|
||
//execute queries concurrently. | ||
for i := 0; i < numTrans; i++ { | ||
go e(false, i) | ||
} | ||
|
||
wg.Wait() | ||
|
||
for i := 0; i < numTrans; i++ { | ||
if errs[i] != nil { | ||
t.Fail() | ||
t.Logf("Error querying chaincode iter %d %s(%s)", i, chaincodeID.Name, errs[i]) | ||
return | ||
} | ||
if results[i] == nil || string(results[i]) != fmt.Sprintf("%d", i) { | ||
t.Fail() | ||
if results[i] == nil { | ||
t.Logf("Error concurrent query %d(%s)", i, chaincodeID.Name) | ||
} else { | ||
t.Logf("Error concurrent query %d(%s, %s, %v)", i, chaincodeID.Name, string(results[i]), results[i]) | ||
} | ||
return | ||
} | ||
} | ||
} |
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
Oops, something went wrong.