This repository has been archived by the owner on Sep 18, 2021. It is now read-only.
/
SqlShard.scala
637 lines (556 loc) · 26.8 KB
/
SqlShard.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/*
* Copyright 2010 Twitter, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.twitter.flockdb.shards
import java.util.Random
import java.sql.{BatchUpdateException, ResultSet, SQLException, SQLIntegrityConstraintViolationException}
import scala.collection.mutable
import com.twitter.gizzard.proxy.SqlExceptionWrappingProxy
import com.twitter.gizzard.shards
import com.twitter.ostrich.Stats
import com.twitter.querulous.config.Connection
import com.twitter.querulous.evaluator.{QueryEvaluator, QueryEvaluatorFactory, Transaction}
import com.twitter.querulous.query
import com.twitter.querulous.query.{SqlQueryTimeoutException}
import com.twitter.util.Time
import com.twitter.util.TimeConversions._
import com.mysql.jdbc.exceptions.MySQLTransactionRollbackException
import net.lag.configgy.ConfigMap
import net.lag.logging.Logger
import State._
object QueryClass {
val Select = query.QueryClass.Select
val Execute = query.QueryClass.Execute
val SelectModify = query.QueryClass("select_modify")
val SelectCopy = query.QueryClass("select_copy")
}
class SqlShardFactory(instantiatingQueryEvaluatorFactory: QueryEvaluatorFactory, materializingQueryEvaluatorFactory: QueryEvaluatorFactory, connection: Connection)
extends shards.ShardFactory[Shard] {
val deadlockRetries = 3
val EDGE_TABLE_DDL = """
CREATE TABLE IF NOT EXISTS %s (
source_id %s NOT NULL,
position BIGINT NOT NULL,
updated_at INT UNSIGNED NOT NULL,
destination_id %s NOT NULL,
count TINYINT UNSIGNED NOT NULL,
state TINYINT NOT NULL,
PRIMARY KEY (source_id, state, position),
UNIQUE unique_source_id_destination_id (source_id, destination_id)
) TYPE=INNODB"""
val METADATA_TABLE_DDL = """
CREATE TABLE IF NOT EXISTS %s (
source_id %s NOT NULL,
count INT NOT NULL,
state TINYINT NOT NULL,
updated_at INT UNSIGNED NOT NULL,
PRIMARY KEY (source_id)
) TYPE=INNODB
"""
def instantiate(shardInfo: shards.ShardInfo, weight: Int, children: Seq[Shard]) = {
val queryEvaluator = instantiatingQueryEvaluatorFactory(connection.withHost(shardInfo.hostname))
new SqlExceptionWrappingProxy(shardInfo.id).apply[Shard](new SqlShard(queryEvaluator, shardInfo, weight, children, deadlockRetries))
}
def materialize(shardInfo: shards.ShardInfo) = {
try {
val queryEvaluator = materializingQueryEvaluatorFactory(connection.withHost(shardInfo.hostname).withoutDatabase)
queryEvaluator.execute("CREATE DATABASE IF NOT EXISTS " + connection.database)
queryEvaluator.execute(EDGE_TABLE_DDL.format(connection.database + "." + shardInfo.tablePrefix + "_edges", shardInfo.sourceType, shardInfo.destinationType))
queryEvaluator.execute(METADATA_TABLE_DDL.format(connection.database + "." + shardInfo.tablePrefix + "_metadata", shardInfo.sourceType))
} catch {
case e: SQLException => throw new shards.ShardException(e.toString)
case e: SqlQueryTimeoutException => throw new shards.ShardTimeoutException(e.timeout, shardInfo.id, e)
}
}
}
class SqlShard(val queryEvaluator: QueryEvaluator, val shardInfo: shards.ShardInfo,
val weight: Int, val children: Seq[Shard], deadlockRetries: Int) extends Shard {
val log = Logger.get(getClass.getName)
private val tablePrefix = shardInfo.tablePrefix
private val randomGenerator = new Random
import QueryClass._
def get(sourceId: Long, destinationId: Long) = {
queryEvaluator.selectOne("SELECT * FROM " + tablePrefix + "_edges WHERE source_id = ? AND destination_id = ?", sourceId, destinationId) { row =>
makeEdge(row)
}
}
def getMetadata(sourceId: Long): Option[Metadata] = {
queryEvaluator.selectOne("SELECT * FROM " + tablePrefix + "_metadata WHERE source_id = ?", sourceId) { row =>
Metadata(sourceId, State(row.getInt("state")), row.getInt("count"), Time(row.getInt("updated_at").seconds))
}
}
def selectAllMetadata(cursor: Cursor, count: Int) = {
val metadatas = new mutable.ArrayBuffer[Metadata]
var nextCursor = Cursor.Start
var returnedCursor = Cursor.End
var i = 0
val query = "SELECT * FROM " + tablePrefix +
"_metadata WHERE source_id > ? ORDER BY source_id LIMIT ?"
queryEvaluator.select(SelectCopy, query, cursor.position, count + 1) { row =>
if (i < count) {
val sourceId = row.getLong("source_id")
metadatas += Metadata(sourceId, State(row.getInt("state")), row.getInt("count"),
Time(row.getInt("updated_at").seconds))
nextCursor = Cursor(sourceId)
i += 1
} else {
returnedCursor = nextCursor
}
}
(metadatas, returnedCursor)
}
def count(sourceId: Long, states: Seq[State]): Int = {
queryEvaluator.selectOne("SELECT state, `count` FROM " + tablePrefix + "_metadata WHERE source_id = ?", sourceId) { row =>
states.foldLeft(0) { (result, state) =>
result + (if (state == State(row.getInt("state"))) row.getInt("count") else 0)
}
} getOrElse {
0
}
}
def counts(sourceIds: Seq[Long], results: mutable.Map[Long, Int]) {
queryEvaluator.select("SELECT source_id, `count` FROM " + tablePrefix + "_metadata WHERE source_id IN (?)", sourceIds) { row =>
results(row.getLong("source_id")) = row.getInt("count")
}
}
private def populateMetadata(sourceId: Long) {
queryEvaluator.selectOne(SelectModify, "SELECT GET_LOCK('" + tablePrefix + sourceId + "', 0.1)") { row => }
queryEvaluator.execute(
"INSERT IGNORE INTO " + tablePrefix + "_metadata (source_id, count, state, updated_at) VALUES (?, 0, 0, 0)",
sourceId)
queryEvaluator.selectOne(SelectModify, "SELECT RELEASE_LOCK('" + tablePrefix + sourceId + "')") { row => }
}
private def computeCount(sourceId: Long, state: State) = {
queryEvaluator.count("SELECT count(*) FROM " + tablePrefix + "_edges WHERE source_id = ? AND state = ?", sourceId, state.id)
}
def selectAll(cursor: (Cursor, Cursor), count: Int): (Seq[Edge], (Cursor, Cursor)) = {
val edges = new mutable.ArrayBuffer[Edge]
var nextCursor = (Cursor.Start, Cursor.Start)
var returnedCursor = (Cursor.End, Cursor.End)
var i = 0
val query = "SELECT * FROM " + tablePrefix + "_edges " +
"USE INDEX (unique_source_id_destination_id) WHERE (source_id = ? AND destination_id > ?) " +
"OR (source_id > ?) ORDER BY source_id, destination_id LIMIT ?"
val (cursor1, cursor2) = cursor
queryEvaluator.select(SelectCopy, query, cursor1.position, cursor2.position, cursor1.position,
count + 1) { row =>
if (i < count) {
edges += makeEdge(row)
nextCursor = (Cursor(row.getLong("source_id")), Cursor(row.getLong("destination_id")))
i += 1
} else {
returnedCursor = nextCursor
}
}
(edges, returnedCursor)
}
def selectByDestinationId(sourceId: Long, states: Seq[State], count: Int, cursor: Cursor) = {
select("destination_id", "unique_source_id_destination_id", count, cursor,
"source_id = ? AND state IN (?)",
List(sourceId, states.map(_.id).toList): _*)
}
def selectIncludingArchived(sourceId: Long, count: Int, cursor: Cursor) = {
select(SelectModify, "destination_id", "unique_source_id_destination_id", count, cursor,
"source_id = ? AND state != ?",
sourceId, Removed.id)
}
def selectByPosition(sourceId: Long, states: Seq[State], count: Int, cursor: Cursor) = {
select("position", "PRIMARY", count, cursor,
"source_id = ? AND state IN (?)",
List(sourceId, states.map(_.id).toList): _*)
}
private def select(cursorName: String, index: String, count: Int,
cursor: Cursor, conditions: String, args: Any*): ResultWindow[Long] = {
select(QueryClass.Select, cursorName, index, count, cursor, conditions, args: _*)
}
private def select(queryClass: querulous.query.QueryClass, cursorName: String, index: String, count: Int,
cursor: Cursor, conditions: String, args: Any*): ResultWindow[Long] = {
var edges = new mutable.ArrayBuffer[(Long, Cursor)]
val order = if (cursor < Cursor.Start) "ASC" else "DESC"
val inequality = if (order == "DESC") "<" else ">"
val (continueCursorQuery, args1) = query(cursorName, index, 1, cursor, opposite(order), opposite(inequality), conditions, args)
val (edgesQuery, args2) = query(cursorName, index, count + 1, cursor, order, inequality, conditions, args)
val totalQuery = continueCursorQuery + " UNION " + edgesQuery
queryEvaluator.select(queryClass, totalQuery, args1 ++ args2: _*) { row =>
edges += (row.getLong("destination_id"), Cursor(row.getLong(cursorName)))
}
var page = edges.projection
if (cursor < Cursor.Start) page = page.reverse
new ResultWindow(page, count, cursor)
}
def selectEdges(sourceId: Long, states: Seq[State], count: Int, cursor: Cursor) = {
val conditions = "source_id = ? AND state IN (?)"
val order = if (cursor < Cursor.Start) "ASC" else "DESC"
val inequality = if (order == "DESC") "<" else ">"
val args = sourceId :: states.map(_.id).toList
val (edgesQuery, args1) = query("*", "position", "PRIMARY", count + 1, cursor, order, inequality, conditions, args)
val (continueCursorQuery, args2) = query("*", "position", "PRIMARY", 1, cursor, opposite(order), opposite(inequality), conditions, args)
val edges = new mutable.ArrayBuffer[(Edge, Cursor)]
queryEvaluator.select(continueCursorQuery + " UNION " + edgesQuery, args1 ++ args2: _*) { row =>
edges += (makeEdge(row), Cursor(row.getLong("position")))
}
var page = edges.projection
if (cursor < Cursor.Start) page = page.reverse
new ResultWindow(page, count, cursor)
}
private def query(cursorName: String, index: String, count: Int, cursor: Cursor, order: String,
inequality: String, conditions: String, args: Seq[Any]): (String, Seq[Any]) = {
val projections = Set("destination_id", cursorName).mkString(", ")
query(projections, cursorName, index, count, cursor, order, inequality, conditions, args)
}
private def query(projections: String, cursorName: String, index: String, count: Int,
cursor: Cursor, order: String, inequality: String, conditions: String, args: Seq[Any]): (String, Seq[Any]) = {
val position = if (cursor == Cursor.Start) Math.MAX_LONG else cursor.magnitude.position
val query = "(SELECT " + projections +
" FROM " + tablePrefix + "_edges USE INDEX (" + index + ")" +
" WHERE " + conditions +
" AND " + cursorName + " " + inequality + "?" +
" ORDER BY " + cursorName + " " + order +
" LIMIT " + count + ")"
(query, args ++ List(position))
}
private def opposite(direction: String) = direction match {
case "ASC" => "DESC"
case "DESC" => "ASC"
case "<" => ">="
case ">" => "<="
}
def intersect(sourceId: Long, states: Seq[State], destinationIds: Seq[Long]) = {
if (destinationIds.size == 0) Nil else {
queryEvaluator.select("SELECT destination_id FROM " + tablePrefix + "_edges WHERE source_id = ? AND state IN (?) AND destination_id IN (?) ORDER BY destination_id DESC",
List(sourceId, states.map(_.id).toList, destinationIds): _*) { row =>
row.getLong("destination_id")
}
}
}
def intersectEdges(sourceId: Long, states: Seq[State], destinationIds: Seq[Long]) = {
if (destinationIds.size == 0) Nil else {
queryEvaluator.select("SELECT * FROM " + tablePrefix + "_edges WHERE source_id = ? AND state IN (?) AND destination_id IN (?) ORDER BY destination_id DESC",
List(sourceId, states.map(_.id).toList, destinationIds): _*) { row =>
makeEdge(row)
}
}
}
def add(sourceId: Long, destinationId: Long, position: Long, updatedAt: Time) = {
write(new Edge(sourceId, destinationId, position, updatedAt, 1, Normal))
}
def add(sourceId: Long, updatedAt: Time) {
updateMetadata(sourceId, Normal, updatedAt)
}
def negate(sourceId: Long, destinationId: Long, position: Long, updatedAt: Time) = {
write(new Edge(sourceId, destinationId, position, updatedAt, 1, Negative))
}
def negate(sourceId: Long, updatedAt: Time) {
updateMetadata(sourceId, Negative, updatedAt)
}
def remove(sourceId: Long, destinationId: Long, position: Long, updatedAt: Time) = {
write(new Edge(sourceId, destinationId, position, updatedAt, 1, Removed))
}
def remove(sourceId: Long, updatedAt: Time) {
updateMetadata(sourceId, Removed, updatedAt)
}
def archive(sourceId: Long, destinationId: Long, position: Long, updatedAt: Time) = {
write(new Edge(sourceId, destinationId, position, updatedAt, 1, Archived))
}
def archive(sourceId: Long, updatedAt: Time) {
updateMetadata(sourceId, Archived, updatedAt)
}
override def equals(other: Any) = {
other match {
case other: SqlShard =>
tablePrefix == other.tablePrefix && queryEvaluator == other.queryEvaluator
case _ =>
false
}
}
override def hashCode = tablePrefix.hashCode * 37 + queryEvaluator.hashCode
private class MissingMetadataRow extends Exception("Missing Count Row")
private def insertEdge(transaction: Transaction, metadata: Metadata, edge: Edge): Int = {
val insertedRows =
transaction.execute("INSERT INTO " + tablePrefix + "_edges (source_id, position, " +
"updated_at, destination_id, count, state) VALUES (?, ?, ?, ?, ?, ?)",
edge.sourceId, edge.position, edge.updatedAt.inSeconds,
edge.destinationId, edge.count, edge.state.id)
if (edge.state == metadata.state) insertedRows else 0
}
def bulkUnsafeInsertEdges(edges: Seq[Edge]) {
bulkUnsafeInsertEdges(queryEvaluator, edges)
}
def bulkUnsafeInsertEdges(transaction: QueryEvaluator, edges: Seq[Edge]) = {
if (edges.size > 0) {
val query = "INSERT INTO " + tablePrefix + "_edges (source_id, position, updated_at, destination_id, count, state) VALUES (?, ?, ?, ?, ?, ?)"
transaction.executeBatch(query) { batch =>
edges.foreach { edge =>
batch(edge.sourceId, edge.position, edge.updatedAt.inSeconds, edge.destinationId, edge.count, edge.state.id)
}
}
}
}
def bulkUnsafeInsertMetadata(metadatas: Seq[Metadata]) = {
if (metadatas.length > 0) {
val query = "INSERT INTO " + tablePrefix + "_metadata (source_id, count, state, updated_at) VALUES (?, ?, ?, ?)"
queryEvaluator.executeBatch(query) { batch =>
metadatas.foreach { metadata =>
batch(metadata.sourceId, metadata.count, metadata.state.id, metadata.updatedAt.inSeconds)
}
}
}
}
private def updateEdge(transaction: Transaction, metadata: Metadata, edge: Edge,
oldEdge: Edge): Int = {
if ((oldEdge.updatedAt == edge.updatedAt) && (oldEdge.state max edge.state) != edge.state) return 0
val updatedRows = if (
oldEdge.state != Archived && // Only update position when coming from removed or negated into normal
oldEdge.state != Normal &&
edge.state == Normal
) {
transaction.execute("UPDATE " + tablePrefix + "_edges SET updated_at = ?, " +
"position = ?, count = 0, state = ? " +
"WHERE source_id = ? AND destination_id = ? AND " +
"updated_at <= ?",
edge.updatedAt.inSeconds, edge.position, edge.state.id,
edge.sourceId, edge.destinationId, edge.updatedAt.inSeconds)
} else {
try {
transaction.execute("UPDATE " + tablePrefix + "_edges SET updated_at = ?, " +
"count = 0, state = ? " +
"WHERE source_id = ? AND destination_id = ? AND updated_at <= ?",
edge.updatedAt.inSeconds, edge.state.id, edge.sourceId,
edge.destinationId, edge.updatedAt.inSeconds)
} catch {
case e: SQLIntegrityConstraintViolationException =>
// usually this is a (source_id, state, position) violation. scramble the position more.
// FIXME: hacky. remove with the new schema.
transaction.execute("UPDATE " + tablePrefix + "_edges SET updated_at = ?, " +
"count = 0, state = ?, position = position + ? " +
"WHERE source_id = ? AND destination_id = ? AND updated_at <= ?",
edge.updatedAt.inSeconds, edge.state.id,
(randomGenerator.nextInt() % 999) + 1, edge.sourceId,
edge.destinationId, edge.updatedAt.inSeconds)
}
}
if (edge.state != oldEdge.state &&
(oldEdge.state == metadata.state || edge.state == metadata.state)) updatedRows else 0
}
// returns +1, 0, or -1, depending on how the metadata count should change after this operation.
// `predictExistence`=true for normal operations, false for copy/migrate.
private def writeEdge(transaction: Transaction, metadata: Metadata, edge: Edge,
predictExistence: Boolean): Int = {
val countDelta = if (predictExistence) {
transaction.selectOne(SelectModify,
"SELECT * FROM " + tablePrefix + "_edges WHERE source_id = ? " +
"and destination_id = ?", edge.sourceId, edge.destinationId) { row =>
makeEdge(row)
}.map { oldRow =>
updateEdge(transaction, metadata, edge, oldRow)
}.getOrElse {
insertEdge(transaction, metadata, edge)
}
} else {
try {
insertEdge(transaction, metadata, edge)
} catch {
case e: SQLIntegrityConstraintViolationException =>
transaction.selectOne(SelectModify,
"SELECT * FROM " + tablePrefix + "_edges WHERE source_id = ? " +
"and destination_id = ?", edge.sourceId, edge.destinationId) { row =>
makeEdge(row)
}.map { oldRow =>
updateEdge(transaction, metadata, edge, oldRow)
}.getOrElse(0)
}
}
if (edge.state == metadata.state) countDelta else -countDelta
}
private def write(edge: Edge) {
write(edge, deadlockRetries, true)
}
private def write(edge: Edge, tries: Int, predictExistence: Boolean) {
try {
atomically(edge.sourceId) { (transaction, metadata) =>
val countDelta = writeEdge(transaction, metadata, edge, predictExistence)
if (countDelta != 0) {
transaction.execute("UPDATE " + tablePrefix + "_metadata SET count = GREATEST(count + ?, 0) " +
"WHERE source_id = ?", countDelta, edge.sourceId)
}
}
} catch {
case e: MySQLTransactionRollbackException if (tries > 0) =>
println("dlock")
write(edge, tries - 1, predictExistence)
case e: SQLIntegrityConstraintViolationException if (tries > 0) =>
// temporary. until the position differential between master/slave is fixed, it's
// possible for a slave migration to have two different edges with the same position.
write(new Edge(edge.sourceId, edge.destinationId, edge.position + 1, edge.updatedAt,
edge.count, edge.state), tries - 1, predictExistence)
}
}
/**
* Mysql may throw an exception for a bulk operation that actually partially completed.
* If that happens, try to pick up the pieces and indicate what happened.
*/
case class BurstResult(completed: Seq[Edge], failed: Seq[Edge])
def writeBurst(transaction: Transaction, edges: Seq[Edge]): BurstResult = {
try {
bulkUnsafeInsertEdges(transaction, edges)
BurstResult(edges, Nil)
} catch {
case e: BatchUpdateException =>
val completed = new mutable.ArrayBuffer[Edge]
val failed = new mutable.ArrayBuffer[Edge]
e.getUpdateCounts().zip(edges.toArray).foreach { case (errorCode, edge) =>
if (errorCode < 0) {
failed += edge
} else {
completed += edge
}
}
BurstResult(completed, failed)
}
}
private def updateCount(transaction: QueryEvaluator, sourceId: Long, countDelta: Int) = {
transaction.execute("UPDATE " + tablePrefix + "_metadata SET count = count + ? " +
"WHERE source_id = ?", countDelta, sourceId)
}
def writeCopies(edges: Seq[Edge]) {
if (!edges.isEmpty) {
Stats.addTiming("x-copy-burst", edges.size)
var sourceIdsSet = Set[Long]()
edges.foreach { edge => sourceIdsSet += edge.sourceId }
val sourceIds = sourceIdsSet.toSeq
atomically(sourceIds) { (transaction, metadataById) =>
val result = writeBurst(transaction, edges)
if (result.completed.size > 0) {
var currentSourceId = -1L
var countDeltas = new Array[Int](4)
result.completed.foreach { edge =>
if (edge.sourceId != currentSourceId) {
if (currentSourceId != -1)
updateCount(transaction, currentSourceId, countDeltas(metadataById(currentSourceId).state.id))
currentSourceId = edge.sourceId
countDeltas = new Array[Int](4)
}
countDeltas(edge.state.id) += 1
}
updateCount(transaction, currentSourceId,
countDeltas(metadataById(currentSourceId).state.id))
}
if (result.failed.size > 0) {
Stats.incr("x-copy-fallback")
var currentSourceId = -1L
var countDelta = 0
result.failed.foreach { edge =>
if (edge.sourceId != currentSourceId) {
if (currentSourceId != -1)
updateCount(transaction, currentSourceId, countDelta)
currentSourceId = edge.sourceId
countDelta = 0
}
countDelta += writeEdge(transaction, metadataById(edge.sourceId), edge, false)
}
updateCount(transaction, currentSourceId, countDelta)
}
}
}
}
def withLock[A](sourceId: Long)(f: (Shard, Metadata) => A) = {
atomically(sourceId) { (transaction, metadata) =>
f(new SqlShard(transaction, shardInfo, weight, children, deadlockRetries), metadata)
}
}
private def atomically[A](sourceId: Long)(f: (Transaction, Metadata) => A): A = {
try {
queryEvaluator.transaction { transaction =>
transaction.selectOne(SelectModify,
"SELECT * FROM " + tablePrefix + "_metadata WHERE source_id = ? FOR UPDATE", sourceId) { row =>
f(transaction, Metadata(sourceId, State(row.getInt("state")), row.getInt("count"), Time(row.getInt("updated_at").seconds)))
} getOrElse(throw new MissingMetadataRow)
}
} catch {
case e: MissingMetadataRow =>
populateMetadata(sourceId)
atomically(sourceId)(f)
}
}
private def atomically[A](sourceIds: Seq[Long])(f: (Transaction, collection.Map[Long, Metadata]) => A): A = {
try {
val metadataMap = mutable.Map[Long, Metadata]()
queryEvaluator.transaction { transaction =>
transaction.select(SelectModify,
"SELECT * FROM " + tablePrefix + "_metadata WHERE source_id in (?) FOR UPDATE", sourceIds) { row =>
metadataMap.put(row.getLong("source_id"), Metadata(row.getLong("source_id"), State(row.getInt("state")), row.getInt("count"), Time(row.getInt("updated_at").seconds)))
}
if (metadataMap.size < sourceIds.length)
throw new MissingMetadataRow
f(transaction, metadataMap)
}
} catch {
case e: MissingMetadataRow =>
sourceIds.foreach { sourceId => populateMetadata(sourceId) }
atomically(sourceIds)(f)
}
}
def writeMetadata(metadata: Metadata) {
println("bork")
try {
queryEvaluator.execute("INSERT INTO " + tablePrefix + "_metadata (source_id, count, state, " +
"updated_at) VALUES (?, ?, ?, ?)",
metadata.sourceId, 0, metadata.state.id, metadata.updatedAt.inSeconds)
} catch {
case e: SQLIntegrityConstraintViolationException =>
atomically(metadata.sourceId) { (transaction, oldMetadata) =>
transaction.execute("UPDATE " + tablePrefix + "_metadata SET state = ?, updated_at = ? " +
"WHERE source_id = ? AND updated_at <= ?",
metadata.state.id, metadata.updatedAt.inSeconds, metadata.sourceId,
metadata.updatedAt.inSeconds)
}
}
}
def writeMetadata(metadatas: Seq[Metadata]) = {
println("bork")
if (!metadatas.isEmpty) {
try {
val query = "INSERT INTO " + tablePrefix + "_metadata (source_id, count, state, updated_at) VALUES (?, 0, ?, ?)"
queryEvaluator.executeBatch(query) { batch =>
metadatas.foreach { metadata =>
batch(metadata.sourceId, metadata.state.id, metadata.updatedAt.inSeconds)
}
}
} catch {
case e: BatchUpdateException =>
e.getUpdateCounts().zip(metadatas.toArray).foreach { case (errorCode, metadata) =>
if (errorCode < 0)
writeMetadata(metadata)
}
}
}
}
def updateMetadata(metadata: Metadata): Unit = updateMetadata(metadata.sourceId, metadata.state, metadata.updatedAt)
// FIXME: computeCount could be really expensive. :(
def updateMetadata(sourceId: Long, state: State, updatedAt: Time) {
atomically(sourceId) { (transaction, metadata) =>
if ((updatedAt != metadata.updatedAt) || ((metadata.state max state) == state)) {
transaction.execute("UPDATE " + tablePrefix + "_metadata SET state = ?, updated_at = ?, count = ? WHERE source_id = ? AND updated_at <= ?",
state.id, updatedAt.inSeconds, computeCount(sourceId, state), sourceId, updatedAt.inSeconds)
}
}
}
private def makeEdge(sourceId: Long, destinationId: Long, position: Long, updatedAt: Time, count: Int, stateId: Int): Edge = {
new Edge(sourceId, destinationId, position, updatedAt, count, State(stateId))
}
private def makeEdge(row: ResultSet): Edge = {
makeEdge(row.getLong("source_id"), row.getLong("destination_id"), row.getLong("position"), Time(row.getInt("updated_at").seconds), row.getInt("count"), row.getInt("state"))
}
}