Skip to content

Commit 8b983be

Browse files
geofffranksameowlia
authored andcommitted
Speeds up ASG syncing and Querying
- Normalizes DB data with join tables associating security_groups and spaces - Adds a migration to prepopulate associations prior to the next sync - When syncing, asg-syncer only updates security groups in the database that have changed compared to the existing data - Database updates are done in bulk to speed up transaction time - Significantly speeds up the database query for retrieving security groups by space guid using the normalized tables for filtering
1 parent de88e2d commit 8b983be

File tree

17 files changed

+1185
-114
lines changed

17 files changed

+1185
-114
lines changed

docs/07-network-policy-database-overview.md

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ Below are all of the tables in the `network_policy` database.
6161
| gorp_migrations | Record of which migrations have been run. |
6262
| groups | List of all apps that are either the source or destination of a network policy. |
6363
| policies | List of source apps and destination metadata for network policies. |
64-
64+
| policies_info | A single row indicating the last update time of any network policy, used to save on DB queries from vxlan-policy-agent |
65+
| security_groups | Lists all security groups defined in CAPI. This is populated by policy-server-asg-syncer, and is not a source of truth. |
66+
| security_groups_info | A single row indicating the last update time of any security group info, used to save on DB queries from vxlan-policy-agent |
67+
| running_security_groups_spaces | A join table associating security groups to the spaces they are bound to for running lifecycle workloads. |
68+
| staging_security_groups_spaces | A join table associating security groups to the spaes they are bound to for staging lifecycle workloads. |
6569

