-
Notifications
You must be signed in to change notification settings - Fork 710
Expand file tree
/
Copy pathsync_worker.cpp
More file actions
256 lines (208 loc) · 8.14 KB
/
sync_worker.cpp
File metadata and controls
256 lines (208 loc) · 8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "pg_deeplake.hpp"
#ifdef __cplusplus
extern "C" {
#endif
#include <postgres.h>
#include <access/xact.h>
#include <catalog/namespace.h>
#include <executor/spi.h>
#include <miscadmin.h>
#include <nodes/makefuncs.h>
#include <pgstat.h>
#include <postmaster/bgworker.h>
#include <storage/ipc.h>
#include <storage/latch.h>
#include <storage/proc.h>
#include <tcop/utility.h>
#include <utils/builtins.h>
#include <utils/guc.h>
#include <utils/snapmgr.h>
#ifdef __cplusplus
}
#endif
#include "sync_worker.hpp"
#include "dl_catalog.hpp"
#include "table_storage.hpp"
#include "utils.hpp"
#include <algorithm>
#include <vector>
// GUC variables
int deeplake_sync_interval_ms = 2000; // Default 2 seconds
namespace {
// Worker state - use sig_atomic_t for signal safety
volatile sig_atomic_t got_sigterm = false;
volatile sig_atomic_t got_sighup = false;
void deeplake_sync_worker_sigterm(SIGNAL_ARGS)
{
int save_errno = errno;
got_sigterm = true;
SetLatch(MyLatch);
errno = save_errno;
}
void deeplake_sync_worker_sighup(SIGNAL_ARGS)
{
int save_errno = errno;
got_sighup = true;
SetLatch(MyLatch);
errno = save_errno;
}
/**
* Sync tables from the deeplake catalog to PostgreSQL.
*
* This function checks the catalog for tables that exist in the deeplake
* catalog but not in PostgreSQL, and creates them.
*/
void deeplake_sync_tables_from_catalog(const std::string& root_path, icm::string_map<> creds)
{
// Load tables and columns in parallel for better performance
auto [catalog_tables, catalog_columns] = pg::dl_catalog::load_tables_and_columns(root_path, creds);
for (const auto& meta : catalog_tables) {
// Skip tables marked as dropping
if (meta.state == "dropping") {
continue;
}
const std::string qualified_name = meta.schema_name + "." + meta.table_name;
// Check if table exists in PostgreSQL
auto* rel = makeRangeVar(pstrdup(meta.schema_name.c_str()), pstrdup(meta.table_name.c_str()), -1);
Oid relid = RangeVarGetRelid(rel, NoLock, true);
if (!OidIsValid(relid)) {
// Gather columns for this table, sorted by position
std::vector<pg::dl_catalog::column_meta> table_columns;
for (const auto& col : catalog_columns) {
if (col.table_id == meta.table_id) {
table_columns.push_back(col);
}
}
std::sort(table_columns.begin(), table_columns.end(),
[](const auto& a, const auto& b) { return a.position < b.position; });
if (table_columns.empty()) {
elog(DEBUG1, "pg_deeplake sync: no columns found for table %s, skipping", qualified_name.c_str());
continue;
}
const char* qschema = quote_identifier(meta.schema_name.c_str());
const char* qtable = quote_identifier(meta.table_name.c_str());
// Build CREATE TABLE IF NOT EXISTS statement
StringInfoData buf;
initStringInfo(&buf);
appendStringInfo(&buf, "CREATE TABLE IF NOT EXISTS %s.%s (", qschema, qtable);
bool first = true;
for (const auto& col : table_columns) {
if (!first) {
appendStringInfoString(&buf, ", ");
}
first = false;
appendStringInfo(&buf, "%s %s", quote_identifier(col.column_name.c_str()), col.pg_type.c_str());
}
appendStringInfo(&buf, ") USING deeplake");
// Wrap in subtransaction so that if another backend concurrently
// creates the same table (race on composite type), the error is
// caught and we continue instead of aborting the sync cycle.
MemoryContext saved_context = CurrentMemoryContext;
ResourceOwner saved_owner = CurrentResourceOwner;
BeginInternalSubTransaction(NULL);
PG_TRY();
{
pg::utils::spi_connector connector;
// Create schema if needed
StringInfoData schema_buf;
initStringInfo(&schema_buf);
appendStringInfo(&schema_buf, "CREATE SCHEMA IF NOT EXISTS %s", qschema);
SPI_execute(schema_buf.data, false, 0);
pfree(schema_buf.data);
if (SPI_execute(buf.data, false, 0) == SPI_OK_UTILITY) {
elog(LOG, "pg_deeplake sync: successfully created table %s", qualified_name.c_str());
}
ReleaseCurrentSubTransaction();
}
PG_CATCH();
{
// Another backend created this table concurrently — not an error.
MemoryContextSwitchTo(saved_context);
CurrentResourceOwner = saved_owner;
RollbackAndReleaseCurrentSubTransaction();
FlushErrorState();
elog(DEBUG1, "pg_deeplake sync: concurrent creation of %s, skipping", qualified_name.c_str());
}
PG_END_TRY();
pfree(buf.data);
}
}
}
} // anonymous namespace
extern "C" {
PGDLLEXPORT void deeplake_sync_worker_main(Datum main_arg)
{
// Set up signal handlers
pqsignal(SIGTERM, deeplake_sync_worker_sigterm);
pqsignal(SIGHUP, deeplake_sync_worker_sighup);
// Unblock signals
BackgroundWorkerUnblockSignals();
// Connect to the default database
BackgroundWorkerInitializeConnection("postgres", NULL, 0);
elog(LOG, "pg_deeplake sync worker started");
int64_t last_catalog_version = 0;
std::string last_root_path; // Track root_path to detect changes
while (!got_sigterm) {
// Handle SIGHUP - reload configuration
if (got_sighup) {
got_sighup = false;
ProcessConfigFile(PGC_SIGHUP);
}
// Skip if stateless mode is disabled
if (!pg::stateless_enabled) {
goto wait_for_latch;
}
// Start a transaction for our work
SetCurrentStatementStartTimestamp();
StartTransactionCommand();
PushActiveSnapshot(GetTransactionSnapshot());
PG_TRY();
{
// Initialize DeepLake (loads table metadata, etc.)
pg::init_deeplake();
auto root_path = pg::session_credentials::get_root_path();
if (root_path.empty()) {
root_path = pg::utils::get_deeplake_root_directory();
}
if (!root_path.empty()) {
auto creds = pg::session_credentials::get_credentials();
// When root_path changes after initial setup, force a full reload
if (root_path != last_root_path) {
if (!last_root_path.empty()) {
pg::table_storage::instance().reset_and_load_table_metadata();
last_catalog_version = 0;
}
last_root_path = root_path;
}
// Use existing catalog version API to check for changes (now fast with cache)
int64_t current_version = pg::dl_catalog::get_catalog_version(root_path, creds);
if (current_version != last_catalog_version) {
// Version changed - sync tables from catalog
deeplake_sync_tables_from_catalog(root_path, creds);
last_catalog_version = current_version;
elog(LOG, "pg_deeplake sync: synced tables (catalog version %ld)", current_version);
}
}
}
PG_CATCH();
{
// Log error but don't crash - continue polling
EmitErrorReport();
FlushErrorState();
}
PG_END_TRY();
PopActiveSnapshot();
CommitTransactionCommand();
pgstat_report_stat(true);
wait_for_latch:
// Wait for latch or timeout
(void)WaitLatch(MyLatch,
WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
deeplake_sync_interval_ms,
PG_WAIT_EXTENSION);
ResetLatch(MyLatch);
}
elog(LOG, "pg_deeplake sync worker shutting down");
proc_exit(0);
}
} // extern "C"