Skip to content

statedb: merge table creation and registration into a single API #97

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

Merged
merged 1 commit into from
Jul 31, 2025

Conversation

giorio94
Copy link
Member

@giorio94 giorio94 commented Jul 25, 2025

Currently, table creation and registration with the database are two separate operations, respectively performed by [NewTable] and [RegisterTable]. However, it is not actually possible to use a table before it has been registered, and it is easy to either forget to register the table altogether, or perform that too late (e.g., when using dependency injection systems such as Hive/Cell, and registration is performed via [cell.Invoke]).

This commit unifies the two APIs into one, letting [NewTable] take a database as parameter, and internally performing the registration automatically. [RegisterTable] is then unexported, as no longer required to be explicitly called by consumers.

This new approach additionally prevents the possibility of registering the same table with two different databases, which would have led to a broken configuration anyhow.

(Leaving as draft for now, until a new version with the other fixes is tagged)

Currently, table creation and registration with the database are
two separate operations, respectively performed by [NewTable] and
[RegisterTable]. However, it is not actually possible to use a
table before it has been registered, and it is easy to either
forget to register the table altogether, or perform that too
late (e.g., when using dependency injection systems such as
Hive/Cell, and registration is performed via [cell.Invoke]).

This commit unifies the two APIs into one, letting [NewTable] take
a database as parameter, and internally performing the registration
automatically. [RegisterTable] is then unexported, as no longer
required to be explicitly called by consumers.

This new approach additionally prevents the possibility of
registering the same table with two different databases, which
would have led to a broken configuration anyhow.

Signed-off-by: Marco Iorio <marco.iorio@isovalent.com>
@giorio94 giorio94 requested a review from joamaki July 25, 2025 14:30
Copy link

$ make
go build ./...
go: downloading go1.24.0 (linux/amd64)
go: downloading go.yaml.in/yaml/v3 v3.0.3
go: downloading github.com/cilium/hive v0.0.0-20250522123230-2946c4940f41
go: downloading golang.org/x/time v0.5.0
go: downloading github.com/spf13/cobra v1.8.0
go: downloading github.com/spf13/pflag v1.0.5
go: downloading github.com/cilium/stream v0.0.0-20240209152734-a0792b51812d
go: downloading github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de
go: downloading github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
go: downloading github.com/go-viper/mapstructure/v2 v2.2.1
go: downloading go.uber.org/dig v1.17.1
go: downloading golang.org/x/term v0.16.0
go: downloading github.com/spf13/viper v1.18.2
go: downloading golang.org/x/sys v0.17.0
go: downloading golang.org/x/tools v0.17.0
go: downloading github.com/spf13/cast v1.6.0
go: downloading github.com/fsnotify/fsnotify v1.7.0
go: downloading github.com/mitchellh/mapstructure v1.5.0
go: downloading github.com/sagikazarmark/slog-shim v0.1.0
go: downloading github.com/spf13/afero v1.11.0
go: downloading golang.org/x/text v0.14.0
go: downloading github.com/subosito/gotenv v1.6.0
go: downloading github.com/hashicorp/hcl v1.0.0
go: downloading gopkg.in/ini.v1 v1.67.0
go: downloading github.com/magiconair/properties v1.8.7
go: downloading github.com/pelletier/go-toml/v2 v2.1.0
go: downloading gopkg.in/yaml.v3 v3.0.1
go test ./... -cover -vet=all -test.count 1
go: downloading github.com/stretchr/testify v1.8.4
go: downloading go.uber.org/goleak v1.3.0
go: downloading golang.org/x/exp v0.0.0-20240119083558-1b970713d09a
go: downloading github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2
ok  	github.com/cilium/statedb	23.042s	coverage: 83.5% of statements
ok  	github.com/cilium/statedb/index	0.008s	coverage: 28.7% of statements
ok  	github.com/cilium/statedb/internal	0.021s	coverage: 46.7% of statements
ok  	github.com/cilium/statedb/part	5.128s	coverage: 87.1% of statements
ok  	github.com/cilium/statedb/reconciler	0.234s	coverage: 88.5% of statements
	github.com/cilium/statedb/reconciler/benchmark		coverage: 0.0% of statements
	github.com/cilium/statedb/reconciler/example		coverage: 0.0% of statements
