-
Notifications
You must be signed in to change notification settings - Fork 16
feat(db): Implement importer and listener for permanode #631
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/* | ||
* Copyright (C) 2020 BiiLabs Co., Ltd. and Contributors | ||
* All Rights Reserved. | ||
* This is free software; you can redistribute it and/or modify it under the | ||
* terms of the MIT license. A copy of the license can be found in the file | ||
* "LICENSE" at the root of this distribution. | ||
*/ | ||
|
||
#include <getopt.h> | ||
#include "ta_storage.h" | ||
|
||
#define logger_id scylladb_logger_id | ||
|
||
typedef struct { | ||
pthread_mutex_t thread_mutex; | ||
db_permanode_pool_t* pool; | ||
char* file_path; | ||
} db_importer_thread_t; | ||
|
||
static status_t init_importer_data(db_importer_thread_t* thread_data, db_permanode_pool_t* pool, char* file_list) { | ||
status_t ret = SC_OK; | ||
pthread_mutex_init(&thread_data->thread_mutex, NULL); | ||
thread_data->pool = pool; | ||
thread_data->file_path = strdup(file_list); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
return ret; | ||
} | ||
|
||
static void* importer_handler(void* data) { | ||
#define TRANSACTION_BUFFER_SIZE \ | ||
(NUM_FLEX_TRITS_HASH + 1 + NUM_TRYTES_SERIALIZED_TRANSACTION + 12) // 12 is for snapshot_index | ||
#define MAX_FILE_PATH 256 | ||
|
||
status_t ret = SC_OK; | ||
db_importer_thread_t* thread_data = (db_importer_thread_t*)data; | ||
pthread_mutex_lock(&thread_data->thread_mutex); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
FILE* list_file = NULL; | ||
char file_name_buffer[MAX_FILE_PATH]; | ||
|
||
if ((list_file = fopen(thread_data->file_path, "r")) == NULL) { | ||
/* The specified configuration file does not exist */ | ||
ret = SC_CONF_FOPEN_ERROR; | ||
ta_log_error("Failed to open file %s\n", thread_data->file_path); | ||
goto exit; | ||
} | ||
|
||
while (fgets(file_name_buffer, MAX_FILE_PATH, list_file) != NULL) { | ||
char input_buffer[TRANSACTION_BUFFER_SIZE]; | ||
FILE* file = NULL; | ||
|
||
int name_len = strlen(file_name_buffer); | ||
if (name_len > 0) { | ||
file_name_buffer[name_len - 1] = 0; | ||
} else { | ||
ta_log_warning("Empty file name\n"); | ||
continue; | ||
} | ||
|
||
if ((file = fopen(file_name_buffer, "r")) == NULL) { | ||
/* The specified configuration file does not exist */ | ||
ret = SC_CONF_FOPEN_ERROR; | ||
ta_log_error("Failed to open file %s\n", file_name_buffer); | ||
goto exit; | ||
} | ||
ta_log_info("%s %s\n", "starting to import file : ", file_name_buffer); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You will get an extra space after the colon |
||
int cnt = 1; | ||
int cnt_base1000 = 0; | ||
while (fgets(input_buffer, TRANSACTION_BUFFER_SIZE, file) != NULL) { | ||
if (cnt % 1000 == 0) { | ||
ta_log_info("Import %d K transactions\n", ++cnt_base1000); | ||
cnt = 0; | ||
} | ||
if (input_buffer[strlen(input_buffer) - 1] != '\n') { | ||
ret = SC_STORAGE_INVALID_INPUT; | ||
ta_log_error("%s\n", "Historical dump file format error"); | ||
continue; | ||
} | ||
|
||
do { | ||
ret = db_permanode_thpool_add((tryte_t*)input_buffer, (tryte_t*)input_buffer + NUM_FLEX_TRITS_HASH + 1, | ||
thread_data->pool); | ||
if (ret != SC_OK) { | ||
pthread_cond_wait(&thread_data->pool->finish_request, &thread_data->thread_mutex); | ||
} | ||
} while (ret != SC_OK); | ||
|
||
cnt++; | ||
} | ||
|
||
ta_log_info("Successfully import file : %s\n", file_name_buffer); | ||
} | ||
|
||
exit: | ||
if (ret == SC_OK) { | ||
ta_log_info("%s %s\n", "Successfully import file : ", thread_data->file_path); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
} else { | ||
ta_log_error("Failed to import file : %s\n", thread_data->file_path); | ||
} | ||
return NULL; | ||
} | ||
|
||
int main(int argc, char* argv[]) { | ||
int thread_num = 1; | ||
pthread_t* worker_threads; /* thread's structures */ | ||
pthread_t importer_thread; | ||
db_worker_thread_t* worker_data; | ||
db_importer_thread_t importer_data; | ||
db_permanode_pool_t pool; | ||
|
||
char* db_host = "localhost"; | ||
char* file_path = NULL; | ||
const struct option longOpt[] = {{"db_host", required_argument, NULL, 's'}, | ||
{"file", required_argument, NULL, 'f'}, | ||
{"thread_num", required_argument, NULL, 't'}, | ||
{NULL, 0, NULL, 0}}; | ||
/* Parse the command line options */ | ||
while (1) { | ||
int cmdOpt; | ||
int optIdx; | ||
cmdOpt = getopt_long(argc, argv, "sft:", longOpt, &optIdx); | ||
if (cmdOpt == -1) break; | ||
|
||
/* Invalid option */ | ||
if (cmdOpt == '?') break; | ||
|
||
if (cmdOpt == 's') { | ||
db_host = optarg; | ||
} | ||
if (cmdOpt == 'f') { | ||
file_path = optarg; | ||
} | ||
if (cmdOpt == 't') { | ||
thread_num = atoi(optarg); | ||
} | ||
} | ||
if (file_path == NULL) { | ||
ta_log_error("No specified import file list\n"); | ||
return EXIT_FAILURE; | ||
} | ||
if (ta_logger_init() != SC_OK) { | ||
ta_log_error("Failed to init logger\n"); | ||
return EXIT_FAILURE; | ||
} | ||
scylladb_logger_init(); | ||
worker_threads = malloc(thread_num * sizeof(pthread_t)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. error checking |
||
worker_data = malloc(thread_num * sizeof(db_worker_thread_t)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
|
||
db_permanode_thpool_init(&pool); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
/* create the request-handling threads */ | ||
for (int i = 0; i < thread_num; i++) { | ||
db_permanode_thpool_init_worker(worker_data + i, &pool, db_host); | ||
pthread_create(&worker_threads[i], NULL, db_permanode_worker_handler, (void*)&worker_data[i]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Error checking |
||
} | ||
init_importer_data(&importer_data, &pool, file_path); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
pthread_create(&importer_thread, NULL, (void*)importer_handler, (void*)&importer_data); | ||
|
||
pthread_join(importer_thread, NULL); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
|
||
db_permanode_tpool_wait(&pool); | ||
free(worker_data); | ||
free(worker_threads); | ||
|
||
scylladb_logger_release(); | ||
|
||
return 0; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need error checking here?