Search in sources :

Example 26 with Catalog

use of org.voltdb.catalog.Catalog in project voltdb by VoltDB.

the class TestCatalogSerialization method testSimplest.

public void testSimplest() throws IOException {
    Catalog catalog1 = TPCCProjectBuilder.getTPCCSchemaCatalog();
    String commands = catalog1.serialize();
    // System.out.println(commands);
    Catalog catalog2 = new Catalog();
    try {
        catalog2.execute(commands);
    } catch (Exception e) {
        e.printStackTrace();
    }
    String commands2 = catalog2.serialize();
    Catalog catalog3 = catalog2.deepCopy();
    String commands3 = catalog3.serialize();
    assertTrue(commands.equals(commands2));
    assertTrue(commands.equals(commands3));
    assertTrue(catalog1.equals(catalog2));
    assertTrue(catalog1.equals(catalog3));
}
Also used : Catalog(org.voltdb.catalog.Catalog) IOException(java.io.IOException)

Example 27 with Catalog

use of org.voltdb.catalog.Catalog in project voltdb by VoltDB.

the class UpdateApplicationBase method prepareApplicationCatalogDiff.

/**
     *
     * @param operationBytes The bytes for the catalog operation, if any. May be null in all cases.
     * For UpdateApplicationCatalog, this will contain the compiled catalog jarfile bytes
     * For UpdateClasses, this will contain the class jarfile bytes
     * For AdHoc DDL work, this will be null
     * @param operationString The string for the catalog operation, if any. May be null in all cases.
     * For UpdateApplicationCatalog, this will contain the deployment string to apply
     * For UpdateClasses, this will contain the class deletion patterns
     * For AdHoc DDL work, this will be null
     */
