aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/scala/parallelai/spyglass/hbase/testing/HBaseSaltTester.scala
blob: d75ff7bda96efa2ef1e2d7540642d73685065b9d (plain)
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
package parallelai.spyglass.hbase.testing

import parallelai.spyglass.base.JobBase
import parallelai.spyglass.hbase.HBaseConstants.{SplitType, SourceMode}

import com.twitter.scalding.{Tsv, IterableSource, Args, TextLine}
import parallelai.spyglass.hbase.{HBasePipeConversions, HBaseSource}
import cascading.tuple.Fields
import org.apache.log4j.{Logger, Level}
import cascading.tap.SinkMode
import cascading.pipe.Pipe
import org.apache.hadoop.hbase.io.ImmutableBytesWritable
import org.apache.hadoop.hbase.util.Bytes

class HBaseSaltTestSetup (args: Args) extends JobBase(args) with HBasePipeConversions {

  val isDebug = args.getOrElse("debug", "false").toBoolean

  if( isDebug ) { Logger.getRootLogger.setLevel(Level.DEBUG) }

  val TABLE_SCHEMA = List('key, 'salted, 'unsalted)

  val prefix = "0123456789"

  val quorum = args("quorum")

  val inVals = (00000 to 99999).toList.map(x => ("" + (x%10) + "_" + "%05d".format(x), "" + (x%10) + "_" + "%05d".format(x), "%05d".format(x)))

  def toIBW(pipe: Pipe, f: Fields): Pipe = {
    asList(f)
      .foldLeft(pipe){ (p, f) => {
      p.map(f.toString -> f.toString){ from: String =>
        Option(from).map(x => new ImmutableBytesWritable(Bytes.toBytes(x))).getOrElse(null)
      }}
    }
  }


  val input = IterableSource(inVals, TABLE_SCHEMA)
    .read
    .write(TextLine("saltTesting/Inputs"))

  val maker = toIBW(IterableSource(inVals, TABLE_SCHEMA).read, TABLE_SCHEMA)
    .write(new HBaseSource( "_TEST.SALT.01", quorum, 'key,
    TABLE_SCHEMA.tail.map((x: Symbol) => "data"),
    TABLE_SCHEMA.tail.map((x: Symbol) => new Fields(x.name)), sinkMode = SinkMode.UPDATE ))
}

class HBaseSaltTester (args: Args) extends JobBase(args) with HBasePipeConversions {

  val isDebug = args.getOrElse("debug", "false").toBoolean

  if( isDebug ) { Logger.getRootLogger.setLevel(Level.DEBUG) }

  val TABLE_SCHEMA = List('key, 'salted, 'unsalted)

  val prefix = "0123456789"

  val quorum = args("quorum")

  val sttKey = "01728"
  val stpKey = "03725"
  val sttKeyP = "8_01728"
  val stpKeyP = "5_03725"
  val listKey = List("01681", "01456")
  val listKeyP = List("0_01681", "6_01456")
  val noSttKey = "9999990"
  val noStpKey = "9999999"
  val noSttKeyP = "9_9999990"
  val noStpKeyP = "9_9999999"
  val noListKey = List("0123456", "6543210")
  val noListKeyP = List("6_0123456", "0_6543210")

  val splitType =  if(args.getOrElse("regional", "true").toBoolean) SplitType.REGIONAL else SplitType.GRANULAR

  val testName01 = "Scan All with NO useSalt"
  val list01 = (00000 to 99999).toList.map(x => ("" + (x%10) + "_" + "%05d".format(x), "" + (x%10) + "_" + "%05d".format(x), "%05d".format(x)))
  val hbase01 = new HBaseSource( "_TEST.SALT.01", quorum, 'key,
          TABLE_SCHEMA.tail.map((x: Symbol) => "data"),
          TABLE_SCHEMA.tail.map((x: Symbol) => new Fields(x.name)),
          sourceMode = SourceMode.SCAN_ALL,
          inputSplitType = splitType ).read
          .fromBytesWritable( TABLE_SCHEMA )
          .map(('key, 'salted, 'unsalted) -> 'testData) {x: (String, String, String) => List(x._1, x._2, x._3)}
          .project('testData)
          .write(TextLine("saltTesting/ScanAllNoSalt01"))
          .groupAll(group => group.toList[List[List[String]]]('testData -> 'testData))

  val testName02 = "Scan All with useSalt=true"
  val hbase02 = new HBaseSource( "_TEST.SALT.01", quorum, 'key,
          TABLE_SCHEMA.tail.map((x: Symbol) => "data"),
          TABLE_SCHEMA.tail.map((x: Symbol) => new Fields(x.name)),
          sourceMode = SourceMode.SCAN_ALL, useSalt = true,
          inputSplitType = splitType).read
          .fromBytesWritable( TABLE_SCHEMA )
          .map(('key, 'salted, 'unsalted) -> 'testData) {x: (String, String, String) => List(x._1, x._2, x._3)}
          .project('testData)
          .write(TextLine("saltTesting/ScanAllPlusSalt01"))
          .groupAll(group => group.toList[List[List[String]]]('testData -> 'testData))

  val testName03 = "Scan Range with NO useSalt"
  val list03 = (sttKey.toInt to stpKey.toInt).toList.map(x => ("" + (x%10) + "_" + "%05d".format(x), "" + (x%10) + "_" + "%05d".format(x), "%05d".format(x)))
  val hbase03 = new HBaseSource( "_TEST.SALT.01", quorum, 'key,
          TABLE_SCHEMA.tail.map((x: Symbol) => "data"),
          TABLE_SCHEMA.tail.map((x: Symbol) => new Fields(x.name)),
          sourceMode = SourceMode.SCAN_RANGE, startKey = sttKey, stopKey = stpKey, useSalt = true, prefixList = prefix,
          inputSplitType = splitType).read
          .fromBytesWritable(TABLE_SCHEMA )
          .map(('key, 'salted, 'unsalted) -> 'testData) {x: (String, String, String) => List(x._1, x._2, x._3)}
          .project('testData)
          .write(TextLine("saltTesting/ScanRangePlusSalt01"))
          .groupAll(group => group.toList[List[List[String]]]('testData -> 'testData))

  val testName04 = "Scan Range with useSalt=true"
  val hbase04 = new HBaseSource( "_TEST.SALT.01", quorum, 'key,
          TABLE_SCHEMA.tail.map((x: Symbol) => "data"),
          TABLE_SCHEMA.tail.map((x: Symbol) => new Fields(x.name)),
          sourceMode = SourceMode.SCAN_RANGE, startKey = sttKeyP, stopKey = stpKeyP,
          inputSplitType = splitType).read
          .fromBytesWritable(TABLE_SCHEMA )
          .map(('key, 'salted, 'unsalted) -> 'testData) {x: (String, String, String) => List(x._1, x._2, x._3)}
          .project('testData)
          .write(TextLine("saltTesting/ScanRangeNoSalt01"))
          .groupAll(group => group.toList[List[List[String]]]('testData -> 'testData))


  val testName05 = "Get List with NO useSalt"
  val list05 = listKey.map(x => x.toInt).map(x => ("" + (x%10) + "_" + "%05d".format(x), "" + (x%10) + "_" + "%05d".format(x), "%05d".format(x)))
  val hbase05 = new HBaseSource( "_TEST.SALT.01", quorum, 'key,
          TABLE_SCHEMA.tail.map((x: Symbol) => "data"),
          TABLE_SCHEMA.tail.map((x: Symbol) => new Fields(x.name)),
          sourceMode = SourceMode.GET_LIST, keyList = listKey, useSalt = true,
          inputSplitType = splitType).read
          .fromBytesWritable(TABLE_SCHEMA )
          .map(('key, 'salted, 'unsalted) -> 'testData) {x: (String, String, String) => List(x._1, x._2, x._3)}
          .project('testData)
          .write(TextLine("saltTesting/GetListPlusSalt01"))
          .groupAll(group => group.toList[List[List[String]]]('testData -> 'testData))

  val testName06 = "Get List with useSalt=true"
  val hbase06 = new HBaseSource( "_TEST.SALT.01", quorum, 'key,
          TABLE_SCHEMA.tail.map((x: Symbol) => "data"),
          TABLE_SCHEMA.tail.map((x: Symbol) => new Fields(x.name)),
          sourceMode = SourceMode.GET_LIST, keyList = listKeyP,
          inputSplitType = splitType).read
          .fromBytesWritable(TABLE_SCHEMA )
          .map(('key, 'salted, 'unsalted) -> 'testData) {x: (String, String, String) => List(x._1, x._2, x._3)}
          .project('testData)
          .write(TextLine("saltTesting/GetListNoSalt01"))
          .groupAll(group => group.toList[List[List[String]]]('testData -> 'testData))

  val testName08 = "Scan Range NO RESULTS"
  val hbase08 = new HBaseSource( "_TEST.SALT.01", quorum, 'key,
    TABLE_SCHEMA.tail.map((x: Symbol) => "data"),
    TABLE_SCHEMA.tail.map((x: Symbol) => new Fields(x.name)),
    sourceMode = SourceMode.SCAN_RANGE, startKey = noSttKey, stopKey = noStpKey, useSalt = true, prefixList = prefix,
    inputSplitType = splitType).read
    .fromBytesWritable(TABLE_SCHEMA )
    .map(('key, 'salted, 'unsalted) -> 'testData) {x: (String, String, String) => List(x._1, x._2, x._3)}
    .project('testData)
    .write(TextLine("saltTesting/ScanRangePlusSaltNoRes01"))
    .groupAll(group => group.toList[List[List[String]]]('testData -> 'testData))

  val testName09 = "Scan Range NO RESULT with useSalt=true"
  val hbase09 = new HBaseSource( "_TEST.SALT.01", quorum, 'key,
    TABLE_SCHEMA.tail.map((x: Symbol) => "data"),
    TABLE_SCHEMA.tail.map((x: Symbol) => new Fields(x.name)),
    sourceMode = SourceMode.SCAN_RANGE, startKey = noSttKeyP, stopKey = noStpKeyP,
    inputSplitType = splitType).read
    .fromBytesWritable(TABLE_SCHEMA )
    .map(('key, 'salted, 'unsalted) -> 'testData) {x: (String, String, String) => List(x._1, x._2, x._3)}
    .project('testData)
    .write(TextLine("saltTesting/ScanRangeNoSaltNoRes01"))
    .groupAll(group => group.toList[List[List[String]]]('testData -> 'testData))


  val testName10 = "Get List NO RESULT"
  val hbase10 = new HBaseSource( "_TEST.SALT.01", quorum, 'key,
    TABLE_SCHEMA.tail.map((x: Symbol) => "data"),
    TABLE_SCHEMA.tail.map((x: Symbol) => new Fields(x.name)),
    sourceMode = SourceMode.GET_LIST, keyList = noListKey, useSalt = true,
    inputSplitType = splitType).read
    .fromBytesWritable(TABLE_SCHEMA )
    .map(('key, 'salted, 'unsalted) -> 'testData) {x: (String, String, String) => List(x._1, x._2, x._3)}
    .project('testData)
    .write(TextLine("saltTesting/GetListPlusSaltNoRes01"))
    .groupAll(group => group.toList[List[List[String]]]('testData -> 'testData))

  val testName11 = "Get List NO RESULT with useSalt=true"
  val hbase11 = new HBaseSource( "_TEST.SALT.01", quorum, 'key,
    TABLE_SCHEMA.tail.map((x: Symbol) => "data"),
    TABLE_SCHEMA.tail.map((x: Symbol) => new Fields(x.name)),
    sourceMode = SourceMode.GET_LIST, keyList = noListKeyP,
    inputSplitType = splitType).read
    .fromBytesWritable(TABLE_SCHEMA )
    .map(('key, 'salted, 'unsalted) -> 'testData) {x: (String, String, String) => List(x._1, x._2, x._3)}
    .project('testData)
    .write(TextLine("saltTesting/GetListNoSaltNoRes01"))
    .groupAll(group => group.toList[List[List[String]]]('testData -> 'testData))



//  (
////      getTestResultPipe(getExpectedPipe(list01), hbase01, testName01) ++
////      getTestResultPipe(getExpectedPipe(list01), hbase02, testName02) ++
//      getTestResultPipe(getExpectedPipe(list03), hbase03, testName03) ++
//      getTestResultPipe(getExpectedPipe(list03), hbase04, testName03) ++
//      getTestResultPipe(getExpectedPipe(list05), hbase05, testName05) ++
//      getTestResultPipe(getExpectedPipe(list05), hbase06, testName06) ++
//      assertPipeIsEmpty(hbase08, testName08) ++
//      assertPipeIsEmpty(hbase09, testName09) ++
//      assertPipeIsEmpty(hbase10, testName10) ++
//      assertPipeIsEmpty(hbase11, testName11)
//    ).groupAll { group =>
//    group.sortBy('testName)
//  }
//    .write(Tsv("saltTesting/FinalTestResults"))

  /**
   * We assume the pipe is empty
   *
   * We concatenate with a header - if the resulting size is 1
   * then the original size was 0 - then the pipe was empty :)
   *
   * The result is then returned in a Pipe
   */
  def assertPipeIsEmpty ( hbasePipe : Pipe , testName:String) : Pipe = {
    val headerPipe = IterableSource(List(testName), 'testData)
    val concatenation = ( hbasePipe ++ headerPipe ).groupAll{ group =>
      group.size('size)
    }
      .project('size)

    val result =
      concatenation
        .mapTo('size -> ('testName, 'result, 'expectedData, 'testData)) { x:String => {
        if (x == "1") {
          (testName, "Success", "", "")
        } else {
          (testName, "Test Failed", "", "")
        }
      }
      }

    result
  }

  /**
   * Methods receives 2 pipes - and projects the results of testing
   *
   * expectedPipe  should have a column 'expecteddata
   * realHBasePipe should have a column 'hbasedata
   */
  def getTestResultPipe ( expectedPipe:Pipe , realHBasePipe:Pipe, testName: String ): Pipe = {
    val results = expectedPipe.insert('testName , testName)
      .joinWithTiny('testName -> 'testName, realHBasePipe.insert('testName , testName))
      .map(('expectedData, 'testData)->'result) { x:(String,String) =>
      if (x._1.equals(x._2))
        "Success"
      else
        "Test Failed"
    }
      .project('testName, 'result, 'expectedData, 'testData)
    results
  }

  /**
   *
   */
  def getExpectedPipe ( expectedList: List[(String,String, String)]) : Pipe = {
      IterableSource(expectedList, TABLE_SCHEMA)
        .map(('key, 'salted, 'unsalted) -> 'expectedData) {x: (String, String, String) => List(x._1, x._2, x._3)}
        .project('expectedData)
        .groupAll(group => group.toList[List[List[String]]]('expectedData -> 'expectedData))
  }

}

class HBaseSaltTestShutdown (args: Args) extends JobBase(args) with HBasePipeConversions {

  val isDebug = args.getOrElse("debug", "false").toBoolean

  if( isDebug ) { Logger.getRootLogger.setLevel(Level.DEBUG) }

  val TABLE_SCHEMA = List('key, 'salted, 'unsalted)

  val prefix = "0123456789"

  val quorum = args("quorum")

  val inVals = (00000 to 99999).toList.map(x => ("" + (x%10) + "_" + "%05d".format(x), "" + (x%10) + "_" + "%05d".format(x), "%05d".format(x)))

  def toIBW(pipe: Pipe, f: Fields): Pipe = {
    asList(f)
      .foldLeft(pipe){ (p, f) => {
      p.map(f.toString -> f.toString){ from: String =>
        Option(from).map(x => new ImmutableBytesWritable(Bytes.toBytes(x))).getOrElse(null)
      }}
    }
  }

  val input = IterableSource(inVals, TABLE_SCHEMA).read

  val eraser = toIBW(input, TABLE_SCHEMA)
    .write(new HBaseSource( "_TEST.SALT.01", quorum, 'key,
      TABLE_SCHEMA.tail.map((x: Symbol) => "data"),
      TABLE_SCHEMA.tail.map((x: Symbol) => new Fields(x.name)), sinkMode = SinkMode.REPLACE ))
}