
com.hazelcast.client.spi.impl.ClientInvocationServiceSupport Maven / Gradle / Ivy
/*
* Copyright (c) 2008-2016, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.client.spi.impl;
import com.hazelcast.client.HazelcastClientNotActiveException;
import com.hazelcast.client.connection.ClientConnectionManager;
import com.hazelcast.client.connection.nio.ClientConnection;
import com.hazelcast.client.impl.HazelcastClientInstanceImpl;
import com.hazelcast.client.impl.client.ClientRequest;
import com.hazelcast.client.impl.client.ClientResponse;
import com.hazelcast.client.impl.client.RemoveAllListeners;
import com.hazelcast.client.spi.ClientExecutionService;
import com.hazelcast.client.spi.ClientInvocationService;
import com.hazelcast.client.spi.ClientPartitionService;
import com.hazelcast.client.spi.EventHandler;
import com.hazelcast.client.spi.impl.listener.ClientListenerServiceImpl;
import com.hazelcast.cluster.client.ClientPingRequest;
import com.hazelcast.core.ICompletableFuture;
import com.hazelcast.internal.serialization.SerializationService;
import com.hazelcast.internal.serialization.impl.HeapData;
import com.hazelcast.logging.ILogger;
import com.hazelcast.logging.Logger;
import com.hazelcast.nio.Address;
import com.hazelcast.nio.Connection;
import com.hazelcast.nio.ConnectionListener;
import com.hazelcast.nio.Packet;
import com.hazelcast.nio.serialization.Data;
import com.hazelcast.spi.exception.TargetDisconnectedException;
import com.hazelcast.util.ConstructorFunction;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import static com.hazelcast.client.config.ClientProperty.MAX_CONCURRENT_INVOCATIONS;
import static com.hazelcast.instance.OutOfMemoryErrorDispatcher.onOutOfMemory;
abstract class ClientInvocationServiceSupport implements ClientInvocationService, ConnectionListener {
private static final int WAIT_TIME_FOR_PACKETS_TO_BE_CONSUMED = 10;
private static final int WAIT_TIME_FOR_PACKETS_TO_BE_CONSUMED_THRESHOLD = 5000;
protected final HazelcastClientInstanceImpl client;
protected ClientConnectionManager connectionManager;
protected ClientPartitionService partitionService;
protected ClientExecutionService executionService;
protected ClientListenerServiceImpl clientListenerService;
private ILogger logger = Logger.getLogger(ClientInvocationService.class);
private ResponseThread responseThread;
private final ConcurrentMap callIdMap
= new ConcurrentHashMap();
private final CallIdSequence callIdSequence;
private volatile boolean isShutdown;
public ClientInvocationServiceSupport(HazelcastClientInstanceImpl client) {
this.client = client;
int maxAllowedConcurrentInvocations = client.getClientProperties().getInteger(MAX_CONCURRENT_INVOCATIONS);
callIdSequence = new CallIdSequence.CallIdSequenceFailFast(maxAllowedConcurrentInvocations);
}
@Override
public void start() {
connectionManager = client.getConnectionManager();
executionService = client.getClientExecutionService();
clientListenerService = (ClientListenerServiceImpl) client.getListenerService();
connectionManager.addConnectionListener(this);
partitionService = client.getClientPartitionService();
responseThread = new ResponseThread(client.getThreadGroup(), client.getName() + ".response-",
client.getClientConfig().getClassLoader());
responseThread.start();
}
@Override
public ICompletableFuture invokeOnTarget(ClientRequest request, Address target) throws Exception {
return new ClientInvocation(client, request, target).invoke();
}
@Override
public boolean isRedoOperation() {
return client.getClientConfig().getNetworkConfig().isRedoOperation();
}
protected void send(ClientInvocation invocation, ClientConnection connection) throws IOException {
if (isShutdown) {
throw new HazelcastClientNotActiveException("Client is shut down");
}
registerInvocation(invocation);
final SerializationService ss = client.getSerializationService();
final byte[] bytes = ss.toBytes(invocation.getRequest());
Packet packet = new Packet(bytes, invocation.getPartitionId());
if (!isAllowedToSendRequest(connection, invocation.getRequest()) || !connection.write(packet)) {
long callId = invocation.getRequest().getCallId();
ClientInvocation clientInvocation = deRegisterCallId(callId);
if (clientInvocation != null) {
callIdSequence.complete();
throw new IOException("Packet not send to " + connection.getRemoteEndpoint());
} else {
if (logger.isFinestEnabled()) {
logger.finest("Invocation not found to deregister for call id " + callId);
}
}
}
invocation.setSendConnection(connection);
}
private boolean isAllowedToSendRequest(ClientConnection connection, ClientRequest request) {
if (!connection.isHeartBeating()) {
if (request instanceof ClientPingRequest || request instanceof RemoveAllListeners) {
//ping request and removeAllListeners should be send even though heart is not beating
return true;
}
if (logger.isFinestEnabled()) {
logger.warning("Connection is not heart-beating, won't write request -> " + request);
}
return false;
}
return true;
}
private void registerInvocation(ClientInvocation clientInvocation) {
long callId;
if (clientInvocation.isUrgent()) {
callId = callIdSequence.renew();
} else {
callId = callIdSequence.next();
}
clientInvocation.getRequest().setCallId(callId);
callIdMap.put(callId, clientInvocation);
EventHandler handler = clientInvocation.getEventHandler();
if (handler != null) {
clientListenerService.addEventHandler(callId, handler);
}
}
private ClientInvocation deRegisterCallId(long callId) {
return callIdMap.remove(callId);
}
public void cleanResources(ConstructorFunction
© 2015 - 2025 Weber Informatics LLC | Privacy Policy