Search in sources :

Example 1 with ConfigurationResponse

use of org.apache.geode.management.internal.configuration.messages.ConfigurationResponse in project geode by apache.

the class GemFireCacheImpl method initialize.

/**
   * Perform initialization, solve the early escaped reference problem by putting publishing
   * references to this instance in this method (vs. the constructor).
   */
private void initialize() {
    if (GemFireCacheImpl.instance != null) {
        Assert.assertTrue(GemFireCacheImpl.instance == null, "Cache instance already in place: " + instance);
    }
    GemFireCacheImpl.instance = this;
    GemFireCacheImpl.pdxInstance = this;
    for (CacheLifecycleListener listener : cacheLifecycleListeners) {
        listener.cacheCreated(this);
    }
    ClassPathLoader.setLatestToDefault(this.system.getConfig().getDeployWorkingDir());
    // request and check cluster configuration
    ConfigurationResponse configurationResponse = requestSharedConfiguration();
    deployJarsReceivedFromClusterConfiguration(configurationResponse);
    // apply the cluster's properties configuration and initialize security using that configuration
    ClusterConfigurationLoader.applyClusterPropertiesConfiguration(this, configurationResponse, this.system.getConfig());
    // first initialize the security service using the security properties
    this.securityService.initSecurity(this.system.getConfig().getSecurityProps());
    // secondly if cacheConfig has a securityManager, use that instead
    if (this.cacheConfig.getSecurityManager() != null) {
        this.securityService.setSecurityManager(this.cacheConfig.getSecurityManager());
    }
    // if cacheConfig has a postProcessor, use that instead
    if (this.cacheConfig.getPostProcessor() != null) {
        this.securityService.setPostProcessor(this.cacheConfig.getPostProcessor());
    }
    SystemMemberCacheEventProcessor.send(this, Operation.CACHE_CREATE);
    this.resourceAdvisor.initializationGate();
    // Register function that we need to execute to fetch available REST service endpoints in DS
    FunctionService.registerFunction(new FindRestEnabledServersFunction());
    // moved this after initializeDeclarativeCache because in the future
    // distributed system creation will not happen until we have read
    // cache.xml file.
    // For now this needs to happen before cache.xml otherwise
    // we will not be ready for all the events that cache.xml
    // processing can deliver (region creation, etc.).
    // This call may need to be moved inside initializeDeclarativeCache.
    // Entry to GemFire Management service
    this.jmxAdvisor.initializationGate();
    // this starts up the ManagementService, register and federate the internal beans
    this.system.handleResourceEvent(ResourceEvent.CACHE_CREATE, this);
    initializeServices();
    boolean completedCacheXml = false;
    try {
        if (configurationResponse == null) {
            // Deploy all the jars from the deploy working dir.
            ClassPathLoader.getLatest().getJarDeployer().loadPreviouslyDeployedJarsFromDisk();
        }
        ClusterConfigurationLoader.applyClusterXmlConfiguration(this, configurationResponse, this.system.getConfig());
        initializeDeclarativeCache();
        completedCacheXml = true;
    } finally {
        if (!completedCacheXml) {
            // so initializeDeclarativeCache threw an exception
            try {
                // fix for bug 34041
                close();
            } catch (Throwable ignore) {
            // I don't want init to throw an exception that came from the close.
            // I want it to throw the original exception that came from initializeDeclarativeCache.
            }
        }
    }
    this.poolFactory = null;
    startColocatedJmxManagerLocator();
    startMemcachedServer();
    startRedisServer();
    startRestAgentServer(this);
    this.isInitialized = true;
}
Also used : ConfigurationResponse(org.apache.geode.management.internal.configuration.messages.ConfigurationResponse) FindRestEnabledServersFunction(org.apache.geode.internal.cache.execute.util.FindRestEnabledServersFunction)

Example 2 with ConfigurationResponse

use of org.apache.geode.management.internal.configuration.messages.ConfigurationResponse in project geode by apache.

the class ClusterConfigurationService method createConfigurationResponse.

/**
   * Creates a ConfigurationResponse based on the configRequest, configuration response contains the
   * requested shared configuration This method locks the ClusterConfigurationService
   */
