Skip to content
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

Add LWLock for OSM usage in loader #6608

Merged
merged 1 commit into from
Feb 7, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .unreleased/pr_6608
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Implements: #6608 Add LWLock for OSM usage in loader
2 changes: 1 addition & 1 deletion src/bgw/launcher_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "launcher_interface.h"
#include "compat/compat.h"

#define MIN_LOADER_API_VERSION 3
#define MIN_LOADER_API_VERSION 4

extern bool
ts_bgw_worker_reserve(void)
Expand Down
9 changes: 8 additions & 1 deletion src/loader/bgw_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@

/* This is where versioned-extension facing functions live. They shouldn't live anywhere else. */

const int32 ts_bgw_loader_api_version = 3;
/* All loader changes should always be backward compatible.
* Update ts_bgw_loader_api_version if the loader changes are needed for newer extension updates.
* e.g. adding a LWLock to loader is required for some future change coming to OSM extension version
* xxxx. RENDEZVOUS_BGW_LOADER_API_VERSION is used to verify if the loader in use is compatible with
* the current TimescaleDB version. This check happens in bgw/bgw_launcher.c When
* ts_bgw_loader_api_version is updated, check the compatibility in bgw/bgw_launcher.c as well
*/
const int32 ts_bgw_loader_api_version = 4;

TS_FUNCTION_INFO_V1(ts_bgw_worker_reserve);
TS_FUNCTION_INFO_V1(ts_bgw_worker_release);
Expand Down
16 changes: 15 additions & 1 deletion src/loader/lwlocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#define TS_LWLOCKS_SHMEM_NAME "ts_lwlocks_shmem"
#define CHUNK_APPEND_LWLOCK_TRANCHE_NAME "ts_chunk_append_lwlock_tranche"
#define OSM_PARALLEL_LWLOCK_TRANCHE_NAME "ts_osm_parallel_lwlock_tranche"

/*
* since shared memory can only be setup in a library loaded as
Expand All @@ -22,6 +23,7 @@
typedef struct TSLWLocks
{
LWLock *chunk_append;
LWLock *osm_parallel_lwlock;
} TSLWLocks;

static TSLWLocks *ts_lwlocks = NULL;
Expand All @@ -30,14 +32,16 @@ void
ts_lwlocks_shmem_startup()
{
bool found;
LWLock **lock_pointer;
LWLock **lock_pointer, **osm_lock_pointer;

LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
ts_lwlocks = ShmemInitStruct(TS_LWLOCKS_SHMEM_NAME, sizeof(TSLWLocks), &found);
if (!found)
{
memset(ts_lwlocks, 0, sizeof(TSLWLocks));
ts_lwlocks->chunk_append = &(GetNamedLWLockTranche(CHUNK_APPEND_LWLOCK_TRANCHE_NAME))->lock;
ts_lwlocks->osm_parallel_lwlock =
&(GetNamedLWLockTranche(OSM_PARALLEL_LWLOCK_TRANCHE_NAME))->lock;
}
LWLockRelease(AddinShmemInitLock);

Expand All @@ -48,11 +52,21 @@ ts_lwlocks_shmem_startup()
*/
lock_pointer = (LWLock **) find_rendezvous_variable(RENDEZVOUS_CHUNK_APPEND_LWLOCK);
*lock_pointer = ts_lwlocks->chunk_append;
osm_lock_pointer = (LWLock **) find_rendezvous_variable(RENDEZVOUS_OSM_PARALLEL_LWLOCK);
*osm_lock_pointer = ts_lwlocks->osm_parallel_lwlock;
}

/*
* from postgres code comments:
* Extensions (or core code) can obtain an LWLocks by calling
* RequestNamedLWLockTranche() during postmaster startup. Subsequently,
* call GetNamedLWLockTranche() to obtain a pointer to an array containing
* the number of LWLocks requested.
*/
void
ts_lwlocks_shmem_alloc()
{
RequestNamedLWLockTranche(CHUNK_APPEND_LWLOCK_TRANCHE_NAME, 1);
RequestNamedLWLockTranche(OSM_PARALLEL_LWLOCK_TRANCHE_NAME, 1);
RequestAddinShmemSpace(sizeof(TSLWLocks));
}
1 change: 1 addition & 0 deletions src/loader/lwlocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#pragma once

#define RENDEZVOUS_CHUNK_APPEND_LWLOCK "ts_chunk_append_lwlock"
#define RENDEZVOUS_OSM_PARALLEL_LWLOCK "ts_osm_parallel_lwlock"

void ts_lwlocks_shmem_startup(void);
void ts_lwlocks_shmem_alloc(void);
1 change: 1 addition & 0 deletions test/expected/loader-tsl.out
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ CREATE EXTENSION timescaledb_osm VERSION 'mock-1';
WARNING: mock post_analyze_hook "mock-2"
WARNING: mock post_analyze_hook "mock-2"
WARNING: OSM-mock-1 _PG_init
WARNING: got lwlock osm lock
WARNING: mock post_analyze_hook "mock-2"
WARNING: mock post_analyze_hook "mock-2"
-- Test that OSM process utility hook works: it should see this DROP TABLE.
Expand Down
11 changes: 11 additions & 0 deletions test/src/loader/osm_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "compat/compat.h"
#include "export.h"
#include "loader/lwlocks.h"

#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
Expand All @@ -33,6 +34,16 @@ void
_PG_init(void)
{
elog(WARNING, "OSM-%s _PG_init", OSM_VERSION_MOD);
void *osm_lock_pointer = (LWLock **) find_rendezvous_variable(RENDEZVOUS_OSM_PARALLEL_LWLOCK);
if (osm_lock_pointer != NULL)
{
elog(WARNING, "got lwlock osm lock");
}
else
{
elog(WARNING, "NO lwlock osm lock");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the current unit tests will never reach this branch... is this correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's correct.

}

prev_ProcessUtility_hook = ProcessUtility_hook;
ProcessUtility_hook = osm_process_utility_hook;
}
Expand Down