go test -race ./... -test.count 1
ok  	github.com/cilium/statedb	101.133s
ok  	github.com/cilium/statedb/index	1.014s
ok  	github.com/cilium/statedb/internal	1.023s
ok  	github.com/cilium/statedb/part	38.421s
ok  	github.com/cilium/statedb/reconciler	1.316s
?   	github.com/cilium/statedb/reconciler/benchmark	[no test files]
?   	github.com/cilium/statedb/reconciler/example	[no test files]
go test ./... -bench . -benchmem -test.run xxx
goos: linux
goarch: amd64
pkg: github.com/cilium/statedb
cpu: AMD EPYC 7763 64-Core Processor                
BenchmarkDB_WriteTxn_1-4                    	  496732	      2317 ns/op	    431526 objects/sec	    1448 B/op	      26 allocs/op
BenchmarkDB_WriteTxn_10-4                   	 1335387	       901.0 ns/op	   1109860 objects/sec	     513 B/op	       9 allocs/op
BenchmarkDB_WriteTxn_100-4                  	 1749117	       689.3 ns/op	   1450750 objects/sec	     430 B/op	       7 allocs/op
BenchmarkDB_WriteTxn_1000-4                 	 1466455	       731.2 ns/op	   1367669 objects/sec	     391 B/op	       7 allocs/op
BenchmarkDB_WriteTxn_100_SecondaryIndex-4   	  528438	      2233 ns/op	    447864 objects/sec	    1259 B/op	      37 allocs/op
BenchmarkDB_NewWriteTxn-4                   	 1423048	       843.3 ns/op	     544 B/op	       8 allocs/op
BenchmarkDB_NewReadTxn-4                    	545996874	         2.197 ns/op	       0 B/op	       0 allocs/op
BenchmarkDB_Modify-4                        	    1480	    810056 ns/op	   1234482 objects/sec	  445974 B/op	    8136 allocs/op
BenchmarkDB_GetInsert-4                     	    1404	    869764 ns/op	   1149737 objects/sec	  430053 B/op	    8136 allocs/op
BenchmarkDB_RandomInsert-4                  	    1537	    746405 ns/op	   1339755 objects/sec	  422239 B/op	    7136 allocs/op
BenchmarkDB_RandomReplace-4                 	     355	   3373961 ns/op	    296388 objects/sec	 2022457 B/op	   49160 allocs/op
BenchmarkDB_SequentialInsert-4              	    1704	    728082 ns/op	   1373471 objects/sec	  391426 B/op	    7110 allocs/op
BenchmarkDB_SequentialInsert_Prefix-4       	     370	   3238924 ns/op	    308745 objects/sec	 3750165 B/op	   58568 allocs/op
BenchmarkDB_Changes_Baseline-4              	    1332	    906588 ns/op	   1103037 objects/sec	  465683 B/op	   10214 allocs/op
BenchmarkDB_Changes-4                       	     676	   1640551 ns/op	    609551 objects/sec	  820257 B/op	   14420 allocs/op
BenchmarkDB_RandomLookup-4                  	   20467	     58124 ns/op	  17204478 objects/sec	       0 B/op	       0 allocs/op
BenchmarkDB_SequentialLookup-4              	   23895	     50344 ns/op	  19863456 objects/sec	       0 B/op	       0 allocs/op
BenchmarkDB_Prefix_SecondaryIndex-4         	    6430	    171688 ns/op	   5824532 objects/sec	  125115 B/op	    1031 allocs/op
BenchmarkDB_FullIteration_All-4             	     945	   1371094 ns/op	  72934448 objects/sec	     320 B/op	      11 allocs/op
BenchmarkDB_FullIteration_Get-4             	     175	   6950765 ns/op	  14386906 objects/sec	       0 B/op	       0 allocs/op
BenchmarkDB_FullIteration_ReadTxnGet-4      	     177	   6629932 ns/op	  15083110 objects/sec	       0 B/op	       0 allocs/op
BenchmarkDB_PropagationDelay-4              	  556870	      2132 ns/op	        18.00 50th_µs	        23.00 90th_µs	        51.00 99th_µs	    1133 B/op	      22 allocs/op
BenchmarkWatchSet_4-4                       	 1260198	       953.5 ns/op	     500 B/op	       8 allocs/op
BenchmarkWatchSet_16-4                      	  457794	      2557 ns/op	    1701 B/op	       8 allocs/op
BenchmarkWatchSet_128-4                     	   57258	     20995 ns/op	   13391 B/op	       8 allocs/op
BenchmarkWatchSet_1024-4                    	    5608	    204410 ns/op	  105640 B/op	       8 allocs/op
PASS
ok  	github.com/cilium/statedb	33.783s
PASS
ok  	github.com/cilium/statedb/index	0.004s
PASS
ok  	github.com/cilium/statedb/internal	0.003s
goos: linux
goarch: amd64
pkg: github.com/cilium/statedb/part
cpu: AMD EPYC 7763 64-Core Processor                
Benchmark_Uint64Map_Random-4        	    1252	    941220 ns/op	   1062451 items/sec	 2813703 B/op	   11034 allocs/op
Benchmark_Uint64Map_Sequential-4    	    1370	    928359 ns/op	   1077170 items/sec	 2591255 B/op	   11749 allocs/op
Benchmark_Insert_RootOnlyWatch-4    	    9055	    126923 ns/op	   7878820 objects/sec	   90621 B/op	    2044 allocs/op
Benchmark_Insert-4                  	    6902	    183566 ns/op	   5447629 objects/sec	  206588 B/op	    3076 allocs/op
Benchmark_Modify-4                  	    8803	    119630 ns/op	   8359137 objects/sec	  122023 B/op	    1052 allocs/op
Benchmark_GetInsert-4               	    7482	    164384 ns/op	   6083308 objects/sec	  122125 B/op	    1052 allocs/op
Benchmark_Replace-4                 	27015116	        46.15 ns/op	  21669333 objects/sec	       0 B/op	       0 allocs/op
Benchmark_Replace_RootOnlyWatch-4   	25194508	        46.10 ns/op	  21693730 objects/sec	       0 B/op	       0 allocs/op
Benchmark_txn_1-4                   	 3118710	       391.8 ns/op	   2552246 objects/sec	     296 B/op	       5 allocs/op
Benchmark_txn_10-4                  	 8071335	       148.9 ns/op	   6717675 objects/sec	     113 B/op	       2 allocs/op
Benchmark_txn_100-4                 	 9342284	       124.1 ns/op	   8059977 objects/sec	     130 B/op	       2 allocs/op
Benchmark_txn_1000-4                	 8677012	       138.6 ns/op	   7214383 objects/sec	     126 B/op	       2 allocs/op
Benchmark_txn_delete_1-4            	 1456671	       817.9 ns/op	   1222674 objects/sec	    3272 B/op	       8 allocs/op
Benchmark_txn_delete_10-4           	 6931958	       177.4 ns/op	   5635415 objects/sec	     368 B/op	       2 allocs/op
Benchmark_txn_delete_100-4          	10551546	       112.6 ns/op	   8880097 objects/sec	      82 B/op	       1 allocs/op
Benchmark_txn_delete_1000-4         	11414845	       106.4 ns/op	   9398130 objects/sec	      28 B/op	       1 allocs/op
Benchmark_Get-4                     	   32383	     36832 ns/op	  27149982 objects/sec	       0 B/op	       0 allocs/op
Benchmark_Iterate-4                 	  156332	      7386 ns/op	 135398167 objects/sec	     104 B/op	       4 allocs/op
Benchmark_Hashmap_Insert-4          	   14725	     81709 ns/op	  12238589 objects/sec	   74265 B/op	      20 allocs/op
Benchmark_Hashmap_Get_Uint64-4      	  135076	      8821 ns/op	 113359840 objects/sec	       0 B/op	       0 allocs/op
Benchmark_Hashmap_Get_Bytes-4       	  110960	     10799 ns/op	  92602892 objects/sec	       0 B/op	       0 allocs/op
PASS
ok  	github.com/cilium/statedb/part	27.188s
PASS
ok  	github.com/cilium/statedb/reconciler	0.005s
?   	github.com/cilium/statedb/reconciler/benchmark	[no test files]
?   	github.com/cilium/statedb/reconciler/example	[no test files]
go run ./reconciler/benchmark -quiet
1000000 objects reconciled in 2.64 seconds (batch size 1000)
Throughput 378899.13 objects per second
1225MB total allocated, 6011105 in-use objects, 384MB bytes in use

@giorio94 giorio94 marked this pull request as ready for review July 30, 2025 10:20
@giorio94 giorio94 requested a review from a team as a code owner July 30, 2025 10:20
@joamaki joamaki merged commit d9e95d5 into main Jul 31, 2025
1 check passed
@joamaki joamaki deleted the pr/giorio94/merge-new-register-table branch July 31, 2025 07:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants