A platform for high-performance distributed tool and library development written in C++. It can be deployed in two different cluster modes: standalone or distributed. API for v0.5.0, released on June 13, 2018.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
VTableMapCatalogLookup.cc
Go to the documentation of this file.
1 /*****************************************************************************
2  * *
3  * Copyright 2018 Rice University *
4  * *
5  * Licensed under the Apache License, Version 2.0 (the "License"); *
6  * you may not use this file except in compliance with the License. *
7  * You may obtain a copy of the License at *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14  * See the License for the specific language governing permissions and *
15  * limitations under the License. *
16  * *
17  *****************************************************************************/
18 
19 #ifndef VTABLEMAP_CAT_LOOKUP_CC
20 #define VTABLEMAP_CAT_LOOKUP_CC
21 
22 #include <dlfcn.h>
23 #include <unistd.h>
24 #include <unistd.h>
25 #include "PDBDebug.h"
26 #include "PDBLogger.h"
27 #include <cctype>
28 #include "CatalogClient.h"
29 
30 namespace pdb {
31 
32 // note: this should only be called while protected by a lock on the vTableMap
33 void* VTableMap::getVTablePtrUsingCatalog(int16_t objectTypeID) {
34 
35  // in this case, we do not have the vTable pointer for this type, so we will try to load it
36  if (theVTable->catalog == nullptr) {
37  if (theVTable->logger != nullptr) {
38  if (objectTypeID >= 8191) {
39  theVTable->logger->error(
40  std::string("unable to obtain shared library file for typeId=") +
41  std::to_string(objectTypeID));
42  }
43  return nullptr;
44  }
45  }
46 
47  // return if the type is unknown b/c it won't be found neither in
48  // an .so library nor in the Catalog
49  if (objectTypeID == 0) {
50  PDB_COUT << "This is typeId=0, just return!!!!! " << endl;
51  return nullptr;
52  }
53 
54  std::string sharedLibraryFile = "/var/tmp/objectFile.";
55  sharedLibraryFile += to_string(getpid()) + "." + to_string(objectTypeID) + ".so";
56  PDB_COUT << "VTableMap:: to get sharedLibraryFile =" << sharedLibraryFile << std::endl;
57  if (theVTable->logger != nullptr) {
58  theVTable->logger->debug(std::string("VTableMap:: to get sharedLibraryFile =") +
59  sharedLibraryFile);
60  }
61  unlink(sharedLibraryFile.c_str());
62  PDB_COUT << "VTableMap:: to get shared for objectTypeID=" << objectTypeID << std::endl;
63  bool ret = theVTable->catalog->getSharedLibrary(objectTypeID, sharedLibraryFile);
64 
65  // we should stop here if someone else updated the VTable
66  void* returnVal = theVTable->allVTables[objectTypeID];
67  if (returnVal != nullptr) {
68  return returnVal;
69  }
70 
71  // we need check return value
72  if (ret == false) {
73  std::cout << "Error fixing VTableMap for objectTypeID=" << objectTypeID << std::endl;
74  std::cout << " This could be because: 1) the name used to retrieve the shared library "
75  "doesn't match the types in the catalog, or"
76  << std::endl;
77  std::cout << " 2) a shared library for that type has not been registered in the catalog"
78  << std::endl;
79  return nullptr;
80  }
81 
82  // open up the shared library
83 
84  void* so_handle = dlopen(sharedLibraryFile.c_str(), RTLD_LOCAL | RTLD_LAZY);
85  theVTable->so_handles.push_back(so_handle);
86 
87  if (!so_handle) {
88  const char* dlsym_error = dlerror();
89  if (theVTable->logger != nullptr)
90  theVTable->logger->error("Cannot load Stored Data Type library: " + sharedLibraryFile +
91  " error " + (std::string)dlsym_error + '\n');
92  std::cout << "Error == " <<(std::string)dlsym_error << std::endl;
93  // if we were able to open it
94  } else {
95  const char* dlsym_error = dlerror();
96 
97  // first we need to correctly set all of the global variables in the shared library
98  typedef void setGlobalVars(Allocator*, VTableMap*, void*, void*);
99  std::string getInstance = "setAllGlobalVariables";
100  PDB_COUT << "to set global variables" << std::endl;
101  setGlobalVars* setGlobalVarsFunc = (setGlobalVars*)dlsym(so_handle, getInstance.c_str());
102  // see if we were able to get the function
103  if ((dlsym_error = dlerror())) {
104  if (theVTable->logger != nullptr)
105  theVTable->logger->error(
106  "Error, can't set global variables in .so file; error is " +
107  (std::string)dlsym_error + "\n");
108  std::cout << "ERROR: we were not able to get the function" << std::endl;
109  return nullptr;
110  // if we were able to, then run it
111  } else {
112  setGlobalVarsFunc(mainAllocatorPtr, theVTable, stackBase, stackEnd);
113  PDB_COUT << "Successfully set global variables" << std::endl;
114  }
115 
116  // get the function that will give us access to the vTable
117  typedef void* getObjectVTable();
118  getInstance = "getObjectVTable";
119  getObjectVTable* getObjectFunc = (getObjectVTable*)dlsym(so_handle, getInstance.c_str());
120 
121  // see if we were able to get the function
122  if ((dlsym_error = dlerror())) {
123  if (theVTable->logger != nullptr)
124  theVTable->logger->error("Error, can't load function getInstance (); error is " +
125  (std::string)dlsym_error + "\n");
126  std::cout << "ERROR: we were not able to load function getObjectVTable" << std::endl;
127  return nullptr;
128  // if we were able to, then run it
129  } else {
130  theVTable->allVTables[objectTypeID] = getObjectFunc();
131  PDB_COUT << "VTablePtr for objectTypeID=" << objectTypeID
132  << " is set in allVTables to be " << theVTable->allVTables[objectTypeID]
133  << std::endl;
134  }
135  }
136  return theVTable->allVTables[objectTypeID];
137 }
138 
139 int16_t VTableMap::lookupTypeNameInCatalog(std::string objectTypeName) {
140  PDB_COUT << "invoke lookupTypeNameInCatalog for objectTypeName=" << objectTypeName << std::endl;
141  return theVTable->catalog->searchForObjectTypeName(objectTypeName);
142 }
143 
144 
145 } /* namespace pdb */
146 
147 #endif
std::vector< void * > so_handles
Definition: VTableMap.h:111
std::vector< void * > allVTables
Definition: VTableMap.h:99
void * stackBase
PDBLoggerPtr logger
Definition: VTableMap.h:105
void * stackEnd
static void * getVTablePtrUsingCatalog(int16_t objectTypeID)
int16_t searchForObjectTypeName(std::string objectTypeName)
static int16_t lookupTypeNameInCatalog(std::string objectTypeName)
CatalogClient * catalog
Definition: VTableMap.h:102
#define PDB_COUT
Definition: PDBDebug.h:31
VTableMap * theVTable
Allocator * mainAllocatorPtr
bool getSharedLibrary(int16_t identifier, std::string sharedLibraryFileName)