Apache Solr open-source search software
SOLR-16475 Make the default replica placement plugin configurable (#1089)
Jan Høydahl15dac229bd08
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex cd517515feb..30d0a43a50f 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -68,6 +68,8 @@ Improvements* SOLR-15478: A v2 equivalent of CLUSTERSTATUS command is now available at `GET /api/cluster`. Collection listing,previously at this path, can still be accessed at `GET /api/collections`. (Joshua Ouma via Jason Gerlowski)+* SOLR-16475: Make the default replica placement plugin configurable as system property on startup (janhoy)+* SOLR-13626: Document the SystemInfoHandler in the Ref Guide. (Tony Cook via Eric Pugh)* SOLR-15479: A v2 equivalent of the RENAME command is now available at `POST /api/collections/collName/rename`. (Anakhe Ajayi via Jason Gerlowski)diff --git a/solr/bin/solr b/solr/bin/solrindex 704efcb724f..a73e2403c59 100755--- a/solr/bin/solr+++ b/solr/bin/solr@@ -1804,6 +1804,11 @@ if [[ -n "${SOLR_MODULES:-}" ]] ; thenSOLR_OPTS+=("-Dsolr.modules=$SOLR_MODULES")fi+# Default placement plugin+if [[ -n "${SOLR_PLACEMENTPLUGIN_DEFAULT:-}" ]] ; then+ SOLR_OPTS+=("-Dsolr.placementplugin.default=$SOLR_PLACEMENTPLUGIN_DEFAULT")+fi+: ${SOLR_SERVER_DIR:=$DEFAULT_SERVER_DIR}if [ ! -e "$SOLR_SERVER_DIR" ]; thendiff --git a/solr/bin/solr.cmd b/solr/bin/solr.cmdindex 7960e9065de..88bda913461 100755--- a/solr/bin/solr.cmd+++ b/solr/bin/solr.cmd@@ -987,6 +987,11 @@ IF DEFINED SOLR_MODULES (set "SOLR_OPTS=%SOLR_OPTS% -Dsolr.modules=%SOLR_MODULES%")+REM Default placement plugin+IF DEFINED SOLR_PLACEMENTPLUGIN_DEFAULT (+ set "SOLR_OPTS=%SOLR_OPTS% -Dsolr.placementplugin.default=%SOLR_PLACEMENTPLUGIN_DEFAULT%"+)+IF "%SOLR_SERVER_DIR%"=="" set "SOLR_SERVER_DIR=%DEFAULT_SERVER_DIR%"IF NOT EXIST "%SOLR_SERVER_DIR%" (diff --git a/solr/bin/solr.in.cmd b/solr/bin/solr.in.cmdindex 76aef1d20c8..c436a743d45 100755--- a/solr/bin/solr.in.cmd+++ b/solr/bin/solr.in.cmd@@ -236,4 +236,8 @@ REM then enable the following setting to address CVE-2021-44228REM set SOLR_OPTS=%SOLR_OPTS% -Dlog4j2.formatMsgNoLookups=trueREM The bundled plugins in the "modules" folder can easily be enabled as a comma-separated list in SOLR_MODULES variable-REM set SOLR_MODULES=extraction,ltr\ No newline at end of file+REM set SOLR_MODULES=extraction,ltr++REM Configure the default replica placement plugin to use if one is not configured in cluster properties+REM See https://solr.apache.org/guide/solr/latest/configuration-guide/replica-placement-plugins.html for details+REM set SOLR_PLACEMENTPLUGIN_DEFAULT=simple\ No newline at end of filediff --git a/solr/bin/solr.in.sh b/solr/bin/solr.in.shindex 8b36f0e491f..f88751e2cf3 100644--- a/solr/bin/solr.in.sh+++ b/solr/bin/solr.in.sh@@ -281,3 +281,7 @@# The bundled plugins in the "modules" folder can easily be enabled as a comma-separated list in SOLR_MODULES variable# SOLR_MODULES=extraction,ltr++# Configure the default replica placement plugin to use if one is not configured in cluster properties+# See https://solr.apache.org/guide/solr/latest/configuration-guide/replica-placement-plugins.html for details+#SOLR_PLACEMENTPLUGIN_DEFAULT=simple\ No newline at end of filediff --git a/solr/core/src/java/org/apache/solr/cloud/api/collections/Assign.java b/solr/core/src/java/org/apache/solr/cloud/api/collections/Assign.javaindex 3d9d849c40a..ff8b7c71b32 100644--- a/solr/core/src/java/org/apache/solr/cloud/api/collections/Assign.java+++ b/solr/core/src/java/org/apache/solr/cloud/api/collections/Assign.java@@ -41,6 +41,9 @@ import org.apache.solr.client.solrj.cloud.SolrCloudManager;import org.apache.solr.client.solrj.cloud.VersionedData;import org.apache.solr.cluster.placement.PlacementPlugin;import org.apache.solr.cluster.placement.impl.PlacementPluginAssignStrategy;+import org.apache.solr.cluster.placement.plugins.AffinityPlacementFactory;+import org.apache.solr.cluster.placement.plugins.MinimizeCoresPlacementFactory;+import org.apache.solr.cluster.placement.plugins.RandomPlacementFactory;import org.apache.solr.cluster.placement.plugins.SimplePlacementFactory;import org.apache.solr.common.SolrException;import org.apache.solr.common.cloud.ClusterState;@@ -63,6 +66,7 @@ import org.slf4j.LoggerFactory;public class Assign {private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());public static final String SYSTEM_COLL_PREFIX = ".sys.";+ public static final String PLACEMENTPLUGIN_DEFAULT_SYSPROP = "solr.placementplugin.default";public static String getCounterNodePath(String collection) {return ZkStateReader.COLLECTIONS_ZKNODE + "/" + collection + "/counter";@@ -562,8 +566,37 @@ public class Assign {coreContainer.getPlacementPluginFactory().createPluginInstance();if (placementPlugin == null) {// Otherwise use the default- // TODO: Replace this with a better options, such as the AffinityPlacementFactory- placementPlugin = (new SimplePlacementFactory()).createPluginInstance();+ String defaultPluginId = System.getProperty(PLACEMENTPLUGIN_DEFAULT_SYSPROP);+ if (defaultPluginId != null) {+ switch (defaultPluginId.toLowerCase(Locale.ROOT)) {+ case "simple":+ placementPlugin = (new SimplePlacementFactory()).createPluginInstance();+ break;+ case "affinity":+ placementPlugin = (new AffinityPlacementFactory()).createPluginInstance();+ break;+ case "minimizecores":+ placementPlugin = (new MinimizeCoresPlacementFactory()).createPluginInstance();+ break;+ case "random":+ placementPlugin = (new RandomPlacementFactory()).createPluginInstance();+ break;+ default:+ throw new SolrException(+ SolrException.ErrorCode.SERVER_ERROR,+ "Invalid value for system property '"+ + PLACEMENTPLUGIN_DEFAULT_SYSPROP+ + "'. Supported values are 'simple', 'random', 'affinity' and 'minimizecores'");+ }+ log.info(+ "Default replica placement plugin set in {} to {}",+ PLACEMENTPLUGIN_DEFAULT_SYSPROP,+ defaultPluginId);+ } else {+ // TODO: Consider making the ootb default AffinityPlacementFactory, see+ // https://issues.apache.org/jira/browse/SOLR-16492+ placementPlugin = (new SimplePlacementFactory()).createPluginInstance();+ }}return new PlacementPluginAssignStrategy(placementPlugin);}diff --git a/solr/packaging/build.gradle b/solr/packaging/build.gradleindex 7c4157bf6fa..5ce4507e09e 100644--- a/solr/packaging/build.gradle+++ b/solr/packaging/build.gradle@@ -159,10 +159,9 @@ assemble.dependsOn installDisttask downloadBats(type: NpmTask) {group = 'Build Dependency Download'- args = ["install", "[email protected]",- "ztombol/bats-support#v0.2.0",- "ztombol/bats-assert#v0.3.0",- "ztombol/bats-file#v0.2.0",+ args = ["install", "https://github.com/bats-core/bats-core#v1.8.2",+ "https://github.com/bats-core/bats-assert#v2.0.0",+ "https://github.com/bats-core/bats-file#v0.3.0",]inputs.files("${project.ext.nodeProjectDir}/package.json")diff --git a/solr/packaging/test/bats_helper.bash b/solr/packaging/test/bats_helper.bashindex 9f7b50da900..d3bd00b3c0f 100644--- a/solr/packaging/test/bats_helper.bash+++ b/solr/packaging/test/bats_helper.bash@@ -20,7 +20,7 @@# The SOLR_HOME directory will be cleared when the next test file is executed.# - "setup" should use "common_setup" if a Solr process is NOT being started in that same "setup" function.common_setup() {- bats_require_minimum_version 1.8.0+ bats_require_minimum_version 1.8.2if [ -z ${BATS_LIB_PREFIX:-} ]; then# Debugging help, if you want to run bats directly, try to detect where libraries might bediff --git a/solr/packaging/test/test_placement_plugin.bats b/solr/packaging/test/test_placement_plugin.batsnew file mode 100644index 00000000000..5828f1aa3c9--- /dev/null+++ b/solr/packaging/test/test_placement_plugin.bats@@ -0,0 +1,47 @@+#!/usr/bin/env bats++# Licensed to the Apache Software Foundation (ASF) under one or more+# contributor license agreements. See the NOTICE file distributed with+# this work for additional information regarding copyright ownership.+# The ASF licenses this file to You under the Apache License, Version 2.0+# (the "License"); you may not use this file except in compliance with+# the License. You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.++load bats_helper++setup() {+ common_clean_setup+}++teardown() {+ # save a snapshot of SOLR_HOME for failed tests+ save_home_on_failure++ delete_all_collections+ solr stop -all >/dev/null 2>&1+}++@test "Affinity placement plugin using sysprop" {+ run solr start -c -Dsolr.placementplugin.default=affinity+ solr assert -c http://localhost:8983/solr -t 3000+ run solr create_collection -c COLL_NAME+ collection_exists COLL_NAME+ assert_file_contains ${SOLR_LOGS_DIR}/solr.log 'Default replica placement plugin set in solr\.placementplugin\.default to affinity'+}++@test "Affinity placement plugin using ENV" {+ export SOLR_PLACEMENTPLUGIN_DEFAULT=random+ run solr start -c+ solr assert -c http://localhost:8983/solr -t 3000+ run solr create_collection -c COLL_NAME+ collection_exists COLL_NAME+ assert_file_contains ${SOLR_LOGS_DIR}/solr.log 'Default replica placement plugin set in solr\.placementplugin\.default to random'+}diff --git a/solr/solr-ref-guide/modules/configuration-guide/pages/replica-placement-plugins.adoc b/solr/solr-ref-guide/modules/configuration-guide/pages/replica-placement-plugins.adocindex 7dbdf828b81..82c5b7fd5de 100644--- a/solr/solr-ref-guide/modules/configuration-guide/pages/replica-placement-plugins.adoc+++ b/solr/solr-ref-guide/modules/configuration-guide/pages/replica-placement-plugins.adoc@@ -34,7 +34,11 @@ The configuration entry may also contain a `config` element if the plugin implem== Placement Plugins Included with SolrThe following placement plugins are available out-of-the-box in Solr 9.0.-If no placement plugin is defined, Solr will default to using the <<#simpleplacementfactory,SimplePlacementPlugin>>.+If no placement plugin is defined in cluster properties, Solr will default to using+the plugin configured in system property `solr.placementplugin.default` or environment+variable `SOLR_PLACEMENTPLUGIN_DEFAULT`. Supported values are `simple`, `random`, `affinity`+and `minimizecores`. If no such property or environment variable is set,+the <<#simpleplacementfactory,SimplePlacementPlugin>> is used.In order to use a plugin its configuration must be added using the `/cluster/plugin` API.For example, in order to use the `AffinityPlacementFactory` plugin the following command should be executed: