Files
Lucas Borges Fernandes 1cf6873426 Merged PR 1661393: Add OSS background_worker initial jobs framework
### Does this PR have any customer impact?
No.

### Type (Feature, Refactoring, Bugfix, DevOps, Testing, Perf, etc)
Feature.

### Does it involve schema level changes? (Table, Column, Index, UDF, etc level changes)
No.

### Are you introducing any new config? If yes, do you have tests with and without them being set?
Yes.

### ChangeLog (Refer [Template](../oss/CHANGELOG.md))

### Description

Add OSS background_worker initial jobs framework. For this initial version, only schedulable jobs are allowed (i.e. jobs that run on timed intervals). The background worker loop has a latch timeout with a predefined duration of 10 seconds that is configured through a GUC. Every time this timeout occurs, it iterates on the JobRegistry array.

1. Checks if the job is running and if it timed out (i.e. running longer than timeoutInSeconds). If yes, cancels the query and finishes libpq connection.
2. Checks if the job finished running (i.e. libpq connection isn't busy). If yes, finishes the libpq connection.
3. Runs the job if its scheduled interval has reached (i.e. currentTime - lastStartTime >= scheduledInterval). Each job execution has its own libpq connection to the database.

In order to add a new job callers must pass a BackgroundWorkerJob structure to RegisterBackgroundWorkerJob.

----
#### AI description  (iteration 1)
#### PR Classification
This pull request introduces a new feature by adding an OSS background worker jobs framework.

#### PR Summary
The changes implement a framework for scheduling, executing, and managing background jobs within the background worker process. This provides structured job processing with timeout, cancellation, and configuration support.

- **`src/background_worker/background_worker.c`**: Added job management functions (e.g., ManageJobs, ExecuteJob, CanExecuteJob, CheckJobCompletion, CancelJobIfTimeIsUp, ValidateJob) and integrated job scheduling in the main loop.
- **`include/background_worker/background_worker_job.h`**: Introduced new structures for defining background worker jobs and their execution objects.
- **`src/api_hook_impl.c`**: Added a hook function to retrieve background worker jobs for registration.
- **`src/configs/background_job_configs.c`**: Defined new configuration variables (GUCs) to enable background worker jobs.
- **API Hooks and Connection Management Files**: Updated declarations and functions (e.g., in `include/api_hooks_def.h` and connection management files) to support connection handling necessary for job execution.
<!-- GitOpsUserAgent=GitOps.Apps.Server.pullrequestcopilot -->

Related work items: #4225917
2025-07-09 18:56:11 +08:00

96 lines
2.8 KiB
Makefile
Executable File

EXTENSION = documentdb_distributed
# compilation configuration
MODULE_big = pg_$(EXTENSION)
SQL_DEPDIR=.deps/sql
SQL_BUILDDIR=build/sql
template_sql_files = $(wildcard sql/*.sql)
generated_sql_files = $(patsubst %,build/%,$(template_sql_files))
DATA_built = $(generated_sql_files)
sql_file_inputs = $(wildcard sql/**/*.sql)
BUILD_SCRIPT_DIR = ../../
OSS_SRC_DIR = ../../
OSS_COMMON_SQL_HEADER = $(wildcard $(OSS_SRC_DIR)/common_header.sql)
# Extension defines
POSTGIS_SCHEMA_NAME=public
API_SCHEMA_NAME=documentdb_api
API_SCHEMA_NAME_V2=documentdb_api
API_SCHEMA_INTERNAL_NAME=documentdb_api_internal
API_SCHEMA_INTERNAL_NAME_V2=documentdb_api_internal
API_CATALOG_SCHEMA_NAME=documentdb_api_catalog
API_CATALOG_SCHEMA_NAME_V2=documentdb_api_catalog
CORE_SCHEMA_NAME=documentdb_core
CORE_SCHEMA_NAME_V2=documentdb_core
API_DISTRIBUTED_SCHEMA_NAME=documentdb_api_distributed
API_DATA_SCHEMA_NAME=documentdb_data
API_ADMIN_ROLE=documentdb_admin_role
API_READONLY_ROLE=documentdb_readonly_role
API_BG_WORKER_ROLE=documentdb_bg_worker_role
EXTENSION_OBJECT_PREFIX=documentdb
API_GUC_PREFIX=documentdb
API_GUC_PREFIX_V2=documentdb
# TODO: Need to remove this with PG16 and expose only valid exports
ALLOW_DEFAULT_VISIBILITY=yes
include $(OSS_SRC_DIR)/Makefile.cflags
PG_CPPFLAGS += -I$(OSS_SRC_DIR)/pg_documentdb_core/include/ -I$(OSS_SRC_DIR)/pg_documentdb/include/
SOURCES = $(wildcard src/*.c) $(wildcard src/**/*.c)
OBJS = $(patsubst %.c,%.o,$(SOURCES))
DEBUG ?= no
ifeq ($(DEBUG),yes)
PG_CPPFLAGS += -ggdb -O0 -g
PG_CFLAGS += -ggdb -O0 -g
endif
SHLIB_LINK = $(libpq)
include $(OSS_SRC_DIR)/Makefile.global
clean-sql:
rm -rf .deps/ build/
check:
$(MAKE) -C src/test all
check-dist-minimal:
$(MAKE) -C src/test $@
citus-indent:
citus_indent
analysis: citus-indent
check-regress:
$(MAKE) -C src/test check-regress
# Before installing, trim any files extension*.sql files in the target install directory
install: trim_installed_data_files
trim_installed_data_files:
rm -f $(DESTDIR)$(datadir)/$(datamoduledir)/$(EXTENSION)--*.sql
build-sql: $(generated_sql_files)
$(generated_sql_files): build/%: %
@mkdir -p $(SQL_DEPDIR) $(SQL_BUILDDIR)
@# -MF is used to store dependency files(.Po) in another directory for separation
@# -MT is used to change the target of the rule emitted by dependency generation.
@# -P is used to inhibit generation of linemarkers in the output from the preprocessor.
@# -undef is used to not predefine any system-specific or GCC-specific macros.
@# -imacros is used to specify a file that defines macros for the global context but its output is thrown away.
@# `man cpp` for further information
cpp -undef -w $(SQL_DEFINES) -imacros $(OSS_COMMON_SQL_HEADER) -P -MMD -MP -MF$(SQL_DEPDIR)/$(*F).Po -MT$@ $< > $@
include $(OSS_SRC_DIR)/Makefile.versions
src/documentdb_extension_version.o : build/include/metadata/documentdb_extension_version.h