use of com.hazelcast.internal.nio.Connection in project hazelcast by hazelcast.
the class ClientHeartbeatTest method testAddingListenerToNewConnectionFailedBecauseOfHeartbeat.
@Test
public void testAddingListenerToNewConnectionFailedBecauseOfHeartbeat() throws Exception {
hazelcastFactory.newHazelcastInstance();
final HazelcastInstance client = hazelcastFactory.newHazelcastClient(getClientConfig());
HazelcastClientInstanceImpl clientInstanceImpl = getHazelcastClientInstanceImpl(client);
final ClientListenerService clientListenerService = clientInstanceImpl.getListenerService();
final CountDownLatch blockIncoming = new CountDownLatch(1);
final CountDownLatch heartbeatStopped = new CountDownLatch(1);
final CountDownLatch onListenerRegister = new CountDownLatch(2);
clientInstanceImpl.getConnectionManager().addConnectionListener(new ConnectionListener() {
@Override
public void connectionAdded(Connection connection) {
}
@Override
public void connectionRemoved(Connection connection) {
heartbeatStopped.countDown();
}
});
clientListenerService.registerListener(createPartitionLostListenerCodec(), new EventHandler() {
AtomicInteger count = new AtomicInteger(0);
@Override
public void handle(Object event) {
}
@Override
public void beforeListenerRegister(Connection connection) {
if (count.incrementAndGet() == 2) {
try {
blockIncoming.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void onListenerRegister(Connection connection) {
onListenerRegister.countDown();
}
});
HazelcastInstance hazelcastInstance2 = hazelcastFactory.newHazelcastInstance();
assertSizeEventually(2, clientInstanceImpl.getConnectionManager().getActiveConnections());
blockMessagesFromInstance(hazelcastInstance2, client);
assertOpenEventually(heartbeatStopped);
blockIncoming.countDown();
unblockMessagesFromInstance(hazelcastInstance2, client);
assertOpenEventually(onListenerRegister);
}
use of com.hazelcast.internal.nio.Connection in project hazelcast by hazelcast.
the class TcpClientConnectionManagerTranslateTest method testTranslateIsNotUsedOnGettingExistingConnection.
@Test
public void testTranslateIsNotUsedOnGettingExistingConnection() {
// given
TestAddressProvider provider = new TestAddressProvider(false);
HazelcastInstance client = HazelcastClientUtil.newHazelcastClient(provider, new ClientConfig());
TcpClientConnectionManager clientConnectionManager = new TcpClientConnectionManager(getHazelcastClientInstanceImpl(client));
clientConnectionManager.start();
clientConnectionManager.reset();
clientConnectionManager.getOrConnectToAddress(privateAddress, false);
provider.shouldTranslate = true;
// when
Connection connection = clientConnectionManager.getOrConnectToAddress(privateAddress, false);
// then
assertNotNull(connection);
}
use of com.hazelcast.internal.nio.Connection in project hazelcast by hazelcast.
the class TcpClientConnectionManagerTranslateTest method testTranslateIsUsedWhenMemberHasPublicClientAddress.
@Test
public void testTranslateIsUsedWhenMemberHasPublicClientAddress() throws UnknownHostException {
// given
ClientConfig clientConfig = new ClientConfig();
clientConfig.setProperty(ClientProperty.DISCOVERY_SPI_PUBLIC_IP_ENABLED.getName(), "true");
HazelcastInstance client = HazelcastClientUtil.newHazelcastClient(null, clientConfig);
TcpClientConnectionManager clientConnectionManager = new TcpClientConnectionManager(getHazelcastClientInstanceImpl(client));
clientConnectionManager.start();
// private member address is unreachable
Member member = new MemberImpl(new Address("192.168.0.1", 5701), VERSION, false, UUID.randomUUID());
// public member address is reachable
member.getAddressMap().put(EndpointQualifier.resolve(ProtocolType.CLIENT, "public"), new Address("127.0.0.1", 5701));
// when
Connection connection = clientConnectionManager.getOrConnectToMember(member, false);
// then
assertNotNull(connection);
}
use of com.hazelcast.internal.nio.Connection in project hazelcast by hazelcast.
the class ClientListenerServiceImpl method deregisterListenerInternal.
private Boolean deregisterListenerInternal(@Nullable UUID userRegistrationId) {
// This method should only be called from registrationExecutor
assert (Thread.currentThread().getName().contains("eventRegistration"));
ClientListenerRegistration listenerRegistration = registrations.remove(userRegistrationId);
if (listenerRegistration == null) {
return false;
}
Map<Connection, ClientConnectionRegistration> registrations = listenerRegistration.getConnectionRegistrations();
CompletableFuture[] futures = new CompletableFuture[registrations.size()];
int i = 0;
for (Map.Entry<Connection, ClientConnectionRegistration> entry : registrations.entrySet()) {
ClientConnectionRegistration registration = entry.getValue();
ClientConnection subscriber = (ClientConnection) entry.getKey();
// remove local handler
subscriber.removeEventHandler(registration.getCallId());
// the rest is for deleting remote registration
ListenerMessageCodec listenerMessageCodec = listenerRegistration.getCodec();
UUID serverRegistrationId = registration.getServerRegistrationId();
ClientMessage request = listenerMessageCodec.encodeRemoveRequest(serverRegistrationId);
if (request == null) {
futures[i++] = CompletableFuture.completedFuture(null);
continue;
}
ClientInvocation clientInvocation = new ClientInvocation(client, request, null, subscriber);
clientInvocation.setInvocationTimeoutMillis(Long.MAX_VALUE);
futures[i++] = clientInvocation.invokeUrgent().exceptionally(throwable -> {
if (!(throwable instanceof HazelcastClientNotActiveException || throwable instanceof IOException || throwable instanceof TargetDisconnectedException)) {
logger.warning("Deregistration of listener with ID " + userRegistrationId + " has failed for address " + subscriber.getRemoteAddress(), throwable);
}
return null;
});
}
CompletableFuture.allOf(futures).join();
return true;
}
use of com.hazelcast.internal.nio.Connection in project hazelcast by hazelcast.
the class AbstractJoiner method sendSplitBrainJoinMessage.
/**
* Sends a split brain join request to the target address and returns the response.
*/
private SplitBrainJoinMessage sendSplitBrainJoinMessage(Address target, SplitBrainJoinMessage request) {
if (logger.isFineEnabled()) {
logger.fine("Sending SplitBrainJoinMessage to " + target);
}
Connection conn = node.getServer().getConnectionManager(MEMBER).getOrConnect(target, true);
long timeout = SPLIT_BRAIN_CONN_TIMEOUT_MILLIS;
while (conn == null) {
timeout -= SPLIT_BRAIN_SLEEP_TIME_MILLIS;
if (timeout < 0) {
logger.fine("Returning null timeout<0, " + timeout);
return null;
}
try {
// noinspection BusyWait
Thread.sleep(SPLIT_BRAIN_SLEEP_TIME_MILLIS);
} catch (InterruptedException e) {
currentThread().interrupt();
return null;
}
conn = node.getServer().getConnectionManager(MEMBER).get(target);
}
NodeEngine nodeEngine = node.nodeEngine;
Future future = nodeEngine.getOperationService().createInvocationBuilder(ClusterServiceImpl.SERVICE_NAME, new SplitBrainMergeValidationOp(request), target).setTryCount(1).invoke();
try {
return (SplitBrainJoinMessage) future.get(SPLIT_BRAIN_JOIN_CHECK_TIMEOUT_SECONDS, TimeUnit.SECONDS);
} catch (TimeoutException e) {
logger.fine("Timeout during join check!", e);
} catch (Exception e) {
logger.warning("Error during join check!", e);
}
return null;
}
Aggregations