|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "github.com/ardanlabs/conf" |
| 9 | + "github.com/elastic/go-elasticsearch/v8" |
| 10 | + "github.com/pkg/errors" |
| 11 | + "github.com/qubic/go-archiver/lts/tx" |
| 12 | + "github.com/qubic/go-archiver/protobuff" |
| 13 | + "go.uber.org/zap" |
| 14 | + "go.uber.org/zap/zapcore" |
| 15 | + "google.golang.org/grpc" |
| 16 | + "google.golang.org/grpc/credentials/insecure" |
| 17 | + "log" |
| 18 | + "net/http" |
| 19 | + "os" |
| 20 | + "time" |
| 21 | +) |
| 22 | + |
| 23 | +const prefix = "QUBIC_ARCHIVER" |
| 24 | + |
| 25 | +func main() { |
| 26 | + if err := run(); err != nil { |
| 27 | + log.Fatalf("main: exited with error: %s", err.Error()) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func run() error { |
| 32 | + config := zap.NewProductionConfig() |
| 33 | + // this is just for sugar, to display a readable date instead of an epoch time |
| 34 | + config.EncoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout(time.DateTime) |
| 35 | + |
| 36 | + logger, err := config.Build() |
| 37 | + if err != nil { |
| 38 | + fmt.Errorf("creating logger: %v", err) |
| 39 | + } |
| 40 | + defer logger.Sync() |
| 41 | + sLogger := logger.Sugar() |
| 42 | + |
| 43 | + var cfg struct { |
| 44 | + InternalStoreFolder string `conf:"default:store"` |
| 45 | + ArchiverHost string `conf:"default:127.0.0.1:6001"` |
| 46 | + ArchiverReadTimeout time.Duration `conf:"default:10s"` |
| 47 | + ElasticSearchAddress string `conf:"default:http://127.0.0.1:9200"` |
| 48 | + ElasticSearchWriteTimeout time.Duration `conf:"default:5m"` |
| 49 | + BatchSize int `conf:"default:10000"` |
| 50 | + NrWorkers int `conf:"default:20"` |
| 51 | + } |
| 52 | + |
| 53 | + if err := conf.Parse(os.Args[1:], prefix, &cfg); err != nil { |
| 54 | + switch err { |
| 55 | + case conf.ErrHelpWanted: |
| 56 | + usage, err := conf.Usage(prefix, &cfg) |
| 57 | + if err != nil { |
| 58 | + return fmt.Errorf("generating config usage: %v", err) |
| 59 | + } |
| 60 | + fmt.Println(usage) |
| 61 | + return nil |
| 62 | + case conf.ErrVersionWanted: |
| 63 | + version, err := conf.VersionString(prefix, &cfg) |
| 64 | + if err != nil { |
| 65 | + return fmt.Errorf("generating config version: %v", err) |
| 66 | + } |
| 67 | + fmt.Println(version) |
| 68 | + return nil |
| 69 | + } |
| 70 | + return fmt.Errorf("parsing config: %v", err) |
| 71 | + } |
| 72 | + |
| 73 | + out, err := conf.String(&cfg) |
| 74 | + if err != nil { |
| 75 | + return fmt.Errorf("generating config for output: %v", err) |
| 76 | + } |
| 77 | + log.Printf("main: Config :\n%v\n", out) |
| 78 | + |
| 79 | + store, err := tx.NewProcessorStore(cfg.InternalStoreFolder) |
| 80 | + if err != nil { |
| 81 | + return fmt.Errorf("creating processor store: %v", err) |
| 82 | + } |
| 83 | + |
| 84 | + archiverConn, err := grpc.NewClient(cfg.ArchiverHost, grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 85 | + if err != nil { |
| 86 | + return fmt.Errorf("creating archiver connection: %v", err) |
| 87 | + } |
| 88 | + |
| 89 | + archiverClient := protobuff.NewArchiveServiceClient(archiverConn) |
| 90 | + |
| 91 | + esInserter, err := NewElasticSearchTxsInserter(cfg.ElasticSearchAddress, "transactions", cfg.ElasticSearchWriteTimeout) |
| 92 | + if err != nil { |
| 93 | + return fmt.Errorf("creating elasticsearch tx inserter: %v", err) |
| 94 | + } |
| 95 | + |
| 96 | + proc, err := tx.NewProcessor(store, archiverClient, esInserter, cfg.BatchSize, sLogger, cfg.ArchiverReadTimeout, cfg.ElasticSearchWriteTimeout) |
| 97 | + if err != nil { |
| 98 | + return fmt.Errorf("creating processor: %v", err) |
| 99 | + } |
| 100 | + |
| 101 | + err = proc.Start(cfg.NrWorkers) |
| 102 | + if err != nil { |
| 103 | + return fmt.Errorf("starting processor: %v", err) |
| 104 | + } |
| 105 | + |
| 106 | + return nil |
| 107 | +} |
| 108 | + |
| 109 | +type ElasticSearchTxsInserter struct { |
| 110 | + port string |
| 111 | + index string |
| 112 | + esClient *elasticsearch.Client |
| 113 | +} |
| 114 | + |
| 115 | +func NewElasticSearchTxsInserter(address, index string, timeout time.Duration) (*ElasticSearchTxsInserter, error) { |
| 116 | + cfg := elasticsearch.Config{ |
| 117 | + Addresses: []string{address}, |
| 118 | + Transport: &http.Transport{ |
| 119 | + MaxIdleConnsPerHost: 10, |
| 120 | + ResponseHeaderTimeout: timeout, |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + esClient, err := elasticsearch.NewClient(cfg) |
| 125 | + if err != nil { |
| 126 | + return nil, fmt.Errorf("creating elasticsearch client: %v", err) |
| 127 | + } |
| 128 | + |
| 129 | + return &ElasticSearchTxsInserter{ |
| 130 | + index: index, |
| 131 | + esClient: esClient, |
| 132 | + }, nil |
| 133 | +} |
| 134 | + |
| 135 | +func (es *ElasticSearchTxsInserter) PushSingleTx(ctx context.Context, tx tx.Tx) error { |
| 136 | + return errors.New("not implemented") |
| 137 | +} |
| 138 | + |
| 139 | +func (es *ElasticSearchTxsInserter) PushMultipleTx(ctx context.Context, txs []tx.Tx) error { |
| 140 | + var buf bytes.Buffer |
| 141 | + |
| 142 | + for _, tx := range txs { |
| 143 | + // Metadata line for each document |
| 144 | + meta := []byte(fmt.Sprintf(`{ "index": { "_index": "%s", "_id": "%s" } }%s`, es.index, tx.TxID, "\n")) |
| 145 | + buf.Write(meta) |
| 146 | + |
| 147 | + // Serialize the transaction to JSON |
| 148 | + data, err := json.Marshal(tx) |
| 149 | + if err != nil { |
| 150 | + return fmt.Errorf("error serializing transaction: %w", err) |
| 151 | + } |
| 152 | + buf.Write(data) |
| 153 | + buf.Write([]byte("\n")) // Add a newline between documents |
| 154 | + } |
| 155 | + |
| 156 | + // Send the bulk request |
| 157 | + res, err := es.esClient.Bulk(bytes.NewReader(buf.Bytes()), es.esClient.Bulk.WithRefresh("true")) |
| 158 | + if err != nil { |
| 159 | + return fmt.Errorf("bulk request failed: %w", err) |
| 160 | + } |
| 161 | + defer res.Body.Close() |
| 162 | + |
| 163 | + // Check response for errors |
| 164 | + if res.IsError() { |
| 165 | + return fmt.Errorf("bulk request error: %s", res.String()) |
| 166 | + } |
| 167 | + |
| 168 | + return nil |
| 169 | +} |
0 commit comments