Skip to content

Commit 143d35a

Browse files
author
Jason Yellick
committed
FAB-9729 Make VMCProcess non-package scoped
The chaincode package has already been reworked to reference the VMCProcess function as an interface method. This CR simply modifies the container package to make the VMCProcess function a method of VMController and exposes a constructor for creating the VMController. Change-Id: Ib08e77ece59b30d2564db065197782513c18d6d6 Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
1 parent acf93e7 commit 143d35a

10 files changed

+75
-194
lines changed

core/chaincode/chaincode_support.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/golang/protobuf/proto"
1414
"github.com/hyperledger/fabric/core/common/ccprovider"
1515
"github.com/hyperledger/fabric/core/common/sysccprovider"
16-
"github.com/hyperledger/fabric/core/container"
1716
"github.com/hyperledger/fabric/core/container/ccintf"
1817
pb "github.com/hyperledger/fabric/protos/peer"
1918
"github.com/pkg/errors"
@@ -47,6 +46,7 @@ func NewChaincodeSupport(
4746
certGenerator CertGenerator,
4847
packageProvider PackageProvider,
4948
aclProvider ACLProvider,
49+
processor Processor,
5050
) *ChaincodeSupport {
5151
cs := &ChaincodeSupport{
5252
UserRunsCC: userRunsCC,
@@ -63,7 +63,7 @@ func NewChaincodeSupport(
6363

6464
cs.Runtime = &ContainerRuntime{
6565
CertGenerator: certGenerator,
66-
Processor: ProcessFunc(container.VMCProcess),
66+
Processor: processor,
6767
CACert: caCert,
6868
PeerAddress: peerAddress,
6969
PeerID: config.PeerID,

core/chaincode/chaincode_support_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ func initMockPeer(chainIDs ...string) (*ChaincodeSupport, error) {
177177
certGenerator,
178178
&ccprovider.CCInfoFSImpl{},
179179
aclmgmt.GetACLProvider(),
180+
container.NewVMController(),
180181
)
181182
SideEffectInitialize(chaincodeSupport)
182183
chaincodeSupport.SetSysCCProvider(sccp)

core/chaincode/container_runtime.go

-8
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,6 @@ type Processor interface {
2828
Process(ctxt context.Context, vmtype string, req container.VMCReqIntf) (container.VMCResp, error)
2929
}
3030

31-
// ProcessFunc adapts a function to satisfy the Processor interface.
32-
type ProcessFunc func(ctxt context.Context, vmtype string, req container.VMCReqIntf) (container.VMCResp, error)
33-
34-
// Process processes vm and container requests.
35-
func (v ProcessFunc) Process(ctxt context.Context, vmtype string, req container.VMCReqIntf) (container.VMCResp, error) {
36-
return v(ctxt, vmtype, req)
37-
}
38-
3931
// CertGenerator generates client certificates for chaincode.
4032
type CertGenerator interface {
4133
// Generate returns a certificate and private key and associates

core/chaincode/container_runtime_test.go

-20
Original file line numberDiff line numberDiff line change
@@ -322,23 +322,3 @@ func TestContainerRuntimeStopErrors(t *testing.T) {
322322
assert.NoError(t, err)
323323
}
324324
}
325-
326-
func TestProcessFunc(t *testing.T) {
327-
var c context.Context
328-
var v string
329-
var r container.VMCReqIntf
330-
331-
processFunc := func(ctxt context.Context, vmtype string, req container.VMCReqIntf) (container.VMCResp, error) {
332-
c = ctxt
333-
v = vmtype
334-
r = req
335-
return container.VMCResp{Err: errors.New("response-error")}, errors.New("func-error")
336-
}
337-
338-
resp, err := chaincode.ProcessFunc(processFunc).Process(context.Background(), "vm-type", container.StartContainerReq{})
339-
assert.EqualError(t, resp.Err, "response-error")
340-
assert.EqualError(t, err, "func-error")
341-
assert.Equal(t, c, context.Background())
342-
assert.Equal(t, v, "vm-type")
343-
assert.Equal(t, r, container.StartContainerReq{})
344-
}

core/chaincode/exectransaction_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ func initPeer(chainIDs ...string) (net.Listener, *ChaincodeSupport, func(), erro
125125
certGenerator,
126126
&ccprovider.CCInfoFSImpl{},
127127
aclmgmt.GetACLProvider(),
128+
container.NewVMController(),
128129
)
129130
chaincodeSupport.SetSysCCProvider(sccp)
130131
SideEffectInitialize(chaincodeSupport)

core/chaincode/systemchaincode_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/hyperledger/fabric/core/chaincode/shim"
1919
"github.com/hyperledger/fabric/core/common/ccprovider"
2020
"github.com/hyperledger/fabric/core/common/sysccprovider"
21+
"github.com/hyperledger/fabric/core/container"
2122
"github.com/hyperledger/fabric/core/peer"
2223
"github.com/hyperledger/fabric/core/scc"
2324
pb "github.com/hyperledger/fabric/protos/peer"
@@ -131,6 +132,7 @@ func initSysCCTests() (*oldSysCCInfo, net.Listener, *ChaincodeSupport, error) {
131132
certGenerator,
132133
&ccprovider.CCInfoFSImpl{},
133134
aclmgmt.GetACLProvider(),
135+
container.NewVMController(),
134136
)
135137
pb.RegisterChaincodeSupportServer(grpcServer, chaincodeSupport)
136138

core/container/controller.go

+18-15
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ type VMController struct {
3838

3939
var vmLogger = flogging.MustGetLogger("container")
4040

41-
var vmcontroller = &VMController{
42-
containerLocks: make(map[string]*refCountedLock),
41+
// NewVMController creates a new instance of VMController
42+
func NewVMController() *VMController {
43+
return &VMController{
44+
containerLocks: make(map[string]*refCountedLock),
45+
}
4346
}
4447

4548
//constants for supported containers
@@ -64,37 +67,37 @@ func (vmc *VMController) newVM(typ string) api.VM {
6467

6568
func (vmc *VMController) lockContainer(id string) {
6669
//get the container lock under global lock
67-
vmcontroller.Lock()
70+
vmc.Lock()
6871
var refLck *refCountedLock
6972
var ok bool
70-
if refLck, ok = vmcontroller.containerLocks[id]; !ok {
73+
if refLck, ok = vmc.containerLocks[id]; !ok {
7174
refLck = &refCountedLock{refCount: 1, lock: &sync.RWMutex{}}
72-
vmcontroller.containerLocks[id] = refLck
75+
vmc.containerLocks[id] = refLck
7376
} else {
7477
refLck.refCount++
7578
vmLogger.Debugf("refcount %d (%s)", refLck.refCount, id)
7679
}
77-
vmcontroller.Unlock()
80+
vmc.Unlock()
7881
vmLogger.Debugf("waiting for container(%s) lock", id)
7982
refLck.lock.Lock()
8083
vmLogger.Debugf("got container (%s) lock", id)
8184
}
8285

8386
func (vmc *VMController) unlockContainer(id string) {
84-
vmcontroller.Lock()
85-
if refLck, ok := vmcontroller.containerLocks[id]; ok {
87+
vmc.Lock()
88+
if refLck, ok := vmc.containerLocks[id]; ok {
8689
if refLck.refCount <= 0 {
8790
panic("refcnt <= 0")
8891
}
8992
refLck.lock.Unlock()
9093
if refLck.refCount--; refLck.refCount == 0 {
9194
vmLogger.Debugf("container lock deleted(%s)", id)
92-
delete(vmcontroller.containerLocks, id)
95+
delete(vmc.containerLocks, id)
9396
}
9497
} else {
9598
vmLogger.Debugf("no lock to unlock(%s)!!", id)
9699
}
97-
vmcontroller.Unlock()
100+
vmc.Unlock()
98101
}
99102

100103
//VMCReqIntf - all requests should implement this interface.
@@ -165,16 +168,16 @@ func (si StopContainerReq) getCCID() ccintf.CCID {
165168
return si.CCID
166169
}
167170

168-
//VMCProcess should be used as follows
171+
//Process should be used as follows
169172
// . construct a context
170173
// . construct req of the right type (e.g., CreateImageReq)
171174
// . call it in a go routine
172175
// . process response in the go routing
173176
//context can be cancelled. VMCProcess will try to cancel calling functions if it can
174177
//For instance docker clients api's such as BuildImage are not cancelable.
175178
//In all cases VMCProcess will wait for the called go routine to return
176-
func VMCProcess(ctxt context.Context, vmtype string, req VMCReqIntf) (VMCResp, error) {
177-
v := vmcontroller.newVM(vmtype)
179+
func (vmc *VMController) Process(ctxt context.Context, vmtype string, req VMCReqIntf) (VMCResp, error) {
180+
v := vmc.newVM(vmtype)
178181
if v == nil {
179182
return VMCResp{}, fmt.Errorf("Unknown VM type %s", vmtype)
180183
}
@@ -189,9 +192,9 @@ func VMCProcess(ctxt context.Context, vmtype string, req VMCReqIntf) (VMCResp, e
189192
resp = VMCResp{Err: err}
190193
return
191194
}
192-
vmcontroller.lockContainer(id)
195+
vmc.lockContainer(id)
193196
resp = req.do(ctxt, v)
194-
vmcontroller.unlockContainer(id)
197+
vmc.unlockContainer(id)
195198
}()
196199

197200
select {

0 commit comments

Comments
 (0)