-
Notifications
You must be signed in to change notification settings - Fork 224
/
NodeCoordinatorActor.scala
287 lines (246 loc) · 11.6 KB
/
NodeCoordinatorActor.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
280
281
282
283
284
285
286
287
package filodb.coordinator
import java.util.concurrent.ConcurrentHashMap
import scala.collection.mutable.HashMap
import scala.concurrent.duration._
import akka.actor.{ActorRef, OneForOneStrategy, PoisonPill, Props, Terminated}
import akka.actor.SupervisorStrategy.{Restart, Stop}
import akka.event.LoggingReceive
import kamon.Kamon
import net.ceedubs.ficus.Ficus._
import filodb.coordinator.client.MiscCommands
import filodb.core._
import filodb.core.downsample.DownsampleConfig
import filodb.core.memstore.MemStore
import filodb.core.metadata._
import filodb.core.store.{IngestionConfig, MetaStore, StoreConfig}
import filodb.query.QueryCommand
/**
* The NodeCoordinatorActor is the common external API entry point for all FiloDB operations.
* It is a singleton - there should be exactly one such actor per node/JVM process.
* It is responsible for:
* - Overall external FiloDB API.
* - Metadata changes (dataset/column changes)
* - Supervising, spinning up, cleaning up IngestionActors, QueryActors
* - Forwarding new changes (rows) to other NodeCoordinatorActors if they are not local
* - Forwarding rows to IngestionActors
*
* Since it is the API entry point its work should be very lightweight, mostly forwarding things to
* other actors to do the real work.
*
* It is called by local (eg HTTP) as well as remote (eg Spark ETL) processes.
*/
object NodeCoordinatorActor {
/** Clears the state of a single dataset. */
final case class ClearState(dataset: DatasetRef)
def props(metaStore: MetaStore,
memStore: MemStore,
settings: FilodbSettings): Props =
Props(classOf[NodeCoordinatorActor], metaStore, memStore, settings)
}
private[filodb] final class NodeCoordinatorActor(metaStore: MetaStore,
memStore: MemStore,
settings: FilodbSettings) extends NamingAwareBaseActor {
import context.dispatcher
import NodeClusterActor._
import NodeCoordinatorActor._
import client.DatasetCommands._
import client.IngestionCommands._
val ingesters = new HashMap[DatasetRef, ActorRef]
val queryActors = new HashMap[DatasetRef, ActorRef]
var clusterActor: Option[ActorRef] = None
val shardMaps = new ConcurrentHashMap[DatasetRef, ShardMapper]
var statusActor: Option[ActorRef] = None
var datasetsInitialized = false
private val statusAckTimeout = settings.config.as[FiniteDuration]("tasks.timeouts.status-ack-timeout")
// By default, stop children IngestionActors when something goes wrong.
// restart query actors though
override val supervisorStrategy = OneForOneStrategy() {
case exception: Exception =>
val stackTrace = exception.getStackTrace
if (stackTrace(0).getClassName equals QueryActor.getClass.getName)
Restart
else
Stop
}
private def withIngester(originator: ActorRef, dataset: DatasetRef)
(func: ActorRef => Unit): Unit = {
ingesters.get(dataset).map(func).getOrElse(originator ! UnknownDataset)
}
// For now, datasets need to be set up for ingestion before they can be queried (in-mem only)
// TODO: if we ever support query API against cold (not in memory) datasets, change this
private def withQueryActor(originator: ActorRef, dataset: DatasetRef)(func: ActorRef => Unit): Unit =
queryActors.get(dataset).map(func).getOrElse(originator ! UnknownDataset)
// Used only for testing
private def createDataset(originator: ActorRef,
datasetObj: Dataset,
numShards: Int): Unit = {
(for {
resp2 <- memStore.store.initialize(datasetObj.ref, numShards) if resp2 == Success
}
yield {
originator ! DatasetCreated
}).recover {
case e: Exception => originator ! DatasetError(e.toString)
}
}
private def initializeDataset(dataset: Dataset, ingestConfig: IngestionConfig): Unit = {
logger.info(s"Initializing dataset ${dataset.ref}")
// FIXME initialization of cass tables below for dev environments is async - need to wait before continuing
// for now if table is not initialized in dev on first run, simply restart server :(
memStore.store.initialize(dataset.ref, ingestConfig.numShards)
// if downsampling is enabled, then initialize downsample datasets
ingestConfig.downsampleConfig
.downsampleDatasetRefs(dataset.ref.dataset)
.foreach { downsampleDataset => memStore.store.initialize(downsampleDataset, ingestConfig.numShards) }
setupDataset( dataset,
ingestConfig.storeConfig,
IngestionSource(ingestConfig.streamFactoryClass, ingestConfig.sourceConfig),
ingestConfig.downsampleConfig)
}
// TODO: move createDataset and truncateDataset into NodeClusterActor. truncate() needs distributed coord
private def truncateDataset(originator: ActorRef, dataset: DatasetRef, numShards: Int): Unit = {
try {
memStore.truncate(dataset, numShards).map {
case Success => originator ! DatasetTruncated
case other: Any => originator ! ServerError(other)
}
} catch {
case e: Exception => originator ! DatasetError(e.getMessage)
}
}
/** Creates a new ingestion actor initialized with the shard actor,
* and sends it a shard resync command created.
*
* Creates a QueryActor, subscribes it to shard events, keeping
* it decoupled from the shard actor. The QueryActor will receive an
* initial `CurrentShardSnapshot` to initialize it's local `ShardMapper`
* for the dataset, which is managed by the shard actor.
*/
private def setupDataset(dataset: Dataset,
storeConf: StoreConfig,
source: IngestionSource,
downsample: DownsampleConfig,
schemaOverride: Boolean = false): Unit = {
import ActorName.{Ingestion, Query}
logger.debug(s"Recreated dataset $dataset from string")
val ref = dataset.ref
clusterActor match {
case Some(nca) =>
val schemas = if (schemaOverride) Schemas(dataset.schema) else settings.schemas
if (schemaOverride) logger.info(s"Overriding schemas from settings: this better be a test!")
val props = IngestionActor.props(dataset.ref, schemas, memStore,
source, downsample, storeConf, statusActor.get)
val ingester = context.actorOf(props, s"$Ingestion-${dataset.name}")
context.watch(ingester)
ingesters(ref) = ingester
val ttl = if (memStore.isDownsampleStore) downsample.ttls.last.toMillis
else storeConf.diskTTLSeconds.toLong * 1000
def earliestTimestampFn: Long = System.currentTimeMillis() - ttl
logger.info(s"Creating QueryActor for dataset $ref with dataset ttlMs=$ttl")
val queryRef = context.actorOf(QueryActor.props(memStore, dataset.ref, schemas,
shardMaps.get(ref), earliestTimestampFn), s"$Query-$ref")
queryActors(ref) = queryRef
// TODO: Send status update to cluster actor
logger.info(s"Coordinator set up for ingestion and querying for $ref.")
case _ =>
// shouldn't happen
logger.error(s"Shard actor not set up for shard assignment, management and events.")
}
}
def datasetHandlers: Receive = LoggingReceive {
case CreateDataset(numShards, datasetObj, db) =>
// used only for unit testing now
createDataset(sender(), datasetObj, numShards)
case TruncateDataset(ref, numShards) =>
truncateDataset(sender(), ref, numShards)
}
def ingestHandlers: Receive = LoggingReceive {
case SetupDataset(dataset, resources, source, storeConf, downsample) =>
// used only in unit tests
if (!(ingesters contains dataset.ref)) { setupDataset(dataset, storeConf, source, downsample, true) }
case IngestRows(dataset, shard, rows) =>
withIngester(sender(), dataset) { _ ! IngestionActor.IngestRows(sender(), shard, rows) }
case GetIngestionStats(dataset) =>
withIngester(sender(), dataset) { _.forward(IngestionActor.GetStatus) }
}
def queryHandlers: Receive = LoggingReceive {
case q: QueryCommand =>
val originator = sender()
Kamon.currentSpan().mark("NodeCoordinatorActor received query")
withQueryActor(originator, q.dataset) { _.tell(q, originator) }
case QueryActor.ThrowException(dataset) =>
val originator = sender()
withQueryActor(originator, dataset) { _.tell(QueryActor.ThrowException(dataset), originator) }
}
def coordinatorReceive: Receive = LoggingReceive {
case e: CoordinatorRegistered => registered(e)
case s: ShardIngestionState => logger.trace(s"Received IngestionState/Snapshot ${s.map}")
shardMaps.put(s.ref, s.map)
forward(s, s.ref, sender())
case Terminated(memstoreCoord) => terminated(memstoreCoord)
case MiscCommands.GetClusterActor => sender() ! clusterActor
case StatusActor.GetCurrentEvents => statusActor.foreach(_.tell(StatusActor.GetCurrentEvents, sender()))
case ClearState(ref) => clearState(ref)
case NodeProtocol.ResetState => reset(sender())
}
def receive: Receive = queryHandlers orElse ingestHandlers orElse datasetHandlers orElse coordinatorReceive
private def registered(e: CoordinatorRegistered): Unit = {
logger.info(s"Registering new ClusterActor ${e.clusterActor}")
clusterActor = Some(e.clusterActor)
if (statusActor.isEmpty) {
statusActor = Some(context.actorOf(StatusActor.props(e.clusterActor, statusAckTimeout), "status"))
} else {
statusActor.get ! e.clusterActor // update proxy. NOTE: this is temporary fix
}
if (!datasetsInitialized) {
logger.debug(s"Initializing stream configs: ${settings.streamConfigs}")
settings.streamConfigs.foreach { config =>
val dataset = settings.datasetFromStream(config)
val ingestion = IngestionConfig(config, NodeClusterActor.noOpSource.streamFactoryClass).get
initializeDataset(dataset, ingestion)
}
datasetsInitialized = true
}
}
/** Forwards shard actions to the ingester for the given dataset.
* TODO version match if needed, when > 1, currently only 0.
*/
private def forward(action: ShardAction, ref: DatasetRef, origin: ActorRef): Unit =
ingesters.get(ref) match {
case Some(actor) =>
actor.tell(action, origin)
case _ =>
logger.warn(s"No IngestionActor for dataset ${ref}")
}
private def terminated(ingester: ActorRef): Unit = {
memStore.shutdown()
ingesters.find { case (key, ref) => ref == ingester }
.foreach { case (datasetRef, _) =>
logger.warn(s"$ingester terminated. Stopping ingestion for ${(datasetRef)}.")
ingesters.remove(datasetRef)
}
}
private def aliveIngesters: Seq[(DatasetRef, ActorRef)] = {
val kids = context.children.toBuffer
ingesters.toSeq.filter { case (dsRef, actorRef) => kids contains actorRef }
}
private def reset(origin: ActorRef): Unit = {
ingesters.values.foreach(_ ! PoisonPill)
queryActors.values.foreach(_ ! PoisonPill)
ingesters.clear()
queryActors.clear()
memStore.reset()
// Wait for all ingestor children to die
while (aliveIngesters.nonEmpty) {
logger.info(s"In reset, waiting for children to die.... ingesters=$ingesters children=${context.children}")
Thread sleep 250
}
origin ! NodeProtocol.StateReset
}
private def clearState(ref: DatasetRef): Unit = {
ingesters.get((ref)).foreach(_ ! PoisonPill)
ingesters.remove((ref))
// This is a bit heavy handed, it clears out the entire cache, not just for all datasets
memStore.reset()
}
}