Search in sources :

Example 1 with CatalogDiffEngine

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

the class RegressionSuite method tearDown.

/**
     * JUnit special method called to shutdown the test. This instance will
     * stop the VoltDB server using the VoltServerConfig instance provided.
     */
@Override
public void tearDown() throws Exception {
    if (m_completeShutdown) {
        m_config.shutDown();
    } else {
        Catalog currentCataog = getCurrentCatalog();
        if (currentCataog != null) {
            CatalogDiffEngine diff = new CatalogDiffEngine(m_config.getInitialCatalog(), currentCataog);
            // We will ignore this case.
            if (diff.commands().split("\n").length > 1) {
                fail("Catalog changed in test " + getName() + " while the regression suite optimization is on: \n" + diff.getDescriptionOfChanges(false));
            }
        }
        Client client = getClient();
        VoltTable tableList = client.callProcedure("@SystemCatalog", "TABLES").getResults()[0];
        ArrayList<String> tableNames = new ArrayList<>(tableList.getRowCount());
        int tableNameColIdx = tableList.getColumnIndex("TABLE_NAME");
        int tableTypeColIdx = tableList.getColumnIndex("TABLE_TYPE");
        while (tableList.advanceRow()) {
            String tableType = tableList.getString(tableTypeColIdx);
            if (!tableType.equalsIgnoreCase("EXPORT")) {
                tableNames.add(tableList.getString(tableNameColIdx));
            }
        }
        for (String tableName : tableNames) {
            try {
                client.callProcedure("@AdHoc", "DELETE FROM " + tableName);
            } catch (ProcCallException pce) {
                if (!pce.getMessage().contains("Illegal to modify a materialized view.")) {
                    fail("Hit an exception when cleaning up tables between tests: " + pce.getMessage());
                }
            }
        }
        client.drain();
    }
    for (final Client c : m_clients) {
        c.close();
    }
    synchronized (m_clientChannels) {
        for (final SocketChannel sc : m_clientChannels) {
            try {
                ConnectionUtil.closeConnection(sc);
            } catch (final IOException e) {
                e.printStackTrace();
            }
        }
        m_clientChannels.clear();
    }
    m_clients.clear();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) CatalogDiffEngine(org.voltdb.catalog.CatalogDiffEngine) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Client(org.voltdb.client.Client) VoltTable(org.voltdb.VoltTable) Catalog(org.voltdb.catalog.Catalog) ProcCallException(org.voltdb.client.ProcCallException)

Example 2 with CatalogDiffEngine

use of org.voltdb.catalog.CatalogDiffEngine 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)

Aggregations

IOException (java.io.IOException)2 Catalog (org.voltdb.catalog.Catalog)2 CatalogDiffEngine (org.voltdb.catalog.CatalogDiffEngine)2 SocketChannel (java.nio.channels.SocketChannel)1 ArrayList (java.util.ArrayList)1 CatalogContext (org.voltdb.CatalogContext)1 VoltTable (org.voltdb.VoltTable)1 Client (org.voltdb.client.Client)1 ProcCallException (org.voltdb.client.ProcCallException)1 CatalogChangeResult (org.voltdb.compiler.CatalogChangeResult)1 PrepareDiffFailureException (org.voltdb.compiler.CatalogChangeResult.PrepareDiffFailureException)1 VoltCompilerException (org.voltdb.compiler.VoltCompiler.VoltCompilerException)1 DeploymentType (org.voltdb.compiler.deploymentfile.DeploymentType)1 InMemoryJarfile (org.voltdb.utils.InMemoryJarfile)1