Search in sources :

Example 11 with InMemoryJarfile

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());
}
Also used : InMemoryJarfile(org.voltdb.utils.InMemoryJarfile) TPCCProjectBuilder(org.voltdb.benchmark.tpcc.TPCCProjectBuilder) File(java.io.File)

Example 12 with InMemoryJarfile

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;
}
Also used : InMemoryJarfile(org.voltdb.utils.InMemoryJarfile) Database(org.voltdb.catalog.Database) Cluster(org.voltdb.catalog.Cluster) IOException(java.io.IOException) Catalog(org.voltdb.catalog.Catalog) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException)

Example 13 with InMemoryJarfile

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;
}
Also used : InMemoryJarfile(org.voltdb.utils.InMemoryJarfile) Database(org.voltdb.catalog.Database) Catalog(org.voltdb.catalog.Catalog)

Example 14 with InMemoryJarfile

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;
    }
}
Also used : InMemoryJarfile(org.voltdb.utils.InMemoryJarfile) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException)

Example 15 with InMemoryJarfile

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) {
            }
        }
    }
}
Also used : InMemoryJarfile(org.voltdb.utils.InMemoryJarfile) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Aggregations

InMemoryJarfile (org.voltdb.utils.InMemoryJarfile)52 VoltCompiler (org.voltdb.compiler.VoltCompiler)26 File (java.io.File)20 ProcCallException (org.voltdb.client.ProcCallException)19 ClientResponse (org.voltdb.client.ClientResponse)18 Test (org.junit.Test)15 Configuration (org.voltdb.VoltDB.Configuration)15 IOException (java.io.IOException)14 VoltProjectBuilder (org.voltdb.compiler.VoltProjectBuilder)12 VoltDB (org.voltdb.VoltDB)9 Catalog (org.voltdb.catalog.Catalog)6 VoltTable (org.voltdb.VoltTable)5 VoltFile (org.voltdb.utils.VoltFile)5 ArrayList (java.util.ArrayList)4 JSONObject (org.json_voltpatches.JSONObject)3 CatalogContext (org.voltdb.CatalogContext)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 JAXBException (javax.xml.bind.JAXBException)2