This repository has been archived by the owner on May 22, 2019. It is now read-only.
/
ManagerServiceSpec.scala
279 lines (230 loc) · 8.16 KB
/
ManagerServiceSpec.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
package com.twitter.gizzard.thrift
import org.specs.mock.{ClassMocker, JMocker}
import org.specs.Specification
import com.twitter.gizzard.thrift.conversions.Sequences._
import com.twitter.gizzard.thrift.conversions.ShardId._
import com.twitter.gizzard.thrift.conversions.ShardInfo._
import com.twitter.gizzard.thrift.conversions.CopyDestination._
import shards.{Busy, Shard}
import scheduler.{CopyJob, CopyJobFactory, JobScheduler, PrioritizingJobScheduler, JsonJob}
import scheduler.{CopyDestination => SCopyDestination}
object ManagerServiceSpec extends ConfiguredSpecification with JMocker with ClassMocker {
val nameServer = mock[nameserver.NameServer[Shard]]
val copier = mock[CopyJobFactory[Shard]]
val scheduler = mock[PrioritizingJobScheduler[JsonJob]]
val subScheduler = mock[JobScheduler[JsonJob]]
val copyScheduler = mock[JobScheduler[JsonJob]]
val manager = new ManagerService(nameServer, copier, scheduler, copyScheduler)
val shard = mock[Shard]
val thriftShardInfo1 = new thrift.ShardInfo(new thrift.ShardId("hostname", "table_prefix"),
"com.example.SqlShard", "INT UNSIGNED", "INT UNSIGNED", Busy.Normal.id)
val shardInfo1 = new shards.ShardInfo(new shards.ShardId("hostname", "table_prefix"),
"com.example.SqlShard", "INT UNSIGNED", "INT UNSIGNED", Busy.Normal)
val thriftShardInfo2 = new thrift.ShardInfo(new thrift.ShardId("other_table_prefix", "hostname"),
"com.example.SqlShard", "INT UNSIGNED", "INT UNSIGNED", Busy.Normal.id)
val shardInfo2 = new shards.ShardInfo(new shards.ShardId("other_table_prefix", "hostname"),
"com.example.SqlShard", "INT UNSIGNED", "INT UNSIGNED", Busy.Normal)
val hostname = "host1"
val classname = "com.example.Classname"
val tableId = 4
val forwarding = new nameserver.Forwarding(tableId, 0, shardInfo1.id)
val thriftForwarding = new thrift.Forwarding(tableId, 0, thriftShardInfo1.id)
"ManagerService" should {
"explode" in {
expect {
one(nameServer).createShard(shardInfo1) willThrow new shards.ShardException("blarg!")
}
manager.create_shard(thriftShardInfo1) must throwA[thrift.GizzardException]
}
"deliver messages for runtime exceptions" in {
var woot = false
expect {
one(nameServer).createShard(shardInfo1) willThrow new RuntimeException("Monkeys!")
}
try{
manager.create_shard(thriftShardInfo1)
} catch {
case e: thrift.GizzardException => {
e.getDescription mustEqual "Monkeys!"
woot = true
}
}
woot mustEqual true
}
"create_shard" in {
expect {
one(nameServer).createShard(shardInfo1)
}
manager.create_shard(thriftShardInfo1)
}
"get_shard" in {
expect {
one(nameServer).getShard(shardInfo1.id) willReturn shardInfo1
}
manager.get_shard(thriftShardInfo1.id) mustEqual thriftShardInfo1
}
"delete_shard" in {
expect {
one(nameServer).deleteShard(shardInfo1.id)
}
manager.delete_shard(thriftShardInfo1.id)
}
"add_link" in {
expect {
one(nameServer).addLink(shardInfo1.id, shardInfo2.id, 2)
}
manager.add_link(thriftShardInfo1.id, thriftShardInfo2.id, 2)
}
"remove_link" in {
expect {
one(nameServer).removeLink(shardInfo1.id, shardInfo2.id)
}
manager.remove_link(thriftShardInfo1.id, thriftShardInfo2.id)
}
"list_downward_links" in {
expect {
one(nameServer).listDownwardLinks(shardInfo1.id) willReturn List(shards.LinkInfo(shardInfo1.id, shardInfo2.id, 1))
}
manager.list_downward_links(thriftShardInfo1.id) mustEqual
List(new thrift.LinkInfo(thriftShardInfo1.id, thriftShardInfo2.id, 1)).toJavaList
}
"mark_shard_busy" in {
expect {
one(nameServer).markShardBusy(shardInfo1.id, Busy.Busy)
}
manager.mark_shard_busy(thriftShardInfo1.id, Busy.Busy.id)
}
"copy_shard" in {
val shardId1 = new shards.ShardId("hostname1", "table1")
val shardId2 = new shards.ShardId("hostname2", "table2")
val dests = List(SCopyDestination(shardId2, None))
val copyJob = mock[CopyJob[Shard]]
expect {
one(copier).apply(shardId1, dests) willReturn copyJob
one(copyScheduler).put(copyJob)
}
manager.copy_shard(shardId1.toThrift, shardId2.toThrift)
}
"multicopy_shard" in {
val shardId1 = new shards.ShardId("hostname1", "table1")
val shardId2a = new shards.ShardId("hostname2", "table2a")
val shardId2b = new shards.ShardId("hostname2", "table2b")
val dests = List(SCopyDestination(shardId2a, Some(0)), SCopyDestination(shardId2b, Some(100)))
val copyJob = mock[CopyJob[Shard]]
expect {
one(copier).apply(shardId1, dests) willReturn copyJob
one(copyScheduler).put(copyJob)
}
manager.addCopyFactory("fake.job.Name", copier)
manager.multicopy_shard("fake.job.Name", shardId1.toThrift, dests.map(_.toThrift).toJavaList)
}
"set_forwarding" in {
expect {
one(nameServer).setForwarding(forwarding)
}
manager.set_forwarding(thriftForwarding)
}
"replace_forwarding" in {
expect {
one(nameServer).replaceForwarding(shardInfo1.id, shardInfo2.id)
}
manager.replace_forwarding(thriftShardInfo1.id, thriftShardInfo2.id)
}
"get_forwarding" in {
expect {
one(nameServer).getForwarding(tableId, 0) willReturn forwarding
}
manager.get_forwarding(tableId, 0) mustEqual thriftForwarding
}
"get_forwarding_for_shard" in {
expect {
one(nameServer).getForwardingForShard(shardInfo1.id) willReturn forwarding
}
manager.get_forwarding_for_shard(thriftShardInfo1.id) mustEqual thriftForwarding
}
"get_forwardings" in {
expect {
one(nameServer).getForwardings() willReturn List(forwarding)
}
manager.get_forwardings() mustEqual List(thriftForwarding).toJavaList
}
"reload_config" in {
expect {
one(nameServer).reload()
}
manager.reload_config()
}
"find_current_forwarding" in {
expect {
one(nameServer).findCurrentForwarding(tableId, 23L) willReturn shard
one(shard).shardInfo willReturn shardInfo1
}
manager.find_current_forwarding(tableId, 23L) mustEqual thriftShardInfo1
}
"shards_for_hostname" in {
expect {
one(nameServer).shardsForHostname(hostname) willReturn List(shardInfo1)
}
manager.shards_for_hostname(hostname) mustEqual List(thriftShardInfo1).toJavaList
}
"get_busy_shards" in {
expect {
one(nameServer).getBusyShards() willReturn List(shardInfo1)
}
manager.get_busy_shards() mustEqual List(thriftShardInfo1).toJavaList
}
"list_upward_links" in {
expect {
one(nameServer).listUpwardLinks(shardInfo1.id) willReturn List(shards.LinkInfo(shardInfo2.id, shardInfo1.id, 1))
}
manager.list_upward_links(thriftShardInfo1.id) mustEqual List(new thrift.LinkInfo(thriftShardInfo2.id, thriftShardInfo1.id, 1)).toJavaList
}
// job management
"retry_errors" in {
expect {
one(scheduler).retryErrors()
}
manager.retry_errors()
}
"stop_writes" in {
expect {
one(scheduler).pause()
}
manager.stop_writes()
}
"resume_writes" in {
expect {
one(scheduler).resume()
}
manager.resume_writes()
}
"retry_errors_for" in {
expect {
one(scheduler).apply(3) willReturn subScheduler
one(subScheduler).retryErrors()
}
manager.retry_errors_for(3)
}
"stop_writes_for" in {
expect {
one(scheduler).apply(3) willReturn subScheduler
one(subScheduler).pause()
}
manager.stop_writes_for(3)
}
"resume_writes_for" in {
expect {
one(scheduler).apply(3) willReturn subScheduler
one(subScheduler).resume()
}
manager.resume_writes_for(3)
}
"is_writing" in {
expect {
one(scheduler).apply(3) willReturn subScheduler
one(subScheduler).isShutdown willReturn false
}
manager.is_writing(3) mustEqual true
}
}
}