Search in sources :

Example 56 with CacheClosedException

use of org.apache.geode.cache.CacheClosedException in project geode by apache.

the class FileSystemJUnitTest method testPartialRename.

/**
   * Test what happens a file rename is aborted in the middle due to the a cache closed exception.
   * The next member that uses those files should be able to clean up after the partial rename.
   */
@Test
public void testPartialRename() throws Exception {
    final CountOperations countOperations = new CountOperations();
    // Create a couple of mock regions where we count the operations
    // that happen to them. We will then use this to abort the rename
    // in the middle.
    ConcurrentHashMap spyFileAndChunkRegion = mock(ConcurrentHashMap.class, new SpyWrapper(countOperations, fileAndChunkRegion));
    system = new FileSystem(spyFileAndChunkRegion, fileSystemStats);
    String name = "file";
    File file = system.createFile(name);
    ByteArrayOutputStream expected = new ByteArrayOutputStream();
    // Make sure the file has a lot of chunks
    for (int i = 0; i < 10; i++) {
        expected.write(writeRandomBytes(file));
    }
    String name2 = "file2";
    countOperations.reset();
    system.renameFile(name, name2);
    // Right now the number of operations is 4.. except if run through a debugger...
    assertTrue(4 <= countOperations.count);
    // This number of operations during a rename actually needs to get to the "putIfAbsent" for the
    // Assertion to be correct. Right now the number of operations is actually 3 so the limit needs
    // to be 3...
    countOperations.after((int) Math.ceil(countOperations.count / 2.0 + 1), new Runnable() {

        @Override
        public void run() {
            throw new CacheClosedException();
        }
    });
    String name3 = "file3";
    countOperations.reset();
    try {
        system.renameFile(name2, name3);
        fail("should have seen an error");
    } catch (CacheClosedException expectedException) {
    }
    system = new FileSystem(fileAndChunkRegion, fileSystemStats);
    // This is not the ideal behavior. We are left
    // with two duplicate files. However, we will still
    // verify that neither file is corrupted.
    assertEquals(2, system.listFileNames().size());
    File sourceFile = system.getFile(name2);
    File destFile = system.getFile(name3);
    byte[] expectedBytes = expected.toByteArray();
    assertContents(expectedBytes, sourceFile);
    assertContents(expectedBytes, destFile);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) CacheClosedException(org.apache.geode.cache.CacheClosedException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 57 with CacheClosedException

use of org.apache.geode.cache.CacheClosedException in project geode by apache.

the class InternalDistributedSystem method reconnect.

/**
   * A reconnect is tried when gemfire is configured to reconnect in case of a required role loss.
   * The reconnect will try reconnecting to the distributed system every max-time-out millseconds
   * for max-number-of-tries configured in gemfire.properties file. It uses the cache.xml file to
   * intialize the cache and create regions.
   */
private void reconnect(boolean forcedDisconnect, String reason) {
    // Collect all the state for cache
    // Collect all the state for Regions
    // Close the cache,
    // loop trying to connect, waiting before each attempt
    //
    // If reconnecting for lost-roles the reconnected system's cache will decide
    // whether the reconnected system should stay up. After max-tries we will
    // give up.
    //
    // If reconnecting for forced-disconnect we ignore max-tries and keep attempting
    // to join the distributed system until successful
    this.attemptingToReconnect = true;
    InternalDistributedSystem ids = InternalDistributedSystem.getAnyInstance();
    if (ids == null) {
        ids = this;
    }
    // first save the current cache description. This is created by
    // the membership manager when forced-disconnect starts. If we're
    // reconnecting for lost roles then this will be null
    String cacheXML = null;
    List<CacheServerCreation> cacheServerCreation = null;
    InternalCache cache = GemFireCacheImpl.getInstance();
    if (cache != null) {
        cacheXML = cache.getCacheConfig().getCacheXMLDescription();
        cacheServerCreation = cache.getCacheConfig().getCacheServerCreation();
    }
    DistributionConfig oldConfig = ids.getConfig();
    Properties configProps = getProperties();
    int timeOut = oldConfig.getMaxWaitTimeForReconnect();
    int maxTries = oldConfig.getMaxNumReconnectTries();
    final boolean isDebugEnabled = logger.isDebugEnabled();
    if (Thread.currentThread().getName().equals("DisconnectThread")) {
        if (isDebugEnabled) {
            logger.debug("changing thread name to ReconnectThread");
        }
        Thread.currentThread().setName("ReconnectThread");
    }
    // get the membership manager for quorum checks
    MembershipManager mbrMgr = this.dm.getMembershipManager();
    this.quorumChecker = mbrMgr.getQuorumChecker();
    if (logger.isDebugEnabled()) {
        if (quorumChecker == null) {
            logger.debug("No quorum checks will be performed during reconnect attempts");
        } else {
            logger.debug("Initialized quorum checking service: {}", quorumChecker);
        }
    }
    // LOG:CLEANUP: deal with reconnect and INHIBIT_DM_BANNER -- this should be ok now
    String appendToLogFile = System.getProperty(APPEND_TO_LOG_FILE);
    if (appendToLogFile == null) {
        System.setProperty(APPEND_TO_LOG_FILE, "true");
    }
    String inhibitBanner = System.getProperty(InternalLocator.INHIBIT_DM_BANNER);
    if (inhibitBanner == null) {
        System.setProperty(InternalLocator.INHIBIT_DM_BANNER, "true");
    }
    if (forcedDisconnect) {
        systemAttemptingReconnect = this;
    }
    try {
        while (this.reconnectDS == null || !this.reconnectDS.isConnected()) {
            if (isReconnectCancelled()) {
                break;
            }
            if (!forcedDisconnect) {
                if (isDebugEnabled) {
                    logger.debug("Max number of tries : {} and max time out : {}", maxTries, timeOut);
                }
                if (reconnectAttemptCounter >= maxTries) {
                    if (isDebugEnabled) {
                        logger.debug("Stopping the checkrequiredrole thread because reconnect : {} reached the max number of reconnect tries : {}", reconnectAttemptCounter, maxTries);
                    }
                    throw new CacheClosedException(LocalizedStrings.InternalDistributedSystem_SOME_REQUIRED_ROLES_MISSING.toLocalizedString());
                }
            }
            if (reconnectAttemptCounter == 0) {
                reconnectAttemptTime = System.currentTimeMillis();
            }
            reconnectAttemptCounter++;
            if (isReconnectCancelled()) {
                return;
            }
            logger.info("Disconnecting old DistributedSystem to prepare for a reconnect attempt");
            try {
                disconnect(true, reason, false);
            } catch (Exception ee) {
                logger.warn("Exception disconnecting for reconnect", ee);
            }
            try {
                reconnectLock.wait(timeOut);
            } catch (InterruptedException e) {
                logger.warn(LocalizedMessage.create(LocalizedStrings.InternalDistributedSystem_WAITING_THREAD_FOR_RECONNECT_GOT_INTERRUPTED));
                Thread.currentThread().interrupt();
                return;
            }
            if (isReconnectCancelled()) {
                return;
            }
            logger.info(LocalizedMessage.create(LocalizedStrings.DISTRIBUTED_SYSTEM_RECONNECTING, new Object[] { reconnectAttemptCounter }));
            int savNumOfTries = reconnectAttemptCounter;
            try {
                // notify listeners of each attempt and then again after successful
                notifyReconnectListeners(this, this.reconnectDS, true);
                if (this.locatorDMTypeForced) {
                    System.setProperty(InternalLocator.FORCE_LOCATOR_DM_TYPE, "true");
                }
                configProps.put(DistributionConfig.DS_RECONNECTING_NAME, Boolean.TRUE);
                if (quorumChecker != null) {
                    configProps.put(DistributionConfig.DS_QUORUM_CHECKER_NAME, quorumChecker);
                }
                InternalDistributedSystem newDS = null;
                if (isReconnectCancelled()) {
                    return;
                }
                try {
                    newDS = (InternalDistributedSystem) connect(configProps);
                } catch (CancelException e) {
                    if (isReconnectCancelled()) {
                        return;
                    } else {
                        throw e;
                    }
                } finally {
                    if (newDS == null && quorumChecker != null) {
                        // make sure the quorum checker is listening for messages from former members
                        quorumChecker.resume();
                    }
                }
                if (this.reconnectCancelled) {
                    newDS.disconnect();
                    continue;
                }
                this.reconnectDS = newDS;
            } catch (SystemConnectException e) {
                logger.debug("Attempt to reconnect failed with SystemConnectException");
                if (e.getMessage().contains("Rejecting the attempt of a member using an older version")) {
                    logger.warn(LocalizedMessage.create(LocalizedStrings.InternalDistributedSystem_EXCEPTION_OCCURRED_WHILE_TRYING_TO_CONNECT_THE_SYSTEM_DURING_RECONNECT), e);
                    attemptingToReconnect = false;
                    return;
                }
                continue;
            } catch (GemFireConfigException e) {
                if (isDebugEnabled) {
                    logger.debug("Attempt to reconnect failed with GemFireConfigException");
                }
                continue;
            } catch (Exception ee) {
                logger.warn(LocalizedMessage.create(LocalizedStrings.InternalDistributedSystem_EXCEPTION_OCCURRED_WHILE_TRYING_TO_CONNECT_THE_SYSTEM_DURING_RECONNECT), ee);
                attemptingToReconnect = false;
                return;
            } finally {
                if (this.locatorDMTypeForced) {
                    System.getProperties().remove(InternalLocator.FORCE_LOCATOR_DM_TYPE);
                }
                reconnectAttemptCounter = savNumOfTries;
            }
            DM newDM = this.reconnectDS.getDistributionManager();
            if (newDM instanceof DistributionManager) {
                // a cache
                if (((DistributionManager) newDM).getDMType() != DistributionManager.ADMIN_ONLY_DM_TYPE) {
                    try {
                        CacheConfig config = new CacheConfig();
                        if (cacheXML != null) {
                            config.setCacheXMLDescription(cacheXML);
                        }
                        cache = GemFireCacheImpl.create(this.reconnectDS, config);
                        createAndStartCacheServers(cacheServerCreation, cache);
                        if (cache.getCachePerfStats().getReliableRegionsMissing() == 0) {
                            reconnectAttemptCounter = 0;
                        } else {
                        // this try failed. The new cache will call reconnect again
                        }
                    } catch (CacheXmlException e) {
                        logger.warn("Exception occurred while trying to create the cache during reconnect", e);
                        reconnectDS.disconnect();
                        reconnectDS = null;
                        reconnectCancelled = true;
                        break;
                    } catch (CancelException ignor) {
                        logger.warn("Exception occurred while trying to create the cache during reconnect", ignor);
                        reconnectDS.disconnect();
                        reconnectDS = null;
                    } catch (Exception e) {
                        logger.warn(LocalizedMessage.create(LocalizedStrings.InternalDistributedSystem_EXCEPTION_OCCURRED_WHILE_TRYING_TO_CREATE_THE_CACHE_DURING_RECONNECT), e);
                    }
                }
            }
            if (reconnectDS != null && reconnectDS.isConnected()) {
                // make sure the new DS and cache are stable before exiting this loop
                try {
                    Thread.sleep(config.getMemberTimeout() * 3);
                } catch (InterruptedException e) {
                    logger.info("Reconnect thread has been interrupted - exiting");
                    Thread.currentThread().interrupt();
                    return;
                }
            }
        }
        if (isReconnectCancelled()) {
            reconnectDS.disconnect();
        } else {
            reconnectDS.isReconnectingDS = false;
            notifyReconnectListeners(this, this.reconnectDS, false);
        }
    } finally {
        systemAttemptingReconnect = null;
        attemptingToReconnect = false;
        if (appendToLogFile == null) {
            System.getProperties().remove(APPEND_TO_LOG_FILE);
        } else {
            System.setProperty(APPEND_TO_LOG_FILE, appendToLogFile);
        }
        if (inhibitBanner == null) {
            System.getProperties().remove(InternalLocator.INHIBIT_DM_BANNER);
        } else {
            System.setProperty(InternalLocator.INHIBIT_DM_BANNER, inhibitBanner);
        }
        if (quorumChecker != null) {
            mbrMgr.releaseQuorumChecker(quorumChecker);
        }
    }
    if (isReconnectCancelled()) {
        logger.debug("reconnect can no longer be done because of an explicit disconnect");
        if (reconnectDS != null) {
            reconnectDS.disconnect();
        }
        attemptingToReconnect = false;
        return;
    } else {
        logger.info("Reconnect completed.\nNew DistributedSystem is {}\nNew Cache is {}", reconnectDS, cache);
    }
}
Also used : InternalCache(org.apache.geode.internal.cache.InternalCache) CacheClosedException(org.apache.geode.cache.CacheClosedException) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) DistributedSystemDisconnectedException(org.apache.geode.distributed.DistributedSystemDisconnectedException) CacheXmlException(org.apache.geode.cache.CacheXmlException) CancelException(org.apache.geode.CancelException) GemFireSecurityException(org.apache.geode.security.GemFireSecurityException) ManagementException(org.apache.geode.management.ManagementException) ForcedDisconnectException(org.apache.geode.ForcedDisconnectException) GemFireIOException(org.apache.geode.GemFireIOException) CacheClosedException(org.apache.geode.cache.CacheClosedException) SystemConnectException(org.apache.geode.SystemConnectException) GemFireConfigException(org.apache.geode.GemFireConfigException) IOException(java.io.IOException) CacheXmlException(org.apache.geode.cache.CacheXmlException) CacheServerCreation(org.apache.geode.internal.cache.xmlcache.CacheServerCreation) GemFireConfigException(org.apache.geode.GemFireConfigException) MembershipManager(org.apache.geode.distributed.internal.membership.MembershipManager) GMSMembershipManager(org.apache.geode.distributed.internal.membership.gms.mgr.GMSMembershipManager) CancelException(org.apache.geode.CancelException) CacheConfig(org.apache.geode.internal.cache.CacheConfig) SystemConnectException(org.apache.geode.SystemConnectException)

