use of herddb.network.Message in project herddb by diennea.
the class ServerSideConnectionPeer method handlePushTxLogChunk.
private void handlePushTxLogChunk(Message message, Channel _channel) {
try {
String tableSpace = (String) message.parameters.get("tableSpace");
List<KeyValue> data = (List<KeyValue>) message.parameters.get("data");
LOGGER.log(Level.INFO, "Received " + data.size() + " records for restore of txlog in tableSpace " + tableSpace);
List<DumpedLogEntry> entries = new ArrayList<>(data.size());
for (KeyValue kv : data) {
entries.add(new DumpedLogEntry(LogSequenceNumber.deserialize(kv.key), kv.value));
}
server.getManager().getTableSpaceManager(tableSpace).restoreRawDumpedEntryLogs(entries);
_channel.sendReplyMessage(message, Message.ACK(null));
} catch (Exception err) {
Message error = Message.ERROR(null, err);
if (err instanceof NotLeaderException) {
error.setParameter("notLeader", "true");
}
_channel.sendReplyMessage(message, error);
}
}
use of herddb.network.Message in project herddb by diennea.
the class ServerSideConnectionPeer method handleOpenScanner.
private void handleOpenScanner(Message message, Channel _channel) {
String tableSpace = (String) message.parameters.get("tableSpace");
Long tx = (Long) message.parameters.get("tx");
long txId = tx != null ? tx : 0;
String query = (String) message.parameters.get("query");
String scannerId = (String) message.parameters.get("scannerId");
int fetchSize = 10;
if (message.parameters.containsKey("fetchSize")) {
fetchSize = (Integer) message.parameters.get("fetchSize");
}
int maxRows = 0;
if (message.parameters.containsKey("maxRows")) {
maxRows = (Integer) message.parameters.get("maxRows");
}
List<Object> parameters = (List<Object>) message.parameters.get("params");
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "openScanner txId+" + txId + ", fetchSize " + fetchSize + ", maxRows " + maxRows + "," + query + " with " + parameters);
}
try {
TranslatedQuery translatedQuery = server.getManager().getPlanner().translate(tableSpace, query, parameters, true, true, false, maxRows);
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, query + " -> " + translatedQuery.plan.mainStatement);
}
TransactionContext transactionContext = new TransactionContext(txId);
if (translatedQuery.plan.mainStatement instanceof SQLPlannedOperationStatement || translatedQuery.plan.mainStatement instanceof ScanStatement || translatedQuery.plan.joinStatements != null) {
ScanResult scanResult = (ScanResult) server.getManager().executePlan(translatedQuery.plan, translatedQuery.context, transactionContext);
DataScanner dataScanner = scanResult.dataScanner;
ServerSideScannerPeer scanner = new ServerSideScannerPeer(dataScanner);
String[] columns = dataScanner.getFieldNames();
List<DataAccessor> records = dataScanner.consume(fetchSize);
TuplesList tuplesList = new TuplesList(columns, records);
boolean last = dataScanner.isFinished();
LOGGER.log(Level.FINEST, "sending first {0} records to scanner {1} query {2}", new Object[] { records.size(), scannerId, query });
if (!last) {
scanners.put(scannerId, scanner);
}
_channel.sendReplyMessage(message, Message.RESULTSET_CHUNK(null, scannerId, tuplesList, last, dataScanner.transactionId));
} else {
_channel.sendReplyMessage(message, Message.ERROR(null, new Exception("unsupported query type for scan " + query + ": PLAN is " + translatedQuery.plan)));
}
} catch (DataScannerException | RuntimeException err) {
LOGGER.log(Level.SEVERE, "error on scanner " + scannerId + ": " + err, err);
scanners.remove(scannerId);
Message error = Message.ERROR(null, err);
if (err instanceof NotLeaderException) {
error.setParameter("notLeader", "true");
}
_channel.sendReplyMessage(message, error);
}
}
use of herddb.network.Message in project herddb by diennea.
the class ServerSideConnectionPeer method handleRequestTableRestore.
private void handleRequestTableRestore(Message message, Channel _channel) {
try {
String tableSpace = (String) message.parameters.get("tableSpace");
byte[] table = (byte[]) message.parameters.get("table");
long dumpLedgerId = (long) message.parameters.get("dumpLedgerId");
long dumpOffset = (long) message.parameters.get("dumpOffset");
Table tableSchema = Table.deserialize(table);
tableSchema = Table.builder().cloning(tableSchema).tablespace(tableSpace).build();
server.getManager().getTableSpaceManager(tableSpace).beginRestoreTable(tableSchema.serialize(), new LogSequenceNumber(dumpLedgerId, dumpOffset));
_channel.sendReplyMessage(message, Message.ACK(null));
} catch (StatementExecutionException err) {
Message error = Message.ERROR(null, err);
if (err instanceof NotLeaderException) {
error.setParameter("notLeader", "true");
}
_channel.sendReplyMessage(message, error);
}
}
use of herddb.network.Message in project herddb by diennea.
the class MessageUtils method decodeMessage.
public static Message decodeMessage(ByteBuf encoded) {
byte version = encoded.readByte();
if (version != VERSION) {
throw new RuntimeException("bad protocol version " + version);
}
int type = ByteBufUtils.readVInt(encoded);
String messageId = readUTF8String(encoded);
String replyMessageId = null;
String workerProcessId = null;
Map<String, Object> params = new HashMap<>();
while (encoded.isReadable()) {
byte opcode = encoded.readByte();
switch(opcode) {
case OPCODE_REPLYMESSAGEID:
replyMessageId = readUTF8String(encoded);
break;
case OPCODE_WORKERPROCESSID:
workerProcessId = readUTF8String(encoded);
break;
case OPCODE_PARAMETERS:
int size = ByteBufUtils.readVInt(encoded);
for (int i = 0; i < size; i++) {
Object key = readEncodedSimpleValue(encoded);
Object value = readEncodedSimpleValue(encoded);
params.put((String) key, value);
}
break;
default:
throw new RuntimeException("invalid opcode: " + opcode);
}
}
Message m = new Message(workerProcessId, type, params);
if (replyMessageId != null) {
m.replyMessageId = replyMessageId;
}
m.messageId = messageId;
return m;
}
use of herddb.network.Message in project herddb by diennea.
the class NettyChannel method handleReply.
private void handleReply(Message anwermessage) {
final ReplyCallback callback = pendingReplyMessages.get(anwermessage.getReplyMessageId());
pendingReplyMessages.remove(anwermessage.getReplyMessageId());
pendingReplyMessagesDeadline.remove(anwermessage.getReplyMessageId());
Message original = pendingReplyMessagesSource.remove(anwermessage.getReplyMessageId());
if (callback != null && original != null) {
submitCallback(() -> {
callback.replyReceived(original, anwermessage, null);
});
}
}
Aggregations