/
StreamHandoffRDD.scala
442 lines (401 loc) · 17.2 KB
/
StreamHandoffRDD.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
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
/*
* 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.carbondata.spark.rdd
import java.text.SimpleDateFormat
import java.util
import java.util.{Date, UUID}
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.mapreduce.{Job, TaskAttemptID, TaskType}
import org.apache.hadoop.mapreduce.task.TaskAttemptContextImpl
import org.apache.spark.{Partition, SerializableWritable, SparkContext, TaskContext}
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.catalyst.expressions.GenericInternalRow
import org.apache.carbondata.common.logging.LogServiceFactory
import org.apache.carbondata.converter.SparkDataTypeConverterImpl
import org.apache.carbondata.core.datamap.Segment
import org.apache.carbondata.core.datastore.block.SegmentProperties
import org.apache.carbondata.core.datastore.impl.FileFactory
import org.apache.carbondata.core.locks.{CarbonLockFactory, LockUsage}
import org.apache.carbondata.core.metadata.CarbonMetadata
import org.apache.carbondata.core.metadata.schema.table.CarbonTable
import org.apache.carbondata.core.scan.result.iterator.RawResultIterator
import org.apache.carbondata.core.statusmanager.{LoadMetadataDetails, SegmentStatus, SegmentStatusManager}
import org.apache.carbondata.core.util.{CarbonUtil, DataTypeUtil}
import org.apache.carbondata.core.util.path.CarbonTablePath
import org.apache.carbondata.events.{OperationContext, OperationListenerBus}
import org.apache.carbondata.hadoop.{CarbonInputSplit, CarbonProjection}
import org.apache.carbondata.hadoop.api.{CarbonInputFormat, CarbonTableInputFormat}
import org.apache.carbondata.processing.loading.events.LoadEvents.{LoadTablePostStatusUpdateEvent, LoadTablePreStatusUpdateEvent}
import org.apache.carbondata.processing.loading.model.CarbonLoadModel
import org.apache.carbondata.processing.merger.{CompactionResultSortProcessor, CompactionType}
import org.apache.carbondata.processing.util.CarbonLoaderUtil
import org.apache.carbondata.spark.{HandoffResult, HandoffResultImpl}
import org.apache.carbondata.spark.util.CommonUtil
import org.apache.carbondata.streaming.{CarbonStreamInputFormat, CarbonStreamRecordReader}
/**
* partition of the handoff segment
*/
class HandoffPartition(
val rddId: Int,
val idx: Int,
@transient val inputSplit: CarbonInputSplit
) extends Partition {
val split = new SerializableWritable[CarbonInputSplit](inputSplit)
override val index: Int = idx
override def hashCode(): Int = 41 * (41 + rddId) + idx
}
/**
* package the record reader of the handoff segment to RawResultIterator
* todo: actually we should not extends rawResultIterator if we don't use any method or variable
* from it. We only use it to reduce duplicate code for compaction and handoff
* and we can extract it later
*/
class StreamingRawResultIterator(
recordReader: CarbonStreamRecordReader
) extends RawResultIterator(null, null, null) {
override def hasNext: Boolean = {
recordReader.nextKeyValue()
}
override def next(): Array[Object] = {
val rowTmp = recordReader
.getCurrentValue
.asInstanceOf[GenericInternalRow]
.values
.asInstanceOf[Array[Object]]
val row = new Array[Object](rowTmp.length)
System.arraycopy(rowTmp, 0, row, 0, rowTmp.length)
row
}
}
/**
* execute streaming segment handoff
*/
class StreamHandoffRDD[K, V](
@transient private val ss: SparkSession,
result: HandoffResult[K, V],
carbonLoadModel: CarbonLoadModel,
handOffSegmentId: String) extends CarbonRDD[(K, V)](ss, Nil) {
private val jobTrackerId: String = {
val formatter = new SimpleDateFormat("yyyyMMddHHmm")
formatter.format(new Date())
}
override def internalCompute(
split: Partition,
context: TaskContext): Iterator[(K, V)] = {
carbonLoadModel.setTaskNo("" + split.index)
val carbonTable = carbonLoadModel.getCarbonDataLoadSchema.getCarbonTable
DataTypeUtil.setDataTypeConverter(new SparkDataTypeConverterImpl)
CarbonMetadata.getInstance().addCarbonTable(carbonTable)
// the input iterator is using raw row
val iteratorList = prepareInputIterator(split, carbonTable)
CommonUtil.setTempStoreLocation(split.index, carbonLoadModel, true, false)
// use CompactionResultSortProcessor to sort data dan write to columnar files
val processor = prepareHandoffProcessor(carbonTable)
val status = processor.execute(iteratorList)
new Iterator[(K, V)] {
private var finished = false
override def hasNext: Boolean = {
!finished
}
override def next(): (K, V) = {
finished = true
result.getKey("" + split.index, status)
}
}
}
/**
* prepare input iterator by basing CarbonStreamRecordReader
*/
private def prepareInputIterator(
split: Partition,
carbonTable: CarbonTable
): util.ArrayList[RawResultIterator] = {
val inputSplit = split.asInstanceOf[HandoffPartition].split.value
val attemptId = new TaskAttemptID(jobTrackerId, id, TaskType.MAP, split.index, 0)
val hadoopConf = getConf
CarbonInputFormat.setDatabaseName(hadoopConf, carbonTable.getDatabaseName)
CarbonInputFormat.setTableName(hadoopConf, carbonTable.getTableName)
CarbonInputFormat.setTablePath(hadoopConf, carbonTable.getTablePath)
val projection = new CarbonProjection
val dataFields = carbonTable.getStreamStorageOrderColumn(carbonTable.getTableName)
(0 until dataFields.size()).foreach { index =>
projection.addColumn(dataFields.get(index).getColName)
}
CarbonInputFormat.setColumnProjection(hadoopConf, projection)
CarbonInputFormat.setTableInfo(hadoopConf, carbonTable.getTableInfo)
val attemptContext = new TaskAttemptContextImpl(hadoopConf, attemptId)
val format = new CarbonTableInputFormat[Array[Object]]()
val model = format.createQueryModel(inputSplit, attemptContext)
val inputFormat = new CarbonStreamInputFormat
val streamReader = inputFormat.createRecordReader(inputSplit, attemptContext)
.asInstanceOf[CarbonStreamRecordReader]
streamReader.setVectorReader(false)
streamReader.setQueryModel(model)
streamReader.setUseRawRow(true)
streamReader.initialize(inputSplit, attemptContext)
val iteratorList = new util.ArrayList[RawResultIterator](1)
iteratorList.add(new StreamingRawResultIterator(streamReader))
iteratorList
}
private def prepareHandoffProcessor(
carbonTable: CarbonTable
): CompactionResultSortProcessor = {
val wrapperColumnSchemaList = CarbonUtil.getColumnSchemaList(
carbonTable.getDimensionByTableName(carbonTable.getTableName),
carbonTable.getMeasureByTableName(carbonTable.getTableName))
val dimLensWithComplex =
(0 until wrapperColumnSchemaList.size()).map(_ => Integer.MAX_VALUE).toArray
val dictionaryColumnCardinality =
CarbonUtil.getFormattedCardinality(dimLensWithComplex, wrapperColumnSchemaList)
val segmentProperties =
new SegmentProperties(wrapperColumnSchemaList, dictionaryColumnCardinality)
new CompactionResultSortProcessor(
carbonLoadModel,
carbonTable,
segmentProperties,
CompactionType.STREAMING,
carbonTable.getTableName,
null
)
}
/**
* get the partitions of the handoff segment
*/
override protected def internalGetPartitions: Array[Partition] = {
val job = Job.getInstance(FileFactory.getConfiguration)
val inputFormat = new CarbonTableInputFormat[Array[Object]]()
val segmentList = new util.ArrayList[Segment](1)
segmentList.add(Segment.toSegment(handOffSegmentId, null))
val splits = inputFormat.getSplitsOfStreaming(
job,
carbonLoadModel.getCarbonDataLoadSchema.getCarbonTable.getAbsoluteTableIdentifier,
segmentList
)
(0 until splits.size()).map { index =>
new HandoffPartition(id, index, splits.get(index).asInstanceOf[CarbonInputSplit])
}.toArray[Partition]
}
}
object StreamHandoffRDD {
private val LOGGER = LogServiceFactory.getLogService(this.getClass.getCanonicalName)
def iterateStreamingHandoff(
carbonLoadModel: CarbonLoadModel,
operationContext: OperationContext,
sparkSession: SparkSession): Unit = {
val carbonTable = carbonLoadModel.getCarbonDataLoadSchema.getCarbonTable
val identifier = carbonTable.getAbsoluteTableIdentifier
var continueHandoff = false
// require handoff lock on table
val lock = CarbonLockFactory.getCarbonLockObj(identifier, LockUsage.HANDOFF_LOCK)
try {
if (lock.lockWithRetries()) {
LOGGER.info("Acquired the handoff lock for table" +
s" ${ carbonTable.getDatabaseName }.${ carbonTable.getTableName }")
// handoff streaming segment one by one
do {
val segmentStatusManager = new SegmentStatusManager(identifier)
var loadMetadataDetails: Array[LoadMetadataDetails] = null
// lock table to read table status file
val statusLock = segmentStatusManager.getTableStatusLock
try {
if (statusLock.lockWithRetries()) {
loadMetadataDetails = SegmentStatusManager.readLoadMetadata(
CarbonTablePath.getMetadataPath(identifier.getTablePath))
}
} finally {
if (null != statusLock) {
statusLock.unlock()
}
}
if (null != loadMetadataDetails) {
val streamSegments =
loadMetadataDetails.filter(_.getSegmentStatus == SegmentStatus.STREAMING_FINISH)
continueHandoff = streamSegments.length > 0
if (continueHandoff) {
// handoff a streaming segment
val loadMetadataDetail = streamSegments(0)
carbonLoadModel.setSegmentId(loadMetadataDetail.getLoadName)
executeStreamingHandoff(
carbonLoadModel,
sparkSession,
operationContext,
loadMetadataDetail.getLoadName
)
}
} else {
continueHandoff = false
}
} while (continueHandoff)
}
} finally {
if (null != lock) {
lock.unlock()
}
}
}
/**
* start new thread to execute stream segment handoff
*/
def startStreamingHandoffThread(
carbonLoadModel: CarbonLoadModel,
operationContext: OperationContext,
sparkSession: SparkSession,
isDDL: Boolean
): Unit = {
if (isDDL) {
iterateStreamingHandoff(carbonLoadModel, operationContext, sparkSession)
} else {
// start a new thread to execute streaming segment handoff
val handoffThread = new Thread() {
override def run(): Unit = {
iterateStreamingHandoff(carbonLoadModel, operationContext, sparkSession)
}
}
handoffThread.start()
}
}
/**
* invoke StreamHandoffRDD to handoff a streaming segment to a columnar segment
*/
def executeStreamingHandoff(
carbonLoadModel: CarbonLoadModel,
sparkSession: SparkSession,
operationContext: OperationContext,
handoffSegmenId: String): Unit = {
var loadStatus = SegmentStatus.SUCCESS
var errorMessage: String = "Handoff failure"
try {
// generate new columnar segment
val newMetaEntry = new LoadMetadataDetails
carbonLoadModel.setFactTimeStamp(System.currentTimeMillis())
CarbonLoaderUtil.populateNewLoadMetaEntry(
newMetaEntry,
SegmentStatus.INSERT_IN_PROGRESS,
carbonLoadModel.getFactTimeStamp,
false)
CarbonLoaderUtil.recordNewLoadMetadata(newMetaEntry, carbonLoadModel, true, false)
// convert a streaming segment to columnar segment
val status = new StreamHandoffRDD(
sparkSession,
new HandoffResultImpl(),
carbonLoadModel,
handoffSegmenId).collect()
status.foreach { x =>
if (!x._2) {
loadStatus = SegmentStatus.LOAD_FAILURE
}
}
} catch {
case ex: Exception =>
loadStatus = SegmentStatus.LOAD_FAILURE
errorMessage = errorMessage + ": " + ex.getCause.getMessage
LOGGER.error(errorMessage)
LOGGER.error(ex, s"Handoff failed on streaming segment $handoffSegmenId")
}
if (loadStatus == SegmentStatus.LOAD_FAILURE) {
CarbonLoaderUtil.updateTableStatusForFailure(carbonLoadModel)
LOGGER.info("********starting clean up**********")
CarbonLoaderUtil.deleteSegment(carbonLoadModel, carbonLoadModel.getSegmentId.toInt)
LOGGER.info("********clean up done**********")
LOGGER.audit(s"Handoff is failed for " +
s"${ carbonLoadModel.getDatabaseName }.${ carbonLoadModel.getTableName }")
LOGGER.error("Cannot write load metadata file as handoff failed")
throw new Exception(errorMessage)
}
if (loadStatus == SegmentStatus.SUCCESS) {
operationContext.setProperty("uuid", UUID.randomUUID().toString)
val loadTablePreStatusUpdateEvent: LoadTablePreStatusUpdateEvent =
new LoadTablePreStatusUpdateEvent(
carbonLoadModel.getCarbonDataLoadSchema.getCarbonTable.getCarbonTableIdentifier,
carbonLoadModel)
OperationListenerBus.getInstance().fireEvent(loadTablePreStatusUpdateEvent, operationContext)
val done = updateLoadMetadata(handoffSegmenId, carbonLoadModel)
val loadTablePostStatusUpdateEvent: LoadTablePostStatusUpdateEvent =
new LoadTablePostStatusUpdateEvent(carbonLoadModel)
OperationListenerBus.getInstance()
.fireEvent(loadTablePostStatusUpdateEvent, operationContext)
if (!done) {
val errorMessage = "Handoff failed due to failure in table status updation."
LOGGER.audit("Handoff is failed for " +
s"${ carbonLoadModel.getDatabaseName }.${ carbonLoadModel.getTableName }")
LOGGER.error("Handoff failed due to failure in table status updation.")
throw new Exception(errorMessage)
}
}
}
/**
* update streaming segment and new columnar segment
*/
private def updateLoadMetadata(
handoffSegmentId: String,
loadModel: CarbonLoadModel
): Boolean = {
var status = false
val metaDataFilepath = loadModel.getCarbonDataLoadSchema.getCarbonTable.getMetadataPath
val identifier = loadModel.getCarbonDataLoadSchema.getCarbonTable.getAbsoluteTableIdentifier
val metadataPath = CarbonTablePath.getMetadataPath(identifier.getTablePath)
val fileType = FileFactory.getFileType(metadataPath)
if (!FileFactory.isFileExist(metadataPath, fileType)) {
FileFactory.mkdirs(metadataPath, fileType)
}
val tableStatusPath = CarbonTablePath.getTableStatusFilePath(identifier.getTablePath)
val segmentStatusManager = new SegmentStatusManager(identifier)
val carbonLock = segmentStatusManager.getTableStatusLock
try {
if (carbonLock.lockWithRetries()) {
LOGGER.info(
"Acquired lock for table" + loadModel.getDatabaseName() + "." + loadModel.getTableName()
+ " for table status updation")
val listOfLoadFolderDetailsArray =
SegmentStatusManager.readLoadMetadata(metaDataFilepath)
// update new columnar segment to success status
val newSegment =
listOfLoadFolderDetailsArray.find(_.getLoadName.equals(loadModel.getSegmentId))
if (newSegment.isEmpty) {
throw new Exception("Failed to update table status for new segment")
} else {
newSegment.get.setSegmentStatus(SegmentStatus.SUCCESS)
newSegment.get.setLoadEndTime(System.currentTimeMillis())
}
// update streaming segment to compacted status
val streamSegment =
listOfLoadFolderDetailsArray.find(_.getLoadName.equals(handoffSegmentId))
if (streamSegment.isEmpty) {
throw new Exception("Failed to update table status for streaming segment")
} else {
streamSegment.get.setSegmentStatus(SegmentStatus.COMPACTED)
streamSegment.get.setMergedLoadName(loadModel.getSegmentId)
}
// refresh table status file
SegmentStatusManager.writeLoadDetailsIntoFile(tableStatusPath, listOfLoadFolderDetailsArray)
status = true
} else {
LOGGER.error("Not able to acquire the lock for Table status updation for table " + loadModel
.getDatabaseName() + "." + loadModel.getTableName())
}
} finally {
if (carbonLock.unlock()) {
LOGGER.info("Table unlocked successfully after table status updation" +
loadModel.getDatabaseName() + "." + loadModel.getTableName())
} else {
LOGGER.error("Unable to unlock Table lock for table" + loadModel.getDatabaseName() +
"." + loadModel.getTableName() + " during table status updation")
}
}
status
}
}