|
| 1 | +/* |
| 2 | + * Copyright 2016-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | +package org.springframework.data.gemfire.function.execution.onservers; |
| 18 | + |
| 19 | +import org.apache.geode.StatisticDescriptor; |
| 20 | +import org.apache.geode.Statistics; |
| 21 | +import org.apache.geode.StatisticsType; |
| 22 | +import org.apache.geode.cache.Cache; |
| 23 | +import org.apache.geode.cache.CacheFactory; |
| 24 | +import org.apache.geode.cache.execute.Function; |
| 25 | +import org.apache.geode.cache.execute.FunctionContext; |
| 26 | +import org.apache.geode.cache.execute.FunctionService; |
| 27 | +import org.apache.geode.cache.server.CacheServer; |
| 28 | +import org.apache.geode.distributed.internal.InternalDistributedSystem; |
| 29 | +import org.apache.geode.internal.statistics.StatisticsManager; |
| 30 | +import org.junit.AfterClass; |
| 31 | +import org.junit.BeforeClass; |
| 32 | +import org.junit.Test; |
| 33 | +import org.junit.runner.RunWith; |
| 34 | +import org.springframework.beans.factory.annotation.Autowired; |
| 35 | +import org.springframework.context.annotation.Configuration; |
| 36 | +import org.springframework.data.gemfire.config.annotation.ClientCacheApplication; |
| 37 | +import org.springframework.data.gemfire.function.config.EnableGemfireFunctionExecutions; |
| 38 | +import org.springframework.data.gemfire.process.ProcessWrapper; |
| 39 | +import org.springframework.data.gemfire.test.support.ClientServerIntegrationTestsSupport; |
| 40 | +import org.springframework.data.gemfire.transaction.config.EnableGemfireCacheTransactions; |
| 41 | +import org.springframework.test.context.ContextConfiguration; |
| 42 | +import org.springframework.test.context.junit4.SpringRunner; |
| 43 | + |
| 44 | +import java.io.IOException; |
| 45 | +import java.util.ArrayList; |
| 46 | +import java.util.List; |
| 47 | + |
| 48 | +import static org.assertj.core.api.Assertions.assertThat; |
| 49 | + |
| 50 | +/** |
| 51 | + * @author Patrick Johnson |
| 52 | + */ |
| 53 | +@SuppressWarnings("unused") |
| 54 | +@RunWith(SpringRunner.class) |
| 55 | +@ContextConfiguration(classes = FunctionsReturnResultsFromAllServersIntegrationTests.Config.class) |
| 56 | +public class FunctionsReturnResultsFromAllServersIntegrationTests extends ClientServerIntegrationTestsSupport { |
| 57 | + |
| 58 | + private static final int PORT_1 = 40407; |
| 59 | + private static final int PORT_2 = 40403; |
| 60 | + |
| 61 | + private static ProcessWrapper gemfireServer1; |
| 62 | + private static ProcessWrapper gemfireServer2; |
| 63 | + |
| 64 | + @Autowired |
| 65 | + private AllServersAdminFunctions allServersAdminFunctions; |
| 66 | + |
| 67 | + @Autowired |
| 68 | + private SingleServerAdminFunctions singleServerAdminFunctions; |
| 69 | + |
| 70 | + @BeforeClass |
| 71 | + public static void startGemFireServer() throws Exception { |
| 72 | + |
| 73 | + gemfireServer1 = run(MetricsFunctionServerProcess.class, |
| 74 | + String.format("-D%s=%d", GEMFIRE_CACHE_SERVER_PORT_PROPERTY, PORT_1)); |
| 75 | + |
| 76 | + waitForServerToStart(DEFAULT_HOSTNAME, PORT_1); |
| 77 | + |
| 78 | + gemfireServer2 = run(MetricsFunctionServerProcess.class, |
| 79 | + String.format("-D%s=%d", GEMFIRE_CACHE_SERVER_PORT_PROPERTY, PORT_2)); |
| 80 | + |
| 81 | + waitForServerToStart(DEFAULT_HOSTNAME, PORT_2); |
| 82 | + } |
| 83 | + |
| 84 | + @ClientCacheApplication(servers = {@ClientCacheApplication.Server(port = PORT_1), @ClientCacheApplication.Server(port = PORT_2)}) |
| 85 | + @Configuration |
| 86 | + @EnableGemfireFunctionExecutions(basePackageClasses = AllServersAdminFunctions.class) |
| 87 | + @EnableGemfireCacheTransactions |
| 88 | + static class Config { } |
| 89 | + |
| 90 | + @AfterClass |
| 91 | + public static void stopGemFireServer() { |
| 92 | + stop(gemfireServer1); |
| 93 | + stop(gemfireServer2); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void executeFunctionOnAllServers() { |
| 98 | + List<List<Metric>> metrics = allServersAdminFunctions.getAllMetrics(); |
| 99 | + assertThat(metrics.size()).isEqualTo(2); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void executeFunctionOnSingleServer() { |
| 104 | + List<Metric> metrics = singleServerAdminFunctions.getAllMetrics(); |
| 105 | + assertThat(metrics.size()).isEqualTo(672); |
| 106 | + } |
| 107 | + |
| 108 | + static class MetricsFunctionServerProcess { |
| 109 | + |
| 110 | + private static final int DEFAULT_CACHE_SERVER_PORT = 40404; |
| 111 | + |
| 112 | + private static final String CACHE_SERVER_PORT_PROPERTY = "spring.data.gemfire.cache.server.port"; |
| 113 | + private static final String GEMFIRE_LOG_LEVEL = "error"; |
| 114 | + private static final String GEMFIRE_NAME = "MetricsServer" + getCacheServerPort(); |
| 115 | + |
| 116 | + public static void main(String[] args) throws Exception { |
| 117 | + registerFunctions(startCacheServer(newGemFireCache())); |
| 118 | + } |
| 119 | + |
| 120 | + private static Cache newGemFireCache() { |
| 121 | + |
| 122 | + return new CacheFactory() |
| 123 | + .set("name", GEMFIRE_NAME) |
| 124 | + .set("log-level", GEMFIRE_LOG_LEVEL) |
| 125 | + .create(); |
| 126 | + } |
| 127 | + |
| 128 | + private static Cache startCacheServer(Cache gemfireCache) throws IOException { |
| 129 | + CacheServer cacheServer = gemfireCache.addCacheServer(); |
| 130 | + cacheServer.setPort(getCacheServerPort()); |
| 131 | + cacheServer.start(); |
| 132 | + return gemfireCache; |
| 133 | + } |
| 134 | + |
| 135 | + private static int getCacheServerPort() { |
| 136 | + return Integer.getInteger(CACHE_SERVER_PORT_PROPERTY, DEFAULT_CACHE_SERVER_PORT); |
| 137 | + } |
| 138 | + |
| 139 | + private static Cache registerFunctions(Cache gemfireCache) { |
| 140 | + |
| 141 | + FunctionService.registerFunction(new GetAllMetricsFunction()); |
| 142 | + |
| 143 | + return gemfireCache; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + static class GetAllMetricsFunction implements Function<List<Metric>> { |
| 148 | + |
| 149 | + private final InternalDistributedSystem system = |
| 150 | + (InternalDistributedSystem) CacheFactory.getAnyInstance().getDistributedSystem(); |
| 151 | + |
| 152 | + @Override |
| 153 | + public void execute(FunctionContext context) { |
| 154 | + List<Metric> allMetrics = new ArrayList<>(); |
| 155 | + StatisticsManager statisticsManager = system.getStatisticsManager(); |
| 156 | + for (Statistics statistics : statisticsManager.getStatsList()) { |
| 157 | + StatisticsType statisticsType = statistics.getType(); |
| 158 | + for (StatisticDescriptor descriptor : statisticsType.getStatistics()) { |
| 159 | + String statName = descriptor.getName(); |
| 160 | + Metric metric = new Metric(statName, statistics.get(statName), statisticsType.getName(), statistics.getTextId()); |
| 161 | + allMetrics.add(metric); |
| 162 | + } |
| 163 | + } |
| 164 | + context.getResultSender().lastResult(allMetrics); |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public String getId() { |
| 169 | + return getClass().getSimpleName(); |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments