Skip to content

Commit

Permalink
Merge pull request #109 from kagkarlsson/fix_autocommit
Browse files Browse the repository at this point in the history
Commit explicitly if auto-commit=false. Add compatibility test for such data-source.
  • Loading branch information
kagkarlsson authored May 23, 2020
2 parents 559054e + 81e9120 commit 3edc901
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 11 deletions.
7 changes: 6 additions & 1 deletion db-scheduler/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<hsqldb.version>2.3.3</hsqldb.version>
<java8-matchers.version>1.6</java8-matchers.version>
<equals-verifier.version>3.1.12</equals-verifier.version>
<micro-jdbc.version>0.1</micro-jdbc.version>
<micro-jdbc.version>0.2</micro-jdbc.version>
<otj-pg-embedded.version>0.11.4</otj-pg-embedded.version>
<postgresql.version>9.4.1207</postgresql.version>
<slf4j.version>1.7.7</slf4j.version>
Expand Down Expand Up @@ -110,6 +110,11 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mysql</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class SchedulerBuilder {
protected Clock clock = new SystemClock(); // if this is set, waiter-clocks must be updated

protected final DataSource dataSource;
protected SchedulerName schedulerName = new SchedulerName.Hostname();
protected SchedulerName schedulerName;
protected int executorThreads = 10;
protected final List<Task<?>> knownTasks = new ArrayList<>();
protected final List<OnStartup> startTasks = new ArrayList<>();
Expand Down Expand Up @@ -150,6 +150,11 @@ public Scheduler build() {
if (pollingLimit < executorThreads) {
LOG.warn("Polling-limit is less than number of threads. Should be equal or higher.");
}

if (schedulerName == null) {
schedulerName = new SchedulerName.Hostname();
}

final TaskResolver taskResolver = new TaskResolver(statsRegistry, clock, knownTasks);
final JdbcCustomization jdbcCustomization = ofNullable(this.jdbcCustomization).orElse(new AutodetectJdbcCustomization(dataSource));
final JdbcTaskRepository taskRepository = new JdbcTaskRepository(dataSource, jdbcCustomization, tableName, taskResolver, schedulerName, serializer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ class Hostname implements SchedulerName {

public Hostname() {
try {
long start = System.currentTimeMillis();
LOG.debug("Resolving hostname..");
cachedHostname = InetAddress.getLocalHost().getHostName();
LOG.debug("Resolved hostname..");
long duration = System.currentTimeMillis() - start;
if (duration > 1000) {
LOG.warn("Hostname-lookup took {}ms", duration);
}
} catch (UnknownHostException e) {
LOG.warn("Failed to resolve hostname. Using dummy-name for scheduler.");
cachedHostname = "failed.hostname.lookup";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class AutodetectJdbcCustomization implements JdbcCustomization {
public AutodetectJdbcCustomization(DataSource dataSource) {
JdbcCustomization detectedCustomization = new DefaultJdbcCustomization();

LOG.debug("Detecting database...");
try (Connection c = dataSource.getConnection()) {
String databaseProductName = c.getMetaData().getDatabaseProductName();
LOG.info("Detected database {}.", databaseProductName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.github.kagkarlsson.scheduler.testhelper;

import com.github.kagkarlsson.scheduler.SchedulerName;
import com.github.kagkarlsson.scheduler.jdbc.DefaultJdbcCustomization;
import com.github.kagkarlsson.scheduler.SchedulerBuilder;
import com.github.kagkarlsson.scheduler.JdbcTaskRepository;
Expand Down Expand Up @@ -65,7 +66,7 @@ public ManualSchedulerBuilder statsRegistry(StatsRegistry statsRegistry) {

public ManualScheduler build() {
final TaskResolver taskResolver = new TaskResolver(statsRegistry, clock, knownTasks);
final JdbcTaskRepository taskRepository = new JdbcTaskRepository(dataSource, new DefaultJdbcCustomization(), tableName, taskResolver, schedulerName, serializer);
final JdbcTaskRepository taskRepository = new JdbcTaskRepository(dataSource, new DefaultJdbcCustomization(), tableName, taskResolver, new SchedulerName.Fixed("manual"), serializer);

return new ManualScheduler(clock, taskRepository, taskResolver, executorThreads, new DirectExecutorService(), schedulerName, waiter, heartbeatInterval, enableImmediateExecution, statsRegistry, pollingLimit, deleteUnresolvedAfter, startTasks);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@ public EmbeddedPostgresqlExtension(Consumer<DataSource> initializeSchema, Consum
this.initializeSchema = initializeSchema;
this.cleanupAfter = cleanupAfter;
try {
if (embeddedPostgresql == null) {
embeddedPostgresql = initPostgres();
synchronized (this) {

HikariConfig config = new HikariConfig();
config.setDataSource(embeddedPostgresql.getDatabase("test", "test"));
if (embeddedPostgresql == null) {
embeddedPostgresql = initPostgres();

dataSource = new HikariDataSource(config);
HikariConfig config = new HikariConfig();
config.setDataSource(embeddedPostgresql.getDatabase("test", "test"));

initializeSchema.accept(dataSource);
dataSource = new HikariDataSource(config);

initializeSchema.accept(dataSource);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public void setUp() {
scheduler = Scheduler.create(getDataSource(), Lists.newArrayList(oneTime, recurring))
.pollingInterval(Duration.ofMillis(10))
.heartbeatInterval(Duration.ofMillis(100))
.schedulerName(new SchedulerName.Fixed("test"))
.statsRegistry(statsRegistry)
.build();
stopScheduler.register(scheduler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.zaxxer.hikari.HikariDataSource;
import com.zaxxer.hikari.util.DriverDataSource;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.junit.jupiter.Container;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.github.kagkarlsson.scheduler.compatibility;

import com.github.kagkarlsson.scheduler.DbUtils;
import com.github.kagkarlsson.scheduler.EmbeddedPostgresqlExtension;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import com.zaxxer.hikari.util.DriverDataSource;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.testcontainers.containers.MSSQLServerContainer;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import javax.sql.DataSource;
import java.util.Properties;

@Tag("compatibility")
@Testcontainers
public class NoAutoCommitPostgresqlCompatibilityTest extends CompatibilityTest {

@Container
private static final PostgreSQLContainer POSTGRES = new PostgreSQLContainer();
private static HikariDataSource pooledDatasource;

@BeforeAll
private static void initSchema() {
final DriverDataSource datasource = new DriverDataSource(POSTGRES.getJdbcUrl(), "org.postgresql.Driver",
new Properties(), POSTGRES.getUsername(), POSTGRES.getPassword());

// init schema
DbUtils.runSqlResource("/postgresql_tables.sql").accept(datasource);


// Setup non auto-committing datasource
final HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDataSource(datasource);
hikariConfig.setAutoCommit(false);
pooledDatasource = new HikariDataSource(hikariConfig);

}
@Override
public DataSource getDataSource() {
return pooledDatasource;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.kagkarlsson.scheduler.DbUtils;
import com.github.kagkarlsson.scheduler.EmbeddedPostgresqlExtension;
import com.github.kagkarlsson.scheduler.Scheduler;
import com.github.kagkarlsson.scheduler.SchedulerName;
import com.github.kagkarlsson.scheduler.StopSchedulerExtension;
import com.github.kagkarlsson.scheduler.helper.TestableRegistry;
import com.github.kagkarlsson.scheduler.task.CompletionHandler;
Expand Down Expand Up @@ -45,6 +46,7 @@ public void complete(ExecutionComplete executionComplete, ExecutionOperations<Vo
Scheduler scheduler = Scheduler.create(postgres.getDataSource(), customTask)
.pollingInterval(Duration.ofMillis(100))
.heartbeatInterval(Duration.ofMillis(100))
.schedulerName(new SchedulerName.Fixed("test"))
.statsRegistry(registry)
.build();
stopScheduler.register(scheduler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import co.unruly.matchers.TimeMatchers;
import com.github.kagkarlsson.scheduler.EmbeddedPostgresqlExtension;
import com.github.kagkarlsson.scheduler.Scheduler;
import com.github.kagkarlsson.scheduler.SchedulerName;
import com.github.kagkarlsson.scheduler.StopSchedulerExtension;
import com.github.kagkarlsson.scheduler.TestTasks;
import com.github.kagkarlsson.scheduler.helper.TestableRegistry;
Expand Down Expand Up @@ -51,7 +52,7 @@ public void test_execute_until_none_left_low_polling_limit() {
}

@Test
@Disabled //FIXLATER: Disabled because of flakiness. Need to investigate and re-enable
@Disabled //FIXLATER: Disabled because of flakiness. Need to investigate and re-enable
public void test_execute_until_none_left_high_volume() {
testExecuteUntilNoneLeft(12, 4, 200);
}
Expand All @@ -69,6 +70,7 @@ private void testExecuteUntilNoneLeft(int pollingLimit, int threads, int executi
.pollingLimit(pollingLimit)
.threads(threads)
.pollingInterval(Duration.ofMinutes(1))
.schedulerName(new SchedulerName.Fixed("test"))
.statsRegistry(registry)
.build();
stopScheduler.register(scheduler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.github.kagkarlsson.scheduler.DbUtils;
import com.github.kagkarlsson.scheduler.EmbeddedPostgresqlExtension;
import com.github.kagkarlsson.scheduler.Scheduler;
import com.github.kagkarlsson.scheduler.SchedulerName;
import com.github.kagkarlsson.scheduler.StopSchedulerExtension;
import com.github.kagkarlsson.scheduler.TestTasks;
import com.github.kagkarlsson.scheduler.helper.TestableRegistry;
Expand Down Expand Up @@ -52,6 +53,7 @@ public void test_immediate_execution() {
Scheduler scheduler = Scheduler.create(postgres.getDataSource(), task)
.pollingInterval(Duration.ofMinutes(1))
.enableImmediateExecution()
.schedulerName(new SchedulerName.Fixed("test"))
.statsRegistry(registry)
.build();
stopScheduler.register(scheduler);
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<developers>
<developer>
<name>Gustav Karlsson</name>
<email>gustav80@gmail.com</email>
<email>kagkarlsson@gmail.com</email>
</developer>
</developers>

Expand Down

0 comments on commit 3edc901

Please sign in to comment.