Skip to content

Commit

Permalink
FAB-10996 Cleanup runtime launcher interface
Browse files Browse the repository at this point in the history
Presently, the runtime launcher has a dependency on lifecycle that
doesn't actually need to exist.  Instead, the runtime launcher should
simply accept a description of the chaincode to launch.

Change-Id: Ie9703afa8d7567e188f3e0b4cf59301ef29d6890
Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
  • Loading branch information
Jason Yellick committed Jul 16, 2018
1 parent 03aabd7 commit 0e578a8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 181 deletions.
25 changes: 16 additions & 9 deletions core/chaincode/chaincode_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ type Runtime interface {

// Launcher is used to launch chaincode runtimes.
type Launcher interface {
Launch(context context.Context, channelID, chaincodeName string) error
LaunchInit(context context.Context, ccci *lifecycle.ChaincodeContainerInfo) error
Launch(context context.Context, ccci *lifecycle.ChaincodeContainerInfo) error
}

// Lifecycle provides a way to retrieve chaincode definitions and the packages necessary to run them
Expand Down Expand Up @@ -61,6 +60,7 @@ type ChaincodeSupport struct {
HandlerRegistry *HandlerRegistry
Launcher Launcher
sccp sysccprovider.SystemChaincodeProvider
Lifecycle Lifecycle
}

// NewChaincodeSupport creates a new ChaincodeSupport instance.
Expand Down Expand Up @@ -91,6 +91,11 @@ func NewChaincodeSupport(
certGenerator = nil
}

cs.Lifecycle = &lifecycle.Lifecycle{
Executor: cs,
InstantiatedChaincodeStore: chaincodeStore,
}

cs.Runtime = &ContainerRuntime{
CertGenerator: certGenerator,
Processor: processor,
Expand All @@ -108,11 +113,7 @@ func NewChaincodeSupport(
Runtime: cs.Runtime,
Registry: cs.HandlerRegistry,
PackageProvider: packageProvider,
Lifecycle: &lifecycle.Lifecycle{
Executor: cs,
InstantiatedChaincodeStore: chaincodeStore,
},
StartupTimeout: config.StartupTimeout,
StartupTimeout: config.StartupTimeout,
}

return cs
Expand All @@ -132,7 +133,7 @@ func (cs *ChaincodeSupport) LaunchInit(ctx context.Context, cccid *ccprovider.CC
ccci := lifecycle.DeploymentSpecToChaincodeContainerInfo(spec)
ccci.Version = cccid.Version

return cs.Launcher.LaunchInit(ctx, ccci)
return cs.Launcher.Launch(ctx, ccci)
}

// Launch starts executing chaincode if it is not already running. This method
Expand All @@ -155,8 +156,14 @@ func (cs *ChaincodeSupport) Launch(ctx context.Context, cccid *ccprovider.CCCont
// used to support system chaincode. It should really be instantiated with the
// appropriate reference to ChaincodeSupport.
ctx = context.WithValue(ctx, ccintf.GetCCHandlerKey(), cs)
chaincodeName := spec.GetChaincodeSpec().Name()

ccci, err := cs.Lifecycle.ChaincodeContainerInfo(cccid.ChainID, chaincodeName)
if err != nil {
return errors.Wrapf(err, "[channel %s] failed to get chaincode container info for %s", cccid.ChainID, chaincodeName)
}

return cs.Launcher.Launch(ctx, cccid.ChainID, spec.ChaincodeSpec.ChaincodeId.Name)
return cs.Launcher.Launch(ctx, ccci)
}

// Stop stops a chaincode if running.
Expand Down
6 changes: 3 additions & 3 deletions core/chaincode/chaincode_support_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,7 @@ func TestStartAndWaitSuccess(t *testing.T) {
}

//actual test - everythings good
err := launcher.start(context.Background(), ccci)
err := launcher.Launch(context.Background(), ccci)
if err != nil {
t.Fatalf("expected success but failed with error %s", err)
}
Expand Down Expand Up @@ -1080,7 +1080,7 @@ func TestStartAndWaitTimeout(t *testing.T) {
}

//the actual test - timeout 1000 > 500
err := launcher.start(context.Background(), ccci)
err := launcher.Launch(context.Background(), ccci)
if err == nil {
t.Fatalf("expected error but succeeded")
}
Expand Down Expand Up @@ -1112,7 +1112,7 @@ func TestStartAndWaitLaunchError(t *testing.T) {
}

//actual test - container launch gives error
err := launcher.start(context.Background(), ccci)
err := launcher.Launch(context.Background(), ccci)
if err == nil {
t.Fatalf("expected error but succeeded")
}
Expand Down
38 changes: 2 additions & 36 deletions core/chaincode/runtime_launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,46 +31,11 @@ type RuntimeLauncher struct {
Runtime Runtime
Registry LaunchRegistry
PackageProvider PackageProvider
Lifecycle Lifecycle
StartupTimeout time.Duration
}

// LaunchInit launches a container which is not yet defined in the LSCC table
// This is only necessary for the pre v1.3 lifecycle
func (r *RuntimeLauncher) LaunchInit(ctx context.Context, ccci *lifecycle.ChaincodeContainerInfo) error {
err := r.start(ctx, ccci)
if err != nil {
chaincodeLogger.Errorf("start failed: %+v", err)
return err
}

chaincodeLogger.Debug("launch complete")

return nil
}

// Launch chaincode with the appropriate runtime.
func (r *RuntimeLauncher) Launch(ctx context.Context, channelID, chaincodeName string) error {
ccci, err := r.Lifecycle.ChaincodeContainerInfo(channelID, chaincodeName)
if err != nil {
return errors.Wrapf(err, "[channel %s] failed to get chaincode container info for %s", channelID, chaincodeName)
}

err = r.start(ctx, ccci)
if err != nil {
chaincodeLogger.Errorf("start failed: %+v", err)
return err
}

chaincodeLogger.Debug("launch complete")

return nil
}

func (r *RuntimeLauncher) start(ctx context.Context, ccci *lifecycle.ChaincodeContainerInfo) error {
func (r *RuntimeLauncher) Launch(ctx context.Context, ccci *lifecycle.ChaincodeContainerInfo) error {
var codePackage []byte
// Note, it is not actually possible for cds.CodePackage to be non-nil in the real world
// But some of the tests rely on the idea that it might be set.
if ccci.ContainerType != inproccontroller.ContainerType {
var err error
codePackage, err = r.PackageProvider.GetChaincodeCodePackage(ccci.Name, ccci.Version)
Expand Down Expand Up @@ -113,5 +78,6 @@ func (r *RuntimeLauncher) start(ctx context.Context, ccci *lifecycle.ChaincodeCo
return err
}

chaincodeLogger.Debug("launch complete")
return nil
}
Loading

0 comments on commit 0e578a8

Please sign in to comment.