public static CatalogChangeResult prepareApplicationCatalogDiff(String invocationName, final byte[] operationBytes, final String operationString, final String[] adhocDDLStmts, final byte[] replayHashOverride, final boolean isPromotion, final DrRoleType drRole, final boolean useAdhocDDL, boolean adminConnection, String hostname, String user) throws PrepareDiffFailureException {
    // create the change result and set up all the boiler plate
    CatalogChangeResult retval = new CatalogChangeResult();
    // ensure non-null
    retval.tablesThatMustBeEmpty = new String[0];
    retval.hasSchemaChange = true;
    if (replayHashOverride != null) {
        retval.isForReplay = true;
    }
    try {
        // catalog change specific boiler plate
        CatalogContext context = VoltDB.instance().getCatalogContext();
        // Start by assuming we're doing an @UpdateApplicationCatalog.  If-ladder below
        // will complete with newCatalogBytes actually containing the bytes of the
        // catalog to be applied, and deploymentString will contain an actual deployment string,
        // or null if it still needs to be filled in.
        InMemoryJarfile newCatalogJar = null;
        InMemoryJarfile oldJar = context.getCatalogJar().deepCopy();
        boolean updatedClass = false;
        String deploymentString = operationString;
        if ("@UpdateApplicationCatalog".equals(invocationName)) {
            // Grab the current catalog bytes if @UAC had a null catalog from deployment-only update
            if ((operationBytes == null) || (operationBytes.length == 0)) {
                newCatalogJar = oldJar;
            } else {
                newCatalogJar = CatalogUtil.loadInMemoryJarFile(operationBytes);
            }
        // If the deploymentString is null, we'll fill it in with current deployment later
        // Otherwise, deploymentString has the right contents, don't need to touch it
        } else if ("@UpdateClasses".equals(invocationName)) {
            // provided newCatalogJar is the jarfile with the new classes
            if (operationBytes != null) {
                newCatalogJar = new InMemoryJarfile(operationBytes);
            }
            try {
                InMemoryJarfile modifiedJar = modifyCatalogClasses(context.catalog, oldJar, operationString, newCatalogJar, drRole == DrRoleType.XDCR);
                if (modifiedJar == null) {
                    newCatalogJar = oldJar;
                } else {
                    newCatalogJar = modifiedJar;
                    updatedClass = true;
                }
            } catch (ClassNotFoundException e) {
                throw new PrepareDiffFailureException(ClientResponse.GRACEFUL_FAILURE, "Unexpected error in @UpdateClasses modifying classes from catalog: " + e.getMessage());
            }
            // Real deploymentString should be the current deployment, just set it to null
            // here and let it get filled in correctly later.
            deploymentString = null;
            // mark it as non-schema change
            retval.hasSchemaChange = false;
        } else if ("@AdHoc".equals(invocationName)) {
            // work.adhocDDLStmts should be applied to the current catalog
            try {
                newCatalogJar = addDDLToCatalog(context.catalog, oldJar, adhocDDLStmts, drRole == DrRoleType.XDCR);
            } catch (VoltCompilerException vce) {
                throw new PrepareDiffFailureException(ClientResponse.GRACEFUL_FAILURE, vce.getMessage());
            } catch (IOException ioe) {
                throw new PrepareDiffFailureException(ClientResponse.UNEXPECTED_FAILURE, ioe.getMessage());
            } catch (Throwable t) {
                String msg = "Unexpected condition occurred applying DDL statements: " + t.toString();
                compilerLog.error(msg);
                throw new PrepareDiffFailureException(ClientResponse.UNEXPECTED_FAILURE, msg);
            }
            assert (newCatalogJar != null);
            if (newCatalogJar == null) {
                // Shouldn't ever get here
                String msg = "Unexpected failure in applying DDL statements to original catalog";
                compilerLog.error(msg);
                throw new PrepareDiffFailureException(ClientResponse.UNEXPECTED_FAILURE, msg);
            }
            // Real deploymentString should be the current deployment, just set it to null
            // here and let it get filled in correctly later.
            deploymentString = null;
        } else {
            // TODO: this if-chain doesn't feel like it even should exist
            assert (false);
        }
        // get the diff between catalogs
        // try to get the new catalog from the params
        Pair<InMemoryJarfile, String> loadResults = null;
        try {
            loadResults = CatalogUtil.loadAndUpgradeCatalogFromJar(newCatalogJar, drRole == DrRoleType.XDCR);
        } catch (IOException ioe) {
            // falling through to the ZOMG message in the big catch
            throw new PrepareDiffFailureException(ClientResponse.GRACEFUL_FAILURE, ioe.getMessage());
        }
        retval.catalogBytes = loadResults.getFirst().getFullJarBytes();
        if (!retval.isForReplay) {
            retval.catalogHash = loadResults.getFirst().getSha1Hash();
        } else {
            retval.catalogHash = replayHashOverride;
        }
        String newCatalogCommands = CatalogUtil.getSerializedCatalogStringFromJar(loadResults.getFirst());
        retval.upgradedFromVersion = loadResults.getSecond();
        if (newCatalogCommands == null) {
            throw new PrepareDiffFailureException(ClientResponse.GRACEFUL_FAILURE, "Unable to read from catalog bytes");
        }
        Catalog newCatalog = new Catalog();
        newCatalog.execute(newCatalogCommands);
        // Retrieve the original deployment string, if necessary
        if (deploymentString == null) {
            // Go get the deployment string from the current catalog context
            byte[] deploymentBytes = context.getDeploymentBytes();
            if (deploymentBytes != null) {
                deploymentString = new String(deploymentBytes, Constants.UTF8ENCODING);
            }
            if (deploymentBytes == null || deploymentString == null) {
                throw new PrepareDiffFailureException(ClientResponse.GRACEFUL_FAILURE, "No deployment file provided and unable to recover previous deployment settings.");
            }
        }
        DeploymentType dt = CatalogUtil.parseDeploymentFromString(deploymentString);
        if (dt == null) {
            throw new PrepareDiffFailureException(ClientResponse.GRACEFUL_FAILURE, "Unable to update deployment configuration: Error parsing deployment string");
        }
        if (isPromotion && drRole == DrRoleType.REPLICA) {
            assert dt.getDr().getRole() == DrRoleType.REPLICA;
            dt.getDr().setRole(DrRoleType.MASTER);
        }
        String result = CatalogUtil.compileDeployment(newCatalog, dt, false);
        if (result != null) {
            throw new PrepareDiffFailureException(ClientResponse.GRACEFUL_FAILURE, "Unable to update deployment configuration: " + result);
        }
        //In non legacy mode discard the path element.
        if (!VoltDB.instance().isRunningWithOldVerbs()) {
            dt.setPaths(null);
        }
        //Always get deployment after its adjusted.
        retval.deploymentString = CatalogUtil.getDeployment(dt, true);
        retval.deploymentHash = CatalogUtil.makeDeploymentHash(retval.deploymentString.getBytes(Constants.UTF8ENCODING));
        // store the version of the catalog the diffs were created against.
        // verified when / if the update procedure runs in order to verify
        // catalogs only move forward
        retval.expectedCatalogVersion = context.catalogVersion;
        // compute the diff in StringBuilder
        CatalogDiffEngine diff = new CatalogDiffEngine(context.catalog, newCatalog);
        if (!diff.supported()) {
            throw new PrepareDiffFailureException(ClientResponse.GRACEFUL_FAILURE, "The requested catalog change(s) are not supported:\n" + diff.errors());
        }
        String commands = diff.commands();
        compilerLog.info(diff.getDescriptionOfChanges(updatedClass));
        retval.requireCatalogDiffCmdsApplyToEE = diff.requiresCatalogDiffCmdsApplyToEE();
        // since diff commands can be stupidly big, compress them here
        retval.encodedDiffCommands = Encoder.compressAndBase64Encode(commands);
        retval.diffCommandsLength = commands.length();
        String[][] emptyTablesAndReasons = diff.tablesThatMustBeEmpty();
        assert (emptyTablesAndReasons.length == 2);
        assert (emptyTablesAndReasons[0].length == emptyTablesAndReasons[1].length);
        retval.tablesThatMustBeEmpty = emptyTablesAndReasons[0];
        retval.reasonsForEmptyTables = emptyTablesAndReasons[1];
        retval.requiresSnapshotIsolation = diff.requiresSnapshotIsolation();
        retval.requiresNewExportGeneration = diff.requiresNewExportGeneration();
        retval.worksWithElastic = diff.worksWithElastic();
    } catch (PrepareDiffFailureException e) {
        throw e;
    } catch (Exception e) {
        String msg = "Unexpected error in adhoc or catalog update: " + e.getClass() + ", " + e.getMessage();
        compilerLog.warn(msg, e);
        throw new PrepareDiffFailureException(ClientResponse.UNEXPECTED_FAILURE, msg);
    }
    return retval;
}
Also used : CatalogDiffEngine(org.voltdb.catalog.CatalogDiffEngine) IOException(java.io.IOException) DeploymentType(org.voltdb.compiler.deploymentfile.DeploymentType) PrepareDiffFailureException(org.voltdb.compiler.CatalogChangeResult.PrepareDiffFailureException) Catalog(org.voltdb.catalog.Catalog) PrepareDiffFailureException(org.voltdb.compiler.CatalogChangeResult.PrepareDiffFailureException) VoltCompilerException(org.voltdb.compiler.VoltCompiler.VoltCompilerException) IOException(java.io.IOException) CatalogChangeResult(org.voltdb.compiler.CatalogChangeResult) InMemoryJarfile(org.voltdb.utils.InMemoryJarfile) VoltCompilerException(org.voltdb.compiler.VoltCompiler.VoltCompilerException) CatalogContext(org.voltdb.CatalogContext)

