2024-03-22 18:05:47 +08:00
|
|
|
package xes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-03-26 17:16:11 +08:00
|
|
|
"net"
|
2024-03-22 18:05:47 +08:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2024-03-26 17:23:10 +08:00
|
|
|
|
|
|
|
elastic "github.com/elastic/go-elasticsearch/v7"
|
|
|
|
"github.com/elastic/go-elasticsearch/v7/esapi"
|
|
|
|
"github.com/elastic/go-elasticsearch/v7/esutil"
|
|
|
|
"github.com/loveuer/esgo2dump/internal/interfaces"
|
|
|
|
"github.com/loveuer/esgo2dump/internal/opt"
|
|
|
|
"github.com/loveuer/esgo2dump/internal/util"
|
|
|
|
"github.com/sirupsen/logrus"
|
2024-03-22 18:05:47 +08:00
|
|
|
)
|
|
|
|
|
2024-03-27 17:44:01 +08:00
|
|
|
func NewClient(url *url.URL, iot interfaces.IO) (interfaces.DumpIO, error) {
|
2024-03-22 18:05:47 +08:00
|
|
|
|
|
|
|
var (
|
2024-03-27 17:44:01 +08:00
|
|
|
address = fmt.Sprintf("%s://%s", url.Scheme, url.Host)
|
|
|
|
urlIndex = strings.TrimPrefix(url.Path, "/")
|
|
|
|
urlUsername string
|
|
|
|
urlPassword string
|
|
|
|
errCh = make(chan error)
|
|
|
|
cliCh = make(chan *elastic.Client)
|
2024-03-22 18:05:47 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
if url.User != nil {
|
2024-03-27 17:44:01 +08:00
|
|
|
urlUsername = url.User.Username()
|
2024-03-22 18:05:47 +08:00
|
|
|
if p, ok := url.User.Password(); ok {
|
2024-03-27 17:44:01 +08:00
|
|
|
urlPassword = p
|
2024-03-22 18:05:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-27 17:44:01 +08:00
|
|
|
logrus.Debugf("xes.NewClient: endpoint=%s index=%s (username=%s password=%s)", address, urlIndex, urlUsername, urlPassword)
|
2024-03-22 18:05:47 +08:00
|
|
|
|
2024-03-27 17:44:01 +08:00
|
|
|
if urlIndex == "" {
|
2024-03-22 18:05:47 +08:00
|
|
|
return nil, fmt.Errorf("please specify index name: (like => http://127.0.0.1:9200/my_index)")
|
|
|
|
}
|
|
|
|
|
2024-03-27 17:44:01 +08:00
|
|
|
ncFunc := func(endpoints []string, username, password, index string) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
cli *elastic.Client
|
|
|
|
infoResp *esapi.Response
|
|
|
|
)
|
|
|
|
|
|
|
|
if cli, err = elastic.NewClient(
|
|
|
|
elastic.Config{
|
|
|
|
Addresses: endpoints,
|
|
|
|
Username: username,
|
|
|
|
Password: password,
|
|
|
|
CACert: nil,
|
|
|
|
RetryOnStatus: []int{429},
|
|
|
|
MaxRetries: 3,
|
|
|
|
RetryBackoff: nil,
|
|
|
|
Transport: &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
DialContext: (&net.Dialer{Timeout: 10 * time.Second}).DialContext,
|
|
|
|
},
|
2024-03-22 18:05:47 +08:00
|
|
|
},
|
2024-03-27 17:44:01 +08:00
|
|
|
); err != nil {
|
|
|
|
logrus.Debugf("xes.NewClient: elastic new client with endpont=%s err=%v", endpoints, err)
|
|
|
|
errCh <- err
|
|
|
|
return
|
|
|
|
}
|
2024-03-22 18:05:47 +08:00
|
|
|
|
2024-03-27 17:44:01 +08:00
|
|
|
if infoResp, err = cli.Info(); err != nil {
|
|
|
|
logrus.Debugf("xes.NewClient: ping err=%v", err)
|
|
|
|
errCh <- err
|
|
|
|
return
|
|
|
|
}
|
2024-03-22 18:05:47 +08:00
|
|
|
|
2024-03-27 17:44:01 +08:00
|
|
|
if infoResp.StatusCode != 200 {
|
|
|
|
err = fmt.Errorf("info xes status=%d", infoResp.StatusCode)
|
|
|
|
logrus.Debugf("xes.NewClient: status err=%v", err)
|
|
|
|
errCh <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cliCh <- cli
|
2024-03-22 18:05:47 +08:00
|
|
|
}
|
|
|
|
|
2024-03-27 17:44:01 +08:00
|
|
|
go ncFunc([]string{address}, urlUsername, urlPassword, urlIndex)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-util.Timeout(10).Done():
|
|
|
|
return nil, fmt.Errorf("dial es=%s err=%v", address, context.DeadlineExceeded)
|
|
|
|
case c := <-cliCh:
|
|
|
|
return &client{c: c, index: urlIndex, iot: iot}, nil
|
|
|
|
case e := <-errCh:
|
|
|
|
return nil, e
|
|
|
|
}
|
2024-03-22 18:05:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type client struct {
|
|
|
|
c *elastic.Client
|
|
|
|
iot interfaces.IO
|
|
|
|
index string
|
|
|
|
scrollId string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *client) checkResponse(r *esapi.Response) error {
|
|
|
|
if r.StatusCode == 200 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("status=%d msg=%s", r.StatusCode, r.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *client) IOType() interfaces.IO {
|
|
|
|
return c.iot
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *client) IsFile() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *client) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-27 17:44:01 +08:00
|
|
|
func (c *client) ResetOffset() {
|
|
|
|
c.scrollId = ""
|
|
|
|
}
|
2024-03-22 18:05:47 +08:00
|
|
|
func (c *client) WriteData(ctx context.Context, docs []*interfaces.ESSource) (int, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
indexer esutil.BulkIndexer
|
|
|
|
count int
|
2024-03-26 18:10:19 +08:00
|
|
|
be error
|
2024-03-22 18:05:47 +08:00
|
|
|
)
|
|
|
|
if indexer, err = esutil.NewBulkIndexer(esutil.BulkIndexerConfig{
|
|
|
|
Client: c.c,
|
|
|
|
Index: c.index,
|
|
|
|
Refresh: "",
|
|
|
|
}); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, doc := range docs {
|
|
|
|
var bs []byte
|
|
|
|
|
|
|
|
if bs, err = json.Marshal(doc.Content); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Debugf("xes.Write: doc content=%s", string(bs))
|
|
|
|
|
|
|
|
if err = indexer.Add(context.Background(), esutil.BulkIndexerItem{
|
|
|
|
Action: "index",
|
|
|
|
Index: c.index,
|
|
|
|
DocumentID: doc.DocId,
|
|
|
|
Body: bytes.NewReader(bs),
|
2024-03-26 18:10:19 +08:00
|
|
|
OnFailure: func(ctx context.Context, item esutil.BulkIndexerItem, item2 esutil.BulkIndexerResponseItem, bulkErr error) {
|
|
|
|
be = bulkErr
|
|
|
|
},
|
2024-03-22 18:05:47 +08:00
|
|
|
}); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = indexer.Close(util.TimeoutCtx(ctx, opt.Timeout)); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2024-03-26 18:10:19 +08:00
|
|
|
if be != nil {
|
|
|
|
return 0, be
|
|
|
|
}
|
|
|
|
|
2024-03-22 18:05:47 +08:00
|
|
|
stats := indexer.Stats()
|
|
|
|
if stats.NumFailed > 0 {
|
2024-03-26 18:10:19 +08:00
|
|
|
return count, fmt.Errorf("write to xes failed_count=%d bulk_count=%d", stats.NumFailed, count)
|
2024-03-22 18:05:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return count, nil
|
|
|
|
}
|
|
|
|
|
2024-03-27 17:44:01 +08:00
|
|
|
func (c *client) ReadData(ctx context.Context, i int, query map[string]any) ([]*interfaces.ESSource, error) {
|
2024-03-22 18:05:47 +08:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
resp *esapi.Response
|
|
|
|
result = new(interfaces.ESResponse)
|
|
|
|
)
|
|
|
|
|
|
|
|
if c.scrollId == "" {
|
|
|
|
qs := []func(*esapi.SearchRequest){
|
|
|
|
c.c.Search.WithContext(util.TimeoutCtx(ctx, opt.Timeout)),
|
|
|
|
c.c.Search.WithIndex(c.index),
|
|
|
|
c.c.Search.WithSize(i),
|
|
|
|
c.c.Search.WithFrom(0),
|
|
|
|
c.c.Search.WithScroll(time.Duration(opt.ScrollDurationSeconds) * time.Second),
|
|
|
|
}
|
|
|
|
|
2024-03-27 17:44:01 +08:00
|
|
|
if query != nil && len(query) > 0 {
|
|
|
|
queryBs, _ := json.Marshal(map[string]any{"query": query})
|
2024-03-22 18:05:47 +08:00
|
|
|
qs = append(qs, c.c.Search.WithBody(bytes.NewReader(queryBs)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp, err = c.c.Search(qs...); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return nil, fmt.Errorf(resp.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(resp.Body)
|
|
|
|
if err = decoder.Decode(result); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.scrollId = result.ScrollId
|
|
|
|
|
|
|
|
return result.Hits.Hits, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp, err = c.c.Scroll(
|
|
|
|
c.c.Scroll.WithScrollID(c.scrollId),
|
|
|
|
c.c.Scroll.WithScroll(time.Duration(opt.ScrollDurationSeconds)*time.Second),
|
|
|
|
); err != nil {
|
|
|
|
return result.Hits.Hits, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(resp.Body)
|
|
|
|
if err = decoder.Decode(result); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.Hits.Hits, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *client) ReadMapping(ctx context.Context) (map[string]any, error) {
|
|
|
|
r, err := c.c.Indices.GetMapping(
|
|
|
|
c.c.Indices.GetMapping.WithIndex(c.index),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.StatusCode != 200 {
|
|
|
|
return nil, fmt.Errorf("status=%d, msg=%s", r.StatusCode, r.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
m := make(map[string]any)
|
|
|
|
decoder := json.NewDecoder(r.Body)
|
|
|
|
if err = decoder.Decode(&m); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
func (c *client) WriteMapping(ctx context.Context, m map[string]any) error {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
bs []byte
|
|
|
|
result *esapi.Response
|
|
|
|
)
|
|
|
|
|
|
|
|
for idxKey := range m {
|
|
|
|
if bs, err = json.Marshal(m[idxKey]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if result, err = c.c.Indices.Create(
|
|
|
|
c.index,
|
|
|
|
c.c.Indices.Create.WithContext(util.TimeoutCtx(ctx, opt.Timeout)),
|
|
|
|
c.c.Indices.Create.WithBody(bytes.NewReader(bs)),
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = c.checkResponse(result); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *client) ReadSetting(ctx context.Context) (map[string]any, error) {
|
|
|
|
r, err := c.c.Indices.GetSettings(
|
|
|
|
c.c.Indices.GetSettings.WithContext(util.TimeoutCtx(ctx, opt.Timeout)),
|
|
|
|
c.c.Indices.GetSettings.WithIndex(c.index),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.StatusCode != 200 {
|
|
|
|
return nil, fmt.Errorf("status=%d, msg=%s", r.StatusCode, r.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
m := make(map[string]any)
|
|
|
|
decoder := json.NewDecoder(r.Body)
|
|
|
|
if err = decoder.Decode(&m); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *client) WriteSetting(ctx context.Context, m map[string]any) error {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
bs []byte
|
|
|
|
result *esapi.Response
|
|
|
|
)
|
|
|
|
|
|
|
|
if bs, err = json.Marshal(m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if result, err = c.c.Indices.PutSettings(
|
|
|
|
bytes.NewReader(bs),
|
|
|
|
c.c.Indices.PutSettings.WithContext(util.TimeoutCtx(ctx, opt.Timeout)),
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.checkResponse(result)
|
|
|
|
}
|