Commit Graph

66 Commits

Author SHA1 Message Date
Julie Tibshirani
70e98dbcbd
Redis: expose KeyValue.WithPrefix (#64466)
This PR makes `WithPrefix` visible on the `KeyValue` interface.
Previously, we had other interface implementations that did not support
`WithPrefix`, but now `redisKeyValue` is the only implementation.
`WithPrefix` is currently just used in tests, but it's broadly useful
and will help clean up a bunch of places that wrap Redis and manually
add key prefixes.
2024-08-14 17:41:59 +03:00
Julie Tibshirani
c222523fa5
Redis: remove some direct pool usages (#64447)
We want to discourage direct usage of the Redis pool in favor of routing
all calls through the main `KeyValue` interface. This PR removes several
usages of `KeyValue.Pool`. To do so, it adds "PING" and "MGET" to the
`KeyValue` interface.
2024-08-14 13:39:10 +03:00
Julie Tibshirani
ca6e72fe18
Redis: remove RedisKeyValue constructor (#64442)
This PR removes the `redispool.RedisKeyValue` constructor in favor of
the `New...KeyValue` methods, which do not take a pool directly. This
way callers won't create a `Pool` reference, allowing us to track all
direct pool usage through `KeyValue.Pool()`.

This also simplifies a few things:
* Tests now use `NewTestKeyValue` instead of dialing up localhost
directly
* We can remove duplicated Redis connection logic in Cody Gateway
2024-08-14 11:24:32 +03:00
Julie Tibshirani
e65e736cb1
Redis: simplify DeleteAllKeysWithPrefix (#64407)
The `DeleteAllKeysWithPrefix` method is now only used in tests to ensure
the test keyspace is clear. This PR makes it clear this method is only
used in tests, and simplifies the implementation so it no longer needs a
script + direct Redis connection.

Another tiny Redis refactor to prepare for multi-tenancy work.
2024-08-12 15:25:33 +03:00
Erik Seliger
ac0d497315
Remove old cache cleanup method (#63645)
We have been using v2 data since >5 years now, this should be safe to
remove.

As a side-effect, we have one less background task running in frontend,
which means it ran N times in horizontally scaled environments, which
isn't exactly useful.

Test plan:

Code review.
2024-07-10 02:04:53 +02:00
Erik Seliger
169db11ce6
rcache: Explicitly pass redis pool to use (#63644)
Recently, this was refactored to also allow using the redispool.Store.
However, that makes it very implicit to know where something is being
written, so instead we pass down the pool instance at instantiation.

This also gives a slightly better overview of where redispool is
actually required.

Test plan: CI passes.
2024-07-10 01:23:19 +02:00
Joe Chen
ce025a069a
enterprise-portal: init database schema and handler store (#63139)
Part of CORE-99

This PR scaffolds the database schema and code structure based on
[CORE-99
comment](https://linear.app/sourcegraph/issue/CORE-99/enterprise-portal-design-sams-user-to-subscription-rpcs#comment-8105ac31)
with some modifications. See inline comments for more elaborations.
- It uses GORM's ONLY for auto migration, just to kick things off, we
may migrate to file-based migration like we are planning for SAMS.
- It then uses the `*pgxpool.Pool` as the DB interface for executing
business logic queries.

Additionally, refactored `subscriptionsservice/v1.go` to use a `Store`
that provide single interface for accessing data(base), as we have been
doing in SAMS and SSC.

## Test plan

Enterprise Portal starts locally, and database is initialized:

![CleanShot 2024-06-06 at 17 02
42@2x](https://github.com/sourcegraph/sourcegraph/assets/2946214/f6cbc2bf-bd95-4691-9f87-b2450cc31e4d)
2024-06-06 18:54:12 -04:00
Ara
52350cd2e5
Fixing cache methods to use the right method to retrieve key value stores (#62087) 2024-04-22 21:51:21 +02:00
Ara
2e2dc2bc42
Adding Token counting support for bring your own key customers using Redis cache (#61134)
* Adding Token counting support for bring your own key customers using Redis cache

* Adding Completions stuff

* Adding Completions stuff

* Adding Completions stuff

* Adding Completions stuff

* Fixing tokenizer

* remvoing redundant comment

* Adding Constants

* Adding Constants

* Fixing naming

* Fixing naming

* Fixing naming and exposure of tokenizer methods

* Fixing naming and exposure of tokenizer methods

* Fixing naming and exposure of tokenizer methods

* Fixing naming and exposure of tokenizer methods

* Adding StoreTokenUsage method right before resetting redis

* Adding StoreTokenUsage method right before resetting redis

* Adding StoreTokenUsage method right before resetting redis

* Update internal/rcache/rcache.go

Co-authored-by: Stephen Gutekanst <stephen@sourcegraph.com>

* Update internal/rcache/rcache.go

Co-authored-by: Stephen Gutekanst <stephen@sourcegraph.com>

* Update cmd/frontend/internal/bg/store_token_usage_in_postgres.go

Co-authored-by: Stephen Gutekanst <stephen@sourcegraph.com>

* Adding StoreTokenUsage method right before resetting redis

---------

Co-authored-by: Stephen Gutekanst <stephen@sourcegraph.com>
2024-04-04 15:49:05 +00:00
Ara
03b561197b
Adding changes to redis caching to be able to support redis store for durability (#61476)
* Adding changes to redis caching to be able to support redis store to be able to add more durability to the redis connections

* Adding changes to redis caching to be able to support redis store to be able to add more durability to the redis connections

* Adding changes to redis caching to be able to support redis store to be able to add more durability to the redis connections

* Adding changes to redis caching to be able to support redis store to be able to add more durability to the redis connections
2024-04-02 12:44:35 +02:00
Petri-Johan Last
0b5e7fd490
Replace all traditional for-loops (#60988) 2024-03-11 16:05:47 +02:00
Rafał Gajdulewicz
a9446eee91
Remove unseen product-subscription tokens (#60585)
* rg/preconfigure_detecotr

* Chat only

* Comment

* Add https://github.com/TwiN/go-away/pull/80

* Bazel

* Update go.sum

* Remove

* Use a set

* remove testing overrides

* Log pattern

* bazel

* Latency + comments

* Comment

* Reorder seen

* Comment

* Switch set implementation

* Bazel
2024-02-20 13:37:27 +00:00
James McNamara
960d97bf8b
bazel: first pass at moving moving logging linting into nogo (#58910)
* First pass at moving moving logging linting into Bazel

* fixed negation operators

* Update dev/linters/logging/logging.go

Co-authored-by: William Bezuidenhout <william.bezuidenhout@sourcegraph.com>

* added more exceptions and refactored one or two impls

* added nogo lint pragmas to offending files

* ran configure

* reverted git-combine refactor

* ran configure

* reverted test as well

---------

Co-authored-by: William Bezuidenhout <william.bezuidenhout@sourcegraph.com>
2024-01-02 10:07:25 -08:00
Erik Seliger
f9ac351dc3
Remove App from codebase (#59115)
Cody no longer needs it and it is obsolete now!

Since App added a non-insignificant amount of new concepts and alternative code paths, I decided to take some time and remove it from out codebase.
This PR removes ~21k lines of code. If we ever want parts of single binary (app), the redis kv alternatives, or the release pipeline for a native mac app back, we can look back at this PR and revert parts of it, but maintaining 21k lines of code and many code paths for which I had to delete a surprisingly small amount of tests justifies this move for me very well.

Technically, to some extent SG App and Cody App both still existed in the codebase, but we don't distribute either of them anymore, so IMO we shouldn't keep this weight in our code.

So.. here we go.

This should not affect any of the existing deployments, we only remove functionality that was special-cased for app.
2023-12-21 01:07:05 +01:00
Camden Cheek
30af07f157
Chore: remove some uses of log15 (#57519) 2023-10-10 16:27:52 -06:00
Idan Varsano
65a5a47d9d
Periodic Rate Limit config job (#55558)
* rate limit config job

* fixup comments

* cleanup

* add network to test

* bazel

* address some PR feedback

* bazel

* addressing PR comments

* bazel

* fix const

* address PR comments

* moving the handler out

* addressing PR comments

* fix double call

* initialize handler work like other periodic routines

* remove unused error

* address pr comments and general cleanups

* just use the logger not the full obsContext

* typo+bazel

* match up the default case to current behavior

* leep default behaviorthe same for existing rate limiters

* bazel
2023-08-21 20:41:40 +00:00
Alex Ostrikov
9933ea9e8d
graphql: add API for listing recorded Git commands. (#55148)
This commit contains the initial simple implementation of returning all
recorded Git commands for a given repo without pagination.

Test plan:
GraphQL tests added.

Co-authored-by: Bolaji Olajide <25608335+BolajiOlajide@users.noreply.github.com>
2023-07-20 19:58:40 +04:00
Sander Ginn
826d96aa6e
rcache: add DeleteHashItem (#53074)
- Followup of https://github.com/sourcegraph/sourcegraph/pull/53012

Adds deleting a hash item from an `rcache`.

## Test plan
Unit tests

<!-- All pull requests REQUIRE a test plan:
https://docs.sourcegraph.com/dev/background-information/testing_principles
-->
2023-06-07 10:04:59 +00:00
Jean-Hadrien Chabran
3d36d34b3d
ci: re-enable race detection (#52776)
The previous approach to enable race detection was too radical and
accidently led to build our binaries with the race flage enabled, which
caused issues when building images down the line.

This happened because putting a `test --something` in bazelrc also sets
it on `build` which is absolutely not what we wanted. Usually folks get
this one working by having a `--stamp` config setting that fixes this
when releasing binaries, which we don't at this stage, as we're still
learning Bazel.

Luckily, this was caught swiftly. The current approach insteads takes a
more granular approach, which makes the `go_test` rule uses our own
variant, which injects the `race = "on"` attribute, but only on
`go_test`.


## Test plan

<!-- All pull requests REQUIRE a test plan:
https://docs.sourcegraph.com/dev/background-information/testing_principles
-->

CI, being a main-dry-run, this will cover the container building jobs,
which were the ones failing.

---------

Co-authored-by: Alex Ostrikov <alex.ostrikov@sourcegraph.com>
2023-06-05 20:41:47 +02:00
Erik Seliger
3ec3c34bf4
Fixup rate limiter with redis < 7.0 (#51138)
This was missed when implementing the initial rate limiter, we cannot
use NX on the EXPIRE operation, because that's a 7.0+ operator only.
Sourcegraph ships with redis 5 by default, so that will fail on dotcom.

Keegan and I brainstormed this approach together and concluded that we
don't need the MULTI statement and do all in one go, so we can simply
read the TTL and set expiry when it's not yet set.
2023-04-26 11:57:21 +02:00
Dave Try
2b8fa079f0
bazel: fix buf files (#49444)
fix protoc-gen-go version
2023-03-15 20:21:38 +00:00
Dave Try
293385d5dd
bazel: update timeouts to suppress warnings (#49399)
Updates all of the BUILD fields with timeouts to suppress warnings and
reduce log spam.


## Test plan

Green CI
2023-03-15 15:04:16 +02:00
Jean-Hadrien Chabran
7c55c2351e
bk: add soft-failing bazel jobs (#47601)
This PR introduces soft-failing bazel jobs which are running on all
builds, as way to gather experience and feedback on running Bazel in CI.

Changes in the go code are mostly about adding missing git configuration
bits that were failing in the sandbox.

## Test plan

<!-- All pull requests REQUIRE a test plan:
https://docs.sourcegraph.com/dev/background-information/testing_principles
-->

CI will show up bazel stuff, but ok to fail. 


![image](https://user-images.githubusercontent.com/10151/223689285-ba4f91f9-991e-42ba-b333-f8adfd503ee9.png)

---------

Co-authored-by: Greg Magolan <gmagolan@gmail.com>
2023-03-08 11:28:18 +00:00
Keegan Carruthers-Smith
0584e283e0
rcache: make it possible for redispool.Cache to change (#47031)
This is needed to work around tricky we have around things being
declared at init() time, which is fairly common with redis usages in our
codebase.

Also removed the no longer needed backwards.go.

Test Plan: go test
2023-01-27 20:22:22 +00:00
Jean-Hadrien Chabran
5bea6c78c8
bazel: build the //lib folder (#46929) 2023-01-27 15:30:35 +01:00
Petri-Johan Last
e5df7823e6
Remove all unused code specified by linter warnings (#47015) 2023-01-27 13:17:36 +02:00
Keegan Carruthers-Smith
abb80727f1
rcache: add TTL, Expire and Incr to KeyValue (#46780)
Was hoping we could nicely avoid making the KeyValue interface even
larger, but no dice. Lets do this and see how we can make it not such a
large thing. Additionally the test for KeyValue has become quite large,
but I believe is still readable and debuggable. But there is scope to
improve that in the future as well.

Test Plan: added unit tests. Existing ones test rcache methods changed
as well.
2023-01-23 14:29:17 +00:00
Jean-Hadrien Chabran
bc5490c4bb
bazel: introduce build files for Go (#46770) 2023-01-23 14:00:01 +01:00
Keegan Carruthers-Smith
1dde055077
all: remove watches for FIFOList (#46771)
This is something that I thought could be improved. In everycase we
setup a watch for a FIFOList all we are doing it fetching a value.
Instead of updating the FIFOList, we can pass in a function which
FIFOList calls when it wants to know what size to trim to. This
simplifies nearly all use cases and removes the need to have a
SetMaxSize API.

The biggest change here was around syncjobs which had a few mocks setup
which meant the change wasn't as clean. But all other call sites should
look nicer.

Test Plan: go test
2023-01-23 14:25:04 +02:00
Keegan Carruthers-Smith
39a07eff28
rcache: migrate most uses to direct KeyValue use (#46764)
Additionally we update KeyValue to correctly model how redis returns
lists. Lists are only returned from list like calls, so we introduce a
Values type to prevent misuse. We also expand the KeyValue interface to
accomodate a new use of HGetAll.

Test Plan: unit tests
2023-01-23 10:13:18 +00:00
Keegan Carruthers-Smith
3b1627dad4
goroutine: use FIFOList for recorder (#46769)
FIFOList directly supports the use case of what recorder wants (store
the most recent N jobs). This makes it possible to remove the list
operations added to rcache. Additionally I think the call sites are
clearer.

Test Plan: go test
2023-01-23 09:49:35 +00:00
Keegan Carruthers-Smith
6b2a6f911f
rcache: remove unused *Multi and ListKeys (#46577)
This is no longer used. We removed it to reduce the API surface of
rcache.

Test Plan: go test
2023-01-20 11:13:10 +00:00
Keegan Carruthers-Smith
a6ff0684ba
rcache: allow negative max sizes in FIFOList (#46702)
This is the same as using a value of zero. This is fixes a potential
footgun when accidently passing in a negative value. Additionally
another PR wants to use negative values as a signal to disable a
feature.

Test Plan: added a test
2023-01-20 10:14:19 +00:00
Keegan Carruthers-Smith
a415aea558
rcache: use KeyValue abstraction in FIFOList (#46658)
I can see things we can improve here now that this exists, but this
feels quite nice.

Test Plan: go test
2023-01-19 10:36:38 +00:00
Keegan Carruthers-Smith
e449cee2a9
rcache: only get connection in Set if it will be used (#46655)
Minor optimization, we would acquire a connection from the pool twice
for Set with TTL since Set would acquire a connection but then just call
SetWithTTL which does the same thing.

Test Plan: go test
2023-01-19 11:29:48 +02:00
Keegan Carruthers-Smith
39581f0de4
all: introduce redispool.KeyValue (#46425)
KeyValue is an abstraction over our most common uses of redis in our
codebase. It is introduced in an effort to have an alternative to redis
in Sourcegraph App. Additionally it provides a more ergonomic API than
the raw redis interface we have in our codebase.

The most important change is in keyvalue.go. Otherwise the changes to
all other files are much more minimal and should be backwards
compatible.

Note: Some call sites would do multiple commands on a single connection.
The only benefit here is performance, but in each case we did that it
was a function rarely called. Additionally in some places we used
redis.Conn.Send, but now we use redis.Conn.Do always. This means
previously if the command failed we wouldn't know since Send only places
the command into a buffer, and in all cases we ignored the error
returned from Close. Now we won't ignore that error.

Note: Currently Pool should always return, optionally disabling redis is
for a future commit.

Test Plan: unit tests, manual testing and will also do integration tests
in CI.
2023-01-18 11:30:20 +02:00
Keegan Carruthers-Smith
a44d9448e5
internal: add keegan to CODENOTIFY for redis packages (#46606)
Test Plan: n/a
2023-01-17 18:54:26 +00:00
David Veszelovszki
bd7f2d3da3
Create a dashboard for background jobs (#44901)
- Add data access layer for Redis HASHes and LISTs (with tests) (in `rcache.go`)
- Add types on the back end
- Add business logic between GraphQL and Redis
- Add GraphQL endpoint in `schema.graphql` with Node compatibility
- Hook into periodic goroutines, DB-backed workers, and Batches scheduler
- Add menu item, routing, and page with page description, legend at the top, job and routine list
- Add filtering for errors and pause/play
- Extracted `formatMilliseconds` for reuse
- Format tooltip for large numbers nicely
- Remove a cyclic dependency in the `types` package

Co-authored-by: Eric Fritz <eric@sourcegraph.com>
Co-authored-by: Thorsten Ball <mrnugget@gmail.com>
Co-authored-by: Kelli Rockwell <kelli@sourcegraph.com>
2023-01-17 13:27:51 +00:00
Keegan Carruthers-Smith
7733451439
rcache: introduce FIFOList.SetMaxSize (#46567)
This is introduced to fix a rare race condition were we overwrite the
variable slowRequestRedisFIFOList while it could be read. Additionally,
if this store is not backed in memory we want to re-use the in memory
store.

Test Plan: go test
2023-01-17 12:17:36 +00:00
Keegan Carruthers-Smith
1cb3b119b9
rcache: allow maxSize == 0 for FIFOList (#46566)
This essentially disables the FIFOList. The outbound request logger has
this as a possibility which I intend to port to FIFOList.

Note: we have to special case maxSize 0, otherwise the LTRIM we send is
"LTRIM key 0 -1" which means don't trim anything.

Test Plan: go test
2023-01-17 14:03:52 +02:00
Jean-Hadrien Chabran
9f0220e9ee
site-admin: add a new page to browse captured slow requests. (#45063)
Co-authored-by: Philipp Spiess <hello@philippspiess.com>
Co-authored-by: William Bezuidenhout <william.bezuidenhout@sourcegraph.com>
2022-12-15 09:52:57 +01:00
Milan Freml
ad787b7736
[fix] only send unlock account email once per locked session (#45479)
* [fix] only send unlock account email once per locked session

* Revert print statements from unrelated debugging

* PR feedback

* Fix bug with SetKeyTTL method

* Update internal/rcache/rcache.go

Co-authored-by: Thorsten Ball <mrnugget@gmail.com>

* Set TTL based on what we configure in site config

* Remove global mock and fix unit tests

* Remove innefective use of mat.Min

* Update cmd/frontend/internal/auth/userpasswd/lockout.go

Co-authored-by: Thorsten Ball <mrnugget@gmail.com>

Co-authored-by: Thorsten Ball <mrnugget@gmail.com>
2022-12-12 18:12:45 +01:00
Jean-Hadrien Chabran
6c3fa0756c
internal: add a FIFOList to only store n recent inserts (#44938) 2022-12-05 09:46:32 +01:00
William Bezuidenhout
ca0d18a290
Revert "Revert "Log external requests (#44286)" (#44890)" aka reapply Log External Requests (#44893)
Revert "Revert "Log external requests (#44286)" (#44890)"

This reverts commit 8dc8fb0ad1.

Co-authored-by: David Veszelovszki <veszelovszki@gmail.com>
2022-11-29 14:15:55 +00:00
William Bezuidenhout
8dc8fb0ad1
Revert "Log external requests (#44286)" (#44890)
This reverts commit cd28a4cc2c.
2022-11-29 13:16:14 +00:00
David Veszelovszki
cd28a4cc2c
Log external requests (#44286)
* Added Redis access
* Added RedisLogItem with request+response info and stack traces
* Added RedisLoggerMiddleware, with sensitive header removal
* Added GraphQL API endpoint, for site admins only, matching the Cursor Connections spec
* Added admin UI with five-sec polling and "copy curl" feature
* Added back-end tests
* Added log limit setting with default=50, range: 0..500
* Added changelog item
* Deduplicated WebhookLogHeader with HTTPHeader
* Unrelated: Fixed two typos
* Use  for the API
* Made the CSS work well with both light and dark themes and made sure it's accessible. Tested with a screen reader.

Co-authored-by: Robert Lin <robert@bobheadxi.dev>
2022-11-29 12:08:54 +01:00
Robert Lin
923a15a249
graphqlbackend: add permissionsSyncJobs query (#44387)
Adds a new permissionsSyncJobs query that returns the most recent outcomes of permissions sync jobs, with per-provider details included (see #44258). This will be used to validate permissions syncing is starting correctly and the desired providers are being involved and in a healthy state.

We don't implement pagination since the endpoint is intended as a stream of recent events and retention is low, but the query schema is set up so that it can be extended to support pagination in the future.

Co-authored-by: Erik Seliger <erikseliger@me.com>
2022-11-16 11:44:38 -08:00
Robert Lin
d2e4f01d64
PermsSyncer: track recent job states in redis (#44258)
Tracks perms sync job outcomes, notably per-provider states (#44257), in Redis. We set an aggressive TTL of 5 minutes by default because in practice, only the details for the last few sync jobs are relevant, and this prevents us from putting too much pressure on Redis on larger instances.

This can be extended in the future to be database-backed, but for now I think Redis is the right choice - the volume of entries can be quite high when accumulated so we want something easy to expire, and realistically only the details for the last few sync jobs are relevant. Additionally, the records store and reader in package synclogs can easily be rewritten without too much impact to write to a database in the future.
2022-11-16 10:11:21 -08:00
Robert Lin
a55b91d552
redis: use latest version of redigo, remove unused rcache mutex (#44385)
The redigo/redis version we use now was published in 2018, and has since been retracted - the latest versions are still being published on 1.x, with the latest release v1.8.9 being published this year:

https://pkg.go.dev/github.com/gomodule/redigo@v1.8.9/redis?tab=versions

This upgrades our dependency to use this latest version, and also removes an unused rcache mutex functionality from package rcache that no longer works with this new version of redigo.
2022-11-14 22:56:42 +00:00
Keegan Carruthers-Smith
27569d1fc7
all: run gofmt -s -w from 1.19 (#41629)
gofmt in go1.19 does a lot of reformating of godoc strings, mostly to
make them more consistent around lists.

Test Plan: CI
2022-09-13 07:44:06 +00:00