ravipesala commented on a change in pull request #3177: [CARBONDATA-3337][CARBONDATA-3306] Distributed index server
URL: https://github.com/apache/carbondata/pull/3177#discussion_r279674964 ########## File path: integration/spark2/src/main/scala/org/apache/carbondata/indexserver/DistributedRDDUtils.scala ########## @@ -0,0 +1,158 @@ +/* + * 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.indexserver + +import java.util.concurrent.ConcurrentHashMap + +import scala.collection.JavaConverters._ + +import org.apache.commons.lang.StringUtils +import org.apache.hadoop.mapreduce.InputSplit +import org.apache.spark.Partition + +import org.apache.carbondata.core.datamap.Segment +import org.apache.carbondata.core.datamap.dev.expr.DataMapDistributableWrapper + +object DistributedRDDUtils { + // Segment number to executorNode mapping + val segmentToExecutorMapping: java.util.Map[String, String] = + new ConcurrentHashMap[String, String]() + + // executorNode to segmentSize mapping + val executorToCacheSizeMapping: java.util.Map[String, Long] = + new ConcurrentHashMap[String, Long]() + + def getExecutors(segment: Array[InputSplit], executorsList : Set[String], + tableUniqueName: String, rddId: Int): Seq[Partition] = { + // sort the partitions in increasing order of index size. + val (segments, legacySegments) = segment.span(split => StringUtils.isNotEmpty(split + .asInstanceOf[DataMapDistributableWrapper].getDistributable.getSegment.getIndexSize)) + val sortedPartitions = segments.sortWith(_.asInstanceOf[DataMapDistributableWrapper] + .getDistributable.getSegment.getIndexSize > + _.asInstanceOf[DataMapDistributableWrapper] + .getDistributable.getSegment.getIndexSize) + val executorCache = DistributedRDDUtils.executorToCacheSizeMapping + // check if any executor is dead. + if (executorsList.size < executorCache.size) { + // extract the dead executor host name + val invalidExecutors = executorCache.keySet().asScala.diff(executorsList) + DistributedRDDUtils.invalidateExecutors(invalidExecutors.toSeq) + } + convertToPartition(legacySegments, tableUniqueName, executorsList, rddId) ++ + convertToPartition(sortedPartitions, tableUniqueName, executorsList, rddId) + } + + private def convertToPartition(segments: Seq[InputSplit], tableUniqueName: String, + executorList: Set[String], rddId: Int): Seq[Partition] = { + segments.zipWithIndex.map { case (partition, index) => + val wrapper = partition.asInstanceOf[DataMapDistributableWrapper] + .getDistributable + if (StringUtils.isEmpty(wrapper.getSegment.getIndexSize)) { + wrapper.getSegment.setIndexSize("1") + } + wrapper.setLocations(Array(DistributedRDDUtils + .assignExecutor(tableUniqueName, wrapper.getSegment, executorList))) + new DataMapRDDPartition(rddId, index, partition) + } + } + + /** + * Update the cache size returned by the executors to the driver mapping. + */ + def updateExecutorCacheSize(cacheSizes: Set[String]): Unit = { + synchronized { + cacheSizes.foreach { + executorCacheSize => + // executorCacheSize would be in the form of 127.0.0.1_10024 where the left of '_' + // would be the executor IP and the right would be the cache that executor is holding. + val size = executorCacheSize.split("_") + executorToCacheSizeMapping.put(size(0), size(1).toLong) + } + } + } + + def invalidateCache(tableUniqueName: String): Unit = { + segmentToExecutorMapping.keySet().asScala.foreach { + key => + if (key.split("_")(0).equalsIgnoreCase(tableUniqueName)) { + segmentToExecutorMapping.remove(key) + } + } + } + + /** + * Invalidate the dead executors from the mapping and assign the segments to some other + * executor, so that the query can load the segments to the new assigned executor. + */ + def invalidateExecutors(invalidExecutors: Seq[String]): Unit = { + // remove all invalidExecutor mapping from cache. + for ((key: String, value: String) <- segmentToExecutorMapping.asScala) { + // find the invalid executor in cache. + if (invalidExecutors.contains(value)) { + // remove mapping for the invalid executor. + val invalidExecutorSize = executorToCacheSizeMapping.remove(key) + // find a new executor for the segment + val reassignedExecutor = getLeastLoadedExecutor + segmentToExecutorMapping.put(key, reassignedExecutor) + // add the size size of the invalid executor to the reassigned executor. + executorToCacheSizeMapping.put( + reassignedExecutor, + executorToCacheSizeMapping.get(reassignedExecutor) + invalidExecutorSize + ) + } + } + } + + /** + * Sorts the executor cache based on the size each one is handling and returns the least of them. + * + * @return + */ + private def getLeastLoadedExecutor: String = { + executorToCacheSizeMapping.asScala.toSeq.sortWith(_._2 < _._2).head._1 + } + + /** + * Assign a executor for the current segment. If a executor was previously assigned to the + * segment then the same would be returned. + * + * @return + */ + def assignExecutor(tableName: String, segment: Segment, validExecutors: Set[String]): String = { + val cacheKey = s"${ tableName }_${ segment.getSegmentNo }" + if (segmentToExecutorMapping.containsKey(cacheKey)) { + segmentToExecutorMapping.get(cacheKey) + } else { + // check if any executor is not assigned. If yes then give priority to that executor + // otherwise get the executor which has handled the least size. + val unassignedExecutors = validExecutors + .diff(executorToCacheSizeMapping.asScala.keys.toSet) + val newExecutor = if (unassignedExecutors.nonEmpty) { + unassignedExecutors.head.split(":")(0) + } else { + val identifiedExecutor = getLeastLoadedExecutor + identifiedExecutor + } + val existingExecutorSize = executorToCacheSizeMapping.get(newExecutor) + executorToCacheSizeMapping.put(newExecutor, existingExecutorSize + segment.getIndexSize Review comment: Please make synchronization on table level during assignment or clean operation. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [hidden email] With regards, Apache Git Services |
Free forum by Nabble | Edit this page |