99 "fmt"
1010 "io"
1111 "net/http"
12- "quesma/backend_connectors"
13- "quesma/ingest"
1412 "quesma/logger"
1513 "quesma/queryparser"
1614 "quesma/quesma/config"
@@ -20,77 +18,57 @@ import (
2018)
2119
2220// handleDocIndex assembles the payload into bulk format to reusing existing logic of bulk ingest
23- func handleDocIndex (payload types.JSON , targetTableName string , temporaryIngestProcessor * ingest. IngestProcessor2 , indexConfig config. QuesmaProcessorConfig ) (bulkmodel.BulkItem , error ) {
21+ func ( p * ElasticsearchToClickHouseIngestProcessor ) handleDocIndex (payload types.JSON , targetTableName string ) (bulkmodel.BulkItem , error ) {
2422 newPayload := []types.JSON {
2523 map [string ]interface {}{"index" : map [string ]interface {}{"_index" : targetTableName }},
2624 payload ,
2725 }
2826
29- if results , err := Write (context .Background (), & targetTableName , newPayload , temporaryIngestProcessor , nil , indexConfig ); err != nil {
27+ if results , err := p . Write (context .Background (), & targetTableName , newPayload ); err != nil {
3028 return bulkmodel.BulkItem {}, err
3129 } else {
3230 return results [0 ], nil
3331 }
3432}
3533
36- func handleBulkIndex (payload types.NDJSON , targetTableName string , temporaryIngestProcessor * ingest. IngestProcessor2 , es * backend_connectors. ElasticsearchBackendConnector , cfg config. QuesmaProcessorConfig ) ([]bulkmodel.BulkItem , error ) {
37- results , err := Write (context .Background (), & targetTableName , payload , temporaryIngestProcessor , es , cfg )
34+ func ( p * ElasticsearchToClickHouseIngestProcessor ) handleBulkIndex (payload types.NDJSON , targetTableName string ) ([]bulkmodel.BulkItem , error ) {
35+ results , err := p . Write (context .Background (), & targetTableName , payload )
3836 if err != nil {
3937 fmt .Printf ("failed writing: %v" , err )
4038 return []bulkmodel.BulkItem {}, err
4139 }
4240 return results , nil
4341}
4442
45- func Write (ctx context.Context , defaultIndex * string , bulk types.NDJSON , ip * ingest. IngestProcessor2 , es * backend_connectors. ElasticsearchBackendConnector , conf config. QuesmaProcessorConfig ) (results []bulkmodel.BulkItem , err error ) {
43+ func ( p * ElasticsearchToClickHouseIngestProcessor ) Write (ctx context.Context , defaultIndex * string , bulk types.NDJSON ) (results []bulkmodel.BulkItem , err error ) {
4644 defer recovery .LogPanic ()
4745
4846 bulkSize := len (bulk ) / 2 // we divided payload by 2 so that we don't take into account the `action_and_meta_data` line, ref: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html
4947
5048 // The returned results should be in the same order as the input request, however splitting the bulk might change the order.
5149 // Therefore, each BulkRequestEntry has a corresponding pointer to the result entry, allowing us to freely split and reshuffle the bulk.
52- results , clickhouseDocumentsToInsert , elasticRequestBody , elasticBulkEntries , err := splitBulk (ctx , defaultIndex , bulk , bulkSize , conf )
50+ results , clickhouseDocumentsToInsert , elasticRequestBody , elasticBulkEntries , err := splitBulk (ctx , defaultIndex , bulk , bulkSize , p . config )
5351 if err != nil {
5452 return []bulkmodel.BulkItem {}, err
5553 }
5654
57- // we fail if there are some documents to insert into Clickhouse but ingest processor is not available
58- //if len(clickhouseDocumentsToInsert) > 0 && ip == nil {
59- //
60- // indexes := make(map[string]struct{})
61- // for index := range clickhouseDocumentsToInsert {
62- // indexes[index] = struct{}{}
63- // }
64- //
65- // indexesAsList := make([]string, 0, len(indexes))
66- // for index := range indexes {
67- // indexesAsList = append(indexesAsList, index)
68- // }
69- // sort.Strings(indexesAsList)
70- //
71- // return []BulkItem{}, end_user_errors.ErrNoIngest.New(fmt.Errorf("ingest processor is not available, but documents are targeted to Clickhouse indexes: %s", strings.Join(indexesAsList, ",")))
72- //}
73-
74- // No place for that here
75- err = sendToElastic (elasticRequestBody , elasticBulkEntries , es )
55+ err = p .sendToElastic (elasticRequestBody , elasticBulkEntries )
7656 if err != nil {
7757 return []bulkmodel.BulkItem {}, err
7858 }
7959
80- //if ip != nil {
81- fmt .Printf ("woudl send to clickhouse: [%v]" , clickhouseDocumentsToInsert )
82- sendToClickhouse (ctx , clickhouseDocumentsToInsert , ip )
83- //}
60+ fmt .Printf ("would send to clickhouse: [%v]\n " , clickhouseDocumentsToInsert )
61+ p .sendToClickhouse (ctx , clickhouseDocumentsToInsert )
8462
8563 return results , nil
8664}
8765
88- func sendToElastic (elasticRequestBody []byte , elasticBulkEntries []BulkRequestEntry , es * backend_connectors. ElasticsearchBackendConnector ) error {
66+ func ( p * ElasticsearchToClickHouseIngestProcessor ) sendToElastic (elasticRequestBody []byte , elasticBulkEntries []BulkRequestEntry ) error {
8967 if len (elasticRequestBody ) == 0 {
9068 return nil
9169 }
9270
93- response , err := es . RequestWithHeaders (context .Background (), "POST" , "/_bulk" , elasticRequestBody , http.Header {"Content-Type" : {"application/x-ndjson" }})
71+ response , err := p . legacyIngestProcessor . RequestToElasticsearch (context .Background (), "POST" , "/_bulk" , elasticRequestBody , http.Header {"Content-Type" : {"application/x-ndjson" }})
9472 if err != nil {
9573 return err
9674 }
@@ -118,7 +96,7 @@ func sendToElastic(elasticRequestBody []byte, elasticBulkEntries []BulkRequestEn
11896 return nil
11997}
12098
121- func sendToClickhouse (ctx context.Context , clickhouseDocumentsToInsert map [string ][]BulkRequestEntry , ip * ingest. IngestProcessor2 ) {
99+ func ( p * ElasticsearchToClickHouseIngestProcessor ) sendToClickhouse (ctx context.Context , clickhouseDocumentsToInsert map [string ][]BulkRequestEntry ) {
122100 for indexName , documents := range clickhouseDocumentsToInsert {
123101 //phoneHomeAgent.IngestCounters().Add(indexName, int64(len(documents)))
124102
@@ -127,16 +105,17 @@ func sendToClickhouse(ctx context.Context, clickhouseDocumentsToInsert map[strin
127105 //}
128106 // if the index is mapped to specified database table in the configuration, use that table
129107 // TODO: Index name override ignored for now
130- //if len(cfg.IndexConfig[indexName].Override) > 0 {
131- // indexName = cfg.IndexConfig[indexName].Override
132- //}
108+
109+ if len (p .config .IndexConfig [indexName ].Override ) > 0 {
110+ indexName = p .config .IndexConfig [indexName ].Override
111+ }
133112
134113 inserts := make ([]types.JSON , len (documents ))
135114 for i , document := range documents {
136115 inserts [i ] = document .document
137116 }
138117
139- err := ip .Ingest (ctx , indexName , inserts )
118+ err := p . legacyIngestProcessor .Ingest (ctx , indexName , inserts )
140119
141120 for _ , document := range documents {
142121 bulkSingleResponse := bulkmodel.BulkSingleResponse {
0 commit comments