Skip to content

Commit

Permalink
Refactor hashtable initalization by using hash_helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
gokhangulbiz committed May 9, 2023
1 parent 7649b9d commit 2dfa19c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 48 deletions.
53 changes: 5 additions & 48 deletions src/backend/distributed/utils/citus_stat_tenants.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "distributed/citus_safe_lib.h"
#include "distributed/colocation_utils.h"
#include "distributed/distributed_planner.h"
#include "distributed/hash_helpers.h"
#include "distributed/jsonbutils.h"
#include "distributed/log_utils.h"
#include "distributed/listutils.h"
Expand Down Expand Up @@ -68,8 +69,6 @@ static TenantStats * CreateTenantStats(MultiTenantMonitor *monitor, TimestampTz
static TenantStatsHashKey * CreateTenantStatsHashKey(char *tenantAttribute, uint32
colocationGroupId);
static TenantStats * FindTenantStats(MultiTenantMonitor *monitor);
static uint32 TenantStatsHashFn(const void *key, Size keysize);
static int TenantStatsMatchFn(const void *key1, const void *key2, Size keysize);
static size_t MultiTenantMonitorshmemSize(void);
static char * ExtractTopComment(const char *inputString);
static char * EscapeCommentChars(const char *str);
Expand Down Expand Up @@ -631,18 +630,10 @@ CreateSharedMemoryForMultiTenantMonitor()
monitor->namedLockTranche.trancheName);
LWLockInitialize(&monitor->lock, monitor->namedLockTranche.trancheId);

HASHCTL info;

memset(&info, 0, sizeof(info));
info.keysize = sizeof(TenantStatsHashKey);
info.entrysize = sizeof(TenantStats);
info.hash = TenantStatsHashFn;
info.match = TenantStatsMatchFn;

monitor->tenants = ShmemInitHash("citus_stats_tenants hash",
StatTenantsLimit * 3, StatTenantsLimit * 3,
&info, HASH_ELEM | HASH_FUNCTION | HASH_COMPARE |
HASH_SHARED_MEM);
monitor->tenants = CreateSimpleHashWithNameAndSize(TenantStatsHashKey, TenantStats,
"citus_stats_tenants hash",
StatTenantsLimit * 3
);

return monitor;
}
Expand Down Expand Up @@ -767,40 +758,6 @@ CreateTenantStatsHashKey(char *tenantAttribute, uint32 colocationGroupId)
return key;
}


/*
* CitusQuerysStatsHashFn calculates and returns hash value for a key
*/
static uint32
TenantStatsHashFn(const void *key, Size keysize)
{
const TenantStatsHashKey *k = (const TenantStatsHashKey *) key;

return hash_any((const unsigned char *) (k->tenantAttribute), strlen(
k->tenantAttribute)) ^
hash_uint32((uint32) k->colocationGroupId);
}


/*
* TenantStatsMatchFn compares two keys - zero means match.
* See definition of HashCompareFunc in hsearch.h for more info.
*/
static int
TenantStatsMatchFn(const void *key1, const void *key2, Size keysize)
{
const TenantStatsHashKey *k1 = (const TenantStatsHashKey *) key1;
const TenantStatsHashKey *k2 = (const TenantStatsHashKey *) key2;

if (strcmp(k1->tenantAttribute, k2->tenantAttribute) == 0 &&
k1->colocationGroupId == k2->colocationGroupId)
{
return 0;
}
return 1;
}


/*
* MultiTenantMonitorshmemSize calculates the size of the multi tenant monitor using
* StatTenantsLimit parameter.
Expand Down
3 changes: 3 additions & 0 deletions src/include/distributed/utils/citus_stat_tenants.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#ifndef CITUS_ATTRIBUTE_H
#define CITUS_ATTRIBUTE_H

#include "distributed/hash_helpers.h"
#include "executor/execdesc.h"
#include "executor/executor.h"
#include "storage/lwlock.h"
Expand All @@ -28,6 +29,8 @@ typedef struct TenantStatsHashKey
char tenantAttribute[MAX_TENANT_ATTRIBUTE_LENGTH];
int colocationGroupId;
} TenantStatsHashKey;
assert_valid_hash_key2(TenantStatsHashKey, tenantAttribute, colocationGroupId);


/*
* TenantStats is the struct that keeps statistics about one tenant.
Expand Down

0 comments on commit 2dfa19c

Please sign in to comment.