Skip to content

Commit

Permalink
timeseries: add a class to persist start dates in mysql (#253)
Browse files Browse the repository at this point in the history
Also:
- Secure the db lock before doing schema migration
- Move the lock table creation out of schema migration
  • Loading branch information
Adrien Surée authored Apr 3, 2018
1 parent 0bb80a5 commit 6bcee28
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
14 changes: 7 additions & 7 deletions core/src/main/scala/com/criteo/cuttle/Database.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,6 @@ private[cuttle] object Database {

val schemaEvolutions = List(
sql"""
CREATE TABLE locks (
locked_by VARCHAR(36) NOT NULL,
locked_at DATETIME NOT NULL
) ENGINE = INNODB;

CREATE TABLE executions (
id CHAR(36) NOT NULL,
job VARCHAR(1000) NOT NULL,
Expand Down Expand Up @@ -119,6 +114,11 @@ private[cuttle] object Database {

// Try to insert our lock at bootstrap
(for {
_ <- sql"""CREATE TABLE IF NOT EXISTS locks (
locked_by VARCHAR(36) NOT NULL,
locked_at DATETIME NOT NULL
) ENGINE = INNODB
""".update.run
locks <- sql"""
SELECT locked_by, locked_at FROM locks WHERE TIMESTAMPDIFF(MINUTE, locked_at, NOW()) < 5;
""".query[(String, Instant)].to[List]
Expand Down Expand Up @@ -189,9 +189,9 @@ private[cuttle] object Database {

def connect(dbConfig: DatabaseConfig): XA = connections.getOrElseUpdate(
dbConfig, {
val xa = newHikariTransactor(dbConfig)
val xa = lockedTransactor(newHikariTransactor(dbConfig))
doSchemaUpdates.transact(xa).unsafeRunSync
lockedTransactor(xa)
xa
}
)
}
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/scala/com/criteo/cuttle/Utils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ import lol.http.{PartialService, Service}
/** A set of basic utilities useful to write workflows. */
package object utils {

/** Get a doobie transactor
*
* @param config Database configuration
*/
def transactor(config: DatabaseConfig): XA = Database.newHikariTransactor(config)

/** Executes unapplied schema evolutions
*
* @param table Name of the table that keeps track of applied schema changes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.criteo.cuttle.timeseries.contrib

import doobie.implicits._
import com.criteo.cuttle._
import java.time._

class PersistInstant(xa: XA) {
def set(id: String, t: Instant): Instant = {
sql"REPLACE INTO instant_data VALUES (${id}, ${t})"
.update.run.transact(xa).unsafeRunSync
t
}

def get(id: String): Option[Instant] =
sql"SELECT instant FROM instant_data WHERE id = ${id}"
.query[Instant].option.transact(xa).unsafeRunSync
}

object PersistInstant {
private val schemaUpgrades = List(
sql"""
CREATE TABLE instant_data (
id VARCHAR(1000) NOT NULL,
instant DATETIME NOT NULL,
PRIMARY KEY (id)
) ENGINE = INNODB
""".update.run
)

def apply(xa: XA): PersistInstant = {
utils.updateSchema("instant", schemaUpgrades).transact(xa).unsafeRunSync
new PersistInstant(xa)
}
}

0 comments on commit 6bcee28

Please sign in to comment.