21
21
#include " expression/expression_util.h"
22
22
#include " network/marshal.h"
23
23
#include " network/postgres_protocol_handler.h"
24
+ #include " network/peloton_server.h"
24
25
#include " parser/postgresparser.h"
25
26
#include " planner/abstract_plan.h"
26
27
#include " planner/delete_plan.h"
@@ -57,7 +58,6 @@ const std::unordered_map<std::string, std::string>
57
58
58
59
PostgresProtocolHandler::PostgresProtocolHandler (tcop::TrafficCop *traffic_cop)
59
60
: ProtocolHandler(traffic_cop),
60
- stage_ (CommStage::SSL_SETUP),
61
61
txn_state_ (NetworkTransactionStateType::IDLE) {}
62
62
63
63
PostgresProtocolHandler::~PostgresProtocolHandler () {}
@@ -429,7 +429,7 @@ void PostgresProtocolHandler::ExecBindMessage(InputPacket *pkt) {
429
429
std::unique_ptr<OutputPacket> response (new OutputPacket ());
430
430
// Send Bind complete response
431
431
response->msg_type = NetworkMessageType::BIND_COMPLETE;
432
- responses .push_back (std::move (response));
432
+ responses_ .push_back (std::move (response));
433
433
// TODO(Tianyi) This is a hack to respond correct describe message
434
434
// as well as execute message
435
435
skipped_stmt_ = true ;
@@ -456,7 +456,7 @@ void PostgresProtocolHandler::ExecBindMessage(InputPacket *pkt) {
456
456
std::unique_ptr<OutputPacket> response (new OutputPacket ());
457
457
// Send Bind complete response
458
458
response->msg_type = NetworkMessageType::BIND_COMPLETE;
459
- responses .push_back (std::move (response));
459
+ responses_ .push_back (std::move (response));
460
460
return ;
461
461
}
462
462
@@ -536,7 +536,7 @@ void PostgresProtocolHandler::ExecBindMessage(InputPacket *pkt) {
536
536
// send bind complete
537
537
std::unique_ptr<OutputPacket> response (new OutputPacket ());
538
538
response->msg_type = NetworkMessageType::BIND_COMPLETE;
539
- responses .push_back (std::move (response));
539
+ responses_ .push_back (std::move (response));
540
540
}
541
541
542
542
size_t PostgresProtocolHandler::ReadParamType (
@@ -694,7 +694,7 @@ ProcessResult PostgresProtocolHandler::ExecDescribeMessage(InputPacket *pkt) {
694
694
// send 'no-data' message
695
695
std::unique_ptr<OutputPacket> response (new OutputPacket ());
696
696
response->msg_type = NetworkMessageType::NO_DATA_RESPONSE;
697
- responses .push_back (std::move (response));
697
+ responses_ .push_back (std::move (response));
698
698
return ProcessResult::COMPLETE;
699
699
}
700
700
@@ -868,28 +868,29 @@ void PostgresProtocolHandler::ExecCloseMessage(InputPacket *pkt) {
868
868
// Send close complete response
869
869
std::unique_ptr<OutputPacket> response (new OutputPacket ());
870
870
response->msg_type = NetworkMessageType::CLOSE_COMPLETE;
871
- responses .push_back (std::move (response));
871
+ responses_ .push_back (std::move (response));
872
872
}
873
873
874
- bool PostgresProtocolHandler::ParseInputPacket (Buffer &rbuf, InputPacket,
875
- bool starup_format ) {
876
- if (request .header_parsed == false ) {
874
+ bool PostgresProtocolHandler::ParseInputPacket (Buffer &rbuf, InputPacket &rpkt ,
875
+ bool startup_format ) {
876
+ if (rpkt .header_parsed == false ) {
877
877
// parse out the header first
878
- if (ReadPacketHeader (rbuf, request , startup_format) == false ) {
878
+ if (ReadPacketHeader (rbuf, rpkt , startup_format) == false ) {
879
879
// need more data
880
880
return false ;
881
881
}
882
882
}
883
883
884
- PL_ASSERT (request .header_parsed == true );
884
+ PL_ASSERT (rpkt .header_parsed == true );
885
885
886
- if (request .is_initialized == false ) {
886
+ if (rpkt .is_initialized == false ) {
887
887
// packet needs to be initialized with rest of the contents
888
- if (PostgresProtocolHandler::ReadPacket (rbuf, request ) == false ) {
888
+ if (PostgresProtocolHandler::ReadPacket (rbuf, rpkt ) == false ) {
889
889
// need more data
890
890
return false ;
891
891
}
892
892
}
893
+ return true ;
893
894
}
894
895
895
896
// The function tries to do a preliminary read to fetch the size value and
@@ -967,27 +968,14 @@ bool PostgresProtocolHandler::ReadPacket(Buffer &rbuf, InputPacket &rpkt) {
967
968
return true ;
968
969
}
969
970
970
- ProcessResult PostgresProtocolHandler::Process (Buffer &rbuf,
971
- const size_t thread_id) {
972
- if (!ParseInputPacket (rbuf, request_, init_stage_))
973
- return ProcessResult::MORE_DATA_REQUIRED;
974
-
975
- ProcessResult process_status;
976
- if (init_stage_) {
977
- process_status = ProcessInitialPacket (request_);
978
- } else {
979
- process_status = ProcessNormalPacket (request_);
980
- }
981
- request_.Reset ();
982
-
983
- return process_status;
984
- }
985
-
971
+ /*
972
+ * process_startup_packet - Processes the startup packet
973
+ * (after the size field of the header).
974
+ */
986
975
ProcessResult PostgresProtocolHandler::ProcessInitialPacket (InputPacket *pkt) {
987
976
int32_t proto_version = PacketGetInt (pkt, sizeof (int32_t ));
988
977
LOG_INFO (" protocol version: %d" , proto_version);
989
978
990
- force_flush_ = true ;
991
979
// TODO(Yuchen): consider more about return value
992
980
if (proto_version == SSL_MESSAGE_VERNO) {
993
981
LOG_TRACE (" process SSL MESSAGE" );
@@ -996,6 +984,7 @@ ProcessResult PostgresProtocolHandler::ProcessInitialPacket(InputPacket *pkt) {
996
984
response->msg_type =
997
985
ssl_able ? NetworkMessageType::SSL_YES : NetworkMessageType::SSL_NO;
998
986
responses_.push_back (std::move (response));
987
+ force_flush_ = true ;
999
988
return ssl_able ? ProcessResult::NEED_SSL_HANDSHAKE
1000
989
: ProcessResult::COMPLETE;
1001
990
} else {
@@ -1023,7 +1012,7 @@ ProcessResult PostgresProtocolHandler::ProcessStartupPacket(
1023
1012
if (pkt->ptr >= pkt->len ) break ;
1024
1013
GetStringToken (pkt, value);
1025
1014
LOG_TRACE (" Option value is %s" , token.c_str ());
1026
- client. cmdline_options [token] = value;
1015
+ cmdline_options_ [token] = value;
1027
1016
if (token.compare (" database" ) == 0 ) {
1028
1017
traffic_cop_->SetDefaultDatabaseName (value);
1029
1018
}
@@ -1035,10 +1024,32 @@ ProcessResult PostgresProtocolHandler::ProcessStartupPacket(
1035
1024
SendStartupResponse ();
1036
1025
1037
1026
init_stage_ = false ;
1027
+ force_flush_ = true ;
1038
1028
return ProcessResult::COMPLETE;
1039
1029
}
1040
1030
1041
- ProcessResult PostgresProtocolHandler::ProcessNormalPackets (
1031
+ ProcessResult PostgresProtocolHandler::Process (Buffer &rbuf,
1032
+ const size_t thread_id) {
1033
+ InputPacket rpkt;
1034
+ rpkt.Reset ();
1035
+ if (!ParseInputPacket (rbuf, rpkt, init_stage_))
1036
+ return ProcessResult::MORE_DATA_REQUIRED;
1037
+
1038
+ ProcessResult process_status;
1039
+ if (init_stage_) {
1040
+ process_status = ProcessInitialPacket (&rpkt);
1041
+ } else {
1042
+ process_status = ProcessNormalPacket (&rpkt, thread_id);
1043
+ }
1044
+
1045
+ return process_status;
1046
+ }
1047
+
1048
+ /*
1049
+ * process_packet - Main switch block; process incoming packets,
1050
+ * Returns false if the session needs to be closed.
1051
+ */
1052
+ ProcessResult PostgresProtocolHandler::ProcessNormalPacket (
1042
1053
InputPacket *pkt, const size_t thread_id) {
1043
1054
LOG_TRACE (" Message type: %c" , static_cast <unsigned char >(pkt->msg_type ));
1044
1055
// We don't set force_flush to true for `PBDE` messages because they're
@@ -1093,7 +1104,6 @@ ProcessResult PostgresProtocolHandler::ProcessNormalPackets(
1093
1104
}
1094
1105
return ProcessResult::COMPLETE;
1095
1106
}
1096
-
1097
1107
void PostgresProtocolHandler::MakeHardcodedParameterStatus (
1098
1108
const std::pair<std::string, std::string> &kv) {
1099
1109
std::unique_ptr<OutputPacket> response (new OutputPacket ());
0 commit comments