Skip to content

Commit

Permalink
Terracotta-OSS#191: Failover Galvan tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed Dec 19, 2016
1 parent 31275e3 commit 8edc6ea
Show file tree
Hide file tree
Showing 14 changed files with 804 additions and 229 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,44 @@
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
Expand Up @@ -32,7 +32,8 @@
/**
* @author Mathieu Carbou
*/
@Ignore // TODO activate
@Ignore("Impacted by https://github.com/Terracotta-OSS/terracotta-core/issues/405")
//TODO: VOLTRON ISSUE ? https://github.com/Terracotta-OSS/terracotta-core/issues/405
public class FailoverIT extends AbstractHATest {

@Test
Expand All @@ -47,16 +48,22 @@ public void failover_management() throws Exception {
tmsAgentService.readMessages();

// kill active - passive should take the active role
System.out.printf("==> terminateActive()");
voltron.getClusterControl().terminateActive();
System.out.printf("==> waitForActive()");
voltron.getClusterControl().waitForActive();

System.out.printf("==> readTopology()");
cluster = tmsAgentService.readTopology();
Server newActive = cluster.serverStream().filter(Server::isActive).findFirst().get();
assertThat(newActive.getState(), equalTo(Server.State.ACTIVE));
assertThat(newActive.getServerName(), equalTo(passive.getServerName()));

// read messages
List<Message> messages = tmsAgentService.readMessages();

messages.forEach(System.out::println);

assertThat(messages.size(), equalTo(3));

List<ContextualNotification> notifs = messages.stream()
Expand All @@ -79,4 +86,15 @@ public void failover_management() throws Exception {
//- check notification that might be there: CLIENT_RECONNECTED and SERVER_ENTITY_FAILOVER_COMPLETE
}

@Test
public void puts_can_be_seen_on_other_clients_after_failover() throws Exception {
put(0, "clients", "client1", "Mathieu");

// kill active - passive should take the active role
voltron.getClusterControl().terminateActive();
voltron.getClusterControl().waitForActive();

assertThat(get(1, "clients", "client1"), equalTo("Mathieu"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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.Test;
import org.terracotta.management.model.cluster.Server;
import org.terracotta.management.model.message.Message;
import org.terracotta.management.model.notification.ContextualNotification;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

/**
* @author Mathieu Carbou
*/
public class PassiveLeaveIT extends AbstractHATest {

@Test
public void get_notifications_when_passive_leaves() throws Exception {
Server active = tmsAgentService.readTopology().serverStream().filter(Server::isActive).findFirst().get();
Server passive = tmsAgentService.readTopology().serverStream().filter(server -> !server.isActive()).findFirst().get();
assertThat(active.getState(), equalTo(Server.State.ACTIVE));
assertThat(passive.getState(), equalTo(Server.State.PASSIVE));

// clear notification buffer
tmsAgentService.readMessages();

// remove one passive
voltron.getClusterControl().terminateOnePassive();

// wait for SERVER_LEFT message
List<Message> messages;
do {
messages = tmsAgentService.readMessages();
}
while (messages.isEmpty() && !Thread.currentThread().isInterrupted());

assertThat(messages.stream()
.filter(message -> message.getType().equals("NOTIFICATION"))
.flatMap(message -> message.unwrap(ContextualNotification.class).stream())
.map(ContextualNotification::getType)
.collect(Collectors.toList()),
equalTo(Arrays.asList("SERVER_LEFT")));

assertThat(messages.stream()
.filter(message -> message.getType().equals("NOTIFICATION"))
.flatMap(message -> message.unwrap(ContextualNotification.class).stream())
.map(contextualNotification -> contextualNotification.getContext().get(Server.NAME_KEY))
.collect(Collectors.toList()),
equalTo(Arrays.asList(passive.getServerName())));
}

}
Loading

0 comments on commit 8edc6ea

Please sign in to comment.