Example 58 with CacheClosedException

use of org.apache.geode.cache.CacheClosedException in project geode by apache.

the class GetDeliveredOrders method execute.

public void execute(FunctionContext context) {
    Cache c = null;
    ArrayList<Object> vals = new ArrayList<Object>();
    try {
        c = CacheFactory.getAnyInstance();
    } catch (CacheClosedException ex) {
        vals.add("NoCacheFoundResult");
        context.getResultSender().lastResult(vals);
    }
    String oql = "SELECT o.purchaseOrderNo, o.deliveryDate  FROM /orders o WHERE o.deliveryDate != NULL";
    final Query query = c.getQueryService().newQuery(oql);
    SelectResults result = null;
    try {
        result = (SelectResults) query.execute();
        int resultSize = result.size();
        if (result instanceof Collection<?>)
            for (Object item : result) {
                vals.add(item);
            }
    } catch (FunctionDomainException e) {
        if (c != null)
            c.getLogger().info("Caught FunctionDomainException while executing function GetDeliveredOrders: " + e.getMessage());
    } catch (TypeMismatchException e) {
        if (c != null)
            c.getLogger().info("Caught TypeMismatchException while executing function GetDeliveredOrders: " + e.getMessage());
    } catch (NameResolutionException e) {
        if (c != null)
            c.getLogger().info("Caught NameResolutionException while executing function GetDeliveredOrders: " + e.getMessage());
    } catch (QueryInvocationTargetException e) {
        if (c != null)
            c.getLogger().info("Caught QueryInvocationTargetException while executing function GetDeliveredOrders: " + e.getMessage());
    }
    context.getResultSender().lastResult(vals);
}
Also used : Query(org.apache.geode.cache.query.Query) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) ArrayList(java.util.ArrayList) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) CacheClosedException(org.apache.geode.cache.CacheClosedException) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) SelectResults(org.apache.geode.cache.query.SelectResults) FunctionDomainException(org.apache.geode.cache.query.FunctionDomainException) Collection(java.util.Collection) Cache(org.apache.geode.cache.Cache)

Example 59 with CacheClosedException

use of org.apache.geode.cache.CacheClosedException in project geode by apache.

the class GetRegions method execute.

public void execute(FunctionContext context) {
    ArrayList<String> vals = new ArrayList<String>();
    Cache c = null;
    try {
        c = CacheFactory.getAnyInstance();
    } catch (CacheClosedException ex) {
        vals.add("NoCacheResult");
        context.getResultSender().lastResult(vals);
    }
    final Set<Region<?, ?>> regionSet = c.rootRegions();
    for (Region<?, ?> r : regionSet) {
        vals.add(r.getName());
    }
    context.getResultSender().lastResult(vals);
}
Also used : ArrayList(java.util.ArrayList) Region(org.apache.geode.cache.Region) CacheClosedException(org.apache.geode.cache.CacheClosedException) Cache(org.apache.geode.cache.Cache)

Example 60 with CacheClosedException

use of org.apache.geode.cache.CacheClosedException in project geode by apache.

the class GetValueForKey method execute.

@Override
public void execute(FunctionContext context) {
    Object args = context.getArguments();
    Cache cache = null;
    try {
        cache = CacheFactory.getAnyInstance();
        if (args.toString().equalsIgnoreCase("1")) {
            Region<String, Object> r = cache.getRegion("Products");
            Object result = r.get("1");
            context.getResultSender().lastResult(result);
        } else if (args.toString().equalsIgnoreCase("2")) {
            Region<String, Object> r = cache.getRegion("People");
            Object result = r.get("2");
            context.getResultSender().lastResult(result);
        } else {
            // Default case
            int i = 10;
            context.getResultSender().lastResult(i);
        }
    } catch (CacheClosedException e) {
        context.getResultSender().lastResult("Error: CacheClosedException");
    }
}
Also used : Region(org.apache.geode.cache.Region) CacheClosedException(org.apache.geode.cache.CacheClosedException) Cache(org.apache.geode.cache.Cache)

Aggregations

CacheClosedException (org.apache.geode.cache.CacheClosedException)95 Cache (org.apache.geode.cache.Cache)26 Test (org.junit.Test)21 IOException (java.io.IOException)20 ArrayList (java.util.ArrayList)20 FunctionException (org.apache.geode.cache.execute.FunctionException)20 FunctionInvocationTargetException (org.apache.geode.cache.execute.FunctionInvocationTargetException)20 CancelException (org.apache.geode.CancelException)18 Region (org.apache.geode.cache.Region)18 Host (org.apache.geode.test.dunit.Host)17 VM (org.apache.geode.test.dunit.VM)17 InternalCache (org.apache.geode.internal.cache.InternalCache)16 IgnoredException (org.apache.geode.test.dunit.IgnoredException)16 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)16 DistributedMember (org.apache.geode.distributed.DistributedMember)14 ReplyException (org.apache.geode.distributed.internal.ReplyException)14 RegionDestroyedException (org.apache.geode.cache.RegionDestroyedException)12 Execution (org.apache.geode.cache.execute.Execution)11 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)11 HashMap (java.util.HashMap)10