Skip to content

Commit

Permalink
using functions to work with time from gpdb instead of libc. add flag…
Browse files Browse the repository at this point in the history
… HASH_FIXED_SIZE to all hash tables in shared memory.
  • Loading branch information
KnightMurloc committed Nov 28, 2023
1 parent f7b0ca1 commit 705d2ca
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 31 deletions.
5 changes: 3 additions & 2 deletions src/diskquota.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ int diskquota_max_workers = 10;
int diskquota_max_table_segments = 0;
int diskquota_max_monitored_databases = 0;
int diskquota_max_quota_probes = 0;
time_t diskquota_hashmap_overflow_report_timeout = 0;
int diskquota_hashmap_overflow_report_timeout = 0;

DiskQuotaLocks diskquota_locks;
ExtensionDDLMessage *extension_ddl_message = NULL;
Expand Down Expand Up @@ -417,7 +417,8 @@ define_guc_variables(void)
PGC_POSTMASTER, 0, NULL, NULL, NULL);
DefineCustomIntVariable("diskquota.hashmap_overflow_report_timeout",
"Time interval in seconds between shared hash map overflow report.", NULL,
&diskquota_hashmap_overflow_report_timeout, 60, 0, INT_MAX, PGC_SUSET, 0, NULL, NULL, NULL);
&diskquota_hashmap_overflow_report_timeout, 60, 0, INT_MAX / 1000, PGC_SUSET, 0, NULL, NULL,
NULL);
}

/* ---- Functions for disk quota worker process ---- */
Expand Down
6 changes: 3 additions & 3 deletions src/diskquota.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ extern Datum diskquota_fetch_table_stat(PG_FUNCTION_ARGS);
extern int diskquota_naptime;
extern int diskquota_max_active_tables;
extern bool diskquota_hardlimit;
extern time_t diskquota_hashmap_overflow_report_timeout;
extern int diskquota_hashmap_overflow_report_timeout;

extern int SEGCOUNT;
extern int worker_spi_get_extension_version(int *major, int *minor);
Expand Down Expand Up @@ -317,6 +317,6 @@ extern HTAB *diskquota_hash_create(const char *tabname, long nelem, HASHC
extern HTAB *DiskquotaShmemInitHash(const char *name, long init_size, long max_size, HASHCTL *infoP, int hash_flags,
DiskquotaHashFunction hash_function);
extern void refresh_monitored_dbid_cache(void);
extern void *shm_hash_enter(HTAB *hashp, void *keyPtr, bool *foundPtr, uint max_size, const char *warning_message,
time_t *last_overflow_report);
extern void *shm_hash_enter(HTAB *hashp, const void *keyPtr, bool *foundPtr, int max_size, const char *warning_message,
TimestampTz *last_overflow_report);
#endif
28 changes: 11 additions & 17 deletions src/diskquota_utility.c
Original file line number Diff line number Diff line change
Expand Up @@ -1678,32 +1678,26 @@ DiskquotaShmemInitHash(const char *name, /* table string name fo
infoP->hash = oid_hash;
else
infoP->hash = string_hash;
return ShmemInitHash(name, init_size, max_size, infoP, hash_flags | HASH_FUNCTION);
return ShmemInitHash(name, init_size, max_size, infoP, hash_flags | HASH_FUNCTION | HASH_FIXED_SIZE);
#else
return ShmemInitHash(name, init_size, max_size, infoP, hash_flags | HASH_BLOBS);
return ShmemInitHash(name, init_size, max_size, infoP, hash_flags | HASH_BLOBS | HASH_FIXED_SIZE);
#endif /* GP_VERSION_NUM */
}

// Add or find an entry in a hash table with a size limit. If the limit is reached, only the search will be performed.
// When overflowing, the warning warning_message will be report. But not more often than specified in
// diskquota_hashmap_overflow_report_timeout. The time of the last warning is passed in last_overflow_report.
void *
shm_hash_enter(HTAB *hashp, void *keyPtr, bool *foundPtr, uint max_size, const char *warning_message,
time_t *last_overflow_report)
shm_hash_enter(HTAB *hashp, const void *keyPtr, bool *foundPtr, int max_size, const char *warning_message,
TimestampTz *last_overflow_report)
{
if (hash_get_num_entries(hashp) >= max_size)
void *result = hash_search(hashp, keyPtr, HASH_ENTER_NULL, foundPtr);
if (hash_get_num_entries(hashp) >= max_size &&
TimestampDifferenceExceeds(*last_overflow_report, GetCurrentTimestamp(),
diskquota_hashmap_overflow_report_timeout * 1000))
{
return hash_search(hashp, keyPtr, HASH_FIND, foundPtr);
}
else
{
void *result = hash_search(hashp, keyPtr, HASH_ENTER, foundPtr);
if (hash_get_num_entries(hashp) >= max_size &&
(time(NULL) - *last_overflow_report) >= diskquota_hashmap_overflow_report_timeout)
{
ereport(WARNING, (errmsg(warning_message, max_size)));
*last_overflow_report = time(NULL);
}
return result;
ereport(WARNING, (errmsg(warning_message, max_size)));
*last_overflow_report = GetCurrentTimestamp();
}
return result;
}
4 changes: 2 additions & 2 deletions src/gp_activetable.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ typedef struct DiskQuotaSetOFCache
} DiskQuotaSetOFCache;

