Commit Graph

64 Commits

Author SHA1 Message Date
Alejandro Ruiz 287428701b
Avoid Watches using write transactions (#840)
* Avoid Watches using write transactions

* Prevent duplicated events

By stopping the backfilling once it reaches the latest ResourceVersion at the moment the watcher was registered

* Move function

* Add comment

* Close channel after removing the watcher

* Remove unnecessary case

* Take derived context into account

* Remove unnecessary case

* Properly wait for goroutine to finish before returning from Watch

This prevents a panic trying to write to the original eventsChan if it was closed

* Simplify buffler flush section

* Parameterize maxBufferSize

* Refactor into a single goroutine

* Split goroutine into 3 different loops for readability

* Release queue for GC once no longer needed

* Change order of primary key for _events tables
2025-10-21 20:08:10 -04:00
Tom Lebreux b4483c7c6b
Support filtering and sorting on allocatable/requestsed fields for provisioning.cattle.io.clusters (#861)
* Capture type info so numeric values sort correctly.

* Add tabular data for other metadata.fields to sort numerically.

The issue is that some fields are not numeric, but are represented
in an entry in metadata.fields with a numeric indicator.  Like
with secrets, metadata.fields[2] gives the number of actual keys
in the stored secret.

* WIP - Add tests and the custom function.

* Add a converter to convert the annotated strings to float64's

* Fold the 'builtinIntTable' into the prepopulated 'typeGuidanceTable'.

* WIP - get memory-tests to pass for  now

* More test tweaking - verify the status.available fields work when not specified.

* WIP - get memory-tests to pass for  now

* WIP - Add the associations for pcio.clusters -> mcio.clusters.

* WIP - need more stuff. Revert this commit?

* WIP - remove these

* Fix rebase cruft

* avail -> requested, and prov is in v1, not v3

* Include cpuRaw in the injected fields.

* Make this debug output a trace so we can access it for debugging

* Start management.cattle.io.clusters caches for provisioning.cattle.io.clusters

* Add comment

---------

Co-authored-by: Eric Promislow <epromislow@suse.com>
2025-10-17 14:59:52 -04:00
Eric Promislow e03f03ea75
Index fields: part 7 (#855)
* Capture type info so numeric values sort correctly.

* Replace conditional string prefixing with strings.TrimPrefix

* Add tabular data for other metadata.fields to sort numerically.

The issue is that some fields are not numeric, but are represented
in an entry in metadata.fields with a numeric indicator.  Like
with secrets, metadata.fields[2] gives the number of actual keys
in the stored secret.

* Index more fields round 7, part 1

Add status.{allocatable,requested}.{cpus,pods,memory,memoryRaw}

* Add a converter to convert the annotated strings to float64's

* Fold the 'builtinIntTable' into the prepopulated 'typeGuidanceTable'.

- More test tweaking - verify the status.available fields work when not specified.

* Correction: 'status.available' should be 'status.requested'.

* Add the 'memoryRaw' field to status.A/R

* golint change, but the sql function uses underscores

* Fix decoding problems.

* Remove redundant type registering for GOB

* We no longer need to process this type because everything is 'map[string]any'.

* Remove commented-out debug helpers

* Use k8s library quantity-parser over ours.

Our unit tests pass as if with this change, looks good.
2025-10-17 08:55:15 -04:00
Eric Promislow 4f27f71146
Sort by ipaddr (#760)
* SQL codegen: map the SortAsIP option to the 'inet_aton' function call

* Implement and test sorting IP addresses by their underlying int64 value.

* Implement parsing 'ip(x)' as an IP-sort directive.

* Fix a change rebasing missed.
2025-10-15 13:45:10 -07:00
Eric Promislow 0d86756a0b
Capture type info so numeric values sort correctly. (#837)
* Capture type info so numeric values sort correctly.

* Replace conditional string prefixing with strings.TrimPrefix

* Add tabular data for other metadata.fields to sort numerically.

The issue is that some fields are not numeric, but are represented
in an entry in metadata.fields with a numeric indicator.  Like
with secrets, metadata.fields[2] gives the number of actual keys
in the stored secret.

* Update added test to include the typeGuidance param.

* Act on review suggestions/comments.

* Add a unit test verifying typeGuidance can change a DB field type.

The test found an error that it's better for getFieldAndColInfo
to return default typeGuidance = an empty map rather than `nil`.

* Add Tom's unit test to verify TypeGuidance influences sorting.

Also add a fake type-guidance as int on a field where the values
start with a number but also have non-numeric data.  sqlite stores
the entire string, and sort maps the values to 0... so sorting
doesn't repeats the incoming order (and sort-DESC reverses it).

* Add a clarifying comment on sorting non-ints or partly-ints as INT.

* Add more test objects to verify sorting mix of ints and non-ints works as expected.
2025-10-15 10:40:55 -07:00
Alejandro Ruiz 6e39b34488
Make vai use the encoding abstraction and improvements (#743) 2025-10-10 08:18:56 +02:00
Alejandro Ruiz 62d576c56c
Rely on foreign keys for deletion (#846) 2025-10-10 08:04:12 +02:00
Alejandro Ruiz ad016aba66
Use a single INSERT for updating the indices table (#858) 2025-10-10 08:03:19 +02:00
Felipe Gehrke 6af3f6dcee
adding revision check (#854)
* added revision check

* added new revision check and test

* removed unecessary spaces

* added equality test

* fixed test
2025-10-09 09:23:56 -03:00
Alejandro Ruiz b939ccc60f
Omit update for immutable fields during upsert (#836) 2025-10-09 10:14:01 +02:00
Alejandro Ruiz b533087afe
Add debug logging for queries (#834) 2025-10-08 08:00:44 +02:00
Alejandro Ruiz b7bf2216da
Fix context handling during List and Watch requests (#843) 2025-10-06 13:40:01 +02:00
Tom Lebreux 24a6c92cb5
Fix nil pointer panic when stopping a cache factory (#841)
* Fix nil pointer panic when stopping a cache factory

* Remove unnecessary comment

* Remove duplicate Stop
2025-09-29 10:45:13 -04:00
Alejandro Ruiz fb0bb4693d
Refactor database/sql structs handling (#833) 2025-09-29 16:27:38 +02:00
Alejandro Ruiz 7caef631d0
Use Upsert syntax instead of REPLACE INTO (#832) 2025-09-23 10:21:31 +02:00
Tom Lebreux 13d5ad3ccb
Stop single caches instead of all of them (#812)
* Revert OnSchemas change work

* Track schema changes

* Only stop a single GVK informer factory

* Add tests

* Rename crd to crdClient

* Rename s to sqlStore

* Don't wait for synced caches if request is canceled

* Move schematracker to pkg/sqlcache/schematracker
2025-09-10 17:04:25 -04:00
Tom Lebreux 45a3b07816
Fix send to closed channel panic in watch (#817) 2025-09-10 15:09:25 -04:00
Tom Lebreux ca3fa10db5
Drop tables instead of removing the database completely (#807)
* Add drop tables

* Fix deadlock

* Use f.Stop in tests

* Rename dropAllStmtFmt to dropBaseStmtFmt

* Fix comment

* More dropAll->dropBase renaming
2025-09-09 15:52:02 -04:00
Eric Promislow 7cff39371f
Add comments to clarify where non-test-mode methods are defined. (#809) 2025-09-08 14:31:18 -07:00
Tom Lebreux 4bd760b22d
Fix race condition with database reset (#804)
* Wait for garbage collection to be finished before creating a new DB

* Prevent DB reset while inflight List requests

* Rename cacheFor to cacheForLocked

* Rename Reset to Stop

* Mention DoneWithCache in README

* Add RunGC to ByOptionsLister interface
2025-09-05 11:30:56 -04:00
Eric Promislow 0de8a57d39
Further fixes to the projectsornamespaces param, with new tests. (#772)
* Further fixes to the projectsornamespaces param, with new tests.

* Simplify projectsornamespaces on namespace kind

Move the processing of this special case from the SQL code generator
to the parser, and map it to simple `metadata.name=` and `metadata.labels[...]`
tests.

* Fix `projectsornamespace!=` code gen.

These should generate (f.namespace != X AND (negative label test).

The code was generating an `OR` giving too many false positives.

* Fix the projORns not error in the SQL
2025-08-18 21:53:50 -07:00
Eric Promislow 4cc7e7b947
Fix new projectsornamespaces negative tests. (#769)
Remember to select objects that don't have the project label assigned to them
when doing a negative test.
2025-08-08 17:23:56 -07:00
Felipe Gehrke 3cac88141b
#50968 - Single SQL Transaction for projectsornamespaces filter (#758)
* working for positive case

* changed to custom code

* removed comment

* added tests

* fixing ci error

* one more fix

* Update proxy_store tests for projectornamespace parsing.

Since we no longer need to execute a db query, and just return a filter,
we can drop a lot of code.

* added distinct back to conform with other queries

* one more fix

---------

Co-authored-by: Eric Promislow <epromislow@suse.com>
2025-08-08 16:07:54 -03:00
Eric Promislow dbd2818d22
VAI: Replace namespace+name default sort with id (#724)
* Sort by ID rather than metadata.namespace,metadata.name

- In tests add an id field to the list of fields to cache/create and to the objects.

* Add benchmark tests to compare sorting by ns/name vs id.
2025-08-07 15:42:33 -07:00
Tom Lebreux a020084518
Fix slow labels queries (#762)
* Add PRAGMA temp_store=2 to write temporary tables in memory

* Only use DISTINCT when filtering labels

* Log long queries to debug

* Add unit test
2025-08-06 20:07:09 -04:00
Felipe Gehrke b556256ed3
#50914 - Changed ParseQuery projectornamespace Handling (#718)
* changed ParseQuery projectornamespace handling

* addressed comments from eric/chad and tom

* fixing lint error

* added resourceVersion to list

* updated mocks

* updated tests

* Update tests after rebase

---------

Co-authored-by: Tom Lebreux <tom.lebreux@suse.com>
2025-07-17 13:51:13 -04:00
Tom Lebreux 0c2c554c8c
Respect shouldEncrypt configuration when storing events (#725) 2025-07-15 09:57:28 -04:00
Tom Lebreux 127d37391d
Better gc (#717)
* More error wrapping for SQL cache

* Use context.Context instead of stopCh

* Move CacheFor to Info log level

* Implement time-based GC

* Set default GC param when running standalone steve
2025-07-11 12:48:19 -04:00
Eric Promislow 76477e98df
Hard-wire external associations: 6 and 7/7: unit tests for updating A=>B links when instances of A and B change (#702)
* Implement hard-wired external associations:

* The table is in sqlproxy.proxy_store
  - externalGVKDependencies - a map of GVKs to dependencies.
    When the key GVK is updated, it triggers the updates in the database for the dependent GVKs,
    replacing fields as specified in the table.

* This is done in an afterUpsert handler, but it's done after the transaction for the core
  GVK update is finished, because most likely the dependent GVK updates will depend on the
  final database values for the GVK being updated, and if we do it as part of the transaction
  the new values won't be committed to the database.

* Wrote unit tests for external associations.

* When an object is modified/created, check for external deps that need updating.

* Stop emitting errors when joining tables if one of the tables doesn't exist.

* Finish rebasing manually.

* Fix external association unit-tests after rebasing.
2025-07-09 13:11:42 -07:00
Eric Promislow a3cd651c75
Revert part of steve PR 633 (#708)
Specifically, recomment a test that was uncommented in this PR,
and revert the underlying code that made this test pass.  That
code change causes user-based integration tests to fail, so something
is not correct in that code.

This was actually unrelated to the reason for PR 633, to fix
a problem when sorting on a label that dropped objects that
didn't have that label.  That part remains untouched.
2025-07-09 11:12:47 -07:00
Eric Promislow 2a86733c64
Use SQL `WITH` statements to sort unbound labels. (#663)
* Use SQL `WITH` statements to sort unbound labels.

These are labels whose names are never positively referenced in a filter,
so they don't need to exist on the row that we still want to display.

Here I create a virtual table of all the rows, substituting a null
value for each label that isn't associated on the row, and then sort on that.

* Just always select-distinct for now.

* Add more tests for filtering/sorting

- Assign more values to the cattle and horses labels
- Move the sortfield value to a number in the 100s -- keep in mind
  these values are sorted by ascii value of underlying chars, not numerically
- Rename the var names to better reflect the values they contain

* Remove mentions of the prepared SQL statement in the test descriptions.

We care either about the items we get back from the query, or in some
cases the SQL that gets generated by the AST interpreter.

* Simplify the use of WITH stmts in SQL (thx Tom)

* Fix the comment about an unexpected situation.

* Fix post-rebase tests.
2025-07-08 10:07:05 -07:00
Eric Promislow 3be82a28d1
Hard-wire external associations: 5/7: update A=>B links when instances of A change (#646)
* Continue rebasing.

* Wrote unit tests for external associations.

* Fix the generated SQL.

Some syntactic sugar (capitalizing the keywords), but use the 'ON' syntax on JOINs.

* We want "management.cattle.io.projects:spec.displayName" not "...spec.clusterName"

* Implement hard-wired external associations:

* The table is in sqlproxy.proxy_store
  - externalGVKDependencies - a map of GVKs to dependencies.
    When the key GVK is updated, it triggers the updates in the database for the dependent GVKs,
    replacing fields as specified in the table.

* This is done in an afterUpsert handler, but it's done after the transaction for the core
  GVK update is finished, because most likely the dependent GVK updates will depend on the
  final database values for the GVK being updated, and if we do it as part of the transaction
  the new values won't be committed to the database.

* When an object is modified/created, check for external deps that need updating.

* Stop emitting errors when joining tables if one of the tables doesn't exist.

* Update unit test syntax for SQL queries.

* And an override check

This ensures we don't overwrite good data when
pulling data from one table to another.

* Drop labels, and use mgmt.cattle.io/spec.displayName

There's no need to hardwire labels in proxy_store:typeSpecificIndexedFields
because all labels are indexed in the shadow labels table.

* Keep clusterName, add displayName for mgmt.cattle.io

* Fix rebase/merge breakage.

* Finish the merge: add the 'selfUpdateInfo' param where it didn't get inserted during merge.

* Patch up rebase failures.

* Now gomock generates named args. I give up.
2025-07-03 14:35:09 -07:00
Peter Matseykanets 952305e096
vai: Rework events garbage collection delete query (#682) 2025-06-26 09:22:40 -07:00
Eric Promislow 496a6f8968
Hard-wire external associations: 3 sections in, this one is 4/7 (#645)
* Continue rebasing.

* Wrote unit tests for external associations.

* Fix the generated SQL.

Some syntactic sugar (capitalizing the keywords), but use the 'ON' syntax on JOINs.

* whitespace fix post rebase

* We want "management.cattle.io.projects:spec.displayName" not "...spec.clusterName"

* Fix the database calls: drop the key

* Fix breakage during automatic rebase merging gone wrong.

* ws fix - NFC

* Post rebase-merge fixes

* Fix rebase-driven merge.

* Fix rebase breakage.

* go-uber v0.5.2 prefers real arg names to '<argN>'
2025-06-25 16:10:48 -07:00
Swastik Gour e17ca28461
Bumped dependencies to be compatible with k8s-v1.33 (#681)
Signed-off-by: swastik959 <Sswastik959@gmail.com>
2025-06-20 17:53:42 +05:30
Eric Promislow 2e8a0f2851
Support indexing on array-like fields (#673)
* Run tests using sqlite DB in a temp directory.

I was running into write-file errors which happens when two sqlite processes try to update the DB at the same time.

* Implement and test the extractBarredValue custom SQL function.

* Explain the DB path constants better.
2025-06-16 15:06:07 -07:00
Chad Roberts 5de54d6a4a
Handle int64 field type when adding index fields (#671)
* Handle int64 field type when adding index fields

* add support for float64
2025-06-13 13:04:55 -04:00
Chad Roberts 58f15a4349
check err after client.List call when creating informer (#672) 2025-06-12 17:03:52 -04:00
Tom Lebreux 1157865ea3
Fix panic due to concurrent map writes (#669)
The same object is given to many HTTP request and they proceed to modify
that object.
2025-06-11 11:57:48 -04:00
Tom Lebreux cf97607be5
Return `resourceversion too old` error to UI instead of logging (#667)
* Return watch error instead of logging it

The UI needs to know about watch error like `resourceversion too old` so
we need to return it.

* Sort by resourceVersion as number

The UI makes some assumption on resourceVersion. It assumes they are a
number and they are ordered by the number value. We'll want to fix this
at some point most likely but for now let's give something in a way that
UI wants.

* Remove -d suffix

After much testing, a delete of an object seems to have its own
resourceVersion so we don't need the -d suffix, we can simply use the
new resourceVersion.
2025-06-10 17:29:42 -04:00
Tom Lebreux f258ebcf31
Add garbage collection for events table (#664)
* Add ListOptionIndexerOptions to create ListOptionIndexer

* Remove unused function

* Implement configurable garbage collection on ListOptionIndexer

* Propagate Steve options from Server
2025-06-10 12:02:55 -04:00
Tom Lebreux b4db257cdb
Handle transaction failure due to canceled context.Context (#662)
* Re-order SQL event hooks so events are last

* Add QueryRowContext for single line queries

* Add test case for unknown resource version

* Properly check rows and close it

* More accurate error message when context.Context is canceled

* Re-order test check
2025-06-09 15:39:09 -04:00
Tom Lebreux b695567794
Keep track of events for SQL cache watches (#661)
* Keep track of past events

* Clarify RV acronym

* Fix comment typo

* Rename listEvents variables

* Improve filter readability

* Move defer
2025-06-05 18:40:19 -04:00
Tom Lebreux b0aa90cd22
Add watch filtering support for vai (#654)
* Add support for watch filtering

* Remove debug Println

* Validate watch selector
2025-06-03 18:10:36 -04:00
Tom Lebreux e3f207ddc2
Add basic watch functionality for SQL cache (#653)
* Remove unused method

* Add basic watch functionality

* Remove TestWatchNamesErrReceive test
2025-06-03 18:07:18 -04:00
Tom Lebreux 2672969496
Add object to RegisterAfterDelete and introduce RegisterAfterDeleteAll (#649)
* Add object to AfterDelete callbacks

* Add RegisterAfterDeleteAll
2025-06-03 17:32:43 -04:00
Tom Lebreux a8f3ce48d6
Convert tests to real SQL store (#652) 2025-06-02 16:01:45 -06:00
Tom Lebreux 55a1b940a0
Split RegisterAfterUpsert into RegisterAfterAdd and RegisterAfterUpdate (#644)
* Split RegisterAfterUpsert into two

We're going to need to be able to differentiate between Add and Update
for storing events in the _events table.

* Update mocks
2025-05-30 08:25:12 -04:00
Tom Lebreux cb0d9d6d54
Remove limit and continue param for SQL cache (#643) 2025-05-27 09:53:10 -04:00
Tom Lebreux 1e5018e31a
Fix returning error for empty ingresses.spec.rules.host (#642)
* Fix returning error for empty ingresses.spec.rules.host

* Fix error format

* Use require instead of assert
2025-05-20 16:14:19 -04:00