Skip to content

Commit

Permalink
added MySQL and MariaDB (#4)
Browse files Browse the repository at this point in the history
* added mariadb

* Update build.yml

* added mysql

* ping will always start a transaction

* update to readme
  • Loading branch information
sterlp authored Jan 1, 2025
1 parent 8fac8b8 commit d64af08
Show file tree
Hide file tree
Showing 22 changed files with 170 additions and 99 deletions.
28 changes: 26 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Java CI with Maven

on:
push:
branches: "*"
branches: "main"
pull_request:
branches: "*"

Expand Down Expand Up @@ -74,6 +74,30 @@ jobs:
- name: Run tests with postgres
run: mvn test -pl core -am -Dspring.profiles.active=postgres

test-mariadb:
runs-on: ubuntu-latest
needs: build
services:
postgres:
image: mariadb:latest
env:
MYSQL_USER: sa
MYSQL_ROOT_PASSWORD: veryStrong123
MYSQL_PASSWORD: veryStrong123
MYSQL_DATABASE: testdb
ports:
- 3306:3306
steps:
- uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'
cache: maven
- name: Run tests with mariadb
run: mvn test -pl core -am -Dspring.profiles.active=mariadb

java-doc:
runs-on: ubuntu-latest
needs: [build]
Expand Down Expand Up @@ -105,7 +129,7 @@ jobs:

maven-deploy:
runs-on: ubuntu-latest
needs: [build, test-mssql, test-postgres]
needs: [build, test-mssql, test-postgres, test-mariadb]
if: ${{ github.ref == 'refs/heads/main' }}
steps:
- uses: actions/checkout@v4
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## v1.3.0 - (2025-01-01)

- MariaDB support
- PostgreSQL support

## v1.2.0 - (2024-12-31)

- Run now button in the UI
Expand Down
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@ Secondary goal is to support [Poor mans Workflow](https://github.com/sterlp/pmw)

## Tested in the pipeline

- H2
- azure-sql-edge (MSSQL)
- H2
- azure-sql-edge (MSSQL)
- PostgreSQL
- MariaDB

![History](screenshots/supported-dbs.png)

## Supported in theory

- MSSQL
- PostgreSQL
- mySQL
- MariaDB
- MSSQL, as azure-sql-edge is tested

## Not supported

- mySQL: sequences are not supported

# Setup and Run a Task

Expand Down
12 changes: 10 additions & 2 deletions RUN_AND_BUILD.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
mvn versions:display-dependency-updates
mvn versions:set -DnewVersion=1.2.1 -DgenerateBackupPoms=false
mvn versions:set -DnewVersion=1.2.2-SNAPSHOT -DgenerateBackupPoms=false
mvn versions:set -DnewVersion=1.3.0 -DgenerateBackupPoms=false
mvn versions:set -DnewVersion=1.3.1-SNAPSHOT -DgenerateBackupPoms=false

## postgres
docker run --name pg-container -e POSTGRES_USER=sa -e POSTGRES_PASSWORD=veryStrong123 -p 5432:5432 -d postgres

## azure-sql-edge
docker run --cap-add SYS_PTRACE -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=veryStrong123' -p 1433:1433 --name azuresqledge -d mcr.microsoft.com/azure-sql-edge

## MariaDB
docker run -e MYSQL_ROOT_PASSWORD=veryStrong123 -e MYSQL_DATABASE=testdb -e MYSQL_USER=sa -e MYSQL_PASSWORD=veryStrong123 -p 3306:3306 -d mariadb:latest

## MySQL
docker run -e MYSQL_ROOT_PASSWORD=veryStrong123 -e MYSQL_DATABASE=testdb -e MYSQL_USER=sa -e MYSQL_PASSWORD=veryStrong123 -p 3306:3306 -d mysql
12 changes: 11 additions & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.sterl.spring</groupId>
<artifactId>spring-persistent-tasks-root</artifactId>
<version>1.2.2-SNAPSHOT</version>
<version>1.3.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -89,6 +89,16 @@
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
* Just a copy of the trigger status but without any data/state.
*/
@Entity
@Table(name = "PT_TRIGGER_HISTORY_DETAILS", indexes = {
@Index(name = "IDX_PT_TRIGGERS_HISTORY_INSTANCE_ID", columnList = "instance_id"),
@Index(name = "IDX_PT_TRIGGERS_HISTORY_TASK_NAME", columnList = "task_name"),
@Index(name = "IDX_PT_TRIGGERS_HISTORY_TRIGGER_ID", columnList = "trigger_id"),
@Index(name = "IDX_PT_TRIGGERS_HISTORY_STATUS", columnList = "status"),
@Index(name = "IDX_PT_TRIGGERS_HISTORY_CREATED_TIME", columnList = "created_time"),
@Table(name = "pt_trigger_history_details", indexes = {
@Index(name = "idx_pt_triggers_history_instance_id", columnList = "instance_id"),
@Index(name = "idx_pt_triggers_history_task_name", columnList = "task_name"),
@Index(name = "idx_pt_triggers_history_trigger_id", columnList = "trigger_id"),
@Index(name = "idx_pt_triggers_history_status", columnList = "status"),
@Index(name = "idx_pt_triggers_history_created_time", columnList = "created_time"),
})
@Data
@NoArgsConstructor
Expand All @@ -39,7 +39,7 @@
@EqualsAndHashCode(of = "id")
public class TriggerHistoryDetailEntity implements HasTriggerData {

@GeneratedValue(generator = "SEQ_PT_TRIGGER_HISTORY_DETAILS", strategy = GenerationType.SEQUENCE)
@GeneratedValue(generator = "seq_pt_trigger_history_details", strategy = GenerationType.SEQUENCE)
@Column(updatable = false)
@Id
private Long id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
import lombok.NoArgsConstructor;

@Entity
@Table(name = "PT_TRIGGER_HISTORY_LAST_STATES", indexes = {
@Index(name = "IDX_PT_TRIGGER_HISTORY_LAST_STATES_TASK_NAME", columnList = "task_name"),
@Index(name = "IDX_PT_TRIGGER_HISTORY_LAST_STATES_TRIGGER_ID", columnList = "trigger_id"),
@Index(name = "IDX_PT_TRIGGER_HISTORY_LAST_STATES_STATUS", columnList = "status"),
@Index(name = "IDX_PT_TRIGGER_HISTORY_LAST_STATES_CREATED_TIME", columnList = "created_time"),
@Table(name = "pt_trigger_history_last_states", indexes = {
@Index(name = "idx_pt_trigger_history_last_states_task_name", columnList = "task_name"),
@Index(name = "idx_pt_trigger_history_last_states_trigger_id", columnList = "trigger_id"),
@Index(name = "idx_pt_trigger_history_last_states_status", columnList = "status"),
@Index(name = "idx_pt_trigger_history_last_states_created_time", columnList = "created_time"),
})
@Data
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,14 @@ public void shutdownNow() {
}

public SchedulerEntity pingRegistry() {
var result = editSchedulerStatus.checkinToRegistry(name);
result.setRunnungTasks(taskExecutor.getRunningTasks());
result.setTasksSlotCount(taskExecutor.getMaxThreads());
log.debug("Ping {}", result);
return result;
// using trx template to ensure the TRX is started if we use this method internally
return trx.execute(t -> {
var result = editSchedulerStatus.checkinToRegistry(name);
result.setRunnungTasks(taskExecutor.getRunningTasks());
result.setTasksSlotCount(taskExecutor.getMaxThreads());
log.debug("Ping {}", result);
return result;
});
}

public SchedulerEntity getScheduler() {
Expand All @@ -100,21 +103,16 @@ public List<Future<TriggerKey>> triggerNextTasks() {
*/
@NonNull
public List<Future<TriggerKey>> triggerNextTasks(OffsetDateTime timeDue) {
var triggers = trx.execute(t -> {
List<TriggerEntity> result;
// in any case we say hello
final var runningOn = pingRegistry();
if (taskExecutor.getFreeThreads() > 0) {
result = triggerService.lockNextTrigger(
name, taskExecutor.getFreeThreads(), timeDue);
runningOn.setRunnungTasks(taskExecutor.getRunningTasks() + result.size());
} else {
result = Collections.emptyList();
log.debug("triggerNextTasks({}) skipped as no free threads are available.", timeDue);
}
return result;
});
return taskExecutor.submit(triggers);
List<TriggerEntity> triggers;
if (taskExecutor.getFreeThreads() > 0) {
triggers = triggerService.lockNextTrigger(
name, taskExecutor.getFreeThreads(), timeDue);
} else {
triggers = Collections.emptyList();
}
var result = taskExecutor.submit(triggers);
pingRegistry();
return result;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import lombok.ToString;

@Entity
@Table(name = "PT_SCHEDULER", indexes = @Index(name = "IDX_TASK_SCHEDULER_STATUS", columnList = "last_ping"))
@Table(name = "pt_scheduler", indexes = @Index(name = "idx_task_scheduler_status", columnList = "last_ping"))
@Data
@ToString(of = { "id", "lastPing", "runnungTasks", "tasksSlotCount" })
@EqualsAndHashCode(of = "id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
import lombok.NoArgsConstructor;

@Entity
@Table(name = "PT_TASK_TRIGGERS", indexes = {
@Index(name = "UNQ_PT_TRIGGERS_KEY", columnList = "trigger_id, task_name", unique = true),
@Index(name = "IDX_PT_TRIGGERS_PRIORITY", columnList = "priority"),
@Index(name = "IDX_PT_TRIGGERS_RUN_AT", columnList = "run_at"),
@Index(name = "IDX_PT_TRIGGERS_STATUS", columnList = "status"),
@Index(name = "IDX_PT_TRIGGERS_PING", columnList = "last_ping"),
@Table(name = "pt_task_triggers", indexes = {
@Index(name = "unq_pt_triggers_key", columnList = "trigger_id, task_name", unique = true),
@Index(name = "idx_pt_triggers_priority", columnList = "priority"),
@Index(name = "idx_pt_triggers_run_at", columnList = "run_at"),
@Index(name = "idx_pt_triggers_status", columnList = "status"),
@Index(name = "idx_pt_triggers_ping", columnList = "last_ping"),
})
@Data
@NoArgsConstructor
Expand All @@ -39,7 +39,7 @@
@Builder
public class TriggerEntity implements HasTriggerData {

@GeneratedValue(generator = "SEQ_PT_TASK_TRIGGERS", strategy = GenerationType.SEQUENCE)
@GeneratedValue(generator = "seq_pt_task_triggers", strategy = GenerationType.SEQUENCE)
@Column(updatable = false)
@Id
private Long id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import uk.co.jemos.podam.api.PodamFactory;
import uk.co.jemos.podam.api.PodamFactoryImpl;

//@ActiveProfiles("postgres") // postgres mssql
// @ActiveProfiles("mssql") // postgres mssql mariadb mysql
@SpringBootTest(classes = SampleApp.class, webEnvironment = WebEnvironment.RANDOM_PORT)
public class AbstractSpringTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,7 @@ void testLockTriggerInSchedulers() throws Exception {
lockInvocations.add(() -> runNextTrigger());
}

while (triggerService.hasPendingTriggers()) {
schedulerService.triggerNextTasks();
schedulerB.triggerNextTasks();
Thread.sleep(10);
}
persistentTaskService.executeTriggersAndWait();

// THEN
for (int i = 1; i <= 100; ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void testWillTriggerOnlyFreeThreadSize() throws Exception {
for (int i = 0; i < 15; i++) {
triggerService.queue(TaskTriggerBuilder
.newTrigger("slowTask")
.state(50L)
.state(200L)
.build()
);
}
Expand All @@ -53,9 +53,9 @@ void testWillTriggerOnlyFreeThreadSize() throws Exception {
subject.triggerNextTasks();

// THEN
Thread.sleep(15);
assertThat(triggerService.countTriggers(TriggerStatus.RUNNING)).isEqualTo(10);
assertThat(triggerService.countTriggers(TriggerStatus.WAITING)).isEqualTo(5);

// AND
final SchedulerEntity scheduler = subject.getScheduler();
assertThat(scheduler.getRunnungTasks()).isEqualTo(10);
Expand Down
11 changes: 11 additions & 0 deletions core/src/test/resources/application-mariadb.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
spring:
datasource:
username: root
password: veryStrong123
url: jdbc:mariadb://localhost:3306/testdb
driver-class-name: org.mariadb.jdbc.Driver
hikari:
maximum-pool-size: 100
jpa:
hibernate:
ddl-auto: none
14 changes: 14 additions & 0 deletions core/src/test/resources/application-mysql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
spring:
datasource:
username: root
password: veryStrong123
url: jdbc:mysql://localhost:3306/testdb
driver-class-name: com.mysql.jdbc.Driver
hikari:
maximum-pool-size: 100
jpa:
properties:
hibernate:
dialect: org.hibernate.dialect.MySQLDialect
hibernate:
ddl-auto: create
4 changes: 2 additions & 2 deletions core/src/test/resources/db/changelog/db.changelog-master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">

<changeSet author="sterlp" id="2024-12-27-initial-test-db">
<createTable tableName="PERSONBE">
<createTable tableName="personbe">
<column computed="false" name="id" type="bigint">
<constraints nullable="false" primaryKey="true"
primaryKeyName="PK_PERSONBE_ID" />
primaryKeyName="pk_personbe_id" />
</column>
<column computed="false" name="name" type="varchar(255)" />
</createTable>
Expand Down
2 changes: 1 addition & 1 deletion db/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.sterl.spring</groupId>
<artifactId>spring-persistent-tasks-root</artifactId>
<version>1.2.2-SNAPSHOT</version>
<version>1.3.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Loading

0 comments on commit d64af08

Please sign in to comment.