-
Notifications
You must be signed in to change notification settings - Fork 57
/
Environment.cpp
executable file
·239 lines (213 loc) · 6.96 KB
/
Environment.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
* Environment.cpp
*
* Created on: Aug 10, 2013
* Author: wangli
*/
#include "Environment.h"
#include "caf/all.hpp"
#include <map>
#include <utility>
#include "common/Message.h"
#include "exec_tracker/stmt_exec_tracker.h"
#include "exec_tracker/segment_exec_tracker.h"
#include "node_manager/base_node.h"
#define GLOG_NO_ABBREVIATED_SEVERITIES
#include <glog/logging.h>
#include <libconfig.h++>
#include <iostream>
#include <sstream>
#include <string>
#include "./Debug.h"
#include "./Config.h"
#include "common/Logging.h"
#include "common/TypePromotionMap.h"
#include "common/TypeCast.h"
#include "common/error_define.h"
#include "codegen/CodeGenerator.h"
#include "common/expression/data_type_oper.h"
#include "common/expression/expr_type_cast.h"
#include "common/expression/type_conversion_matrix.h"
using caf::announce;
using claims::BaseNode;
using claims::catalog::Catalog;
using claims::common::InitAggAvgDivide;
using claims::common::InitOperatorFunc;
using claims::common::InitpartitionValue;
using claims::common::InitTypeCastFunc;
using claims::common::InitTypeConversionMatrix;
//#define DEBUG_MODE
#include "catalog/catalog.h"
using claims::common::rSuccess;
using claims::NodeAddr;
using claims::NodeSegmentID;
using claims::StmtExecTracker;
Environment* Environment::_instance = 0;
Environment::Environment(bool ismaster) : ismaster_(ismaster) {
_instance = this;
Config::getInstance();
CodeGenerator::getInstance();
logging_ = new EnvironmentLogging();
readConfigFile();
initializeExpressionSystem();
portManager = PortManager::getInstance();
if (ismaster) {
catalog_ = claims::catalog::Catalog::getInstance();
logging_->log("restore the catalog ...");
if (rSuccess != catalog_->restoreCatalog()) {
LOG(ERROR) << "failed to restore catalog" << std::endl;
cerr << "ERROR: restore catalog failed" << endl;
}
}
stmt_exec_tracker_ = new StmtExecTracker();
seg_exec_tracker_ = new SegmentExecTracker();
if (true == g_thread_pool_used) {
logging_->log("Initializing the ThreadPool...");
if (false == initializeThreadPool()) {
logging_->elog("initialize ThreadPool failed");
}
}
/**
* TODO:
* DO something in AdaptiveEndPoint such that the construction function does
not return until the connection is completed. If so, the following
sleep()
dose not needed.
This is done in Aug.18 by Li :)
*/
/*Before initializing Resource Manager, the instance ip and port should be
* decided.*/
AnnounceCafMessage();
initializeResourceManager();
// should after above
InitMembership();
initializeStorage();
initializeBufferManager();
logging_->log("Initializing the ExecutorMaster...");
iteratorExecutorMaster = new IteratorExecutorMaster();
logging_->log("Initializing the ExecutorSlave...");
iteratorExecutorSlave = new IteratorExecutorSlave();
exchangeTracker = new ExchangeTracker();
expander_tracker_ = ExpanderTracker::getInstance();
#ifndef DEBUG_MODE
if (ismaster) {
initializeClientListener();
}
#endif
}
Environment::~Environment() {
_instance = 0;
delete expander_tracker_;
delete logging_;
delete portManager;
delete catalog_;
if (ismaster_) {
delete iteratorExecutorMaster;
delete resourceManagerMaster_;
delete blockManagerMaster_;
#ifndef DEBUG_MODE
destoryClientListener();
#endif
}
delete iteratorExecutorSlave;
delete exchangeTracker;
delete resourceManagerSlave_;
delete blockManager_;
delete bufferManager_;
}
Environment* Environment::getInstance(bool ismaster) {
if (_instance == 0) {
new Environment(ismaster);
}
return _instance;
}
std::string Environment::getIp() { return ip; }
unsigned Environment::getPort() { return port; }
ThreadPool* Environment::getThreadPool() const { return thread_pool_; }
void Environment::readConfigFile() {
libconfig::Config cfg;
cfg.readFile(Config::config_file.c_str());
ip = (const char*)cfg.lookup("ip");
}
void Environment::AnnounceCafMessage() {
announce<StorageBudgetMessage>(
"StorageBudgetMessage", &StorageBudgetMessage::nodeid,
&StorageBudgetMessage::memory_budget, &StorageBudgetMessage::disk_budget);
announce<ProjectionID>("ProjectionID", &ProjectionID::table_id,
&ProjectionID::projection_off);
announce<PartitionID>("PartitionID", &PartitionID::projection_id,
&PartitionID::partition_off);
announce<ExchangeID>("ExchangeID", &ExchangeID::exchange_id,
&ExchangeID::partition_offset);
announce<BaseNode>("BaseNode", &BaseNode::node_id_to_addr_);
announce<NodeSegmentID>("NodeSegmentID", &NodeSegmentID::first,
&NodeSegmentID::second);
}
void Environment::initializeStorage() {
if (ismaster_) {
blockManagerMaster_ = BlockManagerMaster::getInstance();
blockManagerMaster_->initialize();
}
/*both master and slave node run the BlockManager.*/
// BlockManagerId *bmid=new BlockManagerId();
// string
// actorname="blockManagerWorkerActor_"+bmid->blockManagerId;
// cout<<actorname.c_str()<<endl;
// BlockManager::BlockManagerWorkerActor
//*blockManagerWorkerActor=new
// BlockManager::BlockManagerWorkerActor(endpoint,framework_storage,actorname.c_str());
blockManager_ = BlockManager::getInstance();
blockManager_->initialize();
}
void Environment::initializeResourceManager() {
if (ismaster_) {
resourceManagerMaster_ = new ResourceManagerMaster();
}
resourceManagerSlave_ = new InstanceResourceManager();
// nodeid = resourceManagerSlave_->Register();
}
void Environment::InitMembership() {
if (ismaster_) {
master_node_ = MasterNode::GetInstance();
}
slave_node_ = SlaveNode::GetInstance();
slave_node_->RegisterToMaster(true);
node_id_ = slave_node_->get_node_id();
}
void Environment::initializeBufferManager() {
bufferManager_ = BufferManager::getInstance();
}
void Environment::initializeIndexManager() {
indexManager_ = IndexManager::getInstance();
}
ExchangeTracker* Environment::getExchangeTracker() { return exchangeTracker; }
ResourceManagerMaster* Environment::getResourceManagerMaster() {
return resourceManagerMaster_;
}
InstanceResourceManager* Environment::getResourceManagerSlave() {
return resourceManagerSlave_;
}
NodeID Environment::getNodeID() const { return node_id_; }
claims::catalog::Catalog* Environment::getCatalog() const { return catalog_; }
void Environment::initializeClientListener() {
listener_ = new ClientListener(Config::client_listener_port);
listener_->configure();
}
void Environment::initializeExpressionSystem() {
InitTypeConversionMatrix();
InitOperatorFunc();
InitAggAvgDivide();
InitTypeCastFunc();
InitpartitionValue();
}
void Environment::destoryClientListener() {
listener_->shutdown();
delete listener_;
}
bool Environment::initializeThreadPool() {
thread_pool_ = new ThreadPool();
return thread_pool_->Init(Config::thread_pool_init_thread_num);
}
IteratorExecutorSlave* Environment::getIteratorExecutorSlave() const {
return iteratorExecutorSlave;
}