Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
lib-cache.h
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 
16 #ifndef IMPALA_RUNTIME_LIB_CACHE_H
17 #define IMPALA_RUNTIME_LIB_CACHE_H
18 
19 #include <string>
20 #include <boost/scoped_ptr.hpp>
21 #include <boost/unordered_map.hpp>
22 #include <boost/unordered_set.hpp>
23 #include <boost/thread/mutex.hpp>
24 #include "common/atomic.h"
25 #include "common/object-pool.h"
26 #include "common/status.h"
27 
28 namespace impala {
29 
30 class RuntimeState;
31 
38 //
44 //
49 //
53 class LibCache {
54  public:
55  struct LibCacheEntry;
56 
57  enum LibType {
58  TYPE_SO, // Shared object
59  TYPE_IR, // IR intermediate
60  TYPE_JAR, // Java jar file. We don't care about the contents in the BE.
61  };
62 
63  static LibCache* instance() { return LibCache::instance_.get(); }
64 
66  ~LibCache();
67 
69  static Status Init();
70 
74  Status GetLocalLibPath(const std::string& hdfs_lib_file, LibType type,
75  std::string* local_path);
76 
79  Status CheckSymbolExists(const std::string& hdfs_lib_file, LibType type,
80  const std::string& symbol, bool quiet = false);
81 
87  //
92  //
94  Status GetSoFunctionPtr(const std::string& hdfs_lib_file, const std::string& symbol,
95  void** fn_ptr, LibCacheEntry** entry, bool quiet = false);
96 
100  void SetNeedsRefresh(const std::string& hdfs_lib_file);
101 
103  void DecrementUseCount(LibCacheEntry* entry);
104 
106  void RemoveEntry(const std::string& hdfs_lib_file);
107 
109  void DropCache();
110 
111  private:
113  static boost::scoped_ptr<LibCache> instance_;
114 
117 
121 
124  boost::mutex lock_;
125 
128  typedef boost::unordered_map<std::string, LibCacheEntry*> LibMap;
130 
131  LibCache();
132  LibCache(LibCache const& l); // disable copy ctor
133  LibCache& operator=(LibCache const& l); // disable assignment
134 
136 
143  Status GetCacheEntry(const std::string& hdfs_lib_file, LibType type,
144  boost::unique_lock<boost::mutex>* entry_lock, LibCacheEntry** entry);
145 
148  Status GetCacheEntryInternal(const std::string& hdfs_lib_file, LibType type,
149  boost::unique_lock<boost::mutex>* entry_lock, LibCacheEntry** entry);
150 
156  std::string MakeLocalPath(const std::string& hdfs_path, const std::string& local_dir);
157 
160  void RemoveEntryInternal(const std::string& hdfs_lib_file,
161  const LibMap::iterator& entry_iterator);
162 };
163 
164 }
165 
166 #endif
Status CheckSymbolExists(const std::string &hdfs_lib_file, LibType type, const std::string &symbol, bool quiet=false)
Definition: lib-cache.cc:192
boost::mutex lock_
Definition: lib-cache.h:124
void RemoveEntryInternal(const std::string &hdfs_lib_file, const LibMap::iterator &entry_iterator)
Definition: lib-cache.cc:239
AtomicInt< int64_t > num_libs_copied_
Definition: lib-cache.h:120
void * current_process_handle_
dlopen() handle for the current process (i.e. impalad).
Definition: lib-cache.h:116
static boost::scoped_ptr< LibCache > instance_
Singleton instance. Instantiated in Init().
Definition: lib-cache.h:113
~LibCache()
Calls dlclose on all cached handles.
Definition: lib-cache.cc:95
LibCache & operator=(LibCache const &l)
Status GetCacheEntry(const std::string &hdfs_lib_file, LibType type, boost::unique_lock< boost::mutex > *entry_lock, LibCacheEntry **entry)
Definition: lib-cache.cc:279
void RemoveEntry(const std::string &hdfs_lib_file)
Removes the cache entry for 'hdfs_lib_file'.
Definition: lib-cache.cc:232
Status GetSoFunctionPtr(const std::string &hdfs_lib_file, const std::string &symbol, void **fn_ptr, LibCacheEntry **entry, bool quiet=false)
If 'quiet' is true, returned error statuses will not be logged.
Definition: lib-cache.cc:130
Status GetCacheEntryInternal(const std::string &hdfs_lib_file, LibType type, boost::unique_lock< boost::mutex > *entry_lock, LibCacheEntry **entry)
Definition: lib-cache.cc:304
Status GetLocalLibPath(const std::string &hdfs_lib_file, LibType type, std::string *local_path)
Definition: lib-cache.cc:181
static LibCache * instance()
Definition: lib-cache.h:63
Status InitInternal()
Definition: lib-cache.cc:106
LibMap lib_cache_
Definition: lib-cache.h:129
void DropCache()
Removes all cached entries.
Definition: lib-cache.cc:262
void SetNeedsRefresh(const std::string &hdfs_lib_file)
Definition: lib-cache.cc:221
std::string MakeLocalPath(const std::string &hdfs_path, const std::string &local_dir)
Definition: lib-cache.cc:414
static Status Init()
Initializes the libcache. Must be called before any other APIs.
Definition: lib-cache.cc:100
void DecrementUseCount(LibCacheEntry *entry)
See comment in GetSoFunctionPtr().
Definition: lib-cache.cc:170
boost::unordered_map< std::string, LibCacheEntry * > LibMap
Definition: lib-cache.h:128