-
Notifications
You must be signed in to change notification settings - Fork 525
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'inject-build-pgo-profile' into update-pgo-1726276668
- Loading branch information
Showing
27 changed files
with
739 additions
and
139 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
moxy |
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,10 @@ | ||
module moxy | ||
|
||
go 1.22.5 | ||
|
||
require ( | ||
github.com/klauspost/compress v1.17.9 | ||
go.uber.org/zap v1.27.0 | ||
) | ||
|
||
require go.uber.org/multierr v1.10.0 // indirect |
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,16 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= | ||
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= | ||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= | ||
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= | ||
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= | ||
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= | ||
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= | ||
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= | ||
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
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 main | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"encoding/base64" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"sync" | ||
|
||
"github.com/klauspost/compress/gzip" | ||
"github.com/klauspost/compress/zstd" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
var memPool = sync.Pool{ | ||
New: func() interface{} { | ||
return new(bytes.Buffer) | ||
}, | ||
} | ||
|
||
func main() { | ||
logLevel := zap.LevelFlag( | ||
"loglevel", zapcore.InfoLevel, | ||
"set log level to one of: DEBUG, INFO (default), WARN, ERROR, DPANIC, PANIC, FATAL", | ||
) | ||
username := flag.String("username", "elastic", "authentication username to mimic ES") | ||
password := flag.String("password", "", "authentication username to mimic ES") | ||
flag.Parse() | ||
zapcfg := zap.NewProductionConfig() | ||
zapcfg.EncoderConfig.EncodeTime = zapcore.RFC3339TimeEncoder | ||
zapcfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder | ||
zapcfg.Encoding = "console" | ||
zapcfg.Level = zap.NewAtomicLevelAt(*logLevel) | ||
logger, err := zapcfg.Build() | ||
if err != nil { | ||
panic(err) | ||
} | ||
if *username == "" || *password == "" { | ||
logger.Fatal("both username and password are required") | ||
} | ||
defer logger.Sync() | ||
s := http.Server{ | ||
Addr: ":9200", | ||
Handler: handler(logger, *username, *password), | ||
} | ||
if err := s.ListenAndServe(); err != nil { | ||
logger.Fatal("listen error", zap.Error(err)) | ||
} | ||
} | ||
|
||
func handler(logger *zap.Logger, username, password string) http.Handler { | ||
expectedAuth := fmt.Sprintf("%s:%s", username, password) | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("X-Elastic-Product", "Elasticsearch") | ||
switch r.URL.Path { | ||
case "/": | ||
w.Write([]byte(`{ | ||
"name": "instance-0000000001", | ||
"cluster_name": "eca3b3c3bbee4816bb92f82184e328dd", | ||
"cluster_uuid": "cc49813b6b8e2138fbb8243ae2b3deed", | ||
"version": { | ||
"number": "8.15.1", | ||
"build_flavor": "default", | ||
"build_type": "docker", | ||
"build_hash": "253e8544a65ad44581194068936f2a5d57c2c051", | ||
"build_date": "2024-09-02T22:04:47.310170297Z", | ||
"build_snapshot": false, | ||
"lucene_version": "9.11.1", | ||
"minimum_wire_compatibility_version": "7.17.0", | ||
"minimum_index_compatibility_version": "7.0.0" | ||
}, | ||
"tagline": "You Know, for Search" | ||
}`)) | ||
return | ||
case "/_security/user/_has_privileges": | ||
w.Write([]byte(`{"username":"admin","has_all_requested":true,"cluster":{},"index":{},"application":{"apm":{"-":{"event:write":true}}}}`)) | ||
case "/_bulk": | ||
auth := r.Header.Get("Authorization") | ||
actualAuth, err := base64.StdEncoding.DecodeString(auth) | ||
if err != nil || string(actualAuth) != expectedAuth { | ||
logger.Error( | ||
"authentication failed", | ||
zap.Error(err), | ||
zap.String("actual", string(actualAuth)), | ||
zap.String("expected", expectedAuth), | ||
) | ||
w.WriteHeader(http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
first := true | ||
var body io.Reader | ||
switch r.Header.Get("Content-Encoding") { | ||
case "gzip": | ||
r, err := gzip.NewReader(r.Body) | ||
if err != nil { | ||
logger.Error("gzip reader err", zap.Error(err)) | ||
http.Error(w, fmt.Sprintf("reader error: %v", err), http.StatusInternalServerError) | ||
return | ||
} | ||
defer r.Close() | ||
body = r | ||
case "zstd": | ||
r, err := zstd.NewReader(r.Body) | ||
if err != nil { | ||
logger.Error("zstd reader err", zap.Error(err)) | ||
http.Error(w, fmt.Sprintf("reader error: %v", err), http.StatusInternalServerError) | ||
return | ||
} | ||
defer r.Close() | ||
body = r | ||
default: | ||
body = r.Body | ||
} | ||
|
||
jsonw := memPool.Get().(*bytes.Buffer) | ||
defer func() { | ||
jsonw.Reset() | ||
memPool.Put(jsonw) | ||
}() | ||
|
||
jsonw.Write([]byte(`{"items":[`)) | ||
scanner := bufio.NewScanner(body) | ||
for scanner.Scan() { | ||
// Action is always "create", skip decoding. | ||
if !scanner.Scan() { | ||
logger.Error("unexpected payload") | ||
http.Error(w, "expected source", http.StatusInternalServerError) | ||
return | ||
} | ||
if first { | ||
first = false | ||
} else { | ||
jsonw.WriteByte(',') | ||
} | ||
jsonw.Write([]byte(`{"create":{"status":201}}`)) | ||
} | ||
if err := scanner.Err(); err != nil { | ||
logger.Error("scanner error", zap.Error(err)) | ||
http.Error(w, fmt.Sprintf("scanner error: %v", err), http.StatusInternalServerError) | ||
} else { | ||
jsonw.Write([]byte(`]}`)) | ||
w.Write(jsonw.Bytes()) | ||
} | ||
// TODO additionally report events throughput metric here, to index into benchmarks. | ||
default: | ||
logger.Error("unknown path", zap.String("path", r.URL.Path)) | ||
} | ||
}) | ||
} |
Oops, something went wrong.