use of org.apache.ignite.raft.client.ReadCommand in project ignite-3 by apache.
the class ItJraftCounterServerTest method testClientCatchExceptionFromSm.
/**
* Tests that users related exceptions from SM are propagated to the client.
*/
@Test
public void testClientCatchExceptionFromSm() throws Exception {
listenerFactory = () -> new CounterListener() {
@Override
public void onWrite(Iterator<CommandClosure<WriteCommand>> iterator) {
while (iterator.hasNext()) {
CommandClosure<WriteCommand> clo = iterator.next();
IncrementAndGetCommand cmd0 = (IncrementAndGetCommand) clo.command();
clo.result(new RuntimeException("Expected message"));
}
}
@Override
public void onRead(Iterator<CommandClosure<ReadCommand>> iterator) {
while (iterator.hasNext()) {
CommandClosure<ReadCommand> clo = iterator.next();
assert clo.command() instanceof GetValueCommand;
clo.result(new RuntimeException("Another expected message"));
}
}
};
startCluster();
RaftGroupService client1 = clients.get(0);
RaftGroupService client2 = clients.get(1);
client1.refreshLeader().get();
client2.refreshLeader().get();
NodeImpl leader = servers.stream().map(s -> ((NodeImpl) s.raftGroupService(COUNTER_GROUP_0).getRaftNode())).filter(n -> n.getState() == STATE_LEADER).findFirst().orElse(null);
assertNotNull(leader);
try {
client1.<Long>run(new IncrementAndGetCommand(3)).get();
fail();
} catch (Exception e) {
// Expected.
Throwable cause = e.getCause();
assertTrue(cause instanceof RuntimeException);
assertEquals(cause.getMessage(), "Expected message");
}
try {
client1.<Long>run(new GetValueCommand()).get();
fail();
} catch (Exception e) {
// Expected.
Throwable cause = e.getCause();
assertTrue(cause instanceof RuntimeException);
assertEquals(cause.getMessage(), "Another expected message");
}
}
use of org.apache.ignite.raft.client.ReadCommand in project ignite-3 by apache.
the class MetaStorageListener method onRead.
/**
* {@inheritDoc}
*/
@Override
public void onRead(Iterator<CommandClosure<ReadCommand>> iter) {
while (iter.hasNext()) {
CommandClosure<ReadCommand> clo = iter.next();
ReadCommand command = clo.command();
if (command instanceof GetCommand) {
GetCommand getCmd = (GetCommand) command;
Entry e;
if (getCmd.revision() != 0) {
e = storage.get(getCmd.key(), getCmd.revision());
} else {
e = storage.get(getCmd.key());
}
SingleEntryResponse resp = new SingleEntryResponse(e.key(), e.value(), e.revision(), e.updateCounter());
clo.result(resp);
} else if (command instanceof GetAllCommand) {
GetAllCommand getAllCmd = (GetAllCommand) command;
Collection<Entry> entries;
if (getAllCmd.revision() != 0) {
entries = storage.getAll(getAllCmd.keys(), getAllCmd.revision());
} else {
entries = storage.getAll(getAllCmd.keys());
}
List<SingleEntryResponse> res = new ArrayList<>(entries.size());
for (Entry e : entries) {
res.add(new SingleEntryResponse(e.key(), e.value(), e.revision(), e.updateCounter()));
}
clo.result(new MultipleEntryResponse(res));
} else if (command instanceof CursorHasNextCommand) {
CursorHasNextCommand cursorHasNextCmd = (CursorHasNextCommand) command;
CursorMeta cursorDesc = cursors.get(cursorHasNextCmd.cursorId());
clo.result(!(cursorDesc == null) && cursorDesc.cursor().hasNext());
} else {
assert false : "Command was not found [cmd=" + command + ']';
}
}
}
Aggregations