You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tv...@apache.org on 2021/11/28 19:10:46 UTC
[commons-jcs] branch master updated: JCS-191: Let Exceptions escalate to cause failover
This is an automated email from the ASF dual-hosted git repository.
tv pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jcs.git
The following commit(s) were added to refs/heads/master by this push:
new f4003ca JCS-191: Let Exceptions escalate to cause failover
f4003ca is described below
commit f4003ca483e91e6dfa98211b1ed861ddfbf98865
Author: Thomas Vandahl <tv...@apache.org>
AuthorDate: Sun Nov 28 20:10:42 2021 +0100
JCS-191: Let Exceptions escalate to cause failover
---
.../remote/AbstractRemoteAuxiliaryCache.java | 21 ++--
.../remote/AbstractRemoteCacheNoWaitFacade.java | 116 +++++----------------
.../jcs3/auxiliary/remote/RemoteCacheFactory.java | 8 +-
.../auxiliary/remote/RemoteCacheNoWaitFacade.java | 3 +-
4 files changed, 37 insertions(+), 111 deletions(-)
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/AbstractRemoteAuxiliaryCache.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/AbstractRemoteAuxiliaryCache.java
index 9e41ab5..453e0d4 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/AbstractRemoteAuxiliaryCache.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/AbstractRemoteAuxiliaryCache.java
@@ -174,13 +174,13 @@ public abstract class AbstractRemoteAuxiliaryCache<K, V>
// Never try to deserialize if you are a cluster client. Cluster
// clients are merely intra-remote cache communicators. Remote caches are assumed
// to have no ability to deserialize the objects.
- if ( (retVal instanceof ICacheElementSerialized) && (this.getRemoteCacheAttributes().getRemoteType() != RemoteType.CLUSTER) )
+ if (retVal instanceof ICacheElementSerialized && this.getRemoteCacheAttributes().getRemoteType() != RemoteType.CLUSTER)
{
retVal = SerializationConversionUtil.getDeSerializedCacheElement( (ICacheElementSerialized<K, V>) retVal,
super.getElementSerializer() );
}
}
- catch ( final Exception ex )
+ catch ( final IOException | ClassNotFoundException ex )
{
handleException( ex, "Failed to get [" + key + "] from [" + cacheName + "]", ICacheEventLogger.GET_EVENT );
}
@@ -279,7 +279,7 @@ public abstract class AbstractRemoteAuxiliaryCache<K, V>
}
}
}
- catch ( final Exception ex )
+ catch ( final IOException | ClassNotFoundException ex )
{
handleException( ex, "Failed to getMatching [" + pattern + "] from [" + cacheName + "]",
ICacheEventLogger.GET_EVENT );
@@ -306,7 +306,7 @@ public abstract class AbstractRemoteAuxiliaryCache<K, V>
{
getRemoteCacheService().remove( cacheName, key, getListenerId() );
}
- catch ( final Exception ex )
+ catch ( final IOException ex )
{
handleException( ex, "Failed to remove " + key + " from " + cacheName, ICacheEventLogger.REMOVE_EVENT );
}
@@ -331,7 +331,7 @@ public abstract class AbstractRemoteAuxiliaryCache<K, V>
{
getRemoteCacheService().removeAll( cacheName, getListenerId() );
}
- catch ( final Exception ex )
+ catch ( final IOException ex )
{
handleException( ex, "Failed to remove all from " + cacheName, ICacheEventLogger.REMOVEALL_EVENT );
}
@@ -352,22 +352,17 @@ public abstract class AbstractRemoteAuxiliaryCache<K, V>
{
if ( !getRemoteCacheAttributes().getGetOnly() )
{
- ICacheElementSerialized<K, V> serialized = null;
try
{
log.debug( "sending item to remote server" );
// convert so we don't have to know about the object on the
// other end.
- serialized = SerializationConversionUtil.getSerializedCacheElement( ce, super.getElementSerializer() );
+ ICacheElementSerialized<K, V> serialized = SerializationConversionUtil.getSerializedCacheElement( ce, super.getElementSerializer() );
remoteCacheService.update( serialized, getListenerId() );
}
- catch ( final NullPointerException npe )
- {
- log.error( "npe for ce = {0} ce.attr = {1}", ce, ce.getElementAttributes(), npe );
- }
- catch ( final Exception ex )
+ catch ( final IOException ex )
{
// event queue will wait and retry
handleException( ex, "Failed to put [" + ce.getKey() + "] to " + ce.getCacheName(),
@@ -421,7 +416,7 @@ public abstract class AbstractRemoteAuxiliaryCache<K, V>
log.debug( "set listenerId = {0}", id );
}
- catch ( final Exception e )
+ catch ( final IOException e )
{
log.error( "Problem setting listenerId", e );
}
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/AbstractRemoteCacheNoWaitFacade.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/AbstractRemoteCacheNoWaitFacade.java
index 736cb40..9a42f2f 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/AbstractRemoteCacheNoWaitFacade.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/AbstractRemoteCacheNoWaitFacade.java
@@ -25,8 +25,8 @@ import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
-import java.util.Objects;
import java.util.Set;
+import java.util.stream.Collectors;
import org.apache.commons.jcs3.auxiliary.AbstractAuxiliaryCache;
import org.apache.commons.jcs3.auxiliary.remote.behavior.IRemoteCacheAttributes;
@@ -91,33 +91,7 @@ public abstract class AbstractRemoteCacheNoWaitFacade<K, V>
for (final RemoteCacheNoWait<K, V> nw : noWaits)
{
- try
- {
- nw.update( ce );
- // an initial move into a zombie will lock this to primary
- // recovery. will not discover other servers until primary
- // reconnect
- // and subsequent error
- }
- catch ( final IOException ex )
- {
- final String message = "Problem updating no wait. Will initiate failover if the noWait is in error.";
- log.error( message, ex );
-
- if ( getCacheEventLogger() != null )
- {
- getCacheEventLogger().logError( "RemoteCacheNoWaitFacade",
- ICacheEventLogger.UPDATE_EVENT,
- message + ":" + ex.getMessage() + " REGION: " + ce.getCacheName()
- + " ELEMENT: " + ce );
- }
-
- // can handle failover here? Is it safe to try the others?
- // check to see it the noWait is now a zombie
- // if it is a zombie, then move to the next in the failover list
- // will need to keep them in order or a count
- failover( nw );
- }
+ nw.update( ce );
}
}
@@ -128,24 +102,14 @@ public abstract class AbstractRemoteCacheNoWaitFacade<K, V>
* @return Either an ICacheElement<K, V> or null if it is not found.
*/
@Override
- public ICacheElement<K, V> get( final K key )
+ public ICacheElement<K, V> get( final K key ) throws IOException
{
- return noWaits.stream()
- .map(nw -> {
- try
- {
- return nw.get( key );
- }
- catch ( final IOException ex )
- {
- log.debug( "Failed to get." );
- }
-
- return null;
- })
- .filter(Objects::nonNull)
- .findFirst()
- .orElse(null);
+ for (final RemoteCacheNoWait<K, V> nw : noWaits)
+ {
+ return nw.get(key);
+ }
+
+ return null;
}
/**
@@ -161,14 +125,7 @@ public abstract class AbstractRemoteCacheNoWaitFacade<K, V>
{
for (final RemoteCacheNoWait<K, V> nw : noWaits)
{
- try
- {
- return nw.getMatching( pattern );
- }
- catch ( final IOException ex )
- {
- log.debug( "Failed to getMatching." );
- }
+ return nw.getMatching( pattern );
}
return Collections.emptyMap();
@@ -182,20 +139,13 @@ public abstract class AbstractRemoteCacheNoWaitFacade<K, V>
* data in cache for any of these keys
*/
@Override
- public Map<K, ICacheElement<K, V>> getMultiple( final Set<K> keys )
+ public Map<K, ICacheElement<K, V>> getMultiple( final Set<K> keys ) throws IOException
{
if ( keys != null && !keys.isEmpty() )
{
for (final RemoteCacheNoWait<K, V> nw : noWaits)
{
- try
- {
- return nw.getMultiple( keys );
- }
- catch ( final IOException ex )
- {
- log.debug( "Failed to get." );
- }
+ return nw.getMultiple( keys );
}
}
@@ -230,18 +180,12 @@ public abstract class AbstractRemoteCacheNoWaitFacade<K, V>
* @return whether or not it was removed, right now it return false.
*/
@Override
- public boolean remove( final K key )
+ public boolean remove( final K key ) throws IOException
{
- noWaits.forEach(nw -> {
- try
- {
- nw.remove( key );
- }
- catch ( final IOException ex )
- {
- log.error( ex );
- }
- });
+ for (final RemoteCacheNoWait<K, V> nw : noWaits)
+ {
+ nw.remove( key );
+ }
return false;
}
@@ -250,18 +194,12 @@ public abstract class AbstractRemoteCacheNoWaitFacade<K, V>
* Adds a removeAll request to the remote cache.
*/
@Override
- public void removeAll()
+ public void removeAll() throws IOException
{
- noWaits.forEach(nw -> {
- try
- {
- nw.removeAll();
- }
- catch ( final IOException ex )
- {
- log.error( ex );
- }
- });
+ for (final RemoteCacheNoWait<K, V> nw : noWaits)
+ {
+ nw.removeAll();
+ }
}
/** Adds a dispose request to the remote cache. */
@@ -395,12 +333,10 @@ public abstract class AbstractRemoteCacheNoWaitFacade<K, V>
{
elems.add(new StatElement<>( "Number of No Waits", Integer.valueOf(noWaits.size()) ) );
- for ( final RemoteCacheNoWait<K, V> rcnw : noWaits )
- {
- // get the stats from the super too
- final IStats sStats = rcnw.getStatistics();
- elems.addAll(sStats.getStatElements());
- }
+ // get the stats from the super too
+ elems.addAll(noWaits.stream()
+ .flatMap(rcnw -> rcnw.getStatistics().getStatElements().stream())
+ .collect(Collectors.toList()));
}
stats.setStatElements( elems );
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/RemoteCacheFactory.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/RemoteCacheFactory.java
index 7d49b35..f68bac2 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/RemoteCacheFactory.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/RemoteCacheFactory.java
@@ -79,11 +79,8 @@ public class RemoteCacheFactory
// not necessary if a failover list is defined
// REGISTER PRIMARY LISTENER
// if it is a primary
- boolean primaryDefined = false;
if ( rca.getRemoteLocation() != null )
{
- primaryDefined = true;
-
failovers.add( rca.getRemoteLocation() );
final RemoteCacheManager rcm = getManager( rca, cacheMgr, cacheEventLogger, elementSerializer );
noWaits.add(rcm.getCache(rca));
@@ -94,10 +91,8 @@ public class RemoteCacheFactory
if ( failoverList != null )
{
final String[] failoverServers = failoverList.split("\\s*,\\s*");
- int fCnt = 0;
for (String server : failoverServers)
{
- fCnt++;
final RemoteLocation location = RemoteLocation.parseServerAndPort(server);
if (location != null)
@@ -109,8 +104,9 @@ public class RemoteCacheFactory
// add a listener if there are none, need to tell rca what
// number it is at
- if (!primaryDefined && fCnt == 1 || noWaits.isEmpty())
+ if (noWaits.isEmpty())
{
+ frca.setFailoverIndex(0);
noWaits.add(rcm.getCache(frca));
}
}
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/RemoteCacheNoWaitFacade.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/RemoteCacheNoWaitFacade.java
index c79459b..ca02fd1 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/RemoteCacheNoWaitFacade.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/RemoteCacheNoWaitFacade.java
@@ -140,8 +140,7 @@ public class RemoteCacheNoWaitFacade<K, V>
// Each RemoteCacheManager corresponds to one remote connection.
final List<RemoteLocation> failovers = rca0.getFailovers();
// we should probably check to see if there are any failovers,
- // even though the caller
- // should have already.
+ // even though the caller should have already.
if ( failovers == null )
{