Skip to content

Commit

Permalink
Test and other fixes for the smgr branch
Browse files Browse the repository at this point in the history
With this commit, old pg_tde code should work as before in the
same branch, and CI tests also should pass.
  • Loading branch information
dutow committed Jun 16, 2024
1 parent 8935125 commit 9b9e105
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 16 deletions.
6 changes: 5 additions & 1 deletion src/access/pg_tde_xlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ pg_tde_rmgr_identify(uint8 info)
return NULL;
}

#ifdef PERCONA_FORK

/*
* XLog Storage Manager
* TODO:
Expand Down Expand Up @@ -398,4 +400,6 @@ SetXLogPageIVPrefix(TimeLineID tli, XLogRecPtr lsn, char* iv_prefix)
iv_prefix[9] = ((lsn >> 16) & 0xFF);
iv_prefix[10] = ((lsn >> 8) & 0xFF);
iv_prefix[11] = (lsn & 0xFF);
}
}

#endif
4 changes: 2 additions & 2 deletions src/access/pg_tdeam.c
Original file line number Diff line number Diff line change
Expand Up @@ -1111,10 +1111,10 @@ pg_tde_getnext(TableScanDesc sscan, ScanDirection direction)
* rather than the AM oid, is that this allows to write regression tests
* that create another AM reusing the heap handler.
*/
if (unlikely(sscan->rs_rd->rd_tableam != GetHeapamTableAmRoutine()))
if (unlikely(sscan->rs_rd->rd_tableam != GetPGTdeamTableAmRoutine()))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg_internal("only heap AM is supported")));
errmsg_internal("only pg_tde AM is supported")));

/*
* We don't expect direct calls to pg_tde_getnext with valid CheckXidAlive
Expand Down
6 changes: 6 additions & 0 deletions src/access/pg_tdeam_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -2623,6 +2623,12 @@ static const TableAmRoutine pg_tdeam_methods = {
.scan_sample_next_tuple = pg_tdeam_scan_sample_next_tuple
};

const TableAmRoutine *
GetPGTdeamTableAmRoutine(void)
{
return &pg_tdeam_methods;
}

Datum
pg_tdeam_handler(PG_FUNCTION_ARGS)
{
Expand Down
26 changes: 25 additions & 1 deletion src/catalog/tde_master_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,26 @@ GetMasterKey(Oid dbOid)
LWLock *lock_files = tde_lwlock_mk_files();
LWLock *lock_cache = tde_lwlock_mk_cache();

// TODO: This recursion counter is a dirty hack until the metadata is in the catalog
// As otherwise we would call GetMasterKey recursively and deadlock
static int recursion = 0;

if(recursion > 0)
{
return NULL;
}

recursion++;

LWLockAcquire(lock_cache, LW_SHARED);
masterKey = get_master_key_from_cache(dbOid);
LWLockRelease(lock_cache);

if (masterKey)
{
recursion--;
return masterKey;
}

/*
* We should hold an exclusive lock here to ensure that a valid master key, if found, is added
Expand All @@ -244,6 +258,7 @@ GetMasterKey(Oid dbOid)
{
LWLockRelease(lock_cache);
LWLockRelease(lock_files);
recursion--;
return masterKey;
}

Expand All @@ -254,6 +269,7 @@ GetMasterKey(Oid dbOid)
LWLockRelease(lock_cache);
LWLockRelease(lock_files);

recursion--;
return NULL;
}

Expand All @@ -264,6 +280,7 @@ GetMasterKey(Oid dbOid)
LWLockRelease(lock_cache);
LWLockRelease(lock_files);

recursion--;
return NULL;
}

Expand All @@ -273,6 +290,7 @@ GetMasterKey(Oid dbOid)
LWLockRelease(lock_cache);
LWLockRelease(lock_files);

recursion--;
return NULL;
}

Expand All @@ -292,6 +310,7 @@ GetMasterKey(Oid dbOid)
if (masterKeyInfo)
pfree(masterKeyInfo);

recursion--;
return masterKey;
}

Expand Down Expand Up @@ -731,7 +750,12 @@ Datum pg_tde_master_key_info(PG_FUNCTION_ARGS)

master_key = GetMasterKey(MyDatabaseId);
if (master_key == NULL)
PG_RETURN_NULL();
{
ereport(ERROR,
(errmsg("Master key does not exists for the database"),
errhint("Use set_master_key interface to set the master key")));
PG_RETURN_NULL();
}

keyring = GetKeyProviderByID(master_key->keyInfo.keyringId);

Expand Down
6 changes: 6 additions & 0 deletions src/include/access/pg_tde_xlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
#include "postgres.h"
#include "access/xlog.h"
#include "access/xlog_internal.h"
#ifdef PERCONA_FORK
#include "access/xlog_smgr.h"
#endif

/* TDE XLOG resource manager */
#define XLOG_TDE_ADD_RELATION_KEY 0x00
Expand All @@ -35,6 +37,8 @@ static const RmgrData pg_tde_rmgr = {
.rm_identify = pg_tde_rmgr_identify
};

#ifdef PERCONA_FORK

/* XLog encryption staff */

/* GUC */
Expand All @@ -58,4 +62,6 @@ extern void TDEInitXLogSmgr(void);

