Skip to content

Refactor zpq_stream #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 60 additions & 38 deletions src/backend/libpq/pqcomm.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
*/
int Unix_socket_permissions;
char *Unix_socket_group;
bool libpq_compression;
bool libpq_compression;

/* Where the Unix socket files are (list of palloc'd strings) */
static List *sock_paths = NIL;
Expand All @@ -146,7 +146,7 @@ static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE];
static int PqRecvPointer; /* Next index to read a byte from PqRecvBuffer */
static int PqRecvLength; /* End of data available in PqRecvBuffer */

static ZpqStream* PqStream;
static ZpqStream * PqStream;


/*
Expand Down Expand Up @@ -191,17 +191,21 @@ const PQcommMethods *PqCommMethods = &PqCommSocketMethods;

WaitEventSet *FeBeWaitSet;

static ssize_t write_compressed(void* arg, void const* data, size_t size)
static ssize_t
write_compressed(void *arg, void const *data, size_t size)
{
ssize_t rc = secure_write((Port*)arg, (void*)data, size);
ssize_t rc = secure_write((Port *) arg, (void *) data, size);

if (rc > 0)
pgstat_report_network_traffic(0, 0, 0, rc);
return rc;
}

static ssize_t read_compressed(void* arg, void* data, size_t size)
static ssize_t
read_compressed(void *arg, void *data, size_t size)
{
ssize_t rc = secure_read((Port*)arg, data, size);
ssize_t rc = secure_read((Port *) arg, data, size);

if (rc > 0)
pgstat_report_network_traffic(0, 0, rc, 0);
return rc;
Expand All @@ -214,8 +218,9 @@ static void
SendCompressionACK(int algorithm)
{
StringInfoData buf;

pq_beginmessage(&buf, 'z');
pq_sendbyte(&buf, (uint8)algorithm);
pq_sendbyte(&buf, (uint8) algorithm);
pq_endmessage(&buf);
pq_flush();
}
Expand All @@ -228,39 +233,41 @@ SendCompressionACK(int algorithm)
* --------------------------------
*/
int
pq_configure(Port* port)
pq_configure(Port *port)
{
char* client_compression_algorithms = port->compression_algorithms;
char *client_compression_algorithms = port->compression_algorithms;

/*
* If client request compression, it sends list of supported compression algorithms separated by comma.
* If client request compression, it sends list of supported compression
* algorithms separated by comma.
*/
if (client_compression_algorithms && libpq_compression)
{
int compression_level = ZPQ_DEFAULT_COMPRESSION_LEVEL;
int impl = -1;
char** server_compression_algorithms = zpq_get_supported_algorithms();
int index = -1;
char* protocol_extension = strchr(client_compression_algorithms, ';');
int compression_level = ZPQ_DEFAULT_COMPRESSION_LEVEL;
int impl = -1;
char **server_compression_algorithms = zpq_get_supported_algorithms();
int index = -1;
char *protocol_extension = strchr(client_compression_algorithms, ';');

/* No protocol extension are currently supported */
if (protocol_extension)
*protocol_extension = '\0';

for (int i = 0; *client_compression_algorithms; i++)
{
char* sep = strchr(client_compression_algorithms, ',');
char* level;
char *sep = strchr(client_compression_algorithms, ',');
char *level;

if (sep != NULL)
*sep = '\0';

level = strchr(client_compression_algorithms, ':');
if (level != NULL)
{
*level = '\0'; /* compression level is ignored now */
if (sscanf(level+1, "%d", &compression_level) != 1)
*level = '\0'; /* compression level is ignored now */
if (sscanf(level + 1, "%d", &compression_level) != 1)
ereport(LOG,
(errmsg("Invalid compression level: %s", level+1)));
(errmsg("Invalid compression level: %s", level + 1)));
}
for (impl = 0; server_compression_algorithms[impl] != NULL; impl++)
{
Expand All @@ -272,17 +279,17 @@ pq_configure(Port* port)
}

if (sep != NULL)
client_compression_algorithms = sep+1;
client_compression_algorithms = sep + 1;
else
break;
}
SendCompressionAck:
SendCompressionAck:
free(server_compression_algorithms);
SendCompressionACK(index);

if (index >= 0) /* Use compression */
if (index >= 0) /* Use compression */
{
PqStream = zpq_create(impl, compression_level, write_compressed, read_compressed, MyProcPort, NULL, 0);
PqStream = zpq_create(impl, compression_level, impl, write_compressed, read_compressed, MyProcPort, NULL, 0);
if (!PqStream)
{
ereport(LOG,
Expand Down Expand Up @@ -1032,14 +1039,14 @@ socket_set_nonblocking(bool nonblocking)
/* --------------------------------
* pq_recvbuf - load some bytes into the input buffer
*
* nowait parameter toggles non-blocking mode.
* nowait parameter toggles non-blocking mode.
* returns number of read bytes, EOF if trouble
* --------------------------------
*/
static int
pq_recvbuf(bool nowait)
{
int r;
int r;

if (PqRecvPointer > 0)
{
Expand All @@ -1061,7 +1068,10 @@ pq_recvbuf(bool nowait)
/* Can fill buffer from PqRecvLength and upwards */
for (;;)
{
/* If streaming compression is enabled then use correspondent compression read function. */
/*
* If streaming compression is enabled then use correspondent
* compression read function.
*/
r = PqStream
? zpq_read(PqStream, PqRecvBuffer + PqRecvLength,
PQ_RECV_BUFFER_SIZE - PqRecvLength)
Expand All @@ -1072,7 +1082,8 @@ pq_recvbuf(bool nowait)
{
if (r == ZPQ_DECOMPRESS_ERROR)
{
char const* msg = zpq_error(PqStream);
char const *msg = zpq_decompress_error(PqStream);

if (msg == NULL)
msg = "end of stream";
ereport(COMMERROR,
Expand Down Expand Up @@ -1122,7 +1133,8 @@ pq_getbyte(void)

while (PqRecvPointer >= PqRecvLength)
{
if (pq_recvbuf(false) == EOF) /* If nothing in buffer, then recv some */
if (pq_recvbuf(false) == EOF) /* If nothing in buffer, then recv
* some */
return EOF; /* Failed to recv data */
}
return (unsigned char) PqRecvBuffer[PqRecvPointer++];
Expand All @@ -1141,7 +1153,8 @@ pq_peekbyte(void)

while (PqRecvPointer >= PqRecvLength)
{
if (pq_recvbuf(false) == EOF) /* If nothing in buffer, then recv some */
if (pq_recvbuf(false) == EOF) /* If nothing in buffer, then recv
* some */
return EOF; /* Failed to recv data */
}
return (unsigned char) PqRecvBuffer[PqRecvPointer];
Expand Down Expand Up @@ -1187,7 +1200,8 @@ pq_getbytes(char *s, size_t len)
{
while (PqRecvPointer >= PqRecvLength)
{
if (pq_recvbuf(false) == EOF) /* If nothing in buffer, then recv some */
if (pq_recvbuf(false) == EOF) /* If nothing in buffer, then recv
* some */
return EOF; /* Failed to recv data */
}
amount = PqRecvLength - PqRecvPointer;
Expand Down Expand Up @@ -1221,7 +1235,8 @@ pq_discardbytes(size_t len)
{
while (PqRecvPointer >= PqRecvLength)
{
if (pq_recvbuf(false) == EOF) /* If nothing in buffer, then recv some */
if (pq_recvbuf(false) == EOF) /* If nothing in buffer, then recv
* some */
return EOF; /* Failed to recv data */
}
amount = PqRecvLength - PqRecvPointer;
Expand Down Expand Up @@ -1262,7 +1277,8 @@ pq_getstring(StringInfo s)
{
while (PqRecvPointer >= PqRecvLength)
{
if (pq_recvbuf(false) == EOF) /* If nothing in buffer, then recv some */
if (pq_recvbuf(false) == EOF) /* If nothing in buffer, then recv
* some */
return EOF; /* Failed to recv data */
}

Expand Down Expand Up @@ -1513,11 +1529,16 @@ internal_flush(void)
char *bufend = PqSendBuffer + PqSendPointer;

while (bufptr < bufend || zpq_buffered_tx(PqStream) != 0)
/* has more data to flush or unsent data in internal compression buffer */

/*
* has more data to flush or unsent data in internal compression
* buffer
*/
{
int r;
size_t processed = 0;
size_t available = bufend - bufptr;
int r;
size_t processed = 0;
size_t available = bufend - bufptr;

r = PqStream
? zpq_write(PqStream, bufptr, available, &processed)
: secure_write(MyProcPort, bufptr, available);
Expand Down Expand Up @@ -2096,7 +2117,8 @@ PG_FUNCTION_INFO_V1(pg_compression_algorithm);
Datum
pg_compression_algorithm(PG_FUNCTION_ARGS)
{
char const* algorithm_name = PqStream ? zpq_algorithm_name(PqStream) : NULL;
char const *algorithm_name = PqStream ? zpq_compress_algorithm_name(PqStream) : NULL;

if (algorithm_name)
PG_RETURN_TEXT_P(cstring_to_text(algorithm_name));
else
Expand Down
Loading