6670
The following tables were related to dynamic egress, which has been removed
6771
from the codebase. These tables should no longer present in your database as of
@@ -181,14 +185,91 @@ mysql> select * from policies; mysql> select * from destinations;
181185
| 2 | 5346072e-7265-45f9-b70a-80c42e3f13ae | app <--+
182186
| 3 | NULL | app |
183187
+----+--------------------------------------+------+
188+
```
189+
190+
## <a name="security-groups-tables"</a> Security Group Related Tables
191+
192+
There are three tables storing information about security groups: security_groups, running_security_groups_spaces,
193+
and staging_security_groups_spaces.
194+
184195

196+
### <a name="security-groups-table"></a> security_groups
197+
This table stores a copy of all security groups found in CAPI, so vxlan-policy-agent can query
198+
policy-server-internal for this information, rather than overwhelm CAPI with requests. Its data is
199+
synced and updated via the policy-server-asg-syncer process, and is not a source of truth for ASG data.
185200

186201
```
202+
mysql> describe security_groups;
203+
+-----------------+--------------+------+-----+---------+----------------+
204+
| Field | Type | Null | Key | Default | Extra |
205+
+-----------------+--------------+------+-----+---------+----------------+
206+
| id | bigint | NO | PRI | NULL | auto_increment |
207+
| guid | varchar(36) | NO | UNI | NULL | |
208+
| name | varchar(255) | NO | | NULL | |
209+
| rules | mediumtext | YES | | NULL | |
210+
| staging_default | tinyint(1) | YES | MUL | 0 | |
211+
| running_default | tinyint(1) | YES | MUL | 0 | |
212+
| staging_spaces | json | YES | | NULL | |
213+
| running_spaces | json | YES | | NULL | |
214+
+-----------------+--------------+------+-----+---------+----------------+
215+
```
216+
217+
| Field | Note |
218+
|---|---|
219+
| id | An internal id for each record |
220+
| guid | The CAPI GUID of the security group |
221+
| name | The name of the security group as it appears in CAPI |
222+
| hash | A SHA256 hash of the ASG data, used to check whether records need updating during policy-server-asg-syncer polls |
223+
| rules | The rules associated with the ASG defined in CAPI |
224+
| staging_default | Whether or not this is a globally bound security group for `staging` lifecycles |
225+
| running_default | Whether or not this is a globally bound security group for `running` lifecycles |
226+
| staging_spaces | A json list of all spaces this security group is bound to for the `staging` lifecycle |
227+
| running_spaces | A json list of all spaces this security group is bound to for the `running` lifecycle |
228+
229+
### <a name="running-security-groups-spaces-table"></a> running_security_groups_spaces
230+
This table is a join table to enable faster querying of security_groups when filtering by
231+
running_space guids. It is synced and updated via the policy-server-asg-syncer process, and is not a source of
232+
truth for ASG data.
187233

234+
```
235+
mysql> describe running_security_groups_spaces;
236+
+---------------------+--------------+------+-----+---------+-------+
237+
| Field | Type | Null | Key | Default | Extra |
238+
+---------------------+--------------+------+-----+---------+-------+
239+
| space_guid | varchar(255) | NO | PRI | NULL | |
240+
| security_group_guid | varchar(255) | NO | PRI | NULL | |
241+
+---------------------+--------------+------+-----+---------+-------+
242+
```
243+
244+
| Field | Note|
245+
|---|---|
246+
| space_guid | This value is the CAPI guid for the space bound to a given security group via the `running` app lifecycle |
247+
| security_group_guid | This value is the CAPI guid for the security group bound to a given space via the `running` app lifecycle |
248+
249+
250+
### <a name="staging-security-groups-spaces-table"></a> staging_security_groups_spaces
251+
This table is a join table to enable faster querying of security_groups when filtering by
252+
staging_space guids. It is synced and updated via the policy-server-asg-syncer process, and is not a source of
253+
truth for ASG data.
254+
255+
```
256+
mysql> describe staging_security_groups_spaces;
257+
+---------------------+--------------+------+-----+---------+-------+
258+
| Field | Type | Null | Key | Default | Extra |
259+
+---------------------+--------------+------+-----+---------+-------+
260+
| space_guid | varchar(255) | NO | PRI | NULL | |
261+
| security_group_guid | varchar(255) | NO | PRI | NULL | |
262+
+---------------------+--------------+------+-----+---------+-------+
263+
```
264+
265+
| Field | Note|
266+
|---|---|
267+
| space_guid | This value is the CAPI guid for the space bound to a given security group via the `staging` app lifecycle |
268+
| security_group_guid | This value is the CAPI guid for the security group bound to a given space via the `staging` app lifecycle |
188269

189270
## <a name="migrations-tables"></a> Migration Related Tables
190271

191-
There are two tables related to migraitons: gorp_migrations and gorp_lock.
272+
There are two tables related to migrations: gorp_migrations and gorp_lock.
192273

193274
### <a name="gorp-mirations-table"></a> gorp_migrations
194275
This table tracks what database migrations have been applied.

src/code.cloudfoundry.org/policy-server/cmd/policy-server-asg-syncer/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ func main() {
7373
}
7474

7575
securityGroupsStore := &store.SGStore{
76-
Conn: connectionPool,
76+
Logger: logger.Session("security-groups-store"),
77+
Conn: connectionPool,
7778
}
7879

7980
metricsSender := &metrics.MetricsSender{

src/code.cloudfoundry.org/policy-server/cmd/policy-server-internal/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ func main() {
8080
)
8181

8282
securityGroupsStore := &store.SGStore{
83-
Conn: connectionPool,
83+
Logger: logger.Session("security-groups-store"),
84+
Conn: connectionPool,
8485
}
8586

8687
tagDataStore := store.NewTagStore(connectionPool, &store.GroupTable{}, conf.TagLength)

src/code.cloudfoundry.org/policy-server/store/helpers/helpers.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,3 @@ func RebindForSQLDialect(query, dialect string) string {
3535
}
3636
return strings.Join(strParts, "")
3737
}
38-
39-
func RebindForSQLDialectAndMark(query, dialect, mark string) string {
40-
if dialect != Postgres && dialect != MySQL {
41-
panic(fmt.Sprintf("Unrecognized DB dialect '%s'", dialect))
42-
}
43-
44-
if dialect == MySQL {
45-
return strings.ReplaceAll(query, mark, "?")
46-
}
47-
48-
strParts := strings.Split(query, mark)
49-
for i := 1; i < len(strParts); i++ {
50-
strParts[i-1] = fmt.Sprintf("%s$%d", strParts[i-1], i)
51-
}
52-
return strings.Join(strParts, "")
53-
}

src/code.cloudfoundry.org/policy-server/store/migrations/migrations.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,4 +472,40 @@ var MigrationsToPerform = PolicyServerMigrations{
472472
Id: "91",
473473
Up: migration_v0091,
474474
},
475+
PolicyServerMigration{
476+
Id: "92",
477+
Up: migration_v0092,
478+
},
479+
PolicyServerMigration{
480+
Id: "93",
481+
Up: migration_v0093,
482+
},
483+
PolicyServerMigration{
484+
Id: "94",
485+
Up: migration_v0094,
486+
},
487+
PolicyServerMigration{
488+
Id: "95",
489+
Up: migration_v0095,
490+
},
491+
PolicyServerMigration{
492+
Id: "96",
493+
Up: migration_v0096,
494+
},
495+
PolicyServerMigration{
496+
Id: "97",
497+
Up: migration_v0097,
498+
},
499+
PolicyServerMigration{
500+
Id: "98",
501+
Up: migration_v0098,
502+
},
503+
PolicyServerMigration{
504+
Id: "99",
505+
Up: migration_v0099,
506+
},
507+
PolicyServerMigration{
508+
Id: "100",
509+
Up: migration_v0100,
510+
},
475511
}

0 commit comments

Comments
 (0)