-
Notifications
You must be signed in to change notification settings - Fork 528
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add gencorpora cmd to generate ES corpora for rally
- Loading branch information
Showing
10 changed files
with
258 additions
and
17 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/elastic/apm-server/systemtest/gencorpora" | ||
"github.com/elastic/apm-server/systemtest/loadgen" | ||
) | ||
|
||
func main() { | ||
corporaDir := flag.String("corpora-dir", "", "output directory for corpora ndjson files") | ||
flag.Parse() | ||
|
||
f, err := getWriter(*corporaDir) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer f.Close() | ||
|
||
// Create fake ES server | ||
esServer, err := gencorpora.GetCatBulkServer(f) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer func() { | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
if err := esServer.Shutdown(ctx); err != nil { | ||
log.Fatal("failed to shutdown cat bulk server", err) | ||
} | ||
cancel() | ||
}() | ||
|
||
// Create APM-Server to send documents to fake ES | ||
apmServer, err := gencorpora.GetAPMServer(esServer.Addr) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer func() { | ||
if err := apmServer.Cmd.Process.Signal(os.Interrupt); err != nil { | ||
apmServer.Cmd.Process.Kill() | ||
log.Fatal("failed to interrupt process, initiated kill", err) | ||
} | ||
if err := apmServer.Wait(); err != nil { | ||
log.Fatal("failed while waiting for APM-Server to exit", err) | ||
} | ||
}() | ||
|
||
// Start fake ES and APM-Server | ||
go func() { | ||
err := esServer.Serve() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
}() | ||
if err := apmServer.Start(); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Wait for APM Server to be ready to accept requests | ||
<-time.After(2 * time.Second) | ||
|
||
// Send data to APM-Server | ||
if err := generateLoad(context.Background(), "http://127.0.0.1:8200"); err != nil { | ||
log.Fatal("failed to generate load", err) | ||
} | ||
} | ||
|
||
func getWriter(corporaDir string) (io.WriteCloser, error) { | ||
if corporaDir == "" { | ||
return os.Stdout, nil | ||
} | ||
return os.Create( | ||
filepath.Join(corporaDir, fmt.Sprintf("%s.ndjson", time.Now().Format(time.RFC3339))), | ||
) | ||
} | ||
|
||
func generateLoad(ctx context.Context, serverURL string) error { | ||
inf := loadgen.GetNewLimiter(0) | ||
handler, err := loadgen.NewEventHandler(`*.ndjson`, serverURL, "", inf) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = handler.SendBatches(ctx) | ||
return err | ||
} |
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,38 @@ | ||
package gencorpora | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/elastic/apm-server/systemtest/apmservertest" | ||
) | ||
|
||
func GetAPMServer(esHost string) (*apmservertest.ServerCmd, error) { | ||
args := []string{ | ||
"--strict.perms=false", | ||
"-E", "logging.level=warning", | ||
"-E", "logging.to_stderr=true", | ||
"-E", "apm-server.data_streams.wait_for_integration=false", | ||
"-E", "apm-server.host=127.0.0.1:8200", | ||
"-E", fmt.Sprintf("output.elasticsearch.hosts=['%s']", esHost), | ||
} | ||
|
||
cmd := apmservertest.ServerCommand(context.Background(), "run", args...) | ||
reader, err := cmd.StderrPipe() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// log streaming | ||
go func() { | ||
scanner := bufio.NewScanner(reader) | ||
|
||
for scanner.Scan() { | ||
log.Println(scanner.Text()) | ||
} | ||
}() | ||
|
||
return cmd, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package gencorpora | ||
|
||
import ( | ||
"bufio" | ||
"compress/gzip" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"log" | ||
"net" | ||
"net/http" | ||
) | ||
|
||
// CatBulkServer wraps http server and a listener to listen | ||
// for ES requests on any available port | ||
type CatBulkServer struct { | ||
*http.Server | ||
listener net.Listener | ||
} | ||
|
||
// GetCatBulkServer returns a HTTP Server which can serve as a | ||
// fake ES server writing the response of the bulk request to the | ||
// provided writer. Writes to the provided writer must be thread safe. | ||
func GetCatBulkServer(writer io.Writer) (*CatBulkServer, error) { | ||
listener, err := net.Listen("tcp", ":0") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &CatBulkServer{ | ||
listener: listener, | ||
Server: &http.Server{ | ||
Addr: listener.Addr().String(), | ||
Handler: handleReq(writer), | ||
}, | ||
}, nil | ||
} | ||
|
||
// Serve accepts incoming requests to the fake ES server on a listener | ||
func (s *CatBulkServer) Serve() error { | ||
return s.Server.Serve(s.listener) | ||
} | ||
|
||
func handleReq(writer io.Writer) http.HandlerFunc { | ||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||
w.Header().Set("X-Elastic-Product", "Elasticsearch") | ||
switch req.Method { | ||
case http.MethodGet: | ||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte(`{"cluster_uuid": "cat_bulk"}`)) | ||
case http.MethodPost: | ||
reader := req.Body | ||
defer req.Body.Close() | ||
|
||
if encoding := req.Header.Get("Content-Encoding"); encoding == "gzip" { | ||
var err error | ||
reader, err = gzip.NewReader(reader) | ||
if err != nil { | ||
log.Println("failed to read request body", err) | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
} | ||
|
||
respItems := make(map[string]map[string]int) | ||
scanner := bufio.NewScanner(reader) | ||
|
||
for count := 0; scanner.Scan(); count++ { | ||
fmt.Fprintln(writer, scanner.Text()) | ||
if count%2 == 0 { | ||
respItems["action"] = map[string]int{"status": 200} | ||
} | ||
} | ||
|
||
resp, err := json.Marshal(respItems) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
w.Write(resp) | ||
default: | ||
w.WriteHeader(http.StatusNotFound) | ||
} | ||
}) | ||
} |
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
Oops, something went wrong.