-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split big batches in ES output plugin (#708)
* Retry 413 * Add e2e test --------- Co-authored-by: george pogosyan <[email protected]>
- Loading branch information
1 parent
6a1c63c
commit ac86bc2
Showing
8 changed files
with
284 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Password for the 'elastic' user (at least 6 characters) | ||
ELASTIC_PASSWORD=password | ||
|
||
# Version of Elastic products | ||
STACK_VERSION=8.16.1 | ||
|
||
# Set the cluster name | ||
CLUSTER_NAME=docker-cluster | ||
|
||
# Set to 'basic' or 'trial' to automatically start the 30-day trial | ||
LICENSE=basic | ||
#LICENSE=trial | ||
|
||
# Port to expose Elasticsearch HTTP API to the host | ||
ES_PORT=9200 | ||
#ES_PORT=127.0.0.1:9200 | ||
|
||
# Increase or decrease based on the available host memory (in bytes) | ||
MEM_LIMIT=1073741824 | ||
|
||
# Project namespace (defaults to the current folder name if not set) | ||
#COMPOSE_PROJECT_NAME=myproject |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
pipelines: | ||
file_es: | ||
input: | ||
type: file | ||
output: | ||
type: elasticsearch | ||
batch_flush_timeout: 200ms | ||
batch_size: 500 * 1 | ||
connection_timeout: 30s | ||
endpoints: | ||
- http://localhost:9200 | ||
fatal_on_failed_insert: true | ||
strict: false | ||
index_format: index_name | ||
retry: 1 | ||
retention: 1s | ||
workers_count: 1 | ||
username: elastic | ||
password: password |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
version: "3" | ||
|
||
services: | ||
es01: | ||
image: elasticsearch:${STACK_VERSION} | ||
volumes: | ||
- esdata01:/usr/share/elasticsearch/data | ||
ports: | ||
- ${ES_PORT}:9200 | ||
environment: | ||
- node.name=es01 | ||
- cluster.name=${CLUSTER_NAME} | ||
- cluster.initial_master_nodes=es01 | ||
- ELASTIC_PASSWORD=${ELASTIC_PASSWORD} | ||
- bootstrap.memory_lock=true | ||
- xpack.security.enabled=false | ||
- xpack.license.self_generated.type=${LICENSE} | ||
- xpack.ml.use_auto_machine_memory_percent=true | ||
- http.max_content_length=128b | ||
mem_limit: ${MEM_LIMIT} | ||
ulimits: | ||
memlock: | ||
soft: -1 | ||
hard: -1 | ||
healthcheck: | ||
test: | ||
[ | ||
"CMD-SHELL", | ||
"curl -s http://localhost:9200 | grep -q 'missing authentication credentials'", | ||
] | ||
interval: 10s | ||
timeout: 10s | ||
retries: 120 | ||
|
||
volumes: | ||
esdata01: | ||
driver: local |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package file_es | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"github.com/ozontech/file.d/cfg" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type Config struct { | ||
ctx context.Context | ||
cancel func() | ||
|
||
inputDir string | ||
} | ||
|
||
func (c *Config) Configure(t *testing.T, conf *cfg.Config, pipelineName string) { | ||
c.ctx, c.cancel = context.WithTimeout(context.Background(), time.Minute*2) | ||
|
||
c.inputDir = t.TempDir() | ||
offsetsDir := t.TempDir() | ||
|
||
input := conf.Pipelines[pipelineName].Raw.Get("input") | ||
input.Set("watching_dir", c.inputDir) | ||
input.Set("filename_pattern", "input.log") | ||
input.Set("offsets_file", filepath.Join(offsetsDir, "offsets.yaml")) | ||
} | ||
|
||
const ( | ||
n = 10 | ||
successEvent = `{"field_a":"AAAA","field_b":"BBBB"}` | ||
failEvent = `{"field_a":"AAAA","field_b":"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"}` | ||
) | ||
|
||
func (c *Config) Send(t *testing.T) { | ||
file, err := os.Create(path.Join(c.inputDir, "input.log")) | ||
require.NoError(t, err) | ||
defer func() { | ||
_ = file.Close() | ||
}() | ||
|
||
for i := 0; i < n; i++ { | ||
err = addEvent(file, successEvent) | ||
require.NoError(t, err) | ||
} | ||
|
||
err = addEvent(file, failEvent) | ||
require.NoError(t, err) | ||
|
||
for i := 0; i < 2*n-1; i++ { | ||
err = addEvent(file, successEvent) | ||
require.NoError(t, err) | ||
} | ||
|
||
_ = file.Sync() | ||
} | ||
|
||
func addEvent(f *os.File, s string) error { | ||
_, err := f.WriteString(s + "\n") | ||
return err | ||
} | ||
|
||
func (c *Config) Validate(t *testing.T) { | ||
time.Sleep(5 * time.Second) | ||
|
||
count, err := c.getEventsCount() | ||
require.NoError(t, err) | ||
require.Equal(t, n, count) | ||
|
||
err = c.deleteAll() | ||
require.NoError(t, err) | ||
} | ||
|
||
func (c *Config) deleteAll() error { | ||
client := &http.Client{Timeout: 3 * time.Second} | ||
|
||
req, err := http.NewRequest(http.MethodDelete, "http://127.0.0.1:9200/index_name", http.NoBody) | ||
if err != nil { | ||
return fmt.Errorf("create request: %w", err) | ||
} | ||
req.Header.Add("Authorization", "elastic:password") | ||
|
||
resp, err := client.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("do request: %w", err) | ||
} | ||
defer func() { | ||
_ = resp.Body.Close() | ||
}() | ||
|
||
respBody, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return fmt.Errorf("read all: %w", err) | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return fmt.Errorf("wrong status code; status = %d; body = %s", resp.StatusCode, respBody) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type searchResp struct { | ||
Hits struct { | ||
Total struct { | ||
Value int `json:"value"` | ||
Relation string `json:"relation"` | ||
} | ||
} `json:"hits"` | ||
} | ||
|
||
func (c *Config) getEventsCount() (int, error) { | ||
client := &http.Client{Timeout: 3 * time.Second} | ||
|
||
req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:9200/index_name/_search", http.NoBody) | ||
if err != nil { | ||
return 0, fmt.Errorf("create request: %w", err) | ||
} | ||
req.Header.Add("Authorization", "elastic:password") | ||
|
||
resp, err := client.Do(req) | ||
if err != nil { | ||
return 0, fmt.Errorf("do request: %w", err) | ||
} | ||
defer func() { | ||
_ = resp.Body.Close() | ||
}() | ||
|
||
respBody, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return 0, fmt.Errorf("read all: %w", err) | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return 0, fmt.Errorf("wrong status code; status = %d; body = %s", resp.StatusCode, respBody) | ||
} | ||
|
||
var respData searchResp | ||
err = json.Unmarshal(respBody, &respData) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return respData.Hits.Total.Value, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters