This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
DataProcessorKeyed.scala
117 lines (96 loc) · 4.99 KB
/
DataProcessorKeyed.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
/*
* Copyright (C) 2017-2019 Lightbend
*
* This file is part of the Lightbend model-serving-tutorial (https://github.com/lightbend/model-serving-tutorial)
*
* The model-serving-tutorial is free software: you can redistribute it and/or modify
* it under the terms of the Apache License Version 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.lightbend.modelserving.flink.keyed
import com.lightbend.model.winerecord.WineRecord
import com.lightbend.modelserving.model._
import com.lightbend.modelserving.flink.typeschema.ModelTypeSerializer
import org.apache.flink.api.common.state.{ValueState, ValueStateDescriptor}
import org.apache.flink.api.scala.createTypeInformation
import org.apache.flink.configuration.Configuration
import org.apache.flink.streaming.api.functions.co.CoProcessFunction
import org.apache.flink.util.Collector
/**
* Class for processing data using models with state managed by key, rather than partitioned.
*
* see http://dataartisans.github.io/flink-training/exercises/eventTimeJoin.html for details
* In Flink, a class instance is created not for each key, but rather for each key group,
* https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/stream/state/state.html#keyed-state-and-operator-state.
* As a result, any state data has to be in the key specific state.
*/
class DataProcessorKeyed[RECORD, RESULT]() extends CoProcessFunction[DataToServe[RECORD], ModelToServe, ServingResult[RESULT]]{
/** The current model state */
var modelState: ValueState[ModelToServeStats] = _
var currentModel : ValueState[Option[Model[RECORD, RESULT]]] = _
/** Called when an instance is created */
override def open(parameters: Configuration): Unit = {
// Model state descriptor
val modelStateDesc = new ValueStateDescriptor[ModelToServeStats](
"currentModelState", // state name
createTypeInformation[ModelToServeStats]) // type information
modelStateDesc.setQueryable("currentModelState") // Expose it for queryable state
// Create Model state
modelState = getRuntimeContext.getState(modelStateDesc)
// Model descriptor
val modelDesc = new ValueStateDescriptor[Option[Model[RECORD, RESULT]]](
"currentModel", // state name
new ModelTypeSerializer[RECORD, RESULT]) // type information
// Create current model state
currentModel = getRuntimeContext.getState(modelDesc)
}
/**
* Process a new model. We store it in the `newModel`, then `processElement1` will detect it and switch out the old
* model.
*/
override def processElement2(model: ModelToServe, ctx: CoProcessFunction[DataToServe[RECORD], ModelToServe, ServingResult[RESULT]]#Context, out: Collector[ServingResult[RESULT]]): Unit = {
// Ensure that the state is initialized
if(currentModel.value == null) currentModel.update(None)
println(s"New model - $model")
// Create a model
ModelToServe.toModel[RECORD, RESULT](model) match {
case Some(md) => // Update model
// Close current model first
currentModel.value.foreach(_.cleanup())
// Update model
currentModel.update(Some(md))
modelState.update(ModelToServeStats(model))
case _ => // Model creation failed, continue
println(s"Model creation for $model failed")
}
}
/** Serve data, i.e., score with the current model */
override def processElement1(record: DataToServe[RECORD], ctx: CoProcessFunction[DataToServe[RECORD], ModelToServe, ServingResult[RESULT]]#Context, out: Collector[ServingResult[RESULT]]): Unit = {
// Exercise:
// Instead of tossing the old model, create a stack of models. Add the ability to pop the current model and recover
// the previous one(s).
// Then decide how to bound the number of stack elements by some N, but this suggests you might want to store them
// in a bounded-size cache, so you can toss the oldest ones.
// Ensure that the state is initialized
if(currentModel.value == null) currentModel.update(None)
// Actually process data
currentModel.value match {
case Some(model) =>
val start = System.currentTimeMillis()
val score = model.score(record.getRecord)
val duration = System.currentTimeMillis() - start
modelState.update(modelState.value().incrementUsage(duration))
val result = ServingResult[RESULT](modelState.value().name, record.getType, record.getRecord.asInstanceOf[WineRecord].ts, score)
out.collect(result)
case _ => // Exercise: print/log when a matching model wasn't found. Does the output make sense?
}
}
}
object DataProcessorKeyed {
def apply[RECORD, RESULT]() = new DataProcessorKeyed[RECORD, RESULT]
}