Skip to content

Commit 5c14727

Browse files
authored
Validator: Add support for timestamp tolerance, by default 0ms (#64)
old behaviour is preserved by default
1 parent 81e4165 commit 5c14727

File tree

5 files changed

+14
-3
lines changed

5 files changed

+14
-3
lines changed

config.yaml.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,5 @@ validation:
197197
failuresToFetch: 100
198198
# What difference should we allow between floating point numbers?
199199
floatingPointTolerance: 0.001
200+
# What difference in ms should we allow between timestamps?
201+
timestampMsTolerance: 0

src/main/scala/com/scylladb/migrator/Validator.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ object Validator {
109109
l,
110110
r,
111111
config.validation.floatingPointTolerance,
112+
config.validation.timestampMsTolerance,
112113
config.validation.ttlToleranceMillis,
113114
config.validation.writetimeToleranceMillis,
114115
config.validation.compareTimestamps

src/main/scala/com/scylladb/migrator/config/Validation.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ case class Validation(compareTimestamps: Boolean,
77
ttlToleranceMillis: Long,
88
writetimeToleranceMillis: Long,
99
failuresToFetch: Int,
10-
floatingPointTolerance: Double)
10+
floatingPointTolerance: Double,
11+
timestampMsTolerance: Double)
1112
object Validation {
1213
implicit val encoder: Encoder[Validation] = deriveEncoder[Validation]
1314
implicit val decoder: Decoder[Validation] = deriveDecoder[Validation]

src/main/scala/com/scylladb/migrator/readers/Cassandra.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ object Cassandra {
2727
def determineCopyType(tableDef: TableDef,
2828
preserveTimesRequest: Boolean): Either[Throwable, CopyType] =
2929
if (tableDef.columnTypes.exists(_.isCollection) && preserveTimesRequest)
30-
Left(
31-
new Exception("TTL/Writetime preservation is unsupported for tables with collection types. Please check in your config the option 'preserveTimestamps' and set it to false to continue."))
30+
Left(new Exception(
31+
"TTL/Writetime preservation is unsupported for tables with collection types. Please check in your config the option 'preserveTimestamps' and set it to false to continue."))
3232
else if (preserveTimesRequest && tableDef.regularColumns.nonEmpty)
3333
Right(CopyType.WithTimestampPreservation)
3434
else if (preserveTimesRequest && tableDef.regularColumns.isEmpty) {

src/main/scala/com/scylladb/migrator/validation/RowComparisonFailure.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package com.scylladb.migrator.validation
33
import com.datastax.spark.connector.CassandraRow
44
import com.google.common.math.DoubleMath
55

6+
import java.time.temporal.ChronoUnit
7+
68
case class RowComparisonFailure(row: CassandraRow,
79
other: Option[CassandraRow],
810
items: List[RowComparisonFailure.Item]) {
@@ -41,6 +43,7 @@ object RowComparisonFailure {
4143
def compareRows(left: CassandraRow,
4244
right: Option[CassandraRow],
4345
floatingPointTolerance: Double,
46+
timestampMsTolerance: Double,
4447
ttlToleranceMillis: Long,
4548
writetimeToleranceMillis: Long,
4649
compareTimestamps: Boolean): Option[RowComparisonFailure] =
@@ -64,6 +67,10 @@ object RowComparisonFailure {
6467
rightValue = rightMap.get(name)
6568

6669
result = (rightValue, leftValue) match {
70+
// All timestamp types need to be compared with a configured tolerance
71+
case (Some(l: java.time.Instant), Some(r: java.time.Instant))
72+
if timestampMsTolerance > 0 =>
73+
Math.abs(r.until(l, ChronoUnit.MILLIS)) > timestampMsTolerance
6774
// All floating-point-like types need to be compared with a configured tolerance
6875
case (Some(l: Float), Some(r: Float)) =>
6976
!DoubleMath.fuzzyEquals(l, r, floatingPointTolerance)

0 commit comments

Comments
 (0)