HTAB *active_tables_map = NULL; // Set<DiskQuotaActiveTableFileEntry>
time_t active_tables_last_overflow_report = 0;
TimestampTz active_tables_last_overflow_report = 0;

#define ACTIVE_TABLE_ENTER(keyPtr, foundPtr) \
shm_hash_enter(active_tables_map, keyPtr, foundPtr, diskquota_max_active_tables, \
Expand All @@ -69,7 +69,7 @@ time_t active_tables_last_overflow_report = 0;
* dbid will be removed from it when droping diskquota extension
*/
HTAB *altered_reloid_cache = NULL; // Set<Oid>
time_t altered_reloid_cache_last_overflow_report = 0;
TimestampTz altered_reloid_cache_last_overflow_report = 0;

#define ALTERED_RELOID_CACHE_ENTER(keyPtr, foundPtr) \
shm_hash_enter(altered_reloid_cache, keyPtr, foundPtr, diskquota_max_active_tables, \
Expand Down
14 changes: 7 additions & 7 deletions src/quotamodel.c
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ disk_quota_shmem_startup(void)
hash_ctl.keysize = sizeof(RejectMapEntry);
hash_ctl.entrysize = sizeof(GlobalRejectMapEntry);
disk_quota_reject_map =
DiskquotaShmemInitHash("rejectmap whose quota limitation is reached", INIT_DISK_QUOTA_REJECT_ENTRIES,
DiskquotaShmemInitHash("rejectmap whose quota limitation is reached", MAX_DISK_QUOTA_REJECT_ENTRIES,
MAX_DISK_QUOTA_REJECT_ENTRIES, &hash_ctl, HASH_ELEM, DISKQUOTA_TAG_HASH);

init_shm_worker_active_tables();
Expand Down Expand Up @@ -582,8 +582,8 @@ init_disk_quota_model(uint32 id)
memset(&hash_ctl, 0, sizeof(hash_ctl));
hash_ctl.keysize = sizeof(TableSizeEntryKey);
hash_ctl.entrysize = sizeof(TableSizeEntry);
table_size_map = DiskquotaShmemInitHash(str.data, INIT_NUM_TABLE_SIZE_ENTRIES, MAX_NUM_TABLE_SIZE_ENTRIES,
&hash_ctl, HASH_ELEM, DISKQUOTA_TAG_HASH);
table_size_map = DiskquotaShmemInitHash(str.data, MAX_NUM_TABLE_SIZE_ENTRIES, MAX_NUM_TABLE_SIZE_ENTRIES, &hash_ctl,
HASH_ELEM, DISKQUOTA_TAG_HASH);

/* for localrejectmap */
/* WARNNING: The max length of name of the map is 48 */
Expand All @@ -600,7 +600,7 @@ init_disk_quota_model(uint32 id)
memset(&hash_ctl, 0, sizeof(hash_ctl));
hash_ctl.entrysize = sizeof(QuotaInfoEntry);
hash_ctl.keysize = sizeof(QuotaInfoEntryKey);
quota_info_map = DiskquotaShmemInitHash(str.data, INIT_QUOTA_MAP_ENTRIES, MAX_QUOTA_MAP_ENTRIES, &hash_ctl,
quota_info_map = DiskquotaShmemInitHash(str.data, MAX_QUOTA_MAP_ENTRIES, MAX_QUOTA_MAP_ENTRIES, &hash_ctl,
HASH_ELEM, DISKQUOTA_TAG_HASH);

pfree(str.data);
Expand Down Expand Up @@ -635,8 +635,8 @@ vacuum_disk_quota_model(uint32 id)
memset(&hash_ctl, 0, sizeof(hash_ctl));
hash_ctl.keysize = sizeof(TableSizeEntryKey);
hash_ctl.entrysize = sizeof(TableSizeEntry);
table_size_map = DiskquotaShmemInitHash(str.data, INIT_NUM_TABLE_SIZE_ENTRIES, MAX_NUM_TABLE_SIZE_ENTRIES,
&hash_ctl, HASH_ELEM, DISKQUOTA_TAG_HASH);
table_size_map = DiskquotaShmemInitHash(str.data, MAX_NUM_TABLE_SIZE_ENTRIES, MAX_NUM_TABLE_SIZE_ENTRIES, &hash_ctl,
HASH_ELEM, DISKQUOTA_TAG_HASH);
hash_seq_init(&iter, table_size_map);
while ((tsentry = hash_seq_search(&iter)) != NULL)
{
Expand All @@ -663,7 +663,7 @@ vacuum_disk_quota_model(uint32 id)
memset(&hash_ctl, 0, sizeof(hash_ctl));
hash_ctl.entrysize = sizeof(QuotaInfoEntry);
hash_ctl.keysize = sizeof(QuotaInfoEntryKey);
quota_info_map = DiskquotaShmemInitHash(str.data, INIT_QUOTA_MAP_ENTRIES, MAX_QUOTA_MAP_ENTRIES, &hash_ctl,
quota_info_map = DiskquotaShmemInitHash(str.data, MAX_QUOTA_MAP_ENTRIES, MAX_QUOTA_MAP_ENTRIES, &hash_ctl,
HASH_ELEM, DISKQUOTA_TAG_HASH);
hash_seq_init(&iter, quota_info_map);
while ((qentry = hash_seq_search(&iter)) != NULL)
Expand Down

0 comments on commit 705d2ca

Please sign in to comment.