aboutsummaryrefslogtreecommitdiff
path: root/graphx/src/test/scala/org/apache/spark/graphx/SerializerSuite.scala
blob: 73438d95359626338b794e1cbea4ee7a79f119cf (plain) (blame)
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
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.spark.graphx

import java.io.{EOFException, ByteArrayInputStream, ByteArrayOutputStream}

import scala.util.Random

import org.scalatest.FunSuite

import org.apache.spark._
import org.apache.spark.graphx.impl._
import org.apache.spark.graphx.impl.MsgRDDFunctions._
import org.apache.spark.serializer.SerializationStream


class SerializerSuite extends FunSuite with LocalSparkContext {

  test("IntVertexBroadcastMsgSerializer") {
    val outMsg = new VertexBroadcastMsg[Int](3, 4, 5)
    val bout = new ByteArrayOutputStream
    val outStrm = new IntVertexBroadcastMsgSerializer().newInstance().serializeStream(bout)
    outStrm.writeObject(outMsg)
    outStrm.writeObject(outMsg)
    bout.flush()
    val bin = new ByteArrayInputStream(bout.toByteArray)
    val inStrm = new IntVertexBroadcastMsgSerializer().newInstance().deserializeStream(bin)
    val inMsg1: VertexBroadcastMsg[Int] = inStrm.readObject()
    val inMsg2: VertexBroadcastMsg[Int] = inStrm.readObject()
    assert(outMsg.vid === inMsg1.vid)
    assert(outMsg.vid === inMsg2.vid)
    assert(outMsg.data === inMsg1.data)
    assert(outMsg.data === inMsg2.data)

    intercept[EOFException] {
      inStrm.readObject()
    }
  }

  test("LongVertexBroadcastMsgSerializer") {
    val outMsg = new VertexBroadcastMsg[Long](3, 4, 5)
    val bout = new ByteArrayOutputStream
    val outStrm = new LongVertexBroadcastMsgSerializer().newInstance().serializeStream(bout)
    outStrm.writeObject(outMsg)
    outStrm.writeObject(outMsg)
    bout.flush()
    val bin = new ByteArrayInputStream(bout.toByteArray)
    val inStrm = new LongVertexBroadcastMsgSerializer().newInstance().deserializeStream(bin)
    val inMsg1: VertexBroadcastMsg[Long] = inStrm.readObject()
    val inMsg2: VertexBroadcastMsg[Long] = inStrm.readObject()
    assert(outMsg.vid === inMsg1.vid)
    assert(outMsg.vid === inMsg2.vid)
    assert(outMsg.data === inMsg1.data)
    assert(outMsg.data === inMsg2.data)

    intercept[EOFException] {
      inStrm.readObject()
    }
  }

  test("DoubleVertexBroadcastMsgSerializer") {
    val outMsg = new VertexBroadcastMsg[Double](3, 4, 5.0)
    val bout = new ByteArrayOutputStream
    val outStrm = new DoubleVertexBroadcastMsgSerializer().newInstance().serializeStream(bout)
    outStrm.writeObject(outMsg)
    outStrm.writeObject(outMsg)
    bout.flush()
    val bin = new ByteArrayInputStream(bout.toByteArray)
    val inStrm = new DoubleVertexBroadcastMsgSerializer().newInstance().deserializeStream(bin)
    val inMsg1: VertexBroadcastMsg[Double] = inStrm.readObject()
    val inMsg2: VertexBroadcastMsg[Double] = inStrm.readObject()
    assert(outMsg.vid === inMsg1.vid)
    assert(outMsg.vid === inMsg2.vid)
    assert(outMsg.data === inMsg1.data)
    assert(outMsg.data === inMsg2.data)

    intercept[EOFException] {
      inStrm.readObject()
    }
  }

  test("IntAggMsgSerializer") {
    val outMsg = (4: VertexId, 5)
    val bout = new ByteArrayOutputStream
    val outStrm = new IntAggMsgSerializer().newInstance().serializeStream(bout)
    outStrm.writeObject(outMsg)
    outStrm.writeObject(outMsg)
    bout.flush()
    val bin = new ByteArrayInputStream(bout.toByteArray)
    val inStrm = new IntAggMsgSerializer().newInstance().deserializeStream(bin)
    val inMsg1: (VertexId, Int) = inStrm.readObject()
    val inMsg2: (VertexId, Int) = inStrm.readObject()
    assert(outMsg === inMsg1)
    assert(outMsg === inMsg2)

    intercept[EOFException] {
      inStrm.readObject()
    }
  }

  test("LongAggMsgSerializer") {
    val outMsg = (4: VertexId, 1L << 32)
    val bout = new ByteArrayOutputStream
    val outStrm = new LongAggMsgSerializer().newInstance().serializeStream(bout)
    outStrm.writeObject(outMsg)
    outStrm.writeObject(outMsg)
    bout.flush()
    val bin = new ByteArrayInputStream(bout.toByteArray)
    val inStrm = new LongAggMsgSerializer().newInstance().deserializeStream(bin)
    val inMsg1: (VertexId, Long) = inStrm.readObject()
    val inMsg2: (VertexId, Long) = inStrm.readObject()
    assert(outMsg === inMsg1)
    assert(outMsg === inMsg2)

    intercept[EOFException] {
      inStrm.readObject()
    }
  }

  test("DoubleAggMsgSerializer") {
    val outMsg = (4: VertexId, 5.0)
    val bout = new ByteArrayOutputStream
    val outStrm = new DoubleAggMsgSerializer().newInstance().serializeStream(bout)
    outStrm.writeObject(outMsg)
    outStrm.writeObject(outMsg)
    bout.flush()
    val bin = new ByteArrayInputStream(bout.toByteArray)
    val inStrm = new DoubleAggMsgSerializer().newInstance().deserializeStream(bin)
    val inMsg1: (VertexId, Double) = inStrm.readObject()
    val inMsg2: (VertexId, Double) = inStrm.readObject()
    assert(outMsg === inMsg1)
    assert(outMsg === inMsg2)

    intercept[EOFException] {
      inStrm.readObject()
    }
  }

  test("TestShuffleVertexBroadcastMsg") {
    withSpark { sc =>
      val bmsgs = sc.parallelize(0 until 100, 10).map { pid =>
        new VertexBroadcastMsg[Int](pid, pid, pid)
      }
      bmsgs.partitionBy(new HashPartitioner(3)).collect()
    }
  }

  test("variable long encoding") {
    def testVarLongEncoding(v: Long, optimizePositive: Boolean) {
      val bout = new ByteArrayOutputStream
      val stream = new ShuffleSerializationStream(bout) {
        def writeObject[T](t: T): SerializationStream = {
          writeVarLong(t.asInstanceOf[Long], optimizePositive = optimizePositive)
          this
        }
      }
      stream.writeObject(v)

      val bin = new ByteArrayInputStream(bout.toByteArray)
      val dstream = new ShuffleDeserializationStream(bin) {
        def readObject[T](): T = {
          readVarLong(optimizePositive).asInstanceOf[T]
        }
      }
      val read = dstream.readObject[Long]()
      assert(read === v)
    }

    // Test all variable encoding code path (each branch uses 7 bits, i.e. 1L << 7 difference)
    val d = Random.nextLong() % 128
    Seq[Long](0, 1L << 0 + d, 1L << 7 + d, 1L << 14 + d, 1L << 21 + d, 1L << 28 + d, 1L << 35 + d,
      1L << 42 + d, 1L << 49 + d, 1L << 56 + d, 1L << 63 + d).foreach { number =>
      testVarLongEncoding(number, optimizePositive = false)
      testVarLongEncoding(number, optimizePositive = true)
      testVarLongEncoding(-number, optimizePositive = false)
      testVarLongEncoding(-number, optimizePositive = true)
    }
  }
}