use of org.voltdb.compiler.CatalogChangeResult.PrepareDiffFailureException in project voltdb by VoltDB.
the class Promote method run.
public CompletableFuture<ClientResponse> run() {
if (VoltDB.instance().getReplicationRole() == ReplicationRole.NONE) {
return makeQuickResponse(ClientResponse.GRACEFUL_FAILURE, "@Promote issued on non-replica cluster. No action taken.");
}
DrRoleType drRole = DrRoleType.fromValue(VoltDB.instance().getCatalogContext().getCluster().getDrrole());
boolean useDDLSchema = VoltDB.instance().getCatalogContext().cluster.getUseddlschema();
if (!allowPausedModeWork(false, isAdminConnection())) {
return makeQuickResponse(ClientResponse.SERVER_UNAVAILABLE, "Server is paused and is available in read-only mode - please try again later.");
}
CatalogChangeResult ccr = null;
try {
ccr = prepareApplicationCatalogDiff("@UpdateApplicationCatalog", null, null, new String[0], null, true, /* isPromotion */
drRole, useDDLSchema, false, getHostname(), getUsername());
} catch (PrepareDiffFailureException pe) {
hostLog.info("A request to update the database catalog and/or deployment settings has been rejected. More info returned to client.");
return makeQuickResponse(pe.statusCode, pe.getMessage());
}
// Log something useful about catalog upgrades when they occur.
if (ccr.upgradedFromVersion != null) {
hostLog.info(String.format("In order to update the application catalog it was " + "automatically upgraded from version %s.", ccr.upgradedFromVersion));
}
// case for @CatalogChangeResult
if (ccr.encodedDiffCommands.trim().length() == 0) {
return makeQuickResponse(ClientResponseImpl.SUCCESS, "Catalog update with no changes was skipped.");
}
// initiate the transaction.
return callProcedure("@UpdateCore", ccr.encodedDiffCommands, ccr.catalogHash, ccr.catalogBytes, ccr.expectedCatalogVersion, ccr.deploymentString, ccr.tablesThatMustBeEmpty, ccr.reasonsForEmptyTables, ccr.requiresSnapshotIsolation ? 1 : 0, ccr.worksWithElastic ? 1 : 0, ccr.deploymentHash, ccr.requireCatalogDiffCmdsApplyToEE ? 1 : 0, ccr.hasSchemaChange ? 1 : 0, ccr.requiresNewExportGeneration ? 1 : 0);
}
use of org.voltdb.compiler.CatalogChangeResult.PrepareDiffFailureException in project voltdb by VoltDB.
the class UpdateApplicationCatalog method run.
public CompletableFuture<ClientResponse> run(byte[] catalogJarBytes, String deploymentString) {
// catalogJarBytes if null, when passed along, will tell the
// catalog change planner that we want to use the current catalog.
DrRoleType drRole = DrRoleType.fromValue(VoltDB.instance().getCatalogContext().getCluster().getDrrole());
boolean useDDLSchema = VoltDB.instance().getCatalogContext().cluster.getUseddlschema() && !isRestoring();
// normalize empty string and null
if ((catalogJarBytes != null) && (catalogJarBytes.length == 0)) {
catalogJarBytes = null;
}
if (!allowPausedModeWork(isRestoring(), isAdminConnection())) {
return makeQuickResponse(ClientResponse.SERVER_UNAVAILABLE, "Server is paused and is available in read-only mode - please try again later.");
}
if (catalogJarBytes != null && useDDLSchema) {
return makeQuickResponse(ClientResponse.GRACEFUL_FAILURE, "Cluster is configured to use AdHoc DDL to change application schema. " + "Use of @UpdateApplicationCatalog is forbidden.");
}
CatalogChangeResult ccr = null;
try {
ccr = prepareApplicationCatalogDiff("@UpdateApplicationCatalog", catalogJarBytes, deploymentString, new String[0], null, false, /* isPromotion */
drRole, useDDLSchema, false, getHostname(), getUsername());
} catch (PrepareDiffFailureException pe) {
hostLog.info("A request to update the database catalog and/or deployment settings has been rejected. More info returned to client.");
return makeQuickResponse(pe.statusCode, pe.getMessage());
}
// Log something useful about catalog upgrades when they occur.
if (ccr.upgradedFromVersion != null) {
hostLog.info(String.format("In order to update the application catalog it was " + "automatically upgraded from version %s.", ccr.upgradedFromVersion));
}
// case for @CatalogChangeResult
if (ccr.encodedDiffCommands.trim().length() == 0) {
return makeQuickResponse(ClientResponseImpl.SUCCESS, "Catalog update with no changes was skipped.");
}
// This means no more @UAC calls when using DDL mode.
if (isRestoring()) {
noteRestoreCompleted();
}
// initiate the transaction.
return callProcedure("@UpdateCore", ccr.encodedDiffCommands, ccr.catalogHash, ccr.catalogBytes, ccr.expectedCatalogVersion, ccr.deploymentString, ccr.tablesThatMustBeEmpty, ccr.reasonsForEmptyTables, ccr.requiresSnapshotIsolation ? 1 : 0, ccr.worksWithElastic ? 1 : 0, ccr.deploymentHash, ccr.requireCatalogDiffCmdsApplyToEE ? 1 : 0, ccr.hasSchemaChange ? 1 : 0, ccr.requiresNewExportGeneration ? 1 : 0);
}
use of org.voltdb.compiler.CatalogChangeResult.PrepareDiffFailureException in project voltdb by VoltDB.
the class AdHoc method run.
public CompletableFuture<ClientResponse> run(ParameterSet params) {
final String invocationName = "@AdHoc";
if (params.size() == 0) {
return makeQuickResponse(ClientResponse.GRACEFUL_FAILURE, "Adhoc system procedure requires at least the query parameter.");
}
Object[] paramArray = params.toArray();
String sql = (String) paramArray[0];
Object[] userParams = null;
if (params.size() > 1) {
userParams = Arrays.copyOfRange(paramArray, 1, paramArray.length);
}
List<String> sqlStatements = new ArrayList<>();
AdHocSQLMix mix = processAdHocSQLStmtTypes(sql, sqlStatements);
if (mix == AdHocSQLMix.EMPTY) {
// response back to the client
return makeQuickResponse(ClientResponse.GRACEFUL_FAILURE, "Failed to plan, no SQL statement provided.");
} else if (mix == AdHocSQLMix.MIXED) {
// No mixing DDL and DML/DQL. Turn this into an error returned to client.
return makeQuickResponse(ClientResponse.GRACEFUL_FAILURE, "DDL mixed with DML and queries is unsupported.");
} else if (mix == AdHocSQLMix.ALL_DML_OR_DQL) {
// this is where we run non-DDL sql statements
return runNonDDLAdHoc(VoltDB.instance().getCatalogContext(), sqlStatements, true, null, ExplainMode.NONE, false, userParams);
}
// at this point assume all DDL
assert (mix == AdHocSQLMix.ALL_DDL);
// conflictTables tracks dropped tables before removing the ones that don't have CREATEs.
SortedSet<String> conflictTables = new TreeSet<String>();
Set<String> createdTables = new HashSet<String>();
for (String stmt : sqlStatements) {
// check that the DDL is allowed
String rejectionExplanation = SQLLexer.checkPermitted(stmt);
if (rejectionExplanation != null) {
return makeQuickResponse(ClientResponse.GRACEFUL_FAILURE, rejectionExplanation);
}
String ddlToken = SQLLexer.extractDDLToken(stmt);
// make sure not to mix drop and create in the same batch for the same table
if (ddlToken.equals("drop")) {
String tableName = SQLLexer.extractDDLTableName(stmt);
if (tableName != null) {
conflictTables.add(tableName);
}
} else if (ddlToken.equals("create")) {
String tableName = SQLLexer.extractDDLTableName(stmt);
if (tableName != null) {
createdTables.add(tableName);
}
}
}
// We have adhoc DDL. Is it okay to run it?
// check for conflicting DDL create/drop table statements.
// unhappy if the intersection is empty
conflictTables.retainAll(createdTables);
if (!conflictTables.isEmpty()) {
StringBuilder sb = new StringBuilder();
sb.append("AdHoc DDL contains both DROP and CREATE statements for the following table(s):");
for (String tableName : conflictTables) {
sb.append(" ");
sb.append(tableName);
}
sb.append("\nYou cannot DROP and ADD a table with the same name in a single batch " + "(via @AdHoc). Issue the DROP and ADD statements as separate commands.");
return makeQuickResponse(ClientResponse.GRACEFUL_FAILURE, sb.toString());
}
// Is it forbidden by the replication role and configured schema change method?
// master and UAC method chosen:
boolean useAdhocDDL = VoltDB.instance().getCatalogContext().cluster.getUseddlschema();
// TODO fix this hack
if (!useAdhocDDL) {
return makeQuickResponse(ClientResponse.GRACEFUL_FAILURE, "Cluster is configured to use @UpdateApplicationCatalog " + "to change application schema. AdHoc DDL is forbidden.");
}
// TODO figure out when internalmode matters
if (!allowPausedModeWork(false, isAdminConnection())) {
return makeQuickResponse(ClientResponse.SERVER_UNAVAILABLE, "Server is paused and is available in read-only mode - please try again later.");
}
CatalogChangeResult ccr = null;
try {
ccr = prepareApplicationCatalogDiff(invocationName, null, null, sqlStatements.toArray(new String[0]), null, false, DrRoleType.NONE, true, false, getHostname(), getUsername());
} catch (PrepareDiffFailureException pe) {
hostLog.info("A request to update the database catalog and/or deployment settings has been rejected. More info returned to client.");
return makeQuickResponse(pe.statusCode, pe.getMessage());
}
// case for @CatalogChangeResult
if (ccr.encodedDiffCommands.trim().length() == 0) {
return makeQuickResponse(ClientResponseImpl.SUCCESS, "Catalog update with no changes was skipped.");
}
// initiate the transaction.
return callProcedure("@UpdateCore", ccr.encodedDiffCommands, ccr.catalogHash, ccr.catalogBytes, ccr.expectedCatalogVersion, ccr.deploymentString, ccr.tablesThatMustBeEmpty, ccr.reasonsForEmptyTables, ccr.requiresSnapshotIsolation ? 1 : 0, ccr.worksWithElastic ? 1 : 0, ccr.deploymentHash, ccr.requireCatalogDiffCmdsApplyToEE ? 1 : 0, ccr.hasSchemaChange ? 1 : 0, ccr.requiresNewExportGeneration ? 1 : 0);
}
use of org.voltdb.compiler.CatalogChangeResult.PrepareDiffFailureException 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;
}
use of org.voltdb.compiler.CatalogChangeResult.PrepareDiffFailureException in project voltdb by VoltDB.
the class UpdateClasses method run.
public CompletableFuture<ClientResponse> run(byte[] jarfileBytes, String classesToDeleteSelector) {
DrRoleType drRole = DrRoleType.fromValue(VoltDB.instance().getCatalogContext().getCluster().getDrrole());
boolean useDDLSchema = VoltDB.instance().getCatalogContext().cluster.getUseddlschema();
if (!allowPausedModeWork(false, isAdminConnection())) {
return makeQuickResponse(ClientResponse.SERVER_UNAVAILABLE, "Server is paused and is available in read-only mode - please try again later.");
}
// master and adhoc DDL method chosen
if (!useDDLSchema) {
return makeQuickResponse(ClientResponse.GRACEFUL_FAILURE, "Cluster is configured to use @UpdateApplicationCatalog " + "to change application schema. Use of @UpdateClasses is forbidden.");
}
CatalogChangeResult ccr = null;
try {
ccr = prepareApplicationCatalogDiff("@UpdateClasses", jarfileBytes, classesToDeleteSelector, new String[0], null, false, /* isPromotion */
drRole, useDDLSchema, false, getHostname(), getUsername());
} catch (PrepareDiffFailureException pe) {
hostLog.info("A request to update the loaded classes has been rejected. More info returned to client.");
return makeQuickResponse(pe.statusCode, pe.getMessage());
}
// Log something useful about catalog upgrades when they occur.
if (ccr.upgradedFromVersion != null) {
hostLog.info(String.format("In order to update the application catalog it was " + "automatically upgraded from version %s.", ccr.upgradedFromVersion));
}
// case for @CatalogChangeResult
if (ccr.encodedDiffCommands.trim().length() == 0) {
return makeQuickResponse(ClientResponseImpl.SUCCESS, "Catalog update with no changes was skipped.");
}
// initiate the transaction.
return callProcedure("@UpdateCore", ccr.encodedDiffCommands, ccr.catalogHash, ccr.catalogBytes, ccr.expectedCatalogVersion, ccr.deploymentString, ccr.tablesThatMustBeEmpty, ccr.reasonsForEmptyTables, ccr.requiresSnapshotIsolation ? 1 : 0, ccr.worksWithElastic ? 1 : 0, ccr.deploymentHash, ccr.requireCatalogDiffCmdsApplyToEE ? 1 : 0, ccr.hasSchemaChange ? 1 : 0, ccr.requiresNewExportGeneration ? 1 : 0);
}
Aggregations