Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit 46822d8

Browse files
author
Tianyi Chen
committed
format and comments added
1 parent 527176e commit 46822d8

File tree

9 files changed

+98
-81
lines changed

9 files changed

+98
-81
lines changed

src/include/common/internal_types.h

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ extern int TEST_TUPLES_PER_TILEGROUP;
7474
enum class CmpBool {
7575
FALSE = 0,
7676
TRUE = 1,
77-
NULL_ = 2 // Note the underscore suffix
77+
NULL_ = 2 // Note the underscore suffix
7878
};
7979

8080
//===--------------------------------------------------------------------===//
@@ -115,7 +115,6 @@ std::string PostgresValueTypeToString(PostgresValueType type);
115115
PostgresValueType StringToPostgresValueType(const std::string &str);
116116
std::ostream &operator<<(std::ostream &os, const PostgresValueType &type);
117117

118-
119118
//===--------------------------------------------------------------------===//
120119
// Predicate Expression Operation Types
121120
//===--------------------------------------------------------------------===//
@@ -631,10 +630,12 @@ std::string DropTypeToString(DropType type);
631630
DropType StringToDropType(const std::string &str);
632631
std::ostream &operator<<(std::ostream &os, const DropType &type);
633632

634-
template<class E> class EnumHash {
633+
template <class E>
634+
class EnumHash {
635635
public:
636-
size_t operator()(const E&e) const {
637-
return std::hash<typename std::underlying_type<E>::type>()(static_cast<typename std::underlying_type<E>::type>(e));
636+
size_t operator()(const E &e) const {
637+
return std::hash<typename std::underlying_type<E>::type>()(
638+
static_cast<typename std::underlying_type<E>::type>(e));
638639
}
639640
};
640641

@@ -665,9 +666,9 @@ enum class StatementType {
665666
ALTER = 12, // alter statement type
666667
TRANSACTION = 13, // transaction statement type,
667668
COPY = 14, // copy type
668-
ANALYZE = 15, // analyze type
669+
ANALYZE = 15, // analyze type
669670
VARIABLE_SET = 16, // variable set statement type
670-
CREATE_FUNC = 17, // create func statement type
671+
CREATE_FUNC = 17, // create func statement type
671672
};
672673
std::string StatementTypeToString(StatementType type);
673674
StatementType StringToStatementType(const std::string &str);
@@ -678,24 +679,24 @@ std::ostream &operator<<(std::ostream &os, const StatementType &type);
678679
//===--------------------------------------------------------------------===//
679680

680681
enum class QueryType {
681-
QUERY_BEGIN = 0, // begin query
682-
QUERY_COMMIT = 1, // commit query
683-
QUERY_ROLLBACK = 2, // rollback query
684-
QUERY_CREATE_TABLE = 3, // create query
682+
QUERY_BEGIN = 0, // begin query
683+
QUERY_COMMIT = 1, // commit query
684+
QUERY_ROLLBACK = 2, // rollback query
685+
QUERY_CREATE_TABLE = 3, // create query
685686
QUERY_CREATE_DB = 4,
686687
QUERY_CREATE_INDEX = 5,
687-
QUERY_DROP = 6, // other queries
688-
QUERY_INSERT = 7, // insert query
689-
QUERY_PREPARE = 8, // prepare query
690-
QUERY_EXECUTE = 9, // execute query
688+
QUERY_DROP = 6, // other queries
689+
QUERY_INSERT = 7, // insert query
690+
QUERY_PREPARE = 8, // prepare query
691+
QUERY_EXECUTE = 9, // execute query
691692
QUERY_UPDATE = 10,
692693
QUERY_DELETE = 11,
693694
QUERY_RENAME = 12,
694695
QUERY_ALTER = 13,
695696
QUERY_COPY = 14,
696697
QUERY_ANALYZE = 15,
697-
QUERY_SET = 16, // set query
698-
QUERY_SHOW = 17, // show query
698+
QUERY_SET = 16, // set query
699+
QUERY_SHOW = 17, // show query
699700
QUERY_SELECT = 18,
700701
QUERY_OTHER = 19,
701702
QUERY_INVALID = 20,
@@ -705,8 +706,11 @@ enum class QueryType {
705706
};
706707
std::string QueryTypeToString(QueryType query_type);
707708
QueryType StringToQueryType(std::string str);
708-
namespace parser{ class SQLStatement;}
709-
QueryType StatementTypeToQueryType(StatementType stmt_type, const parser::SQLStatement* sql_stmt);
709+
namespace parser {
710+
class SQLStatement;
711+
}
712+
QueryType StatementTypeToQueryType(StatementType stmt_type,
713+
const parser::SQLStatement *sql_stmt);
710714
//===--------------------------------------------------------------------===//
711715
// Scan Direction Types
712716
//===--------------------------------------------------------------------===//
@@ -1014,7 +1018,8 @@ const int TRIGGER_TYPE_MAX = TRIGGER_TYPE_ROW | TRIGGER_TYPE_STATEMENT |
10141018

10151019
// Statistics Collection Type
10161020
// Disable or enable
1017-
// TODO: This should probably be a collection level and not a boolean (enable/disable)
1021+
// TODO: This should probably be a collection level and not a boolean
1022+
// (enable/disable)
10181023
enum class StatsType {
10191024
// Disable statistics collection
10201025
INVALID = INVALID_TYPE_ID,
@@ -1093,10 +1098,7 @@ static const int INVALID_FILE_DESCRIPTOR = -1;
10931098
// Tuple serialization formats
10941099
// ------------------------------------------------------------------
10951100

1096-
enum class TupleSerializationFormat {
1097-
NATIVE = 0,
1098-
DR = 1
1099-
};
1101+
enum class TupleSerializationFormat { NATIVE = 0, DR = 1 };
11001102

11011103
// ------------------------------------------------------------------
11021104
// Entity types
@@ -1201,7 +1203,7 @@ std::ostream &operator<<(std::ostream &os, const RWType &type);
12011203

12021204
// ItemPointer -> type
12031205
typedef CuckooMap<ItemPointer, RWType, ItemPointerHasher, ItemPointerComparator>
1204-
ReadWriteSet;
1206+
ReadWriteSet;
12051207

12061208
// this enum is to identify why the version should be GC'd.
12071209
enum class GCVersionType {
@@ -1406,7 +1408,6 @@ enum class NetworkProtocolType {
14061408
POSTGRES_PSQL,
14071409
};
14081410

1409-
14101411
enum class SSLLevel {
14111412
SSL_DISABLE = 0,
14121413
SSL_PREFER = 1,

src/include/network/connection_handle.h

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,7 @@ class ConnectionHandle {
148148
friend class ConnectionHandleFactory;
149149

150150
ConnectionHandle(int sock_fd, ConnectionHandlerTask *handler,
151-
std::shared_ptr<Buffer> rbuf,
152-
std::shared_ptr<Buffer> wbuf);
153-
154-
ProcessResult ProcessInitial();
155-
156-
/**
157-
* Extracts the header of a Postgres start up packet from the read socket
158-
* buffer
159-
*/
160-
static bool ReadStartupPacketHeader(Buffer &rbuf, InputPacket &rpkt);
151+
std::shared_ptr<Buffer> rbuf, std::shared_ptr<Buffer> wbuf);
161152

162153
/**
163154
* Writes a packet's header (type, size) into the write buffer
@@ -176,8 +167,17 @@ class ConnectionHandle {
176167
WriteState FlushWriteBuffer();
177168

178169
/**
179-
* Process SSL handshake to generate valid SSL connection context
180-
* for further communications
170+
* Flush out responses and process SSL handshake
171+
*/
172+
Transition ProcessWrite_SSLHandshake();
173+
174+
/**
175+
* @brief: process SSL handshake to generate valid SSL
176+
* connection context for further communications
177+
* @return FINISH when the SSL handshake failed
178+
* PROCEED when the SSL handshake success
179+
* NEED_DATA when the SSL handshake is partially done due to network
180+
* latency
181181
*/
182182
Transition SSLHandshake();
183183

@@ -201,10 +201,13 @@ class ConnectionHandle {
201201
}
202202

203203
/**
204-
* Determine if there is still responses in the buffer
204+
* @brief: Determine if there is still responses in the buffer
205+
* @return true if there is still responses to flush out in either wbuf or
206+
* responses
205207
*/
206208
inline bool HasResponse() {
207-
return (protocol_handler_->responses_.size() != 0) || (wbuf_->buf_size != 0);
209+
return (protocol_handler_->responses_.size() != 0) ||
210+
(wbuf_->buf_size != 0);
208211
}
209212

210213
int sock_fd_; // socket file descriptor

src/include/network/marshal.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ struct OutputPacket {
138138
size_t ptr; // ByteBuf cursor, which is used for get and put
139139
NetworkMessageType msg_type; // header
140140

141-
bool single_type_pkt; // there would be only a pkt type being written to the
142-
// buffer when this flag is true
141+
bool single_type_pkt; // there would be only a pkt type being written to the
142+
// buffer when this flag is true
143143
bool skip_header_write; // whether we should write header to socket wbuf
144144
size_t write_ptr; // cursor used to write packet content to socket wbuf
145145

@@ -160,8 +160,7 @@ struct OutputPacket {
160160
*/
161161

162162
/* packet_put_byte - used to write a single byte into a packet */
163-
extern void
164-
PacketPutByte(OutputPacket *pkt, const uchar c);
163+
extern void PacketPutByte(OutputPacket *pkt, const uchar c);
165164

166165
/* packet_put_string - used to write a string into a packet */
167166
extern void PacketPutStringWithTerminator(OutputPacket *pkt,

src/include/network/network_state.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ enum class ConnState {
2424
PROCESS, // State that runs the network protocol on received data
2525
CLOSING, // State for closing the client connection
2626
GET_RESULT, // State when triggered by worker thread that completes the task.
27-
PROCESS_WRITE_SSL_HANDSHAKE, // State to flush out responses and doing (Real)
28-
// SSL handshake
27+
PROCESS_WRITE_SSL_HANDSHAKE, // State to flush out responses and doing (Real)
28+
// SSL handshake
2929
};
3030

3131
// TODO(tianyu): Convert use cases of this to just return Transition

src/include/network/postgres_protocol_handler.h

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ class PostgresProtocolHandler : public ProtocolHandler {
4242
~PostgresProtocolHandler();
4343
/**
4444
* Parse the content in the buffer and process to generate results.
45-
* thread_id is the thread of current running thread. This is used
45+
* @param rbuf The read buffer of network
46+
* @param thread_id The thread of current running thread. This is used
4647
* to generate txn
48+
* @return @see ProcessResult
4749
*/
4850
ProcessResult Process(Buffer &rbuf, size_t thread_id);
4951

@@ -70,16 +72,32 @@ class PostgresProtocolHandler : public ProtocolHandler {
7072
// STATIC HELPERS
7173
//===--------------------------------------------------------------------===//
7274

73-
// Parse the input packet based on if it is the startup packet
75+
/**
76+
* @brief Parse the input packet from rbuf
77+
* @param rbuf network read buffer
78+
* @param rpkt the postgres rpkt we want to parse to
79+
* @param startup_format whether we want the rpkt to be of startup packet
80+
* format
81+
* (i.e. no type byte)
82+
* @return true if the parsing is complete
83+
*/
7484
static bool ParseInputPacket(Buffer &rbuf, InputPacket &rpkt,
7585
bool startup_format);
7686

77-
// Packet Reading Function
78-
// Extracts the contents of Postgres packet from the read socket buffer
87+
/**
88+
* @brief Helper function to extract the body of Postgres packet from the
89+
* read buffer
90+
* @param rbuf network read buffer
91+
* @param rpkt the postgres rpkt we want to parse to
92+
* @return true if the parsing is complete
93+
*/
7994
static bool ReadPacket(Buffer &rbuf, InputPacket &rpkt);
8095

81-
// Packet Reading Function
82-
// Extracts the header of a Postgres packet from the read socket buffer
96+
/**
97+
* @brief Helper function to extract the header of a Postgres packet from the
98+
* read buffer
99+
* @see ParseInputPacket from param and return value
100+
*/
83101
static bool ReadPacketHeader(Buffer &rbuf, InputPacket &rpkt,
84102
bool startup_format);
85103

@@ -96,7 +114,7 @@ class PostgresProtocolHandler : public ProtocolHandler {
96114
* @brief Main Switch function to process general packets
97115
*/
98116
ProcessResult ProcessNormalPacket(InputPacket *pkt, const size_t thread_id);
99-
117+
100118
/**
101119
* @brief Helper function to process startup packet
102120
* @param proto_version protocol version of the session

src/include/network/protocol_handler.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ class ProtocolHandler {
2929

3030
virtual ~ProtocolHandler();
3131

32+
// TODO(Tianyi) Move thread_id to traffic_cop
33+
// TODO(Tianyi) Make wbuf as an parameter here
3234
/**
3335
* Main switch case wrapper to process every packet apart from the startup
34-
* packet. Avoid flushing the response for extended protocols.
36+
* packet. Avoid flushing the response for extended protocols.
3537
*/
3638
virtual ProcessResult Process(Buffer &rbuf, const size_t thread_id);
3739

@@ -53,7 +55,6 @@ class ProtocolHandler {
5355

5456
// The traffic cop used for this connection
5557
tcop::TrafficCop *traffic_cop_;
56-
5758
};
5859

5960
} // namespace network

src/network/connection_handle.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ namespace {
7272
return
7373
#define SET_STATE_TO(s) \
7474
{ \
75-
ConnState::s,
76-
#define AND_INVOKE(m) \
77-
([](ConnectionHandle &w) { return w.m(); }) \
78-
} \
75+
ConnState::s,
76+
#define AND_INVOKE(m) \
77+
([](ConnectionHandle & w) { return w.m(); }) \
78+
} \
7979
;
8080
#define AND_WAIT \
8181
([](ConnectionHandle &) { return Transition::NONE; }) \
@@ -123,8 +123,8 @@ DEF_TRANSITION_GRAPH
123123
END_DEF
124124
// clang-format on
125125

126-
void ConnectionHandle::StateMachine::Accept(Transition action,
127-
ConnectionHandle &connection) {
126+
void ConnectionHandle::StateMachine::Accept(Transition action,
127+
ConnectionHandle &connection) {
128128
Transition next = action;
129129
while (next != Transition::NONE) {
130130
transition_result result = Delta_(current_state_, next);
@@ -158,12 +158,10 @@ ConnectionHandle::ConnectionHandle(int sock_fd, ConnectionHandlerTask *handler,
158158

159159
// TODO(Tianyu): should put the initialization else where.. check correctness
160160
// first.
161-
traffic_cop_.SetTaskCallback(
162-
[](void *arg) {
163-
struct event *event = static_cast<struct event *>(arg);
164-
event_active(event, EV_WRITE, 0);
165-
},
166-
workpool_event);
161+
traffic_cop_.SetTaskCallback([](void *arg) {
162+
struct event *event = static_cast<struct event *>(arg);
163+
event_active(event, EV_WRITE, 0);
164+
}, workpool_event);
167165
}
168166

169167
void ConnectionHandle::UpdateEventFlags(short flags) {
@@ -293,9 +291,9 @@ Transition ConnectionHandle::FillReadBuffer() {
293291
}
294292
}
295293
default: {
296-
throw NetworkProcessException(
297-
"SSL read error: %d, error code: " + std::to_string(err) +
298-
" error code:" + std::to_string(ecode));
294+
throw NetworkProcessException("SSL read error: %d, error code: " +
295+
std::to_string(err) + " error code:" +
296+
std::to_string(ecode));
299297
}
300298
}
301299
} else {

src/network/postgres_protocol_handler.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,25 +1032,21 @@ ProcessResult PostgresProtocolHandler::ProcessStartupPacket(
10321032

10331033
ProcessResult PostgresProtocolHandler::Process(Buffer &rbuf,
10341034
const size_t thread_id) {
1035-
InputPacket rpkt;
1036-
rpkt.Reset();
1037-
if (!ParseInputPacket(rbuf, rpkt, init_stage_))
1035+
if (!ParseInputPacket(rbuf, request_, init_stage_))
10381036
return ProcessResult::MORE_DATA_REQUIRED;
10391037

10401038
ProcessResult process_status;
10411039
if (init_stage_) {
1042-
process_status = ProcessInitialPacket(&rpkt);
1040+
process_status = ProcessInitialPacket(&request_);
10431041
} else {
1044-
process_status = ProcessNormalPacket(&rpkt, thread_id);
1042+
process_status = ProcessNormalPacket(&request_, thread_id);
10451043
}
10461044

1045+
request_.Reset();
1046+
10471047
return process_status;
10481048
}
10491049

1050-
/*
1051-
* process_packet - Main switch block; process incoming packets,
1052-
* Returns false if the session needs to be closed.
1053-
*/
10541050
ProcessResult PostgresProtocolHandler::ProcessNormalPacket(
10551051
InputPacket *pkt, const size_t thread_id) {
10561052
LOG_TRACE("Message type: %c", static_cast<unsigned char>(pkt->msg_type));

src/network/protocol_handler.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ ProtocolHandler::ProtocolHandler(tcop::TrafficCop *traffic_cop) {
2323

2424
ProtocolHandler::~ProtocolHandler() {}
2525

26-
ProcessResult ProtocolHandler::Process(
27-
UNUSED_ATTRIBUTE Buffer &rbuf, UNUSED_ATTRIBUTE const size_t thread_id) {
26+
ProcessResult ProtocolHandler::Process(UNUSED_ATTRIBUTE Buffer &rbuf,
27+
UNUSED_ATTRIBUTE const size_t
28+
thread_id) {
2829
return ProcessResult::TERMINATE;
2930
}
3031

0 commit comments

Comments
 (0)