-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
connection.cpp
375 lines (279 loc) · 10.6 KB
/
connection.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cstring>
#include <sstream>
#include <ignite/common/fixed_size_array.h>
#include "ignite/odbc/utility.h"
#include "ignite/odbc/statement.h"
#include "ignite/odbc/connection.h"
#include "ignite/odbc/message.h"
#include "ignite/odbc/config/configuration.h"
namespace
{
#pragma pack(push, 1)
struct OdbcProtocolHeader
{
int32_t len;
};
#pragma pack(pop)
}
namespace ignite
{
namespace odbc
{
Connection::Connection() :
socket(),
connected(false),
parser(),
config()
{
// No-op.
}
Connection::~Connection()
{
// No-op.
}
const config::ConnectionInfo& Connection::GetInfo() const
{
// Connection info is constant and the same for all connections now.
const static config::ConnectionInfo info;
return info;
}
void Connection::GetInfo(config::ConnectionInfo::InfoType type, void* buf, short buflen, short* reslen)
{
IGNITE_ODBC_API_CALL(InternalGetInfo(type, buf, buflen, reslen));
}
SqlResult Connection::InternalGetInfo(config::ConnectionInfo::InfoType type, void* buf, short buflen, short* reslen)
{
const config::ConnectionInfo& info = GetInfo();
SqlResult res = info.GetInfo(type, buf, buflen, reslen);
if (res != SQL_RESULT_SUCCESS)
AddStatusRecord(SQL_STATE_HYC00_OPTIONAL_FEATURE_NOT_IMPLEMENTED, "Not implemented.");
return res;
}
void Connection::Establish(const std::string& connectStr)
{
IGNITE_ODBC_API_CALL(InternalEstablish(connectStr));
}
SqlResult Connection::InternalEstablish(const std::string& connectStr)
{
config::Configuration config;
try
{
config.FillFromConnectString(connectStr);
}
catch (IgniteError& e)
{
AddStatusRecord(SQL_STATE_HY000_GENERAL_ERROR, e.GetText());
return SQL_RESULT_ERROR;
}
return InternalEstablish(config);
}
void Connection::Establish(const config::Configuration cfg)
{
IGNITE_ODBC_API_CALL(InternalEstablish(cfg));
}
SqlResult Connection::InternalEstablish(const config::Configuration cfg)
{
config = cfg;
if (connected)
{
AddStatusRecord(SQL_STATE_08002_ALREADY_CONNECTED, "Already connected.");
return SQL_RESULT_ERROR;
}
connected = socket.Connect(cfg.GetHost().c_str(), cfg.GetTcpPort());
if (!connected)
{
AddStatusRecord(SQL_STATE_08001_CANNOT_CONNECT, "Failed to establish connection with the host.");
return SQL_RESULT_ERROR;
}
return MakeRequestHandshake();
}
void Connection::Release()
{
IGNITE_ODBC_API_CALL(InternalRelease());
}
SqlResult Connection::InternalRelease()
{
if (!connected)
{
AddStatusRecord(SQL_STATE_08003_NOT_CONNECTED, "Connection is not open.");
return SQL_RESULT_ERROR;
}
socket.Close();
connected = false;
return SQL_RESULT_SUCCESS;
}
Statement* Connection::CreateStatement()
{
Statement* statement;
IGNITE_ODBC_API_CALL(InternalCreateStatement(statement));
return statement;
}
SqlResult Connection::InternalCreateStatement(Statement*& statement)
{
statement = new Statement(*this);
if (!statement)
{
AddStatusRecord(SQL_STATE_HY001_MEMORY_ALLOCATION, "Not enough memory.");
return SQL_RESULT_ERROR;
}
return SQL_RESULT_SUCCESS;
}
void Connection::Send(const int8_t* data, size_t len)
{
if (!connected)
IGNITE_ERROR_1(IgniteError::IGNITE_ERR_ILLEGAL_STATE, "Connection is not established");
common::FixedSizeArray<int8_t> msg(len + sizeof(OdbcProtocolHeader));
OdbcProtocolHeader *hdr = reinterpret_cast<OdbcProtocolHeader*>(msg.GetData());
hdr->len = static_cast<int32_t>(len);
memcpy(msg.GetData() + sizeof(OdbcProtocolHeader), data, len);
size_t sent = SendAll(msg.GetData(), msg.GetSize());
if (sent != len + sizeof(OdbcProtocolHeader))
IGNITE_ERROR_1(IgniteError::IGNITE_ERR_GENERIC, "Can not send message");
}
size_t Connection::SendAll(const int8_t* data, size_t len)
{
int sent = 0;
while (sent != static_cast<int64_t>(len))
{
int res = socket.Send(data + sent, len - sent);
LOG_MSG("Sent: %d\n", res);
if (res <= 0)
return sent;
sent += res;
}
return sent;
}
void Connection::Receive(std::vector<int8_t>& msg)
{
if (!connected)
IGNITE_ERROR_1(IgniteError::IGNITE_ERR_ILLEGAL_STATE, "Connection is not established");
msg.clear();
OdbcProtocolHeader hdr;
int64_t received = ReceiveAll(reinterpret_cast<int8_t*>(&hdr), sizeof(hdr));
if (received != sizeof(hdr))
IGNITE_ERROR_1(IgniteError::IGNITE_ERR_GENERIC, "Can not receive message header");
if (hdr.len < 0)
IGNITE_ERROR_1(IgniteError::IGNITE_ERR_GENERIC, "Message lenght is negative");
if (hdr.len == 0)
return;
msg.resize(hdr.len);
received = ReceiveAll(&msg[0], hdr.len);
if (received != hdr.len)
{
msg.resize(received);
IGNITE_ERROR_1(IgniteError::IGNITE_ERR_GENERIC, "Can not receive message body");
}
}
size_t Connection::ReceiveAll(void* dst, size_t len)
{
size_t remain = len;
int8_t* buffer = reinterpret_cast<int8_t*>(dst);
while (remain)
{
size_t received = len - remain;
int res = socket.Receive(buffer + received, remain);
LOG_MSG("Receive res: %d\n", res);
LOG_MSG("remain: %d\n", remain);
if (res <= 0)
return received;
remain -= static_cast<size_t>(res);
}
return len;
}
const std::string& Connection::GetCache() const
{
return config.GetCache();
}
const config::Configuration& Connection::GetConfiguration() const
{
return config;
}
diagnostic::DiagnosticRecord Connection::CreateStatusRecord(SqlState sqlState,
const std::string& message, int32_t rowNum, int32_t columnNum)
{
return diagnostic::DiagnosticRecord(sqlState, message, "", "", rowNum, columnNum);
}
void Connection::TransactionCommit()
{
IGNITE_ODBC_API_CALL(InternalTransactionCommit());
}
SqlResult Connection::InternalTransactionCommit()
{
return SQL_RESULT_SUCCESS;
}
void Connection::TransactionRollback()
{
IGNITE_ODBC_API_CALL(InternalTransactionRollback());
}
SqlResult Connection::InternalTransactionRollback()
{
AddStatusRecord(SQL_STATE_HYC00_OPTIONAL_FEATURE_NOT_IMPLEMENTED,
"Rollback operation is not supported.");
return SQL_RESULT_ERROR;
}
SqlResult Connection::MakeRequestHandshake()
{
bool distributedJoins = false;
bool enforceJoinOrder = false;
int64_t protocolVersion = 0;
try
{
distributedJoins = config.IsDistributedJoins();
enforceJoinOrder = config.IsEnforceJoinOrder();
protocolVersion = config.GetProtocolVersion().GetIntValue();
}
catch (const IgniteError& err)
{
AddStatusRecord(SQL_STATE_01S00_INVALID_CONNECTION_STRING_ATTRIBUTE, err.GetText());
return SQL_RESULT_ERROR;
}
HandshakeRequest req(protocolVersion, distributedJoins, enforceJoinOrder);
HandshakeResponse rsp;
try
{
SyncMessage(req, rsp);
}
catch (const IgniteError& err)
{
AddStatusRecord(SQL_STATE_HYT01_CONNECTIOIN_TIMEOUT, err.GetText());
return SQL_RESULT_ERROR;
}
if (rsp.GetStatus() != RESPONSE_STATUS_SUCCESS)
{
LOG_MSG("Error: %s\n", rsp.GetError().c_str());
AddStatusRecord(SQL_STATE_08001_CANNOT_CONNECT, rsp.GetError());
InternalRelease();
return SQL_RESULT_ERROR;
}
if (!rsp.IsAccepted())
{
LOG_MSG("Hanshake message has been rejected.\n");
std::stringstream constructor;
constructor << "Node rejected handshake message. "
<< "Current node Apache Ignite version: " << rsp.CurrentVer() << ", "
<< "node protocol version introduced in version: " << rsp.ProtoVerSince() << ", "
<< "driver protocol version introduced in version: " << config.GetProtocolVersion().ToString() << ".";
AddStatusRecord(SQL_STATE_08001_CANNOT_CONNECT, constructor.str());
InternalRelease();
return SQL_RESULT_ERROR;
}
return SQL_RESULT_SUCCESS;
}
}
}