use of herddb.core.HerdDBInternalException in project herddb by diennea.
the class ServerSideConnectionPeer method commitTransaction.
public void commitTransaction(String tableSpace, long txId) throws HDBException {
try {
CommitTransactionStatement statement = new CommitTransactionStatement(tableSpace, txId);
TransactionContext transactionContext = new TransactionContext(txId);
server.getManager().executeStatement(statement, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), transactionContext);
} catch (HerdDBInternalException t) {
throw new HDBException(t);
}
}
use of herddb.core.HerdDBInternalException in project herddb by diennea.
the class ServerSideConnectionPeer method handleFetchScannerData.
private void handleFetchScannerData(Pdu message, Channel channel) {
long scannerId = PduCodec.FetchScannerData.readScannerId(message);
int fetchSize = PduCodec.FetchScannerData.readFetchSize(message);
if (fetchSize <= 0) {
fetchSize = 10;
}
ServerSideScannerPeer scanner = scanners.get(scannerId);
if (scanner != null) {
try {
DataScanner dataScanner = scanner.getScanner();
List<DataAccessor> records = dataScanner.consume(fetchSize);
String[] columns = dataScanner.getFieldNames();
TuplesList tuplesList = new TuplesList(columns, records);
boolean last = false;
if (dataScanner.isFinished()) {
LOGGER.log(Level.FINEST, "unregistering scanner {0}, resultset is finished", scannerId);
scanners.remove(scannerId);
last = true;
}
// LOGGER.log(Level.SEVERE, "sending " + converted.size() + " records to scanner " + scannerId);
try {
ByteBuf result = PduCodec.ResultSetChunk.write(message.messageId, tuplesList, last, dataScanner.getTransactionId());
channel.sendReplyMessage(message.messageId, result);
} catch (HerdDBInternalException err) {
// do not leak an unserializable scanner
scanners.remove(scannerId);
scanner.close();
throw err;
}
if (last) {
dataScanner.close();
}
} catch (DataScannerException | StatementExecutionException err) {
ByteBuf error = composeErrorResponse(message.messageId, err);
channel.sendReplyMessage(message.messageId, error);
}
} else {
ByteBuf error = PduCodec.ErrorResponse.write(message.messageId, "no such scanner " + scannerId);
channel.sendReplyMessage(message.messageId, error);
}
}
use of herddb.core.HerdDBInternalException in project herddb by diennea.
the class ServerSideConnectionPeer method handleExecuteStatements.
private void handleExecuteStatements(Pdu message, Channel channel) {
long transactionId = PduCodec.ExecuteStatements.readTx(message);
String tableSpace = PduCodec.ExecuteStatements.readTablespace(message);
long statementId = PduCodec.ExecuteStatements.readStatementId(message);
String query = statementId > 0 ? preparedStatements.resolveQuery(tableSpace, statementId) : PduCodec.ExecuteStatements.readQuery(message);
if (query == null) {
ByteBuf error = PduCodec.ErrorResponse.writeMissingPreparedStatementError(message.messageId, "bad statement id: " + statementId);
channel.sendReplyMessage(message.messageId, error);
message.close();
return;
}
boolean returnValues = PduCodec.ExecuteStatements.readReturnValues(message);
PduCodec.ListOfListsReader statementParameters = PduCodec.ExecuteStatements.startReadStatementsParameters(message);
int numStatements = statementParameters.getNumLists();
List<List<Object>> batch = new ArrayList<>(numStatements);
for (int i = 0; i < numStatements; i++) {
PduCodec.ObjectListReader parametersReader = statementParameters.nextList();
List<Object> batchParams = new ArrayList<>(parametersReader.getNumParams());
for (int j = 0; j < parametersReader.getNumParams(); j++) {
batchParams.add(parametersReader.nextObject());
}
batch.add(batchParams);
}
RunningStatementsStats runningStatements = server.getManager().getRunningStatements();
RunningStatementInfo statementInfo = new RunningStatementInfo(query, System.currentTimeMillis(), tableSpace, "", numStatements);
try {
List<TranslatedQuery> queries = new ArrayList<>();
for (int i = 0; i < numStatements; i++) {
List<Object> parameters = batch.get(i);
TranslatedQuery translatedQuery = server.getManager().getPlanner().translate(tableSpace, query, parameters, false, true, returnValues, -1);
queries.add(translatedQuery);
}
List<Long> updateCounts = new CopyOnWriteArrayList<>();
List<Map<String, Object>> otherDatas = new CopyOnWriteArrayList<>();
class ComputeNext implements BiConsumer<StatementExecutionResult, Throwable> {
int current;
public ComputeNext(int current) {
this.current = current;
}
@Override
public void accept(StatementExecutionResult result, Throwable error) {
if (error != null) {
ByteBuf errorMsg = composeErrorResponse(message.messageId, error);
channel.sendReplyMessage(message.messageId, errorMsg);
message.close();
runningStatements.unregisterRunningStatement(statementInfo);
return;
}
if (result instanceof DMLStatementExecutionResult) {
DMLStatementExecutionResult dml = (DMLStatementExecutionResult) result;
Map<String, Object> otherData = Collections.emptyMap();
if (returnValues && dml.getKey() != null) {
TranslatedQuery translatedQuery = queries.get(current - 1);
Statement statement = translatedQuery.plan.mainStatement;
TableAwareStatement tableStatement = (TableAwareStatement) statement;
Table table = server.getManager().getTableSpaceManager(statement.getTableSpace()).getTableManager(tableStatement.getTable()).getTable();
Object key = RecordSerializer.deserializePrimaryKey(dml.getKey(), table);
otherData = new HashMap<>();
otherData.put("_key", key);
if (dml.getNewvalue() != null) {
Map<String, Object> newvalue = RecordSerializer.toBean(new Record(dml.getKey(), dml.getNewvalue()), table);
otherData.putAll(newvalue);
}
}
updateCounts.add((long) dml.getUpdateCount());
otherDatas.add(otherData);
} else if (result instanceof DDLStatementExecutionResult) {
Map<String, Object> otherData = Collections.emptyMap();
updateCounts.add(1L);
otherDatas.add(otherData);
} else {
ByteBuf response = PduCodec.ErrorResponse.write(message.messageId, "bad result type " + result.getClass() + " (" + result + ")");
channel.sendReplyMessage(message.messageId, response);
message.close();
runningStatements.unregisterRunningStatement(statementInfo);
return;
}
long newTransactionId = result.transactionId;
if (current == queries.size()) {
try {
ByteBuf response = PduCodec.ExecuteStatementsResult.write(message.messageId, updateCounts, otherDatas, newTransactionId);
channel.sendReplyMessage(message.messageId, response);
message.close();
runningStatements.unregisterRunningStatement(statementInfo);
} catch (Throwable t) {
LOGGER.log(Level.SEVERE, "Internal error", t);
}
return;
}
TranslatedQuery nextPlannedQuery = queries.get(current);
TransactionContext transactionContext = new TransactionContext(newTransactionId);
CompletableFuture<StatementExecutionResult> nextPromise = server.getManager().executePlanAsync(nextPlannedQuery.plan, nextPlannedQuery.context, transactionContext);
nextPromise.whenComplete(new ComputeNext(current + 1));
}
}
TransactionContext transactionContext = new TransactionContext(transactionId);
TranslatedQuery firstTranslatedQuery = queries.get(0);
server.getManager().executePlanAsync(firstTranslatedQuery.plan, firstTranslatedQuery.context, transactionContext).whenComplete(new ComputeNext(1));
} catch (HerdDBInternalException err) {
ByteBuf response = composeErrorResponse(message.messageId, err);
channel.sendReplyMessage(message.messageId, response);
message.close();
runningStatements.unregisterRunningStatement(statementInfo);
}
}
use of herddb.core.HerdDBInternalException in project herddb by diennea.
the class ServerSideConnectionPeer method handleExecuteStatements.
private void handleExecuteStatements(Message message, Channel _channel) {
Long tx = (Long) message.parameters.get("tx");
long txId = tx != null ? tx : TransactionContext.NOTRANSACTION_ID;
long transactionId = txId;
String query = (String) message.parameters.get("query");
String tableSpace = (String) message.parameters.get("tableSpace");
Boolean returnValues = (Boolean) message.parameters.get("returnValues");
if (returnValues == null) {
returnValues = Boolean.FALSE;
}
List<List<Object>> batch = (List<List<Object>>) message.parameters.get("params");
try {
List<Long> updateCounts = new ArrayList<>(batch.size());
List<Map<String, Object>> otherDatas = new ArrayList<>(batch.size());
for (int i = 0; i < batch.size(); i++) {
List<Object> parameters = batch.get(i);
TransactionContext transactionContext = new TransactionContext(transactionId);
TranslatedQuery translatedQuery = server.getManager().getPlanner().translate(tableSpace, query, parameters, false, true, returnValues, -1);
Statement statement = translatedQuery.plan.mainStatement;
StatementExecutionResult result = server.getManager().executePlan(translatedQuery.plan, translatedQuery.context, transactionContext);
if (transactionId > 0 && result.transactionId > 0 && transactionId != result.transactionId) {
throw new StatementExecutionException("transactionid changed during batch execution, " + transactionId + "<>" + result.transactionId);
}
transactionId = result.transactionId;
if (result instanceof DMLStatementExecutionResult) {
DMLStatementExecutionResult dml = (DMLStatementExecutionResult) result;
Map<String, Object> otherData = Collections.emptyMap();
if (returnValues && dml.getKey() != null) {
TableAwareStatement tableStatement = (TableAwareStatement) statement;
Table table = server.getManager().getTableSpaceManager(statement.getTableSpace()).getTableManager(tableStatement.getTable()).getTable();
Object key = RecordSerializer.deserializePrimaryKey(dml.getKey().data, table);
otherData = new HashMap<>();
otherData.put("key", key);
if (dml.getNewvalue() != null) {
Map<String, Object> newvalue = RecordSerializer.toBean(new Record(dml.getKey(), dml.getNewvalue()), table);
otherData.put("newvalue", newvalue);
}
}
updateCounts.add(Long.valueOf(dml.getUpdateCount()));
otherDatas.add(otherData);
} else {
_channel.sendReplyMessage(message, Message.ERROR(null, new Exception("bad result type " + result.getClass() + " (" + result + ")")));
}
}
_channel.sendReplyMessage(message, Message.EXECUTE_STATEMENT_RESULTS(updateCounts, otherDatas, transactionId));
} catch (HerdDBInternalException err) {
Message error = Message.ERROR(null, err);
if (err instanceof NotLeaderException) {
error.setParameter("notLeader", "true");
}
_channel.sendReplyMessage(message, error);
}
}
use of herddb.core.HerdDBInternalException in project herddb by diennea.
the class JMXUtils method registerTableSpaceManagerStatsMXBean.
public static void registerTableSpaceManagerStatsMXBean(String tableSpaceName, TableSpaceManagerStatsMXBean bean) {
if (platformMBeanServer == null) {
throw new HerdDBInternalException("PlatformMBeanServer not available", mBeanServerLookupError);
}
String safeTableSpaceName = safeName(tableSpaceName);
try {
ObjectName name = new ObjectName("herddb.server:type=TableSpace,Name=" + safeTableSpaceName);
LOG.log(Level.FINE, "Publishing stats for table {0} at {1}", new Object[] { tableSpaceName, name });
if (platformMBeanServer.isRegistered(name)) {
try {
platformMBeanServer.unregisterMBean(name);
} catch (InstanceNotFoundException noProblem) {
}
}
platformMBeanServer.registerMBean(bean, name);
} catch (MalformedObjectNameException | InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e) {
throw new HerdDBInternalException("Could not register MXBean " + e);
}
}
Aggregations