Skip to content

Conversation

joamaki
Copy link
Contributor

@joamaki joamaki commented Jan 15, 2024

This upstreams the improvements to pkg/statedb that arose as part of #30024.

  • statedb.Map function for transforming iterators
  • statedb.Observable function for making a stream.Observable out of a table
  • statedb.Derive function for deriving one table from another with a transform function
  • simplifying index.KeySet to reduce amount of allocation

@joamaki joamaki requested a review from a team as a code owner January 15, 2024 09:26
@maintainer-s-little-helper maintainer-s-little-helper bot added the dont-merge/needs-release-note-label The author needs to describe the release impact of these changes. label Jan 15, 2024
@joamaki joamaki added the release-note/misc This PR makes changes that have no direct user impact. label Jan 15, 2024
@maintainer-s-little-helper maintainer-s-little-helper bot removed the dont-merge/needs-release-note-label The author needs to describe the release impact of these changes. label Jan 15, 2024
@joamaki joamaki enabled auto-merge January 15, 2024 09:30
@joamaki joamaki force-pushed the pr/joamaki/statedb-improvements branch from ad9bed9 to f9addee Compare January 15, 2024 15:46
@joamaki
Copy link
Contributor Author

joamaki commented Jan 15, 2024

/test

Table[T].NumObjects() returns the number of objects in the table
in O(1) time.

Derive transforms objects from an input table to an output table.
Useful in conjunction with a reconciler where the desired state
is derived from a single input table.

Example:

  // Assuming we have Table[*Foo] and RWTable[*Bar] and that
  // *Bar is an object we want reconciled.
  cell.Invoke(
    statedb.Derive[*Foo, *Bar](
      func(foo *Foo, deleted bool) (*Bar, statedb.DeriveResult) {
        if deleted {
            return &Bar{
              ID: foo.ID, // Only need enough for primary key
              Status: reconciler.StatusPendingDelete(),
            }, statedb.DeriveUpdate
        }
        return &Bar{
          ID: foo.ID,
          Quux: foo.Quux,
          Status: reconciler.StatusPending(),
        }, statedb.DeriveInsert
      },
    ),
  )

Signed-off-by: Jussi Maki <jussi@isovalent.com>
Memory profiling showed that we were allocating fair bit in NewKeySet.
This can be avoided in cases where the indexer only returns a single
key by using a special case implementation.

This replaces KeySet with a struct of head & tail. This allows writing
indexers that can return a constant and avoid all memory allocation for
the key.

Signed-off-by: Jussi Maki <jussi@isovalent.com>
Useful when combined with CollectSet:

  type Foo struct {
    Key string
  } 
  var iter Iterator[Foo]
  var keys sets.Set[string]
  keys = CollectSet(
    Map(
      iter,
      func(f Foo) string { return f.Key }))

Signed-off-by: Jussi Maki <jussi@isovalent.com>
As we already have lots of code doing processing via event streams (e.g. Resource[T]),
make it easier to migrate to the Kubernetes source from Resource[T] by making it possible
to observe the table as a stream.Observable. 

The workqueue that Resource[T] is not supported. It wasn't used in many cases anyway and
for those the workqueue can be implemented directly.

Signed-off-by: Jussi Maki <jussi@isovalent.com>
@joamaki joamaki force-pushed the pr/joamaki/statedb-improvements branch from f9addee to 1b08bf5 Compare January 16, 2024 13:59
@joamaki
Copy link
Contributor Author

joamaki commented Jan 16, 2024

/test

@joamaki joamaki added this pull request to the merge queue Jan 16, 2024
@maintainer-s-little-helper maintainer-s-little-helper bot added the ready-to-merge This PR has passed all tests and received consensus from code owners to merge. label Jan 16, 2024
Merged via the queue into cilium:main with commit 7cc920b Jan 16, 2024
@joamaki joamaki deleted the pr/joamaki/statedb-improvements branch January 16, 2024 16:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready-to-merge This PR has passed all tests and received consensus from code owners to merge. release-note/misc This PR makes changes that have no direct user impact.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants