Skip to content
This repository was archived by the owner on Dec 26, 2022. It is now read-only.

Commit 1271e47

Browse files
committed
feat(db): Implement importer and listener for permanode
The listener subscribes newly confirmed transactions from IRI and adds inserting tasks into the task queue of thread pool. The importer reads confirmed transactions from historical transaction files dumped from IRI and adds inserting tasks into the task queue of thread pool. Each worker in the thread pool establishes a session connected to ScyllaDB cluster and takes inserting tasks.
1 parent 3a4942f commit 1271e47

File tree

9 files changed

+717
-7
lines changed

9 files changed

+717
-7
lines changed

common/ta_errors.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,13 @@ typedef enum {
244244
SC_STORAGE_INVALID_INPUT = 0x03 | SC_MODULE_STORAGE | SC_SEVERITY_MAJOR,
245245
/**< Invalid input parameter, e.g., null pointer */
246246
SC_STORAGE_CASSANDRA_QUERY_FAIL = 0x04 | SC_MODULE_STORAGE | SC_SEVERITY_MAJOR,
247-
/**< Failed to execute Cassandra query */
247+
/**< Failed to execute Cassandra query */
248+
SC_STORAGE_SYNC_ERROR = 0x05 | SC_MODULE_STORAGE | SC_SEVERITY_MAJOR,
249+
/**< Failed to synchronize lastest confirmed transactions from IRI */
250+
SC_STORAGE_THPOOL_ADD_REQUEST_FAIL = 0x06 | SC_MODULE_STORAGE | SC_SEVERITY_MAJOR,
251+
/**< Failed to add requests to permanode thread pool. (request queue full) */
252+
SC_STORAGE_PTHREAD_ERROR = 0x07 | SC_MODULE_STORAGE | SC_SEVERITY_MAJOR,
253+
/**< Failed when calling pthread library */
248254

249255
// Core module
250256
SC_CORE_OOM = 0x01 | SC_MODULE_CORE | SC_SEVERITY_FATAL,

docs/permanode.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,34 @@ The ScyllaDB backend in `tangle-accelerator` supports the following APIs:
1111
- find_transactions_approvees
1212
- get_inclusion_status
1313

14-
See [docs/build.md] for more information about enabling the external storage.
14+
Read [docs/build.md] for more information about enabling the external storage.
15+
16+
## Listener
17+
18+
The listener subscribes newly confirmed transactions from IRI and adds inserting tasks into the task queue of thread pool.
19+
20+
Here are configurations and CLI options you need to specify:
21+
22+
* `--iri_host`: Listening IRI host for ZMQ events and quering trytes.
23+
* `--db_host`: Connecting ScyllaDB host name.
24+
* `--thread_num`: Workers number in the thread pool to handle receiving transactions.
25+
26+
Build command:
27+
28+
`bazel build //storage:scylladb_listener`
29+
30+
## Importer
31+
32+
The importer reads confirmed transactions from historical transaction files dumped from IRI and adds inserting tasks into the task queue of thread pool.
33+
34+
The historical transaction files must consist of lines with the format: `TRANSACTION_HASH,TRYTES,SNAPSHOT_INDEX`
35+
36+
Here are configurations and CLI options you need to specify:
37+
38+
* `--db_host`: Connecting ScyllaDB host name.
39+
* `--file`: A file consist of historical transactions file pathes.
40+
* `--thread_num`: Worker's number in the thread pool to handle receiving transactions.
41+
42+
Build command:
43+
44+
`bazel build //storage:scylladb_importer`

storage/BUILD

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,28 @@ cc_library(
55
deps = [
66
":scylladb_identity",
77
":scylladb_permanode",
8+
":scylladb_permanode_thpool",
9+
],
10+
)
11+
12+
cc_binary(
13+
name = "scylladb_importer",
14+
srcs = ["scylladb_importer.c"],
15+
deps = [
16+
":storage",
17+
],
18+
)
19+
20+
cc_binary(
21+
name = "scylladb_listener",
22+
srcs = ["scylladb_listener.c"],
23+
linkopts = [
24+
"-lzmq",
25+
],
26+
deps = [
27+
":storage",
28+
"//accelerator:ta_config",
29+
"@entangled//cclient/api",
830
],
931
)
1032

@@ -33,6 +55,16 @@ cc_library(
3355
],
3456
)
3557