public ConfigurationResponse createConfigurationResponse(final ConfigurationRequest configRequest) throws LeaseExpiredException, IOException {
    ConfigurationResponse configResponse = new ConfigurationResponse();
    for (int i = 0; i < configRequest.getNumAttempts(); i++) {
        boolean isLocked = this.sharedConfigLockingService.lock(SHARED_CONFIG_LOCK_NAME, 5000, 5000);
        try {
            if (isLocked) {
                Set<String> groups = configRequest.getGroups();
                groups.add(ClusterConfigurationService.CLUSTER_CONFIG);
                logger.info("Building up configuration response with following configurations: {}", groups);
                for (String group : groups) {
                    Configuration configuration = getConfiguration(group);
                    configResponse.addConfiguration(configuration);
                }
                Map<String, byte[]> jarNamesToJarBytes = getAllJarsFromThisLocator(groups);
                String[] jarNames = jarNamesToJarBytes.keySet().stream().toArray(String[]::new);
                byte[][] jarBytes = jarNamesToJarBytes.values().toArray(new byte[jarNames.length][]);
                configResponse.addJarsToBeDeployed(jarNames, jarBytes);
                configResponse.setFailedToGetSharedConfig(false);
                return configResponse;
            }
        } finally {
            this.sharedConfigLockingService.unlock(SHARED_CONFIG_LOCK_NAME);
        }
    }
    configResponse.setFailedToGetSharedConfig(true);
    return configResponse;
}
Also used : ConfigurationResponse(org.apache.geode.management.internal.configuration.messages.ConfigurationResponse) Configuration(org.apache.geode.management.internal.configuration.domain.Configuration)

Example 3 with ConfigurationResponse

use of org.apache.geode.management.internal.configuration.messages.ConfigurationResponse in project geode by apache.

the class ClusterConfigurationServiceDUnitTest method testSharedConfigurationService.

@Test
public void testSharedConfigurationService() throws Exception {
    // Start the Locator and wait for shared configuration to be available
    final String testGroup = "G1";
    final String clusterLogLevel = "error";
    final String groupLogLevel = "fine";
    final String testName = getName();
    final VM locator1Vm = getHost(0).getVM(1);
    final VM dataMemberVm = getHost(0).getVM(2);
    final VM locator2Vm = getHost(0).getVM(3);
    final int[] ports = getRandomAvailableTCPPorts(3);
    final int locator1Port = ports[0];
    locator1Vm.invoke(() -> {
        final File locatorLogFile = new File(testName + "-locator-" + locator1Port + ".log");
        final Properties locatorProps = new Properties();
        locatorProps.setProperty(NAME, "Locator1");
        locatorProps.setProperty(MCAST_PORT, "0");
        locatorProps.setProperty(LOG_LEVEL, "info");
        locatorProps.setProperty(ENABLE_CLUSTER_CONFIGURATION, "true");
        try {
            final InternalLocator locator = (InternalLocator) Locator.startLocatorAndDS(locator1Port, 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, TIMEOUT, INTERVAL, true);
        } catch (IOException e) {
            fail("Unable to create a locator with a shared configuration", e);
        }
    });
    XmlEntity xmlEntity = dataMemberVm.invoke(() -> {
        Properties localProps = new Properties();
        localProps.setProperty(MCAST_PORT, "0");
        localProps.setProperty(LOCATORS, "localhost[" + locator1Port + "]");
        localProps.setProperty(GROUPS, testGroup);
        getSystem(localProps);
        Cache cache = getCache();
        assertNotNull(cache);
        DiskStoreFactory dsFactory = cache.createDiskStoreFactory();
        File dsDir = new File("dsDir");
        if (!dsDir.exists()) {
            dsDir.mkdir();
        }
        dsFactory.setDiskDirs(new File[] { dsDir });
        dsFactory.create(DISKSTORENAME);
        RegionFactory regionFactory = getCache().createRegionFactory(RegionShortcut.REPLICATE);
        regionFactory.create(REGION1);
        return new XmlEntity(CacheXml.REGION, "name", REGION1);
    });
    locator1Vm.invoke(() -> {
        ClusterConfigurationService sc = InternalLocator.getLocator().getSharedConfiguration();
        sc.addXmlEntity(xmlEntity, new String[] { testGroup });
        // Modify property and cache attributes
        Properties clusterProperties = new Properties();
        clusterProperties.setProperty(LOG_LEVEL, clusterLogLevel);
        XmlEntity cacheEntity = XmlEntity.builder().withType(CacheXml.CACHE).build();
        Map<String, String> cacheAttributes = new HashMap<String, String>();
        cacheAttributes.put(CacheXml.COPY_ON_READ, "true");
        sc.modifyXmlAndProperties(clusterProperties, cacheEntity, null);
        clusterProperties.setProperty(LOG_LEVEL, groupLogLevel);
        sc.modifyXmlAndProperties(clusterProperties, cacheEntity, new String[] { testGroup });
        // Add a jar
        byte[][] jarBytes = new byte[1][];
        jarBytes[0] = "Hello".getBytes();
        assertTrue(sc.addJarsToThisLocator(new String[] { "foo.jar" }, jarBytes, null));
        // Add a jar for the group
        jarBytes = new byte[1][];
        jarBytes[0] = "Hello".getBytes();
        assertTrue(sc.addJarsToThisLocator(new String[] { "bar.jar" }, jarBytes, new String[] { testGroup }));
    });
    final int locator2Port = ports[1];
    // Create another locator in VM2
    locator2Vm.invoke(() -> {
        final File locatorLogFile = new File(testName + "-locator-" + locator2Port + ".log");
        final Properties locatorProps = new Properties();
        locatorProps.setProperty(NAME, "Locator2");
        locatorProps.setProperty(MCAST_PORT, "0");
        locatorProps.setProperty(LOG_LEVEL, "info");
        locatorProps.setProperty(ENABLE_CLUSTER_CONFIGURATION, "true");
        locatorProps.setProperty(LOCATORS, "localhost[" + locator1Port + "]");
        try {
            final InternalLocator locator = (InternalLocator) Locator.startLocatorAndDS(locator2Port, 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, TIMEOUT, INTERVAL, true);
        } catch (IOException e) {
            fail("Unable to create a locator with a shared configuration", e);
        }
        InternalLocator locator = (InternalLocator) Locator.getLocator();
        ClusterConfigurationService sharedConfig = locator.getSharedConfiguration();
        Map<String, Configuration> entireConfiguration = sharedConfig.getEntireConfiguration();
        Configuration clusterConfig = entireConfiguration.get(ClusterConfigurationService.CLUSTER_CONFIG);
        assertNotNull(clusterConfig);
        assertNotNull(clusterConfig.getJarNames());
        assertTrue(clusterConfig.getJarNames().contains("foo.jar"));
        assertTrue(clusterConfig.getGemfireProperties().getProperty(LOG_LEVEL).equals(clusterLogLevel));
        assertNotNull(clusterConfig.getCacheXmlContent());
        Configuration testGroupConfiguration = entireConfiguration.get(testGroup);
        assertNotNull(testGroupConfiguration);
        assertNotNull(testGroupConfiguration.getJarNames());
        assertTrue(testGroupConfiguration.getJarNames().contains("bar.jar"));
        assertTrue(testGroupConfiguration.getGemfireProperties().getProperty(LOG_LEVEL).equals(groupLogLevel));
        assertNotNull(testGroupConfiguration.getCacheXmlContent());
        assertTrue(testGroupConfiguration.getCacheXmlContent().contains(REGION1));
        Map<String, byte[]> jarData = sharedConfig.getAllJarsFromThisLocator(entireConfiguration.keySet());
        String[] jarNames = jarData.keySet().stream().toArray(String[]::new);
        byte[][] jarBytes = jarData.values().toArray(new byte[jarNames.length][]);
        assertNotNull(jarNames);
        assertNotNull(jarBytes);
        sharedConfig.deleteXmlEntity(new XmlEntity(CacheXml.REGION, "name", REGION1), new String[] { testGroup });
        sharedConfig.removeJars(new String[] { "foo.jar" }, null);
        sharedConfig.removeJars(null, null);
    });
    dataMemberVm.invoke(() -> {
        Set<String> groups = new HashSet<String>();
        groups.add(testGroup);
        ConfigurationRequest configRequest = new ConfigurationRequest(groups);
        ConfigurationResponse configResponse = (ConfigurationResponse) new TcpClient().requestToServer(InetAddress.getByName("localhost"), locator2Port, configRequest, 1000);
        assertNotNull(configResponse);
        Map<String, Configuration> requestedConfiguration = configResponse.getRequestedConfiguration();
        Configuration clusterConfiguration = requestedConfiguration.get(ClusterConfigurationService.CLUSTER_CONFIG);
        assertNotNull(clusterConfiguration);
        assertTrue(configResponse.getJarNames().length == 0);
        assertTrue(configResponse.getJars().length == 0);
        assertTrue(clusterConfiguration.getJarNames().isEmpty());
        assertTrue(clusterConfiguration.getGemfireProperties().getProperty(LOG_LEVEL).equals(clusterLogLevel));
        Configuration testGroupConfiguration = requestedConfiguration.get(testGroup);
        assertNotNull(testGroupConfiguration);
        assertFalse(testGroupConfiguration.getCacheXmlContent().contains(REGION1));
        assertTrue(testGroupConfiguration.getJarNames().isEmpty());
        assertTrue(testGroupConfiguration.getGemfireProperties().getProperty(LOG_LEVEL).equals(groupLogLevel));
        GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
        Map<InternalDistributedMember, Collection<String>> locatorsWithSharedConfiguration = cache.getDistributionManager().getAllHostedLocatorsWithSharedConfiguration();
        assertFalse(locatorsWithSharedConfiguration.isEmpty());
        assertTrue(locatorsWithSharedConfiguration.size() == 2);
        Set<InternalDistributedMember> locatorMembers = locatorsWithSharedConfiguration.keySet();
        for (InternalDistributedMember locatorMember : locatorMembers) {
            System.out.println(locatorMember);
        }
        return null;
    });
}
Also used : ConfigurationResponse(org.apache.geode.management.internal.configuration.messages.ConfigurationResponse) Configuration(org.apache.geode.management.internal.configuration.domain.Configuration) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) RegionFactory(org.apache.geode.cache.RegionFactory) TcpClient(org.apache.geode.distributed.internal.tcpserver.TcpClient) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) IOException(java.io.IOException) DiskStoreFactory(org.apache.geode.cache.DiskStoreFactory) XmlEntity(org.apache.geode.management.internal.configuration.domain.XmlEntity) InternalLocator(org.apache.geode.distributed.internal.InternalLocator) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) ClusterConfigurationService(org.apache.geode.distributed.internal.ClusterConfigurationService) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) ConfigurationRequest(org.apache.geode.management.internal.configuration.messages.ConfigurationRequest) VM(org.apache.geode.test.dunit.VM) File(java.io.File) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 4 with ConfigurationResponse

