keith-turner commented on code in PR #3915:
URL: https://github.com/apache/accumulo/pull/3915#discussion_r1402652097


##########
core/src/test/java/org/apache/accumulo/core/util/compaction/CompactionServicesConfigTest.java:
##########
@@ -0,0 +1,106 @@
+/*
+ * 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
+ *
+ *   https://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.
+ */
+package org.apache.accumulo.core.util.compaction;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.Map;
+
+import org.apache.accumulo.core.conf.ConfigurationCopy;
+import org.apache.accumulo.core.conf.Property;
+import org.apache.accumulo.core.spi.compaction.DefaultCompactionPlanner;
+import org.junit.jupiter.api.Test;
+
+public class CompactionServicesConfigTest {
+
+  @SuppressWarnings("removal")
+  private final Property oldPrefix = Property.TSERV_COMPACTION_SERVICE_PREFIX;
+  private final Property newPrefix = Property.COMPACTION_SERVICE_PREFIX;
+
+  @Test
+  public void testCompactionProps() {
+    ConfigurationCopy conf = new ConfigurationCopy();
+
+    conf.set(newPrefix.getKey() + "default.planner", 
DefaultCompactionPlanner.class.getName());
+    conf.set(newPrefix.getKey() + "default.planner.opts.maxOpen", "10");
+    conf.set(newPrefix.getKey() + "default.planner.opts.executors",
+        
"[{'name':'small','type':'internal','maxSize':'32M','numThreads':2},{'name':'medium','type':'internal','maxSize':'128M','numThreads':2},{'name':'large','type':'internal','numThreads':2}]");
+
+    conf.set(oldPrefix.getKey() + "default.planner.opts.ignoredProp", "1");
+    conf.set(newPrefix.getKey() + "default.planner.opts.validProp", "1");
+    conf.set(oldPrefix.getKey() + "default.planner.opts.validProp", "a");
+
+    var compactionConfig = new CompactionServicesConfig(conf);
+    
assertTrue(compactionConfig.getOptions().get("default").containsKey("validProp"));
+    assertEquals("1", 
compactionConfig.getOptions().get("default").get("validProp"));
+    
assertNull(compactionConfig.getOptions().get("default").get("ignoredProp"));

Review Comment:
   ```suggestion
       assertEquals(Map.of("validProp","1"), 
compactionConfig.getOptions().get("default"));
   ```



##########
core/src/main/java/org/apache/accumulo/core/spi/compaction/DefaultCompactionPlanner.java:
##########
@@ -147,44 +186,78 @@ public String toString() {
       justification = "Field is written by Gson")
   @Override
   public void init(InitParameters params) {
-    ExecutorConfig[] execConfigs =
-        GSON.get().fromJson(params.getOptions().get("executors"), 
ExecutorConfig[].class);
-
     List<Executor> tmpExec = new ArrayList<>();
+    String values;
+
+    if (params.getOptions().containsKey("executors")
+        && !params.getOptions().get("executors").isBlank()) {
+      values = params.getOptions().get("executors");
 
-    for (ExecutorConfig executorConfig : execConfigs) {
-      Long maxSize = executorConfig.maxSize == null ? null
-          : 
ConfigurationTypeHelper.getFixedMemoryAsBytes(executorConfig.maxSize);
+      // Generate a list of fields from the desired object.
+      final List<String> execFields = 
Arrays.stream(ExecutorConfig.class.getDeclaredFields())
+          .map(Field::getName).collect(Collectors.toList());
 
-      CompactionExecutorId ceid;
+      for (JsonElement element : GSON.get().fromJson(values, JsonArray.class)) 
{
+        validateConfig(element, execFields, ExecutorConfig.class.getName());
+        ExecutorConfig executorConfig = GSON.get().fromJson(element, 
ExecutorConfig.class);
 
-      // If not supplied, GSON will leave type null. Default to internal
-      if (executorConfig.type == null) {
-        executorConfig.type = "internal";
+        Long maxSize = executorConfig.maxSize == null ? null
+            : 
ConfigurationTypeHelper.getFixedMemoryAsBytes(executorConfig.maxSize);
+        CompactionExecutorId ceid;
+
+        // If not supplied, GSON will leave type null. Default to internal
+        if (executorConfig.type == null) {
+          executorConfig.type = "internal";
+        }
+
+        switch (executorConfig.type) {
+          case "internal":
+            Preconditions.checkArgument(null == executorConfig.queue,
+                "'queue' should not be specified for internal compactions");
+            int numThreads = Objects.requireNonNull(executorConfig.numThreads,
+                "'numThreads' must be specified for internal type");
+            ceid = 
params.getExecutorManager().createExecutor(executorConfig.name, numThreads);
+            break;
+          case "external":
+            Preconditions.checkArgument(null == executorConfig.numThreads,
+                "'numThreads' should not be specified for external 
compactions");
+            String queue = Objects.requireNonNull(executorConfig.queue,
+                "'queue' must be specified for external type");
+            ceid = params.getExecutorManager().getExternalExecutor(queue);
+            break;
+          default:
+            throw new IllegalArgumentException("type must be 'internal' or 
'external'");
+        }
+        tmpExec.add(new Executor(ceid, maxSize));
       }
+    }
 
-      switch (executorConfig.type) {
-        case "internal":
-          Preconditions.checkArgument(null == executorConfig.queue,
-              "'queue' should not be specified for internal compactions");
-          int numThreads = Objects.requireNonNull(executorConfig.numThreads,
-              "'numThreads' must be specified for internal type");
-          ceid = 
params.getExecutorManager().createExecutor(executorConfig.name, numThreads);
-          break;
-        case "external":
-          Preconditions.checkArgument(null == executorConfig.numThreads,
-              "'numThreads' should not be specified for external compactions");
-          String queue = Objects.requireNonNull(executorConfig.queue,
-              "'queue' must be specified for external type");
-          ceid = params.getExecutorManager().getExternalExecutor(queue);
-          break;
-        default:
-          throw new IllegalArgumentException("type must be 'internal' or 
'external'");
+    if (params.getOptions().containsKey("queues") && 
!params.getOptions().get("queues").isBlank()) {
+      values = params.getOptions().get("queues");
+
+      // Generate a list of fields from the desired object.
+      final List<String> queueFields = 
Arrays.stream(QueueConfig.class.getDeclaredFields())
+          .map(Field::getName).collect(Collectors.toList());
+
+      for (JsonElement element : GSON.get().fromJson(values, JsonArray.class)) 
{
+        validateConfig(element, queueFields, QueueConfig.class.getName());
+        QueueConfig queueConfig = GSON.get().fromJson(element, 
QueueConfig.class);
+
+        Long maxSize = queueConfig.maxSize == null ? null
+            : 
ConfigurationTypeHelper.getFixedMemoryAsBytes(queueConfig.maxSize);
+
+        CompactionExecutorId ceid;
+        String queue = Objects.requireNonNull(queueConfig.name, "'name' must 
be specified");
+        ceid = params.getExecutorManager().getExternalExecutor(queue);
+        tmpExec.add(new Executor(ceid, maxSize));
       }
-      tmpExec.add(new Executor(ceid, maxSize));
     }
 
-    Collections.sort(tmpExec, Comparator.comparing(Executor::getMaxSize,
+    if (tmpExec.size() < 1) {

Review Comment:
   This is a nice addition.  If this check is not present in 2.1,  maybe 
worthwhile adding check for zero executors in 2.1



##########
core/src/main/java/org/apache/accumulo/core/util/compaction/CompactionServicesConfig.java:
##########
@@ -50,31 +58,94 @@ private long getDefaultThroughput() {
         
.getMemoryAsBytes(Property.TSERV_COMPACTION_SERVICE_DEFAULT_RATE_LIMIT.getDefaultValue());
   }
 
-  private Map<String,String> getConfiguration(AccumuloConfiguration aconf) {
-    return 
aconf.getAllPropertiesWithPrefix(Property.TSERV_COMPACTION_SERVICE_PREFIX);
+  private Map<String,Map<String,String>> 
getConfiguration(AccumuloConfiguration aconf) {
+    Map<String,Map<String,String>> properties = new HashMap<>();
+
+    var newProps = aconf.getAllPropertiesWithPrefixStripped(newPrefix);
+    properties.put(newPrefix.getKey(), newProps);
+
+    // get all of the services under the new prefix
+    var newServices =
+        newProps.keySet().stream().map(prop -> 
prop.split("\\.")[0]).collect(Collectors.toSet());
+
+    Map<String,String> oldServices = new HashMap<>();
+
+    for (Map.Entry<String,String> entry : 
aconf.getAllPropertiesWithPrefixStripped(oldPrefix)
+        .entrySet()) {
+      // Discard duplicate service definitions
+      var service = entry.getKey().split("\\.")[0];
+      if (newServices.contains(service)) {
+        log.warn("Duplicate compaction service '{}' definition exists. 
Ignoring property : '{}'",
+            service, entry.getKey());
+      } else {
+        oldServices.put(entry.getKey(), entry.getValue());
+      }
+    }
+    properties.put(oldPrefix.getKey(), oldServices);
+    // Return unmodifiable map
+    return Map.copyOf(properties);
   }
 
   public CompactionServicesConfig(AccumuloConfiguration aconf) {
-    Map<String,String> configs = getConfiguration(aconf);
-
-    configs.forEach((prop, val) -> {
-
-      var suffix = 
prop.substring(Property.TSERV_COMPACTION_SERVICE_PREFIX.getKey().length());
-      String[] tokens = suffix.split("\\.");
-      if (tokens.length == 4 && tokens[1].equals("planner") && 
tokens[2].equals("opts")) {
-        options.computeIfAbsent(tokens[0], k -> new 
HashMap<>()).put(tokens[3], val);
-      } else if (tokens.length == 2 && tokens[1].equals("planner")) {
-        planners.put(tokens[0], val);
-      } else if (tokens.length == 3 && tokens[1].equals("rate") && 
tokens[2].equals("limit")) {
-        var eprop = Property.getPropertyByKey(prop);
-        if (eprop == null || aconf.isPropertySet(eprop)) {
-          rateLimits.put(tokens[0], 
ConfigurationTypeHelper.getFixedMemoryAsBytes(val));
+    Map<String,Map<String,String>> configs = getConfiguration(aconf);
+
+    // Find compaction planner defs first.
+    configs.forEach((prefix, props) -> {
+      props.forEach((prop, val) -> {
+        String[] tokens = prop.split("\\.");
+        if (tokens.length == 2 && tokens[1].equals("planner")) {
+          if (prefix.equals(oldPrefix.getKey())) {
+            // Log a warning if the old prefix planner is defined by a user.
+            Property userDefined = null;
+            try {
+              userDefined = Property.valueOf(prefix + prop);
+            } catch (IllegalArgumentException e) {
+              log.trace("Property: {} is not set by default configuration", 
prefix + prop);
+            }
+            boolean isPropSet = true;
+            if (userDefined != null) {
+              isPropSet = aconf.isPropertySet(userDefined);
+            }
+            if (isPropSet) {
+              log.warn(
+                  "Found compaction planner '{}' using a deprecated prefix. 
Please update property to use the '{}' prefix",
+                  tokens[0], newPrefix);
+            }
+          }
+          plannerPrefixes.put(tokens[0], prefix);
+          planners.put(tokens[0], val);
         }
-      } else {
-        throw new IllegalArgumentException("Malformed compaction service 
property " + prop);
-      }
+      });
     });
 
+    // Now find all compaction planner options.
+    configs.forEach((prefix, props) -> {
+      props.forEach((prop, val) -> {
+        String[] tokens = prop.split("\\.");
+        if (!plannerPrefixes.containsKey(tokens[0])) {
+          throw new IllegalArgumentException(
+              "Incomplete compaction service definition, missing planner 
class: " + prop);
+        }
+        // Only add the compaction options if they match the planner's defined 
prefix.
+        if (plannerPrefixes.get(tokens[0]).equals(prefix)) {

Review Comment:
   This check may not be needed because `getConfiguration()` is already 
deduping at the service level, so should not get props with mixed prefixes from 
it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to