my-ship-it commented on code in PR #1629:
URL: https://github.com/apache/cloudberry/pull/1629#discussion_r3014434981
##########
src/backend/commands/portalcmds.c:
##########
@@ -373,6 +374,10 @@ PortalCleanup(Portal portal)
FreeQueryDesc(queryDesc);
CurrentResourceOwner = saveResourceOwner;
+ } else {
Review Comment:
1) Minor style nit: } else { does not follow CBDB's brace convention.
2) As comments says
```
Note: if portal->status is PORTAL_FAILED, we are probably being called
during error abort, and must be careful to avoid doing anything that
is likely to fail again.
```
The backend may be in an unstable state during exception recovery. Calling
extension code that does socket I/O and protobuf serialization here risks
nested exceptions or hangs.
I'd suggest wrapping the hook call in its own PG_TRY/PG_CATCH block to
isolate failures:
```
else
{
/* GPDB hook for collecting query info */
if (queryDesc->gpsc_query_key && query_info_collect_hook)
{
PG_TRY();
{
(*query_info_collect_hook)(METRICS_QUERY_ERROR,
queryDesc);
}
PG_CATCH();
{
FlushErrorState();
}
PG_END_TRY();
}
}
```
This way, if the hook fails, the error is silently discarded and normal
cleanup proceeds.
##########
gpcontrib/gp_stats_collector/gp_stats_collector--1.0--1.1.sql:
##########
@@ -0,0 +1,113 @@
+/* gp_stats_collector--1.0--1.1.sql */
+
+-- complain if script is sourced in psql, rather than via ALTER EXTENSION
+\echo Use "ALTER EXTENSION gp_stats_collector UPDATE TO '1.1'" to load this
file. \quit
+
+CREATE SCHEMA gpsc;
+
+-- Unlink existing objects from extension.
+ALTER EXTENSION gp_stats_collector DROP VIEW gpsc_stat_messages;
+ALTER EXTENSION gp_stats_collector DROP FUNCTION gpsc_stat_messages_reset();
+ALTER EXTENSION gp_stats_collector DROP FUNCTION
__gpsc_stat_messages_f_on_segments();
+ALTER EXTENSION gp_stats_collector DROP FUNCTION
__gpsc_stat_messages_f_on_master();
+ALTER EXTENSION gp_stats_collector DROP FUNCTION
__gpsc_stat_messages_reset_f_on_segments();
+ALTER EXTENSION gp_stats_collector DROP FUNCTION
__gpsc_stat_messages_reset_f_on_master();
+
+-- Now drop the objects.
+DROP VIEW gpsc_stat_messages;
+DROP FUNCTION gpsc_stat_messages_reset();
+DROP FUNCTION __gpsc_stat_messages_f_on_segments();
+DROP FUNCTION __gpsc_stat_messages_f_on_master();
+DROP FUNCTION __gpsc_stat_messages_reset_f_on_segments();
+DROP FUNCTION __gpsc_stat_messages_reset_f_on_master();
+
+-- Recreate functions and view in new schema.
+CREATE FUNCTION gpsc.__stat_messages_reset_f_on_master()
+RETURNS SETOF void
+AS 'MODULE_PATHNAME', 'gpsc_stat_messages_reset'
+LANGUAGE C EXECUTE ON MASTER;
Review Comment:
All SQL definition files (gp_stats_collector--1.0.sql, --1.1.sql,
--1.0--1.1.sql) use EXECUTE ON MASTER. Cloudberry has moved to EXECUTE ON
COORDINATOR. While the old syntax still works, better for new extensions to use
the current terminology.
##########
gpcontrib/gp_stats_collector/src/UDSConnector.cpp:
##########
@@ -0,0 +1,146 @@
+/*-------------------------------------------------------------------------
+ *
+ * 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.
+ *
+ * UDSConnector.cpp
+ *
+ * IDENTIFICATION
+ * gpcontrib/gp_stats_collector/src/UDSConnector.cpp
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "UDSConnector.h"
+#include "Config.h"
+#include "GpscStat.h"
+#include "log/LogOps.h"
+#include "memory/gpdbwrappers.h"
+
+#include <chrono>
+#include <string>
+#include <sys/fcntl.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <sys/un.h>
+#include <thread>
+#include <unistd.h>
+
+extern "C" {
+#include "postgres.h"
+}
+
+static void inline log_tracing_failure(const gpsc::SetQueryReq &req,
+
const std::string &event)
+{
+ ereport(LOG, (errmsg("Query {%d-%d-%d} %s tracing failed with error %m",
+ req.query_key().tmid(),
req.query_key().ssid(),
+ req.query_key().ccnt(),
event.c_str())));
+}
+
+bool
+UDSConnector::report_query(const gpsc::SetQueryReq &req,
+ const std::string &event,
const Config &config)
+{
+ sockaddr_un address{};
+ address.sun_family = AF_UNIX;
+ const auto &uds_path = config.uds_path();
+
+ if (uds_path.size() >= sizeof(address.sun_path))
+ {
+ ereport(WARNING, (errmsg("UDS path is too long for socket
buffer")));
+ GpscStat::report_error();
+ return false;
+ }
+ strcpy(address.sun_path, uds_path.c_str());
+
+ const auto sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (sockfd == -1)
+ {
+ log_tracing_failure(req, event);
+ GpscStat::report_error();
+ return false;
+ }
+
+ // Close socket automatically on error path.
+ struct SockGuard
+ {
+ int fd;
+ ~SockGuard()
+ {
+ close(fd);
+ }
+ } sock_guard{sockfd};
+
+ if (fcntl(sockfd, F_SETFL, O_NONBLOCK) == -1)
+ {
+ // That's a very important error that should never happen, so
make it
+ // visible to an end-user and admins.
+ ereport(WARNING,
+ (errmsg("Unable to create non-blocking socket
connection %m")));
+ GpscStat::report_error();
+ return false;
+ }
+
+ if (connect(sockfd, reinterpret_cast<sockaddr *>(&address),
+ sizeof(address)) == -1)
+ {
+ log_tracing_failure(req, event);
+ GpscStat::report_bad_connection();
+ return false;
+ }
+
+ const auto data_size = req.ByteSizeLong();
+ const auto total_size = data_size + sizeof(uint32_t);
+ auto *buf = static_cast<uint8_t *>(gpdb::palloc(total_size));
+ // Free buf automatically on error path.
+ struct BufGuard
+ {
+ void *p;
+ ~BufGuard()
+ {
+ gpdb::pfree(p);
+ }
+ } buf_guard{buf};
+
+ *reinterpret_cast<uint32_t *>(buf) = data_size;
+ req.SerializeWithCachedSizesToArray(buf + sizeof(uint32_t));
+
+ int64_t sent = 0, sent_total = 0;
+ do
+ {
+ sent = send(sockfd, buf + sent_total, total_size - sent_total,
+ MSG_DONTWAIT);
+ if (sent > 0)
+ sent_total += sent;
+ } while (sent > 0 && size_t(sent_total) != total_size &&
+ // the line below is a small throttling hack:
+ // if a message does not fit a single packet, we take
a nap
+ // before sending the next one.
+ // Otherwise, MSG_DONTWAIT send might overflow the UDS
+
(std::this_thread::sleep_for(std::chrono::milliseconds(1)), true));
Review Comment:
std::this_thread::sleep_for does not interact with PostgreSQL's signal
handling infrastructure. During a backend shutdown or query cancel, this sleep
will not be interrupted by CHECK_FOR_INTERRUPTS(). How about use pg_usleep() or
WaitLatch() instead?
##########
gpcontrib/gp_stats_collector/src/hook_wrappers.cpp:
##########
@@ -0,0 +1,473 @@
+/*-------------------------------------------------------------------------
+ *
+ * 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.
+ *
+ * hook_wrappers.cpp
+ *
+ * IDENTIFICATION
+ * gpcontrib/gp_stats_collector/src/hook_wrappers.cpp
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#define typeid __typeid
+extern "C" {
+#include "postgres.h"
+#include "cdb/cdbvars.h"
+#include "cdb/ml_ipc.h"
+#include "executor/execUtils.h"
+#include "executor/executor.h"
+#include "funcapi.h"
+#include "stat_statements_parser/pg_stat_statements_parser.h"
+#include "tcop/utility.h"
+#include "utils/builtins.h"
+#include "utils/elog.h"
+#include "utils/metrics_utils.h"
+
+#include <errno.h>
+#include <poll.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <unistd.h>
+}
+#undef typeid
+
+#include "Config.h"
+#include "EventSender.h"
+#include "GpscStat.h"
+#include "hook_wrappers.h"
+#include "memory/gpdbwrappers.h"
+
+static ExecutorStart_hook_type previous_ExecutorStart_hook = nullptr;
+static ExecutorRun_hook_type previous_ExecutorRun_hook = nullptr;
+static ExecutorFinish_hook_type previous_ExecutorFinish_hook = nullptr;
+static ExecutorEnd_hook_type previous_ExecutorEnd_hook = nullptr;
+static query_info_collect_hook_type previous_query_info_collect_hook = nullptr;
+#ifdef ANALYZE_STATS_COLLECT_HOOK
+static analyze_stats_collect_hook_type previous_analyze_stats_collect_hook =
+ nullptr;
+#endif
+#ifdef IC_TEARDOWN_HOOK
+static ic_teardown_hook_type previous_ic_teardown_hook = nullptr;
+#endif
+static ProcessUtility_hook_type previous_ProcessUtility_hook = nullptr;
+
+static void gpsc_ExecutorStart_hook(QueryDesc *query_desc, int eflags);
+static void gpsc_ExecutorRun_hook(QueryDesc *query_desc,
+ ScanDirection
direction, uint64 count,
+ bool
execute_once);
+static void gpsc_ExecutorFinish_hook(QueryDesc *query_desc);
+static void gpsc_ExecutorEnd_hook(QueryDesc *query_desc);
+static void gpsc_query_info_collect_hook(QueryMetricsStatus status, void *arg);
+#ifdef IC_TEARDOWN_HOOK
+static void gpsc_ic_teardown_hook(ChunkTransportState *transportStates,
+ bool
hasErrors);
+#endif
+#ifdef ANALYZE_STATS_COLLECT_HOOK
+static void gpsc_analyze_stats_collect_hook(QueryDesc *query_desc);
+#endif
+static void gpsc_process_utility_hook(
+ PlannedStmt *pstmt, const char *queryString, bool readOnlyTree,
+ ProcessUtilityContext context, ParamListInfo params,
+ QueryEnvironment *queryEnv, DestReceiver *dest, QueryCompletion *qc);
+
+#define TEST_MAX_CONNECTIONS 4
+#define TEST_RCV_BUF_SIZE 8192
+#define TEST_POLL_TIMEOUT_MS 200
+
+static int test_server_fd = -1;
+static char *test_sock_path = NULL;
+
+static EventSender *sender = nullptr;
+
+static inline EventSender *
+get_sender()
+{
+ if (!sender)
+ {
+ sender = new EventSender();
+ }
+ return sender;
+}
+
+template <typename T, typename R, typename... Args>
+R
+cpp_call(T *obj, R (T::*func)(Args...), Args... args)
+{
+ try
+ {
+ return (obj->*func)(args...);
+ }
+ catch (const std::exception &e)
+ {
+ ereport(ERROR, (errmsg("Unexpected exception in gpsc %s",
e.what())));
+ }
Review Comment:
The catch block calls ereport(ERROR, ...) which does not return, but the
compiler doesn't know that.
When R is non-void, this is undefined behavior (missing return after catch).
Add pg_unreachable() or __builtin_unreachable() after the ereport?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]