Skip to content
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

codeintel: Call query methods from from lsifserver client #10872

Merged
merged 13 commits into from May 21, 2020
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/gorilla/mux"
"github.com/inconshreveable/log15"
"github.com/pkg/errors"
"github.com/sourcegraph/sourcegraph/cmd/precise-code-intel-api-server/internal/api"
"github.com/sourcegraph/sourcegraph/internal/codeintel/api"
"github.com/sourcegraph/sourcegraph/internal/codeintel/gitserver"
)

Expand Down
@@ -1,7 +1,7 @@
package server

import (
"github.com/sourcegraph/sourcegraph/cmd/precise-code-intel-api-server/internal/api"
"github.com/sourcegraph/sourcegraph/internal/codeintel/api"
bundles "github.com/sourcegraph/sourcegraph/internal/codeintel/bundles/client"
)

Expand Down
Expand Up @@ -9,7 +9,7 @@ import (
"sync"

"github.com/inconshreveable/log15"
"github.com/sourcegraph/sourcegraph/cmd/precise-code-intel-api-server/internal/api"
"github.com/sourcegraph/sourcegraph/internal/codeintel/api"
bundles "github.com/sourcegraph/sourcegraph/internal/codeintel/bundles/client"
"github.com/sourcegraph/sourcegraph/internal/codeintel/db"
"github.com/sourcegraph/sourcegraph/internal/codeintel/enqueuer"
Expand Down
2 changes: 1 addition & 1 deletion cmd/precise-code-intel-api-server/main.go
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/inconshreveable/log15"
"github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus"
"github.com/sourcegraph/sourcegraph/cmd/precise-code-intel-api-server/internal/api"
"github.com/sourcegraph/sourcegraph/cmd/precise-code-intel-api-server/internal/server"
"github.com/sourcegraph/sourcegraph/internal/codeintel/api"
bundles "github.com/sourcegraph/sourcegraph/internal/codeintel/bundles/client"
"github.com/sourcegraph/sourcegraph/internal/codeintel/db"
"github.com/sourcegraph/sourcegraph/internal/codeintel/gitserver"
Expand Down
56 changes: 38 additions & 18 deletions enterprise/cmd/frontend/main.go
Expand Up @@ -32,8 +32,12 @@ import (
campaignsResolvers "github.com/sourcegraph/sourcegraph/enterprise/internal/campaigns/resolvers"
"github.com/sourcegraph/sourcegraph/enterprise/internal/codeintel/lsifserver/proxy"
codeIntelResolvers "github.com/sourcegraph/sourcegraph/enterprise/internal/codeintel/resolvers"
codeintelapi "github.com/sourcegraph/sourcegraph/internal/codeintel/api"
bundles "github.com/sourcegraph/sourcegraph/internal/codeintel/bundles/client"
codeinteldb "github.com/sourcegraph/sourcegraph/internal/codeintel/db"
"github.com/sourcegraph/sourcegraph/internal/codeintel/enqueuer"
codeintelgitserver "github.com/sourcegraph/sourcegraph/internal/codeintel/gitserver"
lsifserverclient "github.com/sourcegraph/sourcegraph/internal/codeintel/lsifserver/client"
"github.com/sourcegraph/sourcegraph/internal/conf"
"github.com/sourcegraph/sourcegraph/internal/db/dbconn"
"github.com/sourcegraph/sourcegraph/internal/db/globalstatedb"
Expand All @@ -49,8 +53,9 @@ func main() {
}

initLicensing()
initResolvers()
initLSIFEndpoints()
initAuthz()
initCampaigns()
initCodeIntel()

clock := func() time.Time {
return time.Now().UTC().Truncate(time.Microsecond)
Expand Down Expand Up @@ -144,34 +149,49 @@ func initLicensing() {
}
}

func initResolvers() {
graphqlbackend.NewCampaignsResolver = campaignsResolvers.NewResolver
graphqlbackend.NewCodeIntelResolver = codeIntelResolvers.NewResolver
func initAuthz() {
graphqlbackend.NewAuthzResolver = func() graphqlbackend.AuthzResolver {
return authzResolvers.NewResolver(dbconn.Global, func() time.Time {
return time.Now().UTC().Truncate(time.Microsecond)
})
}
}

func initCampaigns() {
graphqlbackend.NewCampaignsResolver = campaignsResolvers.NewResolver

}

var bundleManagerURL = env.Get("PRECISE_CODE_INTEL_BUNDLE_MANAGER_URL", "", "HTTP address for internal LSIF bundle manager server.")

func initLSIFEndpoints() {
httpapi.NewLSIFServerProxy = func() (*httpapi.LSIFServerProxy, error) {
if bundleManagerURL == "" {
log.Fatalf("invalid value for PRECISE_CODE_INTEL_BUNDLE_MANAGER_URL: no value supplied")
}
func initCodeIntel() {
if bundleManagerURL == "" {
log.Fatalf("invalid value for PRECISE_CODE_INTEL_BUNDLE_MANAGER_URL: no value supplied")
}

observationContext := &observation.Context{
Logger: log15.Root(),
Tracer: &trace.Tracer{Tracer: opentracing.GlobalTracer()},
Registerer: prometheus.DefaultRegisterer,
}
observationContext := &observation.Context{
Logger: log15.Root(),
Tracer: &trace.Tracer{Tracer: opentracing.GlobalTracer()},
Registerer: prometheus.DefaultRegisterer,
}

db := codeinteldb.NewObserved(codeinteldb.NewWithHandle(dbconn.Global), observationContext)
bundleManagerClient := bundles.New(bundleManagerURL)

client := lsifserverclient.New(
db,
bundleManagerClient,
codeintelapi.New(db, bundleManagerClient, codeintelgitserver.DefaultClient),
)

enqueuer := enqueuer.NewEnqueuer(db, bundleManagerClient)

db := codeinteldb.NewObserved(codeinteldb.NewWithHandle(dbconn.Global), observationContext)
bundleManagerClient := bundles.New(bundleManagerURL)
graphqlbackend.NewCodeIntelResolver = func() graphqlbackend.CodeIntelResolver {
return codeIntelResolvers.NewResolver(client)
}

return proxy.NewProxy(db, bundleManagerClient)
httpapi.NewLSIFServerProxy = func() (*httpapi.LSIFServerProxy, error) {
return proxy.NewProxy(enqueuer, client)
}
}

Expand Down
9 changes: 4 additions & 5 deletions enterprise/internal/codeintel/lsifserver/proxy/proxy.go
Expand Up @@ -14,21 +14,20 @@ import (
"github.com/sourcegraph/sourcegraph/cmd/frontend/httpapi"
"github.com/sourcegraph/sourcegraph/cmd/frontend/types"
"github.com/sourcegraph/sourcegraph/internal/api"
bundles "github.com/sourcegraph/sourcegraph/internal/codeintel/bundles/client"
codeinteldb "github.com/sourcegraph/sourcegraph/internal/codeintel/db"
"github.com/sourcegraph/sourcegraph/internal/codeintel/enqueuer"
"github.com/sourcegraph/sourcegraph/internal/codeintel/lsifserver/client"
"github.com/sourcegraph/sourcegraph/internal/conf"
"github.com/sourcegraph/sourcegraph/internal/errcode"
"github.com/sourcegraph/sourcegraph/internal/gitserver"
)

func NewProxy(db codeinteldb.DB, bundleManagerClient bundles.BundleManagerClient) (*httpapi.LSIFServerProxy, error) {
func NewProxy(enqueuer *enqueuer.Enqueuer, lsifserverClient *client.Client) (*httpapi.LSIFServerProxy, error) {
return &httpapi.LSIFServerProxy{
UploadHandler: http.HandlerFunc(uploadProxyHandler(enqueuer.NewEnqueuer(db, bundleManagerClient))),
UploadHandler: http.HandlerFunc(uploadProxyHandler(enqueuer, lsifserverClient)),
}, nil
}

func uploadProxyHandler(enqueuer *enqueuer.Enqueuer) func(http.ResponseWriter, *http.Request) {
func uploadProxyHandler(enqueuer *enqueuer.Enqueuer, lsifserverClient *client.Client) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
repoName := q.Get("repository")
Expand Down
8 changes: 5 additions & 3 deletions enterprise/internal/codeintel/resolvers/query.go
Expand Up @@ -13,6 +13,8 @@ import (
)

type lsifQueryResolver struct {
lsifserverClient *client.Client

repositoryResolver *graphqlbackend.RepositoryResolver
// commit is the requested target commit
commit api.CommitID
Expand Down Expand Up @@ -50,7 +52,7 @@ func (r *lsifQueryResolver) Definitions(ctx context.Context, args *graphqlbacken
UploadID: upload.ID,
}

locations, _, err := client.DefaultClient.Definitions(ctx, opts)
locations, _, err := r.lsifserverClient.Definitions(ctx, opts)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -121,7 +123,7 @@ func (r *lsifQueryResolver) References(ctx context.Context, args *graphqlbackend
continue
}

locations, nextURL, err := client.DefaultClient.References(ctx, opts)
locations, nextURL, err := r.lsifserverClient.References(ctx, opts)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -155,7 +157,7 @@ func (r *lsifQueryResolver) Hover(ctx context.Context, args *graphqlbackend.LSIF
continue
}

text, lspRange, err := client.DefaultClient.Hover(ctx, &struct {
text, lspRange, err := r.lsifserverClient.Hover(ctx, &struct {
RepoID api.RepoID
Commit api.CommitID
Path string
Expand Down
19 changes: 12 additions & 7 deletions enterprise/internal/codeintel/resolvers/resolver.go
Expand Up @@ -11,12 +11,16 @@ import (
"github.com/sourcegraph/sourcegraph/internal/codeintel/lsifserver/client"
)

type Resolver struct{}
type Resolver struct {
lsifserverClient *client.Client
}

var _ graphqlbackend.CodeIntelResolver = &Resolver{}

func NewResolver() graphqlbackend.CodeIntelResolver {
return &Resolver{}
func NewResolver(lsifserverClient *client.Client) graphqlbackend.CodeIntelResolver {
return &Resolver{
lsifserverClient: lsifserverClient,
}
}

func (r *Resolver) LSIFUploadByID(ctx context.Context, id graphql.ID) (graphqlbackend.LSIFUploadResolver, error) {
Expand All @@ -25,7 +29,7 @@ func (r *Resolver) LSIFUploadByID(ctx context.Context, id graphql.ID) (graphqlba
return nil, err
}

lsifUpload, err := client.DefaultClient.GetUpload(ctx, &struct {
lsifUpload, err := r.lsifserverClient.GetUpload(ctx, &struct {
UploadID int64
}{
UploadID: uploadID,
Expand All @@ -48,7 +52,7 @@ func (r *Resolver) DeleteLSIFUpload(ctx context.Context, id graphql.ID) (*graphq
return nil, err
}

err = client.DefaultClient.DeleteUpload(ctx, &struct {
err = r.lsifserverClient.DeleteUpload(ctx, &struct {
UploadID int64
}{
UploadID: uploadID,
Expand Down Expand Up @@ -87,11 +91,11 @@ func (r *Resolver) LSIFUploads(ctx context.Context, args *graphqlbackend.LSIFRep
opt.NextURL = &nextURL
}

return &lsifUploadConnectionResolver{opt: opt}, nil
return &lsifUploadConnectionResolver{lsifserverClient: r.lsifserverClient, opt: opt}, nil
}

func (r *Resolver) LSIF(ctx context.Context, args *graphqlbackend.LSIFQueryArgs) (graphqlbackend.LSIFQueryResolver, error) {
uploads, err := client.DefaultClient.Exists(ctx, &struct {
uploads, err := r.lsifserverClient.Exists(ctx, &struct {
RepoID api.RepoID
Commit api.CommitID
Path string
Expand All @@ -110,6 +114,7 @@ func (r *Resolver) LSIF(ctx context.Context, args *graphqlbackend.LSIFQueryArgs)
}

return &lsifQueryResolver{
lsifserverClient: r.lsifserverClient,
repositoryResolver: args.Repository,
commit: args.Commit,
path: args.Path,
Expand Down
4 changes: 3 additions & 1 deletion enterprise/internal/codeintel/resolvers/upload.go
Expand Up @@ -107,6 +107,8 @@ type LSIFUploadsListOptions struct {
}

type lsifUploadConnectionResolver struct {
lsifserverClient *client.Client

opt LSIFUploadsListOptions

// cache results because they are used by multiple fields
Expand Down Expand Up @@ -166,7 +168,7 @@ func (r *lsifUploadConnectionResolver) compute(ctx context.Context) ([]*lsif.LSI
return
}

r.uploads, r.nextURL, r.totalCount, r.err = client.DefaultClient.GetUploads(ctx, &struct {
r.uploads, r.nextURL, r.totalCount, r.err = r.lsifserverClient.GetUploads(ctx, &struct {
RepoID api.RepoID
Query *string
State *string
Expand Down
Expand Up @@ -271,7 +271,7 @@ func setMockBundleClientPackageInformation(t *testing.T, mockBundleClient *bundl
}

func readTestFilter(t *testing.T, dirname, filename string) []byte {
content, err := ioutil.ReadFile(fmt.Sprintf("../../testdata/filters/%s/%s", dirname, filename))
content, err := ioutil.ReadFile(fmt.Sprintf("./testdata/filters/%s/%s", dirname, filename))
if err != nil {
t.Fatalf("unexpected error reading: %s", err)
}
Expand Down
23 changes: 17 additions & 6 deletions internal/codeintel/lsifserver/client/client.go
Expand Up @@ -6,6 +6,9 @@ import (
"strings"
"sync"

"github.com/sourcegraph/sourcegraph/internal/codeintel/api"
bundles "github.com/sourcegraph/sourcegraph/internal/codeintel/bundles/client"
"github.com/sourcegraph/sourcegraph/internal/codeintel/db"
"github.com/sourcegraph/sourcegraph/internal/endpoint"
"github.com/sourcegraph/sourcegraph/internal/env"
"github.com/sourcegraph/sourcegraph/internal/trace/ot"
Expand All @@ -16,19 +19,27 @@ var (

preciseCodeIntelAPIServerURLsOnce sync.Once
preciseCodeIntelAPIServerURLs *endpoint.Map
)

type Client struct {
endpoint *endpoint.Map
HTTPClient *http.Client
server *Server
}

DefaultClient = &Client{
func New(
db db.DB,
bundleManagerClient bundles.BundleManagerClient,
codeIntelAPI api.CodeIntelAPI,
) *Client {
return &Client{
endpoint: LSIFURLs(),
HTTPClient: &http.Client{
// ot.Transport will propagate opentracing spans
Transport: &ot.Transport{},
},
server: NewServer(db, bundleManagerClient, codeIntelAPI),
}
)

type Client struct {
endpoint *endpoint.Map
HTTPClient *http.Client
}

func LSIFURLs() *endpoint.Map {
Expand Down