Skip to content

Commit c3d9edc

Browse files
authored
Merge pull request #5611 from filecoin-project/feat/config-migration-worker-limit
feat: optionally set max migration workers
2 parents 0182082 + 2a48978 commit c3d9edc

File tree

1 file changed

+39
-14
lines changed

1 file changed

+39
-14
lines changed

pkg/fork/fork.go

+39-14
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import (
77
"encoding/binary"
88
"errors"
99
"fmt"
10+
"os"
1011
"runtime"
1112
"sort"
13+
"strconv"
1214
"sync"
1315
"time"
1416

@@ -69,6 +71,29 @@ var log = logging.Logger("fork")
6971

7072
var ErrExpensiveFork = errors.New("refusing explicit call due to state fork at epoch")
7173

74+
var (
75+
MigrationMaxWorkerCount int
76+
EnvMigrationMaxWorkerCount = "VENUS_MIGRATION_MAX_WORKER_COUNT"
77+
)
78+
79+
func init() {
80+
// the default calculation used for migration worker count
81+
MigrationMaxWorkerCount = runtime.NumCPU()
82+
// check if an alternative value was request by environment
83+
if mwcs := os.Getenv(EnvMigrationMaxWorkerCount); mwcs != "" {
84+
mwc, err := strconv.ParseInt(mwcs, 10, 32)
85+
if err != nil {
86+
log.Warnf("invalid value for %s (%s) defaulting to %d: %s", EnvMigrationMaxWorkerCount, mwcs, MigrationMaxWorkerCount, err)
87+
return
88+
}
89+
// use value from environment
90+
log.Infof("migration worker cound set from %s (%d)", EnvMigrationMaxWorkerCount, mwc)
91+
MigrationMaxWorkerCount = int(mwc)
92+
return
93+
}
94+
log.Infof("migration worker count: %d", MigrationMaxWorkerCount)
95+
}
96+
7297
// MigrationCache can be used to cache information used by a migration. This is primarily useful to
7398
// "pre-compute" some migration state ahead of time, and make it accessible in the migration itself.
7499
type MigrationCache interface {
@@ -1603,7 +1628,7 @@ func terminateActor(ctx context.Context, tree *vmstate.State, addr address.Addre
16031628

16041629
func (c *ChainFork) UpgradeActorsV3(ctx context.Context, cache MigrationCache, root cid.Cid, epoch abi.ChainEpoch, ts *types.TipSet) (cid.Cid, error) {
16051630
// Use all the CPUs except 3.
1606-
workerCount := runtime.NumCPU() - 3
1631+
workerCount := MigrationMaxWorkerCount - 3
16071632
if workerCount <= 0 {
16081633
workerCount = 1
16091634
}
@@ -1642,7 +1667,7 @@ func (c *ChainFork) UpgradeActorsV3(ctx context.Context, cache MigrationCache, r
16421667
func (c *ChainFork) PreUpgradeActorsV3(ctx context.Context, cache MigrationCache, root cid.Cid, epoch abi.ChainEpoch, ts *types.TipSet) error {
16431668
log.Info("PreUpgradeActorsV3 ......")
16441669
// Use half the CPUs for pre-migration, but leave at least 3.
1645-
workerCount := runtime.NumCPU()
1670+
workerCount := MigrationMaxWorkerCount
16461671
if workerCount <= 4 {
16471672
workerCount = 1
16481673
} else {
@@ -1706,7 +1731,7 @@ func (c *ChainFork) upgradeActorsV3Common(
17061731

17071732
func (c *ChainFork) UpgradeActorsV4(ctx context.Context, cache MigrationCache, root cid.Cid, epoch abi.ChainEpoch, ts *types.TipSet) (cid.Cid, error) {
17081733
// Use all the CPUs except 3.
1709-
workerCount := runtime.NumCPU() - 3
1734+
workerCount := MigrationMaxWorkerCount - 3
17101735
if workerCount <= 0 {
17111736
workerCount = 1
17121737
}
@@ -1728,7 +1753,7 @@ func (c *ChainFork) UpgradeActorsV4(ctx context.Context, cache MigrationCache, r
17281753

17291754
func (c *ChainFork) PreUpgradeActorsV4(ctx context.Context, cache MigrationCache, root cid.Cid, epoch abi.ChainEpoch, ts *types.TipSet) error {
17301755
// Use half the CPUs for pre-migration, but leave at least 3.
1731-
workerCount := runtime.NumCPU()
1756+
workerCount := MigrationMaxWorkerCount
17321757
if workerCount <= 4 {
17331758
workerCount = 1
17341759
} else {
@@ -1792,7 +1817,7 @@ func (c *ChainFork) upgradeActorsV4Common(
17921817

17931818
func (c *ChainFork) UpgradeActorsV5(ctx context.Context, cache MigrationCache, root cid.Cid, epoch abi.ChainEpoch, ts *types.TipSet) (cid.Cid, error) {
17941819
// Use all the CPUs except 3.
1795-
workerCount := runtime.NumCPU() - 3
1820+
workerCount := MigrationMaxWorkerCount - 3
17961821
if workerCount <= 0 {
17971822
workerCount = 1
17981823
}
@@ -1814,7 +1839,7 @@ func (c *ChainFork) UpgradeActorsV5(ctx context.Context, cache MigrationCache, r
18141839

18151840
func (c *ChainFork) PreUpgradeActorsV5(ctx context.Context, cache MigrationCache, root cid.Cid, epoch abi.ChainEpoch, ts *types.TipSet) error {
18161841
// Use half the CPUs for pre-migration, but leave at least 3.
1817-
workerCount := runtime.NumCPU()
1842+
workerCount := MigrationMaxWorkerCount
18181843
if workerCount <= 4 {
18191844
workerCount = 1
18201845
} else {
@@ -1878,7 +1903,7 @@ func (c *ChainFork) upgradeActorsV5Common(
18781903

18791904
func (c *ChainFork) UpgradeActorsV6(ctx context.Context, cache MigrationCache, root cid.Cid, epoch abi.ChainEpoch, ts *types.TipSet) (cid.Cid, error) {
18801905
// Use all the CPUs except 3.
1881-
workerCount := runtime.NumCPU() - 3
1906+
workerCount := MigrationMaxWorkerCount - 3
18821907
if workerCount <= 0 {
18831908
workerCount = 1
18841909
}
@@ -1900,7 +1925,7 @@ func (c *ChainFork) UpgradeActorsV6(ctx context.Context, cache MigrationCache, r
19001925

19011926
func (c *ChainFork) PreUpgradeActorsV6(ctx context.Context, cache MigrationCache, root cid.Cid, epoch abi.ChainEpoch, ts *types.TipSet) error {
19021927
// Use half the CPUs for pre-migration, but leave at least 3.
1903-
workerCount := runtime.NumCPU()
1928+
workerCount := MigrationMaxWorkerCount
19041929
if workerCount <= 4 {
19051930
workerCount = 1
19061931
} else {
@@ -1966,7 +1991,7 @@ func (c *ChainFork) upgradeActorsV6Common(
19661991

19671992
func (c *ChainFork) UpgradeActorsV7(ctx context.Context, cache MigrationCache, root cid.Cid, epoch abi.ChainEpoch, ts *types.TipSet) (cid.Cid, error) {
19681993
// Use all the CPUs except 3.
1969-
workerCount := runtime.NumCPU() - 3
1994+
workerCount := MigrationMaxWorkerCount - 3
19701995
if workerCount <= 0 {
19711996
workerCount = 1
19721997
}
@@ -1988,7 +2013,7 @@ func (c *ChainFork) UpgradeActorsV7(ctx context.Context, cache MigrationCache, r
19882013

19892014
func (c *ChainFork) PreUpgradeActorsV7(ctx context.Context, cache MigrationCache, root cid.Cid, epoch abi.ChainEpoch, ts *types.TipSet) error {
19902015
// Use half the CPUs for pre-migration, but leave at least 3.
1991-
workerCount := runtime.NumCPU()
2016+
workerCount := MigrationMaxWorkerCount
19922017
if workerCount <= 4 {
19932018
workerCount = 1
19942019
} else {
@@ -2063,7 +2088,7 @@ func (c *ChainFork) upgradeActorsV7Common(
20632088

20642089
func (c *ChainFork) UpgradeActorsV8(ctx context.Context, cache MigrationCache, root cid.Cid, epoch abi.ChainEpoch, ts *types.TipSet) (cid.Cid, error) {
20652090
// Use all the CPUs except 3.
2066-
workerCount := runtime.NumCPU() - 3
2091+
workerCount := MigrationMaxWorkerCount - 3
20672092
if workerCount <= 0 {
20682093
workerCount = 1
20692094
}
@@ -2087,7 +2112,7 @@ func (c *ChainFork) UpgradeActorsV8(ctx context.Context, cache MigrationCache, r
20872112

20882113
func (c *ChainFork) PreUpgradeActorsV8(ctx context.Context, cache MigrationCache, root cid.Cid, epoch abi.ChainEpoch, ts *types.TipSet) error {
20892114
// Use half the CPUs for pre-migration, but leave at least 3.
2090-
workerCount := runtime.NumCPU()
2115+
workerCount := MigrationMaxWorkerCount
20912116
if workerCount <= 4 {
20922117
workerCount = 1
20932118
} else {
@@ -2174,7 +2199,7 @@ func (c *ChainFork) upgradeActorsV8Common(
21742199

21752200
func (c *ChainFork) UpgradeActorsV9(ctx context.Context, cache MigrationCache, root cid.Cid, epoch abi.ChainEpoch, ts *types.TipSet) (cid.Cid, error) {
21762201
// Use all the CPUs except 3.
2177-
workerCount := runtime.NumCPU() - 3
2202+
workerCount := MigrationMaxWorkerCount - 3
21782203
if workerCount <= 0 {
21792204
workerCount = 1
21802205
}
@@ -2201,7 +2226,7 @@ func (c *ChainFork) PreUpgradeActorsV9(ctx context.Context,
22012226
ts *types.TipSet,
22022227
) error {
22032228
// Use half the CPUs for pre-migration, but leave at least 3.
2204-
workerCount := runtime.NumCPU()
2229+
workerCount := MigrationMaxWorkerCount
22052230
if workerCount <= 4 {
22062231
workerCount = 1
22072232
} else {

0 commit comments

Comments
 (0)