58+
cc_library(
59+
name = "scylladb_permanode_thpool",
60+
srcs = ["scylladb_permanode_thpool.c"],
61+
hdrs = ["scylladb_permanode_thpool.h"],
62+
linkopts = ["-lpthread"],
63+
deps = [
64+
":scylladb_permanode",
65+
],
66+
)
67+
3668
cc_library(
3769
name = "scylladb_permanode",
3870
srcs = ["scylladb_permanode.c"],

storage/scylladb_client.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,18 @@ typedef struct {
3737
} db_client_service_t;
3838

3939
/**
40-
* @brief init ScyllaDB client serivce and connect to specific cluster
40+
* @brief init ScyllaDB client service and connect to specific cluster
4141
*
4242
* @param[out] service ScyllaDB client service
43-
* @param[in] usage specfic usage for db client serivce
43+
* @param[in] usage specfic usage for db client service
4444
* @return
4545
* - SC_OK on success
4646
* - non-zero on error
4747
*/
4848
status_t db_client_service_init(db_client_service_t* service, db_client_usage_t usage);
4949

5050
/**
51-
* @brief free ScyllaDB client serivce
51+
* @brief free ScyllaDB client service
5252
*
5353
* @param[in] service ScyllaDB client service
5454
* @return

storage/scylladb_importer.c

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
* Copyright (C) 2020 BiiLabs Co., Ltd. and Contributors
3+
* All Rights Reserved.
4+
* This is free software; you can redistribute it and/or modify it under the
5+
* terms of the MIT license. A copy of the license can be found in the file
6+
* "LICENSE" at the root of this distribution.
7+
*/
8+
9+
#include <getopt.h>
10+
#include "ta_storage.h"
11+
12+
#define logger_id scylladb_logger_id
13+
14+
typedef struct {
15+
pthread_mutex_t thread_mutex;
16+
db_permanode_pool_t* pool;
17+
char* file_path;
18+
} db_importer_thread_t;
19+
20+
static status_t init_importer_data(db_importer_thread_t* thread_data, db_permanode_pool_t* pool, char* file_list) {
21+
status_t ret = SC_OK;
22+
pthread_mutex_init(&thread_data->thread_mutex, NULL);
23+
thread_data->pool = pool;
24+
thread_data->file_path = strdup(file_list);
25+
return ret;
26+
}
27+
28+
static void* importer_handler(void* data) {
29+
#define TRANSACTION_BUFFER_SIZE \
30+
(NUM_FLEX_TRITS_HASH + 1 + NUM_TRYTES_SERIALIZED_TRANSACTION + 12) // 12 is for snapshot_index
31+
#define MAX_FILE_PATH 256
32+
33+
status_t ret = SC_OK;
34+
db_importer_thread_t* thread_data = (db_importer_thread_t*)data;
35+
pthread_mutex_lock(&thread_data->thread_mutex);
36+
FILE* list_file = NULL;
37+
char file_name_buffer[MAX_FILE_PATH];
38+
39+
if ((list_file = fopen(thread_data->file_path, "r")) == NULL) {
40+
/* The specified configuration file does not exist */
41+
ret = SC_CONF_FOPEN_ERROR;
42+
ta_log_error("Failed to open file %s\n", thread_data->file_path);
43+
goto exit;
44+
}
45+
46+
while (fgets(file_name_buffer, MAX_FILE_PATH, list_file) != NULL) {
47+
char input_buffer[TRANSACTION_BUFFER_SIZE];
48+
FILE* file = NULL;
49+
50+
int name_len = strlen(file_name_buffer);
51+
if (name_len > 0) {
52+
file_name_buffer[name_len - 1] = 0;
53+
} else {
54+
ta_log_warning("Empty file name\n");
55+
continue;
56+
}
57+
58+
if ((file = fopen(file_name_buffer, "r")) == NULL) {
59+
/* The specified configuration file does not exist */
60+
ret = SC_CONF_FOPEN_ERROR;
61+
ta_log_error("Failed to open file %s\n", file_name_buffer);
62+
goto exit;
63+
}
64+
ta_log_info("%s %s\n", "starting to import file : ", file_name_buffer);
65+
int cnt = 1;
66+
int cnt_base1000 = 0;
67+
while (fgets(input_buffer, TRANSACTION_BUFFER_SIZE, file) != NULL) {
68+
if (cnt % 1000 == 0) {
69+
ta_log_info("Import %d K transactions\n", ++cnt_base1000);
70+
cnt = 0;
71+
}
72+
if (input_buffer[strlen(input_buffer) - 1] != '\n') {
73+
ret = SC_STORAGE_INVALID_INPUT;
74+
ta_log_error("%s\n", "Historical dump file format error");
75+
continue;
76+
}
77+
78+
do {
79+
ret = db_permanode_thpool_add((tryte_t*)input_buffer, (tryte_t*)input_buffer + NUM_FLEX_TRITS_HASH + 1,
80+
thread_data->pool);
81+
if (ret != SC_OK) {
82+
pthread_cond_wait(&thread_data->pool->finish_request, &thread_data->thread_mutex);
83+
}
84+
} while (ret != SC_OK);
85+
86+
cnt++;
87+
}
88+
89+
ta_log_info("Successfully import file : %s\n", file_name_buffer);
90+
}
91+
92+
exit:
93+
if (ret == SC_OK) {
94+
ta_log_info("%s %s\n", "Successfully import file : ", thread_data->file_path);
95+
} else {
96+
ta_log_error("Failed to import file : %s\n", thread_data->file_path);
97+
}
98+
return NULL;
99+
}
100+
101+
int main(int argc, char* argv[]) {
102+
int thread_num = 1;
103+
pthread_t* worker_threads; /* thread's structures */
104+
pthread_t importer_thread;
105+
db_worker_thread_t* worker_data;
106+
db_importer_thread_t importer_data;
107+
db_permanode_pool_t pool;
108+
109+
char* db_host = "localhost";
110+
char* file_path = NULL;
111+
const struct option longOpt[] = {{"db_host", required_argument, NULL, 's'},
112+
{"file", required_argument, NULL, 'f'},
113+
{"thread_num", required_argument, NULL, 't'},
114+
{NULL, 0, NULL, 0}};
115+
/* Parse the command line options */
116+
while (1) {
117+
int cmdOpt;
118+
int optIdx;
119+
cmdOpt = getopt_long(argc, argv, "sft:", longOpt, &optIdx);
120+
if (cmdOpt == -1) break;
121+
122+
/* Invalid option */
123+
if (cmdOpt == '?') break;
124+
125+
if (cmdOpt == 's') {
126+
db_host = optarg;
127+
}
128+
if (cmdOpt == 'f') {
129+
file_path = optarg;
130+
}
131+
if (cmdOpt == 't') {
132+
thread_num = atoi(optarg);
133+
}
134+
}
135+
if (file_path == NULL) {
136+
ta_log_error("No specified import file list\n");
137+
return EXIT_FAILURE;
138+
}
139+
if (ta_logger_init() != SC_OK) {
140+
ta_log_error("Failed to init logger\n");
141+
return EXIT_FAILURE;
142+
}
143+
scylladb_logger_init();
144+
worker_threads = malloc(thread_num * sizeof(pthread_t));
145+
worker_data = malloc(thread_num * sizeof(db_worker_thread_t));
146+
147+
db_permanode_thpool_init(&pool);
148+
/* create the request-handling threads */
149+
for (int i = 0; i < thread_num; i++) {
150+
db_permanode_thpool_init_worker(worker_data + i, &pool, db_host);
151+
pthread_create(&worker_threads[i], NULL, db_permanode_worker_handler, (void*)&worker_data[i]);
152+
}
153+
init_importer_data(&importer_data, &pool, file_path);
154+
pthread_create(&importer_thread, NULL, (void*)importer_handler, (void*)&importer_data);
155+
156+
pthread_join(importer_thread, NULL);
157+
158+
db_permanode_tpool_wait(&pool);
159+
free(worker_data);
160+
free(worker_threads);
161+
162+
scylladb_logger_release();
163+
164+
return 0;
165+
}

0 commit comments

Comments
 (0)