Skip to content

Commit

Permalink
Merge pull request #243 from mathieucarbou/ittests
Browse files Browse the repository at this point in the history
Issue #191 : HA and Failover Galvan tests
  • Loading branch information
mathieucarbou authored Dec 20, 2016
2 parents 31275e3 + 1426f35 commit d29ac19
Show file tree
Hide file tree
Showing 24 changed files with 1,301 additions and 487 deletions.
7 changes: 7 additions & 0 deletions management/testing/doc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@
<version>${terracotta-core.version}</version>
</dependency>

<!-- sample entity client -->
<dependency>
<groupId>org.terracotta.management.testing</groupId>
<artifactId>sample-entity</artifactId>
<version>${project.version}</version>
</dependency>

<!-- logging -->
<dependency>
<groupId>org.terracotta.internal</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.terracotta.management.entity.tms.client.TmsAgentService;
import org.terracotta.management.model.capabilities.context.CapabilityContext;
import org.terracotta.management.model.cluster.Cluster;
import org.terracotta.management.model.cluster.ServerEntity;
import org.terracotta.management.model.context.Context;

import java.io.IOException;
Expand All @@ -42,7 +43,7 @@ public static void main(String[] args) throws ConnectionException, EntityConfigu

Cluster cluster = tmsAgentService.readTopology();

// 1. find ehcache clients in topology to clear
// REMOTE MANAGEMENT CALL ON A CLIENT
cluster
.clientStream()
.filter(c -> c.getName().startsWith("Ehcache:"))
Expand All @@ -60,6 +61,15 @@ public static void main(String[] args) throws ConnectionException, EntityConfigu
}
});

// REMOTE MANAGEMENT CALL ON A SERVER
ServerEntity serverEntity = cluster
.activeServerEntityStream()
.filter(e -> e.getName().equals("pet-clinic/pets"))
.findFirst()
.get();
Context cacheName = serverEntity.getContext().with("cacheName", "pet-clinic/pets");
tmsAgentService.call(cacheName, "ServerCacheCalls", "clear", Void.TYPE).waitForReturn();

System.in.read();

