Search in sources :

Example 1 with CacheConfig

use of org.apache.geode.internal.cache.CacheConfig in project geode by apache.

the class ServerLauncherTest method shouldBeMockable.

@Test
public void shouldBeMockable() throws Exception {
    ServerLauncher mockServerLauncher = mock(ServerLauncher.class);
    Cache mockCache = mock(Cache.class);
    CacheConfig mockCacheConfig = mock(CacheConfig.class);
    when(mockServerLauncher.getCache()).thenReturn(mockCache);
    when(mockServerLauncher.getCacheConfig()).thenReturn(mockCacheConfig);
    when(mockServerLauncher.getId()).thenReturn("ID");
    when(mockServerLauncher.isWaiting(eq(mockCache))).thenReturn(true);
    when(mockServerLauncher.isHelping()).thenReturn(true);
    mockServerLauncher.startCacheServer(mockCache);
    verify(mockServerLauncher, times(1)).startCacheServer(mockCache);
    assertThat(mockServerLauncher.getCache()).isSameAs(mockCache);
    assertThat(mockServerLauncher.getCacheConfig()).isSameAs(mockCacheConfig);
    assertThat(mockServerLauncher.getId()).isSameAs("ID");
    assertThat(mockServerLauncher.isWaiting(mockCache)).isTrue();
    assertThat(mockServerLauncher.isHelping()).isTrue();
}
Also used : CacheConfig(org.apache.geode.internal.cache.CacheConfig) Cache(org.apache.geode.cache.Cache) UnitTest(org.apache.geode.test.junit.categories.UnitTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 2 with CacheConfig

use of org.apache.geode.internal.cache.CacheConfig in project geode by apache.

the class ServerLauncher method getCacheConfig.

/**
   * Gets the CacheConfig object used to configure additional GemFire Cache components and features
   * (e.g. PDX).
   *
   * @return a CacheConfig object with additional GemFire Cache configuration meta-data used on
   *         startup to configure the Cache.
   */
public CacheConfig getCacheConfig() {
    final CacheConfig copy = new CacheConfig();
    copy.setDeclarativeConfig(this.cacheConfig);
    return copy;
}
Also used : CacheConfig(org.apache.geode.internal.cache.CacheConfig)

Example 3 with CacheConfig

use of org.apache.geode.internal.cache.CacheConfig in project geode by apache.

the class CacheXmlGenerator method generatePdx.

private void generatePdx() throws SAXException {
    AttributesImpl atts = new AttributesImpl();
    CacheConfig config = ((InternalCache) cache).getCacheConfig();
    if (config.pdxReadSerializedUserSet) {
        if (generateDefaults() || this.cache.getPdxReadSerialized())
            atts.addAttribute("", "", READ_SERIALIZED, "", Boolean.toString(this.cache.getPdxReadSerialized()));
    }
    if (config.pdxIgnoreUnreadFieldsUserSet) {
        if (generateDefaults() || this.cache.getPdxIgnoreUnreadFields())
            atts.addAttribute("", "", IGNORE_UNREAD_FIELDS, "", Boolean.toString(this.cache.getPdxIgnoreUnreadFields()));
    }
    if (config.pdxPersistentUserSet) {
        if (generateDefaults() || this.cache.getPdxPersistent())
            atts.addAttribute("", "", PERSISTENT, "", Boolean.toString(this.cache.getPdxPersistent()));
    }
    if (config.pdxDiskStoreUserSet) {
        if (generateDefaults() || this.cache.getPdxDiskStore() != null && !this.cache.getPdxDiskStore().equals(""))
            atts.addAttribute("", "", DISK_STORE_NAME, "", this.cache.getPdxDiskStore());
    }
    if (!generateDefaults() && this.cache.getPdxSerializer() == null && atts.getLength() == 0) {
        return;
    }
    handler.startElement("", PDX, PDX, atts);
    if (this.cache.getPdxSerializer() != null) {
        generate(PDX_SERIALIZER, this.cache.getPdxSerializer());
    }
    handler.endElement("", PDX, PDX);
}
Also used : DiskWriteAttributesImpl(org.apache.geode.internal.cache.DiskWriteAttributesImpl) PartitionAttributesImpl(org.apache.geode.internal.cache.PartitionAttributesImpl) AttributesImpl(org.xml.sax.helpers.AttributesImpl) InternalCache(org.apache.geode.internal.cache.InternalCache) CacheConfig(org.apache.geode.internal.cache.CacheConfig)

Example 4 with CacheConfig

use of org.apache.geode.internal.cache.CacheConfig in project geode by apache.

the class WANTestBase method createCache_PDX.

public static void createCache_PDX(Integer locPort) {
    WANTestBase test = new WANTestBase();
    Properties props = test.getDistributedSystemProperties();
    props.setProperty(MCAST_PORT, "0");
    props.setProperty(LOCATORS, "localhost[" + locPort + "]");
    InternalDistributedSystem ds = test.getSystem(props);
    CacheConfig cacheConfig = new CacheConfig();
    cacheConfig.setPdxPersistent(true);
    cacheConfig.setPdxDiskStore("PDX_TEST");
    cache = GemFireCacheImpl.create(ds, false, cacheConfig);
    File pdxDir = new File(CacheTestCase.getDiskDir(), "pdx");
    DiskStoreFactory dsf = cache.createDiskStoreFactory();
    File[] dirs1 = new File[] { pdxDir };
    dsf.setDiskDirs(dirs1).setMaxOplogSize(1).create("PDX_TEST");
}
Also used : InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) Properties(java.util.Properties) CacheConfig(org.apache.geode.internal.cache.CacheConfig) File(java.io.File) DiskStoreFactory(org.apache.geode.cache.DiskStoreFactory)

Example 5 with CacheConfig

use of org.apache.geode.internal.cache.CacheConfig 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)

Aggregations

CacheConfig (org.apache.geode.internal.cache.CacheConfig)8 Properties (java.util.Properties)3 InternalCache (org.apache.geode.internal.cache.InternalCache)3 File (java.io.File)2 IOException (java.io.IOException)2 InternalDistributedSystem (org.apache.geode.distributed.internal.InternalDistributedSystem)2 CancelException (org.apache.geode.CancelException)1 ForcedDisconnectException (org.apache.geode.ForcedDisconnectException)1 GemFireConfigException (org.apache.geode.GemFireConfigException)1 GemFireIOException (org.apache.geode.GemFireIOException)1 SystemConnectException (org.apache.geode.SystemConnectException)1 Cache (org.apache.geode.cache.Cache)1 CacheClosedException (org.apache.geode.cache.CacheClosedException)1 CacheFactory (org.apache.geode.cache.CacheFactory)1 CacheXmlException (org.apache.geode.cache.CacheXmlException)1 DiskStoreFactory (org.apache.geode.cache.DiskStoreFactory)1 GatewayReceiver (org.apache.geode.cache.wan.GatewayReceiver)1 GatewayReceiverFactory (org.apache.geode.cache.wan.GatewayReceiverFactory)1 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)1 DistributedSystem (org.apache.geode.distributed.DistributedSystem)1