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
DistributionManagerServer.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 #include <cstddef>
19 #include <iostream> // std::cout
20 //#include <iterator> // std::iterator, std::input_iterator_tag
21 #include <fstream>
22 #include <vector>
23 #include <cstring>
24 #include <unistd.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <string>
29 
31 #include "SimpleRequestHandler.h"
32 #include "BuiltInObjectTypeIDs.h"
33 
34 #include "SimpleRequestResult.h"
36 #include "NodeInfo.h"
37 #include "GetListOfNodes.h"
38 #include "ListOfNodes.h"
39 #include "Ack.h"
40 #include "InterfaceFunctions.h"
42 
43 namespace pdb {
44 
46  PDBDistributionManagerPtr distributionManagerIn) {
47  distributionManager = distributionManagerIn;
48 }
49 
51  this->logToMe = getWorker()->getLogger();
52 }
53 
55 
56  // A handler for Heart Beat Operation - Nodes send NodeInfo
57  forMe.registerHandler(
58  NodeInfo_TYPEID,
59  make_shared<SimpleRequestHandler<NodeInfo>>(
60  [&](Handle<NodeInfo> request, PDBCommunicatorPtr sendUsingMe) {
61 
62  // TODO: There is no errMsg that I can forward. Check if we need to forward some
63  // errMsg.
64  std::string errMsg;
65 
66  // get the pointer to the distribution manager instance
68  getFunctionality<DistributionManagerServer>().getDistributionManager();
69  std::string hostname = request->getHostName();
70 
71  // update
72  // TODO: result is unused so far.
73  bool resulstFromUpdate = myDM->addOrUpdateNodes(forMe.getLogger(), hostname);
74 
75  bool wasError;
76 
77  // TODO
78  if (resulstFromUpdate)
79  wasError = true;
80  else
81  wasError = true;
82 
83  // TODO : I have no fail case, so I send always true with no errMsg.
84  // return the result
85  return make_pair(wasError, errMsg);
86  }));
87 
88  forMe.registerHandler(
89  GetListOfNodes_TYPEID,
91  PDBCommunicatorPtr sendUsingMe) {
92  std::string errMsg;
93 
94  // get the pointer to the distribution manager instance
96  getFunctionality<DistributionManagerServer>().getDistributionManager();
97  unordered_map<string, long> myList = myDM->getUpNodesOfCluster();
98  bool res;
99 
100  try {
101  // make the vector
102  makeObjectAllocatorBlock(1024 * 24, true);
103 
104  Handle<Vector<String>> hostNames = makeObject<Vector<String>>();
105  if (hostNames == nullptr) {
106  getLogger()->error(
107  "DistributionManagerServer::registerHandlers - "
108  "CouldNotMakeObjectHostNames");
109  errMsg += "CouldNotMakeObjectHostNames";
110  return make_pair(false, errMsg);
111  }
112 
113  for (auto& ent1 : myList) {
114  // ent1.first is the first key
115  string tmpHostName = ent1.first;
116  hostNames->push_back(tmpHostName);
117  }
118 
119  // Make the list of nodes
120  makeObjectAllocatorBlock(1024 * 48, true);
121  Handle<ListOfNodes> response = makeObject<ListOfNodes>();
122 
123  if (response == nullptr) {
124 
125  getLogger()->error(
126  "DistributionManagerServer::registerHandlers - "
127  "CouldNotMakeObjectListOfNodes");
128  errMsg += "CouldNotMakeObjectListOfNodes";
129  return make_pair(false, errMsg);
130  }
131 
132  response->setHostNames(hostNames);
133  // return the result
134  res = sendUsingMe->sendObject(response, errMsg);
135 
136  } catch (NotEnoughSpace& e) {
137 
138  res = false;
139  errMsg += "NotEnoughSpace";
140  getLogger()->error(
141  "DistributionManagerServer::registerHandlers - NotEnoughSpace to make objects");
142  return make_pair(false, errMsg);
143  }
144 
145  return make_pair(res, errMsg);
146  }));
147 }
148 
150  PDBDistributionManagerPtr distributionManagerIn) {
151  distributionManager = distributionManagerIn;
152 }
153 
155  return this->distributionManager;
156 }
157 
159  return this->logToMe;
160 }
161 }
PDBDistributionManagerPtr getDistributionManager()
PDBDistributionManagerPtr distributionManager
DistributionManagerServer(PDBDistributionManagerPtr distributionManagerIn)
PDBLoggerPtr getLogger()
Definition: PDBServer.cc:233
void registerHandlers(PDBServer &forMe) override
std::shared_ptr< PDBCommunicator > PDBCommunicatorPtr
void registerHandler(int16_t typeID, PDBCommWorkPtr handledBy)
Definition: PDBServer.cc:81
std::shared_ptr< PDBLogger > PDBLoggerPtr
Definition: PDBLogger.h:40
void setDistributionManager(PDBDistributionManagerPtr distributionManagerIn)
void makeObjectAllocatorBlock(size_t numBytesIn, bool throwExceptionOnFail)
shared_ptr< PDBDistributionManager > PDBDistributionManagerPtr