Example 28 with Catalog

use of org.voltdb.catalog.Catalog in project voltdb by VoltDB.

the class TestInitStartAction method validateStagedCatalog.

/*
     * "voltdb init --schema --procedures" tests:
     * 1.  Positive test with valid schema that requires no procedures
     * 2a. Positive test with valid schema and procedures that are in CLASSPATH
     * 2b. Negative test with valid files but not "init --force"
     * 3.  Negative test with a bad schema
     * 4.  Negative test with procedures missing
     *
     * Note that SimulatedExitException is thrown by the command line parser with no descriptive details.
     * VoltDB.crashLocalVoltDB() throws an AssertionError with the message "Faux crash of VoltDB successful."
     */
/** Verifies that the staged catalog matches what VoltCompiler emits given the supplied schema.
     * @param schema Schema used to generate the staged catalog
     * @throws Exception upon test failure or error (unable to write temp file for example)
     */
private void validateStagedCatalog(String schema, InMemoryJarfile proceduresJar) throws Exception {
    // setup reference point for the supplied schema
    File schemaFile = VoltProjectBuilder.writeStringToTempFile(schema);
    schemaFile.deleteOnExit();
    File referenceFile = File.createTempFile("reference", ".jar");
    referenceFile.deleteOnExit();
    VoltCompiler compiler = new VoltCompiler(false);
    compiler.setInitializeDDLWithFiltering(true);
    final boolean success = compiler.compileFromDDL(referenceFile.getAbsolutePath(), schemaFile.getPath());
    assertEquals(true, success);
    InMemoryJarfile referenceCatalogJar = new InMemoryJarfile(referenceFile);
    Catalog referenceCatalog = new Catalog();
    referenceCatalog.execute(CatalogUtil.getSerializedCatalogStringFromJar(referenceCatalogJar));
    // verify that the staged catalog is identical
    File stagedJarFile = new VoltFile(RealVoltDB.getStagedCatalogPath(rootDH.getPath() + File.separator + "voltdbroot"));
    assertEquals(true, stagedJarFile.isFile());
    InMemoryJarfile stagedCatalogJar = new InMemoryJarfile(stagedJarFile);
    Catalog stagedCatalog = new Catalog();
    stagedCatalog.execute(CatalogUtil.getSerializedCatalogStringFromJar(stagedCatalogJar));
    assertEquals(true, referenceCatalog.equals(stagedCatalog));
    assertEquals(true, stagedCatalog.equals(referenceCatalog));
    assertEquals(true, referenceFile.delete());
    assertEquals(true, schemaFile.delete());
    if (proceduresJar != null) {
        // Validate that the list of files in the supplied jarfile are present in the staged catalog also.
        InMemoryJarfile strippedReferenceJar = CatalogUtil.getCatalogJarWithoutDefaultArtifacts(proceduresJar);
        InMemoryJarfile strippedTestJar = CatalogUtil.getCatalogJarWithoutDefaultArtifacts(stagedCatalogJar);
        for (Entry<String, byte[]> entry : strippedReferenceJar.entrySet()) {
            System.out.println("Checking " + entry.getKey());
            byte[] testClass = strippedTestJar.get(entry.getKey());
            assertNotNull(entry.getKey() + " was not found in staged catalog", testClass);
            assertArrayEquals(entry.getValue(), testClass);
        }
    }
}
Also used : VoltCompiler(org.voltdb.compiler.VoltCompiler) VoltFile(org.voltdb.utils.VoltFile) InMemoryJarfile(org.voltdb.utils.InMemoryJarfile) VoltFile(org.voltdb.utils.VoltFile) File(java.io.File) Catalog(org.voltdb.catalog.Catalog)

