dlmarion commented on a change in pull request #2238: URL: https://github.com/apache/accumulo/pull/2238#discussion_r717577787
########## File path: core/src/main/java/org/apache/accumulo/core/conf/cluster/ClusterConfigParser.java ########## @@ -0,0 +1,125 @@ +/* + * 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. + */ +package org.apache.accumulo.core.conf.cluster; + +import java.io.IOException; +import java.io.InputStream; +import java.io.PrintStream; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.lang3.StringUtils; +import org.yaml.snakeyaml.Yaml; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + +public class ClusterConfigParser { + + private static final String PROPERTY_FORMAT = "%s=\"%s\""; + private static final String[] SECTIONS = + new String[] {"manager", "monitor", "gc", "tracer", "tserver"}; + + @SuppressFBWarnings(value = "PATH_TRAVERSAL_IN", justification = "paths not set by user input") + public static Map<String,String> parseConfiguration(String configFile) throws IOException { + Map<String,String> results = new HashMap<>(); + try (InputStream fis = Files.newInputStream(Paths.get(configFile), StandardOpenOption.READ)) { + Yaml y = new Yaml(); + Map<String,Object> config = y.load(fis); + config.forEach((k, v) -> flatten("", k, v, results)); + } + return results; + } + + private static String addTheDot(String key) { + return (key.endsWith(".")) ? "" : "."; + } + + @SuppressWarnings("unchecked") Review comment: Not according to my IDE. ########## File path: assemble/bin/accumulo-cluster ########## @@ -43,52 +43,68 @@ function invalid_args { exit 1 } -function verify_config { +function parse_fail { + echo "Failed to parse ${conf}/cluster.yml" + exit 1 +} + +function parse_config { if [[ -f ${conf}/slaves ]]; then echo "ERROR: A 'slaves' file was found in ${conf}/" - echo "Accumulo now reads tablet server hosts from 'tservers' and requires that the 'slaves' file not be present to reduce confusion." + echo "Accumulo now uses cluster host configuration information from 'cluster.yml' and requires that the 'slaves' file not be present to reduce confusion." echo "Please rename the 'slaves' file to 'tservers' or remove it if both exist." exit 1 fi - if [[ ! -f ${conf}/tservers ]]; then - echo "ERROR: A 'tservers' file was not found at ${conf}/tservers" - echo "Please make sure it exists and is configured with tablet server hosts." + if [[ ! -f ${conf}/cluster.yml ]]; then + echo "ERROR: A 'cluster.yml' file was not found at ${conf}/cluster.yml" + echo "Please make sure it exists and is configured with the host information. Run 'create-config' to create an example configuration." exit 1 fi - unset manager1 - if [[ -f "${conf}/$manager_file" ]]; then - manager1=$(grep -Ev '(^#|^\s*$)' "${conf}/$manager_file" | head -1) + trap 'rm -f "$CONFIG_FILE"' EXIT + CONFIG_FILE=$(mktemp) || exit 1 + ${accumulo_cmd} org.apache.accumulo.core.conf.cluster.ClusterConfigParser ${conf}/cluster.yml > $CONFIG_FILE || parse_fail Review comment: Thanks for the reference to ShellCheck. I ran the script with that and fixed most issues. ########## File path: core/src/test/resources/org/apache/accumulo/core/conf/cluster/cluster.yml ########## @@ -0,0 +1,53 @@ +# Review comment: I fixed this in the changes I pushed today (9/28) ########## File path: core/src/test/java/org/apache/accumulo/core/conf/cluster/ClusterConfigParserTest.java ########## @@ -0,0 +1,145 @@ +/* + * 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. + */ +package org.apache.accumulo.core.conf.cluster; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.BufferedReader; +import java.io.File; +import java.io.PrintStream; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + +@SuppressFBWarnings(value = "PATH_TRAVERSAL_IN", justification = "paths provided by test") +public class ClusterConfigParserTest { + + @Rule + public TemporaryFolder tmp = new TemporaryFolder(); + + @Test + public void testParse() throws Exception { + URL configFile = ClusterConfigParserTest.class + .getResource("/org/apache/accumulo/core/conf/cluster/cluster.yml"); + assertNotNull(configFile); + + Map<String,String> contents = + ClusterConfigParser.parseConfiguration(new File(configFile.toURI()).getAbsolutePath()); + assertEquals(5, contents.size()); + assertTrue(contents.containsKey("manager")); + assertEquals("localhost1 localhost2", contents.get("manager")); + assertTrue(contents.containsKey("monitor")); + assertEquals("localhost1 localhost2", contents.get("monitor")); + assertTrue(contents.containsKey("tracer")); + assertEquals("localhost", contents.get("tracer")); + assertTrue(contents.containsKey("gc")); + assertEquals("localhost", contents.get("gc")); + assertTrue(contents.containsKey("tserver")); + assertEquals("localhost1 localhost2 localhost3 localhost4", contents.get("tserver")); + assertFalse(contents.containsKey("compaction")); + assertFalse(contents.containsKey("compaction.coordinator")); + assertFalse(contents.containsKey("compaction.compactor")); + assertFalse(contents.containsKey("compaction.compactor.queue")); + assertFalse(contents.containsKey("compaction.compactor.q1")); + assertFalse(contents.containsKey("compaction.compactor.q2")); + } + + @Test + public void testParseWithExternalCompactions() throws Exception { + URL configFile = ClusterConfigParserTest.class.getResource( + "/org/apache/accumulo/core/conf/cluster/cluster-with-external-compactions.yml"); + assertNotNull(configFile); + + Map<String,String> contents = + ClusterConfigParser.parseConfiguration(new File(configFile.toURI()).getAbsolutePath()); + assertEquals(9, contents.size()); + assertTrue(contents.containsKey("manager")); + assertEquals("localhost1 localhost2", contents.get("manager")); + assertTrue(contents.containsKey("monitor")); + assertEquals("localhost1 localhost2", contents.get("monitor")); + assertTrue(contents.containsKey("tracer")); + assertEquals("localhost", contents.get("tracer")); + assertTrue(contents.containsKey("gc")); + assertEquals("localhost", contents.get("gc")); + assertTrue(contents.containsKey("tserver")); + assertEquals("localhost1 localhost2 localhost3 localhost4", contents.get("tserver")); + assertFalse(contents.containsKey("compaction")); + assertTrue(contents.containsKey("compaction.coordinator")); + assertEquals("localhost1 localhost2", contents.get("compaction.coordinator")); + assertFalse(contents.containsKey("compaction.compactor")); + assertTrue(contents.containsKey("compaction.compactor.queue")); + assertEquals("q1 q2", contents.get("compaction.compactor.queue")); + assertTrue(contents.containsKey("compaction.compactor.q1")); + assertEquals("localhost1 localhost2", contents.get("compaction.compactor.q1")); + assertTrue(contents.containsKey("compaction.compactor.q2")); + assertEquals("localhost1 localhost2", contents.get("compaction.compactor.q2")); + } + + @Test + public void testShellOutput() throws Exception { + + File f = tmp.newFile(); + f.deleteOnExit(); + + PrintStream ps = new PrintStream(f); + + URL configFile = ClusterConfigParserTest.class.getResource( + "/org/apache/accumulo/core/conf/cluster/cluster-with-external-compactions.yml"); + assertNotNull(configFile); + + Map<String,String> contents = + ClusterConfigParser.parseConfiguration(new File(configFile.toURI()).getAbsolutePath()); + + ClusterConfigParser.outputShellVariables(contents, ps); + ps.close(); + + Map<String,String> expected = new HashMap<>(); Review comment: I fixed this in the changes I pushed today (9/28) ########## File path: core/src/test/java/org/apache/accumulo/core/conf/cluster/ClusterConfigParserTest.java ########## @@ -0,0 +1,145 @@ +/* + * 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. + */ +package org.apache.accumulo.core.conf.cluster; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.BufferedReader; +import java.io.File; +import java.io.PrintStream; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + +@SuppressFBWarnings(value = "PATH_TRAVERSAL_IN", justification = "paths provided by test") +public class ClusterConfigParserTest { + + @Rule + public TemporaryFolder tmp = new TemporaryFolder(); Review comment: I fixed this in the changes I pushed today (9/28) ########## File path: core/src/main/java/org/apache/accumulo/core/conf/cluster/ClusterConfigParser.java ########## @@ -0,0 +1,125 @@ +/* + * 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. + */ +package org.apache.accumulo.core.conf.cluster; + +import java.io.IOException; +import java.io.InputStream; +import java.io.PrintStream; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.lang3.StringUtils; +import org.yaml.snakeyaml.Yaml; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + +public class ClusterConfigParser { + + private static final String PROPERTY_FORMAT = "%s=\"%s\""; + private static final String[] SECTIONS = + new String[] {"manager", "monitor", "gc", "tracer", "tserver"}; + + @SuppressFBWarnings(value = "PATH_TRAVERSAL_IN", justification = "paths not set by user input") + public static Map<String,String> parseConfiguration(String configFile) throws IOException { + Map<String,String> results = new HashMap<>(); + try (InputStream fis = Files.newInputStream(Paths.get(configFile), StandardOpenOption.READ)) { + Yaml y = new Yaml(); + Map<String,Object> config = y.load(fis); + config.forEach((k, v) -> flatten("", k, v, results)); + } + return results; + } + + private static String addTheDot(String key) { + return (key.endsWith(".")) ? "" : "."; + } + + @SuppressWarnings("unchecked") Review comment: Not according to my IDE ########## File path: core/src/test/java/org/apache/accumulo/core/conf/cluster/ClusterConfigParserTest.java ########## @@ -0,0 +1,145 @@ +/* + * 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. + */ +package org.apache.accumulo.core.conf.cluster; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.BufferedReader; +import java.io.File; +import java.io.PrintStream; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + +@SuppressFBWarnings(value = "PATH_TRAVERSAL_IN", justification = "paths provided by test") +public class ClusterConfigParserTest { + + @Rule + public TemporaryFolder tmp = new TemporaryFolder(); Review comment: Lol, I didn't realize you could do that, I just took your suggestion literally. -- 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]
