Skip to content

Commit

Permalink
ARTEMIS-4081 Comparing upgrades against fresh instances and some adju…
Browse files Browse the repository at this point in the history
…stments
  • Loading branch information
clebertsuconic committed Nov 8, 2022
1 parent 3040336 commit 03dec4e
Show file tree
Hide file tree
Showing 29 changed files with 398 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public Object run(ActionContext context) throws Exception {
return null;
}

protected String applyFilters(String content, Map<String, String> filters) {
protected static String applyFilters(String content, Map<String, String> filters) {
if (filters != null) {
for (Map.Entry<String, String> entry : filters.entrySet()) {
content = replace(content, entry.getKey(), entry.getValue());
Expand All @@ -103,11 +103,11 @@ protected String applyFilters(String content, Map<String, String> filters) {
return content;
}

protected String replace(String content, String key, String value) {
protected static String replace(String content, String key, String value) {
return content.replaceAll(Pattern.quote(key), Matcher.quoteReplacement(value));
}

protected void copy(InputStream is, OutputStream os) throws IOException {
protected static void copy(InputStream is, OutputStream os) throws IOException {
byte[] buffer = new byte[1024 * 4];
int c = is.read(buffer);
while (c >= 0) {
Expand Down Expand Up @@ -140,7 +140,7 @@ protected void write(String source,
}
}

protected String readTextFile(String source, Map<String, String> filters) throws IOException {
protected static String readTextFile(String source, Map<String, String> filters) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (InputStream in = openStream(source)) {
if (in == null) {
Expand All @@ -163,7 +163,7 @@ protected void write(String source, boolean force) throws IOException {
}
}

protected InputStream openStream(String source) {
return this.getClass().getResourceAsStream(source);
protected static InputStream openStream(String source) {
return InputAbstract.class.getResourceAsStream(source);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,20 @@
import java.util.stream.Stream;

import io.airlift.airline.Command;
import org.apache.activemq.artemis.util.JVMArgumentParser;

@Command(name = "upgrade", description = "Update an artemis instance to the current artemis.home, keeping all the data and broker.xml. Warning: backup your instance before using this command and compare the files.")
public class Upgrade extends InstallAbstract {

// these are the JVM argumnents we must keep between upgrades
private static final String[] KEEPING_JVM_ARGUMENTS = new String[]{"-Xmx", "-Djava.security.auth.login.config", "-Dhawtio.role="};

// this is the prefix where we can find the JDK arguments in Windows script
private static final String JDK_PREFIX_WINDOWS = "IF \"%JAVA_ARGS%\"==\"\" (set JAVA_ARGS=";

// this is the prefix where we can find the JDK arguments in Linux script
private static final String JDK_PREFIX_LINUX = "JAVA_ARGS=";

protected static final String OLD_LOGGING_PROPERTIES = "logging.properties";

/**
Expand Down Expand Up @@ -98,18 +108,23 @@ public Object run(ActionContext context) throws Exception {
Create.addScriptFilters(filters, getHome(), getInstance(), etcFolder, new File(getInstance(), "notUsed"), new File(getInstance(), "om-not-used.dmp"), javaMemory, javaOptions, "NA");

if (IS_WINDOWS) {
// recreating the service.exe in case we ever upgrade it
write(Create.BIN_ARTEMIS_SERVICE_EXE, true);
write(Create.BIN_ARTEMIS_SERVICE_EXE_CONFIG, true);

write(Create.BIN_ARTEMIS_CMD, new File(tmp, Create.ARTEMIS_CMD), filters, false, false);
upgrade(new File(tmp, Create.ARTEMIS_CMD), new File(bin, Create.ARTEMIS_CMD), binBkp, "set ARTEMIS_INSTANCE_ETC=");

write(Create.BIN_ARTEMIS_SERVICE_XML, new File(tmp, Create.ARTEMIS_SERVICE_XML), filters, false, false);
upgrade(new File(tmp, Create.ARTEMIS_SERVICE_XML), new File(bin, Create.ARTEMIS_SERVICE_XML), binBkp,
"<env name=\"ARTEMIS_INSTANCE\"", "<env name=\"ARTEMIS_INSTANCE_ETC\"",
"<env name=\"ARTEMIS_INSTANCE_URI\"", "<env name=\"ARTEMIS_INSTANCE_ETC_URI\"",
"<env name=\"ARTEMIS_DATA_DIR\"", "<logpath>", "<startargument>-Xmx", "<stopargument>-Xmx");
"<env name=\"ARTEMIS_DATA_DIR\"", "<logpath>", "<startargument>-Xmx", "<stopargument>-Xmx",
"<name>", "<id>", "<startargument>-Dhawtio.role=");

write("etc/" + Create.ETC_ARTEMIS_PROFILE_CMD, new File(tmp, Create.ETC_ARTEMIS_PROFILE_CMD), filters, false, false);
upgrade(new File(tmp, Create.ETC_ARTEMIS_PROFILE_CMD), new File(etcFolder, Create.ETC_ARTEMIS_PROFILE_CMD), binBkp,
"set ARTEMIS_INSTANCE=\"", "set ARTEMIS_DATA_DIR=", "set ARTEMIS_ETC_DIR=", "set ARTEMIS_OOME_DUMP=", "set ARTEMIS_INSTANCE_URI=", "set ARTEMIS_INSTANCE_ETC_URI=");
upgradeJDK(JDK_PREFIX_WINDOWS, "", KEEPING_JVM_ARGUMENTS, new File(tmp, Create.ETC_ARTEMIS_PROFILE_CMD), new File(etcFolder, Create.ETC_ARTEMIS_PROFILE_CMD), binBkp,
"set ARTEMIS_INSTANCE=\"", "set ARTEMIS_DATA_DIR=", "set ARTEMIS_ETC_DIR=", "set ARTEMIS_OOME_DUMP=", "set ARTEMIS_INSTANCE_URI=", "set ARTEMIS_INSTANCE_ETC_URI=");
}

if (!IS_WINDOWS || IS_CYGWIN) {
Expand All @@ -120,10 +135,15 @@ public Object run(ActionContext context) throws Exception {
upgrade(new File(tmp, Create.ARTEMIS_SERVICE), new File(bin, Create.ARTEMIS_SERVICE), binBkp); // we replace the whole thing

write("etc/" + Create.ETC_ARTEMIS_PROFILE, new File(tmp, Create.ETC_ARTEMIS_PROFILE), filters, false, false);
upgrade(new File(tmp, Create.ETC_ARTEMIS_PROFILE), new File(etcFolder, Create.ETC_ARTEMIS_PROFILE), etcBkp, "ARTEMIS_INSTANCE=",
"ARTEMIS_DATA_DIR=", "ARTEMIS_ETC_DIR=", "ARTEMIS_OOME_DUMP=", "ARTEMIS_INSTANCE_URI=", "ARTEMIS_INSTANCE_ETC_URI=", "HAWTIO_ROLE=");
upgradeJDK(JDK_PREFIX_LINUX, "\"", KEEPING_JVM_ARGUMENTS,
new File(tmp, Create.ETC_ARTEMIS_PROFILE), new File(etcFolder, Create.ETC_ARTEMIS_PROFILE), etcBkp, "ARTEMIS_INSTANCE=",
"ARTEMIS_DATA_DIR=", "ARTEMIS_ETC_DIR=", "ARTEMIS_OOME_DUMP=", "ARTEMIS_INSTANCE_URI=", "ARTEMIS_INSTANCE_ETC_URI=", "HAWTIO_ROLE=");
}


Files.copy( new File(etcFolder, Create.ETC_BOOTSTRAP_XML).toPath(), new File(tmp, Create.ETC_BOOTSTRAP_XML).toPath());
replaceLines(new File(tmp, Create.ETC_BOOTSTRAP_XML), new File(etcFolder, Create.ETC_BOOTSTRAP_XML), binBkp, "<web path", " <web path=\"web\" rootRedirectLocation=\"console\">");

upgradeLogging(context, etcBkp, etcFolder);

context.out.println();
Expand Down Expand Up @@ -158,6 +178,54 @@ private String getLine(File cmd, String pattern) throws IOException {
return null;
}


private void upgradeJDK(String jdkPrefix, String endOfLine, String[] keepArguments, File tmpFile, File targetFile, File bkp, String... keepingPrefixes) throws Exception {

final HashMap<String, String> replaceMatrix = new HashMap<>();
final HashMap<String, String> currentArguments = new HashMap<>();

doUpgrade(tmpFile, targetFile, bkp,
oldLine -> {
if (oldLine.trim().startsWith(jdkPrefix)) {
JVMArgumentParser.parseOriginalArgs(jdkPrefix, endOfLine, oldLine, keepArguments, currentArguments);
return;
} else {
for (String prefix : keepingPrefixes) {
if (oldLine.trim().startsWith(prefix)) {
replaceMatrix.put(prefix, oldLine);
}
}
}
},
newLine -> {
if (newLine.trim().startsWith(jdkPrefix)) {
String result = JVMArgumentParser.parseNewLine(jdkPrefix, endOfLine, newLine, keepArguments, currentArguments);
return result;
} else {
for (String prefix : keepingPrefixes) {
if (newLine.trim().startsWith(prefix)) {
String originalLine = replaceMatrix.get(prefix);
return originalLine;
}
}
return newLine;
}
});
}

private void replaceLines(File tmpFile, File targetFile, File bkp, String... replacePairs) throws Exception {
doUpgrade(tmpFile, targetFile, bkp,
null,
newLine -> {
for (int i = 0; i < replacePairs.length; i += 2) {
if (newLine.trim().startsWith(replacePairs[i])) {
return replacePairs[i + 1];
}
}
return newLine;
});
}

private void upgrade(File tmpFile, File targetFile, File bkp, String... keepingPrefixes) throws Exception {
HashMap<String, String> replaceMatrix = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.activemq.artemis.util;

import java.util.Map;

public class JVMArgumentParser {

public static void parseOriginalArgs(String prefix, String endOfLine, String originalLine, String[] keepingPrefixes, Map<String, String> originalArgs) {
originalLine = originalLine.trim();
String line = originalLine.substring(prefix.length(), originalLine.length() - endOfLine.length());
String[] split = line.split(" ");
for (String s : split) {
for (String k : keepingPrefixes) {
if (s.startsWith(k)) {
originalArgs.put(k, s);
}
}
}
}

public static String parseNewLine(String prefix, String endOfLine, String newLine, String[] keepingPrefixes, Map<String, String> originalArgs) {
String spacesBeginning = newLine.substring(0, newLine.indexOf(prefix));
newLine = newLine.trim();
StringBuffer output = new StringBuffer();
String line = newLine.substring(prefix.length(), newLine.length() - endOfLine.length());
String[] split = line.split(" ");
for (String s : split) {
for (String k : keepingPrefixes) {
if (s.startsWith(k)) {
String value = originalArgs.get(k);
if (value != null) {
s = value;
}
break;
}
}
output.append(s);
output.append(" ");
}

return spacesBeginning + prefix + output + endOfLine;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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.activemq.artemis.util;

import java.util.HashMap;

import org.junit.Assert;
import org.junit.Test;

public class JVMArgumentTest {

@Test
public void testArgumentsWindows() {
String arguments = "IF \"%JAVA_ARGS%\"==\"\" (set JAVA_ARGS= -must-go -XX:AutoBoxCacheMax=20000 -XX:+PrintClassHistogram -XX:+UseG1GC -XX:+UseStringDeduplication -Xms333M -Xmx77G -Djava.security.auth.login.config=%ARTEMIS_ETC_DIR%\\login.config -Dhawtio.disableProxy=true -Dhawtio.offline=true -Dhawtio.realm=activemq -Dhawtio.role=amq -Dhawtio.rolePrincipalClasses=org.apache.activemq.artemis.spi.core.security.jaas.RolePrincipal -Djolokia.policyLocation=%ARTEMIS_INSTANCE_ETC_URI%\\jolokia-access.xml -Dartemis.instance=%ARTEMIS_INSTANCE%)";

String prefix = "IF \"%JAVA_ARGS%\"==\"\" (set JAVA_ARGS= ";

String[] fixedArguments = new String[]{"-Xmx", "-Xms"};

HashMap<String, String> usedArgs = new HashMap<>();
JVMArgumentParser.parseOriginalArgs(prefix, "\"", arguments, fixedArguments, usedArgs);
Assert.assertEquals(2, usedArgs.size());
Assert.assertEquals("-Xmx77G", usedArgs.get("-Xmx"));
Assert.assertEquals("-Xms333M", usedArgs.get("-Xms"));

String newLine = "IF \"%JAVA_ARGS%\"==\"\" (set JAVA_ARGS= -XX:AutoBoxCacheMax=20000 -XX:+PrintClassHistogram -XX:+UseG1GC -XX:+UseStringDeduplication -Xms512M -Xmx1G -Djava.security.auth.login.config=%ARTEMIS_ETC_DIR%\\login.config -Dhawtio.disableProxy=true -Dhawtio.offline=true -Dhawtio.realm=activemq -Dhawtio.role=amq -Dhawtio.rolePrincipalClasses=org.apache.activemq.artemis.spi.core.security.jaas.RolePrincipal -Djolokia.policyLocation=%ARTEMIS_INSTANCE_ETC_URI%\\jolokia-access.xml -Dartemis.instance=%ARTEMIS_INSTANCE%)";

String resultLine = JVMArgumentParser.parseNewLine(prefix, "\"", newLine, fixedArguments, usedArgs);

System.out.println("output::" + resultLine);

Assert.assertFalse(resultLine.contains("-must-go"));
Assert.assertTrue(resultLine.contains("-Xmx77G"));
Assert.assertTrue(resultLine.contains("-Xms333M"));
Assert.assertFalse(resultLine.contains("-Xmx1G"));
Assert.assertFalse(resultLine.contains("-Xmx512M"));
}


@Test
public void testArgumentsLinux() {
String arguments = " JAVA_ARGS=\"-must-go -XX:AutoBoxCacheMax=20000 -XX:+PrintClassHistogram -XX:+UseG1GC -XX:+UseStringDeduplication -Xms333M -Xmx77G -Dhawtio.disableProxy=true -Dhawtio.realm=activemq -Dhawtio.offline=true -Dhawtio.rolePrincipalClasses=org.apache.activemq.artemis.spi.core.security.jaas.RolePrincipal -Djolokia.policyLocation=${ARTEMIS_INSTANCE_ETC_URI}jolokia-access.xml \"";

String prefix = "JAVA_ARGS=";

String[] fixedArguments = new String[]{"-Xmx", "-Xms"};

HashMap<String, String> usedArgs = new HashMap<>();
JVMArgumentParser.parseOriginalArgs(prefix, "\"", arguments, fixedArguments, usedArgs);
Assert.assertEquals(2, usedArgs.size());
Assert.assertEquals("-Xmx77G", usedArgs.get("-Xmx"));
Assert.assertEquals("-Xms333M", usedArgs.get("-Xms"));

String newLine = " JAVA_ARGS= -XX:AutoBoxCacheMax=20000 -XX:+PrintClassHistogram -XX:+UseG1GC -XX:+UseStringDeduplication -Xms512M -Xmx1G -Djava.security.auth.login.config=%ARTEMIS_ETC_DIR%\\login.config -Dhawtio.disableProxy=true -Dhawtio.offline=true -Dhawtio.realm=activemq -Dhawtio.role=amq -Dhawtio.rolePrincipalClasses=org.apache.activemq.artemis.spi.core.security.jaas.RolePrincipal -Djolokia.policyLocation=%ARTEMIS_INSTANCE_ETC_URI%\\jolokia-access.xml -Dartemis.instance=%ARTEMIS_INSTANCE%)";

String resultLine = JVMArgumentParser.parseNewLine(prefix, "\"", newLine, fixedArguments, usedArgs);

System.out.println("output::" + resultLine);

Assert.assertFalse(resultLine.contains("-must-go"));
Assert.assertTrue(resultLine.contains("-Xmx77G"));
Assert.assertTrue(resultLine.contains("-Xms333M"));
Assert.assertFalse(resultLine.contains("-Xmx1G"));
Assert.assertFalse(resultLine.contains("-Xmx512M"));

Assert.assertTrue(resultLine.startsWith(" "));
}

}
54 changes: 51 additions & 3 deletions tests/smoke-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1224,12 +1224,14 @@
</execution>
<execution>
<phase>test-compile</phase>
<id>upgrade</id>
<id>upgrade-linux</id>
<goals>
<goal>upgrade</goal>
</goals>
<configuration>
<instance>${basedir}/target/classes/servers/toUpgradeTest</instance>
<instance>${basedir}/target/classes/servers/linuxUpgrade</instance>
<!-- we don't pass the java memory argumnent on purpose,
as the upgrade should keep the relevant JVM arguments during the upgrade -->
</configuration>
</execution>
<execution>
Expand All @@ -1239,12 +1241,58 @@
<goal>upgrade</goal>
</goals>
<configuration>
<instance>${basedir}/target/classes/servers/windows</instance>
<instance>${basedir}/target/classes/servers/windowsUpgrade</instance>
<args>
<arg>--windows</arg>
<!-- we don't pass the java memory argumnent on purpose,
as the upgrade should keep the relevant JVM arguments during the upgrade -->
</args>
</configuration>
</execution>
<execution>
<phase>test-compile</phase>
<id>createExpectedWindows</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<allowAnonymous>true</allowAnonymous>
<instance>${basedir}/target/classes/servers/windowsUpgradeExpected</instance>
<noWeb>false</noWeb>
<role>amq</role>
<user>y</user>
<password>y</password>
<args>
<arg>--windows</arg>
<arg>--etc</arg>
<arg>${basedir}/target/classes/servers/windowsUpgradeETCExpected</arg>
<arg>--java-memory</arg>
<arg>1G</arg>
</args>
</configuration>
</execution>
<execution>
<phase>test-compile</phase>
<id>createExpectedLinux</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<allowAnonymous>true</allowAnonymous>
<instance>${basedir}/target/classes/servers/linuxUpgradeExpected</instance>
<noWeb>false</noWeb>
<role>amq</role>
<user>y</user>
<password>y</password>
<args>
<arg>--etc</arg>
<arg>${basedir}/target/classes/servers/linuxUpgradeETCExpected</arg>
<arg>--java-memory</arg>
<arg>1G</arg>
</args>
</configuration>
</execution>

</executions>
<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
## the upgrade should keep this line in the new script


ARTEMIS_INSTANCE_ETC='${project.basedir}/target/classes/servers/toUpgradeETC'
ARTEMIS_INSTANCE_ETC='${project.basedir}/target/classes/servers/linuxUpgradeETC'
Loading

0 comments on commit 03dec4e

Please sign in to comment.