private boolean restorePrimary()

in commons-jcs3-core/src/main/java/org/apache/commons/jcs3/auxiliary/remote/RemoteCacheNoWaitFacade.java [287:403]


    private boolean restorePrimary()
    {
        final IRemoteCacheAttributes rca0 = getAuxiliaryCacheAttributes();
        // try to move back to the primary
        final RemoteLocation server = rca0.getFailovers().get(0);

        log.info( "Trying to restore connection to primary remote server "
                + "[{0}]", server );

        final RemoteCacheAttributes rca = (RemoteCacheAttributes) rca0.clone();
        rca.setRemoteLocation(server);
        final RemoteCacheManager rcm = cacheFactory.getManager( rca );

        if (rcm != null)
        {
            // add a listener if there are none, need to tell rca what number it
            // is at
            final ICache<K, V> ic = rcm.getCache( rca );
            // by default the listener id should be 0, else it will be the
            // listener
            // Originally associated with the remote cache. either way is fine.
            // We just don't want the listener id from a failover being used.
            // If the remote server was rebooted this could be a problem if new
            // locals were also added.

            if ( ic.getStatus() == CacheStatus.ALIVE )
            {
                try
                {
                    // we could have more than one listener registered right
                    // now.
                    // this will not result in a loop, only duplication
                    // stop duplicate listening.
                    if (getPrimaryServer() != null && getPrimaryServer().getStatus() == CacheStatus.ALIVE )
                    {
                        final int fidx = rca0.getFailoverIndex();

                        if ( fidx > 0 )
                        {
                            final RemoteLocation serverOld = rca0.getFailovers().get(fidx);

                            log.debug( "Failover Index = {0} the server at that "
                                    + "index is [{1}]", fidx, serverOld );

                            if ( serverOld != null )
                            {
                                // create attributes that reflect the
                                // previous failed over configuration.
                                final RemoteCacheAttributes rcaOld = (RemoteCacheAttributes) rca0.clone();
                                rcaOld.setRemoteLocation(serverOld);
                                final RemoteCacheManager rcmOld = cacheFactory.getManager( rcaOld );

                                if ( rcmOld != null )
                                {
                                    // manager can remove by name if
                                    // necessary
                                    rcmOld.removeRemoteCacheListener( rcaOld );
                                }
                                log.info( "Successfully deregistered from "
                                        + "FAILOVER remote server = {0}", serverOld );
                            }
                        }
                        else if ( fidx == 0 )
                        {
                            // this should never happen. If there are no
                            // failovers this shouldn't get called.
                            if ( log.isDebugEnabled() )
                            {
                                log.debug( "No need to restore primary, it is already restored." );
                                return true;
                            }
                        }
                        else {
                            // this should never happen
                            log.warn( "Failover index is less than 0, this shouldn't happen" );
                        }
                    }
                }
                catch ( final IOException e )
                {
                    // TODO, should try again, or somehow stop the listener
                    log.error("Trouble trying to deregister old failover "
                            + "listener prior to restoring the primary = {0}",
                            server, e );
                }

                // Restore primary
                // may need to do this more gracefully, letting the failover finish in the background
                final RemoteCacheNoWait<K, V> failoverNoWait = getPrimaryServer();

                // swap in a new one
                restorePrimaryServer((RemoteCacheNoWait<K, V>) ic);
                rca0.setFailoverIndex( 0 );

                final String message = "Successfully reconnected to PRIMARY "
                        + "remote server. Substituted primary for "
                        + "failoverNoWait [" + failoverNoWait + "]";
                log.info( message );

                if (getCacheEventLogger() != null)
                {
                    getCacheEventLogger().logApplicationEvent(
                            "RemoteCacheFailoverRunner", "RestoredPrimary",
                            message );
                }
                return true;
            }
        }

        // else all right
        // if the failover index was at 0 here, we would be in a bad
        // situation, unless there were just
        // no failovers configured.
        log.debug( "Primary server status in error, not connected." );

        return false;
    }