From commits-return-13313-archive-asf-public=cust-asf.ponee.io@hudi.apache.org Fri Mar 13 06:57:51 2020 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id A3DF91806C2 for ; Fri, 13 Mar 2020 07:57:50 +0100 (CET) Received: (qmail 48495 invoked by uid 500); 13 Mar 2020 06:57:50 -0000 Mailing-List: contact commits-help@hudi.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@hudi.apache.org Delivered-To: mailing list commits@hudi.apache.org Received: (qmail 48418 invoked by uid 99); 13 Mar 2020 06:57:50 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 13 Mar 2020 06:57:50 +0000 From: GitBox To: commits@hudi.apache.org Subject: [GitHub] [incubator-hudi] vinothchandar commented on a change in pull request #1402: [HUDI-407] Adding Simple Index Message-ID: <158408266997.15275.10038664951420138347.gitbox@gitbox.apache.org> References: In-Reply-To: Date: Fri, 13 Mar 2020 06:57:49 -0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit vinothchandar commented on a change in pull request #1402: [HUDI-407] Adding Simple Index URL: https://github.com/apache/incubator-hudi/pull/1402#discussion_r392057064 ########## File path: hudi-client/src/main/java/org/apache/hudi/index/bloom/HoodieSimpleIndex.java ########## @@ -0,0 +1,263 @@ +/* + * 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.hudi.index.bloom; + +import org.apache.hudi.WriteStatus; +import org.apache.hudi.common.model.HoodieDataFile; +import org.apache.hudi.common.model.HoodieKey; +import org.apache.hudi.common.model.HoodieRecord; +import org.apache.hudi.common.model.HoodieRecordLocation; +import org.apache.hudi.common.model.HoodieRecordPayload; +import org.apache.hudi.common.table.timeline.HoodieInstant; +import org.apache.hudi.common.util.Option; +import org.apache.hudi.common.util.ParquetUtils; +import org.apache.hudi.common.util.collection.Pair; +import org.apache.hudi.config.HoodieWriteConfig; +import org.apache.hudi.table.HoodieTable; + +import com.google.common.annotations.VisibleForTesting; +import org.apache.hadoop.fs.Path; +import org.apache.log4j.LogManager; +import org.apache.log4j.Logger; +import org.apache.spark.api.java.JavaPairRDD; +import org.apache.spark.api.java.JavaRDD; +import org.apache.spark.api.java.JavaSparkContext; +import org.apache.spark.api.java.Optional; +import org.apache.spark.api.java.function.PairFunction; +import org.apache.spark.storage.StorageLevel; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import scala.Tuple2; + +import static java.util.stream.Collectors.toList; + +/** + * A simple index which reads interested fields from parquet and joins with incoming records to find the tagged location + * + * @param + */ +public class HoodieSimpleIndex extends HoodieBloomIndex { + + private static final Logger LOG = LogManager.getLogger(HoodieSimpleIndex.class); + + public HoodieSimpleIndex(HoodieWriteConfig config) { + super(config); + } + + /** + * Returns an RDD mapping each HoodieKey with a partitionPath/fileID which contains it. Option.Empty if the key is not + * found. + * + * @param hoodieKeys keys to lookup + * @param jsc spark context + * @param hoodieTable hoodie table object + */ + @Override + public JavaPairRDD>> fetchRecordLocation(JavaRDD hoodieKeys, + JavaSparkContext jsc, HoodieTable hoodieTable) { + JavaPairRDD partitionRecordKeyPairRDD = + hoodieKeys.mapToPair(key -> new Tuple2<>(key.getPartitionPath(), key.getRecordKey())); + + // Lookup indexes for all the partition/recordkey pair + JavaPairRDD recordKeyLocationRDD = + lookupIndex(partitionRecordKeyPairRDD, jsc, hoodieTable); + + JavaPairRDD keyHoodieKeyPairRDD = hoodieKeys.mapToPair(key -> new Tuple2<>(key, null)); + + return keyHoodieKeyPairRDD.leftOuterJoin(recordKeyLocationRDD).mapToPair(keyLoc -> { + Option> partitionPathFileidPair; + if (keyLoc._2._2.isPresent()) { + partitionPathFileidPair = Option.of(Pair.of(keyLoc._1().getPartitionPath(), keyLoc._2._2.get().getFileId())); + } else { + partitionPathFileidPair = Option.empty(); + } + return new Tuple2<>(keyLoc._1, partitionPathFileidPair); + }); + } + + @Override + public JavaRDD> tagLocation(JavaRDD> recordRDD, JavaSparkContext jsc, + HoodieTable hoodieTable) { + + // Step 0: cache the input record RDD + if (config.getBloomIndexUseCaching()) { + recordRDD.persist(config.getBloomIndexInputStorageLevel()); + } + + // Step 1: Extract out thinner JavaPairRDD of (partitionPath, recordKey) + JavaPairRDD partitionRecordKeyPairRDD = + recordRDD.mapToPair(record -> new Tuple2<>(record.getPartitionPath(), record.getRecordKey())); + + // Lookup indexes for all the partition/recordkey pair + JavaPairRDD keyFilenamePairRDD = + lookupIndex(partitionRecordKeyPairRDD, jsc, hoodieTable); + + // Cache the result, for subsequent stages. + if (config.getBloomIndexUseCaching()) { + keyFilenamePairRDD.persist(StorageLevel.MEMORY_AND_DISK_SER()); + } + if (LOG.isDebugEnabled()) { + long totalTaggedRecords = keyFilenamePairRDD.count(); + LOG.debug("Number of update records (ones tagged with a fileID): " + totalTaggedRecords); + } + + // Step 4: Tag the incoming records, as inserts or updates, by joining with existing record keys + JavaRDD> taggedRecordRDD = tagLocationBacktoRecords(keyFilenamePairRDD, recordRDD); + + if (config.getBloomIndexUseCaching()) { + recordRDD.unpersist(); // unpersist the input Record RDD + keyFilenamePairRDD.unpersist(); + } + return taggedRecordRDD; + } + + @Override + public JavaRDD updateLocation(JavaRDD writeStatusRDD, JavaSparkContext jsc, + HoodieTable hoodieTable) { + return writeStatusRDD; + } + + @Override + public boolean rollbackCommit(String commitTime) { + // Nope, don't need to do anything. + return true; + } + + /** + * This is not global, since we depend on the partitionPath to do the lookup. + */ + @Override + public boolean isGlobal() { + return false; + } + + /** + * No indexes into log files yet. + */ + @Override + public boolean canIndexLogFiles() { + return false; + } + + /** + * Bloom filters are stored, into the same data files. + */ + @Override + public boolean isImplicitWithStorage() { + return true; + } + + /** + * Lookup the location for each record key and return the pair for all record keys already + * present and drop the record keys if not present. + */ + private JavaPairRDD lookupIndex( + JavaPairRDD partitionRecordKeyPairRDD, final JavaSparkContext jsc, + final HoodieTable hoodieTable) { + // Obtain records per partition, in the incoming records + Map recordsPerPartition = partitionRecordKeyPairRDD.countByKey(); + List affectedPartitionPathList = new ArrayList<>(recordsPerPartition.keySet()); + + // Step 2: Load all involved files as pairs + List> fileInfoList = + loadInvolvedFileIds(affectedPartitionPathList, jsc, hoodieTable); + + return findMatchingFilesForRecordKeys(jsc, fileInfoList, partitionRecordKeyPairRDD, hoodieTable); + } + + /** + * Load all involved files as pair RDD. + */ + @VisibleForTesting + List> loadInvolvedFileIds(List partitions, final JavaSparkContext jsc, Review comment: code reuse please ---------------------------------------------------------------- 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: users@infra.apache.org With regards, Apache Git Services