forked from MyCATApache/Mycat-Server
-
Notifications
You must be signed in to change notification settings - Fork 89
/
MultiNodeQueryHandler.java
888 lines (790 loc) · 27.1 KB
/
MultiNodeQueryHandler.java
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
/*
* Copyright (c) 2013, OpenCloudDB/MyCAT and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software;Designed and Developed mainly by many Chinese
* opensource volunteers. you can redistribute it and/or modify it under the
* terms of the GNU General Public License version 2 only, as published by the
* Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Any questions about this component can be directed to it's project Web address
* https://code.google.com/p/opencloudb/.
*
*/
package io.mycat.backend.mysql.nio.handler;
import io.mycat.MycatServer;
import io.mycat.backend.BackendConnection;
import io.mycat.backend.datasource.PhysicalDBNode;
import io.mycat.backend.mysql.LoadDataUtil;
import io.mycat.cache.LayerCachePool;
import io.mycat.config.MycatConfig;
import io.mycat.memory.unsafe.row.UnsafeRow;
import io.mycat.net.mysql.*;
import io.mycat.route.RouteResultset;
import io.mycat.route.RouteResultsetNode;
import io.mycat.server.NonBlockingSession;
import io.mycat.server.ServerConnection;
import io.mycat.server.parser.ServerParse;
import io.mycat.sqlengine.mpp.*;
import io.mycat.statistic.stat.QueryResult;
import io.mycat.statistic.stat.QueryResultDispatcher;
import io.mycat.util.ResultSetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantLock;
/**
* 堆外
*
* @author mycat
*/
public class MultiNodeQueryHandler extends MultiNodeHandler implements LoadDataResponseHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(MultiNodeQueryHandler.class);
private final RouteResultset rrs;
private final NonBlockingSession session;
// private final CommitNodeHandler icHandler;
/**
* 数据合并服务
*/
private final AbstractDataNodeMerge dataMergeSvr;
private final boolean autocommit;
private String priamaryKeyTable = null;
private int primaryKeyIndex = -1;
private int fieldCount = 0;
private final ReentrantLock lock;
private long affectedRows;
private long selectRows;
private long insertId;
/**
* fields 是否返回
*/
private volatile boolean fieldsReturned;
private int okCount;
private final boolean isCallProcedure;
/**
* 开始执行 SQL 时间
*/
private long startTime;
private long netInBytes;
/**
* 网络流量字节数:后端数据库=》MyCAT
*/
private long netOutBytes;
private int execCount = 0;
private boolean prepared;
private List<FieldPacket> fieldPackets = new ArrayList<FieldPacket>();
/**
* 是否启用Off Heap for Merge
* 1-启用
* 0-不启用
*/
private int isOffHeapuseOffHeapForMerge = 1;
//huangyiming add 中间处理结果是否处理完毕
private final AtomicBoolean isMiddleResultDone;
/**
* 分页开始位置
*/
private int limitStart;
/**
* 分页大小
*/
private int limitSize;
private int index = 0;
/**
* 分页结束位置
*/
private int end = 0;
//huangyiming
/**
* header
*/
private byte[] header = null;
/**
* fields 字段
*/
private List<byte[]> fields = null;
public MultiNodeQueryHandler(int sqlType, RouteResultset rrs,
boolean autocommit, NonBlockingSession session) {
super(session);
this.isMiddleResultDone = new AtomicBoolean(false);
if (rrs.getNodes() == null) {
throw new IllegalArgumentException("routeNode is null!");
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("execute mutinode query " + rrs.getStatement());
}
this.rrs = rrs;
// 赋值 数据合并服务
isOffHeapuseOffHeapForMerge = MycatServer.getInstance().getConfig().getSystem().getUseOffHeapForMerge();
if (ServerParse.SELECT == sqlType && rrs.needMerge()) {
if (isOffHeapuseOffHeapForMerge == 1) { // 堆外内存(off-heap memory)
dataMergeSvr = new DataNodeMergeManager(this,rrs,isMiddleResultDone);
} else { // 堆内内存(on-heap memory)
dataMergeSvr = new DataMergeService(this,rrs);
}
} else {
dataMergeSvr = null;
}
isCallProcedure = rrs.isCallStatement();
this.autocommit = session.getSource().isAutocommit();
this.session = session;
this.lock = new ReentrantLock();
// this.icHandler = new CommitNodeHandler(session);
// 赋值 分页位置
this.limitStart = rrs.getLimitStart();
this.limitSize = rrs.getLimitSize();
this.end = limitStart + rrs.getLimitSize();
if (this.limitStart < 0) {
this.limitStart = 0;
}
if (rrs.getLimitSize() < 0) {
end = Integer.MAX_VALUE;
}
if ((dataMergeSvr != null) && LOGGER.isDebugEnabled()) {
LOGGER.debug("has data merge logic ");
}
// 赋值 网络流量统计
if ( rrs != null && rrs.getStatement() != null) {
netInBytes += rrs.getStatement().getBytes().length;
}
}
protected void reset(int initCount) {
super.reset(initCount);
this.okCount = initCount;
this.execCount = 0;
this.netInBytes = 0;
this.netOutBytes = 0;
}
public NonBlockingSession getSession() {
return session;
}
public void execute() throws Exception {
// 初始化 Handler
final ReentrantLock lock = this.lock;
lock.lock();
try {
this.reset(rrs.getNodes().length);
this.fieldsReturned = false;
this.affectedRows = 0L;
this.insertId = 0L;
} finally {
lock.unlock();
}
// 遍历路由node,发送 SQL 执行
MycatConfig conf = MycatServer.getInstance().getConfig();
startTime = System.currentTimeMillis();
LOGGER.debug("rrs.getRunOnSlave()-" + rrs.getRunOnSlave());
for (final RouteResultsetNode node : rrs.getNodes()) {
BackendConnection conn = session.getTarget(node);
if (session.tryExistsCon(conn, node)) {
LOGGER.debug("node.getRunOnSlave()-" + node.getRunOnSlave());
node.setRunOnSlave(rrs.getRunOnSlave()); // 实现 master/slave注解
LOGGER.debug("node.getRunOnSlave()-" + node.getRunOnSlave());
_execute(conn, node);
} else {
// create new connection
LOGGER.debug("node.getRunOnSlave()1-" + node.getRunOnSlave());
node.setRunOnSlave(rrs.getRunOnSlave()); // 实现 master/slave注解
LOGGER.debug("node.getRunOnSlave()2-" + node.getRunOnSlave());
PhysicalDBNode dn = conf.getDataNodes().get(node.getName());
dn.getConnection(dn.getDatabase(), autocommit, node, this, node);
// 注意该方法不仅仅是获取连接,获取新连接成功之后,会通过层层回调,最后回调到本类 的connectionAcquired
// 这是通过 上面方法的 this 参数的层层传递完成的。
// connectionAcquired 进行执行操作:
// session.bindConnection(node, conn);
// _execute(conn, node);
}
}
}
private void _execute(BackendConnection conn, RouteResultsetNode node) {
if (clearIfSessionClosed(session)) {
return;
}
conn.setResponseHandler(this);
try {
conn.execute(node, session.getSource(), autocommit);
} catch (IOException e) {
connectionError(e, conn);
}
}
@Override
public void connectionAcquired(final BackendConnection conn) {
final RouteResultsetNode node = (RouteResultsetNode) conn
.getAttachment();
session.bindConnection(node, conn);
_execute(conn, node);
}
private boolean decrementOkCountBy(int finished) {
lock.lock();
try {
return --okCount == 0;
} finally {
lock.unlock();
}
}
@Override
public void okResponse(byte[] data, BackendConnection conn) {
this.netOutBytes += data.length;
boolean executeResponse = conn.syncAndExcute();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("received ok response ,executeResponse:"
+ executeResponse + " from " + conn);
}
if (executeResponse) {
ServerConnection source = session.getSource();
OkPacket ok = new OkPacket();
ok.read(data);
//存储过程
boolean isCanClose2Client =(!rrs.isCallStatement()) ||(rrs.isCallStatement() &&!rrs.getProcedure().isResultSimpleValue());;
if(!isCallProcedure)
{
if (clearIfSessionClosed(session))
{
return;
} else if (canClose(conn, false))
{
return;
}
}
lock.lock();
try {
// 判断是否是全局表,如果是,执行行数不做累加,以最后一次执行的为准。
if (!rrs.isGlobalTable()) {
affectedRows += ok.affectedRows;
} else {
affectedRows = ok.affectedRows;
}
if (ok.insertId > 0) {
insertId = (insertId == 0) ? ok.insertId : Math.min(
insertId, ok.insertId);
}
} finally {
lock.unlock();
}
// 对于存储过程,其比较特殊,查询结果返回EndRow报文以后,还会再返回一个OK报文,才算结束
boolean isEndPacket = isCallProcedure ? decrementOkCountBy(1): decrementCountBy(1);
if (isEndPacket && isCanClose2Client) {
if (this.autocommit && !session.getSource().isLocked()) {// clear all connections
session.releaseConnections(false);
}
if (this.isFail() || session.closed()) {
tryErrorFinished(true);
return;
}
lock.lock();
try {
if (rrs.isLoadData()) {
byte lastPackId = source.getLoadDataInfileHandler()
.getLastPackId();
ok.packetId = ++lastPackId;// OK_PACKET
ok.message = ("Records: " + affectedRows + " Deleted: 0 Skipped: 0 Warnings: 0")
.getBytes();// 此处信息只是为了控制台给人看的
source.getLoadDataInfileHandler().clear();
} else {
ok.packetId = ++packetId;// OK_PACKET
}
ok.affectedRows = affectedRows;
ok.serverStatus = source.isAutocommit() ? 2 : 1;
if (insertId > 0) {
ok.insertId = insertId;
source.setLastInsertId(insertId);
}
ok.write(source);
} catch (Exception e) {
handleDataProcessException(e);
} finally {
lock.unlock();
}
}
// add by lian
// 解决sql统计中写操作永远为0
execCount++;
if (execCount == rrs.getNodes().length) {
QueryResult queryResult = new QueryResult(session.getSource().getUser(),
rrs.getSqlType(), rrs.getStatement(), selectRows, netInBytes, netOutBytes, startTime, System.currentTimeMillis(),0);
QueryResultDispatcher.dispatchQuery( queryResult );
}
}
}
@Override
public void rowEofResponse(final byte[] eof, BackendConnection conn) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("on row end reseponse " + conn);
}
this.netOutBytes += eof.length;
MiddlerResultHandler middlerResultHandler = session.getMiddlerResultHandler();
if (errorRepsponsed.get()) {
// the connection has been closed or set to "txInterrupt" properly
//in tryErrorFinished() method! If we close it here, it can
// lead to tx error such as blocking rollback tx for ever.
// @author Uncle-pan
// @since 2016-03-25
// conn.close(this.error);
return;
}
final ServerConnection source = session.getSource();
if (!isCallProcedure) {
if (clearIfSessionClosed(session)) {
return;
} else if (canClose(conn, false)) {
return;
}
}
if (decrementCountBy(1)) {
if (!rrs.isCallStatement()||(rrs.isCallStatement()&&rrs.getProcedure().isResultSimpleValue())) {
if (this.autocommit && !session.getSource().isLocked()) {// clear all connections
session.releaseConnections(false);
}
if (this.isFail() || session.closed()) {
tryErrorFinished(true);
return;
}
}
if (dataMergeSvr != null) {
//huangyiming add 数据合并前如果有中间过程则先执行数据合并再执行下一步
if(session.getMiddlerResultHandler() !=null ){
isMiddleResultDone.set(true);
}
try {
dataMergeSvr.outputMergeResult(session, eof);
} catch (Exception e) {
handleDataProcessException(e);
}
} else {
try {
lock.lock();
eof[3] = ++packetId;
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("last packet id:" + packetId);
}
if( middlerResultHandler ==null ){
//middlerResultHandler.secondEexcute();
source.write(eof);
}
} finally {
lock.unlock();
}
}
}
execCount++;
if(middlerResultHandler !=null){
if (execCount != rrs.getNodes().length) {
return;
}
/*else{
middlerResultHandler.secondEexcute();
}*/
}
if (execCount == rrs.getNodes().length) {
int resultSize = source.getWriteQueue().size()*MycatServer.getInstance().getConfig().getSystem().getBufferPoolPageSize();
//TODO: add by zhuam
//查询结果派发
QueryResult queryResult = new QueryResult(session.getSource().getUser(),
rrs.getSqlType(), rrs.getStatement(), selectRows, netInBytes, netOutBytes, startTime, System.currentTimeMillis(),resultSize);
QueryResultDispatcher.dispatchQuery( queryResult );
// add huangyiming 如果是中间过程,必须等数据合并好了再进行下一步语句的拼装
if(middlerResultHandler !=null ){
while (!this.isMiddleResultDone.compareAndSet(false, true)) {
Thread.yield();
}
middlerResultHandler.secondEexcute();
isMiddleResultDone.set(false);
}
}
}
/**
* 将汇聚结果集数据真正的发送给Mycat客户端
* @param source
* @param eof
* @param
*/
public void outputMergeResult(final ServerConnection source, final byte[] eof, Iterator<UnsafeRow> iter,AtomicBoolean isMiddleResultDone) {
try {
lock.lock();
ByteBuffer buffer = session.getSource().allocate();
final RouteResultset rrs = this.dataMergeSvr.getRrs();
/**
* 处理limit语句的start 和 end位置,将正确的结果发送给
* Mycat 客户端
*/
int start = rrs.getLimitStart();
int end = start + rrs.getLimitSize();
int index = 0;
if (start < 0)
start = 0;
if (rrs.getLimitSize() < 0)
end = Integer.MAX_VALUE;
if(prepared) {
while (iter.hasNext()){
UnsafeRow row = iter.next();
if(index >= start){
row.packetId = ++packetId;
BinaryRowDataPacket binRowPacket = new BinaryRowDataPacket();
binRowPacket.read(fieldPackets, row);
buffer = binRowPacket.write(buffer, source, true);
}
index++;
if(index == end){
break;
}
}
} else {
while (iter.hasNext()){
UnsafeRow row = iter.next();
if(index >= start){
row.packetId = ++packetId;
buffer = row.write(buffer,source,true);
}
index++;
if(index == end){
break;
}
}
}
eof[3] = ++packetId;
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("last packet id:" + packetId);
}
//huangyiming add 中间过程缓存起来,isMiddleResultDone是确保合并部分执行完成后才会执行secondExecute
MiddlerResultHandler middlerResultHandler = source.getSession2().getMiddlerResultHandler();
if(null != middlerResultHandler){
if(buffer.position() > 0){
buffer.flip();
byte[] data = new byte[buffer.limit()];
buffer.get(data);
buffer.clear();
//如果该操作只是一个中间过程则把结果存储起来
String str = ResultSetUtil.getColumnValAsString(data, fields, 0);
//真的需要数据合并的时候才合并
if(rrs.isHasAggrColumn()){
middlerResultHandler.getResult().clear();
if(str !=null){
middlerResultHandler.add(str);
}
}
}
isMiddleResultDone.set(false);
}else{
ByteBuffer byteBuffer = source.writeToBuffer(eof, buffer);
/**
* 真正的开始把Writer Buffer的数据写入到channel 中
*/
session.getSource().write(byteBuffer);
}
} catch (Exception e) {
e.printStackTrace();
handleDataProcessException(e);
} finally {
lock.unlock();
dataMergeSvr.clear();
}
}
public void outputMergeResult(final ServerConnection source,
final byte[] eof, List<RowDataPacket> results) {
try {
lock.lock();
ByteBuffer buffer = session.getSource().allocate();
final RouteResultset rrs = this.dataMergeSvr.getRrs();
// 处理limit语句
int start = rrs.getLimitStart();
int end = start + rrs.getLimitSize();
if (start < 0) {
start = 0;
}
if (rrs.getLimitSize() < 0) {
end = results.size();
}
// // 对于不需要排序的语句,返回的数据只有rrs.getLimitSize()
// if (rrs.getOrderByCols() == null) {
// end = results.size();
// start = 0;
// }
if (end > results.size()) {
end = results.size();
}
// for (int i = start; i < end; i++) {
// RowDataPacket row = results.get(i);
// if( prepared ) {
// BinaryRowDataPacket binRowDataPk = new BinaryRowDataPacket();
// binRowDataPk.read(fieldPackets, row);
// binRowDataPk.packetId = ++packetId;
// //binRowDataPk.write(source);
// buffer = binRowDataPk.write(buffer, session.getSource(), true);
// } else {
// row.packetId = ++packetId;
// buffer = row.write(buffer, source, true);
// }
// }
if(prepared) {
for (int i = start; i < end; i++) {
RowDataPacket row = results.get(i);
BinaryRowDataPacket binRowDataPk = new BinaryRowDataPacket();
binRowDataPk.read(fieldPackets, row);
binRowDataPk.packetId = ++packetId;
//binRowDataPk.write(source);
buffer = binRowDataPk.write(buffer, session.getSource(), true);
}
} else {
for (int i = start; i < end; i++) {
RowDataPacket row = results.get(i);
row.packetId = ++packetId;
buffer = row.write(buffer, source, true);
}
}
eof[3] = ++packetId;
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("last packet id:" + packetId);
}
source.write(source.writeToBuffer(eof, buffer));
} catch (Exception e) {
handleDataProcessException(e);
} finally {
lock.unlock();
dataMergeSvr.clear();
}
}
@Override
public void fieldEofResponse(byte[] header, List<byte[]> fields,
byte[] eof, BackendConnection conn) {
//huangyiming add
this.header = header;
this.fields = fields;
MiddlerResultHandler middlerResultHandler = session.getMiddlerResultHandler();
/*if(null !=middlerResultHandler ){
return;
}*/
// 计算 网络流量 :后端数据库=》MyCAT
this.netOutBytes += header.length;
this.netOutBytes += eof.length;
for (int i = 0, len = fields.size(); i < len; ++i) {
byte[] field = fields.get(i);
this.netOutBytes += field.length;
}
ServerConnection source = null;
if (fieldsReturned) {
return;
}
lock.lock();
try {
if (fieldsReturned) {
return;
}
fieldsReturned = true;
// 计算需要 移除、重命名 的 求平均 的 字段
boolean needMerg = (dataMergeSvr != null) && dataMergeSvr.getRrs().needMerge();
Set<String> shouldRemoveAvgField = new HashSet<>();
Set<String> shouldRenameAvgField = new HashSet<>();
if (needMerg) {
Map<String, Integer> mergeColsMap = dataMergeSvr.getRrs().getMergeCols();
if (mergeColsMap != null) {
for (Map.Entry<String, Integer> entry : mergeColsMap.entrySet()) {
String key = entry.getKey();
int mergeType = entry.getValue();
if (MergeCol.MERGE_AVG == mergeType && mergeColsMap.containsKey(key + "SUM")) {
shouldRemoveAvgField.add((key + "COUNT").toUpperCase()); // 移除
shouldRenameAvgField.add((key + "SUM").toUpperCase()); // 重命名
}
}
}
}
// 输出 header 到 buffer
source = session.getSource();
ByteBuffer buffer = source.allocate();
fieldCount = fields.size();
if (shouldRemoveAvgField.size() > 0) {
ResultSetHeaderPacket packet = new ResultSetHeaderPacket();
packet.packetId = ++packetId;
packet.fieldCount = fieldCount - shouldRemoveAvgField.size();
buffer = packet.write(buffer, source, true);
} else {
header[3] = ++packetId;
buffer = source.writeToBuffer(header, buffer);
}
String primaryKey = null; // TODO 待读
if (rrs.hasPrimaryKeyToCache()) {
String[] items = rrs.getPrimaryKeyItems();
priamaryKeyTable = items[0];
primaryKey = items[1];
}
// 输出 field 到 buffer;
Map<String, ColMeta> columToIndx = new HashMap<String, ColMeta>(fieldCount); // 字段元数据集合
for (int i = 0, len = fieldCount; i < len; ++i) {
boolean shouldSkip = false;
byte[] field = fields.get(i);
if (needMerg) { // important:如果需要合并,设置字段元数据集合
FieldPacket fieldPkg = new FieldPacket();
fieldPkg.read(field);
fieldPackets.add(fieldPkg);
String fieldName = new String(fieldPkg.name).toUpperCase();
if (columToIndx != null && !columToIndx.containsKey(fieldName)) {
// 移除 需要移除的求平均字段
if (shouldRemoveAvgField.contains(fieldName)) {
shouldSkip = true;
fieldPackets.remove(fieldPackets.size() - 1);
}
// 重命名 需要重命名的求平均字段
if (shouldRenameAvgField.contains(fieldName)) {
String newFieldName = fieldName.substring(0, fieldName.length() - 3); // 截取结尾AVG
fieldPkg.name = newFieldName.getBytes();
fieldPkg.packetId = ++packetId;
shouldSkip = true;
// 处理AVG字段位数和精度, AVG位数 = SUM位数 - 14
fieldPkg.length = fieldPkg.length - 14;
// AVG精度 = SUM精度 + 4
fieldPkg.decimals = (byte) (fieldPkg.decimals + 4);
// 输出 field 到 buffer
buffer = fieldPkg.write(buffer, source, false);
// 还原精度
fieldPkg.decimals = (byte) (fieldPkg.decimals - 4);
}
// 设置字段元数据集合
ColMeta colMeta = new ColMeta(i, fieldPkg.type);
colMeta.decimals = fieldPkg.decimals;
columToIndx.put(fieldName, colMeta);
}
} else {
FieldPacket fieldPkg = new FieldPacket();
fieldPkg.read(field);
fieldPackets.add(fieldPkg);
fieldCount = fields.size();
if (primaryKey != null && primaryKeyIndex == -1) {
// find primary key index
String fieldName = new String(fieldPkg.name);
if (primaryKey.equalsIgnoreCase(fieldName)) {
primaryKeyIndex = i;
}
}
}
// 输出 field 到 buffer
if (!shouldSkip) {
field[3] = ++packetId;
buffer = source.writeToBuffer(field, buffer);
}
}
// 输出 eof
eof[3] = ++packetId;
buffer = source.writeToBuffer(eof, buffer);
// 输出 field 到 socket
if (null == middlerResultHandler ) {
//session.getSource().write(row);
source.write(buffer);
}
if (dataMergeSvr != null) {
dataMergeSvr.onRowMetaData(columToIndx, fieldCount);
}
} catch (Exception e) {
handleDataProcessException(e);
} finally {
lock.unlock();
}
}
public void handleDataProcessException(Exception e) {
if (!errorRepsponsed.get()) {
this.error = e.toString();
LOGGER.warn("caught exception ", e);
setFail(e.toString());
this.tryErrorFinished(true);
}
}
@Override
public void rowResponse(final byte[] row, final BackendConnection conn) {
if (errorRepsponsed.get()) {
// the connection has been closed or set to "txInterrupt" properly
//in tryErrorFinished() method! If we close it here, it can
// lead to tx error such as blocking rollback tx for ever.
// @author Uncle-pan
// @since 2016-03-25
//conn.close(error);
return;
}
lock.lock();
try {
this.selectRows++;
RouteResultsetNode rNode = (RouteResultsetNode) conn.getAttachment();
String dataNode = rNode.getName();
if (dataMergeSvr != null) {
// even through discarding the all rest data, we can't
//close the connection for tx control such as rollback or commit.
// So the "isClosedByDiscard" variable is unnecessary.
// @author Uncle-pan
// @since 2016-03-25
dataMergeSvr.onNewRecord(dataNode, row);
MiddlerResultHandler middlerResultHandler = session.getMiddlerResultHandler();
if(null != middlerResultHandler ){
if(middlerResultHandler instanceof MiddlerQueryResultHandler){
byte[] rv = ResultSetUtil.getColumnVal(row, fields, 0);
String rowValue = rv==null? "":new String(rv);
middlerResultHandler.add(rowValue);
}
}
} else {
row[3] = ++packetId;
RowDataPacket rowDataPkg =null;
// cache primaryKey-> dataNode
if (primaryKeyIndex != -1) {
rowDataPkg = new RowDataPacket(fieldCount);
rowDataPkg.read(row);
String primaryKey = new String(rowDataPkg.fieldValues.get(primaryKeyIndex));
LayerCachePool pool = MycatServer.getInstance().getRouterservice().getTableId2DataNodeCache();
pool.putIfAbsent(priamaryKeyTable, primaryKey, dataNode);
}
if( prepared ) {
if(rowDataPkg==null) {
rowDataPkg = new RowDataPacket(fieldCount);
rowDataPkg.read(row);
}
BinaryRowDataPacket binRowDataPk = new BinaryRowDataPacket();
binRowDataPk.read(fieldPackets, rowDataPkg);
binRowDataPk.write(session.getSource());
} else {
//add huangyiming
MiddlerResultHandler middlerResultHandler = session.getMiddlerResultHandler();
if(null == middlerResultHandler ){
session.getSource().write(row);
}else{
if(middlerResultHandler instanceof MiddlerQueryResultHandler){
String rowValue = ResultSetUtil.getColumnValAsString(row, fields, 0);
middlerResultHandler.add(rowValue);
}
}
}
}
} catch (Exception e) {
handleDataProcessException(e);
} finally {
lock.unlock();
}
}
@Override
public void clearResources() {
if (dataMergeSvr != null) {
dataMergeSvr.clear();
}
}
@Override
public void writeQueueAvailable() {
}
@Override
public void requestDataResponse(byte[] data, BackendConnection conn) {
LoadDataUtil.requestFileDataResponse(data, conn);
}
public boolean isPrepared() {
return prepared;
}
public void setPrepared(boolean prepared) {
this.prepared = prepared;
}
}