Skip to content

Commit

Permalink
Merge pull request #216 from CJSCommonPlatform/cherry-pick-2.4.x
Browse files Browse the repository at this point in the history
Cherry pick 2.4.x
  • Loading branch information
mapingo committed Jan 30, 2020
2 parents ae5385e + 77d6a5a commit 2159bb4
Show file tree
Hide file tree
Showing 72 changed files with 817 additions and 621 deletions.
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to
- Updated framework-api to 4.2.1
- Database cleaner updated to handle the latest system database tables

## [2.4.8] - 2020-01-29
### Changed
- Inserts into the event-buffer no longer fails if there is a conflict; it just logs a warning

## [2.4.7] - 2020-01-24
### Changed
- Event store now works with multiple event sources
- Event store now compatible with contexts that do not have a command pillar
- Extracted all command pillar SystemCommands into their own module

## [2.4.6] - 2020-01-21
### Added
- Catchup for multiple components now run in order of component and subscription priority
- Added event source name to catchup logger output
### Fixed
- Fixed catchup error where catchup was marked as complete after all subscriptions rather than all components

## [2.4.5] - 2020-01-06
### Removed
- Remove mechanism to also drop/add trigger on SUSPEND/UNSUSPEND as it causes
many strange ejb database errors

## [2.4.4] - 2020-01-06
### Added
- Added mechanism to also drop/add trigger to event_log table on SUSPEND/UNSUSPEND commands
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import javax.inject.Inject;
import javax.sql.DataSource;

import org.slf4j.Logger;

