Skip to content

lifecycle: defer chaincode build during peer startup initialization - #5612

Merged
pfi79 merged 2 commits into
hyperledger:mainfrom
shubhamrai9122-creator:optimize-chaincode-startup-build
Dec 4, 2026
Merged

pfi79 merged 2 commits into
hyperledger:mainfrom
shubhamrai9122-creator:optimize-chaincode-startup-build

Conversation

@shubhamrai9122-creator

@shubhamrai9122-creator shubhamrai9122-creator commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #5599

Currently, when the Fabric peer starts up, lifecycleCache.InitializeLocalChaincodes() loads all chaincode packages previously installed on the peer. For each installed package, handleChaincodeInstalledWhileLocked() unconditionally called c.chaincodeCustodian.NotifyInstalled(packageID). On peers that have accumulated many historical chaincode packages/versions across iterations, this causes the custodian to eagerly enqueue builds for every historical version during startup, consuming significant disk space and CPU even if those versions are no longer committed or approved on any channel.

Solution

  • In core/chaincode/lifecycle/cache.go, in handleChaincodeInstalledWhileLocked(): only enqueue c.chaincodeCustodian.NotifyInstalled(packageID) when !initializing.
  • When initializing == true (peer startup via InitializeLocalChaincodes()), the eager build enqueue is skipped. The chaincode image is instead built lazily when first launched via RuntimeLauncher.Launch().
  • When initializing == false (explicit install via peer lifecycle chaincode install), the eager build continues to be triggered immediately, preserving current operator expectations.
  • Added unit tests in core/chaincode/lifecycle/cache_test.go: InitializeLocalChaincodes does not enqueue build or launch work for an installed package that is not yet in the local cache, and HandleChaincodeInstalled still builds a newly installed package eagerly.

Scope

This PR stops startup from building installed packages that no chaincode definition references (for example, older versions). InitializeLocalChaincodes() runs before any channel is initialized, so no package is linked to a definition at that point. Approved+installed chaincodes are still built and launched when their channels initialize, through update(), as before. Deferring those until first invoke would change behaviour for chaincodes in use and is left for a separate change.

All 407 unit tests in core/chaincode/lifecycle pass cleanly.

@shubhamrai9122-creator
shubhamrai9122-creator requested a review from a team as a code owner October 10, 2026 17:28
@mergify

mergify Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

This pull request is merged.

@pfi79 pfi79 closed this Nov 20, 2026
@pfi79 pfi79 reopened this Nov 20, 2026
@pfi79

pfi79 commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

please make sure that the DCO is checked.

@shubhamrai9122-creator

Copy link
Copy Markdown
Contributor Author

Hi @pfi79, thanks for checking! Yes, the commit has been signed off with DCO (Signed-off-by: shubhamrai9122-creator <shubhamrai9122@gmail.com>), and all CI checks (including Basic Checks and integration tests) have passed successfully.

@pfi79

pfi79 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Hi @pfi79, thanks for checking! Yes, the commit has been signed off with DCO (Signed-off-by: shubhamrai9122-creator <shubhamrai9122@gmail.com>), and all CI checks (including Basic Checks and integration tests) have passed successfully.

image That's how I see it now. I don't know why it shows that way. But this is a problem.

@shubhamrai9122-creator

Copy link
Copy Markdown
Contributor Author

Thanks @pfi79! The commit is signed off (Signed-off-by: shubhamrai9122-creator <shubhamrai9122@gmail.com>, matching the commit author), but the DCO check never reported on this PR, so it doesn't appear in the checks list. Could you re-trigger it, or would you like me to force-push the same commit to kick it off?

@pfi79

pfi79 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Thanks @pfi79! The commit is signed off (Signed-off-by: shubhamrai9122-creator <shubhamrai9122@gmail.com>, matching the commit author), but the DCO check never reported on this PR, so it doesn't appear in the checks list. Could you re-trigger it, or would you like me to force-push the same commit to kick it off?

make rebase

@shubhamrai9122-creator
shubhamrai9122-creator force-pushed the optimize-chaincode-startup-build branch from 493d8f6 to 2b0866b Compare November 27, 2026 05:24
When a peer starts up with many historical chaincode packages,
lifecycleCache.InitializeLocalChaincodes() loads each installed package.
Previously, handleChaincodeInstalledWhileLocked() unconditionally called
c.chaincodeCustodian.NotifyInstalled(packageID), triggering eager builds
for all historical versions even if they were never approved or committed
on any channel.

This commit updates handleChaincodeInstalledWhileLocked to invoke
c.chaincodeCustodian.NotifyInstalled(packageID) only when initializing is
false (i.e., explicit install via 'peer lifecycle chaincode install').
During startup initialization (initializing is true), builds are deferred
until first actual launch via RuntimeLauncher.Launch().

