You are viewing a plain text version of this content. The canonical link for it is here.
Posted to gitbox@hive.apache.org by GitBox <gi...@apache.org> on 2021/03/16 15:43:13 UTC

[GitHub] [hive] szlta commented on a change in pull request #2066: HIVE-24727: Cache hydration api in llap proto

szlta commented on a change in pull request #2066:
URL: https://github.com/apache/hive/pull/2066#discussion_r595243094



##########
File path: llap-server/src/java/org/apache/hadoop/hive/llap/io/api/impl/LlapIoImpl.java
##########
@@ -438,4 +451,20 @@ public OrcTail getOrcTailFromCache(Path path, Configuration jobConf, CacheTag ta
       throw new IOException(e);
     }
   }
+
+  @Override
+  public LlapDaemonProtocolProtos.CacheEntryList fetchCachedMetadata() {
+    GenericDataCache cache = new GenericDataCache(dataCache, bufferManager);
+    LlapCacheMetadataSerializer serializer = new LlapCacheMetadataSerializer(fileMetadataCache, cache, daemonConf,
+        pathCache, tracePool, realCachePolicy);
+    return serializer.fetchMetadata();
+  }
+
+  @Override
+  public void loadDataIntoCache(LlapDaemonProtocolProtos.CacheEntryList metadata) {
+    GenericDataCache cache = new GenericDataCache(dataCache, bufferManager);
+    LlapCacheMetadataSerializer serializer = new LlapCacheMetadataSerializer(fileMetadataCache, cache, daemonConf,
+        pathCache, tracePool, realCachePolicy);

Review comment:
       fileMetadataCache, realCachePolicy, etc.. can be null. Please check as per how "useLowLevelCache" is used in the private CTOR above.

##########
File path: llap-server/src/java/org/apache/hadoop/hive/llap/io/api/impl/LlapIoImpl.java
##########
@@ -18,24 +18,36 @@
 
 package org.apache.hadoop.hive.llap.io.api.impl;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInput;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Comparator;
+import java.util.HashMap;
 import java.util.List;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.TimeUnit;
 import java.util.function.Predicate;
+import java.util.stream.Collectors;
 
 import javax.management.ObjectName;
 
+import com.google.protobuf.ByteString;
 import org.apache.hadoop.fs.Path;
 import org.apache.hadoop.hive.common.io.CacheTag;
 import org.apache.hadoop.hive.llap.ProactiveEviction;
 import org.apache.hadoop.hive.llap.cache.MemoryLimitedPathCache;
 import org.apache.hadoop.hive.llap.cache.PathCache;
+import org.apache.hadoop.hive.llap.cache.LlapCacheableBuffer;
 import org.apache.hadoop.hive.llap.cache.ProactiveEvictingCachePolicy;
 import org.apache.hadoop.hive.llap.daemon.impl.StatsRecordingThreadPool;
+import org.apache.hadoop.hive.llap.io.encoded.LlapOrcCacheLoader;
+import org.apache.hadoop.hive.ql.io.SyntheticFileId;

Review comment:
       Unused imports? On the other hand I can't seem to find the import for LlapCacheMetadataSerializer.

##########
File path: llap-server/src/java/org/apache/hadoop/hive/llap/io/api/impl/LlapCacheMetadataSerializer.java
##########
@@ -0,0 +1,202 @@
+/*
+ * 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.hadoop.hive.llap.io.api.impl;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.protobuf.ByteString;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hive.common.io.CacheTag;
+import org.apache.hadoop.hive.common.io.DataCache;
+import org.apache.hadoop.hive.common.io.DiskRangeList;
+import org.apache.hadoop.hive.common.io.FileMetadataCache;
+import org.apache.hadoop.hive.llap.cache.LlapCacheableBuffer;
+import org.apache.hadoop.hive.llap.cache.LlapDataBuffer;
+import org.apache.hadoop.hive.llap.cache.LowLevelCachePolicy;
+import org.apache.hadoop.hive.llap.cache.PathCache;
+import org.apache.hadoop.hive.llap.daemon.rpc.LlapDaemonProtocolProtos;
+import org.apache.hadoop.hive.llap.io.encoded.LlapOrcCacheLoader;
+import org.apache.hadoop.hive.ql.io.SyntheticFileId;
+import org.apache.hadoop.hive.ql.io.orc.encoded.IoTrace;
+import org.apache.hive.common.util.FixedSizedObjectPool;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInput;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * Internal helper class for extracting the metadata of the cache content and loading data into the cache
+ * based on the provided metadata.
+ */
+final class LlapCacheMetadataSerializer {
+
+  public static final Logger LOG = LoggerFactory.getLogger(LlapCacheMetadataSerializer.class);
+
+  private final FileMetadataCache metadataCache;
+  private final DataCache cache;
+  private final Configuration conf;
+  private final PathCache pathCache;
+  private final FixedSizedObjectPool<IoTrace> tracePool;
+  private final LowLevelCachePolicy cachePolicy;
+
+  LlapCacheMetadataSerializer(FileMetadataCache fileMetadataCache, DataCache cache, Configuration daemonConf,
+      PathCache pathCache, FixedSizedObjectPool<IoTrace> tracePool, LowLevelCachePolicy realCachePolicy) {
+    this.metadataCache = fileMetadataCache;
+    this.cache = cache;
+    this.conf = daemonConf;
+    this.pathCache = pathCache;
+    this.tracePool = tracePool;
+    this.cachePolicy = realCachePolicy;
+
+  }
+
+  public LlapDaemonProtocolProtos.CacheEntryList fetchMetadata() {
+    List<LlapCacheableBuffer> buffers = cachePolicy.getHotBuffers();
+    List<LlapDaemonProtocolProtos.CacheEntry> entries = encodeAndConvertHotBuffers(buffers);
+    return LlapDaemonProtocolProtos.CacheEntryList.newBuilder().addAllEntries(entries).build();
+  }
+
+  private List<LlapDaemonProtocolProtos.CacheEntry> encodeAndConvertHotBuffers(List<LlapCacheableBuffer> buffers) {
+    Map<Object, LlapDaemonProtocolProtos.CacheEntry.Builder> entries = encodeAndSortHotBuffersByFileKey(buffers);
+    return entries.values().stream().map(v -> v.build()).collect(Collectors.toList());
+  }
+
+  private Map<Object, LlapDaemonProtocolProtos.CacheEntry.Builder> encodeAndSortHotBuffersByFileKey(
+      List<LlapCacheableBuffer> buffers) {
+    Map<Object, LlapDaemonProtocolProtos.CacheEntry.Builder> lookupMap = new HashMap<>();
+    for (LlapCacheableBuffer b : buffers) {
+      if (b instanceof LlapDataBuffer) {
+        LlapDataBuffer db = (LlapDataBuffer) b;
+        try {
+          Object fileKey = db.getFileKey();
+          String path = pathCache.resolve(db.getFileKey());
+          if (path != null) {
+            LlapDaemonProtocolProtos.CacheEntry.Builder builder =
+                lookupOrCreateCacheEntryFromDataBuffer(lookupMap, db, fileKey, path);
+            LlapDaemonProtocolProtos.CacheEntryRange.Builder range =
+                LlapDaemonProtocolProtos.CacheEntryRange.newBuilder().setStart(db.getStart())
+                    .setEnd(db.getStart() + db.declaredCachedLength);
+            builder.addRanges(range);
+          }
+        } catch (IOException ex) {
+          // This should never happen. It only happens if we can't decode the fileKey.
+          LOG.warn("Skip buffer from CacheEntryList.", ex);
+        }
+      }
+    }
+    return lookupMap;
+  }
+
+  private LlapDaemonProtocolProtos.CacheEntry.Builder lookupOrCreateCacheEntryFromDataBuffer(
+      Map<Object, LlapDaemonProtocolProtos.CacheEntry.Builder> lookupMap, LlapDataBuffer db, Object fileKey,
+      String path) throws IOException {
+    LlapDaemonProtocolProtos.CacheEntry.Builder builder = lookupMap.get(fileKey);
+    if (builder == null) {
+      ByteString encodedFileKey = encodeFileKey(fileKey);
+      LlapDaemonProtocolProtos.CacheTag.Builder ctb = encodeCacheTag(db.getTag());
+      builder = LlapDaemonProtocolProtos.CacheEntry.newBuilder().setFileKey(encodedFileKey).setCacheTag(ctb)
+          .setFilePath(path);
+
+      lookupMap.put(fileKey, builder);
+    }
+    return builder;
+  }
+
+  private LlapDaemonProtocolProtos.CacheTag.Builder encodeCacheTag(CacheTag cacheTag) {

Review comment:
       Could be static?

##########
File path: llap-server/src/java/org/apache/hadoop/hive/llap/io/api/impl/LlapCacheMetadataSerializer.java
##########
@@ -0,0 +1,202 @@
+/*
+ * 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.hadoop.hive.llap.io.api.impl;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.protobuf.ByteString;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hive.common.io.CacheTag;
+import org.apache.hadoop.hive.common.io.DataCache;
+import org.apache.hadoop.hive.common.io.DiskRangeList;
+import org.apache.hadoop.hive.common.io.FileMetadataCache;
+import org.apache.hadoop.hive.llap.cache.LlapCacheableBuffer;
+import org.apache.hadoop.hive.llap.cache.LlapDataBuffer;
+import org.apache.hadoop.hive.llap.cache.LowLevelCachePolicy;
+import org.apache.hadoop.hive.llap.cache.PathCache;
+import org.apache.hadoop.hive.llap.daemon.rpc.LlapDaemonProtocolProtos;
+import org.apache.hadoop.hive.llap.io.encoded.LlapOrcCacheLoader;
+import org.apache.hadoop.hive.ql.io.SyntheticFileId;
+import org.apache.hadoop.hive.ql.io.orc.encoded.IoTrace;
+import org.apache.hive.common.util.FixedSizedObjectPool;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInput;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * Internal helper class for extracting the metadata of the cache content and loading data into the cache
+ * based on the provided metadata.
+ */
+final class LlapCacheMetadataSerializer {
+
+  public static final Logger LOG = LoggerFactory.getLogger(LlapCacheMetadataSerializer.class);
+
+  private final FileMetadataCache metadataCache;
+  private final DataCache cache;
+  private final Configuration conf;
+  private final PathCache pathCache;
+  private final FixedSizedObjectPool<IoTrace> tracePool;
+  private final LowLevelCachePolicy cachePolicy;
+
+  LlapCacheMetadataSerializer(FileMetadataCache fileMetadataCache, DataCache cache, Configuration daemonConf,
+      PathCache pathCache, FixedSizedObjectPool<IoTrace> tracePool, LowLevelCachePolicy realCachePolicy) {
+    this.metadataCache = fileMetadataCache;
+    this.cache = cache;
+    this.conf = daemonConf;
+    this.pathCache = pathCache;
+    this.tracePool = tracePool;
+    this.cachePolicy = realCachePolicy;
+
+  }
+
+  public LlapDaemonProtocolProtos.CacheEntryList fetchMetadata() {

Review comment:
       Same naming observation: fetchContentInfo or something rather than "metadata".

##########
File path: llap-client/src/java/org/apache/hadoop/hive/llap/io/api/LlapIo.java
##########
@@ -87,4 +87,15 @@
    */
   RecordReader<NullWritable, VectorizedRowBatch> llapVectorizedOrcReaderForPath(Object fileKey, Path path, CacheTag tag,
       List<Integer> tableIncludedCols, JobConf conf, long offset, long length) throws IOException;
+
+  /**
+   * Extract and return the cache content metadata.
+   */
+  LlapDaemonProtocolProtos.CacheEntryList fetchCachedMetadata();

Review comment:
       Sounds like we're fetching the cached metadata (e.g. Orc file tails).
   How about: fetchCachedContentInfo() ?

##########
File path: llap-server/src/java/org/apache/hadoop/hive/llap/io/api/impl/LlapCacheMetadataSerializer.java
##########
@@ -0,0 +1,202 @@
+/*
+ * 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.hadoop.hive.llap.io.api.impl;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.protobuf.ByteString;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hive.common.io.CacheTag;
+import org.apache.hadoop.hive.common.io.DataCache;
+import org.apache.hadoop.hive.common.io.DiskRangeList;
+import org.apache.hadoop.hive.common.io.FileMetadataCache;
+import org.apache.hadoop.hive.llap.cache.LlapCacheableBuffer;
+import org.apache.hadoop.hive.llap.cache.LlapDataBuffer;
+import org.apache.hadoop.hive.llap.cache.LowLevelCachePolicy;
+import org.apache.hadoop.hive.llap.cache.PathCache;
+import org.apache.hadoop.hive.llap.daemon.rpc.LlapDaemonProtocolProtos;
+import org.apache.hadoop.hive.llap.io.encoded.LlapOrcCacheLoader;
+import org.apache.hadoop.hive.ql.io.SyntheticFileId;
+import org.apache.hadoop.hive.ql.io.orc.encoded.IoTrace;
+import org.apache.hive.common.util.FixedSizedObjectPool;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInput;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * Internal helper class for extracting the metadata of the cache content and loading data into the cache
+ * based on the provided metadata.
+ */
+final class LlapCacheMetadataSerializer {
+
+  public static final Logger LOG = LoggerFactory.getLogger(LlapCacheMetadataSerializer.class);
+
+  private final FileMetadataCache metadataCache;
+  private final DataCache cache;
+  private final Configuration conf;
+  private final PathCache pathCache;
+  private final FixedSizedObjectPool<IoTrace> tracePool;
+  private final LowLevelCachePolicy cachePolicy;
+
+  LlapCacheMetadataSerializer(FileMetadataCache fileMetadataCache, DataCache cache, Configuration daemonConf,
+      PathCache pathCache, FixedSizedObjectPool<IoTrace> tracePool, LowLevelCachePolicy realCachePolicy) {
+    this.metadataCache = fileMetadataCache;
+    this.cache = cache;
+    this.conf = daemonConf;
+    this.pathCache = pathCache;
+    this.tracePool = tracePool;
+    this.cachePolicy = realCachePolicy;
+
+  }
+
+  public LlapDaemonProtocolProtos.CacheEntryList fetchMetadata() {
+    List<LlapCacheableBuffer> buffers = cachePolicy.getHotBuffers();
+    List<LlapDaemonProtocolProtos.CacheEntry> entries = encodeAndConvertHotBuffers(buffers);
+    return LlapDaemonProtocolProtos.CacheEntryList.newBuilder().addAllEntries(entries).build();
+  }
+
+  private List<LlapDaemonProtocolProtos.CacheEntry> encodeAndConvertHotBuffers(List<LlapCacheableBuffer> buffers) {
+    Map<Object, LlapDaemonProtocolProtos.CacheEntry.Builder> entries = encodeAndSortHotBuffersByFileKey(buffers);
+    return entries.values().stream().map(v -> v.build()).collect(Collectors.toList());
+  }
+
+  private Map<Object, LlapDaemonProtocolProtos.CacheEntry.Builder> encodeAndSortHotBuffersByFileKey(
+      List<LlapCacheableBuffer> buffers) {
+    Map<Object, LlapDaemonProtocolProtos.CacheEntry.Builder> lookupMap = new HashMap<>();
+    for (LlapCacheableBuffer b : buffers) {
+      if (b instanceof LlapDataBuffer) {
+        LlapDataBuffer db = (LlapDataBuffer) b;
+        try {
+          Object fileKey = db.getFileKey();
+          String path = pathCache.resolve(db.getFileKey());
+          if (path != null) {
+            LlapDaemonProtocolProtos.CacheEntry.Builder builder =
+                lookupOrCreateCacheEntryFromDataBuffer(lookupMap, db, fileKey, path);
+            LlapDaemonProtocolProtos.CacheEntryRange.Builder range =
+                LlapDaemonProtocolProtos.CacheEntryRange.newBuilder().setStart(db.getStart())
+                    .setEnd(db.getStart() + db.declaredCachedLength);
+            builder.addRanges(range);
+          }
+        } catch (IOException ex) {
+          // This should never happen. It only happens if we can't decode the fileKey.
+          LOG.warn("Skip buffer from CacheEntryList.", ex);
+        }
+      }
+    }
+    return lookupMap;
+  }
+
+  private LlapDaemonProtocolProtos.CacheEntry.Builder lookupOrCreateCacheEntryFromDataBuffer(

Review comment:
       Could be static?

##########
File path: llap-server/src/java/org/apache/hadoop/hive/llap/io/api/impl/LlapCacheMetadataSerializer.java
##########
@@ -0,0 +1,202 @@
+/*
+ * 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.hadoop.hive.llap.io.api.impl;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.protobuf.ByteString;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hive.common.io.CacheTag;
+import org.apache.hadoop.hive.common.io.DataCache;
+import org.apache.hadoop.hive.common.io.DiskRangeList;
+import org.apache.hadoop.hive.common.io.FileMetadataCache;
+import org.apache.hadoop.hive.llap.cache.LlapCacheableBuffer;
+import org.apache.hadoop.hive.llap.cache.LlapDataBuffer;
+import org.apache.hadoop.hive.llap.cache.LowLevelCachePolicy;
+import org.apache.hadoop.hive.llap.cache.PathCache;
+import org.apache.hadoop.hive.llap.daemon.rpc.LlapDaemonProtocolProtos;
+import org.apache.hadoop.hive.llap.io.encoded.LlapOrcCacheLoader;
+import org.apache.hadoop.hive.ql.io.SyntheticFileId;
+import org.apache.hadoop.hive.ql.io.orc.encoded.IoTrace;
+import org.apache.hive.common.util.FixedSizedObjectPool;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInput;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * Internal helper class for extracting the metadata of the cache content and loading data into the cache
+ * based on the provided metadata.
+ */
+final class LlapCacheMetadataSerializer {
+
+  public static final Logger LOG = LoggerFactory.getLogger(LlapCacheMetadataSerializer.class);
+
+  private final FileMetadataCache metadataCache;
+  private final DataCache cache;
+  private final Configuration conf;
+  private final PathCache pathCache;
+  private final FixedSizedObjectPool<IoTrace> tracePool;
+  private final LowLevelCachePolicy cachePolicy;
+
+  LlapCacheMetadataSerializer(FileMetadataCache fileMetadataCache, DataCache cache, Configuration daemonConf,
+      PathCache pathCache, FixedSizedObjectPool<IoTrace> tracePool, LowLevelCachePolicy realCachePolicy) {
+    this.metadataCache = fileMetadataCache;
+    this.cache = cache;
+    this.conf = daemonConf;
+    this.pathCache = pathCache;
+    this.tracePool = tracePool;
+    this.cachePolicy = realCachePolicy;
+
+  }
+
+  public LlapDaemonProtocolProtos.CacheEntryList fetchMetadata() {
+    List<LlapCacheableBuffer> buffers = cachePolicy.getHotBuffers();
+    List<LlapDaemonProtocolProtos.CacheEntry> entries = encodeAndConvertHotBuffers(buffers);
+    return LlapDaemonProtocolProtos.CacheEntryList.newBuilder().addAllEntries(entries).build();
+  }
+
+  private List<LlapDaemonProtocolProtos.CacheEntry> encodeAndConvertHotBuffers(List<LlapCacheableBuffer> buffers) {
+    Map<Object, LlapDaemonProtocolProtos.CacheEntry.Builder> entries = encodeAndSortHotBuffersByFileKey(buffers);
+    return entries.values().stream().map(v -> v.build()).collect(Collectors.toList());
+  }
+
+  private Map<Object, LlapDaemonProtocolProtos.CacheEntry.Builder> encodeAndSortHotBuffersByFileKey(
+      List<LlapCacheableBuffer> buffers) {
+    Map<Object, LlapDaemonProtocolProtos.CacheEntry.Builder> lookupMap = new HashMap<>();
+    for (LlapCacheableBuffer b : buffers) {
+      if (b instanceof LlapDataBuffer) {
+        LlapDataBuffer db = (LlapDataBuffer) b;
+        try {
+          Object fileKey = db.getFileKey();
+          String path = pathCache.resolve(db.getFileKey());
+          if (path != null) {
+            LlapDaemonProtocolProtos.CacheEntry.Builder builder =
+                lookupOrCreateCacheEntryFromDataBuffer(lookupMap, db, fileKey, path);
+            LlapDaemonProtocolProtos.CacheEntryRange.Builder range =
+                LlapDaemonProtocolProtos.CacheEntryRange.newBuilder().setStart(db.getStart())
+                    .setEnd(db.getStart() + db.declaredCachedLength);
+            builder.addRanges(range);
+          }
+        } catch (IOException ex) {
+          // This should never happen. It only happens if we can't decode the fileKey.
+          LOG.warn("Skip buffer from CacheEntryList.", ex);
+        }
+      }
+    }
+    return lookupMap;
+  }
+
+  private LlapDaemonProtocolProtos.CacheEntry.Builder lookupOrCreateCacheEntryFromDataBuffer(
+      Map<Object, LlapDaemonProtocolProtos.CacheEntry.Builder> lookupMap, LlapDataBuffer db, Object fileKey,
+      String path) throws IOException {
+    LlapDaemonProtocolProtos.CacheEntry.Builder builder = lookupMap.get(fileKey);
+    if (builder == null) {
+      ByteString encodedFileKey = encodeFileKey(fileKey);
+      LlapDaemonProtocolProtos.CacheTag.Builder ctb = encodeCacheTag(db.getTag());
+      builder = LlapDaemonProtocolProtos.CacheEntry.newBuilder().setFileKey(encodedFileKey).setCacheTag(ctb)
+          .setFilePath(path);
+
+      lookupMap.put(fileKey, builder);
+    }
+    return builder;
+  }
+
+  private LlapDaemonProtocolProtos.CacheTag.Builder encodeCacheTag(CacheTag cacheTag) {
+    LlapDaemonProtocolProtos.CacheTag.Builder ctb =
+        LlapDaemonProtocolProtos.CacheTag.newBuilder().setTableName(cacheTag.getTableName());
+    if (cacheTag instanceof CacheTag.PartitionCacheTag) {
+      CacheTag.PartitionCacheTag partitionCacheTag = (CacheTag.PartitionCacheTag) cacheTag;
+      ctb.addAllPartitionDesc(Arrays.asList((partitionCacheTag.getEncodedPartitionDesc())));
+    }
+    return ctb;
+  }
+
+  public void loadData(LlapDaemonProtocolProtos.CacheEntryList data) {
+    for (LlapDaemonProtocolProtos.CacheEntry ce : data.getEntriesList()) {
+      try {
+        loadData(ce);
+      } catch (IOException ex) {
+        LOG.warn("Skip cache entry load to the cache.", ex);
+      }
+    }
+  }
+
+  private void loadData(LlapDaemonProtocolProtos.CacheEntry ce) throws IOException {
+    CacheTag cacheTag = decodeCacheTag(ce.getCacheTag());
+    DiskRangeList ranges = decodeRanges(ce.getRangesList());
+    Object fileKey = decodeFileKey(ce.getFileKey());
+    try (LlapOrcCacheLoader llr = new LlapOrcCacheLoader(new Path(ce.getFilePath()), fileKey, conf, cache,
+        metadataCache, cacheTag, tracePool)) {
+      llr.init();
+      llr.loadFileFooter();
+      llr.loadRanges(ranges);
+    }
+  }
+
+  private DiskRangeList decodeRanges(List<LlapDaemonProtocolProtos.CacheEntryRange> ranges) {

Review comment:
       Could be static?

##########
File path: llap-server/src/java/org/apache/hadoop/hive/llap/io/api/impl/LlapCacheMetadataSerializer.java
##########
@@ -0,0 +1,202 @@
+/*
+ * 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.hadoop.hive.llap.io.api.impl;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.protobuf.ByteString;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hive.common.io.CacheTag;
+import org.apache.hadoop.hive.common.io.DataCache;
+import org.apache.hadoop.hive.common.io.DiskRangeList;
+import org.apache.hadoop.hive.common.io.FileMetadataCache;
+import org.apache.hadoop.hive.llap.cache.LlapCacheableBuffer;
+import org.apache.hadoop.hive.llap.cache.LlapDataBuffer;
+import org.apache.hadoop.hive.llap.cache.LowLevelCachePolicy;
+import org.apache.hadoop.hive.llap.cache.PathCache;
+import org.apache.hadoop.hive.llap.daemon.rpc.LlapDaemonProtocolProtos;
+import org.apache.hadoop.hive.llap.io.encoded.LlapOrcCacheLoader;
+import org.apache.hadoop.hive.ql.io.SyntheticFileId;
+import org.apache.hadoop.hive.ql.io.orc.encoded.IoTrace;
+import org.apache.hive.common.util.FixedSizedObjectPool;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInput;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * Internal helper class for extracting the metadata of the cache content and loading data into the cache
+ * based on the provided metadata.
+ */
+final class LlapCacheMetadataSerializer {
+
+  public static final Logger LOG = LoggerFactory.getLogger(LlapCacheMetadataSerializer.class);
+
+  private final FileMetadataCache metadataCache;
+  private final DataCache cache;
+  private final Configuration conf;
+  private final PathCache pathCache;
+  private final FixedSizedObjectPool<IoTrace> tracePool;
+  private final LowLevelCachePolicy cachePolicy;
+
+  LlapCacheMetadataSerializer(FileMetadataCache fileMetadataCache, DataCache cache, Configuration daemonConf,
+      PathCache pathCache, FixedSizedObjectPool<IoTrace> tracePool, LowLevelCachePolicy realCachePolicy) {
+    this.metadataCache = fileMetadataCache;
+    this.cache = cache;
+    this.conf = daemonConf;
+    this.pathCache = pathCache;
+    this.tracePool = tracePool;
+    this.cachePolicy = realCachePolicy;
+
+  }
+
+  public LlapDaemonProtocolProtos.CacheEntryList fetchMetadata() {
+    List<LlapCacheableBuffer> buffers = cachePolicy.getHotBuffers();
+    List<LlapDaemonProtocolProtos.CacheEntry> entries = encodeAndConvertHotBuffers(buffers);
+    return LlapDaemonProtocolProtos.CacheEntryList.newBuilder().addAllEntries(entries).build();
+  }
+
+  private List<LlapDaemonProtocolProtos.CacheEntry> encodeAndConvertHotBuffers(List<LlapCacheableBuffer> buffers) {
+    Map<Object, LlapDaemonProtocolProtos.CacheEntry.Builder> entries = encodeAndSortHotBuffersByFileKey(buffers);
+    return entries.values().stream().map(v -> v.build()).collect(Collectors.toList());
+  }
+
+  private Map<Object, LlapDaemonProtocolProtos.CacheEntry.Builder> encodeAndSortHotBuffersByFileKey(
+      List<LlapCacheableBuffer> buffers) {
+    Map<Object, LlapDaemonProtocolProtos.CacheEntry.Builder> lookupMap = new HashMap<>();
+    for (LlapCacheableBuffer b : buffers) {
+      if (b instanceof LlapDataBuffer) {
+        LlapDataBuffer db = (LlapDataBuffer) b;
+        try {
+          Object fileKey = db.getFileKey();
+          String path = pathCache.resolve(db.getFileKey());
+          if (path != null) {
+            LlapDaemonProtocolProtos.CacheEntry.Builder builder =
+                lookupOrCreateCacheEntryFromDataBuffer(lookupMap, db, fileKey, path);
+            LlapDaemonProtocolProtos.CacheEntryRange.Builder range =
+                LlapDaemonProtocolProtos.CacheEntryRange.newBuilder().setStart(db.getStart())
+                    .setEnd(db.getStart() + db.declaredCachedLength);
+            builder.addRanges(range);
+          }
+        } catch (IOException ex) {
+          // This should never happen. It only happens if we can't decode the fileKey.
+          LOG.warn("Skip buffer from CacheEntryList.", ex);
+        }
+      }
+    }
+    return lookupMap;
+  }
+
+  private LlapDaemonProtocolProtos.CacheEntry.Builder lookupOrCreateCacheEntryFromDataBuffer(
+      Map<Object, LlapDaemonProtocolProtos.CacheEntry.Builder> lookupMap, LlapDataBuffer db, Object fileKey,
+      String path) throws IOException {
+    LlapDaemonProtocolProtos.CacheEntry.Builder builder = lookupMap.get(fileKey);
+    if (builder == null) {
+      ByteString encodedFileKey = encodeFileKey(fileKey);
+      LlapDaemonProtocolProtos.CacheTag.Builder ctb = encodeCacheTag(db.getTag());
+      builder = LlapDaemonProtocolProtos.CacheEntry.newBuilder().setFileKey(encodedFileKey).setCacheTag(ctb)
+          .setFilePath(path);
+
+      lookupMap.put(fileKey, builder);
+    }
+    return builder;
+  }
+
+  private LlapDaemonProtocolProtos.CacheTag.Builder encodeCacheTag(CacheTag cacheTag) {
+    LlapDaemonProtocolProtos.CacheTag.Builder ctb =
+        LlapDaemonProtocolProtos.CacheTag.newBuilder().setTableName(cacheTag.getTableName());
+    if (cacheTag instanceof CacheTag.PartitionCacheTag) {
+      CacheTag.PartitionCacheTag partitionCacheTag = (CacheTag.PartitionCacheTag) cacheTag;
+      ctb.addAllPartitionDesc(Arrays.asList((partitionCacheTag.getEncodedPartitionDesc())));
+    }
+    return ctb;
+  }
+
+  public void loadData(LlapDaemonProtocolProtos.CacheEntryList data) {
+    for (LlapDaemonProtocolProtos.CacheEntry ce : data.getEntriesList()) {
+      try {
+        loadData(ce);
+      } catch (IOException ex) {
+        LOG.warn("Skip cache entry load to the cache.", ex);
+      }
+    }
+  }
+
+  private void loadData(LlapDaemonProtocolProtos.CacheEntry ce) throws IOException {
+    CacheTag cacheTag = decodeCacheTag(ce.getCacheTag());
+    DiskRangeList ranges = decodeRanges(ce.getRangesList());
+    Object fileKey = decodeFileKey(ce.getFileKey());
+    try (LlapOrcCacheLoader llr = new LlapOrcCacheLoader(new Path(ce.getFilePath()), fileKey, conf, cache,
+        metadataCache, cacheTag, tracePool)) {
+      llr.init();
+      llr.loadFileFooter();
+      llr.loadRanges(ranges);
+    }
+  }
+
+  private DiskRangeList decodeRanges(List<LlapDaemonProtocolProtos.CacheEntryRange> ranges) {
+    DiskRangeList.CreateHelper helper = new DiskRangeList.CreateHelper();
+    ranges.stream().sorted(Comparator.comparing(LlapDaemonProtocolProtos.CacheEntryRange::getStart))
+        .forEach(r -> helper.addOrMerge(r.getStart(), r.getEnd(), false, false));
+    return helper.get();
+  }
+
+  private CacheTag decodeCacheTag(LlapDaemonProtocolProtos.CacheTag ct) {
+    return ct.getPartitionDescCount() == 0 ? CacheTag.build(ct.getTableName()) : CacheTag
+        .build(ct.getTableName(), ct.getPartitionDescList());
+  }
+
+  @VisibleForTesting
+  ByteString encodeFileKey(Object fileKey) throws IOException {
+    ByteArrayOutputStream baos = new ByteArrayOutputStream();
+    DataOutputStream dos = new DataOutputStream(baos);
+    if (fileKey instanceof SyntheticFileId) {
+      SyntheticFileId fk = (SyntheticFileId) fileKey;
+      fk.write(dos);
+    } else {
+      dos.writeLong((Long) fileKey);
+    }
+    return ByteString.copyFrom(baos.toByteArray());
+  }
+
+  @VisibleForTesting
+  Object decodeFileKey(ByteString encodedFileKey) throws IOException {
+    byte[] bytes = encodedFileKey.toByteArray();
+    DataInput in = new DataInputStream(new ByteArrayInputStream(bytes));
+    Object fileKey;
+    if (bytes.length == Long.BYTES) {
+      fileKey = in.readLong();
+    } else {
+      SyntheticFileId fileId = new SyntheticFileId();

Review comment:
       I'm fine with not writing an indicator part for file ID type, but let's add some more comments then to explain in what cases is this Long or Synthetic

##########
File path: llap-server/src/java/org/apache/hadoop/hive/llap/io/api/impl/LlapCacheMetadataSerializer.java
##########
@@ -0,0 +1,202 @@
+/*
+ * 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.hadoop.hive.llap.io.api.impl;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.protobuf.ByteString;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hive.common.io.CacheTag;
+import org.apache.hadoop.hive.common.io.DataCache;
+import org.apache.hadoop.hive.common.io.DiskRangeList;
+import org.apache.hadoop.hive.common.io.FileMetadataCache;
+import org.apache.hadoop.hive.llap.cache.LlapCacheableBuffer;
+import org.apache.hadoop.hive.llap.cache.LlapDataBuffer;
+import org.apache.hadoop.hive.llap.cache.LowLevelCachePolicy;
+import org.apache.hadoop.hive.llap.cache.PathCache;
+import org.apache.hadoop.hive.llap.daemon.rpc.LlapDaemonProtocolProtos;
+import org.apache.hadoop.hive.llap.io.encoded.LlapOrcCacheLoader;
+import org.apache.hadoop.hive.ql.io.SyntheticFileId;
+import org.apache.hadoop.hive.ql.io.orc.encoded.IoTrace;
+import org.apache.hive.common.util.FixedSizedObjectPool;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInput;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * Internal helper class for extracting the metadata of the cache content and loading data into the cache
+ * based on the provided metadata.
+ */
+final class LlapCacheMetadataSerializer {
+
+  public static final Logger LOG = LoggerFactory.getLogger(LlapCacheMetadataSerializer.class);
+
+  private final FileMetadataCache metadataCache;
+  private final DataCache cache;
+  private final Configuration conf;
+  private final PathCache pathCache;
+  private final FixedSizedObjectPool<IoTrace> tracePool;
+  private final LowLevelCachePolicy cachePolicy;
+
+  LlapCacheMetadataSerializer(FileMetadataCache fileMetadataCache, DataCache cache, Configuration daemonConf,
+      PathCache pathCache, FixedSizedObjectPool<IoTrace> tracePool, LowLevelCachePolicy realCachePolicy) {
+    this.metadataCache = fileMetadataCache;
+    this.cache = cache;
+    this.conf = daemonConf;
+    this.pathCache = pathCache;
+    this.tracePool = tracePool;
+    this.cachePolicy = realCachePolicy;
+
+  }
+
+  public LlapDaemonProtocolProtos.CacheEntryList fetchMetadata() {
+    List<LlapCacheableBuffer> buffers = cachePolicy.getHotBuffers();
+    List<LlapDaemonProtocolProtos.CacheEntry> entries = encodeAndConvertHotBuffers(buffers);
+    return LlapDaemonProtocolProtos.CacheEntryList.newBuilder().addAllEntries(entries).build();
+  }
+
+  private List<LlapDaemonProtocolProtos.CacheEntry> encodeAndConvertHotBuffers(List<LlapCacheableBuffer> buffers) {
+    Map<Object, LlapDaemonProtocolProtos.CacheEntry.Builder> entries = encodeAndSortHotBuffersByFileKey(buffers);
+    return entries.values().stream().map(v -> v.build()).collect(Collectors.toList());
+  }
+
+  private Map<Object, LlapDaemonProtocolProtos.CacheEntry.Builder> encodeAndSortHotBuffersByFileKey(
+      List<LlapCacheableBuffer> buffers) {
+    Map<Object, LlapDaemonProtocolProtos.CacheEntry.Builder> lookupMap = new HashMap<>();
+    for (LlapCacheableBuffer b : buffers) {
+      if (b instanceof LlapDataBuffer) {
+        LlapDataBuffer db = (LlapDataBuffer) b;
+        try {
+          Object fileKey = db.getFileKey();
+          String path = pathCache.resolve(db.getFileKey());
+          if (path != null) {
+            LlapDaemonProtocolProtos.CacheEntry.Builder builder =
+                lookupOrCreateCacheEntryFromDataBuffer(lookupMap, db, fileKey, path);
+            LlapDaemonProtocolProtos.CacheEntryRange.Builder range =
+                LlapDaemonProtocolProtos.CacheEntryRange.newBuilder().setStart(db.getStart())
+                    .setEnd(db.getStart() + db.declaredCachedLength);
+            builder.addRanges(range);
+          }
+        } catch (IOException ex) {
+          // This should never happen. It only happens if we can't decode the fileKey.
+          LOG.warn("Skip buffer from CacheEntryList.", ex);
+        }
+      }
+    }
+    return lookupMap;
+  }
+
+  private LlapDaemonProtocolProtos.CacheEntry.Builder lookupOrCreateCacheEntryFromDataBuffer(
+      Map<Object, LlapDaemonProtocolProtos.CacheEntry.Builder> lookupMap, LlapDataBuffer db, Object fileKey,
+      String path) throws IOException {
+    LlapDaemonProtocolProtos.CacheEntry.Builder builder = lookupMap.get(fileKey);
+    if (builder == null) {
+      ByteString encodedFileKey = encodeFileKey(fileKey);
+      LlapDaemonProtocolProtos.CacheTag.Builder ctb = encodeCacheTag(db.getTag());
+      builder = LlapDaemonProtocolProtos.CacheEntry.newBuilder().setFileKey(encodedFileKey).setCacheTag(ctb)
+          .setFilePath(path);
+
+      lookupMap.put(fileKey, builder);
+    }
+    return builder;
+  }
+
+  private LlapDaemonProtocolProtos.CacheTag.Builder encodeCacheTag(CacheTag cacheTag) {
+    LlapDaemonProtocolProtos.CacheTag.Builder ctb =
+        LlapDaemonProtocolProtos.CacheTag.newBuilder().setTableName(cacheTag.getTableName());
+    if (cacheTag instanceof CacheTag.PartitionCacheTag) {
+      CacheTag.PartitionCacheTag partitionCacheTag = (CacheTag.PartitionCacheTag) cacheTag;
+      ctb.addAllPartitionDesc(Arrays.asList((partitionCacheTag.getEncodedPartitionDesc())));
+    }
+    return ctb;
+  }
+
+  public void loadData(LlapDaemonProtocolProtos.CacheEntryList data) {
+    for (LlapDaemonProtocolProtos.CacheEntry ce : data.getEntriesList()) {
+      try {
+        loadData(ce);
+      } catch (IOException ex) {
+        LOG.warn("Skip cache entry load to the cache.", ex);
+      }
+    }
+  }
+
+  private void loadData(LlapDaemonProtocolProtos.CacheEntry ce) throws IOException {
+    CacheTag cacheTag = decodeCacheTag(ce.getCacheTag());
+    DiskRangeList ranges = decodeRanges(ce.getRangesList());
+    Object fileKey = decodeFileKey(ce.getFileKey());
+    try (LlapOrcCacheLoader llr = new LlapOrcCacheLoader(new Path(ce.getFilePath()), fileKey, conf, cache,
+        metadataCache, cacheTag, tracePool)) {
+      llr.init();
+      llr.loadFileFooter();
+      llr.loadRanges(ranges);
+    }
+  }
+
+  private DiskRangeList decodeRanges(List<LlapDaemonProtocolProtos.CacheEntryRange> ranges) {
+    DiskRangeList.CreateHelper helper = new DiskRangeList.CreateHelper();
+    ranges.stream().sorted(Comparator.comparing(LlapDaemonProtocolProtos.CacheEntryRange::getStart))
+        .forEach(r -> helper.addOrMerge(r.getStart(), r.getEnd(), false, false));
+    return helper.get();
+  }
+
+  private CacheTag decodeCacheTag(LlapDaemonProtocolProtos.CacheTag ct) {
+    return ct.getPartitionDescCount() == 0 ? CacheTag.build(ct.getTableName()) : CacheTag
+        .build(ct.getTableName(), ct.getPartitionDescList());
+  }
+
+  @VisibleForTesting
+  ByteString encodeFileKey(Object fileKey) throws IOException {
+    ByteArrayOutputStream baos = new ByteArrayOutputStream();
+    DataOutputStream dos = new DataOutputStream(baos);
+    if (fileKey instanceof SyntheticFileId) {
+      SyntheticFileId fk = (SyntheticFileId) fileKey;
+      fk.write(dos);
+    } else {
+      dos.writeLong((Long) fileKey);
+    }
+    return ByteString.copyFrom(baos.toByteArray());
+  }
+
+  @VisibleForTesting
+  Object decodeFileKey(ByteString encodedFileKey) throws IOException {
+    byte[] bytes = encodedFileKey.toByteArray();
+    DataInput in = new DataInputStream(new ByteArrayInputStream(bytes));
+    Object fileKey;
+    if (bytes.length == Long.BYTES) {
+      fileKey = in.readLong();
+    } else {
+      SyntheticFileId fileId = new SyntheticFileId();
+      fileId.readFields(in);
+      fileKey = fileId;
+    }
+    return fileKey;
+  }

Review comment:
       These methods could be static.
   Also shouldn't in- and outputstreams be closed in a finally clause?

##########
File path: llap-server/src/java/org/apache/hadoop/hive/llap/io/api/impl/LlapCacheMetadataSerializer.java
##########
@@ -0,0 +1,202 @@
+/*
+ * 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.hadoop.hive.llap.io.api.impl;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.protobuf.ByteString;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hive.common.io.CacheTag;
+import org.apache.hadoop.hive.common.io.DataCache;
+import org.apache.hadoop.hive.common.io.DiskRangeList;
+import org.apache.hadoop.hive.common.io.FileMetadataCache;
+import org.apache.hadoop.hive.llap.cache.LlapCacheableBuffer;
+import org.apache.hadoop.hive.llap.cache.LlapDataBuffer;
+import org.apache.hadoop.hive.llap.cache.LowLevelCachePolicy;
+import org.apache.hadoop.hive.llap.cache.PathCache;
+import org.apache.hadoop.hive.llap.daemon.rpc.LlapDaemonProtocolProtos;
+import org.apache.hadoop.hive.llap.io.encoded.LlapOrcCacheLoader;
+import org.apache.hadoop.hive.ql.io.SyntheticFileId;
+import org.apache.hadoop.hive.ql.io.orc.encoded.IoTrace;
+import org.apache.hive.common.util.FixedSizedObjectPool;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInput;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * Internal helper class for extracting the metadata of the cache content and loading data into the cache
+ * based on the provided metadata.
+ */
+final class LlapCacheMetadataSerializer {
+
+  public static final Logger LOG = LoggerFactory.getLogger(LlapCacheMetadataSerializer.class);
+
+  private final FileMetadataCache metadataCache;
+  private final DataCache cache;
+  private final Configuration conf;
+  private final PathCache pathCache;
+  private final FixedSizedObjectPool<IoTrace> tracePool;
+  private final LowLevelCachePolicy cachePolicy;
+
+  LlapCacheMetadataSerializer(FileMetadataCache fileMetadataCache, DataCache cache, Configuration daemonConf,
+      PathCache pathCache, FixedSizedObjectPool<IoTrace> tracePool, LowLevelCachePolicy realCachePolicy) {
+    this.metadataCache = fileMetadataCache;
+    this.cache = cache;
+    this.conf = daemonConf;
+    this.pathCache = pathCache;
+    this.tracePool = tracePool;
+    this.cachePolicy = realCachePolicy;
+
+  }
+
+  public LlapDaemonProtocolProtos.CacheEntryList fetchMetadata() {
+    List<LlapCacheableBuffer> buffers = cachePolicy.getHotBuffers();
+    List<LlapDaemonProtocolProtos.CacheEntry> entries = encodeAndConvertHotBuffers(buffers);
+    return LlapDaemonProtocolProtos.CacheEntryList.newBuilder().addAllEntries(entries).build();
+  }
+
+  private List<LlapDaemonProtocolProtos.CacheEntry> encodeAndConvertHotBuffers(List<LlapCacheableBuffer> buffers) {
+    Map<Object, LlapDaemonProtocolProtos.CacheEntry.Builder> entries = encodeAndSortHotBuffersByFileKey(buffers);
+    return entries.values().stream().map(v -> v.build()).collect(Collectors.toList());
+  }
+
+  private Map<Object, LlapDaemonProtocolProtos.CacheEntry.Builder> encodeAndSortHotBuffersByFileKey(
+      List<LlapCacheableBuffer> buffers) {
+    Map<Object, LlapDaemonProtocolProtos.CacheEntry.Builder> lookupMap = new HashMap<>();
+    for (LlapCacheableBuffer b : buffers) {
+      if (b instanceof LlapDataBuffer) {
+        LlapDataBuffer db = (LlapDataBuffer) b;
+        try {
+          Object fileKey = db.getFileKey();
+          String path = pathCache.resolve(db.getFileKey());
+          if (path != null) {
+            LlapDaemonProtocolProtos.CacheEntry.Builder builder =
+                lookupOrCreateCacheEntryFromDataBuffer(lookupMap, db, fileKey, path);
+            LlapDaemonProtocolProtos.CacheEntryRange.Builder range =
+                LlapDaemonProtocolProtos.CacheEntryRange.newBuilder().setStart(db.getStart())
+                    .setEnd(db.getStart() + db.declaredCachedLength);
+            builder.addRanges(range);
+          }
+        } catch (IOException ex) {
+          // This should never happen. It only happens if we can't decode the fileKey.
+          LOG.warn("Skip buffer from CacheEntryList.", ex);
+        }
+      }
+    }
+    return lookupMap;
+  }
+
+  private LlapDaemonProtocolProtos.CacheEntry.Builder lookupOrCreateCacheEntryFromDataBuffer(
+      Map<Object, LlapDaemonProtocolProtos.CacheEntry.Builder> lookupMap, LlapDataBuffer db, Object fileKey,
+      String path) throws IOException {
+    LlapDaemonProtocolProtos.CacheEntry.Builder builder = lookupMap.get(fileKey);
+    if (builder == null) {
+      ByteString encodedFileKey = encodeFileKey(fileKey);
+      LlapDaemonProtocolProtos.CacheTag.Builder ctb = encodeCacheTag(db.getTag());
+      builder = LlapDaemonProtocolProtos.CacheEntry.newBuilder().setFileKey(encodedFileKey).setCacheTag(ctb)
+          .setFilePath(path);
+
+      lookupMap.put(fileKey, builder);
+    }
+    return builder;
+  }
+
+  private LlapDaemonProtocolProtos.CacheTag.Builder encodeCacheTag(CacheTag cacheTag) {
+    LlapDaemonProtocolProtos.CacheTag.Builder ctb =
+        LlapDaemonProtocolProtos.CacheTag.newBuilder().setTableName(cacheTag.getTableName());
+    if (cacheTag instanceof CacheTag.PartitionCacheTag) {
+      CacheTag.PartitionCacheTag partitionCacheTag = (CacheTag.PartitionCacheTag) cacheTag;
+      ctb.addAllPartitionDesc(Arrays.asList((partitionCacheTag.getEncodedPartitionDesc())));
+    }
+    return ctb;
+  }
+
+  public void loadData(LlapDaemonProtocolProtos.CacheEntryList data) {
+    for (LlapDaemonProtocolProtos.CacheEntry ce : data.getEntriesList()) {
+      try {
+        loadData(ce);
+      } catch (IOException ex) {
+        LOG.warn("Skip cache entry load to the cache.", ex);
+      }
+    }
+  }
+
+  private void loadData(LlapDaemonProtocolProtos.CacheEntry ce) throws IOException {
+    CacheTag cacheTag = decodeCacheTag(ce.getCacheTag());
+    DiskRangeList ranges = decodeRanges(ce.getRangesList());
+    Object fileKey = decodeFileKey(ce.getFileKey());
+    try (LlapOrcCacheLoader llr = new LlapOrcCacheLoader(new Path(ce.getFilePath()), fileKey, conf, cache,
+        metadataCache, cacheTag, tracePool)) {
+      llr.init();
+      llr.loadFileFooter();
+      llr.loadRanges(ranges);
+    }
+  }
+
+  private DiskRangeList decodeRanges(List<LlapDaemonProtocolProtos.CacheEntryRange> ranges) {
+    DiskRangeList.CreateHelper helper = new DiskRangeList.CreateHelper();
+    ranges.stream().sorted(Comparator.comparing(LlapDaemonProtocolProtos.CacheEntryRange::getStart))
+        .forEach(r -> helper.addOrMerge(r.getStart(), r.getEnd(), false, false));
+    return helper.get();
+  }
+
+  private CacheTag decodeCacheTag(LlapDaemonProtocolProtos.CacheTag ct) {

Review comment:
       Could be static?

##########
File path: llap-server/src/java/org/apache/hadoop/hive/llap/daemon/impl/LlapProtocolServerImpl.java
##########
@@ -369,6 +372,13 @@ public GetTokenResponseProto getDelegationToken(RpcController controller,
     return responseProtoBuilder.build();
   }
 
+  @Override
+  public GetCacheContentResponseProto getCacheContent(RpcController controller,
+      GetCacheContentRequestProto request) {
+    CacheEntryList entries = LlapProxy.getIo().fetchCachedMetadata();

Review comment:
       I'd make a null check here on LlapIo instance. In such case you can return with null as result, as I see the proto changes allow that.




----------------------------------------------------------------
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



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org