use of org.apache.ignite.lang.IgniteInternalException in project ignite-3 by apache.
the class IgniteTestUtils method setFieldValue.
/**
* Set object field value via reflection.
*
* @param obj Object to set field value to.
* @param cls Class to get field from.
* @param fieldName Field name to set value for.
* @param val New field value.
* @throws IgniteInternalException In case of error.
*/
public static void setFieldValue(Object obj, Class cls, String fieldName, Object val) throws IgniteInternalException {
assert fieldName != null;
try {
Field field = cls.getDeclaredField(fieldName);
if (!field.isAccessible()) {
field.setAccessible(true);
}
boolean isFinal = (field.getModifiers() & Modifier.FINAL) != 0;
boolean isStatic = (field.getModifiers() & Modifier.STATIC) != 0;
/*
* http://java.sun.com/docs/books/jls/third_edition/html/memory.html#17.5.3
* If a final field is initialized to a compile-time constant in the field declaration,
* changes to the final field may not be observed.
*/
if (isFinal && isStatic) {
throw new IgniteInternalException("Modification of static final field through reflection.");
}
if (isFinal) {
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
}
field.set(obj, val);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new IgniteInternalException("Failed to set object field [obj=" + obj + ", field=" + fieldName + ']', e);
}
}
use of org.apache.ignite.lang.IgniteInternalException in project ignite-3 by apache.
the class ItJraftCounterServerTest method testCreateSnapshotAbnormalFailure.
@Test
public void testCreateSnapshotAbnormalFailure() throws Exception {
listenerFactory = () -> new CounterListener() {
@Override
public void onSnapshotSave(Path path, Consumer<Throwable> doneClo) {
doneClo.accept(new IgniteInternalException("Very bad"));
}
};
startCluster();
RaftGroupService client1 = clients.get(0);
RaftGroupService client2 = clients.get(1);
client1.refreshLeader().get();
client2.refreshLeader().get();
long val = applyIncrements(client1, 1, 10);
assertEquals(sum(10), val);
Peer peer = servers.get(0).localPeer(COUNTER_GROUP_0);
try {
client1.snapshot(peer).get();
fail();
} catch (Exception e) {
assertTrue(e.getCause() instanceof RaftException);
}
}
use of org.apache.ignite.lang.IgniteInternalException in project ignite-3 by apache.
the class ItJraftCounterServerTest method testCreateSnapshotGracefulFailure.
@Test
public void testCreateSnapshotGracefulFailure() throws Exception {
listenerFactory = () -> new CounterListener() {
@Override
public void onSnapshotSave(Path path, Consumer<Throwable> doneClo) {
doneClo.accept(new IgniteInternalException("Very bad"));
}
};
startCluster();
RaftGroupService client1 = clients.get(0);
RaftGroupService client2 = clients.get(1);
client1.refreshLeader().get();
client2.refreshLeader().get();
RaftServer server = servers.get(0);
Peer peer = server.localPeer(COUNTER_GROUP_0);
long val = applyIncrements(client1, 1, 10);
assertEquals(sum(10), val);
try {
client1.snapshot(peer).get();
fail();
} catch (Exception e) {
assertTrue(e.getCause() instanceof RaftException);
}
}
use of org.apache.ignite.lang.IgniteInternalException in project ignite-3 by apache.
the class ItJraftCounterServerTest method testApplyWithFailure.
/**
* Tests if a raft group become unavailable in case of a critical error.
*/
@Test
public void testApplyWithFailure() throws Exception {
listenerFactory = () -> new CounterListener() {
@Override
public void onWrite(Iterator<CommandClosure<WriteCommand>> iterator) {
Iterator<CommandClosure<WriteCommand>> wrapper = new Iterator<>() {
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public CommandClosure<WriteCommand> next() {
CommandClosure<WriteCommand> cmd = iterator.next();
IncrementAndGetCommand command = (IncrementAndGetCommand) cmd.command();
if (command.delta() == 10) {
throw new IgniteInternalException("Very bad");
}
return cmd;
}
};
super.onWrite(wrapper);
}
};
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);
long val1 = applyIncrements(client1, 1, 5);
long val2 = applyIncrements(client2, 1, 7);
assertEquals(sum(5), val1);
assertEquals(sum(7), val2);
long val3 = applyIncrements(client1, 6, 9);
assertEquals(sum(9), val3);
try {
client1.<Long>run(new IncrementAndGetCommand(10)).get();
fail();
} catch (Exception e) {
// Expected.
Throwable cause = e.getCause();
assertTrue(cause instanceof RaftException);
}
NodeImpl finalLeader = leader;
waitForCondition(() -> finalLeader.getState() == STATE_ERROR, 5_000);
// Client can't switch to new leader, because only one peer in the list.
try {
client1.<Long>run(new IncrementAndGetCommand(11)).get();
} catch (Exception e) {
boolean isValid = e.getCause() instanceof TimeoutException;
if (!isValid) {
LOG.error("Got unexpected exception", e);
}
assertTrue(isValid, "Expecting the timeout");
}
}
use of org.apache.ignite.lang.IgniteInternalException in project ignite-3 by apache.
the class JraftServerImpl method startRaftGroup.
/**
* {@inheritDoc}
*/
@Override
public synchronized boolean startRaftGroup(String groupId, RaftGroupListener lsnr, @Nullable List<Peer> initialConf) {
if (groups.containsKey(groupId)) {
return false;
}
// Thread pools are shared by all raft groups.
NodeOptions nodeOptions = opts.copy();
Path serverDataPath = getServerDataPath(groupId);
try {
Files.createDirectories(serverDataPath);
} catch (IOException e) {
throw new IgniteInternalException(e);
}
nodeOptions.setLogUri(serverDataPath.resolve("logs").toString());
nodeOptions.setRaftMetaUri(serverDataPath.resolve("meta").toString());
nodeOptions.setSnapshotUri(serverDataPath.resolve("snapshot").toString());
nodeOptions.setFsm(new DelegatingStateMachine(lsnr));
if (initialConf != null) {
List<PeerId> mapped = initialConf.stream().map(PeerId::fromPeer).collect(Collectors.toList());
nodeOptions.setInitialConf(new Configuration(mapped, null));
}
IgniteRpcClient client = new IgniteRpcClient(service);
nodeOptions.setRpcClient(client);
NetworkAddress addr = service.topologyService().localMember().address();
var peerId = new PeerId(addr.host(), addr.port(), 0, ElectionPriority.DISABLED);
var server = new RaftGroupService(groupId, peerId, nodeOptions, rpcServer, nodeManager);
server.start();
groups.put(groupId, server);
return true;
}
Aggregations