Skip to content

Commit

Permalink
Add a signal handler to print crashes, plus extra logging (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
elefeint authored Jan 3, 2025
1 parent 541b9e9 commit 8a6a892
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/motherduck_destination.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "duckdb.hpp"
#include "motherduck_destination_server.hpp"
#include <csignal>
#include <execinfo.h>
#include <grpcpp/grpcpp.h>
#include <string>

Expand Down Expand Up @@ -39,7 +41,15 @@ void download_motherduck_extension() {
}
}

void logCrash(int sig) {
std::cerr << "Crash signal " << sig << std::endl;
std::exit(sig);
}

int main(int argc, char **argv) {
std::signal(SIGSEGV, logCrash);
std::signal(SIGABRT, logCrash);

std::string port = "50052";
for (auto i = 1; i < argc; i++) {
if (strcmp(argv[i], "--port") == 0) {
Expand Down
6 changes: 6 additions & 0 deletions src/motherduck_destination_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,16 @@ grpc::Status DestinationSdkImpl::DescribeTable(
find_property(request->configuration(), MD_PROP_DATABASE);
std::unique_ptr<duckdb::Connection> con =
get_connection(request->configuration(), db_name);
mdlog::info("Endpoint <DescribeTable>: got connection");
table_def table_name{db_name, get_schema_name(request),
get_table_name(request)};

mdlog::info("Endpoint <DescribeTable>: schema name <" +
table_name.schema_name + ">");
mdlog::info("Endpoint <DescribeTable>: table name <" +
table_name.table_name + ">");
if (!table_exists(*con, table_name)) {
mdlog::info("Endpoint <DescribeTable>: table not found");
response->set_not_found(true);
return ::grpc::Status(::grpc::StatusCode::OK, "");
}
Expand Down
4 changes: 4 additions & 0 deletions src/sql_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,23 @@ bool table_exists(duckdb::Connection &con, const table_def &table) {
const std::string err =
"Could not find whether table <" + table.to_escaped_string() + "> exists";
auto statement = con.Prepare(query);
mdlog::info(" prepared table_exists query for table " + table.table_name);
if (statement->HasError()) {
throw std::runtime_error(err + " (at bind step): " + statement->GetError());
}
duckdb::vector<duckdb::Value> params = {duckdb::Value(table.db_name),
duckdb::Value(table.schema_name),
duckdb::Value(table.table_name)};
auto result = statement->Execute(params, false);
mdlog::info(" executed table_exists query for table " + table.table_name);

if (result->HasError()) {
throw std::runtime_error(err + ": " + result->GetError());
}
auto materialized_result = duckdb::unique_ptr_cast<
duckdb::QueryResult, duckdb::MaterializedQueryResult>(std::move(result));
mdlog::info(" materialized table_exists results for table " +
table.table_name);
return materialized_result->RowCount() > 0;
}

Expand Down

0 comments on commit 8a6a892

Please sign in to comment.