Skip to content

Commit

Permalink
Refactor diskquota local_table_stats_map
Browse files Browse the repository at this point in the history
  • Loading branch information
RekGRpth committed Oct 10, 2024
1 parent 2862042 commit 1dd79be
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 31 deletions.
33 changes: 12 additions & 21 deletions src/gp_activetable.c
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,8 @@ gp_fetch_active_tables(bool is_init)
Assert(Gp_role == GP_ROLE_DISPATCH);

memset(&ctl, 0, sizeof(ctl));
ctl.keysize = sizeof(TableEntryKey);
ctl.entrysize = sizeof(DiskQuotaActiveTableEntry);
ctl.keysize = sizeof(Oid);
ctl.entrysize = sizeof(ActiveTableEntryCombined) + SEGCOUNT * sizeof(Size);
ctl.hcxt = CurrentMemoryContext;

local_table_stats_map = diskquota_hash_create("local active table map with relfilenode info", 1024, &ctl,
Expand Down Expand Up @@ -949,8 +949,7 @@ load_table_size(HTAB *local_table_stats_map)
TupleDesc tupdesc;
int i;
bool found;
TableEntryKey key;
DiskQuotaActiveTableEntry *quota_entry;
ActiveTableEntryCombined *quota_entry;
SPIPlanPtr plan;
Portal portal;
char *sql = "select tableid, size, segid from diskquota.table_size";
Expand Down Expand Up @@ -1017,13 +1016,10 @@ load_table_size(HTAB *local_table_stats_map)
dat = SPI_getbinval(tup, tupdesc, 3, &isnull);
if (isnull) continue;
segid = DatumGetInt16(dat);
key.reloid = reloid;
key.segid = segid;

quota_entry = (DiskQuotaActiveTableEntry *)hash_search(local_table_stats_map, &key, HASH_ENTER, &found);
quota_entry = (ActiveTableEntryCombined *)hash_search(local_table_stats_map, &reloid, HASH_ENTER, &found);
quota_entry->reloid = reloid;
quota_entry->tablesize = size;
quota_entry->segid = segid;
quota_entry->tablesize[segid + 1] = size;
}
SPI_freetuptable(SPI_tuptable);
SPI_cursor_fetch(portal, true, 10000);
Expand Down Expand Up @@ -1168,8 +1164,7 @@ pull_active_table_size_from_seg(HTAB *local_table_stats_map, char *active_oid_ar
bool found;
Oid reloid;
int segId;
TableEntryKey key;
DiskQuotaActiveTableEntry *entry;
ActiveTableEntryCombined *entry;

PGresult *pgresult = cdb_pgresults.pg_results[i];

Expand All @@ -1184,39 +1179,35 @@ pull_active_table_size_from_seg(HTAB *local_table_stats_map, char *active_oid_ar
{
reloid = atooid(PQgetvalue(pgresult, j, 0));
tableSize = (Size)atoll(PQgetvalue(pgresult, j, 1));
key.reloid = reloid;
/* for diskquota extension version is 1.0, pgresult doesn't contain segid */
if (PQnfields(pgresult) == 3)
{
/* get the segid, tablesize for each table */
segId = atoi(PQgetvalue(pgresult, j, 2));
key.segid = segId;
entry = (DiskQuotaActiveTableEntry *)hash_search(local_table_stats_map, &key, HASH_ENTER, &found);
entry = (ActiveTableEntryCombined *)hash_search(local_table_stats_map, &reloid, HASH_ENTER, &found);

if (!found)
{
/* receive table size info from the first segment */
entry->reloid = reloid;
entry->segid = segId;
}
entry->tablesize = tableSize;
entry->tablesize[segId + 1] = tableSize;
}

/* when segid is -1, the tablesize is the sum of tablesize of master and all segments */
key.segid = -1;
entry = (DiskQuotaActiveTableEntry *)hash_search(local_table_stats_map, &key, HASH_ENTER, &found);
segId = -1;
entry = (ActiveTableEntryCombined *)hash_search(local_table_stats_map, &reloid, HASH_ENTER, &found);

if (!found)
{
/* receive table size info from the first segment */
entry->reloid = reloid;
entry->tablesize = tableSize;
entry->segid = -1;
entry->tablesize[segId + 1] = tableSize;
}
else
{
/* sum table size from all the segments */
entry->tablesize = entry->tablesize + tableSize;
entry->tablesize[segId + 1] = entry->tablesize[segId + 1] + tableSize;
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/gp_activetable.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ typedef struct DiskQuotaActiveTableEntry
Size tablesize;
} DiskQuotaActiveTableEntry;

typedef struct ActiveTableEntryCombined
{
Oid reloid;
Size tablesize[];
} ActiveTableEntryCombined;

extern HTAB *gp_fetch_active_tables(bool force);
extern void init_active_table_hook(void);
extern void init_shm_worker_active_tables(void);
Expand Down
17 changes: 7 additions & 10 deletions src/quotamodel.c
Original file line number Diff line number Diff line change
Expand Up @@ -918,9 +918,8 @@ calculate_table_disk_usage(bool is_init, HTAB *local_active_table_stat_map)
TableSizeEntry *tsentry = NULL;
Oid relOid;
HASH_SEQ_STATUS iter;
DiskQuotaActiveTableEntry *active_table_entry;
ActiveTableEntryCombined *active_table_entry;
TableSizeEntryKey key;
TableEntryKey active_table_key;
List *oidlist;
ListCell *l;
int delete_entries_num = 0;
Expand Down Expand Up @@ -1042,10 +1041,8 @@ calculate_table_disk_usage(bool is_init, HTAB *local_active_table_stat_map)

/* mark tsentry is_exist */
if (tsentry) set_table_size_entry_flag(tsentry, TABLE_EXIST);
active_table_key.reloid = relOid;
active_table_key.segid = cur_segid;
active_table_entry = (DiskQuotaActiveTableEntry *)hash_search(
local_active_table_stat_map, &active_table_key, HASH_FIND, &active_tbl_found);
active_table_entry = (ActiveTableEntryCombined *)hash_search(
local_active_table_stat_map, &relOid, HASH_FIND, &active_tbl_found);

/* skip to recalculate the tables which are not in active list */
if (active_tbl_found)
Expand All @@ -1055,15 +1052,15 @@ calculate_table_disk_usage(bool is_init, HTAB *local_active_table_stat_map)
/* pretend process as utility mode, and append the table size on master */
Gp_role = GP_ROLE_UTILITY;

active_table_entry->tablesize += calculate_table_size(relOid);
active_table_entry->tablesize[cur_segid + 1] += calculate_table_size(relOid);

Gp_role = GP_ROLE_DISPATCH;
}
/* firstly calculate the updated total size of a table */
updated_total_size = active_table_entry->tablesize - TableSizeEntryGetSize(tsentry, cur_segid);
updated_total_size = active_table_entry->tablesize[cur_segid + 1] - TableSizeEntryGetSize(tsentry, cur_segid);

/* update the table_size entry */
TableSizeEntrySetSize(tsentry, cur_segid, active_table_entry->tablesize);
TableSizeEntrySetSize(tsentry, cur_segid, active_table_entry->tablesize[cur_segid + 1]);
TableSizeEntrySetFlushFlag(tsentry, cur_segid);

/* update the disk usage, there may be entries in the map whose keys are InvlidOid as the tsentry does
Expand Down Expand Up @@ -1347,7 +1344,7 @@ dispatch_rejectmap(HTAB *local_active_table_stat_map)
{
HASH_SEQ_STATUS hash_seq;
GlobalRejectMapEntry *rejectmap_entry;
DiskQuotaActiveTableEntry *active_table_entry;
ActiveTableEntryCombined *active_table_entry;
int num_entries, count = 0;
CdbPgResults cdb_pgresults = {NULL, 0};
StringInfoData rows;
Expand Down

0 comments on commit 1dd79be

Please sign in to comment.