use of org.apache.geode.cache.client.internal.pooling.ConnectionDestroyedException in project geode by apache.
the class GatewaySenderEventRemoteDispatcher method readAcknowledgement.
protected GatewayAck readAcknowledgement() {
SenderProxy sp = new SenderProxy(this.processor.getSender().getProxy());
GatewayAck ack = null;
Exception ex;
try {
connection = getConnection(false);
if (logger.isDebugEnabled()) {
logger.debug(" Receiving ack on the thread {}", connection);
}
this.connectionLifeCycleLock.readLock().lock();
try {
if (connection != null && !processor.isStopped()) {
ack = (GatewayAck) sp.receiveAckFromReceiver(connection);
}
} finally {
this.connectionLifeCycleLock.readLock().unlock();
}
} catch (Exception e) {
Throwable t = e.getCause();
if (t instanceof BatchException70) {
// A BatchException has occurred.
// Do not process the connection as dead since it is not dead.
ex = (BatchException70) t;
} else if (e instanceof GatewaySenderException) {
// This Exception is thrown from
// getConnection
ex = (Exception) e.getCause();
} else {
ex = e;
// keep using the connection if we had a batch exception. Else, destroy
// it
destroyConnection();
}
if (this.sender.getProxy() == null || this.sender.getProxy().isDestroyed()) {
// if our pool is shutdown then just be silent
} else if (ex instanceof IOException || (ex instanceof ServerConnectivityException && !(ex.getCause() instanceof PdxRegistryMismatchException)) || ex instanceof ConnectionDestroyedException) {
// Sleep for a bit and recheck.
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
} else {
if (!(ex instanceof CancelException)) {
logger.fatal(LocalizedMessage.create(LocalizedStrings.GatewayEventRemoteDispatcher_STOPPING_THE_PROCESSOR_BECAUSE_THE_FOLLOWING_EXCEPTION_OCCURRED_WHILE_PROCESSING_A_BATCH), ex);
}
this.processor.setIsStopped(true);
}
}
return ack;
}
use of org.apache.geode.cache.client.internal.pooling.ConnectionDestroyedException in project geode by apache.
the class GatewaySenderEventRemoteDispatcher method _dispatchBatch.
private boolean _dispatchBatch(List events, boolean isRetry) {
Exception ex = null;
int currentBatchId = this.processor.getBatchId();
connection = getConnection(true);
int batchIdForThisConnection = this.processor.getBatchId();
GatewaySenderStats statistics = this.sender.getStatistics();
// i.e The connection has been reset. It also resets the batchId.
if (currentBatchId != batchIdForThisConnection || this.processor.isConnectionReset()) {
return false;
}
try {
if (this.processor.isConnectionReset()) {
isRetry = true;
}
SenderProxy sp = new SenderProxy(this.sender.getProxy());
this.connectionLifeCycleLock.readLock().lock();
try {
if (connection != null) {
sp.dispatchBatch_NewWAN(connection, events, currentBatchId, sender.isRemoveFromQueueOnException(), isRetry);
if (logger.isDebugEnabled()) {
logger.debug("{} : Dispatched batch (id={}) of {} events, queue size: {} on connection {}", this.processor.getSender(), currentBatchId, events.size(), this.processor.getQueue().size(), connection);
}
} else {
throw new ConnectionDestroyedException();
}
} finally {
this.connectionLifeCycleLock.readLock().unlock();
}
return true;
} catch (ServerOperationException e) {
Throwable t = e.getCause();
if (t instanceof BatchException70) {
// A BatchException has occurred.
// Do not process the connection as dead since it is not dead.
ex = (BatchException70) t;
} else {
ex = e;
// keep using the connection if we had a batch exception. Else, destroy it
destroyConnection();
}
throw new GatewaySenderException(LocalizedStrings.GatewayEventRemoteDispatcher_0_EXCEPTION_DURING_PROCESSING_BATCH_1_ON_CONNECTION_2.toLocalizedString(new Object[] { this, Integer.valueOf(currentBatchId), connection }), ex);
} catch (GemFireIOException e) {
Throwable t = e.getCause();
if (t instanceof MessageTooLargeException) {
// A MessageTooLargeException has occurred.
// Do not process the connection as dead since it is not dead.
ex = (MessageTooLargeException) t;
// Reduce the batch size by half of the configured batch size or number of events in the
// current batch (whichever is less)
int newBatchSize = Math.min(events.size(), this.processor.getBatchSize()) / 2;
logger.warn(LocalizedMessage.create(LocalizedStrings.GatewaySenderEventRemoteDispatcher_MESSAGE_TOO_LARGE_EXCEPTION, new Object[] { events.size(), newBatchSize }), e);
this.processor.setBatchSize(newBatchSize);
statistics.incBatchesResized();
} else {
ex = e;
// keep using the connection if we had a MessageTooLargeException. Else, destroy it
destroyConnection();
}
throw new GatewaySenderException(LocalizedStrings.GatewayEventRemoteDispatcher_0_EXCEPTION_DURING_PROCESSING_BATCH_1_ON_CONNECTION_2.toLocalizedString(new Object[] { this, Integer.valueOf(currentBatchId), connection }), ex);
} catch (IllegalStateException e) {
this.processor.setException(new GatewaySenderException(e));
throw new GatewaySenderException(LocalizedStrings.GatewayEventRemoteDispatcher_0_EXCEPTION_DURING_PROCESSING_BATCH_1_ON_CONNECTION_2.toLocalizedString(new Object[] { this, Integer.valueOf(currentBatchId), connection }), e);
} catch (Exception e) {
// An Exception has occurred. Get its cause.
Throwable t = e.getCause();
if (t instanceof IOException) {
// An IOException has occurred.
ex = (IOException) t;
} else {
ex = e;
}
// the cause is not going to be BatchException70. So, destroy the connection
destroyConnection();
throw new GatewaySenderException(LocalizedStrings.GatewayEventRemoteDispatcher_0_EXCEPTION_DURING_PROCESSING_BATCH_1_ON_CONNECTION_2.toLocalizedString(new Object[] { this, Integer.valueOf(currentBatchId), connection }), ex);
}
}
use of org.apache.geode.cache.client.internal.pooling.ConnectionDestroyedException in project geode by apache.
the class RemoteParallelGatewaySenderEventProcessor method shouldSendVersionEvents.
/**
* Returns if corresponding receiver WAN site of this GatewaySender has GemfireVersion > 7.0.1
*
* @param disp
* @return true if remote site Gemfire Version is >= 7.0.1
*/
private boolean shouldSendVersionEvents(GatewaySenderEventDispatcher disp) throws GatewaySenderException {
try {
GatewaySenderEventRemoteDispatcher remoteDispatcher = (GatewaySenderEventRemoteDispatcher) disp;
// This will create a new connection if no batch has been sent till
// now.
Connection conn = remoteDispatcher.getConnection(false);
if (conn != null) {
short remoteSiteVersion = conn.getWanSiteVersion();
if (Version.GFE_701.compareTo(remoteSiteVersion) <= 0) {
return true;
}
}
} catch (GatewaySenderException e) {
Throwable cause = e.getCause();
if (cause instanceof IOException || e instanceof GatewaySenderConfigurationException || cause instanceof ConnectionDestroyedException) {
try {
int sleepInterval = GatewaySender.CONNECTION_RETRY_INTERVAL;
if (logger.isDebugEnabled()) {
logger.debug("Sleeping for {} milliseconds", sleepInterval);
}
Thread.sleep(sleepInterval);
} catch (InterruptedException ie) {
// log the exception
if (logger.isDebugEnabled()) {
logger.debug(ie.getMessage(), ie);
}
}
}
throw e;
}
return false;
}
use of org.apache.geode.cache.client.internal.pooling.ConnectionDestroyedException in project geode by apache.
the class GatewaySenderEventRemoteDispatcher method initializeConnection.
/**
* Initializes the <code>Connection</code>.
*
* @throws GatewaySenderException
*/
private void initializeConnection() throws GatewaySenderException, GemFireSecurityException {
this.connectionLifeCycleLock.writeLock().lock();
try {
// Attempt to acquire a connection
if (this.sender.getProxy() == null || this.sender.getProxy().isDestroyed()) {
this.sender.initProxy();
} else {
this.processor.resetBatchId();
}
Connection con;
try {
if (this.sender.isParallel()) {
/*
* TODO - The use of acquireConnection should be removed from the gateway code. This
* method is fine for tests, but these connections should really be managed inside the
* pool code. If the gateway needs to persistent connection to a single server, which
* should create have the OpExecutor that holds a reference to the connection (similar to
* the way we do with thread local connections). Use {@link
* ExecutablePool#setupServerAffinity(boolean)} for gateway code
*/
con = this.sender.getProxy().acquireConnection();
// For parallel sender, setting server location will not matter.
// everytime it will ask for acquire connection whenever it needs it. I
// am saving this server location for command purpose
sender.setServerLocation(con.getServer());
} else {
synchronized (this.sender.getLockForConcurrentDispatcher()) {
ServerLocation server = this.sender.getServerLocation();
if (server != null) {
if (logger.isDebugEnabled()) {
logger.debug("ServerLocation is: {}. Connecting to this serverLocation...", server);
}
con = this.sender.getProxy().acquireConnection(server);
} else {
if (logger.isDebugEnabled()) {
logger.debug("ServerLocation is null. Creating new connection. ");
}
con = this.sender.getProxy().acquireConnection();
// PRIMARY
if (this.sender.isPrimary()) {
if (sender.getServerLocation() == null) {
sender.setServerLocation(con.getServer());
}
new UpdateAttributesProcessor(this.sender).distribute(false);
}
}
}
}
} catch (ServerConnectivityException e) {
this.failedConnectCount++;
Throwable ex = null;
if (e.getCause() instanceof GemFireSecurityException) {
ex = e.getCause();
if (logConnectionFailure()) {
// only log this message once; another msg is logged once we connect
logger.warn(LocalizedMessage.create(LocalizedStrings.GatewayEventRemoteDispatcher_0_COULD_NOT_CONNECT_1, new Object[] { this.processor.getSender().getId(), ex.getMessage() }));
}
throw new GatewaySenderException(ex);
}
List<ServerLocation> servers = this.sender.getProxy().getCurrentServers();
String ioMsg = null;
if (servers.size() == 0) {
ioMsg = LocalizedStrings.GatewayEventRemoteDispatcher_THERE_ARE_NO_ACTIVE_SERVERS.toLocalizedString();
} else {
final StringBuilder buffer = new StringBuilder();
for (ServerLocation server : servers) {
String endpointName = String.valueOf(server);
if (buffer.length() > 0) {
buffer.append(", ");
}
buffer.append(endpointName);
}
ioMsg = LocalizedStrings.GatewayEventRemoteDispatcher_NO_AVAILABLE_CONNECTION_WAS_FOUND_BUT_THE_FOLLOWING_ACTIVE_SERVERS_EXIST_0.toLocalizedString(buffer.toString());
}
ex = new IOException(ioMsg);
// Set the serverLocation to null so that a new connection can be
// obtained in next attempt
this.sender.setServerLocation(null);
if (this.failedConnectCount == 1) {
// only log this message once; another msg is logged once we connect
logger.warn(LocalizedMessage.create(LocalizedStrings.GatewayEventRemoteDispatcher__0___COULD_NOT_CONNECT, this.processor.getSender().getId()));
}
// same as the other exceptions that might occur in sendBatch.
throw new GatewaySenderException(LocalizedStrings.GatewayEventRemoteDispatcher__0___COULD_NOT_CONNECT.toLocalizedString(this.processor.getSender().getId()), ex);
}
if (this.failedConnectCount > 0) {
Object[] logArgs = new Object[] { this.processor.getSender().getId(), con, Integer.valueOf(this.failedConnectCount) };
logger.info(LocalizedMessage.create(LocalizedStrings.GatewayEventRemoteDispatcher_0_USING_1_AFTER_2_FAILED_CONNECT_ATTEMPTS, logArgs));
this.failedConnectCount = 0;
} else {
Object[] logArgs = new Object[] { this.processor.getSender().getId(), con };
logger.info(LocalizedMessage.create(LocalizedStrings.GatewayEventRemoteDispatcher_0_USING_1, logArgs));
}
this.connection = con;
this.processor.checkIfPdxNeedsResend(this.connection.getQueueStatus().getPdxSize());
} catch (ConnectionDestroyedException e) {
throw new GatewaySenderException(LocalizedStrings.GatewayEventRemoteDispatcher__0___COULD_NOT_CONNECT.toLocalizedString(this.processor.getSender().getId()), e);
} finally {
this.connectionLifeCycleLock.writeLock().unlock();
}
}
use of org.apache.geode.cache.client.internal.pooling.ConnectionDestroyedException in project geode by apache.
the class OpExecutorImpl method execute.
public Object execute(Op op, int retries) {
if (this.serverAffinity.get()) {
ServerLocation loc = this.affinityServerLocation.get();
if (loc == null) {
loc = getNextOpServerLocation();
this.affinityServerLocation.set(loc);
if (logger.isDebugEnabled()) {
logger.debug("setting server affinity to {}", this.affinityServerLocation.get());
}
}
return executeWithServerAffinity(loc, op);
}
boolean success = false;
Set attemptedServers = new HashSet();
Connection conn = (Connection) (threadLocalConnections ? localConnection.get() : null);
if (conn == null || conn.isDestroyed()) {
conn = connectionManager.borrowConnection(serverTimeout);
} else if (threadLocalConnections) {
// Fix for 43718. Clear the thread local connection
// while we're performing the op. It will be reset
// if the op succeeds.
localConnection.set(null);
try {
this.connectionManager.activate(conn);
} catch (ConnectionDestroyedException ex) {
conn = connectionManager.borrowConnection(serverTimeout);
}
}
try {
for (int attempt = 0; true; attempt++) {
// attempt's version stamp
if (attempt == 1 && (op instanceof AbstractOp)) {
AbstractOp absOp = (AbstractOp) op;
absOp.getMessage().setIsRetry();
}
try {
authenticateIfRequired(conn, op);
Object result = executeWithPossibleReAuthentication(conn, op);
success = true;
return result;
} catch (MessageTooLargeException e) {
throw new GemFireIOException("unable to transmit message to server", e);
} catch (Exception e) {
// This method will throw an exception if we need to stop
// It also unsets the threadlocal connection and notifies
// the connection manager if there are failures.
handleException(e, conn, attempt, attempt >= retries && retries != -1);
attemptedServers.add(conn.getServer());
try {
conn = connectionManager.exchangeConnection(conn, attemptedServers, serverTimeout);
} catch (NoAvailableServersException nse) {
// if retries is -1, don't try again after the last server has failed
if (retries == -1 || TRY_SERVERS_ONCE) {
handleException(e, conn, attempt, true);
} else {
// try one of the failed servers again, until we exceed the retry attempts.
attemptedServers.clear();
try {
conn = connectionManager.exchangeConnection(conn, attemptedServers, serverTimeout);
} catch (NoAvailableServersException nse2) {
handleException(e, conn, attempt, true);
}
}
}
}
}
} finally {
if (threadLocalConnections) {
this.connectionManager.passivate(conn, success);
// Fix for 43718. If the thread local was set to a different
// connection deeper in the call stack, return that connection
// and set our connection on the thread local.
Connection existingConnection = localConnection.get();
if (existingConnection != null && existingConnection != conn) {
connectionManager.returnConnection(existingConnection);
}
if (!conn.isDestroyed()) {
localConnection.set(conn);
} else {
localConnection.set(null);
}
} else {
connectionManager.returnConnection(conn);
}
}
}
Aggregations