fabric icon indicating copy to clipboard operation
fabric copied to clipboard

Optimize chaincode builds: lazy build on peer startup, eager build on explicit install

Open TaoGunner opened this issue 2 months ago • 0 comments

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.gz archives), 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 install command, 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:

  1. In core/chaincode/lifecycle/cache.go, in handleChaincodeInstalledWhileLocked(): Call chaincodeCustodian.NotifyInstalled(packageID) only when initializing == false (i.e., the package is installed explicitly via peer lifecycle chaincode install). When initializing == true (called from InitializeLocalChaincodes() during peer startup), skip enqueuing the build. In that case, the chaincode will be built lazily on first use via RuntimeLauncher.Launch().
  2. Reuse the existing flag: The initializing bool parameter exists exactly for this purpose. InitializeLocalChaincodes() passes true, HandleChaincodeInstalled() passes false.
  3. Update unit tests in core/chaincode/lifecycle/cache_test.go to 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

TaoGunner avatar Oct 02 '26 14:10 TaoGunner