@ApplicationScoped
public class EventBufferJdbcRepository {

private static final String INSERT = "INSERT INTO stream_buffer (stream_id, position, event, source, component) VALUES (?, ?, ?, ?, ?)";
private static final String INSERT = "INSERT INTO stream_buffer (stream_id, position, event, source, component) VALUES (?, ?, ?, ?, ?) ON CONFLICT DO NOTHING";
private static final String SELECT_STREAM_BUFFER_BY_STREAM_ID_SOURCE_AND_COMPONENT = "SELECT stream_id, position, event, source, component FROM stream_buffer WHERE stream_id=? AND source=? AND component=? ORDER BY position";
private static final String DELETE_BY_STREAM_ID_POSITION = "DELETE FROM stream_buffer WHERE stream_id=? AND position=? AND source=? AND component=?";

Expand All @@ -41,16 +43,21 @@ public class EventBufferJdbcRepository {
@Inject
private ViewStoreJdbcDataSourceProvider dataSourceProvider;

@Inject
private Logger logger;

private DataSource dataSource;

public EventBufferJdbcRepository() {}

public EventBufferJdbcRepository(final JdbcResultSetStreamer jdbcResultSetStreamer,
final PreparedStatementWrapperFactory preparedStatementWrapperFactory,
final DataSource dataSource) {
final DataSource dataSource,
final Logger logger) {
this.jdbcResultSetStreamer = jdbcResultSetStreamer;
this.dataSource = dataSource;
this.preparedStatementWrapperFactory = preparedStatementWrapperFactory;
this.logger = logger;
}


Expand All @@ -67,7 +74,11 @@ public void insert(final EventBufferEvent bufferedEvent) {
ps.setString(3, bufferedEvent.getEvent());
ps.setString(4, bufferedEvent.getSource());
ps.setString(5, bufferedEvent.getComponent());
ps.executeUpdate();
final int rowsUpdated = ps.executeUpdate();
if (rowsUpdated == 0){
logger.warn("Event already present in event buffer. Ignoring");
}

} catch (SQLException e) {
throw new JdbcRepositoryException(format("Exception while storing event in the buffer: %s", bufferedEvent), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;

import uk.gov.justice.services.jdbc.persistence.JdbcResultSetStreamer;
import uk.gov.justice.services.jdbc.persistence.PreparedStatementWrapperFactory;
Expand All @@ -20,6 +21,7 @@

import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;

public class EventBufferJdbcRepositoryIT {

Expand All @@ -32,7 +34,8 @@ public void initDatabase() throws Exception {
eventBufferJdbcRepository = new EventBufferJdbcRepository(
new JdbcResultSetStreamer(),
new PreparedStatementWrapperFactory(),
dataSource);
dataSource,
mock(Logger.class));

new DatabaseCleaner().cleanViewStoreTables("framework", "stream_buffer", "stream_status");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;

import uk.gov.justice.services.jdbc.persistence.JdbcRepositoryException;
Expand All @@ -29,11 +30,12 @@
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import org.slf4j.Logger;

@RunWith(MockitoJUnitRunner.class)
public class EventBufferJdbcRepositoryTest {
private static final String SELECT_STREAM_BUFFER_BY_STREAM_ID_SOURCE_AND_COMPONENT = "SELECT stream_id, position, event, source, component FROM stream_buffer WHERE stream_id=? AND source=? AND component=? ORDER BY position";
private static final String INSERT = "INSERT INTO stream_buffer (stream_id, position, event, source, component) VALUES (?, ?, ?, ?, ?)";
private static final String INSERT = "INSERT INTO stream_buffer (stream_id, position, event, source, component) VALUES (?, ?, ?, ?, ?) ON CONFLICT DO NOTHING";
private static final String DELETE_BY_STREAM_ID_POSITION = "DELETE FROM stream_buffer WHERE stream_id=? AND position=? AND source=? AND component=?";

@Spy
Expand All @@ -57,6 +59,9 @@ public class EventBufferJdbcRepositoryTest {
@Mock
private JdbcResultSetStreamer jdbcResultSetStreamer;

@Mock
private Logger logger;

@InjectMocks
private EventBufferJdbcRepository eventBufferJdbcRepository;

Expand All @@ -72,8 +77,29 @@ public void initDatabase() throws Exception {
public void shouldInsertEvent() throws SQLException {
final String source = "source";

when(connection.prepareStatement(INSERT))
.thenReturn(preparedStatement);
when(connection.prepareStatement(INSERT)).thenReturn(preparedStatement);
when(preparedStatement.executeUpdate()).thenReturn(1);

final UUID streamId = randomUUID();
final long position = 1l;
eventBufferJdbcRepository.insert(new EventBufferEvent(streamId, position, "eventVersion_2", source, EVENT_LISTENER));

verify(preparedStatement).setObject(1, streamId);
verify(preparedStatement).setLong(2, position);
verify(preparedStatement).setString(3, "eventVersion_2");
verify(preparedStatement).setString(4, source);
verify(preparedStatement).setString(5, EVENT_LISTENER);
verify(preparedStatement).executeUpdate();
verifyZeroInteractions(logger);
}

@Test
public void shouldWarnIfInsertDoesNothing() throws SQLException {
final String source = "source";


when(connection.prepareStatement(INSERT)).thenReturn(preparedStatement);
when(preparedStatement.executeUpdate()).thenReturn(0);

final UUID streamId = randomUUID();
final long position = 1l;
Expand All @@ -85,6 +111,7 @@ public void shouldInsertEvent() throws SQLException {
verify(preparedStatement).setString(4, source);
verify(preparedStatement).setString(5, EVENT_LISTENER);
verify(preparedStatement).executeUpdate();
verify(logger).warn("Event already present in event buffer. Ignoring");
}

@Test(expected = JdbcRepositoryException.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>event-store-management</artifactId>
<groupId>uk.gov.justice.event-store</groupId>
<version>2.5.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>event-store-management-command-handler-extension</artifactId>

<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>uk.gov.justice.services</groupId>
<artifactId>framework-management</artifactId>
<version>${framework.version}</version>
</dependency>
<dependency>
<groupId>uk.gov.justice.event-store</groupId>
<artifactId>event-store-management-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>uk.gov.justice.event-store</groupId>
<artifactId>event-store-util</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>uk.gov.justice.event-store</groupId>
<artifactId>event-publisher-timer</artifactId>
<version>${project.version}</version>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package uk.gov.justice.services.eventstore.management.shuttering.observers;
package uk.gov.justice.services.eventstore.management.extension.suspension;

import static java.lang.String.format;
import static uk.gov.justice.services.management.suspension.api.SuspensionResult.suspensionFailed;
import static uk.gov.justice.services.management.suspension.api.SuspensionResult.suspensionSucceeded;

import uk.gov.justice.services.eventsourcing.util.jee.timer.StopWatchFactory;
import uk.gov.justice.services.eventstore.management.shuttering.process.CommandHandlerQueueInterrogator;
import uk.gov.justice.services.management.suspension.api.Suspendable;
import uk.gov.justice.services.management.suspension.api.SuspensionResult;
import uk.gov.justice.services.management.suspension.commands.SuspensionCommand;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uk.gov.justice.services.eventstore.management.shuttering.process;
package uk.gov.justice.services.eventstore.management.extension.suspension;

import uk.gov.justice.services.common.polling.MultiIteratingPoller;
import uk.gov.justice.services.common.polling.MultiIteratingPollerFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uk.gov.justice.services.eventstore.management.shuttering.process;
package uk.gov.justice.services.eventstore.management.extension.suspension;

import uk.gov.justice.services.messaging.jms.JmsCommandHandlerDestinationNameProvider;
import uk.gov.justice.services.messaging.jms.JmsQueueBrowser;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!-- Marker file indicating CDI should be enabled -->

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="
http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" bean-discovery-mode="all">
</beans>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.gov.justice.services.eventstore.management.shuttering.observers;
package uk.gov.justice.services.eventstore.management.extension.suspension;

import static java.util.Optional.empty;
import static java.util.UUID.randomUUID;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.inOrder;
Expand All @@ -10,7 +11,6 @@
import static uk.gov.justice.services.jmx.api.domain.CommandState.COMMAND_FAILED;

import uk.gov.justice.services.eventsourcing.util.jee.timer.StopWatchFactory;
import uk.gov.justice.services.eventstore.management.shuttering.process.CommandHandlerQueueInterrogator;
import uk.gov.justice.services.management.suspension.api.SuspensionResult;
import uk.gov.justice.services.management.suspension.commands.SuspensionCommand;

Expand Down Expand Up @@ -50,7 +50,7 @@ public void shouldSuspendButNotUnsuspend() throws Exception {
@Test
public void shouldWaitForCommandHandlerQueueToDrainAndReturnSuccess() throws Exception {

final UUID commandId = UUID.randomUUID();
final UUID commandId = randomUUID();
final StopWatch stopWatch = mock(StopWatch.class);
final SuspensionCommand suspensionCommand = mock(SuspensionCommand.class);

Expand Down Expand Up @@ -79,7 +79,7 @@ public void shouldWaitForCommandHandlerQueueToDrainAndReturnSuccess() throws Exc
@Test
public void shouldReturnFailureIfQueueDoesNotDrainInTime() throws Exception {

final UUID commandId = UUID.randomUUID();
final UUID commandId = randomUUID();
final StopWatch stopWatch = mock(StopWatch.class);
final SuspensionCommand applicationShutteringCommand = mock(SuspensionCommand.class);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uk.gov.justice.services.eventstore.management.shuttering.process;
package uk.gov.justice.services.eventstore.management.extension.suspension;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uk.gov.justice.services.eventstore.management.shuttering.process;
package uk.gov.justice.services.eventstore.management.extension.suspension;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down
5 changes: 0 additions & 5 deletions event-store-management/event-store-management-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@
<artifactId>event-store-management-events</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>uk.gov.justice.event-store</groupId>
<artifactId>event-publisher-timer</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>uk.gov.justice.event-store</groupId>
<artifactId>event-tracking-service</artifactId>
Expand Down
Loading

0 comments on commit 2159bb4

Please sign in to comment.