This repository has been archived by the owner on Sep 18, 2021. It is now read-only.
/
Single.scala
135 lines (116 loc) · 4.25 KB
/
Single.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
/*
* 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.jobs.single
import com.twitter.logging.Logger
import com.twitter.util.{Time, Return, Throw}
import com.twitter.gizzard.scheduler._
import com.twitter.gizzard.shards._
import com.twitter.conversions.time._
import com.twitter.flockdb.{State, ForwardingManager, Cursor, UuidGenerator, Direction}
import com.twitter.flockdb.conversions.Numeric._
import com.twitter.flockdb.shards.Shard
import com.twitter.flockdb.shards.LockingRoutingNode._
class SingleJobParser(
forwardingManager: ForwardingManager,
uuidGenerator: UuidGenerator)
extends JsonJobParser {
def log = Logger.get
def apply(attributes: Map[String, Any]): JsonJob = {
val writeSuccesses = try {
attributes.get("write_successes") map {
_.asInstanceOf[Seq[Seq[String]]] map { case Seq(h, tp) => ShardId(h, tp) }
} getOrElse Nil
} catch {
case e => {
log.warning("Error parsing write successes. falling back to non-memoization", e)
Nil
}
}
val casted = attributes.asInstanceOf[Map[String, AnyVal]]
new Single(
casted("source_id").toLong,
casted("graph_id").toInt,
casted("destination_id").toLong,
casted("position").toLong,
State(casted("state").toInt),
Time.fromSeconds(casted("updated_at").toInt),
forwardingManager,
uuidGenerator,
writeSuccesses.toList
)
}
}
case class Single(
sourceId: Long,
graphId: Int,
destinationId: Long,
position: Long,
preferredState: State,
updatedAt: Time,
forwardingManager: ForwardingManager,
uuidGenerator: UuidGenerator,
var successes: List[ShardId] = Nil)
extends JsonJob {
def toMap = {
val base = Map(
"source_id" -> sourceId,
"graph_id" -> graphId,
"destination_id" -> destinationId,
"position" -> position,
"state" -> preferredState.id,
"updated_at" -> updatedAt.inSeconds
)
if (successes.isEmpty) {
base
} else {
base + ("write_successes" -> (successes map { case ShardId(h, tp) => Seq(h, tp) }))
}
}
def apply() = {
val forward = forwardingManager.findNode(sourceId, graphId, Direction.Forward)
val backward = forwardingManager.findNode(destinationId, graphId, Direction.Backward)
val uuid = uuidGenerator(position)
var currSuccesses: List[ShardId] = Nil
var currErrs: List[Throwable] = Nil
forward.optimistically(sourceId) { left =>
backward.optimistically(destinationId) { right =>
val state = left max right max preferredState
val forwardResults = writeToShard(forward.write, sourceId, destinationId, uuid, state)
val backwardResults = writeToShard(backward.write, destinationId, sourceId, uuid, state)
List(forwardResults, backwardResults) foreach {
_ foreach {
case Return(id) => currSuccesses = id :: currSuccesses
case Throw(e) => currErrs = e :: currErrs
}
}
}
}
// add successful writes here, since we are only successful if an optimistic lock exception is not raised.
successes = successes ++ currSuccesses
currErrs.headOption foreach { e => throw e }
}
def writeToShard(shards: NodeSet[Shard], sourceId: Long, destinationId: Long, uuid: Long, state: State) = {
shards.skip(successes) all { (shardId, shard) =>
state match {
case State.Normal => shard.add(sourceId, destinationId, uuid, updatedAt)
case State.Removed => shard.remove(sourceId, destinationId, uuid, updatedAt)
case State.Archived => shard.archive(sourceId, destinationId, uuid, updatedAt)
case State.Negative => shard.negate(sourceId, destinationId, uuid, updatedAt)
}
shardId
}
}
}