extern void xlogInitGUC(void);

#endif

#endif /* PG_TDE_XLOG_H */
3 changes: 3 additions & 0 deletions src/include/access/pg_tdeam.h
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,7 @@ extern void HeapCheckForSerializableConflictOut(bool visible, Relation relation,
/* Defined in pg_tdeam_handler.c */
extern bool is_pg_tde_rel(Relation rel);

const TableAmRoutine *
GetPGTdeamTableAmRoutine(void);

#endif /* PG_TDEAM_H */
2 changes: 1 addition & 1 deletion src/include/smgr/pg_tde_smgr.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

#pragma once

extern void RegisterStorageMgr();
extern void RegisterStorageMgr();
6 changes: 6 additions & 0 deletions src/pg_tde.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ tde_shmem_request(void)
Size sz = TdeRequiredSharedMemorySize();
int required_locks = TdeRequiredLocksCount();

#ifdef PERCONA_FORK
sz = add_size(sz, XLOG_TDE_ENC_BUFF_ALIGNED_SIZE);
#endif

if (prev_shmem_request_hook)
prev_shmem_request_hook();
Expand All @@ -78,8 +80,10 @@ tde_shmem_startup(void)

TdeShmemInit();
AesInit();
#ifdef PERCONA_FORK
TDEXLogShmemInit();
TDEInitXLogSmgr();
#endif
}

void
Expand All @@ -92,7 +96,9 @@ _PG_init(void)

keyringRegisterVariables();
InitializeMasterKeyInfo();
#ifdef PERCONA_FORK
xlogInitGUC();
#endif

prev_shmem_request_hook = shmem_request_hook;
shmem_request_hook = tde_shmem_request;
Expand Down
10 changes: 5 additions & 5 deletions src/pg_tde_event_capture.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ GetCurrentTdeCreateEvent(void)
Datum
pg_tde_ddl_command_start_capture(PG_FUNCTION_ARGS)
{
/* TODO: verify update_compare_indexes failure related to this */
#ifdef PERCONA_FORK
EventTriggerData *trigdata;
Node *parsetree;

Expand Down Expand Up @@ -83,14 +85,10 @@ pg_tde_ddl_command_start_capture(PG_FUNCTION_ARGS)
if (rel->rd_rel->relam == get_tde_table_am_oid())
{
/* We are creating the index on encrypted table */
ereport(NOTICE,
(errmsg("Creating index on **ENCRYPTED** relation:%s with Oid:%d", stmt->relation->relname, relationId)));
/* set the global state */
tdeCurrentCreateEvent.encryptMode = true;
}
else
ereport(DEBUG1,
(errmsg("Creating index on relation:%s with Oid:%d", stmt->relation->relname, relationId)));
table_close(rel, lockmode);
}
else
Expand All @@ -104,12 +102,12 @@ pg_tde_ddl_command_start_capture(PG_FUNCTION_ARGS)
tdeCurrentCreateEvent.eventType = TDE_TABLE_CREATE_EVENT;
tdeCurrentCreateEvent.relation = stmt->relation;

elog(DEBUG1, "CREATING TABLE %s Using Access Method %s", stmt->relation->relname, stmt->accessMethod);
if (stmt->accessMethod && !strcmp(stmt->accessMethod, "pg_tde2"))
{
tdeCurrentCreateEvent.encryptMode = true;
}
}
#endif
PG_RETURN_NULL();
}

Expand All @@ -120,6 +118,7 @@ pg_tde_ddl_command_start_capture(PG_FUNCTION_ARGS)
Datum
pg_tde_ddl_command_end_capture(PG_FUNCTION_ARGS)
{
#ifdef PERCONA_FORK
/* Ensure this function is being called as an event trigger */
if (!CALLED_AS_EVENT_TRIGGER(fcinfo)) /* internal error */
ereport(ERROR,
Expand All @@ -134,6 +133,7 @@ pg_tde_ddl_command_end_capture(PG_FUNCTION_ARGS)

/* All we need to do is to clear the event state */
reset_current_tde_create_event();
#endif
PG_RETURN_NULL();
}

Expand Down
8 changes: 2 additions & 6 deletions src/smgr/pg_tde_smgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "access/pg_tde_tdemap.h"
#include "pg_tde_event_capture.h"

#if PG_VERSION_NUM >= 170000
#ifdef PERCONA_FORK

// TODO: implement proper IV
// iv should be based on blocknum + relfile, available in the API
Expand All @@ -21,10 +21,6 @@ tde_smgr_get_key(SMgrRelation reln)
// As otherwise we would call GetMasterKey recursively and deadlock
static int recursion = 0;

ereport(NOTICE,
(errmsg("Trying to decide if table is encrypted: %u", reln->smgr_rlocator.locator.relNumber)));


if(IsCatalogRelationOid(reln->smgr_rlocator.locator.relNumber))
{
// do not try to encrypt/decrypt catalog tables
Expand Down Expand Up @@ -214,4 +210,4 @@ void RegisterStorageMgr()
void RegisterStorageMgr()
{
}
#endif
#endif /* PERCONA_FORK */

0 comments on commit 9b9e105

Please sign in to comment.