use of org.apache.geode.management.internal.configuration.messages.ConfigurationResponse in project geode by apache.

the class ClusterConfigurationLoader method requestConfigurationFromLocators.

/**
   * Request the shared configuration for group(s) from locator(s) this member is bootstrapped with.
   *
   * This will request the group config this server belongs plus the "cluster" config
   * 
   * @param config this member's configuration.
   * @return {@link ConfigurationResponse}
   */
public static ConfigurationResponse requestConfigurationFromLocators(DistributionConfig config, List<String> locatorList) throws ClusterConfigurationNotAvailableException, UnknownHostException {
    List<String> groups = ClusterConfigurationLoader.getGroups(config);
    ConfigurationRequest request = new ConfigurationRequest();
    request.addGroups(ClusterConfigurationService.CLUSTER_CONFIG);
    for (String group : groups) {
        request.addGroups(group);
    }
    request.setNumAttempts(10);
    ConfigurationResponse response = null;
    // Try talking to all the locators in the list
    // to get the shared configuration.
    TcpClient client = new TcpClient();
    for (String locatorInfo : locatorList) {
        DistributionLocatorId dlId = new DistributionLocatorId(locatorInfo);
        String ipaddress = dlId.getBindAddress();
        InetAddress locatorInetAddress = null;
        if (StringUtils.isNotBlank(ipaddress)) {
            locatorInetAddress = InetAddress.getByName(ipaddress);
        } else {
            locatorInetAddress = dlId.getHost();
        }
        int port = dlId.getPort();
        try {
            response = (ConfigurationResponse) client.requestToServer(locatorInetAddress, port, request, 10000);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Log
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    if (response == null || response.failedToGetSharedConfig()) {
        throw new ClusterConfigurationNotAvailableException(LocalizedStrings.Launcher_Command_FAILED_TO_GET_SHARED_CONFIGURATION.toLocalizedString());
    }
    return response;
}
Also used : ConfigurationResponse(org.apache.geode.management.internal.configuration.messages.ConfigurationResponse) UnknownHostException(java.net.UnknownHostException) DistributionLocatorId(org.apache.geode.internal.admin.remote.DistributionLocatorId) ConfigurationRequest(org.apache.geode.management.internal.configuration.messages.ConfigurationRequest) TcpClient(org.apache.geode.distributed.internal.tcpserver.TcpClient) IOException(java.io.IOException) ClusterConfigurationNotAvailableException(org.apache.geode.internal.process.ClusterConfigurationNotAvailableException) InetAddress(java.net.InetAddress)

Example 5 with ConfigurationResponse

use of org.apache.geode.management.internal.configuration.messages.ConfigurationResponse in project geode by apache.

the class ClusterConfigurationLoader method deployJarsReceivedFromClusterConfiguration.

/**
   * Deploys the jars received from shared configuration, it undeploys any other jars that were not
   * part of shared configuration
   * 
   * @param cache Cache of this member
   * @param response {@link ConfigurationResponse} received from the locators
   */
public static void deployJarsReceivedFromClusterConfiguration(Cache cache, ConfigurationResponse response) throws IOException, ClassNotFoundException {
    logger.info("Requesting cluster configuration");
    if (response == null) {
        return;
    }
    String[] jarFileNames = response.getJarNames();
    byte[][] jarBytes = response.getJars();
    logger.info("Got response with jars: {}", Stream.of(jarFileNames).collect(joining(",")));
    if (jarFileNames != null && jarBytes != null) {
        JarDeployer jarDeployer = ClassPathLoader.getLatest().getJarDeployer();
        jarDeployer.suspendAll();
        try {
            List<String> extraJarsOnServer = jarDeployer.findDeployedJars().stream().map(DeployedJar::getJarName).filter(jarName -> !ArrayUtils.contains(jarFileNames, jarName)).collect(toList());
            for (String extraJar : extraJarsOnServer) {
                logger.info("Removing jar not present in cluster configuration: {}", extraJar);
                jarDeployer.deleteAllVersionsOfJar(extraJar);
            }
            List<DeployedJar> deployedJars = jarDeployer.deploy(jarFileNames, jarBytes);
            deployedJars.stream().filter(Objects::nonNull).forEach((jar) -> logger.info("Deployed: {}", jar.getFile().getAbsolutePath()));
        } finally {
            jarDeployer.resumeAll();
        }
    }
}
Also used : ClassPathLoader(org.apache.geode.internal.ClassPathLoader) StringUtils(org.apache.commons.lang.StringUtils) Arrays(java.util.Arrays) ClusterConfigurationService(org.apache.geode.distributed.internal.ClusterConfigurationService) DeployedJar(org.apache.geode.internal.DeployedJar) JarDeployer(org.apache.geode.internal.JarDeployer) LocalizedStrings(org.apache.geode.internal.i18n.LocalizedStrings) DistributionLocatorId(org.apache.geode.internal.admin.remote.DistributionLocatorId) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) Cache(org.apache.geode.cache.Cache) ConfigSource(org.apache.geode.internal.ConfigSource) ConfigurationRequest(org.apache.geode.management.internal.configuration.messages.ConfigurationRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) UnmodifiableException(org.apache.geode.UnmodifiableException) LogService(org.apache.geode.internal.logging.LogService) Map(java.util.Map) LinkedList(java.util.LinkedList) ClusterConfigurationNotAvailableException(org.apache.geode.internal.process.ClusterConfigurationNotAvailableException) Properties(java.util.Properties) TcpClient(org.apache.geode.distributed.internal.tcpserver.TcpClient) Set(java.util.Set) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) Collectors.joining(java.util.stream.Collectors.joining) Objects(java.util.Objects) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) DistributionConfig(org.apache.geode.distributed.internal.DistributionConfig) Logger(org.apache.logging.log4j.Logger) Stream(java.util.stream.Stream) Configuration(org.apache.geode.management.internal.configuration.domain.Configuration) ConfigurationResponse(org.apache.geode.management.internal.configuration.messages.ConfigurationResponse) ArrayUtils(org.apache.commons.lang.ArrayUtils) InputStream(java.io.InputStream) DeployedJar(org.apache.geode.internal.DeployedJar) JarDeployer(org.apache.geode.internal.JarDeployer)

Aggregations

ConfigurationResponse (org.apache.geode.management.internal.configuration.messages.ConfigurationResponse)6 Configuration (org.apache.geode.management.internal.configuration.domain.Configuration)4 IOException (java.io.IOException)3 UnknownHostException (java.net.UnknownHostException)3 TcpClient (org.apache.geode.distributed.internal.tcpserver.TcpClient)3 ClusterConfigurationNotAvailableException (org.apache.geode.internal.process.ClusterConfigurationNotAvailableException)3 ConfigurationRequest (org.apache.geode.management.internal.configuration.messages.ConfigurationRequest)3 InetAddress (java.net.InetAddress)2 Properties (java.util.Properties)2 Cache (org.apache.geode.cache.Cache)2 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)2 ClusterConfigurationService (org.apache.geode.distributed.internal.ClusterConfigurationService)2 DistributionConfig (org.apache.geode.distributed.internal.DistributionConfig)2 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)2 DistributionLocatorId (org.apache.geode.internal.admin.remote.DistributionLocatorId)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1