Example 29 with Catalog

use of org.voltdb.catalog.Catalog in project voltdb by VoltDB.

the class TPCCProjectBuilder method compileAllCatalogs.

@Override
public String[] compileAllCatalogs(int sitesPerHost, int length, int kFactor, String voltRoot) {
    addAllDefaults();
    Catalog catalog = compile(m_jarFileName, sitesPerHost, length, kFactor, voltRoot);
    if (catalog == null) {
        throw new RuntimeException("Bingo project builder failed app compilation.");
    }
    return new String[] { m_jarFileName };
}
Also used : Catalog(org.voltdb.catalog.Catalog)

Example 30 with Catalog

use of org.voltdb.catalog.Catalog in project voltdb by VoltDB.

the class RegressionSuite method getCurrentCatalog.

private static Catalog getCurrentCatalog() {
    CatalogContext context = VoltDB.instance().getCatalogContext();
    if (context == null) {
        return null;
    }
    InMemoryJarfile currentCatalogJar = context.getCatalogJar();
    String serializedCatalogString = CatalogUtil.getSerializedCatalogStringFromJar(currentCatalogJar);
    assertNotNull(serializedCatalogString);
    Catalog c = new Catalog();
    c.execute(serializedCatalogString);
    return c;
}
Also used : InMemoryJarfile(org.voltdb.utils.InMemoryJarfile) CatalogContext(org.voltdb.CatalogContext) Catalog(org.voltdb.catalog.Catalog)

Aggregations

Catalog (org.voltdb.catalog.Catalog)44 File (java.io.File)21 Database (org.voltdb.catalog.Database)12 IOException (java.io.IOException)8 Table (org.voltdb.catalog.Table)7 InMemoryJarfile (org.voltdb.utils.InMemoryJarfile)6 CatalogContext (org.voltdb.CatalogContext)5 Procedure (org.voltdb.catalog.Procedure)5 DeploymentType (org.voltdb.compiler.deploymentfile.DeploymentType)5 DbSettings (org.voltdb.settings.DbSettings)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 TPCCProjectBuilder (org.voltdb.benchmark.tpcc.TPCCProjectBuilder)4 Cluster (org.voltdb.catalog.Cluster)4 VoltCompiler (org.voltdb.compiler.VoltCompiler)4 VoltProjectBuilder (org.voltdb.compiler.VoltProjectBuilder)4 FileInputStream (java.io.FileInputStream)3 HostMessenger (org.voltcore.messaging.HostMessenger)3 PlannerTool (org.voltdb.compiler.PlannerTool)3 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2