@@ -7,8 +7,10 @@ import (
7
7
"encoding/binary"
8
8
"errors"
9
9
"fmt"
10
+ "os"
10
11
"runtime"
11
12
"sort"
13
+ "strconv"
12
14
"sync"
13
15
"time"
14
16
@@ -69,6 +71,29 @@ var log = logging.Logger("fork")
69
71
70
72
var ErrExpensiveFork = errors .New ("refusing explicit call due to state fork at epoch" )
71
73
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
+
72
97
// MigrationCache can be used to cache information used by a migration. This is primarily useful to
73
98
// "pre-compute" some migration state ahead of time, and make it accessible in the migration itself.
74
99
type MigrationCache interface {
@@ -1603,7 +1628,7 @@ func terminateActor(ctx context.Context, tree *vmstate.State, addr address.Addre
1603
1628
1604
1629
func (c * ChainFork ) UpgradeActorsV3 (ctx context.Context , cache MigrationCache , root cid.Cid , epoch abi.ChainEpoch , ts * types.TipSet ) (cid.Cid , error ) {
1605
1630
// Use all the CPUs except 3.
1606
- workerCount := runtime . NumCPU () - 3
1631
+ workerCount := MigrationMaxWorkerCount - 3
1607
1632
if workerCount <= 0 {
1608
1633
workerCount = 1
1609
1634
}
@@ -1642,7 +1667,7 @@ func (c *ChainFork) UpgradeActorsV3(ctx context.Context, cache MigrationCache, r
1642
1667
func (c * ChainFork ) PreUpgradeActorsV3 (ctx context.Context , cache MigrationCache , root cid.Cid , epoch abi.ChainEpoch , ts * types.TipSet ) error {
1643
1668
log .Info ("PreUpgradeActorsV3 ......" )
1644
1669
// Use half the CPUs for pre-migration, but leave at least 3.
1645
- workerCount := runtime . NumCPU ()
1670
+ workerCount := MigrationMaxWorkerCount
1646
1671
if workerCount <= 4 {
1647
1672
workerCount = 1
1648
1673
} else {
@@ -1706,7 +1731,7 @@ func (c *ChainFork) upgradeActorsV3Common(
1706
1731
1707
1732
func (c * ChainFork ) UpgradeActorsV4 (ctx context.Context , cache MigrationCache , root cid.Cid , epoch abi.ChainEpoch , ts * types.TipSet ) (cid.Cid , error ) {
1708
1733
// Use all the CPUs except 3.
1709
- workerCount := runtime . NumCPU () - 3
1734
+ workerCount := MigrationMaxWorkerCount - 3
1710
1735
if workerCount <= 0 {
1711
1736
workerCount = 1
1712
1737
}
@@ -1728,7 +1753,7 @@ func (c *ChainFork) UpgradeActorsV4(ctx context.Context, cache MigrationCache, r
1728
1753
1729
1754
func (c * ChainFork ) PreUpgradeActorsV4 (ctx context.Context , cache MigrationCache , root cid.Cid , epoch abi.ChainEpoch , ts * types.TipSet ) error {
1730
1755
// Use half the CPUs for pre-migration, but leave at least 3.
1731
- workerCount := runtime . NumCPU ()
1756
+ workerCount := MigrationMaxWorkerCount
1732
1757
if workerCount <= 4 {
1733
1758
workerCount = 1
1734
1759
} else {
@@ -1792,7 +1817,7 @@ func (c *ChainFork) upgradeActorsV4Common(
1792
1817
1793
1818
func (c * ChainFork ) UpgradeActorsV5 (ctx context.Context , cache MigrationCache , root cid.Cid , epoch abi.ChainEpoch , ts * types.TipSet ) (cid.Cid , error ) {
1794
1819
// Use all the CPUs except 3.
1795
- workerCount := runtime . NumCPU () - 3
1820
+ workerCount := MigrationMaxWorkerCount - 3
1796
1821
if workerCount <= 0 {
1797
1822
workerCount = 1
1798
1823
}
@@ -1814,7 +1839,7 @@ func (c *ChainFork) UpgradeActorsV5(ctx context.Context, cache MigrationCache, r
1814
1839
1815
1840
func (c * ChainFork ) PreUpgradeActorsV5 (ctx context.Context , cache MigrationCache , root cid.Cid , epoch abi.ChainEpoch , ts * types.TipSet ) error {
1816
1841
// Use half the CPUs for pre-migration, but leave at least 3.
1817
- workerCount := runtime . NumCPU ()
1842
+ workerCount := MigrationMaxWorkerCount
1818
1843
if workerCount <= 4 {
1819
1844
workerCount = 1
1820
1845
} else {
@@ -1878,7 +1903,7 @@ func (c *ChainFork) upgradeActorsV5Common(
1878
1903
1879
1904
func (c * ChainFork ) UpgradeActorsV6 (ctx context.Context , cache MigrationCache , root cid.Cid , epoch abi.ChainEpoch , ts * types.TipSet ) (cid.Cid , error ) {
1880
1905
// Use all the CPUs except 3.
1881
- workerCount := runtime . NumCPU () - 3
1906
+ workerCount := MigrationMaxWorkerCount - 3
1882
1907
if workerCount <= 0 {
1883
1908
workerCount = 1
1884
1909
}
@@ -1900,7 +1925,7 @@ func (c *ChainFork) UpgradeActorsV6(ctx context.Context, cache MigrationCache, r
1900
1925
1901
1926
func (c * ChainFork ) PreUpgradeActorsV6 (ctx context.Context , cache MigrationCache , root cid.Cid , epoch abi.ChainEpoch , ts * types.TipSet ) error {
1902
1927
// Use half the CPUs for pre-migration, but leave at least 3.
1903
- workerCount := runtime . NumCPU ()
1928
+ workerCount := MigrationMaxWorkerCount
1904
1929
if workerCount <= 4 {
1905
1930
workerCount = 1
1906
1931
} else {
@@ -1966,7 +1991,7 @@ func (c *ChainFork) upgradeActorsV6Common(
1966
1991
1967
1992
func (c * ChainFork ) UpgradeActorsV7 (ctx context.Context , cache MigrationCache , root cid.Cid , epoch abi.ChainEpoch , ts * types.TipSet ) (cid.Cid , error ) {
1968
1993
// Use all the CPUs except 3.
1969
- workerCount := runtime . NumCPU () - 3
1994
+ workerCount := MigrationMaxWorkerCount - 3
1970
1995
if workerCount <= 0 {
1971
1996
workerCount = 1
1972
1997
}
@@ -1988,7 +2013,7 @@ func (c *ChainFork) UpgradeActorsV7(ctx context.Context, cache MigrationCache, r
1988
2013
1989
2014
func (c * ChainFork ) PreUpgradeActorsV7 (ctx context.Context , cache MigrationCache , root cid.Cid , epoch abi.ChainEpoch , ts * types.TipSet ) error {
1990
2015
// Use half the CPUs for pre-migration, but leave at least 3.
1991
- workerCount := runtime . NumCPU ()
2016
+ workerCount := MigrationMaxWorkerCount
1992
2017
if workerCount <= 4 {
1993
2018
workerCount = 1
1994
2019
} else {
@@ -2063,7 +2088,7 @@ func (c *ChainFork) upgradeActorsV7Common(
2063
2088
2064
2089
func (c * ChainFork ) UpgradeActorsV8 (ctx context.Context , cache MigrationCache , root cid.Cid , epoch abi.ChainEpoch , ts * types.TipSet ) (cid.Cid , error ) {
2065
2090
// Use all the CPUs except 3.
2066
- workerCount := runtime . NumCPU () - 3
2091
+ workerCount := MigrationMaxWorkerCount - 3
2067
2092
if workerCount <= 0 {
2068
2093
workerCount = 1
2069
2094
}
@@ -2087,7 +2112,7 @@ func (c *ChainFork) UpgradeActorsV8(ctx context.Context, cache MigrationCache, r
2087
2112
2088
2113
func (c * ChainFork ) PreUpgradeActorsV8 (ctx context.Context , cache MigrationCache , root cid.Cid , epoch abi.ChainEpoch , ts * types.TipSet ) error {
2089
2114
// Use half the CPUs for pre-migration, but leave at least 3.
2090
- workerCount := runtime . NumCPU ()
2115
+ workerCount := MigrationMaxWorkerCount
2091
2116
if workerCount <= 4 {
2092
2117
workerCount = 1
2093
2118
} else {
@@ -2174,7 +2199,7 @@ func (c *ChainFork) upgradeActorsV8Common(
2174
2199
2175
2200
func (c * ChainFork ) UpgradeActorsV9 (ctx context.Context , cache MigrationCache , root cid.Cid , epoch abi.ChainEpoch , ts * types.TipSet ) (cid.Cid , error ) {
2176
2201
// Use all the CPUs except 3.
2177
- workerCount := runtime . NumCPU () - 3
2202
+ workerCount := MigrationMaxWorkerCount - 3
2178
2203
if workerCount <= 0 {
2179
2204
workerCount = 1
2180
2205
}
@@ -2201,7 +2226,7 @@ func (c *ChainFork) PreUpgradeActorsV9(ctx context.Context,
2201
2226
ts * types.TipSet ,
2202
2227
) error {
2203
2228
// Use half the CPUs for pre-migration, but leave at least 3.
2204
- workerCount := runtime . NumCPU ()
2229
+ workerCount := MigrationMaxWorkerCount
2205
2230
if workerCount <= 4 {
2206
2231
workerCount = 1
2207
2232
} else {
0 commit comments