use of org.apache.ignite.lang.IgniteInternalException in project ignite-3 by apache.
the class NettyClient method start.
/**
* Start client.
*
* @param bootstrapTemplate Template client bootstrap.
* @return Future that resolves when client channel is opened.
*/
public CompletableFuture<NettySender> start(Bootstrap bootstrapTemplate) {
synchronized (startStopLock) {
if (stopped) {
throw new IgniteInternalException("Attempted to start an already stopped NettyClient");
}
if (clientFuture != null) {
throw new IgniteInternalException("Attempted to start an already started NettyClient");
}
Bootstrap bootstrap = bootstrapTemplate.clone();
bootstrap.handler(new ChannelInitializer<SocketChannel>() {
/**
* {@inheritDoc}
*/
@Override
public void initChannel(SocketChannel ch) {
var sessionSerializationService = new PerSessionSerializationService(serializationService);
ch.pipeline().addLast(new InboundDecoder(sessionSerializationService), new HandshakeHandler(handshakeManager, (consistentId) -> new MessageHandler(messageListener, consistentId, sessionSerializationService)), new ChunkedWriteHandler(), new OutboundEncoder(sessionSerializationService), new IoExceptionSuppressingHandler());
}
});
clientFuture = NettyUtils.toChannelCompletableFuture(bootstrap.connect(address)).handle((channel, throwable) -> {
synchronized (startStopLock) {
this.channel = channel;
if (throwable != null) {
channelFuture.completeExceptionally(throwable);
} else {
channelFuture.complete(null);
}
if (stopped) {
return CompletableFuture.<NettySender>failedFuture(new CancellationException("Client was stopped"));
} else if (throwable != null) {
return CompletableFuture.<NettySender>failedFuture(throwable);
} else {
return handshakeManager.handshakeFuture();
}
}
}).thenCompose(Function.identity());
return clientFuture;
}
}
use of org.apache.ignite.lang.IgniteInternalException in project ignite-3 by apache.
the class SqlQueryProcessor method query0.
private List<SqlCursor<List<?>>> query0(QueryContext context, String schemaName, String sql, Object... params) {
SchemaPlus schema = schemaManager.schema(schemaName);
assert schema != null : "Schema not found: " + schemaName;
QueryPlan plan = planCache.queryPlan(new CacheKey(schema.getName(), sql));
if (plan != null) {
final QueryPlan finalPlan = plan;
context.maybeUnwrap(QueryValidator.class).ifPresent(queryValidator -> queryValidator.validatePlan(finalPlan));
RootQuery<Object[]> qry = new RootQuery<>(sql, schema, params, exchangeService, (q) -> queryRegistry.unregister(q.id()), LOG);
queryRegistry.register(qry);
try {
return Collections.singletonList(executionSrvc.executePlan(qry, plan));
} catch (Exception e) {
boolean isCanceled = qry.isCancelled();
qry.cancel();
queryRegistry.unregister(qry.id());
if (isCanceled) {
throw new IgniteInternalException("The query was cancelled while planning", e);
} else {
throw e;
}
}
}
SqlNodeList qryList = Commons.parse(sql, FRAMEWORK_CONFIG.getParserConfig());
List<SqlCursor<List<?>>> cursors = new ArrayList<>(qryList.size());
List<RootQuery<Object[]>> qrys = new ArrayList<>(qryList.size());
for (final SqlNode sqlNode : qryList) {
RootQuery<Object[]> qry = new RootQuery<>(sqlNode.toString(), // Update schema for each query in multiple statements.
schemaManager.schema(schemaName), params, exchangeService, (q) -> queryRegistry.unregister(q.id()), LOG);
qrys.add(qry);
queryRegistry.register(qry);
try {
if (qryList.size() == 1) {
plan = planCache.queryPlan(new CacheKey(schemaName, qry.sql()), () -> prepareSvc.prepareSingle(sqlNode, qry.planningContext()));
} else {
plan = prepareSvc.prepareSingle(sqlNode, qry.planningContext());
}
final QueryPlan finalPlan = plan;
context.maybeUnwrap(QueryValidator.class).ifPresent(queryValidator -> queryValidator.validatePlan(finalPlan));
cursors.add(executionSrvc.executePlan(qry, plan));
} catch (Exception e) {
boolean isCanceled = qry.isCancelled();
qrys.forEach(RootQuery::cancel);
queryRegistry.unregister(qry.id());
if (isCanceled) {
throw new IgniteInternalException("The query was cancelled while planning", e);
} else {
throw e;
}
}
}
return cursors;
}
use of org.apache.ignite.lang.IgniteInternalException in project ignite-3 by apache.
the class ExchangeServiceImpl method onMessage.
protected void onMessage(String nodeId, QueryBatchMessage msg) {
Inbox<?> inbox = mailboxRegistry.inbox(msg.queryId(), msg.exchangeId());
if (inbox == null && msg.batchId() == 0) {
// first message sent before a fragment is built
// note that an inbox source fragment id is also used as an exchange id
Inbox<?> newInbox = new Inbox<>(baseInboxContext(nodeId, msg.queryId(), msg.fragmentId()), this, mailboxRegistry, msg.exchangeId(), msg.exchangeId());
inbox = mailboxRegistry.register(newInbox);
}
if (inbox != null) {
try {
inbox.onBatchReceived(nodeId, msg.batchId(), msg.last(), Commons.cast(msg.rows()));
} catch (Throwable e) {
inbox.onError(e);
throw new IgniteInternalException("Unexpected exception", e);
}
} else if (LOG.isDebugEnabled()) {
LOG.debug("Stale batch message received: [" + "nodeId=" + nodeId + ", " + "queryId=" + msg.queryId() + ", " + "fragmentId=" + msg.fragmentId() + ", " + "exchangeId=" + msg.exchangeId() + ", " + "batchId=" + msg.batchId() + "]");
}
}
use of org.apache.ignite.lang.IgniteInternalException in project ignite-3 by apache.
the class ExecutionServiceImpl method executeFragment.
private void executeFragment(Query<RowT> qry, FragmentPlan plan, ExecutionContext<RowT> ectx) {
String origNodeId = ectx.originatingNodeId();
Outbox<RowT> node = new LogicalRelImplementor<>(ectx, affSrvc, mailboxRegistry, exchangeSrvc).go(plan.root());
qry.addFragment(new RunningFragment<>(node, ectx));
try {
msgSrvc.send(origNodeId, FACTORY.queryStartResponse().queryId(qry.id()).fragmentId(ectx.fragmentId()).build());
} catch (IgniteInternalCheckedException e) {
IgniteInternalException wrpEx = new IgniteInternalException("Failed to send reply. [nodeId=" + origNodeId + ']', e);
throw wrpEx;
}
node.init();
}
use of org.apache.ignite.lang.IgniteInternalException in project ignite-3 by apache.
the class MetaStorageListener method onWrite.
/**
* {@inheritDoc}
*/
@Override
public void onWrite(Iterator<CommandClosure<WriteCommand>> iter) {
while (iter.hasNext()) {
CommandClosure<WriteCommand> clo = iter.next();
WriteCommand command = clo.command();
if (command instanceof PutCommand) {
PutCommand putCmd = (PutCommand) command;
storage.put(putCmd.key(), putCmd.value());
clo.result(null);
} else if (command instanceof GetAndPutCommand) {
GetAndPutCommand getAndPutCmd = (GetAndPutCommand) command;
Entry e = storage.getAndPut(getAndPutCmd.key(), getAndPutCmd.value());
clo.result(new SingleEntryResponse(e.key(), e.value(), e.revision(), e.updateCounter()));
} else if (command instanceof PutAllCommand) {
PutAllCommand putAllCmd = (PutAllCommand) command;
storage.putAll(putAllCmd.keys(), putAllCmd.values());
clo.result(null);
} else if (command instanceof GetAndPutAllCommand) {
GetAndPutAllCommand getAndPutAllCmd = (GetAndPutAllCommand) command;
Collection<Entry> entries = storage.getAndPutAll(getAndPutAllCmd.keys(), getAndPutAllCmd.vals());
List<SingleEntryResponse> resp = new ArrayList<>(entries.size());
for (Entry e : entries) {
resp.add(new SingleEntryResponse(e.key(), e.value(), e.revision(), e.updateCounter()));
}
clo.result(new MultipleEntryResponse(resp));
} else if (command instanceof RemoveCommand) {
RemoveCommand rmvCmd = (RemoveCommand) command;
storage.remove(rmvCmd.key());
clo.result(null);
} else if (command instanceof GetAndRemoveCommand) {
GetAndRemoveCommand getAndRmvCmd = (GetAndRemoveCommand) command;
Entry e = storage.getAndRemove(getAndRmvCmd.key());
clo.result(new SingleEntryResponse(e.key(), e.value(), e.revision(), e.updateCounter()));
} else if (command instanceof RemoveAllCommand) {
RemoveAllCommand rmvAllCmd = (RemoveAllCommand) command;
storage.removeAll(rmvAllCmd.keys());
clo.result(null);
} else if (command instanceof GetAndRemoveAllCommand) {
GetAndRemoveAllCommand getAndRmvAllCmd = (GetAndRemoveAllCommand) command;
Collection<Entry> entries = storage.getAndRemoveAll(getAndRmvAllCmd.keys());
List<SingleEntryResponse> resp = new ArrayList<>(entries.size());
for (Entry e : entries) {
resp.add(new SingleEntryResponse(e.key(), e.value(), e.revision(), e.updateCounter()));
}
clo.result(new MultipleEntryResponse(resp));
} else if (command instanceof InvokeCommand) {
InvokeCommand cmd = (InvokeCommand) command;
boolean res = storage.invoke(toCondition(cmd.condition()), toOperations(cmd.success()), toOperations(cmd.failure()));
clo.result(res);
} else if (command instanceof MultiInvokeCommand) {
MultiInvokeCommand cmd = (MultiInvokeCommand) command;
StatementResult res = storage.invoke(toIf(cmd.iif()));
clo.result(new StatementResultInfo(res.bytes()));
} else if (command instanceof RangeCommand) {
RangeCommand rangeCmd = (RangeCommand) command;
IgniteUuid cursorId = rangeCmd.getCursorId();
Cursor<Entry> cursor = (rangeCmd.revUpperBound() != -1) ? storage.range(rangeCmd.keyFrom(), rangeCmd.keyTo(), rangeCmd.revUpperBound()) : storage.range(rangeCmd.keyFrom(), rangeCmd.keyTo());
cursors.put(cursorId, new CursorMeta(cursor, CursorType.RANGE, rangeCmd.requesterNodeId()));
clo.result(cursorId);
} else if (command instanceof CursorNextCommand) {
CursorNextCommand cursorNextCmd = (CursorNextCommand) command;
CursorMeta cursorDesc = cursors.get(cursorNextCmd.cursorId());
if (cursorDesc == null) {
clo.result(new NoSuchElementException("Corresponding cursor on the server side is not found."));
return;
}
try {
if (cursorDesc.type() == CursorType.RANGE) {
Entry e = (Entry) cursorDesc.cursor().next();
clo.result(new SingleEntryResponse(e.key(), e.value(), e.revision(), e.updateCounter()));
} else if (cursorDesc.type() == CursorType.WATCH) {
WatchEvent evt = (WatchEvent) cursorDesc.cursor().next();
List<SingleEntryResponse> resp = new ArrayList<>(evt.entryEvents().size() * 2);
for (EntryEvent e : evt.entryEvents()) {
Entry o = e.oldEntry();
Entry n = e.entry();
resp.add(new SingleEntryResponse(o.key(), o.value(), o.revision(), o.updateCounter()));
resp.add(new SingleEntryResponse(n.key(), n.value(), n.revision(), n.updateCounter()));
}
clo.result(new MultipleEntryResponse(resp));
}
} catch (NoSuchElementException e) {
clo.result(e);
}
} else if (command instanceof CursorCloseCommand) {
CursorCloseCommand cursorCloseCmd = (CursorCloseCommand) command;
CursorMeta cursorDesc = cursors.remove(cursorCloseCmd.cursorId());
if (cursorDesc == null) {
clo.result(null);
return;
}
try {
cursorDesc.cursor().close();
} catch (Exception e) {
throw new IgniteInternalException(e);
}
clo.result(null);
} else if (command instanceof WatchRangeKeysCommand) {
WatchRangeKeysCommand watchCmd = (WatchRangeKeysCommand) command;
IgniteUuid cursorId = watchCmd.getCursorId();
Cursor<WatchEvent> cursor = storage.watch(watchCmd.keyFrom(), watchCmd.keyTo(), watchCmd.revision());
cursors.put(cursorId, new CursorMeta(cursor, CursorType.WATCH, watchCmd.requesterNodeId()));
clo.result(cursorId);
} else if (command instanceof WatchExactKeysCommand) {
WatchExactKeysCommand watchCmd = (WatchExactKeysCommand) command;
IgniteUuid cursorId = watchCmd.getCursorId();
Cursor<WatchEvent> cursor = storage.watch(watchCmd.keys(), watchCmd.revision());
cursors.put(cursorId, new CursorMeta(cursor, CursorType.WATCH, watchCmd.requesterNodeId()));
clo.result(cursorId);
} else if (command instanceof CursorsCloseCommand) {
CursorsCloseCommand cursorsCloseCmd = (CursorsCloseCommand) command;
Iterator<CursorMeta> cursorsIter = cursors.values().iterator();
while (cursorsIter.hasNext()) {
CursorMeta cursorDesc = cursorsIter.next();
if (cursorDesc.requesterNodeId().equals(cursorsCloseCmd.nodeId())) {
try {
cursorDesc.cursor().close();
} catch (Exception e) {
throw new IgniteInternalException(e);
}
cursorsIter.remove();
}
}
clo.result(null);
} else {
assert false : "Command was not found [cmd=" + command + ']';
}
}
}
Aggregations