-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmycat-server.js
190 lines (157 loc) · 5.17 KB
/
mycat-server.js
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
const SystemConfig = require('./config/model/system-config');
const MycatConfig = require('./config/mycat-config');
const Scheduler = require('./util/scheduler');
const BufferPool = require('./buffer/buffer-pool');
const Properties = require('./util/properties');
const Logger = require('./util/logger');
const RouteService = require('./route/route-service');
class MycatServer {
static #INSTANCE = null;
#startupTime = 0;
#config = new MycatConfig();
#online = true;
#scheduler = new Scheduler();
#connManager = null;
#bufferPool = null;
// Init route service
#routeService = new RouteService();
#dnIndexProperties = new Properties();
constructor() {
if (MycatServer.#INSTANCE) {
throw new Error(`MycatServer instance existing!`);
}
// TODO
// SQL recorder
// Init cache service
// Load dataNode active index from properties
this.#dnIndexProperties = ServerHelper.loadDnIndexProps();
// TODO
// Init SQL interceptor
// Init catlet loader
// Init zk switch
this.#startupTime = new Date().getTime();
}
startup() {
SystemConfig.resetLogger();
const config = this.config;
const system = config.system;
const dataHosts = config.dataHosts;
// TODO route strategy
Logger.info('%s is ready to startup ...', MycatServer.NAME);
Logger.info('System config: %s', system);
this.#bufferPool = new BufferPool(system.bufferPoolChunkSize,
system.bufferPoolPageSize, system.bufferPoolPageNumber);
const ConnManager = require('./net/conn-manager');
this.#connManager = new ConnManager(this.#bufferPool);
// start server and manager
let prefix = MycatServer.NAME;
let addr = system.bindIp;
let backlog = system.serverBacklog;
// Note: here require solving the cycled reference issue.
const ServerConnFactory = require('./frontend/server/server-conn-factory');
const ManagerConnFactory = require('./frontend/manager/manager-conn-factory');
let mgrFactory = new ManagerConnFactory();
let svrFactory = new ServerConnFactory();
const NetServer = require('./net/net-server');
const manager = new NetServer(prefix+'Manager', addr, system.managerPort, backlog, mgrFactory);
const server = new NetServer(prefix+'Server', addr, system.serverPort, backlog, svrFactory);
let failed = true;
try {
manager.start();
server.start();
// init data hosts
for (let dbPool of dataHosts.values()) {
let hostName = dbPool.hostName;
let index = this.#dnIndexProperties.getIntProperty(hostName, 0);
if (index !== 0) {
Logger.info(`Init dataHost '${hostName}' by using source#${index}.`);
}
dbPool.init(index);
dbPool.startHeartbeat();
}
failed = false;
} finally {
if (failed) {
manager.stop();
server.stop();
}
}
// Schedule timers
let scheduler = this.#scheduler;
scheduler.schedule("dataNodeHeartbeat", () => {
Logger.debug("DataNode heartbeat start.");
for (let dbPool of dataHosts.values()) {
dbPool.doHeartbeat();
}
Logger.debug("DataNode heartbeat end.");
}, 0, system.dataNodeHeartbeatPeriod);
}
saveDataHostIndex(hostName, index) {
let props = this.#dnIndexProperties;
props.setProperty(hostName, index);
ServerHelper.saveDnIndexProps(props);
}
get routeService() {
return this.#routeService;
}
get connManager() {
return this.#connManager;
}
get online() {
return this.#online;
}
get config() {
return this.#config;
}
get system() {
return this.config.system;
}
online() {
this.#online = true;
}
offline() {
this.#online = false;
}
toString() {
return MycatServer.NAME;
}
// Class level
static get NAME() {
return 'MyCat';
}
static getInstance() {
return this.instance;
}
static get instance() {
let server = this.#INSTANCE;
if (server) return server;
else return this.#INSTANCE = new MycatServer();
}
static uncaught(p, e) {
if (p instanceof Error) {
e = p;
p = '';
}
if (e.stack) {
if (p) p += ': \r\n';
Logger.error(p + e.stack);
} else {
if (p) p += ': ';
Logger.error(p + e);
}
}
}
class ServerHelper {
static #dnindexFile = "dnindex.properties";
static loadDnIndexProps() {
const props = new Properties();
let baseDir = SystemConfig.confPath;
props.load(baseDir, this.#dnindexFile);
return props;
}
static saveDnIndexProps(props) {
let baseDir = SystemConfig.confPath;
props.save(baseDir, this.#dnindexFile);
}
}
module.exports = MycatServer;