Apache Lucene open-source search software
LUCENE-6437: randomize cpu core count and spins for ConcurrentMergeScheduler's dynamic defaults
Michael McCandlessa4369f070e9d
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 64b48251d74..b4b04369c75 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -146,6 +146,11 @@ Test Framework* LUCENE-6419: Added two-phase iteration assertions to AssertingQuery.(Adrien Grand)+* LUCENE-6437: Randomly set CPU core count and spins, derived from+ test's master seed, used by ConcurrentMergeScheduler to set dynamic+ defaults, for better test randomization and to help tests reproduce+ (Robert Muir, Mike McCandless)+======================= Lucene 5.1.0 =======================New Featuresdiff --git a/lucene/core/src/java/org/apache/lucene/index/ConcurrentMergeScheduler.java b/lucene/core/src/java/org/apache/lucene/index/ConcurrentMergeScheduler.javaindex e7418deedd1..641598323df 100644--- a/lucene/core/src/java/org/apache/lucene/index/ConcurrentMergeScheduler.java+++ b/lucene/core/src/java/org/apache/lucene/index/ConcurrentMergeScheduler.java@@ -67,6 +67,16 @@ public class ConcurrentMergeScheduler extends MergeScheduler {* Linux; other platforms will assume the index is not on an SSD. */public static final int AUTO_DETECT_MERGES_AND_THREADS = -1;+ /** Used for testing.+ *+ * @lucene.internal */+ public static final String DEFAULT_CPU_CORE_COUNT_PROPERTY = "lucene.cms.override_core_count";++ /** Used for testing.+ *+ * @lucene.internal */+ public static final String DEFAULT_SPINS_PROPERTY = "lucene.cms.override_spins";+/** List of currently active {@link MergeThread}s. */protected final List<MergeThread> mergeThreads = new ArrayList<>();@@ -159,7 +169,19 @@ public class ConcurrentMergeScheduler extends MergeScheduler {maxThreadCount = 1;maxMergeCount = 6;} else {- maxThreadCount = Math.max(1, Math.min(4, Runtime.getRuntime().availableProcessors()/2));+ int coreCount = Runtime.getRuntime().availableProcessors();++ // Let tests override this to help reproducing a failure on a machine that has a different+ // core count than the one where the test originally failed:+ try {+ String value = System.getProperty(DEFAULT_CPU_CORE_COUNT_PROPERTY);+ if (value != null) {+ coreCount = Integer.parseInt(value);+ }+ } catch (Throwable ignored) {+ }++ maxThreadCount = Math.max(1, Math.min(4, coreCount/2));maxMergeCount = maxThreadCount+5;}}@@ -347,6 +369,16 @@ public class ConcurrentMergeScheduler extends MergeScheduler {private synchronized void initDynamicDefaults(IndexWriter writer) throws IOException {if (maxThreadCount == AUTO_DETECT_MERGES_AND_THREADS) {boolean spins = IOUtils.spins(writer.getDirectory());++ // Let tests override this to help reproducing a failure on a machine that has a different+ // core count than the one where the test originally failed:+ try {+ String value = System.getProperty(DEFAULT_SPINS_PROPERTY);+ if (value != null) {+ spins = Boolean.parseBoolean(value);+ }+ } catch (Throwable ignored) {+ }setDefaultMaxMergesAndThreads(spins);if (verbose()) {message("initDynamicDefaults spins=" + spins + " maxThreadCount=" + maxThreadCount + " maxMergeCount=" + maxMergeCount);diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.javaindex ca1ce974a54..3d9b00827a3 100644--- a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java@@ -1702,6 +1702,32 @@ public abstract class LuceneTestCase extends Assert {IndexSearcher.setDefaultQueryCachingPolicy(DEFAULT_CACHING_POLICY);}+ @BeforeClass+ public static void setupCPUCoreCount() {+ // Randomize core count so CMS varies its dynamic defaults, and this also "fixes" core+ // count from the master seed so it will always be the same on reproduce:+ int numCores = TestUtil.nextInt(random(), 1, 4);+ System.setProperty(ConcurrentMergeScheduler.DEFAULT_CPU_CORE_COUNT_PROPERTY, Integer.toString(numCores));+ }++ @AfterClass+ public static void restoreCPUCoreCount() {+ System.clearProperty(ConcurrentMergeScheduler.DEFAULT_CPU_CORE_COUNT_PROPERTY);+ }++ @BeforeClass+ public static void setupSpins() {+ // Randomize IOUtils.spins() count so CMS varies its dynamic defaults, and this also "fixes" core+ // count from the master seed so it will always be the same on reproduce:+ boolean spins = random().nextBoolean();+ System.setProperty(ConcurrentMergeScheduler.DEFAULT_SPINS_PROPERTY, Boolean.toString(spins));+ }++ @AfterClass+ public static void restoreSpins() {+ System.clearProperty(ConcurrentMergeScheduler.DEFAULT_SPINS_PROPERTY);+ }+/*** Create a new searcher over the reader. This searcher might randomly use* threads.diff --git a/lucene/tools/junit4/tests.policy b/lucene/tools/junit4/tests.policyindex 8594c400654..b9c9d0ec76d 100644--- a/lucene/tools/junit4/tests.policy+++ b/lucene/tools/junit4/tests.policy@@ -84,6 +84,10 @@ grant {// timezone randomizationpermission java.util.PropertyPermission "user.timezone", "write";+ // CMS randomization+ permission java.util.PropertyPermission "lucene.cms.override_core_count", "write";+ permission java.util.PropertyPermission "lucene.cms.override_spins", "write";+// used by nested tests? (e.g. TestLeaveFilesIfTestFails). TODO: look into thispermission java.util.PropertyPermission "tests.runnested", "write";