deniskuzZ commented on code in PR #6961: URL: https://github.com/apache/hive/pull/6961#discussion_r4683932145
########## ql/src/java/org/apache/hadoop/hive/ql/metadata/client/QueryHMSCache.java: ########## @@ -0,0 +1,243 @@ +/* + * 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.ql.metadata.client; + +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.tuple.Pair; +import org.apache.hadoop.hive.metastore.api.GetTableRequest; +import org.apache.hadoop.hive.metastore.api.GetValidWriteIdsRequest; +import org.apache.hadoop.hive.metastore.api.NoSuchObjectException; +import org.apache.hadoop.hive.metastore.api.Table; +import org.apache.hadoop.hive.metastore.api.TableValidWriteIds; +import org.apache.hadoop.hive.ql.metadata.client.MetaStoreClientCacheUtils.CacheI; +import org.apache.hadoop.hive.ql.metadata.client.MetaStoreClientCacheUtils.CacheKey; +import org.apache.hadoop.hive.ql.metadata.client.MetaStoreClientCacheUtils.KeyType; +import org.apache.hadoop.hive.ql.metadata.client.MetaStoreClientCacheUtils.Supplier; +import org.apache.thrift.TException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Query-level cache of HMS requests/responses, owned by the QueryState and used by SessionHiveMetaStoreClient + * while the query compiles. + */ +public class QueryHMSCache implements CacheI { + private static final Logger LOG = LoggerFactory.getLogger(QueryHMSCache.class); + + private final Map<Object, Object> cache = new HashMap<>(); + + @Override + public void put(Object k, Object v) { + cache.put(k, v); + } + + @Override + public Object get(Object k) { + return cache.get(k); + } + + public void clear() { + cache.clear(); + } + + /** + * Returns the cached value of the key, or loads and caches it. + * @param method the client method, for logging + */ + @SuppressWarnings("unchecked") + public <T> T getOrLoad(String method, CacheKey key, Supplier<T, TException> loader) throws TException { + T v = (T) cache.get(key); + if (v == null) { + v = loader.get(); + cache.put(key, v); + } else { + LOG.debug("Query level HMS cache: method={}, key={}", method, key); + } + return v; + } + + /** + * Answers getTable(GetTableRequest): sets the cached table id on the request and caches the table also by name. + * A table missing from the metastore stays missing for the rest of the compilation; Executor drops these + * entries before the tasks run (see {@link #dropTablesNotFound()}). + * @param catName the catalog of the request, the default one if the request has none + */ + public Table getTable(GetTableRequest req, String catName, Supplier<Table, TException> loader) throws TException { + throwIfTableNotFound(catName, req.getDbName(), req.getTblName()); + CacheKey tableIdKey = new CacheKey(KeyType.TABLE_ID, req.getCatName(), req.getDbName(), req.getTblName()); Review Comment: the TABLE_ID key uses the raw req.getCatName(), while the not-found and by-name keys in this method use the normalized catName. Could we use catName here too? Otherwise a request with a null catalog and one with the explicit default catalog get separate id entries. ```` CacheKey tableIdKey = new CacheKey(KeyType.TABLE_ID, catName, req.getDbName(), req.getTblName()); ```` -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
