-
Notifications
You must be signed in to change notification settings - Fork 3
/
bwcontrol.c
1475 lines (1266 loc) · 43.2 KB
/
bwcontrol.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* K4M bwcontrol
*
* API Adaptor for C
*
* Author
* JHB [email protected]
*/
/**
\mainpage eXperDB PostgreSQL Plugin Api reference manual
\author JHB [email protected]
\date 2016-11-30
\section api_groups API 종류
APIs : bwcontrol.c <br>
error code : error_sting.h <br>
\ref mapping <br>
\ref control <br>
\ref status <br>
\ref kafka_connect <br>
\section sample
@code
select pg_add_ingest_table('public', 'test_table', 1, 1, '');
INFO: Success(9)
pg_add_ingest_table
---------------------
Success(9)
(1 row)
select pg_del_ingest_table('public', 'test_table');
INFO: Success(0)
pg_del_ingest_table
---------------------
Success(0)
(1 row)
select pg_suspend_ingest();
INFO: Success(0)
pg_suspend_ingest
-------------------
Success(0)
(1 row)
select pg_resume_ingest(1);
INFO: Success(0)
pg_resume_ingest
------------------
Success(0)
(1 row)
select pg_get_status_ingest();
INFO: Process is running(0)
pg_get_status_ingest
-----------------------
Process is running(0)
(1 row)
select pg_make_kafka_connect('conn_name');
INFO: Success(7)
pg_make_kafka_connect
-------------------------
Success(7)
(1 row)
select pg_delete_kafka_connect('conn_name');
INFO: Success(8)
pg_delete_kafka_connect
-------------------------
Success(8)
(1 row)
@endcode
\section setup
@see README.md
\code
$ USE_PGXS=1 make
$ USE_PGXS=1 make install
\endcode
\subsection Install
Install bwcontrol extension
\code
testdb=# create extension bwcontrol;
\endcode
\subsection Uninstall
Uninstall bwcontrol extention
\code
testdb=# drop extension bwcontrol;
\endcode
\subsection Configuration
How to configure bwcontrol
refer to postgresql.auto.conf
bw.bwpath = '/home/robin/pghome/bin/bottledwater' <br>
bw.kafka_broker = 'localhost:9092' <br>
bw.schema_registry='localhost:8081' <br>
bw.consumer='localhost:8083/connectors' <br>
bw.consumer_sub='default-config' <br>
*/
/**
* @file bwcontrol.c
* @author JHB
* @date 30 Nov 2016
* @brief bwcontrol Plugin API
*
*/
#include "postgres.h"
#include "fmgr.h"
#include "executor/spi.h"
#include "utils/builtins.h"
#include "lib/stringinfo.h"
#include <sys/stat.h>
#include <signal.h>
#include <unistd.h>
#include <curl/curl.h>
#include <string.h>
#include <dlfcn.h>
#include <stdlib.h>
#include "error_string.h"
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(pg_add_ingest_table);
PG_FUNCTION_INFO_V1(pg_del_ingest_table);
PG_FUNCTION_INFO_V1(pg_add_ingest_column);
PG_FUNCTION_INFO_V1(pg_del_ingest_column);
PG_FUNCTION_INFO_V1(pg_resume_ingest);
PG_FUNCTION_INFO_V1(pg_suspend_ingest);
PG_FUNCTION_INFO_V1(pg_get_status_ingest);
PG_FUNCTION_INFO_V1(pg_make_kafka_connect);
PG_FUNCTION_INFO_V1(pg_delete_kafka_connect);
void _PG_init(void);
void _PG_fini(void);
/* ------------------------------------------------
* Maros
* ------------------------------------------------*/
#define DB_LOG_RETURN(level, str, err) do { \
char err_buff[256]; \
snprintf(err_buff, 255, "%s(%d)", str, err);\
elog(level, "%s", err_buff); \
PG_RETURN_TEXT_P(cstring_to_text(err_buff));\
} while(0)
#define xpfree(var_) \
do { \
if (var_ != NULL) \
{ \
pfree(var_); \
var_ = NULL; \
} \
} while (0)
#define xfree(var_) \
do { \
if (var_ != NULL) \
{ \
free(var_); \
var_ = NULL; \
} \
} while (0)
#define ALLOCNCOPY(ptr_, var_) \
do { \
int len = 0; \
if (var_ != NULL){ \
len = strlen(var_)+1 \
ptr_ = (char*)malloc(len); \
memset(ptr_, 0x00, len); \
strncpy(ptr_, var_, len); \
} \
} while (0)
#define CHECKMAXLEN(val_, len) \
do { \
if (val_ != NULL) \
{ \
if(strlen(val_) >= len) \
PG_RETURN_TEXT_P(cstring_to_text("Check string length."));\
} \
} while (0)
#define CHECK_RETURN(err, call) { err = call; if (err < 0) return err;}
#define CHECK(err, call) err = call
#define MODE_INSERT 0
#define MODE_UPDATE 1
#define MODE_DELETE 2
#define MODE_SUSPEND 3
#define MODE_RESUME 4
#define QBUFFLEN 10240
#define CONTENTSDATALEN 10240
#define URLLEN 512
#define OPTLEN 256
#define INVALID_PID (-1)
#define CONTENT_TYPE "application/json"
#define HTTP_TIMOUT 3
/* ------------------------------------------------
* define queries
* ------------------------------------------------*/
#define CHECK_TABLE_EXISTS "SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = '%s' AND tablename = '%s'"
#define CHECK_COLUMN_EXISTS "SELECT column_name FROM information_schema.columns WHERE table_schema = '%s' AND table_name = '%s' AND column_name = '%s'"
#define GET_DATABASE_INFO "select user, current_database() from pg_stat_activity where pg_stat_activity.pid=pg_backend_pid()"
#define GET_REPLICATION_SETTING "select current_setting('bw.bwpath') bwpath, current_setting('bw.kafka_broker') kafka_broker, current_setting('bw.schema_registry') schema_registry, current_setting('bw.consumer') consumer, current_setting('bw.consumer_sub') consumer_sub"
//#define INSERT_MAP_TABLE "INSERT INTO tbl_mapps SELECT A.table_catalog database_name, A.table_schema, A.table_name, B.relnamespace, B.oid reloid, A.table_catalog || '-' || A.table_name as topic_name, now() create_date, current_user create_user, '' remark FROM information_schema.tables A inner join pg_class B on A.table_name = B.relname where table_name='%s'"
// If the Postgres schema name is 'public', we just set topic_name with the table_name.
// schema = 'public' : topic_name = table name
// schema != 'public' : topic_name = table_schema.table name
#define INSERT_MAP_TABLE "INSERT INTO tbl_mapps (database_name, table_schema, table_name, relnamespace, reloid, topic_name, create_date, create_user, remark) \
SELECT A.table_catalog database_name, A.table_schema, \
A.table_name, B.relnamespace, B.oid reloid, \
(select case when A.table_schema = 'public' then A.table_name else A.table_schema || '.' || A.table_name end) as topic_name, \
now() create_date, current_user create_user, '' remark \
FROM information_schema.tables A inner join pg_class B on A.table_name = B.relname \
where table_name='%s' and A.table_schema = '%s' \
and B.oid = (SELECT '%s.%s'::regclass::oid) \
and not exists (select 1 from tbl_mapps C where C.table_name = '%s' and C.table_schema = '%s');"
#define INSERT_COLUMN_MAP_TABLE "INSERT INTO col_mapps (reloid, column_name, colseq, create_date, create_user, remark) \
SELECT B.oid, column_name, (select count(*)+1 from col_mapps where reloid = C.reloid) ord, now(), current_user, '%s' remark \
FROM information_schema.columns A inner join pg_class B on A.table_name = B.relname \
JOIN tbl_mapps C on B.oid = C.reloid \
WHERE A.table_schema = '%s' \
AND A.table_name = '%s' \
AND column_name = '%s'"
#define DELETE_MAP_TABLE "DELETE FROM tbl_mapps WHERE table_schema = '%s' and table_name = '%s'"
#define INSERT_KAFKA_CONFIG "INSERT INTO kafka_con_config VALUES(current_database(), '%s', '%s'::json->'config', now(), current_user, '')"
#define UPDATE_KAFKA_CONFIG "UPDATE kafka_con_config SET contents = '%s' WHERE connect_name = '%s'"
#define DELETE_KAFKA_CONFIG "DELETE from kafka_con_config"
#define GET_KAFKA_CONN_INFO "SELECT connect_name, contents FROM kafka_con_config"
#define GET_TOPIC_LIST_EXCEPT_PARAM "SELECT topic_name FROM tbl_mapps WHERE table_name != '%s'"
#define GET_TOPIC_LIST "SELECT topic_name FROM tbl_mapps"
#define GENERATE_TOPICS "select jsonb_set('%s'::jsonb, '{topics}', '\"%s\"'::jsonb)"
#define GENERATE_CONNECT_NAME "select jsonb_set(f1,'{\"config\", \"hdfs.url\"}', f2::jsonb) from (select result f1, to_json((result::json->>'config')::json->>'hdfs.url'||'%s') f2 from jsonb_set(('%s'::jsonb-'tasks')::jsonb#-'{\"config\",\"name\"}', '{name}', '\"%s\"'::jsonb) result) result2"
/* ------------------------------------------------
* structures
* ------------------------------------------------*/
typedef struct custom_config{
char bwpath[MAXPGPATH]; /* Bottledwater intalled path */
char broker[URLLEN]; /* Kafka broker URL */
char schema_registry[URLLEN]; /* Schema Registry URL */
char consumer[URLLEN]; /* Kafka connect URL */
char consumer_sub[URLLEN]; /* Kafka connect sub URL for default config */
} custom_config_t;
struct MemoryStruct {
char *memory;
size_t size;
};
/* ------------------------------------------------
* internal functions
* ------------------------------------------------*/
int check_bw_process(const char* db_name);
int control_process(const char* db_name, const char* db_user, const char* hostname, const char* conn_info, const custom_config_t* config, int mode, char* snapshot);
int check_exists_table(const char * schema_name, const char * table_name);
int check_exists_column(const char * schema_name, const char * table_name, const char * column_name);
int get_kafka_connect_info(char* conn_name, char *contents);
int remove_replication_slot(const char* db_name);
int update_mapping_table(const char * schema_name, const char * table_name, bool operation);
int update_mapping_column_table(const char *schema_name, const char *table_name, const char *column_name, const char *remark, bool operation);
int get_database_info(char *db_user, char *db_name);
int get_custom_config(custom_config_t *pconfig);
static size_t response_cb(void *contents, size_t size, size_t nmemb, void *userp);
pid_t spawn_bw_process(const char *cmdline);
int sync_with_kafka_connect(const char* db_name, const char* table_name, const char* conn_info, const custom_config_t* config);
int update_kafka_conn_info(unsigned char mode, const char *contents, const char *conn_name);
int http_get_kafka_connect(char *contents, const custom_config_t *config);
int http_set_kafka_connect(const char *conn_name, const char *contents, const custom_config_t *config);
int http_create_kafka_connect(const char *conn_name, const char *contents, const custom_config_t *config);
int http_delete_kafka_connect(const char *conn_name, const char *contents, const custom_config_t *config);
int get_topic_list(const char* conn_name, const char* table_name, char **topics);
int generate_contents(char *contents, char *key, char *value);
/* ------------------------------------------------
* Global variables
* ------------------------------------------------*/
static CURL * g_curl = NULL;
static struct curl_slist *g_curl_headers = NULL;
/* ------------------------------------------------
* Startup
* ------------------------------------------------*/
//void _PG_init(void)
//{
//// curl_global_init(CURL_GLOBAL_ALL);
//}
/* ------------------------------------------------
* Shutdown
* ------------------------------------------------*/
//void _PG_fini(void)
//{
//// if ( g_curl )
//// {
//// curl_slist_free_all(g_curl_headers);
//// curl_easy_cleanup(g_curl);
//// g_curl = NULL;
//// }
////
//// curl_global_cleanup();
//}
/**
* \addtogroup mapping table mapping API
* @{ */
/**
* @brief Add mapping table API
*
* Add table in database to internal repository <br>
*
* @param[in] schema_name table schema name
* @param[in] table_name table name
* @param[in] dbtype bigdata database type 1: HDFS 2: Hive
* @param[in] opratetype operation type 1: insert only 2: update only 4: delete only 7: all
* @param[in] remark [Unused currently]
* @return return string with error code (refer to error_string.h )<br>
* @see pg_del_ingest_table()
*/
/* ------------------------------------------------
* pg_add_ingest_table().
*
* Usage: select pg_add_ingest_table();
* ------------------------------------------------ */
Datum
pg_add_ingest_table(PG_FUNCTION_ARGS)
{
char *schema_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
char *table_name = text_to_cstring(PG_GETARG_TEXT_PP(1));
int ndbtype = PG_GETARG_INT32(2);
int noperate = PG_GETARG_INT32(3);
char *conn_info = text_to_cstring(PG_GETARG_TEXT_PP(4));
custom_config_t config;
char db_user[NAMEDATALEN];
char db_name[NAMEDATALEN];
// char hostname[NAMEDATALEN];
int ret = 0;
/* Check parameters */
/* check that the database type. */
if (ndbtype != 1)
DB_LOG_RETURN(ERROR, K_NOT_SUPPORT_DBTYPE, ret);
/* check that the operating type. */
if (noperate < 1 || noperate > 7)
DB_LOG_RETURN(ERROR, K_CHECK_OPERATE, ret);
CHECKMAXLEN(table_name, NAMEDATALEN);
CHECKMAXLEN(schema_name , NAMEDATALEN);
CHECKMAXLEN(conn_info, OPTLEN );
/* check option. to do ..*/
if (conn_info == NULL && !strlen(conn_info))
DB_LOG_RETURN(ERROR, K_CHECK_OPTION, ret);
/* check that the table exists. */
if((CHECK(ret, check_exists_table(schema_name, table_name))) < 0)
DB_LOG_RETURN(ERROR, K_TABLE_NOT_EXIST, ret);
/* Get custom config */
if((CHECK(ret, get_custom_config(&config))) < 0)
DB_LOG_RETURN(ERROR, K_SPI_ERR, ret);
/* Get database name and user id */
if((CHECK(ret, get_database_info(db_user, db_name))) < 0)
DB_LOG_RETURN(ERROR, K_SPI_ERR, ret);
/* update table name into bw_table_list */
if((CHECK(ret, update_mapping_table(schema_name, table_name, true))) < 0)
DB_LOG_RETURN(ERROR, K_SPI_ERR, ret);
// /* check and run the bw process */
// if((CHECK(ret, control_process(db_name, db_user, hostname, conn_info, &config, MODE_INSERT))) < 0)
// DB_LOG_RETURN(ERROR, K_FAILED_START, ret);
/* sync config with kafka connecta */
if(strlen(config.consumer))
if((CHECK(ret, sync_with_kafka_connect(db_name, NULL, conn_info, &config))) < 0)
DB_LOG_RETURN(LOG, K_SUCCESS, ret);
DB_LOG_RETURN(INFO, K_SUCCESS, ret);
}
/**
* @brief Add mapping table API
*
* Delete table in database from internal repository <br>
*
* @param[in] schema_name table schema name
* @param[in] table_name table name
* @return return string with error code (refer to error_string.h )<br>
* @see pg_add_ingest_table()
*/
/* ------------------------------------------------
* pg_del_ingest_table().
*
* Usage: select pg_del_ingest_table();
* ------------------------------------------------
*/
Datum
pg_del_ingest_table(PG_FUNCTION_ARGS)
{
char *schema_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
char *table_name = text_to_cstring(PG_GETARG_TEXT_PP(1));
char db_user[NAMEDATALEN];
char db_name[NAMEDATALEN];
custom_config_t config;
int ret = 0;
CHECKMAXLEN(table_name, NAMEDATALEN);
CHECKMAXLEN(schema_name, NAMEDATALEN);
/* check that the table exists.*/
if((CHECK(ret, check_exists_table(schema_name, table_name))) < 0)
DB_LOG_RETURN(ERROR, K_TABLE_NOT_EXIST, ret);
/* Get database name and user id */
if((CHECK(ret, get_database_info(db_user, db_name))) < 0)
DB_LOG_RETURN(ERROR, K_SPI_ERR, ret);
/* Get custom config */
if((CHECK(ret, get_custom_config(&config))) < 0)
DB_LOG_RETURN(ERROR, K_SPI_ERR, ret);
/* update table name into bw_table_list */
if((CHECK(ret, update_mapping_table(schema_name, table_name, false))) < 0)
DB_LOG_RETURN(ERROR, K_SPI_ERR, ret);
/* get config from kafka connect */
if(strlen(config.consumer))
ret = sync_with_kafka_connect(db_name, table_name, NULL, &config);
// /* check and send signal to reload conf */
// if((CHECK(ret, control_process(db_name, db_user, NULL, NULL, NULL, MODE_DELETE))) < 0)
// DB_LOG_RETURN(INFO, K_EVENT_FAIL, ret );
DB_LOG_RETURN(INFO, K_SUCCESS, ret);
}
/**
* \addtogroup mapping column API
* @{ */
/**
* @brief Add mapping column API
*
* Add column in database to internal repository <br>
*
* @param[in] schema_name table schema name
* @param[in] table_name table name
* @param[in] column_name column name
* @param[in] remark [Unused currently]
* @return return string with error code (refer to error_string.h )<br>
* @see pg_del_ingest_column()
*/
/* ------------------------------------------------
* pg_add_ingest_column().
*
* Usage: select pg_add_ingest_column();
* ------------------------------------------------ */
Datum
pg_add_ingest_column(PG_FUNCTION_ARGS)
{
char *schema_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
char *table_name = text_to_cstring(PG_GETARG_TEXT_PP(1));
char *column_name = text_to_cstring(PG_GETARG_TEXT_PP(2));
char *remark = text_to_cstring(PG_GETARG_TEXT_PP(3));
int ret = 0;
/* Check parameters */
/* check that the database type. */
CHECKMAXLEN(table_name, NAMEDATALEN);
CHECKMAXLEN(schema_name , NAMEDATALEN);
CHECKMAXLEN(column_name, NAMEDATALEN);
CHECKMAXLEN(remark, OPTLEN );
/* check that the column exists. */
if((CHECK(ret, check_exists_column(schema_name, table_name, column_name))) < 0)
DB_LOG_RETURN(ERROR, K_TABLE_NOT_EXIST, ret);
/* update table name into bw_table_list */
if((CHECK(ret, update_mapping_column_table(schema_name, table_name, column_name, remark, true))) < 0)
DB_LOG_RETURN(ERROR, K_SPI_ERR, ret);
DB_LOG_RETURN(INFO, K_SUCCESS, ret);
}
/**
* @brief Add mapping table API
*
* Delete table in database from internal repository <br>
*
* @param[in] schema_name table schema name
* @param[in] table_name table name
* @param[in] column_name column name
* @return return string with error code (refer to error_string.h )<br>
* @see pg_add_ingest_column()
*/
/* ------------------------------------------------
* pg_del_ingest_column().
*
* Usage: select pg_del_ingest_column();
* ------------------------------------------------
*/
Datum
pg_del_ingest_column(PG_FUNCTION_ARGS)
{
char *schema_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
char *table_name = text_to_cstring(PG_GETARG_TEXT_PP(1));
char *column_name = text_to_cstring(PG_GETARG_TEXT_PP(1));
int ret = 0;
CHECKMAXLEN(table_name, NAMEDATALEN);
CHECKMAXLEN(schema_name, NAMEDATALEN);
CHECKMAXLEN(column_name, NAMEDATALEN);
/* check that the column exists. */
if((CHECK(ret, check_exists_column(schema_name, table_name, column_name))) < 0)
DB_LOG_RETURN(ERROR, K_TABLE_NOT_EXIST, ret);
/* update table name into bw_table_list */
if((CHECK(ret, update_mapping_column_table(schema_name, table_name, column_name, NULL, false))) < 0)
DB_LOG_RETURN(ERROR, K_SPI_ERR, ret);
DB_LOG_RETURN(INFO, K_SUCCESS, ret);
}
/* @} */
/**
* \addtogroup control Control bottledwater interface engine API
* @{ */
/**
* @brief Resume bottledwater process API
*
* Start replication (spawning bottledwater process.) <br>
*
* @param[in] snapshot 0: skip taking a consistent snapshot of the existing
* @return return string with error code (refer to error_string.h )<br>
* @see pg_suspend_ingest()
*/
/* ------------------------------------------------
* pg_resume_ingest().
*
* Usage: select pg_resume_ingest();
* ------------------------------------------------ */
Datum
pg_resume_ingest(PG_FUNCTION_ARGS)
{
int snapshot = PG_GETARG_INT32(0);
char db_user[NAMEDATALEN];
char db_name[NAMEDATALEN];
int ret;
custom_config_t config;
/* Get custom config */
if((CHECK(ret, get_custom_config(&config))) < 0)
DB_LOG_RETURN(ERROR, K_SPI_ERR, ret);
/* Get database name and user id */
if((CHECK(ret, get_database_info(db_user, db_name))) < 0)
DB_LOG_RETURN(ERROR, K_SPI_ERR, ret);
/* resume process */
if((CHECK(ret, control_process(db_name, db_user, NULL, NULL, &config, MODE_RESUME, (!snapshot) ? "--skip-snapshot" : ""))) < 0)
DB_LOG_RETURN(ERROR, K_EVENT_FAIL,ret);
/* sync config with kafka connecta */
if(strlen(config.consumer))
if((CHECK(ret, sync_with_kafka_connect(db_name, NULL, NULL, &config))) < 0)
DB_LOG_RETURN(LOG, K_SUCCESS, ret);
DB_LOG_RETURN(INFO, K_SUCCESS, ret);
}
/**
* @brief Resume bottledwater process API
*
* Suspend replication (<br>
* Stop bottledwater process gracefully by sending SIGTERM) <br>
* drop replication <br>
*
* @return return string with error code (refer to error_string.h )<br>
* @see pg_suspend_ingest()
*/
/* ------------------------------------------------
* pg_suspend_ingest().
*
* Usage: select pg_suspend_ingest();
* ------------------------------------------------ */
Datum
pg_suspend_ingest(PG_FUNCTION_ARGS)
{
char db_user[NAMEDATALEN];
char db_name[NAMEDATALEN];
int ret;
/* Get database name and user id */
if((CHECK(ret, get_database_info(db_user, db_name))) < 0)
DB_LOG_RETURN(ERROR, K_SPI_ERR, ret);
/* suspend process and delete replication slot */
if((CHECK(ret, control_process(db_name, db_user, NULL, NULL, NULL, MODE_SUSPEND, NULL))) < 0)
DB_LOG_RETURN(ERROR, K_EVENT_FAIL, ret);
if((CHECK(ret, remove_replication_slot(db_name))) < 0)
DB_LOG_RETURN(ERROR, K_REMOVE_SLOT_ERR, ret);
DB_LOG_RETURN(INFO, K_SUCCESS, ret);
}
/* @} */
/**
* \addtogroup status Check status of bottledwater and replication.
* @{ */
/**
* @brief Check status of bottledwater and replication.
*
* Check status of bottledwater and replication. <br>
*
* @return return string with error code (refer to error_string.h )<br>
* @see pg_suspend_ingest()
* @see pg_resume_ingest()
*/
/* ------------------------------------------------
* pg_get_status_ingest().
*
* Usage: select pg_get_status_ingest();
* ------------------------------------------------ */
Datum
pg_get_status_ingest(PG_FUNCTION_ARGS)
{
char db_user[NAMEDATALEN];
char db_name[NAMEDATALEN];
int ret = 0 ;
/* Get database name and user id */
if((CHECK(ret, get_database_info(db_user, db_name))) < 0)
DB_LOG_RETURN(ERROR, K_SPI_ERR, ret);
/* Check process status */
if (check_bw_process(db_name) > 0)
DB_LOG_RETURN(INFO, K_PROC_WORKING, ret);
/* Check replication (to do..)*/
/* Check Kafka Status (to do..)*/
DB_LOG_RETURN(INFO, K_PROC_NOT_WORKING, ret);
}
/* @} */
/**
* \addtogroup kafka_connect Synchronize topics with Kafka-Connect.
* @{ */
/**
* @brief Create Kafka connect.
*
* HTTP Request creation Kafka connection. <br>
*
* @param[in] Kafka connect name
* @return return string with error code (refer to error_string.h )<br>
* @see pg_delete_kafka_connect()
*/
/* ------------------------------------------------
* pg_make_kafka_connect().
*
* Usage: pg_make_kafka_connect('connect name');
* ------------------------------------------------ */
Datum
pg_make_kafka_connect(PG_FUNCTION_ARGS)
{
char contents[CONTENTSDATALEN];
int ret = 0 ;
char *conn_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
custom_config_t config;
CHECKMAXLEN(conn_name, NAMEDATALEN);
/* Get custom config */
if((CHECK(ret, get_custom_config(&config))) < 0)
DB_LOG_RETURN(ERROR, K_SPI_ERR, ret);
if(!strlen(config.consumer))
DB_LOG_RETURN(ERROR, K_KAFKA_CONN_CONFIG, ECONFIG);
if((CHECK(ret, get_kafka_connect_info(conn_name, contents))) >= 0)
DB_LOG_RETURN(ERROR, K_EXIST , ret);
/* Get default config of Kafka connect */
if((CHECK(ret, http_get_kafka_connect(contents, &config))) < 0)
DB_LOG_RETURN(ERROR, K_KAFKA_CONN_ERR, ret);
/* Generate json data to request creation */
if((CHECK(ret, generate_contents(contents, "name", conn_name))) < 0)
DB_LOG_RETURN(ERROR, K_SPI_ERR, ret);
/* Create Kafka connect by connect_name */
if((CHECK(ret, http_create_kafka_connect(conn_name, contents, &config))) < 0)
DB_LOG_RETURN(ERROR, K_KAFKA_CONN_ERR, ret);
if((CHECK(ret, update_kafka_conn_info(MODE_INSERT, contents, conn_name))) < 0)
DB_LOG_RETURN(INFO, K_SPI_ERR, ret);
DB_LOG_RETURN(INFO, K_SUCCESS , ret);
}
/**
* @brief Delete Kafka connect.
*
* HTTP Request creation Kafka connection. <br>
*
* @param[in] Kafka connect name
* @return return string with error code (refer to error_string.h )<br>
* @see pg_make_kafka_connect()
*/
/* ------------------------------------------------
* pg_delete_kafka_connect().
*
* Usage: pg_delete_kafka_connect('connect name');
* ------------------------------------------------ */
Datum
pg_delete_kafka_connect(PG_FUNCTION_ARGS)
{
char contents[CONTENTSDATALEN];
int ret = 0 ;
char *conn_name = text_to_cstring(PG_GETARG_TEXT_PP(0));
custom_config_t config;
CHECKMAXLEN(conn_name, NAMEDATALEN);
/* Get custom config */
if((CHECK(ret, get_custom_config(&config))) < 0)
DB_LOG_RETURN(ERROR, K_SPI_ERR, ret);
if(!strlen(config.consumer))
DB_LOG_RETURN(ERROR, K_KAFKA_CONN_CONFIG, ECONFIG);
/* Generate json data to request creation */
if((CHECK(ret, get_kafka_connect_info(conn_name, contents))) < 0)
DB_LOG_RETURN(ERROR, K_NOT_EXIST , ret);
/* Create Kafka connect by connect_name */
if((CHECK(ret, http_delete_kafka_connect(conn_name, contents, &config))) < 0)
DB_LOG_RETURN(ERROR, K_KAFKA_CONN_ERR, ret);
if((CHECK(ret, update_kafka_conn_info(MODE_DELETE, contents, conn_name))) < 0)
DB_LOG_RETURN(INFO, K_SPI_ERR, ret);
DB_LOG_RETURN(INFO, K_SUCCESS , ret);
}
/* @} */
/* ----------------------------
* Check bottledwater process,
* replication slot, kafka connect status
* ---------------------------- */
int check_bw_process(const char* db_name)
{
char tmppath[MAXPGPATH];
char pidbuf[32];
FILE *pidfp = NULL;
// struct stat st;
snprintf(tmppath, sizeof(tmppath)-1, "/tmp/bw_%s.pid", db_name);
if ((pidfp = fopen(tmppath, "r")) == NULL){
return 0; //process is not running
}
if (!fgets(pidbuf, sizeof(pidbuf), pidfp)){
fclose(pidfp);
return -1; //abnormal state - process started abnormally
}
fclose(pidfp);
snprintf(tmppath, sizeof(tmppath), "/proc/%s/status", pidbuf);
if((pidfp = fopen(tmppath, "r"))){
fclose(pidfp);
return atoi(pidbuf);
}
return -1;
}
/* ----------------------------
* Spawn bottledwater
* Send signal to reload table list
* ---------------------------- */
int control_process(const char* db_name, const char* db_user, const char* hostname, const char* conn_info, const custom_config_t* config, int mode, char* snapshot)
{
int ret = 0;
char conn_name[NAMEDATALEN];
char command[MAXPGPATH];
pid_t pid = check_bw_process(db_name);
if(pid <= 0) {
switch (mode){
case MODE_INSERT:
case MODE_RESUME:
if(pid<=0){ //process is not running
if(!strlen(config->consumer)){
if(strlen(db_name) < NAMEDATALEN) strcpy(conn_name, db_name);}
else{
if((CHECK(ret, get_kafka_connect_info(conn_name, NULL))) < 0)
if(strlen(db_name) < NAMEDATALEN) strcpy(conn_name, db_name);
}
snprintf(command, sizeof(command),
"nohup %s --postgres=postgres://%[email protected]/%s --slot=%s --broker=%s --schema-registry=%s --topic-prefix=%s %s --allow-unkeyed --on-error=log 1>/dev/null 2>&1 &",
config->bwpath, db_user, db_name, db_name, config->broker, config->schema_registry, conn_name, snapshot);
// snprintf(command, sizeof(command), "nohup /home/postgres/install/bottledwater-pg-master/kafka/bottledwater --postgres=postgres://localhost/protoavro --slot=protoavro --broker=K4M-KAFKA-1:9092 --schema-registry=http://K4M-KAFKA-1:8081 1>/dev/null/ 2>&1 &");
CHECK_RETURN(ret, spawn_bw_process(command));
}
break;
default:
return EUKNOWN ;
}
}
else {
if(mode == MODE_SUSPEND)
kill(pid, SIGTERM); //send signal to process - reload configuration
else{
kill(pid, SIGQUIT); //send signal to process - reload configuration
}
}
return 0;
}
/*----------------------------
* Check user input table name.
*---------------------------- */
int check_exists_table(const char * schema_name, const char * table_name)
{
char sql[QBUFFLEN];
int ret = 0;
SPI_connect();
snprintf(sql, sizeof(sql), CHECK_TABLE_EXISTS, schema_name, table_name);
ret = SPI_exec(sql, 1);
if (ret != SPI_OK_SELECT || SPI_tuptable == NULL || SPI_processed <= 0) {
SPI_finish();
return ESPI;
}
SPI_finish();
return 0;
}
/*----------------------------
* Check user input column name.
*---------------------------- */
int check_exists_column(const char * schema_name, const char * table_name, const char * column_name)
{
char sql[QBUFFLEN];
int ret = 0;
SPI_connect();
snprintf(sql, sizeof(sql), CHECK_COLUMN_EXISTS, schema_name, table_name, column_name);
ret = SPI_exec(sql, 1);
if (ret != SPI_OK_SELECT || SPI_tuptable == NULL || SPI_processed <= 0) {
SPI_finish();
return ESPI;
}
SPI_finish();
return 0;
}
/*----------------------------
* Add/Delete replication tables.
*---------------------------- */
int update_mapping_table(const char * schema_name, const char * table_name, bool operation)
{
char sql[QBUFFLEN];
int i = 0;
int ret = 0;
SPI_connect();
if(operation)
snprintf(sql, QBUFFLEN-1, INSERT_MAP_TABLE, table_name, schema_name, schema_name, table_name, table_name, schema_name);
else
snprintf(sql, QBUFFLEN-1, DELETE_MAP_TABLE, schema_name, table_name);
ret = SPI_exec(sql, 1);
if(ret != SPI_OK_DELETE && ret != SPI_OK_INSERT){
for(i = 0; i < 10; i++)
ret = ESPI;
}
if(SPI_processed < 1){
ret = ENEXIST ;
}
SPI_finish();
return ret;
}
/*----------------------------
* Add/Delete replication column.
*---------------------------- */
int update_mapping_column_table(const char *schema_name, const char *table_name, const char *column_name, const char *remark, bool operation)
{
char sql[QBUFFLEN];
int i = 0;
int ret = 0;
SPI_connect();
if(operation)
snprintf(sql, QBUFFLEN-1, INSERT_COLUMN_MAP_TABLE, remark, schema_name, table_name, column_name);
else
snprintf(sql, QBUFFLEN-1, DELETE_MAP_TABLE, schema_name, table_name);
ret = SPI_exec(sql, 1);
if(ret != SPI_OK_DELETE && ret != SPI_OK_INSERT){
for(i = 0; i < 10; i++)
ret = ESPI;
}
if(SPI_processed < 1){
ret = ENEXIST ;
}
SPI_finish();
return ret;
}
/*----------------------------
* Remove replication slot
*---------------------------- */
int remove_replication_slot(const char* db_name)
{
char sql[QBUFFLEN];
int ret = 0, i = 3;
while ((check_bw_process(db_name) > 0) && i-- > 0){
sleep(1);
}
SPI_connect();
snprintf(sql, sizeof(sql), "select pg_drop_replication_slot('%s')", db_name);
ret = SPI_exec(sql, 1);
if (ret != SPI_OK_SELECT || SPI_tuptable == NULL || SPI_processed <= 0) {
SPI_finish();
return ESPI;
}
SPI_finish();
return 0;
}
/*----------------------------
* Get database info
*---------------------------- */
int get_database_info(char *db_user, char *db_name)
{
char sql[QBUFFLEN];
int ret = 0;
char *pdb_user;
char *pdb_name;
SPI_connect();
snprintf(sql, sizeof(sql), GET_DATABASE_INFO);
ret = SPI_exec(sql, 1);
if (ret > 0 && SPI_tuptable != NULL && SPI_processed > 0){
TupleDesc tupdesc = SPI_tuptable->tupdesc;
SPITupleTable *tuptable = SPI_tuptable;
HeapTuple tuple = tuptable->vals[0];
pdb_user = SPI_getvalue(tuple, tupdesc, 1);