Skip to content

Commit 0007f1f

Browse files
committed
holo-client 2.5.4, update README
1 parent 6fdefea commit 0007f1f

51 files changed

Lines changed: 3885 additions & 386 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

holo-client-c/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ add_definitions(-DBUILD_VERSION=\"${GIT_VERSION}\")
1212
include(GNUInstallDirs)
1313
add_compile_options(-Wall)
1414
add_compile_options(-Werror)
15+
# 编译带符号
16+
# add_compile_options(-O0)
17+
# add_compile_options(-g3)
1518
# 当前使用的c编译器
1619
message("-- CMAKE_C_COMPILER: ${CMAKE_C_COMPILER_ID}")
1720
# 当前使用的c编译器的版本

holo-client-c/src/holo_client.c

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include "logger_private.h"
55
#include "unistd.h"
66

7+
#define INT32_STRING_MAX_LENGTH 12
8+
79
int check_mutation(HoloMutation mutation);
810
int check_partition(HoloClient* client, HoloMutation mutation);
911
int check_get(HoloGet get);
@@ -95,6 +97,7 @@ int holo_client_close_client(HoloClient* client) {
9597
}
9698

9799
HoloTableSchema* holo_client_get_tableschema_by_tablename(HoloClient* client, HoloTableName name, bool withCache, char** errMsgAddr) {
100+
LOG_DEBUG("get table schema by table name:%s", name.fullName);
98101
Meta meta = NULL;
99102
HoloTableSchema* schema = NULL;
100103
if(withCache) {
@@ -292,23 +295,38 @@ int check_mutation(HoloMutation mutation) {
292295
return HOLO_CLIENT_RET_OK;
293296
}
294297

298+
static int get_int_partition_value_string(char* intValueStr, const char* intPartitionValue) {
299+
char* copyValue = MALLOC(4, char);
300+
memcpy(copyValue, intPartitionValue, 4);
301+
endian_swap(copyValue, 4);
302+
snprintf(intValueStr, INT32_STRING_MAX_LENGTH, "%d", *(int32_t*)copyValue);
303+
endian_swap(copyValue, 4);
304+
FREE(copyValue);
305+
return HOLO_CLIENT_RET_OK;
306+
}
307+
295308
void* find_partition_table_name(PGconn* conn, void* arg) {
296309
HoloRecord* record = arg;
297310
const char* findPartitionSql = "with inh as (SELECT i.inhrelid, i.inhparent FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace LEFT JOIN pg_catalog.pg_inherits i on c.oid=i.inhparent where n.nspname = $1 and c.relname= $2) select n.nspname as schema_name, c.relname as table_name, inh.inhrelid, inh.inhparent, p.partstrat, pg_get_expr(c.relpartbound, c.oid, true) as part_expr, p.partdefid, p.partnatts, p.partattrs from inh join pg_catalog.pg_class c on inh.inhrelid = c.oid join pg_catalog.pg_namespace n on c.relnamespace = n.oid join pg_partitioned_table p on p.partrelid = inh.inhparent where pg_get_expr(c.relpartbound, c.oid, true) = $3 limit 1";
298311
const char* schemaName = record->schema->tableName->schemaName;
299312
const char* tableName = record->schema->tableName->tableName;
300-
int length = strlen(record->values[record->schema->partitionColumn]) + 19;
301-
char* partitionInfo = MALLOC(length, char);
302-
const char* params[3] = {schemaName, tableName, partitionInfo};
313+
char* partitionInfo = NULL;
303314
PGresult* res = NULL;
304315
HoloTableName* name = NULL;
305-
Oid type = record->schema->columns[record->schema->partitionColumn].type;
306-
if (type == HOLO_TYPE_TEXT || type == HOLO_TYPE_VARCHAR) {
316+
char intValStr[INT32_STRING_MAX_LENGTH];
317+
if (record->valueFormats[record->schema->partitionColumn] == 1) {
318+
//int
319+
get_int_partition_value_string(intValStr, record->values[record->schema->partitionColumn]);
320+
int length = strlen(intValStr) + 17;
321+
partitionInfo = MALLOC(length, char);
322+
snprintf(partitionInfo, length, "FOR VALUES IN (%s)", intValStr);
323+
} else {
307324
//text or varchar
325+
int length = strlen(record->values[record->schema->partitionColumn]) + 19;
326+
partitionInfo = MALLOC(length, char);
308327
snprintf(partitionInfo, length, "FOR VALUES IN ('%s')", record->values[record->schema->partitionColumn]);
309-
} else {
310-
snprintf(partitionInfo, length, "FOR VALUES IN (%s)", record->values[record->schema->partitionColumn]);
311328
}
329+
const char* params[3] = {schemaName, tableName, partitionInfo};
312330
res = PQexecParams(conn, findPartitionSql, 3, NULL, params, NULL, NULL, 0);
313331
if (PQntuples(res) == 0) {
314332
PQclear(res);
@@ -328,13 +346,13 @@ void* retry_create_partition_child_table(PGconn* conn, void* arg) {
328346
int retry = 0;
329347
HoloRecord* record = arg;
330348
HoloTableSchema* parentSchema = record->schema;
331-
char intVal[12];
332-
char* partitionValue = record->values[record->schema->partitionColumn];
349+
char* partitionValue = NULL;
350+
char intValStr[INT32_STRING_MAX_LENGTH];
333351
if (record->valueFormats[parentSchema->partitionColumn] == 1) {
334-
endian_swap(partitionValue, 4);
335-
snprintf(intVal, 12, "%d", *(int32_t*)partitionValue);
336-
endian_swap(partitionValue, 4);
337-
partitionValue = intVal;
352+
get_int_partition_value_string(intValStr, record->values[record->schema->partitionColumn]);
353+
partitionValue = intValStr;
354+
} else {
355+
partitionValue = record->values[record->schema->partitionColumn];
338356
}
339357
int maxTableNameLength = strlen(parentSchema->tableName->tableName) + strlen(partitionValue) + 22;
340358
int maxSqlLength = 2 * strlen(parentSchema->tableName->schemaName) + strlen(parentSchema->tableName->tableName) + maxTableNameLength + strlen(partitionValue) + 57;
@@ -411,15 +429,14 @@ int check_partition(HoloClient* client, HoloMutation mutation) {
411429
HoloTableName* name = NULL;
412430
Sql sql = NULL;
413431
char* partition = NULL;
414-
char intVal[12];
432+
char intValStr[INT32_STRING_MAX_LENGTH];
415433
if (mutation->record->schema->partitionColumn == -1) return HOLO_CLIENT_RET_OK;
416434
parentSchema = mutation->record->schema;
417-
partitionValue = mutation->record->values[parentSchema->partitionColumn];
418435
if (mutation->record->valueFormats[parentSchema->partitionColumn] == 1) {
419-
endian_swap(partitionValue, 4);
420-
snprintf(intVal, 12, "%d", *(int32_t*)partitionValue);
421-
endian_swap(partitionValue, 4);
422-
partitionValue = intVal;
436+
get_int_partition_value_string(intValStr, mutation->record->values[parentSchema->partitionColumn]);
437+
partitionValue = intValStr;
438+
} else {
439+
partitionValue = mutation->record->values[parentSchema->partitionColumn];
423440
}
424441
partitionSchema = meta_cache_find_partition(client->workerPool->metaCache, parentSchema, partitionValue);
425442
if (partitionSchema != NULL) {

holo-client-c/src/logger_log4c.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,24 @@ void* holo_client_log_log4c(const int logLevel, const char* msg) {
4242
switch (logLevel)
4343
{
4444
case HOLO_LOG_LEVEL_DEBUG:
45-
log4c_category_log(log_category, LOG4C_PRIORITY_DEBUG, "%s", msg);
45+
log4c_category_log(log_category, LOG4C_PRIORITY_DEBUG, "[%ld] %s", (unsigned long)pthread_self(), msg);
4646
break;
4747
case HOLO_LOG_LEVEL_INFO:
48-
log4c_category_log(log_category, LOG4C_PRIORITY_INFO, "%s", msg);
48+
log4c_category_log(log_category, LOG4C_PRIORITY_INFO, "[%ld] %s", (unsigned long)pthread_self(), msg);
4949
break;
5050
case HOLO_LOG_LEVEL_WARNING:
51-
log4c_category_log(log_category, LOG4C_PRIORITY_WARN, "%s", msg);
51+
log4c_category_log(log_category, LOG4C_PRIORITY_WARN, "[%ld] %s", (unsigned long)pthread_self(), msg);
5252
break;
5353
case HOLO_LOG_LEVEL_ERROR:
54-
log4c_category_log(log_category, LOG4C_PRIORITY_ERROR, "%s", msg);
54+
log4c_category_log(log_category, LOG4C_PRIORITY_ERROR, "[%ld] %s", (unsigned long)pthread_self(), msg);
5555
break;
5656
case HOLO_LOG_LEVEL_FATAL:
57-
log4c_category_log(log_category, LOG4C_PRIORITY_FATAL, "%s", msg);
57+
log4c_category_log(log_category, LOG4C_PRIORITY_FATAL, "[%ld] %s", (unsigned long)pthread_self(), msg);
5858
break;
5959
case HOLO_LOG_LEVEL_NONE:
6060
break;
6161
default:
62-
log4c_category_log(log_category, LOG4C_PRIORITY_INFO, "%s", msg);
62+
log4c_category_log(log_category, LOG4C_PRIORITY_INFO, "[%ld] %s", (unsigned long)pthread_self(), msg);
6363
break;
6464
}
6565
return NULL;

holo-client-c/src/meta_cache.c

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ MetaCache* holo_client_new_metacache() {
2727
MetaCache* cache = MALLOC(1, MetaCache);
2828
dlist_init(&(cache->schemaList));
2929
dlist_init(&(cache->parentList));
30+
dlist_init(&(cache->garbageList));
3031
cache->rwlock = MALLOC(1, pthread_rwlock_t);
3132
pthread_rwlock_init(cache->rwlock, NULL);
3233
return cache;
@@ -56,6 +57,13 @@ void clear_all_contents(MetaCache* cache) {
5657
holo_client_destroy_tableschema(schema);
5758
FREE(schemaItem);
5859
}
60+
dlist_foreach_modify(miter, &(cache->garbageList)) {
61+
schemaItem = dlist_container(SchemaItem, list_node, miter.cur);
62+
schema = schemaItem->schema;
63+
dlist_delete(miter.cur);
64+
holo_client_destroy_tableschema(schema);
65+
FREE(schemaItem);
66+
}
5967
}
6068

6169
void holo_client_destroy_metacache(MetaCache* cache) {
@@ -82,21 +90,53 @@ HoloTableSchema* find_tableschema_in_metacache(MetaCache* cache, HoloTableName n
8290
return schema;
8391
}
8492

93+
void add_tableschema_to_garbage_list(MetaCache* cache, HoloTableSchema* schema) {
94+
LOG_DEBUG("add table schema:%s to garbage list", schema->tableName->fullName);
95+
SchemaItem* schemaItem = create_schema_item(schema);
96+
dlist_push_tail(&(cache->garbageList), &(schemaItem->list_node));
97+
}
98+
99+
bool should_update_table_schema_in_cache(HoloTableSchema* origin, HoloTableSchema* current) {
100+
if (strcmp(origin->tableName->fullName, current->tableName->fullName) != 0) {
101+
return false;
102+
}
103+
if (origin->nColumns != current->nColumns || origin->nPrimaryKeys != current->nPrimaryKeys || origin->nDistributionKeys != current->nDistributionKeys) {
104+
return true;
105+
}
106+
for (int i = 0; i < current->nColumns; i++) {
107+
HoloColumn originColumn = origin->columns[i];
108+
HoloColumn currentColumn = current->columns[i];
109+
if (!compare_strings(originColumn.name, currentColumn.name)
110+
|| !compare_strings(originColumn.defaultValue, currentColumn.defaultValue)
111+
|| originColumn.type != currentColumn.type
112+
|| originColumn.isPrimaryKey != currentColumn.isPrimaryKey
113+
|| originColumn.nullable != currentColumn.nullable) {
114+
return true;
115+
}
116+
}
117+
118+
return false;
119+
}
120+
85121
void add_tableschema_to_metacache(MetaCache* cache, HoloTableSchema* schema) {
86122
dlist_iter iter;
87123
SchemaItem* schemaItem;
88124
bool updated = false;
89125
pthread_rwlock_wrlock(cache->rwlock);
90126
dlist_foreach(iter, &cache->schemaList) {
91127
schemaItem = dlist_container(SchemaItem, list_node, iter.cur);
92-
if (strcmp(schema->tableName->fullName, schemaItem->schema->tableName->fullName) == 0) {
93-
holo_client_destroy_tableschema(schemaItem->schema);
128+
if (should_update_table_schema_in_cache(schemaItem->schema, schema)) {
129+
// 这里不能直接destory,还可能有mutation用着这个schema
130+
// c没有引用计数,干脆先加到garbageList,最后一起清理
131+
add_tableschema_to_garbage_list(cache, schemaItem->schema);
94132
schemaItem->schema = schema;
133+
LOG_DEBUG("update table schema:%s in meta cache", schema->tableName->fullName);
95134
updated = true;
96135
break;
97136
}
98137
}
99138
if(!updated) {
139+
LOG_DEBUG("add table schema:%s to meta cache", schema->tableName->fullName);
100140
schemaItem = create_schema_item(schema);
101141
dlist_push_tail(&(cache->schemaList), &(schemaItem->list_node));
102142
if (schema->partitionColumn > -1) {

holo-client-c/src/meta_cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
typedef struct _MetaCache {
1010
dlist_head schemaList;
1111
dlist_head parentList;
12+
dlist_head garbageList;
1213
pthread_rwlock_t* rwlock;
1314
} MetaCache;
1415

@@ -38,5 +39,6 @@ HoloTableSchema* find_tableschema_in_metacache(MetaCache*, HoloTableName);
3839
void add_tableschema_to_metacache(MetaCache*, HoloTableSchema*);
3940
HoloTableSchema* meta_cache_find_partition(MetaCache*, HoloTableSchema*, char*);
4041
void meta_cache_add_partition(MetaCache*, HoloTableSchema*, HoloTableSchema*, char*);
42+
void add_tableschema_to_garbage_list(MetaCache*, HoloTableSchema*);
4143

4244
#endif

holo-client-c/src/table_schema.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ HoloTableSchema* holo_client_new_tableschema() {
5656
}
5757

5858
void holo_client_destroy_tableschema(HoloTableSchema* schema) {
59+
LOG_DEBUG("destory table schema:%s", schema->tableName->fullName);
5960
holo_client_destroy_tablename(schema->tableName);
6061
FREE(schema->tableName);
6162
holo_client_destroy_columns(schema->columns, schema->nColumns);

holo-client-c/test/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ COBJS:=$(SRCS:.c=.o)
3939
all:$(BIN)
4040

4141
$(COBJS) : %.o: %.c
42-
$(CC) -c $< -o $@ $(INCS) $(VERSION_FLAG) -std=c11 -D_XOPEN_SOURCE=600 -Wall -Werror
42+
$(CC) -c $< -o $@ $(INCS) $(VERSION_FLAG) -std=c11 -D_XOPEN_SOURCE=600 -Wall -Werror -O0 -g3
4343

4444
$(BIN):$(COBJS)
4545
$(CC) -o $(BIN) $(COBJS) $(STATCI_LIBS) $(SHARED_LIBS) $(CFLAGS)

0 commit comments

Comments
 (0)