Search in sources :

Example 16 with ClusterConfigurationService

use of org.apache.geode.distributed.internal.ClusterConfigurationService in project geode by apache.

the class DiskStoreCommandsDUnitTest method testCreateDestroyUpdatesSharedConfig.

/**
   * Asserts that creating and destroying disk stores correctly updates the shared configuration.
   */
// GEODE-1406
@Category(FlakyTest.class)
@Test
public void testCreateDestroyUpdatesSharedConfig() {
    disconnectAllFromDS();
    final int[] ports = AvailablePortHelper.getRandomAvailableTCPPorts(2);
    jmxPort = ports[0];
    httpPort = ports[1];
    try {
        jmxHost = InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException ignore) {
        jmxHost = "localhost";
    }
    final String groupName = "testDiskStoreSharedConfigGroup";
    final String diskStoreName = "testDiskStoreSharedConfigDiskStore";
    // Start the Locator and wait for shared configuration to be available
    final int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
    final Properties locatorProps = new Properties();
    locatorProps.setProperty(NAME, "Locator");
    locatorProps.setProperty(MCAST_PORT, "0");
    locatorProps.setProperty(LOG_LEVEL, "fine");
    locatorProps.setProperty(ENABLE_CLUSTER_CONFIGURATION, "true");
    locatorProps.setProperty(JMX_MANAGER, "true");
    locatorProps.setProperty(JMX_MANAGER_START, "true");
    locatorProps.setProperty(JMX_MANAGER_BIND_ADDRESS, String.valueOf(jmxHost));
    locatorProps.setProperty(JMX_MANAGER_PORT, String.valueOf(jmxPort));
    locatorProps.setProperty(HTTP_SERVICE_PORT, String.valueOf(httpPort));
    Host.getHost(0).getVM(0).invoke(new SerializableRunnable() {

        @Override
        public void run() {
            final File locatorLogFile = new File("locator-" + locatorPort + ".log");
            try {
                final InternalLocator locator = (InternalLocator) Locator.startLocatorAndDS(locatorPort, locatorLogFile, null, locatorProps);
                WaitCriterion wc = new WaitCriterion() {

                    @Override
                    public boolean done() {
                        return locator.isSharedConfigurationRunning();
                    }

                    @Override
                    public String description() {
                        return "Waiting for shared configuration to be started";
                    }
                };
                waitForCriterion(wc, 5000, 500, true);
            } catch (IOException ioex) {
                fail("Unable to create a locator with a shared configuration");
            }
        }
    });
    connect(jmxHost, jmxPort, httpPort, getDefaultShell());
    // Create a cache in VM 1
    final File diskStoreDir = new File(new File(".").getAbsolutePath(), diskStoreName);
    this.filesToBeDeleted.add(diskStoreDir.getAbsolutePath());
    VM vm = Host.getHost(0).getVM(1);
    vm.invoke(new SerializableRunnable() {

        @Override
        public void run() {
            diskStoreDir.mkdirs();
            Properties localProps = new Properties();
            localProps.setProperty(MCAST_PORT, "0");
            localProps.setProperty(LOCATORS, "localhost[" + locatorPort + "]");
            localProps.setProperty(GROUPS, groupName);
            getSystem(localProps);
            assertNotNull(getCache());
        }
    });
    // Test creating the disk store
    CommandStringBuilder commandStringBuilder = new CommandStringBuilder(CliStrings.CREATE_DISK_STORE);
    commandStringBuilder.addOption(CliStrings.CREATE_DISK_STORE__NAME, diskStoreName);
    commandStringBuilder.addOption(CliStrings.CREATE_DISK_STORE__GROUP, groupName);
    commandStringBuilder.addOption(CliStrings.CREATE_DISK_STORE__DIRECTORY_AND_SIZE, diskStoreDir.getAbsolutePath());
    CommandResult cmdResult = executeCommand(commandStringBuilder.toString());
    assertEquals(Result.Status.OK, cmdResult.getStatus());
    // Make sure the disk store exists in the shared config
    Host.getHost(0).getVM(0).invoke(new SerializableRunnable() {

        @Override
        public void run() {
            ClusterConfigurationService sharedConfig = ((InternalLocator) Locator.getLocator()).getSharedConfiguration();
            String xmlFromConfig;
            try {
                xmlFromConfig = sharedConfig.getConfiguration(groupName).getCacheXmlContent();
                assertTrue(xmlFromConfig.contains(diskStoreName));
            } catch (Exception e) {
                fail("Error occurred in cluster configuration service", e);
            }
        }
    });
    // Restart the cache and make sure it has the diskstore
    vm = Host.getHost(0).getVM(1);
    vm.invoke(new SerializableCallable() {

        @Override
        public Object call() {
            getCache().close();
            Properties localProps = new Properties();
            localProps.setProperty(MCAST_PORT, "0");
            localProps.setProperty(LOCATORS, "localhost[" + locatorPort + "]");
            localProps.setProperty(GROUPS, groupName);
            localProps.setProperty(USE_CLUSTER_CONFIGURATION, "true");
            getSystem(localProps);
            Cache cache = getCache();
            assertNotNull(cache);
            InternalCache internalCache = (InternalCache) cache;
            Collection<DiskStore> diskStoreList = internalCache.listDiskStores();
            assertNotNull(diskStoreList);
            assertFalse(diskStoreList.isEmpty());
            assertTrue(diskStoreList.size() == 1);
            for (DiskStore diskStore : diskStoreList) {
                assertTrue(diskStore.getName().equals(diskStoreName));
                break;
            }
            return null;
        }
    });
    // Test destroying the disk store
    commandStringBuilder = new CommandStringBuilder(CliStrings.DESTROY_DISK_STORE);
    commandStringBuilder.addOption(CliStrings.DESTROY_DISK_STORE__NAME, diskStoreName);
    commandStringBuilder.addOption(CliStrings.DESTROY_DISK_STORE__GROUP, groupName);
    cmdResult = executeCommand(commandStringBuilder.toString());
    assertEquals(Result.Status.OK, cmdResult.getStatus());
    // Make sure the disk store was removed from the shared config
    Host.getHost(0).getVM(0).invoke(new SerializableRunnable() {

        @Override
        public void run() {
            ClusterConfigurationService sharedConfig = ((InternalLocator) Locator.getLocator()).getSharedConfiguration();
            String xmlFromConfig;
            try {
                xmlFromConfig = sharedConfig.getConfiguration(groupName).getCacheXmlContent();
                assertFalse(xmlFromConfig.contains(diskStoreName));
            } catch (Exception e) {
                fail("Error occurred in cluster configuration service", e);
            }
        }
    });
    // Restart the cache and make sure it DOES NOT have the diskstore
    vm = Host.getHost(0).getVM(1);
    vm.invoke(new SerializableCallable() {

        @Override
        public Object call() {
            getCache().close();
            Properties localProps = new Properties();
            localProps.setProperty(MCAST_PORT, "0");
            localProps.setProperty(LOCATORS, "localhost[" + locatorPort + "]");
            localProps.setProperty(GROUPS, groupName);
            localProps.setProperty(USE_CLUSTER_CONFIGURATION, "true");
            getSystem(localProps);
            Cache cache = getCache();
            assertNotNull(cache);
            InternalCache internalCache = (InternalCache) cache;
            Collection<DiskStore> diskStores = internalCache.listDiskStores();
            assertNotNull(diskStores);
            assertTrue(diskStores.isEmpty());
            return null;
        }
    });
}
Also used : UnknownHostException(java.net.UnknownHostException) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) InternalCache(org.apache.geode.internal.cache.InternalCache) IOException(java.io.IOException) Properties(java.util.Properties) DistributedSystemDisconnectedException(org.apache.geode.distributed.DistributedSystemDisconnectedException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) CommandResult(org.apache.geode.management.internal.cli.result.CommandResult) DiskStore(org.apache.geode.cache.DiskStore) InternalLocator(org.apache.geode.distributed.internal.InternalLocator) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) ClusterConfigurationService(org.apache.geode.distributed.internal.ClusterConfigurationService) CommandStringBuilder(org.apache.geode.management.internal.cli.util.CommandStringBuilder) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Collection(java.util.Collection) File(java.io.File) Cache(org.apache.geode.cache.Cache) InternalCache(org.apache.geode.internal.cache.InternalCache) Category(org.junit.experimental.categories.Category) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Aggregations

ClusterConfigurationService (org.apache.geode.distributed.internal.ClusterConfigurationService)16 InternalLocator (org.apache.geode.distributed.internal.InternalLocator)15 File (java.io.File)11 IOException (java.io.IOException)10 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)10 Test (org.junit.Test)10 Properties (java.util.Properties)9 VM (org.apache.geode.test.dunit.VM)9 UnknownHostException (java.net.UnknownHostException)6 Cache (org.apache.geode.cache.Cache)6 CommandResult (org.apache.geode.management.internal.cli.result.CommandResult)6 CommandStringBuilder (org.apache.geode.management.internal.cli.util.CommandStringBuilder)6 WaitCriterion (org.apache.geode.test.dunit.WaitCriterion)6 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)6 Configuration (org.apache.geode.management.internal.configuration.domain.Configuration)4 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)4 Category (org.junit.experimental.categories.Category)4 Region (org.apache.geode.cache.Region)3 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)3 FileNotFoundException (java.io.FileNotFoundException)2