Fixes hyperledger#5599

Signed-off-by: shubhamrai9122-creator <shubhamrai9122@gmail.com>
@shubhamrai9122-creator
shubhamrai9122-creator force-pushed the optimize-chaincode-startup-build branch from 2b0866b to 38a2f57 Compare November 27, 2026 17:07
@shubhamrai9122-creator

Copy link
Copy Markdown
Contributor Author

Rebased onto main and force-pushed. The DCO check should run now.

@pfi79 pfi79 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To assess whether this change is needed, please consider the inline comments.

Comment thread core/chaincode/lifecycle/cache_test.go Outdated
Expect(fakeParser.ParseCallCount()).To(Equal(0))
})

It("does not enqueue build requests with the custodian during initialization", func() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Major: The new test does not exercise the changed startup path

Scenario
This test runs InitializeLocalChaincodes() in a setup where the package ID is already present in localChaincodes (populated by the outer BeforeEach). In that case, handleChaincodeInstalledWhileLocked takes the ok == true path and never enters the modified if !ok { ... } branch. The test only asserts that fakeBuilder.BuildCallCount stays at 0, so it can pass even with the old behavior.

Why it matters
The review/CI signal suggests the reported startup-build regression is covered, but the changed behavior is not actually pinned by this test.

How to verify/fix
Add a test where the installed package ID is not present in localChaincodes before InitializeLocalChaincodes() runs. After initialization, start the custodian worker with a fake builder/launcher/build registry and assert that no build chore is enqueued. The test should fail on the old behavior and pass with this PR. It may also be useful to assert that no unexpected runnable/launch work is enqueued for an unreferenced historical package.

}
c.localChaincodes[hashOfCCHash] = localChaincode
c.chaincodeCustodian.NotifyInstalled(packageID)
if !initializing {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: The explicit-install eager-build contract is not covered by a regression test

Scenario
The PR and issue #5599 state that peer lifecycle chaincode install should still trigger an eager build, while startup initialization should defer it. The diff changes the initializing guard, but there is no test that verifies the HandleChaincodeInstalled / initializing == false path still enqueues a build for a newly installed package.

Why it matters
A future refactor could accidentally remove the eager build for explicit installs, changing operator-visible behavior without a failing test.

How to verify/fix
Add a test for HandleChaincodeInstalled with a package ID that is not yet in the local cache. Start the custodian worker with a fake builder/launcher/build registry and assert that Build is eventually called once for that package ID.

if !initializing {
c.chaincodeCustodian.NotifyInstalled(packageID)
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Major: The fix only partially covers the issue scope for startup-installed packages

Scenario
The change skips NotifyInstalled during startup initialization when a local chaincode entry is first discovered. However, when the installed package already has references to approved chaincode definitions, handleChaincodeInstalledWhileLocked still calls NotifyInstalledAndRunnable(...) for those references regardless of initializing. The runnable path goes through launcher.Launch, and RuntimeLauncher.Launch performs Runtime.Build(ccid) when needed.

Why it matters
Issue #5599 is phrased as avoiding builds for all previously installed packages on peer startup. This PR helps the historical unused-package case, but approved+installed chaincodes may still be built/launched during startup. That makes the actual benefit narrower than the issue title/body may suggest.

How to verify/fix
Clarify the intended scope in the PR/issue: is the goal only to avoid building unreferenced historical packages, or to defer all installed-package builds until first real invoke? If full scope is intended, add coverage for approved+installed chaincodes during startup initialization and adjust the startup path so it does not enqueue launch/build work before first use.

Make the InitializeLocalChaincodes test start from an empty local cache
so it exercises the new-entry branch, and assert that neither a build
nor a launch is enqueued. Add a HandleChaincodeInstalled test asserting
that an explicit install still builds the package eagerly.

Signed-off-by: shubhamrai9122-creator <shubhamrai9122@gmail.com>
@shubhamrai9122-creator

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed review @pfi79!

  1. Good catch: the test's package was already in the cache, so it never hit the changed branch. It now starts from an empty local cache with an installed package that isn't in it, and asserts that neither Build nor Launch is enqueued. I confirmed it fails without the change and passes with it.
  2. Added a HandleChaincodeInstalled test for a package not yet in the cache: the custodian builds it once, for that package ID, and doesn't launch it.
  3. The scope is intentionally the narrower one: don't build packages that no definition references. InitializeLocalChaincodes() runs in start.go before any channel is initialized, so References is empty at that point and the NotifyInstalledAndRunnable loop doesn't fire there. When the channels come up, approved+installed chaincodes are still built and launched through update(), the same as before. Deferring those until first invoke would change behaviour for chaincodes that are in use, so I think it belongs in a separate change if wanted. I've updated the PR description to say this.

@pfi79
pfi79 merged commit 5431755 into hyperledger:main Dec 4, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants