@@ -1505,69 +1505,86 @@ func TestCompactionConcurrencyEnvVars(t *testing.T) {
15051505 defer leaktest .AfterTest (t )()
15061506 defer log .Scope (t ).Close (t )
15071507
1508- maxProcsBefore := runtime .GOMAXPROCS (0 )
1509- defer runtime .GOMAXPROCS (maxProcsBefore )
1510-
15111508 type testCase struct {
1512- maxProcs int
1513- rocksDBConcurrency string
1514- cockroachConcurrency string
1515- want int
1509+ cockroachConcurrentCompactions string
1510+ cockroachRocksDBConcurrency string
1511+ want int
15161512 }
15171513 cases := []testCase {
1518- // Defaults
1519- {32 , "" , "" , 3 },
1520- {4 , "" , "" , 3 },
1521- {3 , "" , "" , 2 },
1522- {2 , "" , "" , 1 },
1523- {1 , "" , "" , 1 },
1524- // Old COCKROACH_ROCKSDB_CONCURRENCY env var is set. The user-provided
1525- // value includes 1 slot for flushes, so resulting compaction
1526- // concurrency is n-1.
1527- {32 , "4" , "" , 3 },
1528- {4 , "4" , "" , 3 },
1529- {2 , "4" , "" , 3 },
1530- {1 , "4" , "" , 3 },
1531- {32 , "8" , "" , 7 },
1532- {4 , "8" , "" , 7 },
1533- {2 , "8" , "" , 7 },
1534- {1 , "8" , "" , 7 },
1535- // New COCKROACH_CONCURRENT_COMPACTIONS env var is set.
1536- {32 , "" , "4" , 4 },
1537- {4 , "" , "4" , 4 },
1538- {2 , "" , "4" , 4 },
1539- {1 , "" , "4" , 4 },
1540- {32 , "" , "8" , 8 },
1541- {4 , "" , "8" , 8 },
1542- {2 , "" , "8" , 8 },
1543- {1 , "" , "8" , 8 },
1544- // Both settings are set; COCKROACH_CONCURRENT_COMPACTIONS supersedes
1545- // COCKROACH_ROCKSDB_CONCURRENCY.
1546- {32 , "8" , "4" , 4 },
1547- {4 , "1" , "4" , 4 },
1548- {2 , "2" , "4" , 4 },
1549- {1 , "5" , "4" , 4 },
1550- {32 , "1" , "8" , 8 },
1551- {4 , "2" , "8" , 8 },
1552- {2 , "4" , "8" , 8 },
1553- {1 , "1" , "8" , 8 },
1514+ // Default.
1515+ {want : 0 },
1516+ {cockroachConcurrentCompactions : "5" , want : 5 },
1517+ {cockroachConcurrentCompactions : "1" , want : 1 },
1518+ {cockroachConcurrentCompactions : "0" , want : 0 },
1519+ // COCKROACH_ROCKSDB_CONCURRENCY is deprecated but we still support it. Note
1520+ // that for this env var, the compaction concurrency is the value minus 1.
1521+ {cockroachRocksDBConcurrency : "5" , want : 4 },
1522+ {cockroachRocksDBConcurrency : "2" , want : 1 },
1523+ {cockroachRocksDBConcurrency : "1" , want : 1 },
1524+ {cockroachRocksDBConcurrency : "0" , want : 0 },
1525+ // The non-deprecated var takes precedence.
1526+ {cockroachConcurrentCompactions : "10" , cockroachRocksDBConcurrency : "5" , want : 10 },
15541527 }
15551528 for _ , tc := range cases {
1556- t .Run (fmt .Sprintf ("GOMAXPROCS=%d, old=%q,new=%q" , tc .maxProcs , tc .rocksDBConcurrency , tc . cockroachConcurrency ),
1529+ t .Run (fmt .Sprintf ("old=%q,new=%q" , tc .cockroachRocksDBConcurrency , tc .cockroachConcurrentCompactions ),
15571530 func (t * testing.T ) {
1558- runtime .GOMAXPROCS (tc .maxProcs )
1559-
1560- if tc .rocksDBConcurrency == "" {
1531+ if tc .cockroachRocksDBConcurrency == "" {
15611532 defer envutil .TestUnsetEnv (t , "COCKROACH_ROCKSDB_CONCURRENCY" )()
15621533 } else {
1563- defer envutil .TestSetEnv (t , "COCKROACH_ROCKSDB_CONCURRENCY" , tc .rocksDBConcurrency )()
1534+ defer envutil .TestSetEnv (t , "COCKROACH_ROCKSDB_CONCURRENCY" , tc .cockroachRocksDBConcurrency )()
15641535 }
1565- if tc .cockroachConcurrency == "" {
1536+ if tc .cockroachConcurrentCompactions == "" {
15661537 defer envutil .TestUnsetEnv (t , "COCKROACH_CONCURRENT_COMPACTIONS" )()
15671538 } else {
1568- defer envutil .TestSetEnv (t , "COCKROACH_CONCURRENT_COMPACTIONS" , tc .cockroachConcurrency )()
1539+ defer envutil .TestSetEnv (t , "COCKROACH_CONCURRENT_COMPACTIONS" , tc .cockroachConcurrentCompactions )()
15691540 }
1570- require .Equal (t , tc .want , getDefaultMaxConcurrentCompactions ())
1541+ require .Equal (t , tc .want , getMaxConcurrentCompactionsFromEnv ())
1542+ })
1543+ }
1544+ }
1545+
1546+ func TestDetermineMaxConcurrentCompactions (t * testing.T ) {
1547+ defer leaktest .AfterTest (t )()
1548+ defer log .Scope (t ).Close (t )
1549+
1550+ maxProcsBefore := runtime .GOMAXPROCS (0 )
1551+ defer runtime .GOMAXPROCS (maxProcsBefore )
1552+
1553+ type testCase struct {
1554+ maxProcs int
1555+ envValue int
1556+ clusterSetting int
1557+ want int
1558+ }
1559+ cases := []testCase {
1560+ // Defaults.
1561+ {maxProcs : 32 , want : 3 },
1562+ {maxProcs : 8 , want : 3 },
1563+ {maxProcs : 4 , want : 3 },
1564+ {maxProcs : 2 , want : 1 },
1565+ {maxProcs : 1 , want : 1 },
1566+ // Env value override.
1567+ {maxProcs : 32 , envValue : 10 , want : 10 },
1568+ {maxProcs : 1 , envValue : 10 , want : 10 },
1569+ // Cluster setting override.
1570+ {maxProcs : 32 , clusterSetting : 10 , want : 10 },
1571+ {maxProcs : 1 , clusterSetting : 10 , want : 10 },
1572+ // Env value and cluster setting override.
1573+ {maxProcs : 32 , envValue : 15 , clusterSetting : 10 , want : 15 },
1574+ {maxProcs : 32 , envValue : 10 , clusterSetting : 15 , want : 15 },
1575+ {maxProcs : 1 , envValue : 15 , clusterSetting : 10 , want : 15 },
1576+ {maxProcs : 1 , envValue : 10 , clusterSetting : 15 , want : 15 },
1577+ }
1578+ for _ , tc := range cases {
1579+ t .Run (fmt .Sprintf ("GOMAXPROCS=%d,env=%d,setting=%d" , tc .maxProcs , tc .envValue , tc .clusterSetting ),
1580+ func (t * testing.T ) {
1581+ runtime .GOMAXPROCS (tc .maxProcs )
1582+ actual := determineMaxConcurrentCompactions (
1583+ getDefaultMaxConcurrentCompactions (),
1584+ tc .envValue ,
1585+ tc .clusterSetting ,
1586+ )
1587+ require .Equal (t , tc .want , actual )
15711588 })
15721589 }
15731590}
0 commit comments