connection.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ public static void main(String[] args) throws ConnectionException, EntityConfigu
ScheduledFuture<?> task = executorService.scheduleWithFixedDelay(() -> {
try {

// READ M&M MESSAGES and filter NOTIFICATIONS
List<Message> messages = service.readMessages();
System.out.println(messages.size() + " messages");
messages
.stream()
.filter(message -> message.getType().equals("NOTIFICATION"))
.flatMap(message -> message.unwrap(ContextualNotification.class).stream())
.forEach(notification -> System.out.println(" - " + notification.getType() + ": " + notification.getContext() + " - " + notification.getAttributes()));
.forEach(notification -> System.out.println(" - " + notification.getType() + ":\n - " + notification.getContext() + "\n - " + notification.getAttributes()));

} catch (InterruptedException | ExecutionException | TimeoutException e) {
LoggerFactory.getLogger(className).error("ERR: " + e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,20 @@ public static void main(String[] args) throws ConnectionException, EntityConfigu

Cluster cluster = tmsAgentService.readTopology();

// trigger stats computation on server-side
// TRIGGER SERVER-SIDE STATS COMPUTATION

// 1. find the tms entity (management)
ServerEntity serverEntity = cluster
.activeServerEntityStream()
.filter(e -> e.getType().equals(TmsAgentConfig.ENTITY_TYPE))
.findFirst()
.get();

// 2. create a routing context
Context context = serverEntity.getContext();

// 3. collect stats on 3 capabilities (pool, stores, offheap)
tmsAgentService.updateCollectedStatistics(context, "PoolStatistics", asList(
"Pool:AllocatedSize"
)).waitForReturn();

tmsAgentService.updateCollectedStatistics(context, "ServerStoreStatistics", asList(
"Store:AllocatedMemory",
"Store:DataAllocatedMemory",
Expand All @@ -83,25 +81,31 @@ public static void main(String[] args) throws ConnectionException, EntityConfigu
"Store:DataSize",
"Store:TableCapacity"
)).waitForReturn();

tmsAgentService.updateCollectedStatistics(context, "OffHeapResourceStatistics", asList(
"OffHeapResource:AllocatedMemory"
)).waitForReturn();

// trigger stats computation on client-side
tmsAgentService.updateCollectedStatistics(context, "ServerCacheStatistics", asList(
"Cluster:HitCount",
"Cluster:MissCount",
"Cluster:HitRatio",
"ServerCache:Size"))
.waitForReturn();

// TRIGGER CLIENT-SIDE STATS COMPUTATION

// 1. find ehcache clients in topology
cluster
.clientStream()
.filter(c -> c.getName().startsWith("Ehcache:"))
.forEach(ehcache -> {
.filter(c -> c.getName().equals("pet-clinic"))
.findFirst()
.ifPresent(ehcache -> {

// 2. create a routing context
Context ctx = ehcache.getContext()
.with("cacheManagerName", "my-super-cache-manager");
.with("appName", "pet-clinic");

try {
// 3. collect stats on client-side
tmsAgentService.updateCollectedStatistics(ctx, "StatisticsCapability", asList("Cache:HitCount", "Clustered:HitCount", "Cache:MissCount", "Clustered:MissCount")).waitForReturn();
tmsAgentService.updateCollectedStatistics(ctx, "CacheStatistics", asList("Cache:HitCount", "Cache:MissCount", "Cache:HitRatio", "ClientCache:Size")).waitForReturn();
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public static void main(String[] args) throws ConnectionException, EntityConfigu
ScheduledFuture<?> task = executorService.scheduleWithFixedDelay(() -> {
try {

// READ TOPOLOGY
Cluster cluster = service.readTopology();
System.out.println(mapper.writeValueAsString(cluster.toMap()));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright Terracotta, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.terracotta.management.doc;

import org.terracotta.connection.ConnectionException;
import org.terracotta.management.entity.sample.Cache;
import org.terracotta.management.entity.sample.client.CacheFactory;
import org.terracotta.management.registry.collect.StatisticConfiguration;

import java.net.URI;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
* @author Mathieu Carbou
*/
public class StartSampleEntity {
public static void main(String[] args) throws ConnectionException, ExecutionException, TimeoutException, InterruptedException {
StatisticConfiguration statisticConfiguration = new StatisticConfiguration()
.setAverageWindowDuration(1, TimeUnit.MINUTES)
.setHistorySize(100)
.setHistoryInterval(1, TimeUnit.SECONDS)
.setTimeToDisable(5, TimeUnit.SECONDS);
CacheFactory cacheFactory = new CacheFactory(URI.create("terracotta://localhost:9510/pet-clinic"), statisticConfiguration);

cacheFactory.init();

Cache pets = cacheFactory.getCache("pets");

while (true) {

String key = "pet-" + new Random().nextInt(100);
System.out.println("put(" + key + ")");
pets.put(key, "Garfield");

key = "pet-" + new Random().nextInt(100);
System.out.println("get(" + key + ")");
pets.get(key);

Thread.sleep(1_000);
}

}
}
43 changes: 43 additions & 0 deletions management/testing/doc/src/main/resources/tc-config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright Terracotta, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<tc-config xmlns="http://www.terracotta.org/config"
xmlns:ohr="http://www.terracotta.org/config/offheap-resource">

<plugins>
<config>
<ohr:offheap-resources>
<ohr:resource name="primary" unit="MB">64</ohr:resource>
</ohr:offheap-resources>
</config>
</plugins>

<servers>
<server host="localhost" name="server1">
<logs>./logs/server1</logs>
<tsa-port>9510</tsa-port>
<tsa-group-port>9530</tsa-group-port>
</server>
<server host="localhost" name="server2">
<logs>./logs/server2</logs>
<tsa-port>9511</tsa-port>
<tsa-group-port>9531</tsa-group-port>
</server>
</servers>

</tc-config>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TestName;
import org.terracotta.testing.rules.BasicExternalCluster;

import java.io.File;
Expand All @@ -41,8 +42,12 @@ public abstract class AbstractHATest extends AbstractTest {
public org.terracotta.testing.rules.Cluster voltron =
new BasicExternalCluster(new File("target/galvan"), 2, emptyList(), "", resourceConfig, "");

@Rule
public TestName testName = new TestName();

@Before
public void setUp() throws Exception {
System.out.println(" => [" + testName.getMethodName() + "] " + getClass().getSimpleName() + ".setUp()");
voltron.getClusterControl().waitForActive();
voltron.getClusterControl().waitForRunningPassivesInStandby();
commonSetUp(voltron);
Expand All @@ -51,6 +56,7 @@ public void setUp() throws Exception {

@After
public void tearDown() throws Exception {
System.out.println(" => [" + testName.getMethodName() + "] " + getClass().getSimpleName() + ".tearDown()");
commonTearDown();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright Terracotta, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.terracotta.management.integration.tests;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.terracotta.testing.rules.BasicExternalCluster;

import java.io.File;

import static java.util.Collections.emptyList;

/**
* @author Mathieu Carbou
*/
public abstract class AbstractSingleTest extends AbstractTest {

private final String offheapResource = "primary-server-resource";
private final String resourceConfig =
"<config xmlns:ohr='http://www.terracotta.org/config/offheap-resource'>"
+ "<ohr:offheap-resources>"
+ "<ohr:resource name=\"" + offheapResource + "\" unit=\"MB\">64</ohr:resource>"
+ "</ohr:offheap-resources>" +
"</config>\n";

@Rule
public org.terracotta.testing.rules.Cluster voltron =
new BasicExternalCluster(new File("target/galvan"), 1, emptyList(), "", resourceConfig, "");

@Before
public void setUp() throws Exception {
voltron.getClusterControl().waitForActive();
commonSetUp(voltron);
}

@After
public void tearDown() throws Exception {
commonTearDown();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.junit.Rule;
Expand Down Expand Up @@ -56,7 +57,7 @@
*/
public abstract class AbstractTest {

private final ObjectMapper mapper = new ObjectMapper();
private final ObjectMapper mapper = new ObjectMapper().configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);

private Connection managementConnection;
private Cluster cluster;
Expand Down Expand Up @@ -198,4 +199,25 @@ protected void queryAllRemoteStatsUntil(Predicate<List<? extends ContextualStati
assertTrue(test.test(statistics));
}

protected String removeRandomValues(String currentTopo) {
// removes all random values
return currentTopo
.replaceAll("\"(hostName)\":\"[^\"]*\"", "\"$1\":\"<hostname>\"")
.replaceAll("\"hostAddress\":[^,]*", "\"hostAddress\":\"127\\.0\\.0\\.1\"")
.replaceAll("\"bindPort\":[0-9]+", "\"bindPort\":0")
.replaceAll("\"groupPort\":[0-9]+", "\"groupPort\":0")
.replaceAll("\"port\":[0-9]+", "\"port\":0")
.replaceAll("\"activateTime\":[0-9]+", "\"activateTime\":0")
.replaceAll("\"time\":[0-9]+", "\"time\":0")
.replaceAll("\"startTime\":[0-9]+", "\"startTime\":0")
.replaceAll("\"upTimeSec\":[0-9]+", "\"upTimeSec\":0")
.replaceAll("\"id\":\"[0-9]+@[^:]*:([^:]*):[^\"]*\",\"pid\":[0-9]+", "\"id\":\"[email protected]:$1:<uuid>\",\"pid\":0")
.replaceAll("\"buildId\":\"[^\"]*\"", "\"buildId\":\"Build ID\"")
.replaceAll("\"version\":\"[^\"]*\"", "\"version\":\"<version>\"")
.replaceAll("\"clientId\":\"[0-9]+@[^:]*:([^:]*):[^\"]*\"", "\"clientId\":\"[email protected]:$1:<uuid>\"")
.replaceAll("\"logicalConnectionUid\":\"[^\"]*\"", "\"logicalConnectionUid\":\"<uuid>\"")
.replaceAll("\"id\":\"[^\"]*\",\"logicalConnectionUid\":\"[^\"]*\"", "\"id\":\"<uuid>:SINGLE:testServer0:127.0.0.1:0\",\"logicalConnectionUid\":\"<uuid>\"")
.replaceAll("\"vmId\":\"[^\"]*\"", "\"vmId\":\"[email protected]\"");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.terracotta.management.entity.sample;
package org.terracotta.management.integration.tests;

import org.junit.Test;
import org.terracotta.management.model.call.Parameter;
Expand All @@ -37,7 +37,7 @@
/**
* @author Mathieu Carbou
*/
public class ClientCacheRemoteManagementTest extends AbstractTest {
public class ClientCacheRemoteManagementIT extends AbstractSingleTest {

@Test
public void can_access_remote_management_registry_of_client() throws Exception {
Expand Down
Loading

0 comments on commit d29ac19

Please sign in to comment.