-
Notifications
You must be signed in to change notification settings - Fork 1
/
lambda-serialization.html
452 lines (379 loc) · 34.8 KB
/
lambda-serialization.html
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
<!DOCTYPE html>
<html lang="en" prefix="og: http://ogp.me/ns# fb: https://www.facebook.com/2008/fbml">
<head>
<title>Lambda serialization - Stackdiver as a Service</title>
<!-- Using the latest rendering mode for IE -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://www.lyh.me/lambda-serialization.html">
<meta name="author" content="Neville Li" />
<meta name="keywords" content="scala,scio,data" />
<meta name="description" content="Lambda serialization is one of the more confusion issues in distributed data processing in Scala. No matter which framework you choose, whether it’s Scalding, Spark, Flink or Scio, sooner or later you’ll be hit by the dreaded NotSerializableException. In this post we’ll take a closer look at the common causes and solutions to this problem. Setup To demonstrate the problem, first we need a minimal setup that minics the behavior of a distributed data processing system. We start with a utility method that roundtrips an object throguh Java serialization. Anonymous functions, or lambdas, in such systems are serialized so that they can be distributed to workers for parallel processing. import java.io.{ByteArrayInputStream, ByteArrayOutputStream, ObjectInputStream, ObjectOutputStream} object SerDeUtil { def serDe[T](obj: T): T = { val buffer = new ByteArrayOutputStream() val out = new ObjectOutputStream(buffer) out.writeObject(obj) out.close() val in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray)) in.readObject().asInstanceOf[T] } } Next we create a bare minimal Collection[T] type that mimics an abstract distributed data set, akin to TypedPipe, RDD, or SCollection in Scalding, Spark or Scio respectively. Our implementation is backed by a local in-memory Seq[T] but does pass the function f through serialization like …" />
<meta property="og:site_name" content="Stackdiver as a Service" />
<meta property="og:type" content="article"/>
<meta property="og:title" content="Lambda serialization"/>
<meta property="og:url" content="https://www.lyh.me/lambda-serialization.html"/>
<meta property="og:description" content="Lambda serialization is one of the more confusion issues in distributed data processing in Scala. No matter which framework you choose, whether it’s Scalding, Spark, Flink or Scio, sooner or later you’ll be hit by the dreaded NotSerializableException. In this post we’ll take a closer look at the common causes and solutions to this problem. Setup To demonstrate the problem, first we need a minimal setup that minics the behavior of a distributed data processing system. We start with a utility method that roundtrips an object throguh Java serialization. Anonymous functions, or lambdas, in such systems are serialized so that they can be distributed to workers for parallel processing. import java.io.{ByteArrayInputStream, ByteArrayOutputStream, ObjectInputStream, ObjectOutputStream} object SerDeUtil { def serDe[T](obj: T): T = { val buffer = new ByteArrayOutputStream() val out = new ObjectOutputStream(buffer) out.writeObject(obj) out.close() val in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray)) in.readObject().asInstanceOf[T] } } Next we create a bare minimal Collection[T] type that mimics an abstract distributed data set, akin to TypedPipe, RDD, or SCollection in Scalding, Spark or Scio respectively. Our implementation is backed by a local in-memory Seq[T] but does pass the function f through serialization like …"/>
<meta property="article:published_time" content="2017-08-01" />
<meta property="article:section" content="code" />
<meta property="article:tag" content="scala" />
<meta property="article:tag" content="scio" />
<meta property="article:tag" content="data" />
<meta property="article:author" content="Neville Li" />
<!-- Bootstrap -->
<link rel="stylesheet" href="https://www.lyh.me/theme/css/bootstrap.min.css" type="text/css"/>
<link href="https://www.lyh.me/theme/css/font-awesome.min.css" rel="stylesheet">
<link href="https://www.lyh.me/theme/css/pygments/monokai.css" rel="stylesheet">
<link href="https://www.lyh.me/theme/css/typogrify.css" rel="stylesheet">
<link rel="stylesheet" href="https://www.lyh.me/theme/css/style.css" type="text/css"/>
<link href="https://www.lyh.me/feeds/all.atom.xml" type="application/atom+xml" rel="alternate"
title="Stackdiver as a Service ATOM Feed"/>
<link href="https://www.lyh.me/feeds/code.atom.xml" type="application/atom+xml" rel="alternate"
title="Stackdiver as a Service code ATOM Feed"/>
</head>
<body>
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="https://www.lyh.me/" class="navbar-brand">
Stackdiver as a Service </a>
</div>
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav">
<li><a href="https://www.lyh.me/pages/about-me.html">
About Me
</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
</div> <!-- /.navbar -->
<!-- Banner -->
<!-- End Banner -->
<!-- Content Container -->
<div class="container">
<div class="row">
<div class="col-sm-9">
<section id="content">
<article>
<header class="page-header">
<h1>
<a href="https://www.lyh.me/lambda-serialization.html"
rel="bookmark"
title="Permalink to Lambda serialization">
Lambda serialization
</a>
</h1>
</header>
<div class="entry-content">
<div class="panel">
<div class="panel-body">
<footer class="post-info">
<span class="label label-default">Date</span>
<span class="published">
<i class="fa fa-calendar"></i><time datetime="2017-08-01T13:51:00-04:00"> Tue 01 August 2017</time>
</span>
<span class="label label-default">Category</span>
<a href="https://www.lyh.me/category/code.html">code</a>
<span class="label label-default">Tags</span>
<a href="https://www.lyh.me/tag/scala.html">scala</a>
/
<a href="https://www.lyh.me/tag/scio.html">scio</a>
/
<a href="https://www.lyh.me/tag/data.html">data</a>
</footer><!-- /.post-info --> </div>
</div>
<p>Lambda serialization is one of the more confusion issues in distributed data processing in Scala. No matter which framework you choose, whether it’s Scalding, Spark, Flink or Scio, sooner or later you’ll be hit by the dreaded <code>NotSerializableException</code>. In this post we’ll take a closer look at the common causes and solutions to this problem.</p>
<h2>Setup</h2>
<p>To demonstrate the problem, first we need a minimal setup that minics the behavior of a distributed data processing system. We start with a utility method that roundtrips an object throguh Java serialization. Anonymous functions, or lambdas, in such systems are serialized so that they can be distributed to workers for parallel processing.</p>
<div class="highlight"><pre><span></span><code><span class="k">import</span> <span class="nn">java.io.</span><span class="o">{</span><span class="nc">ByteArrayInputStream</span><span class="o">,</span> <span class="nc">ByteArrayOutputStream</span><span class="o">,</span> <span class="nc">ObjectInputStream</span><span class="o">,</span> <span class="nc">ObjectOutputStream</span><span class="o">}</span>
<span class="k">object</span> <span class="nc">SerDeUtil</span> <span class="o">{</span>
<span class="k">def</span> <span class="n">serDe</span><span class="o">[</span><span class="kt">T</span><span class="o">](</span><span class="n">obj</span><span class="k">:</span> <span class="kt">T</span><span class="o">)</span><span class="k">:</span> <span class="kt">T</span> <span class="o">=</span> <span class="o">{</span>
<span class="k">val</span> <span class="n">buffer</span> <span class="k">=</span> <span class="k">new</span> <span class="nc">ByteArrayOutputStream</span><span class="o">()</span>
<span class="k">val</span> <span class="n">out</span> <span class="k">=</span> <span class="k">new</span> <span class="nc">ObjectOutputStream</span><span class="o">(</span><span class="n">buffer</span><span class="o">)</span>
<span class="n">out</span><span class="o">.</span><span class="n">writeObject</span><span class="o">(</span><span class="n">obj</span><span class="o">)</span>
<span class="n">out</span><span class="o">.</span><span class="n">close</span><span class="o">()</span>
<span class="k">val</span> <span class="n">in</span> <span class="k">=</span> <span class="k">new</span> <span class="nc">ObjectInputStream</span><span class="o">(</span><span class="k">new</span> <span class="nc">ByteArrayInputStream</span><span class="o">(</span><span class="n">buffer</span><span class="o">.</span><span class="n">toByteArray</span><span class="o">))</span>
<span class="n">in</span><span class="o">.</span><span class="n">readObject</span><span class="o">().</span><span class="n">asInstanceOf</span><span class="o">[</span><span class="kt">T</span><span class="o">]</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div>
<p>Next we create a bare minimal <code>Collection[T]</code> type that mimics an abstract distributed data set, akin to <code>TypedPipe</code>, <code>RDD</code>, or <code>SCollection</code> in Scalding, Spark or Scio respectively. Our implementation is backed by a local in-memory <code>Seq[T]</code> but does pass the function <code>f</code> through serialization like a distributed system.</p>
<div class="highlight"><pre><span></span><code><span class="k">class</span> <span class="nc">Collection</span><span class="o">[</span><span class="kt">T</span><span class="o">](</span><span class="k">private</span> <span class="k">val</span> <span class="n">xs</span><span class="k">:</span> <span class="kt">Seq</span><span class="o">[</span><span class="kt">T</span><span class="o">])</span> <span class="o">{</span>
<span class="k">def</span> <span class="n">map</span><span class="o">[</span><span class="kt">U</span><span class="o">](</span><span class="n">f</span><span class="k">:</span> <span class="kt">T</span> <span class="o">=></span> <span class="n">U</span><span class="o">)</span><span class="k">:</span> <span class="kt">Collection</span><span class="o">[</span><span class="kt">U</span><span class="o">]</span> <span class="k">=</span> <span class="o">{</span>
<span class="k">val</span> <span class="n">g</span> <span class="k">=</span> <span class="nc">SerDeUtil</span><span class="o">.</span><span class="n">serDe</span><span class="o">(</span><span class="n">f</span><span class="o">)</span>
<span class="k">new</span> <span class="nc">Collection</span><span class="o">[</span><span class="kt">U</span><span class="o">](</span><span class="n">xs</span><span class="o">.</span><span class="n">map</span><span class="o">(</span><span class="n">g</span><span class="o">))</span>
<span class="o">}</span>
<span class="k">override</span> <span class="k">def</span> <span class="n">toString</span><span class="k">:</span> <span class="kt">String</span> <span class="o">=</span> <span class="n">xs</span><span class="o">.</span><span class="n">toString</span><span class="o">()</span>
<span class="o">}</span>
<span class="k">object</span> <span class="nc">Collection</span> <span class="o">{</span>
<span class="k">def</span> <span class="n">apply</span><span class="o">[</span><span class="kt">T</span><span class="o">](</span><span class="n">xs</span><span class="k">:</span> <span class="kt">T*</span><span class="o">)</span><span class="k">:</span> <span class="kt">Collection</span><span class="o">[</span><span class="kt">T</span><span class="o">]</span> <span class="k">=</span> <span class="k">new</span> <span class="nc">Collection</span><span class="o">[</span><span class="kt">T</span><span class="o">](</span><span class="n">xs</span><span class="o">)</span>
<span class="o">}</span>
</code></pre></div>
<h2>Problems</h2>
<p>The base case works perfectly fine. The anonymous function <code>_ + 1</code> gets translated to a <code>Function1[Int, Int]</code> instance. Since Scala functions extend <code>Serializable</code> and it doesn’t refer to any closure, the instance is serializable by default.</p>
<div class="highlight"><pre><span></span><code><span class="k">object</span> <span class="nc">FnSerDe</span> <span class="o">{</span>
<span class="k">def</span> <span class="n">main</span><span class="o">(</span><span class="n">args</span><span class="k">:</span> <span class="kt">Array</span><span class="o">[</span><span class="kt">String</span><span class="o">])</span><span class="k">:</span> <span class="kt">Unit</span> <span class="o">=</span> <span class="o">{</span>
<span class="cm">/*</span>
<span class="cm"> _ + 1 =></span>
<span class="cm"> new Function1[Int, Int] {</span>
<span class="cm"> override def apply(x: Int): Int = x + 1</span>
<span class="cm"> }</span>
<span class="cm"> */</span>
<span class="nc">Collection</span><span class="o">(</span><span class="mi">1</span><span class="o">,</span> <span class="mi">2</span><span class="o">,</span> <span class="mi">3</span><span class="o">).</span><span class="n">map</span><span class="o">(</span><span class="k">_</span> <span class="o">+</span> <span class="mi">1</span><span class="o">)</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div>
<p>The next case also works fine. <code>plus</code> is a static method in <code>object FnSerDe</code> so it doesn’t go through serialization. Instead the Scala compiler generates a <code>Function1[Int, Int]</code> instance to wrap it.</p>
<div class="highlight"><pre><span></span><code><span class="k">object</span> <span class="nc">FnSerDe</span> <span class="o">{</span>
<span class="k">def</span> <span class="n">plus</span><span class="o">(</span><span class="n">x</span><span class="k">:</span> <span class="kt">Int</span><span class="o">)</span><span class="k">:</span> <span class="kt">Int</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
<span class="k">def</span> <span class="n">main</span><span class="o">(</span><span class="n">args</span><span class="k">:</span> <span class="kt">Array</span><span class="o">[</span><span class="kt">String</span><span class="o">])</span><span class="k">:</span> <span class="kt">Unit</span> <span class="o">=</span> <span class="o">{</span>
<span class="cm">/*</span>
<span class="cm"> new Function1[Int, Int] {</span>
<span class="cm"> override def apply(x: Int): Int = FnSerDe.plus(x)</span>
<span class="cm"> }</span>
<span class="cm"> */</span>
<span class="nc">Collection</span><span class="o">(</span><span class="mi">1</span><span class="o">,</span> <span class="mi">2</span><span class="o">,</span> <span class="mi">3</span><span class="o">).</span><span class="n">map</span><span class="o">(</span><span class="n">plus</span><span class="o">)</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div>
<p>The next example is where the problem begins. By passing <code>new A().plus</code> into <code>map</code>, the <code>new A()</code> instance got pulled into the closure of the automatically generated <code>Function1</code> and needs to be serializable.</p>
<div class="highlight"><pre><span></span><code><span class="k">object</span> <span class="nc">FnSerDe</span> <span class="o">{</span>
<span class="k">def</span> <span class="n">main</span><span class="o">(</span><span class="n">args</span><span class="k">:</span> <span class="kt">Array</span><span class="o">[</span><span class="kt">String</span><span class="o">])</span><span class="k">:</span> <span class="kt">Unit</span> <span class="o">=</span> <span class="o">{</span>
<span class="cm">/*</span>
<span class="cm"> val a = new A()</span>
<span class="cm"> new Function1[Int, Int] {</span>
<span class="cm"> override def apply(x: Int): Int = a.plus(x) // a from closure</span>
<span class="cm"> }</span>
<span class="cm"> */</span>
<span class="nc">Collection</span><span class="o">(</span><span class="mi">1</span><span class="o">,</span> <span class="mi">2</span><span class="o">,</span> <span class="mi">3</span><span class="o">).</span><span class="n">map</span><span class="o">(</span><span class="k">new</span> <span class="n">A</span><span class="o">().</span><span class="n">plus</span><span class="o">)</span> <span class="c1">// A is not serializable</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="k">class</span> <span class="nc">A</span> <span class="o">{</span>
<span class="k">def</span> <span class="n">plus</span><span class="o">(</span><span class="n">x</span><span class="k">:</span> <span class="kt">Int</span><span class="o">)</span><span class="k">:</span> <span class="kt">Int</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
<span class="o">}</span>
</code></pre></div>
<p>This can be easily fixed by making <code>A</code> extend <code>Serializable</code>.</p>
<div class="highlight"><pre><span></span><code><span class="k">class</span> <span class="nc">A</span> <span class="k">extends</span> <span class="nc">Serializable</span> <span class="o">{</span>
<span class="k">def</span> <span class="n">plus</span><span class="o">(</span><span class="n">x</span><span class="k">:</span> <span class="kt">Int</span><span class="o">)</span><span class="k">:</span> <span class="kt">Int</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
<span class="o">}</span>
</code></pre></div>
<p>Things get more complicated as our code grows. In the next example, even though <code>A</code> extends <code>Serializable</code> and its member <code>val b</code> is never used, it fails serialization since <code>B</code> doesn’t extend <code>Serializable</code>.</p>
<div class="highlight"><pre><span></span><code><span class="k">class</span> <span class="nc">A</span> <span class="k">extends</span> <span class="nc">Serializable</span> <span class="o">{</span>
<span class="k">val</span> <span class="n">b</span> <span class="k">=</span> <span class="k">new</span> <span class="n">B</span>
<span class="k">def</span> <span class="n">plus</span><span class="o">(</span><span class="n">x</span><span class="k">:</span> <span class="kt">Int</span><span class="o">)</span><span class="k">:</span> <span class="kt">Int</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
<span class="o">}</span>
<span class="k">class</span> <span class="nc">B</span>
</code></pre></div>
<p>We can apply the same trick we just learned by making <code>B</code> extend <code>Serializable</code> as well.</p>
<div class="highlight"><pre><span></span><code><span class="k">class</span> <span class="nc">B</span> <span class="k">extends</span> <span class="nc">Serializable</span>
</code></pre></div>
<p>However this gets tedious if the code base is large, and doesn’t work with third-party code we have no control of. Since <code>b</code> is never used, we can mark it as <code>@transient</code> so that Java serializer ignores it.</p>
<div class="highlight"><pre><span></span><code><span class="k">class</span> <span class="nc">A</span> <span class="k">extends</span> <span class="nc">Serializable</span> <span class="o">{</span>
<span class="nd">@transient</span> <span class="k">val</span> <span class="n">b</span> <span class="k">=</span> <span class="k">new</span> <span class="n">B</span> <span class="c1">// becomes null after ser/de</span>
<span class="k">def</span> <span class="n">plus</span><span class="o">(</span><span class="n">x</span><span class="k">:</span> <span class="kt">Int</span><span class="o">)</span><span class="k">:</span> <span class="kt">Int</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
<span class="o">}</span>
</code></pre></div>
<p>This won’t work if we do need to access <code>b</code> in the function though, since transient members become <code>null</code> after serialization, and we get an NullPointerException instead.</p>
<div class="highlight"><pre><span></span><code><span class="k">class</span> <span class="nc">A</span> <span class="k">extends</span> <span class="nc">Serializable</span> <span class="o">{</span>
<span class="nd">@transient</span> <span class="k">val</span> <span class="n">b</span> <span class="k">=</span> <span class="k">new</span> <span class="n">B</span> <span class="c1">// becomes null after ser/de</span>
<span class="k">def</span> <span class="n">plus</span><span class="o">(</span><span class="n">x</span><span class="k">:</span> <span class="kt">Int</span><span class="o">)</span><span class="k">:</span> <span class="kt">Int</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="n">b</span><span class="o">.</span><span class="n">one</span> <span class="c1">// b.one throws NullPointerException</span>
<span class="o">}</span>
<span class="k">class</span> <span class="nc">B</span> <span class="o">{</span>
<span class="k">val</span> <span class="n">one</span> <span class="k">=</span> <span class="mi">1</span>
<span class="o">}</span>
</code></pre></div>
<p>Not all is lost though, we can still fix this by making <code>b</code> a <code>@transient lazy val</code>, so that <code>b</code> gets re-initialized with <code>new B</code> when it’s first accessed after going through ser/de cycle.</p>
<div class="highlight"><pre><span></span><code><span class="k">class</span> <span class="nc">A</span> <span class="k">extends</span> <span class="nc">Serializable</span> <span class="o">{</span>
<span class="nd">@transient</span> <span class="k">lazy</span> <span class="k">val</span> <span class="n">b</span> <span class="k">=</span> <span class="k">new</span> <span class="n">B</span> <span class="c1">// new B called again after ser/de</span>
<span class="k">def</span> <span class="n">plus</span><span class="o">(</span><span class="n">x</span><span class="k">:</span> <span class="kt">Int</span><span class="o">)</span><span class="k">:</span> <span class="kt">Int</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="n">b</span><span class="o">.</span><span class="n">one</span>
<span class="o">}</span>
</code></pre></div>
<p>We can also use this trick to make sure a non-serializable object survives ser/de, by making it <code>@transient lazy</code> while also keeping a serializable representation of it.</p>
<div class="highlight"><pre><span></span><code><span class="k">class</span> <span class="nc">A</span> <span class="k">extends</span> <span class="nc">Serializable</span> <span class="o">{</span>
<span class="nd">@transient</span> <span class="k">lazy</span> <span class="k">val</span> <span class="n">b</span> <span class="k">=</span> <span class="k">new</span> <span class="n">B</span><span class="o">(</span><span class="n">arg</span><span class="o">)</span> <span class="c1">// B is not serializable</span>
<span class="k">val</span> <span class="n">arg</span> <span class="k">=</span> <span class="mi">1</span> <span class="c1">// but arg: Int is and can be used to re-initialize b</span>
<span class="k">def</span> <span class="n">plus</span><span class="o">(</span><span class="n">x</span><span class="k">:</span> <span class="kt">Int</span><span class="o">)</span><span class="k">:</span> <span class="kt">Int</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="n">b</span><span class="o">.</span><span class="n">one</span>
<span class="o">}</span>
<span class="c1">// non-serializable third-party code, but requires a serializable Int to initialize</span>
<span class="k">class</span> <span class="nc">B</span><span class="o">(</span><span class="n">arg</span><span class="k">:</span> <span class="kt">Int</span><span class="o">)</span> <span class="o">{</span>
<span class="k">val</span> <span class="n">one</span> <span class="k">=</span> <span class="n">arg</span>
<span class="o">}</span>
</code></pre></div>
<h2>Summary</h2>
<p>This post only covered a tiny fraction of the broad topic of lambda serialization. Some systems like Spark and Scio uses closure cleaner to either remove non-serializable fields, or convert them to <code>Serializable</code> before serializing a lambda. The issue is made more complex by Scala 2.12 and Java 8 lambdas, since they’re now dynamically generated and doesn’t work with traditionally byte code manipulation used in these cleaners. Check out the following links for more about closure serialization.</p>
<ul>
<li><a href="https://github.com/twitter/chill/blob/develop/chill-scala/src/main/scala/com/twitter/chill/Externalizer.scala">Externalizer</a> - in Twitter chill, which wraps a non-serializable object and uses Kryo to handle serialization</li>
<li><a href="https://github.com/scala/bug/issues/10232">https://github.com/scala/bug/issues/10232</a> - Invalid bytecode generated for deserialization when many lambdas are declared</li>
<li><a href="https://github.com/scala/bug/issues/10233">https://github.com/scala/bug/issues/10233</a> - NotSerializableException on object methods due to lambda closure</li>
<li><a href="https://issues.apache.org/jira/browse/SPARK-14540">https://issues.apache.org/jira/browse/<span class="caps">SPARK</span>-14540</a> - Support Scala 2.12 closures and Java 8 lambdas in ClosureCleaner</li>
<li><a href="https://github.com/twitter/chill/pull/292">https://github.com/twitter/chill/pull/292</a> - Clean closures transitively</li>
</ul>
</div>
<!-- /.entry-content -->
<hr />
<!-- AddThis Button BEGIN -->
<div class="addthis_toolbox addthis_default_style">
<a class="addthis_button_facebook_like" fb:like:layout="button_count"></a>
<a class="addthis_button_tweet"></a>
<a class="addthis_button_google_plusone" g:plusone:size="medium"></a>
</div>
<!-- AddThis Button END -->
<hr/>
<section class="comments" id="comments">
<h2>Comments</h2>
<div id="disqus_thread"></div>
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'lyh'; // required: replace example with your forum shortname
var disqus_config = function () {
this.language = "en";
this.page.identifier = '2017-08-01-lambda-serialization';
this.page.url = 'https://www.lyh.me/lambda-serialization.html';
};
/* * * DON'T EDIT BELOW THIS LINE * * */
(function () {
var dsq = document.createElement('script');
dsq.type = 'text/javascript';
dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by
Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
</section>
</article>
</section>
</div>
<div class="col-sm-3" id="sidebar">
<aside>
<div id="aboutme">
<p>
<img width="100%" class="img-thumbnail" src="https://www.lyh.me//avatar.jpg"/>
</p>
<p>
<strong>About Neville Li</strong><br/>
Data infrastructure @<a href="https://twitter.com/Spotify">Spotify</a>, ex-@<a href="https://twitter.com/Yahoo">Yahoo</a> search, creator of <a href="https://github.com/spotify/scio">Scio</a>, technical cave & wreck diver, lefty guitar player
</p>
</div><!-- Sidebar -->
<section class="well well-sm">
<ul class="list-group list-group-flush">
<!-- Sidebar/Social -->
<li class="list-group-item">
<h4><i class="fa fa-home fa-lg"></i><span class="icon-label">Social</span></h4>
<ul class="list-group" id="social">
<li class="list-group-item"><a href="https://open.spotify.com/user/sinisa_lyh"><i class="fa fa-spotify fa-lg"></i> Spotify</a></li>
<li class="list-group-item"><a href="https://github.com/nevillelyh"><i class="fa fa-github-square fa-lg"></i> GitHub</a></li>
<li class="list-group-item"><a href="https://twitter.com/sinisa_lyh"><i class="fa fa-twitter-square fa-lg"></i> Twitter</a></li>
<li class="list-group-item"><a href="https://www.slideshare.net/sinisalyh"><i class="fa fa-slideshare fa-lg"></i> SlideShare</a></li>
<li class="list-group-item"><a href="https://www.youtube.com/user/sinisalyh/videos"><i class="fa fa-youtube-square fa-lg"></i> YouTube</a></li>
<li class="list-group-item"><a href="https://www.instagram.com/sinisa/"><i class="fa fa-instagram fa-lg"></i> Instagram</a></li>
<li class="list-group-item"><a href="https://www.flickr.com/photos/sinisa_lyh"><i class="fa fa-flickr fa-lg"></i> Flickr</a></li>
</ul>
</li>
<!-- End Sidebar/Social -->
<!-- Sidebar/Recent Posts -->
<li class="list-group-item">
<h4><i class="fa fa-home fa-lg"></i><span class="icon-label">Recent Posts</span></h4>
<ul class="list-group" id="recentposts">
<li class="list-group-item"><a href="https://www.lyh.me/magnolify.html">Magnolify</a></li>
<li class="list-group-item"><a href="https://www.lyh.me/featran.html">Featran</a></li>
<li class="list-group-item"><a href="https://www.lyh.me/automatic-type-class-derivation-with-shapeless.html">Automatic type-class derivation with Shapeless</a></li>
<li class="list-group-item"><a href="https://www.lyh.me/lambda-serialization.html">Lambda serialization</a></li>
<li class="list-group-item"><a href="https://www.lyh.me/lawfulness-of-aggregatebykey.html">Lawfulness of aggregateByKey</a></li>
</ul>
</li>
<!-- End Sidebar/Recent Posts -->
<!-- Sidebar/Categories -->
<li class="list-group-item">
<h4><i class="fa fa-home fa-lg"></i><span class="icon-label">Categories</span></h4>
<ul class="list-group" id="categories">
<li class="list-group-item">
<a href="https://www.lyh.me/category/code.html"><i class="fa fa-folder-open fa-lg"></i>code</a>
</li>
<li class="list-group-item">
<a href="https://www.lyh.me/category/misc.html"><i class="fa fa-folder-open fa-lg"></i>misc</a>
</li>
</ul>
</li>
<!-- End Sidebar/Categories -->
<!-- Sidebar/Twitter Timeline -->
<li class="list-group-item">
<h4><i class="fa fa-twitter fa-lg"></i><span class="icon-label">Latest Tweets</span></h4>
<div id="twitter_timeline">
<a class="twitter-timeline" data-width="250" data-height="300" data-dnt="true" data-theme="light" href="https://twitter.com/sinisa_lyh">Tweets by sinisa_lyh</a> <script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
</div>
</li>
<!-- End Sidebar/Twitter Timeline -->
</ul>
</section>
<!-- End Sidebar --> </aside>
</div>
</div>
</div>
<!-- End Content Container -->
<footer>
<div class="container">
<hr>
<div class="row">
<div class="col-xs-10">© 2020 Neville Li
· Powered by <a href="https://github.com/getpelican/pelican-themes/tree/master/pelican-bootstrap3" target="_blank">pelican-bootstrap3</a>,
<a href="http://docs.getpelican.com/" target="_blank">Pelican</a>,
<a href="http://getbootstrap.com" target="_blank">Bootstrap</a> <p><small> <a rel="license" href="https://creativecommons.org/licenses/by-nc/4.0/deed.en"><img alt="Creative Commons License" style="border-width:0" src="//i.creativecommons.org/l/by-nc/4.0/80x15.png" /></a>
Content
licensed under a <a rel="license" href="https://creativecommons.org/licenses/by-nc/4.0/deed.en">Creative Commons Attribution-NonCommercial 4.0 International License</a>, except where indicated otherwise.
</small></p>
</div>
<div class="col-xs-2"><p class="pull-right"><i class="fa fa-arrow-up"></i> <a href="#">Back to top</a></p></div>
</div>
</div>
</footer>
<script src="https://www.lyh.me/theme/js/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="https://www.lyh.me/theme/js/bootstrap.min.js"></script>
<!-- Enable responsive features in IE8 with Respond.js (https://github.com/scottjehl/Respond) -->
<script src="https://www.lyh.me/theme/js/respond.min.js"></script>
<!-- Disqus -->
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'lyh'; // required: replace example with your forum shortname
/* * * DON'T EDIT BELOW THIS LINE * * */
(function () {
var s = document.createElement('script');
s.async = true;
s.type = 'text/javascript';
s.src = '//' + disqus_shortname + '.disqus.com/count.js';
(document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
}());
</script>
<!-- End Disqus Code -->
<!-- Google Analytics -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-6988688-5']);
_gaq.push(['_trackPageview']);
(function () {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>
<!-- End Google Analytics Code -->
<script type="text/javascript">var addthis_config = {"data_track_addressbar": true};</script>
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=sinisalyh"></script>
</body>
</html>