-
Notifications
You must be signed in to change notification settings - Fork 623
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose individual apis #177
base: main
Are you sure you want to change the base?
Conversation
…ration object as the regular client
Just as a quick demonstration of what the original issue was reporting, taking one of the examples: package main
import (
"log"
"github.com/elastic/go-elasticsearch/v8"
)
func main() {
es, err := elasticsearch.NewDefaultClient()
if err != nil {
log.Fatalf("Error creating the client: %s\n", err)
}
res, err := es.Info()
if err != nil {
log.Fatalf("Error getting the response: %s\n", err)
}
defer res.Body.Close()
log.Print(res.String())
} and replacing it with the equivalent package main
import (
"log"
"github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/esapi"
)
func main() {
ts, err := elasticsearch.NewTransportClient(elasticsearch.Config{})
if err != nil {
log.Fatalf("Error creating the client: %s\n", err)
}
info := esapi.NewInfo(ts)
res, err := info()
if err != nil {
log.Fatalf("Error getting the response: %s\n", err)
}
defer res.Body.Close()
log.Print(res.String())
} gives me an approximately 4MB binary difference on my mac when compiled with: bash-3.2$ CGO_ENABLED=0 go build -ldflags '-s' bash-3.2$ ls -la full-client info-only
-rwxr-xr-x 1 andrew.stucki staff 11946532 Aug 6 00:25 full-client
-rwxr-xr-x 1 andrew.stucki staff 7735528 Aug 6 00:29 info-only Alternatively you could do the same with |
Thanks for looking into this, @andrewstucki! I'm out of office until the end of the month, so it might take some time to fully process the changes, merge them, etc. We can have a conversation here in the meantime, though. Couple of comments:
I definitely agree that trimming down the size for the bulk indexer is important. We'll need to tweak it a little bit, as you suggest, though. One way I have suggested elsewhere is to change it from accepting go-elasticsearch/esutil/bulk_indexer.go Lines 477 to 496 in 527225d
|
That should be fine. You need me to do only the generator + regenerated API functions/structs here and then I can open up a separate PR for the client changes?
Yeah, that name needs some work. Regarding just building the config itself v. an underlying client, the other aspect to that method is copying over all of the fields from an
Yep, it's basically that the compiler is able to prune a whole bunch of code that's never used. When someone initializes a
Yeah, I think that once there's a simple way of doing a fairly straightforward initialization of an |
Hello! With regards to separating the changes, it's perfectly fine to just rebase the commits, and force push to this branch/PR. Thinking about it, perhaps it's better to leave out the changes to the APIs from this PR, since I tend to commit those in a fairly uniform manner, and always isolated from other changes (see https://github.com/elastic/go-elasticsearch/commits/master/esapi). I'll need to think more about the whole "simplified client" / "just transport client" aspect, but I think you've nailed the neccessary changes to the generator and the API constructors. Totally agreed about the change to the bulk indexer, I always envisioned making the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like a good change overall, just one question about node discovery.
if cfg.DiscoverNodesOnStart { | ||
go client.DiscoverNodes() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the reason for starting discovery in NewClient and not NewTransportClient?
It seems surprising that cfg.DiscoverNodesOnStart would be ignored when calling NewTransportClient, and probably not a desired change in behaviour for users who are only trying to slim down the API surface area.
Maybe we should add a DiscoverNodesOnStart flag to estransport.Config, and start the goroutine there?
I finally had some time to look into the issue and think about the problem today. While I'm definitely not opposed to exposing the constructors of the individual APIs, I think the current package structure allows for "almost" achieving the stated goal of reducing the build size. It occured to me that if the package main
import (
"context"
"log"
"net/url"
"github.com/elastic/go-elasticsearch/v8/esapi"
"github.com/elastic/go-elasticsearch/v8/estransport"
)
func main() {
clusterURL, _ := url.Parse("http://localhost:9200")
tp, err := estransport.New(
estransport.Config{
URLs: []*url.URL{clusterURL},
},
)
if err != nil {
log.Fatalf("Error creating the client: %s\n", err)
}
req := esapi.InfoRequest{}
res, err := req.Do(context.Background(), tp)
if err != nil {
log.Fatalf("Error getting the response: %s\n", err)
}
defer res.Body.Close()
log.Print(res.String())
} When I check the size, I'm getting this output:
It needs to be noted that Not sure which way to go now. Does anybody has any thoughts? |
I believe this is one of the major asks in #172, the other being something like an
elasticsearch.NewTransport
function that you could pass to each of these if you so desired. Basically rather than having one giant API object, we also expose the ability to decompose the client into individual APIs so someone can use just what they want and avoid the penalty of instantiating every client.Edit:
I went ahead and just exposed a method for creating an
*estransport.Client
with the same configuration structure as the top-levelelasticsearch.NewClient
and named itelasticsearch.NewTransportClient
.This will basically allow people to do what's being requested, something along the lines of:
Also, this has the added benefit of, with a few changes being a potential way to slim down the
esutil.BulkIndexer
implementation, since really, the only thing that theBulkIndexer
needs is an*estransport.Client
rather than the whole wrapped API.