Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Enable "Test connection" for Perforce code hosts#56697

Merged
peterguy merged 5 commits into
mainfrom
peterguy/perforce-check-connection
Sep 19, 2023
Merged

Enable "Test connection" for Perforce code hosts#56697
peterguy merged 5 commits into
mainfrom
peterguy/perforce-check-connection

Conversation

@peterguy

@peterguy peterguy commented Sep 15, 2023

Copy link
Copy Markdown
Contributor

Enable the "Test connection" button in the code host page UI

Test plan

Click the "Test connection" button in the code host connection UI. A message with an orange border will display, "Checking code host connection..." and then "Code host is reachable" should display with a green border.
Screenshot 2023-09-15 at 14 17 00

@cla-bot cla-bot Bot added the cla-signed label Sep 15, 2023
@peterguy peterguy changed the title fill out CheckConnection and mark Perforce as able to check connections Enable "Check connection" for Perforce code hosts Sep 15, 2023
@peterguy peterguy requested a review from a team September 15, 2023 21:19
@peterguy peterguy self-assigned this Sep 15, 2023
@sourcegraph-bot

sourcegraph-bot commented Sep 15, 2023

Copy link
Copy Markdown
Contributor

Codenotify: Notifying subscribers in CODENOTIFY files for diff cb63189...3883a38.

Notify File(s)
@eseliger cmd/gitserver/server/vcs_syncer_perforce.go
internal/repos/perforce.go

@peterguy peterguy requested a review from eseliger September 15, 2023 21:57
@peterguy peterguy linked an issue Sep 15, 2023 that may be closed by this pull request
@peterguy peterguy changed the title Enable "Check connection" for Perforce code hosts Enable "Test connection" for Perforce code hosts Sep 16, 2023

@mrnugget mrnugget left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you run this locally to make sure it works as expected? The err == nil tells me you haven't run it locally, so I at least want to make sure it works.

Comment thread internal/repos/perforce.go Outdated
Comment thread internal/repos/perforce.go Outdated
Comment thread internal/repos/perforce.go Outdated
Comment on lines 58 to 64

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised that we don't have code that already does this. Maybe time to pull this out into the perforce package and allow user and password? https://sourcegraph.sourcegraph.com/github.com/sourcegraph/sourcegraph@2fa7fbba450030d1249935fac6083405351210fa/-/blob/internal/repos/perforce.go?L87-97

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. We should extract decomposePerforceCloneURL also.
But which perforce package?

  • internal/perforce
  • internal/extsvc/perforce
  • internal/authz/providers/perforce
  • cmd/gitserver/server/perforce

Comment thread internal/repos/perforce.go Outdated
Comment on lines 73 to 75

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand what the flow is supposed to be: if we can connect to the first depot without a problem, we return, yes? What if we can't connect to the others?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach was confusing; trying to leverage existing methods. Abandoning that approach for a better one.

Comment thread internal/repos/perforce.go Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmmm, I think we should return the error here, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we want to try additional repos ins case the error is with the repo and not with the code host. But that approach is outdated now.

@peterguy

Copy link
Copy Markdown
Contributor Author

Can you run this locally to make sure it works as expected? The err == nil tells me you haven't run it locally, so I at least want to make sure it works.

The err = nil was a noob copy/paste mistake, for sure (copied approach from ListRepos), but I did test it locally. It worked as expected for a successful test. I did not force an error.

However, based on your feedback, @mrnugget, I have revised the approach. Instead of leveraging an existing method in the syncer - IsCloneable - which required the repo to also be valid, causing the iteration over the depots, I've added a new method - CanConnect (the name choice is up for debate) - which tests only the info needed to make a connection to the code host.

