Skip to content

Commit

Permalink
Create JMX bean command handlers for system commands
Browse files Browse the repository at this point in the history
  • Loading branch information
amckenzie committed Jun 6, 2019
1 parent 2345b1c commit a18386a
Show file tree
Hide file tree
Showing 14 changed files with 191 additions and 298 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@
<artifactId>aggregate</artifactId>
<version>${framework.version}</version>
</dependency>
<dependency>
<groupId>uk.gov.justice.event-store</groupId>
<artifactId>event-store-management</artifactId>
<version>${event-store.version}</version>
</dependency>

<!-- Test Dependencies -->
<dependency>
Expand Down
19 changes: 19 additions & 0 deletions example-context/example-service/example-it/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,25 @@
<groupId>uk.gov.justice.event-store</groupId>
<artifactId>test-utils-event</artifactId>
<version>${event-store.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>uk.gov.justice.event-store</groupId>
<artifactId>subscription-manager</artifactId>
<version>${event-store.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>uk.gov.justice.event-store</groupId>
<artifactId>event-store-management</artifactId>
<version>${event-store.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>uk.gov.justice.services</groupId>
<artifactId>framework-management</artifactId>
<version>${framework.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,33 @@
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

import uk.gov.justice.services.event.buffer.core.repository.subscription.StreamStatusJdbcRepository;
import uk.gov.justice.services.eventsourcing.repository.jdbc.event.Event;
import uk.gov.justice.services.eventsourcing.repository.jdbc.event.EventJdbcRepository;
import uk.gov.justice.services.eventsourcing.repository.jdbc.event.EventRepositoryFactory;
import uk.gov.justice.services.eventsourcing.repository.jdbc.event.EventStreamJdbsRepositoryFactory;
import uk.gov.justice.services.eventsourcing.repository.jdbc.event.PublishedEventTableTruncator;
import uk.gov.justice.services.eventsourcing.repository.jdbc.eventstream.EventStreamJdbcRepository;
import uk.gov.justice.services.eventsourcing.repository.jdbc.exception.InvalidPositionException;
import uk.gov.justice.services.eventstore.management.catchup.commands.CatchupCommand;
import uk.gov.justice.services.example.cakeshop.it.helpers.CakeshopEventGenerator;
import uk.gov.justice.services.example.cakeshop.it.helpers.DatabaseManager;
import uk.gov.justice.services.example.cakeshop.it.helpers.MBeanHelper;
import uk.gov.justice.services.example.cakeshop.it.helpers.PositionInStreamIterator;
import uk.gov.justice.services.example.cakeshop.it.helpers.RecipeTableInspector;
import uk.gov.justice.services.example.cakeshop.it.helpers.RestEasyClientFactory;
import uk.gov.justice.services.example.cakeshop.it.helpers.StandaloneStreamStatusJdbcRepositoryFactory;
import uk.gov.justice.services.example.cakeshop.it.helpers.SystemCommandMBeanClient;
import uk.gov.justice.services.example.cakeshop.persistence.entity.Recipe;
import uk.gov.justice.services.jmx.Catchup;
import uk.gov.justice.services.jmx.CatchupMBean;
import uk.gov.justice.services.test.utils.core.messaging.Poller;
import uk.gov.justice.services.test.utils.persistence.DatabaseCleaner;

import java.sql.SQLException;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Stream;

import javax.management.JMX;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.sql.DataSource;
import javax.ws.rs.client.Client;

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

public class CakeShopReplayEventsIT {
Expand All @@ -49,30 +41,36 @@ public class CakeShopReplayEventsIT {
private final DataSource eventStoreDataSource = new DatabaseManager().initEventStoreDb();
private final DataSource viewStoreDataSource = new DatabaseManager().initViewStoreDb();
private final EventJdbcRepository eventJdbcRepository = new EventRepositoryFactory().getEventJdbcRepository(eventStoreDataSource);
private final PublishedEventTableTruncator publishedEventTableTruncator = new PublishedEventTableTruncator(eventStoreDataSource);

private final EventStreamJdbsRepositoryFactory eventStreamJdbcRepositoryFactory = new EventStreamJdbsRepositoryFactory();
private final EventStreamJdbcRepository eventStreamJdbcRepository = eventStreamJdbcRepositoryFactory.getEventStreamJdbcRepository(eventStoreDataSource);

private final StandaloneStreamStatusJdbcRepositoryFactory standaloneStreamStatusJdbcRepositoryFactory = new StandaloneStreamStatusJdbcRepositoryFactory();
private final StreamStatusJdbcRepository streamStatusJdbcRepository = standaloneStreamStatusJdbcRepositoryFactory.getStreamStatusJdbcRepository(viewStoreDataSource);

private final RecipeTableInspector recipeTableInspector = new RecipeTableInspector(viewStoreDataSource);

private final Poller poller = new Poller(60, 1000L);

private final SystemCommandMBeanClient systemCommandMBeanClient = new SystemCommandMBeanClient();
private final DatabaseCleaner databaseCleaner = new DatabaseCleaner();

private Client client;
private MBeanHelper mBeanHelper;

@Before
public void before() {
client = new RestEasyClientFactory().createResteasyClient();
mBeanHelper = new MBeanHelper();

final String contextName = "framework";

databaseCleaner.cleanEventStoreTables(contextName);
cleanViewstoreTables();

databaseCleaner.cleanStreamBufferTable(contextName);
databaseCleaner.cleanStreamStatusTable(contextName);
}

@After
public void cleanup() {
client.close();
systemCommandMBeanClient.close();
}

@Test
Expand All @@ -81,9 +79,6 @@ public void shouldReplayAndFindRecipesInViewStore() throws Exception {
final int numberOfStreams = 6;
final int numberOfEventsPerStream = 2;

truncateEventLog();
recipeTableInspector.truncateViewstoreTables();

addEventsToEventLog(numberOfStreams, numberOfEventsPerStream);

System.out.println("Inserted " + numberOfStreams * numberOfEventsPerStream + " events");
Expand All @@ -96,9 +91,9 @@ public void shouldReplayAndFindRecipesInViewStore() throws Exception {

final List<Recipe> originalRecipes = recipeTableInspector.getAllRecipes();

recipeTableInspector.truncateViewstoreTables();
cleanViewstoreTables();

runCatchup();
systemCommandMBeanClient.getMbeanProxy().runCommand(new CatchupCommand());

final Optional<Integer> numberOfReplayedRecipesOptional = checkExpectedNumberOfRecipes(numberOfStreams);

Expand Down Expand Up @@ -144,30 +139,6 @@ private void addEventsToEventLog(final int numberOfStreams, final int numberOfEv
}
}

private void truncateEventLog() throws SQLException {
final Stream<Event> eventStream = eventJdbcRepository.findAll();
eventStream.forEach(event -> eventJdbcRepository.clear(event.getStreamId()));

publishedEventTableTruncator.truncate();
}

private void runCatchup() throws Exception {

try (final JMXConnector jmxConnector = mBeanHelper.getJMXConnector()) {
final MBeanServerConnection connection = jmxConnector.getMBeanServerConnection();

final ObjectName objectName = new ObjectName("catchup", "type", Catchup.class.getSimpleName());

mBeanHelper.getMbeanDomains(connection);

mBeanHelper.getMbeanOperations(objectName, connection);

final CatchupMBean catchupMBean = JMX.newMBeanProxy(connection, objectName, CatchupMBean.class, true);

catchupMBean.doCatchupRequested();
}
}

private Optional<Integer> checkExpectedNumberOfRecipes(final int numberOfStreams) {
return poller.pollUntilFound(() -> {
final int numberOfRecipes = recipeTableInspector.countNumberOfRecipes();
Expand All @@ -179,4 +150,19 @@ private Optional<Integer> checkExpectedNumberOfRecipes(final int numberOfStreams
return empty();
});
}

private void cleanViewstoreTables() {

final String contextName = "framework";

databaseCleaner.cleanViewStoreTables(contextName,
"ingredient",
"recipe",
"cake",
"cake_order",
"processed_event",
"shuttered_command_store",
"stream_status"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static java.util.Optional.empty;
import static java.util.Optional.of;
import static javax.management.JMX.newMBeanProxy;
import static org.junit.Assert.fail;

import uk.gov.justice.services.eventsourcing.repository.jdbc.event.Event;
Expand All @@ -12,27 +11,22 @@
import uk.gov.justice.services.eventsourcing.repository.jdbc.event.PublishedEventTableTruncator;
import uk.gov.justice.services.eventsourcing.repository.jdbc.eventstream.EventStreamJdbcRepository;
import uk.gov.justice.services.eventsourcing.repository.jdbc.exception.InvalidPositionException;
import uk.gov.justice.services.eventstore.management.catchup.commands.CatchupCommand;
import uk.gov.justice.services.example.cakeshop.it.helpers.CakeshopEventGenerator;
import uk.gov.justice.services.example.cakeshop.it.helpers.DatabaseManager;
import uk.gov.justice.services.example.cakeshop.it.helpers.MBeanHelper;
import uk.gov.justice.services.example.cakeshop.it.helpers.PositionInStreamIterator;
import uk.gov.justice.services.example.cakeshop.it.helpers.PublishedEventCounter;
import uk.gov.justice.services.example.cakeshop.it.helpers.RecipeTableInspector;
import uk.gov.justice.services.example.cakeshop.it.helpers.RestEasyClientFactory;
import uk.gov.justice.services.jmx.Catchup;
import uk.gov.justice.services.jmx.CatchupMBean;
import uk.gov.justice.services.example.cakeshop.it.helpers.SystemCommandMBeanClient;
import uk.gov.justice.services.test.utils.core.messaging.Poller;
import uk.gov.justice.services.test.utils.persistence.DatabaseCleaner;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Stream;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.sql.DataSource;
import javax.ws.rs.client.Client;

Expand All @@ -53,20 +47,27 @@ public class CatchupPerformanceIT {
private final RecipeTableInspector recipeTableInspector = new RecipeTableInspector(viewStoreDataSource);
private final PublishedEventCounter publishedEventCounter = new PublishedEventCounter(eventStoreDataSource);

private final SystemCommandMBeanClient systemCommandMBeanClient = new SystemCommandMBeanClient();
private final DatabaseCleaner databaseCleaner = new DatabaseCleaner();

private final Poller longPoller = new Poller(1200, 1000L);

private Client client;
private MBeanHelper mBeanHelper;

@Before
public void before() {
client = new RestEasyClientFactory().createResteasyClient();
mBeanHelper = new MBeanHelper();

final String contextName = "framework";

databaseCleaner.cleanEventStoreTables(contextName);
cleanViewstoreTables();
}

@After
public void cleanup() {
client.close();
systemCommandMBeanClient.close();
}

@Test
Expand All @@ -77,9 +78,6 @@ public void shouldReplayAndFindRecipesInViewStore() throws Exception {
final int totalEvents = numberOfStreams * numberOfEventsPerStream;
final String componentName = "EVENT_LISTENER";

truncateEventLog();
recipeTableInspector.truncateViewstoreTables();

final List<UUID> streamIds = addEventsToEventLog(numberOfStreams, numberOfEventsPerStream);

final Optional<Integer> numberOfEvents = longPoller.pollUntilFound(() -> {
Expand Down Expand Up @@ -109,11 +107,11 @@ public void shouldReplayAndFindRecipesInViewStore() throws Exception {
});

if (!eventCount.isPresent()) {
fail();
fail("Expected " + numberOfEventsPerStream + " events but found " + recipeTableInspector.countEventsPerStream(streamId, componentName) + " in stream " + streamId);
}
}

recipeTableInspector.truncateViewstoreTables();
cleanViewstoreTables();

runCatchup();

Expand Down Expand Up @@ -170,28 +168,10 @@ private List<UUID> addEventsToEventLog(final int numberOfStreams, final int numb
return streamIds;
}

private void truncateEventLog() throws SQLException {
final Stream<Event> eventStream = eventJdbcRepository.findAll();
eventStream.forEach(event -> eventJdbcRepository.clear(event.getStreamId()));

publishedEventTableTruncator.truncate();
}

private void runCatchup() throws Exception {

try (final JMXConnector jmxConnector = mBeanHelper.getJMXConnector()) {
final MBeanServerConnection connection = jmxConnector.getMBeanServerConnection();

final ObjectName objectName = new ObjectName("catchup", "type", Catchup.class.getSimpleName());

mBeanHelper.getMbeanDomains(connection);
systemCommandMBeanClient.getMbeanProxy().runCommand(new CatchupCommand());

mBeanHelper.getMbeanOperations(objectName, connection);

final CatchupMBean catchupMBean = newMBeanProxy(connection, objectName, CatchupMBean.class, true);

catchupMBean.doCatchupRequested();
}
}

private Optional<Integer> checkExpectedNumberOfRecipes(final int numberOfStreams) {
Expand All @@ -205,4 +185,22 @@ private Optional<Integer> checkExpectedNumberOfRecipes(final int numberOfStreams
return empty();
});
}

private void cleanViewstoreTables() {

final String contextName = "framework";

databaseCleaner.cleanViewStoreTables(contextName,
"ingredient",
"recipe",
"cake",
"cake_order",
"processed_event",
"shuttered_command_store"
);

databaseCleaner.cleanStreamBufferTable(contextName);
databaseCleaner.cleanStreamStatusTable(contextName);
}

}
Loading

0 comments on commit a18386a

Please sign in to comment.