fabric
fabric copied to clipboard
Optimize chaincode builds: lazy build on peer startup, eager build on explicit install
Current Status
Currently, the Fabric peer eagerly builds chaincode images for all installed chaincode packages, including versions that are obsolete or never used.
Specifically:
- During peer startup,
lifecycleCache.InitializeLocalChaincodes()loads every chaincode package previously installed on the peer (peer lifecycle chaincode install). - For each installed package, the lifecycle cache immediately enqueues a build request via
ChaincodeCustodian.NotifyInstalled(). - As a result, if a peer has accumulated many historical versions (e.g., 20 smart contracts × 50 versions = ~1000 packages, or more than 500 chaincode
.tar.gzarchives), it will attempt to build all of them at startup, even if only the latest committed version (e.g., v50 or v51) is actually used on any channel. All older versions (v1–v49) are built unnecessarily. - After an explicit
peer lifecycle chaincode installcommand, the build is also triggered immediately. - Additionally, Fabric has no standard built-in mechanism to safely clean up installed chaincode packages and their built container images for versions that are no longer committed, approved, or used on any channel. This leads to unnecessary disk space consumption over time.
Expected
- On peer startup: Do not build chaincodes for all previously installed packages. Defer the build until the chaincode is actually needed (lazy build on first use). The trigger should be the first real execution (endorse/proposal) that requires launching that chaincode via
RuntimeLauncher.Launch(). - On explicit
peer lifecycle chaincode install: Trigger the chaincode build immediately (eager build). This preserves the current predictable behavior and allows operators to pre-build if desired. - On first use after peer restart: If the chaincode container image was already built previously and still exists locally, it must be reused (no full rebuild). A full rebuild should only occur if the image is missing (e.g., was removed by cleanup/prune).
- Overall behavior: Reduce peer startup time and resource consumption in environments with many historical chaincode versions, while preserving correctness and deterministic chaincode execution.
Solution
Propose a minimal, backward-compatible change in the lifecycle cache to distinguish between bulk loading at startup and explicit install:
- In
core/chaincode/lifecycle/cache.go, inhandleChaincodeInstalledWhileLocked(): CallchaincodeCustodian.NotifyInstalled(packageID)only wheninitializing == false(i.e., the package is installed explicitly viapeer lifecycle chaincode install). Wheninitializing == true(called fromInitializeLocalChaincodes()during peer startup), skip enqueuing the build. In that case, the chaincode will be built lazily on first use viaRuntimeLauncher.Launch(). - Reuse the existing flag: The
initializing boolparameter exists exactly for this purpose.InitializeLocalChaincodes()passestrue,HandleChaincodeInstalled()passesfalse. - Update unit tests in
core/chaincode/lifecycle/cache_test.goto reflect the new semantics (build deferred at startup/init time, but still triggered immediately on explicit install). Proposed code change:
if !ok {
localChaincode = &LocalChaincode{
References: map[string]map[string]*CachedChaincodeDefinition{},
}
c.localChaincodes[hashOfCCHash] = localChaincode
if !initializing {
c.chaincodeCustodian.NotifyInstalled(packageID)
}
// If initializing (peer startup), defer build to first actual launch.
}
This change provides the desired optimization: explicit install remains eager, peer startup becomes lazy. It does not affect approve/commit flows and fully preserves correctness (images are built on demand, and previously built images are reused).
Please let us know if you plan to work on this.
No response