use of org.voltdb.utils.InMemoryJarfile in project voltdb by VoltDB.
the class TestCatalogVersionUpgrade method testCatalogAutoUpgrade.
public void testCatalogAutoUpgrade() throws Exception {
TPCCProjectBuilder project = new TPCCProjectBuilder();
project.addDefaultSchema();
project.addDefaultPartitioning();
project.addDefaultProcedures();
String testDir = BuildDirectoryUtils.getBuildDirectoryPath();
String jarName = "compile-deployment.jar";
String catalogJar = testDir + File.separator + jarName;
assertTrue("Project failed to compile", project.compile(catalogJar));
// Load the catalog to an in-memory jar and tweak the version.
byte[] bytes = MiscUtils.fileToBytes(new File(catalogJar));
InMemoryJarfile memCatalog = CatalogUpgradeTools.loadCatalog(bytes, false);
CatalogUpgradeTools.dorkVersion(memCatalog);
// Load/upgrade and check against the server version.
InMemoryJarfile memCatalog2 = CatalogUpgradeTools.loadCatalog(memCatalog.getFullJarBytes(), true);
assertNotNull(memCatalog2);
String[] buildInfoLines2 = CatalogUpgradeTools.getBuildInfoLines(memCatalog2);
String serverVersion = VoltDB.instance().getVersionString();
assertTrue(serverVersion.equals(buildInfoLines2[0]));
// Make sure the jar file is present.
String jarName2 = String.format("catalog-%s.jar", serverVersion);
File jar2 = new File(VoltDB.Configuration.getPathToCatalogForTest(jarName2));
assertTrue(jar2.exists());
}
use of org.voltdb.utils.InMemoryJarfile in project voltdb by VoltDB.
the class VoltCompiler method compileInternal.
/**
* Internal method for compiling with and without a project.xml file or DDL files.
*
* @param projectReader Reader for project file or null if a project file is not used.
* @param ddlFilePaths The list of DDL files to compile (when no project is provided).
* @param jarOutputRet The in-memory jar to populate or null if the caller doesn't provide one.
* @return The InMemoryJarfile containing the compiled catalog if
* successful, null if not. If the caller provided an InMemoryJarfile, the
* return value will be the same object, not a copy.
*/
private InMemoryJarfile compileInternal(final VoltCompilerReader cannonicalDDLIfAny, final Catalog previousCatalogIfAny, final List<VoltCompilerReader> ddlReaderList, final InMemoryJarfile jarOutputRet) {
// Expect to have either >1 ddl file or a project file.
assert (ddlReaderList.size() > 0);
// Make a temporary local output jar if one wasn't provided.
final InMemoryJarfile jarOutput = (jarOutputRet != null ? jarOutputRet : new InMemoryJarfile());
if (ddlReaderList == null || ddlReaderList.isEmpty()) {
addErr("One or more DDL files are required.");
return null;
}
// clear out the warnings and errors
m_warnings.clear();
m_infos.clear();
m_errors.clear();
// do all the work to get the catalog
final Catalog catalog = compileCatalogInternal(cannonicalDDLIfAny, previousCatalogIfAny, ddlReaderList, jarOutput);
if (catalog == null) {
return null;
}
Cluster cluster = catalog.getClusters().get("cluster");
assert (cluster != null);
Database database = cluster.getDatabases().get("database");
assert (database != null);
// Build DDL from Catalog Data
String ddlWithBatchSupport = CatalogSchemaTools.toSchema(catalog, m_importLines);
m_canonicalDDL = CatalogSchemaTools.toSchemaWithoutInlineBatches(ddlWithBatchSupport);
// generate the catalog report and write it to disk
try {
generateCatalogReport(ddlWithBatchSupport);
} catch (IOException e) {
e.printStackTrace();
return null;
}
jarOutput.put(AUTOGEN_DDL_FILE_NAME, m_canonicalDDL.getBytes(Constants.UTF8ENCODING));
if (DEBUG_VERIFY_CATALOG) {
debugVerifyCatalog(jarOutput, catalog);
}
// WRITE CATALOG TO JAR HERE
final String catalogCommands = catalog.serialize();
byte[] catalogBytes = catalogCommands.getBytes(Constants.UTF8ENCODING);
try {
// Note when upgrading the version has already been updated by the caller.
if (!jarOutput.containsKey(CatalogUtil.CATALOG_BUILDINFO_FILENAME)) {
addBuildInfo(jarOutput);
}
jarOutput.put(CatalogUtil.CATALOG_FILENAME, catalogBytes);
// put the compiler report into the jarfile
jarOutput.put("catalog-report.html", m_report.getBytes(Constants.UTF8ENCODING));
} catch (final Exception e) {
e.printStackTrace();
return null;
}
assert (!hasErrors());
if (hasErrors()) {
return null;
}
return jarOutput;
}
use of org.voltdb.utils.InMemoryJarfile in project voltdb by VoltDB.
the class VoltCompiler method loadSchema.
/**
* Simplified interface for loading a ddl file with full support for VoltDB
* extensions (partitioning, procedures, export), but no support for "project file" input.
* This is, at least initially, only a back door to create a fully functional catalog for
* the purposes of planner unit testing.
* @param hsql an interface to the hsql frontend, initialized and potentially reused by the caller.
* @param whichProcs indicates which ddl-defined procedures to load: none, single-statement, or all
* @param ddlFilePaths schema file paths
* @throws VoltCompilerException
*/
public Catalog loadSchema(HSQLInterface hsql, DdlProceduresToLoad whichProcs, String... ddlFilePaths) throws VoltCompilerException {
//
m_catalog = new Catalog();
m_catalog.execute("add / clusters cluster");
Database db = initCatalogDatabase(m_catalog);
List<VoltCompilerReader> ddlReaderList = DDLPathsToReaderList(ddlFilePaths);
final VoltDDLElementTracker voltDdlTracker = new VoltDDLElementTracker(this);
InMemoryJarfile jarOutput = new InMemoryJarfile();
compileDatabase(db, hsql, voltDdlTracker, null, null, ddlReaderList, null, whichProcs, jarOutput);
return m_catalog;
}
use of org.voltdb.utils.InMemoryJarfile in project voltdb by VoltDB.
the class VoltCompiler method compileFromSchemaAndClasses.
/** Compiles a catalog from a user provided schema and (optional) jar file. */
public boolean compileFromSchemaAndClasses(final File schemaPath, final File classesJarPath, final File catalogOutputPath) {
if (schemaPath == null || !schemaPath.exists()) {
compilerLog.error("Cannot compile nonexistent or missing schema.");
return false;
}
List<VoltCompilerReader> ddlReaderList;
try {
ddlReaderList = DDLPathsToReaderList(schemaPath.getAbsolutePath());
} catch (VoltCompilerException e) {
compilerLog.error("Unable to open schema file \"" + schemaPath + "\"", e);
return false;
}
InMemoryJarfile inMemoryUserJar = null;
ClassLoader originalClassLoader = m_classLoader;
try {
if (classesJarPath != null && classesJarPath.exists()) {
// Make user's classes available to the compiler and add all VoltDB artifacts to theirs (overwriting any existing VoltDB artifacts).
// This keeps all their resources because stored procedures may depend on them.
inMemoryUserJar = new InMemoryJarfile(classesJarPath);
m_classLoader = inMemoryUserJar.getLoader();
} else {
inMemoryUserJar = new InMemoryJarfile();
}
if (compileInternal(null, null, ddlReaderList, inMemoryUserJar) == null) {
return false;
}
} catch (IOException e) {
compilerLog.error("Could not load classes from user supplied jar file", e);
return false;
} finally {
m_classLoader = originalClassLoader;
}
try {
inMemoryUserJar.writeToFile(catalogOutputPath).run();
return true;
} catch (final Exception e) {
e.printStackTrace();
addErr("Error writing catalog jar to disk: " + e.getMessage());
return false;
}
}
use of org.voltdb.utils.InMemoryJarfile in project voltdb by VoltDB.
the class VoltCompiler method compileInMemoryJarfileWithNewDDL.
/**
* Compile the provided jarfile. Basically, treat the jarfile as a staging area
* for the artifacts to be included in the compile, and then compile it in place.
*
* *NOTE*: Does *NOT* work with project.xml jarfiles.
*
* @return the compiled catalog is contained in the provided jarfile.
* @throws VoltCompilerException
*
*/
public void compileInMemoryJarfileWithNewDDL(InMemoryJarfile jarfile, String newDDL, Catalog oldCatalog) throws IOException, VoltCompilerException {
String oldDDL = new String(jarfile.get(VoltCompiler.AUTOGEN_DDL_FILE_NAME), Constants.UTF8ENCODING);
compilerLog.trace("OLD DDL: " + oldDDL);
VoltCompilerStringReader canonicalDDLReader = null;
VoltCompilerStringReader newDDLReader = null;
// Use the in-memory jarfile-provided class loader so that procedure
// classes can be found and copied to the new file that gets written.
ClassLoader originalClassLoader = m_classLoader;
try {
canonicalDDLReader = new VoltCompilerStringReader(VoltCompiler.AUTOGEN_DDL_FILE_NAME, oldDDL);
newDDLReader = new VoltCompilerStringReader("Ad Hoc DDL Input", newDDL);
List<VoltCompilerReader> ddlList = new ArrayList<>();
ddlList.add(newDDLReader);
m_classLoader = jarfile.getLoader();
// Do the compilation work.
InMemoryJarfile jarOut = compileInternal(canonicalDDLReader, oldCatalog, ddlList, jarfile);
// explanation
if (jarOut == null) {
String errString = "Adhoc DDL failed";
if (m_errors.size() > 0) {
errString = m_errors.get(m_errors.size() - 1).getLogString();
}
int endtrim = errString.indexOf(" in statement starting");
if (endtrim < 0) {
endtrim = errString.length();
}
String trimmed = errString.substring(0, endtrim);
throw new VoltCompilerException(trimmed);
}
compilerLog.debug("Successfully recompiled InMemoryJarfile");
} finally {
// Restore the original class loader
m_classLoader = originalClassLoader;
if (canonicalDDLReader != null) {
try {
canonicalDDLReader.close();
} catch (IOException ioe) {
}
}
if (newDDLReader != null) {
try {
newDDLReader.close();
} catch (IOException ioe) {
}
}
}
}
Aggregations