Comment thread internal/repos/perforce.go Outdated
Comment on lines 53 to 54

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That comment is quite correct (or at least, it doesn't work in frontend), but importing the syncer from the cmd/gitserver codebase doesn't actually run it in gitserver. If this works, it'll magically do because repo-updater has the p4 cli at the moment as well, but the comment is not 100% accurate.
However, this touches on a great point that it would be ideal to RPC to gitserver to check the perforce status instead.
But this doesn't have to be rushed in the day before branch cut and we can improve it later.

@peterguy peterguy Sep 18, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, so internal/repos/perforce.go::CheckConnection is called from cmd/frontend/graphqlbackend/external_service.go::CheckConnection, which is in the frontend, right? So how does that work? Does frontend run in the server container, which does have p4 (and nearly everything else) installed? I'm looking at the architecture diagram.

We should be able to easily convert this call to RPC to gitserver by using P4Exec from gitserver.Client. I'm not sure gitserver.Client will honor any timeouts we add to the context.

I'm not sure I want to switch to that, but this is what it'll look like (tested locally):

func (s PerforceSource) CheckConnection(ctx context.Context) error {
	gclient := gitserver.NewClient()
	rc, _, err := gclient.P4Exec(ctx, s.config.P4Port, s.config.P4User, s.config.P4Passwd, "users")
	if err != nil {
		return errors.Wrap(err, "Unable to connect to the Perforce server")
	}
	rc.Close()
	return nil
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

frontend runs in the frontend container (on k8s) and in the server container if it's single-server image. On k8s you can't guarantee that p4 is installed in the frontend container.

So basically the problem with this code here

https://github.com/sourcegraph/sourcegraph/blob/dd32bfbcb1f4b0a3f47975a53c65e97e56c49668/cmd/frontend/graphqlbackend/external_service.go#L249-L264

is that it should actually send a request to gitserver (from frontend) where then the connection check would be made.

I wouldn't hide that information in the Source.

@peterguy peterguy Sep 19, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the other sources than Perforce use HTTP(S) to check the connection with the code host, so they don't need gitserver. It's fine to leave the details in source.CheckConnection; for Perforce we'll make sure to RPC to gitserver for the connection check.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created issue #56782 for looking into ListRepos, which is another Source method that may need to also use RPC, although it seems to be ok as-is currently.

Comment thread internal/repos/perforce.go Outdated
Because connecting to Perforce requires using the `p4` CLI binary,
which is on `gitserver`, we're using the Perforce VCS Syncer to do the connection check.
Add a method to the VCS syncer for Perforce specifically for checking the connection
instead of hacking a solution that re-uses IsCloneable, which requires a valid depot.
Adding another func won't break composition with VCSSyncer.
10 seconds is somewhat arbitrary, but seems like enough time to allow
for various connection issues, whle not being too long for the frontend.
to making an RPC call to gitserver because the call to
CheckConnection comes fro mthe frontend, where `p4` is not available.
@peterguy peterguy force-pushed the peterguy/perforce-check-connection branch from ba66498 to 3883a38 Compare September 19, 2023 17:12
@peterguy peterguy merged commit b1fd4da into main Sep 19, 2023
@peterguy peterguy deleted the peterguy/perforce-check-connection branch September 19, 2023 20:19
sourcegraph-release-bot pushed a commit that referenced this pull request Sep 19, 2023
* fill out `CheckConnection` and mark Perforce as able to check connections.

* Refactor the connection test to not rely on IsCloneable
Because connecting to Perforce requires using the `p4` CLI binary,
which is on `gitserver`, we're using the Perforce VCS Syncer to do the connection check.
Add a method to the VCS syncer for Perforce specifically for checking the connection
instead of hacking a solution that re-uses IsCloneable, which requires a valid depot.
Adding another func won't break composition with VCSSyncer.

* update comment on CheckConnection

* add a 10-second timeout for the connection test
10 seconds is somewhat arbitrary, but seems like enough time to allow
for various connection issues, whle not being too long for the frontend.

* Change from directly using the syncer
to making an RPC call to gitserver because the call to
CheckConnection comes fro mthe frontend, where `p4` is not available.

(cherry picked from commit b1fd4da)
peterguy added a commit that referenced this pull request Sep 19, 2023
Added CHANGELOG entry for #56697
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Check connection is not available for Perforce code hosts

4 participants