Skip to content

Commit

Permalink
Fix #2 Insert: Stop replication on DuplicateKeyException only when th…
Browse files Browse the repository at this point in the history
…e doc to be inserted is different from the doc in DB
  • Loading branch information
ngocdaothanh committed Mar 20, 2015
1 parent 8a9156c commit a041c24
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
1.2:

* [#2](https://github.com/ngocdaothanh/mydit/issues/2)
Insert: Stop replication on DuplicateKeyException only when the doc to be
inserted is different from the doc in DB
* [#3](https://github.com/ngocdaothanh/mydit/issues/3)
Log INFO when failed replication event queue changes from nonempty to empty

Expand Down
20 changes: 17 additions & 3 deletions src/main/scala/mydit/MongoDBApplier.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ package mydit
import java.io.Serializable
import java.math.BigDecimal
import java.util.BitSet

import scala.collection.JavaConverters._
import scala.util.control.NonFatal

import com.github.shyiko.mysql.binlog.event.DeleteRowsEventData
import com.github.shyiko.mysql.binlog.event.UpdateRowsEventData
import com.github.shyiko.mysql.binlog.event.WriteRowsEventData

import com.mongodb.BasicDBObject
import com.mongodb.DBObject
import com.mongodb.DuplicateKeyException
import com.mongodb.MongoClient
import com.mongodb.MongoClientURI
import com.mongodb.ReadPreference
import com.mongodb.WriteConcern

/**
Expand Down Expand Up @@ -50,12 +54,22 @@ class MongoDBApplier(uri: String, binlogDb: String, binlogCollName: String, enum
def insert(nextPosition: Long, dbName: String, collName: String, cols: Seq[ColInfo], data: WriteRowsEventData) {
val db = client.getDB(dbName)
val coll = db.getCollection(collName)
var objs = Seq[DBObject]()
for (mySQLValues <- data.getRows.asScala) {
val obj = mySQLRowToMongoDBObject(cols, data.getIncludedColumns, mySQLValues)
objs = objs :+ obj
try {
coll.insert(obj, WriteConcern.ACKNOWLEDGED)
} catch {
case dup: DuplicateKeyException =>
// Stop replication only when the doc to be inserted is different from
// the doc in DB
// https://github.com/ngocdaothanh/mydit/issues/2
val fields = new BasicDBObject("_id", 1)
if (coll.findOne(obj, fields, ReadPreference.primary) == null) throw dup

case NonFatal(e) =>
throw e
}
}
coll.insert(WriteConcern.ACKNOWLEDGED, objs :_*)
binlogNextPosition(nextPosition)
}

Expand Down

0 comments on commit a041c24

Please sign in to comment.