Add Search::Zoekt::Index model
Proposal
This issue is to add Search::Zoekt::Index
The goal is to replace FOREIGN KEY zoekt_indexed_namespaces(zoekt_shard_id) REFERENCES zoekt_shards(id) with this new table and use it for tracking on which shard/node each namespace primary/replica is located.
We'll also need to add a migration to populate this new table with the namespaces we already index.
Implementation plan
- Write up a new module where all of the logic to find
node_idand determine if search is enabled for a project or a namespace will live. Move all existing references toZoekt::IndexedNamespaceto this new module. This step makes it easier to do the subsequent steps - Create a
zoekt_enabled_namespacestable, this will be a replacement forzoekt_indexed_namespaces - Create
zoekt_indicestable - Migrate all data to the new tables created above (in same migration MR)
- Will need to load all existing Zoekt::IndexedNamepace records and call unique on it (there is not a unique constraint on
namespace_idso there could be multiple values) - make sure to respect the existing
searchvalue, if any of the values are set tofalse, it should be set like that in the new table. Especially in the case of two records for the samenamespace_id
- Will need to load all existing Zoekt::IndexedNamepace records and call unique on it (there is not a unique constraint on
- Enable feature flag to use new tables
- Remove feature flag
- Drop
zoekt_indexed_namespacestable
Note: An alternative implementation plan was considered that used the existing tables.
Click to expand
- Write up a new module where all of the logic to find
node_idand determine if search is enabled for a project or a namespace will live. Move all existing references toZoekt::IndexedNamespaceto this new module. This step makes it easier to do the subsequent steps - Create a unique constraint on
namespace_idin thezoekt_indexed_namespacestable (will likely be a multi-milestone effort if unique constraints follow the null constraint guide, will need to make sure that there are no duplicate records - Create
zoekt_indicestable - Migrate all data to the new tables created above
- make sure to respect the existing
searchvalue, if any of the values are set tofalse, it should be set like that in the new table. Especially in the case of two records for the samenamespace_id
- make sure to respect the existing
- Enable feature flag to use new tables
- Remove feature flag
Schema design
SSOT located in: https://gitlab.com/dgruzd/notes/-/blob/main/zoekt_brainstorming/schema_design.md
Model: Search::Zoekt::EnabledNamespace
Table name: zoekt_enabled_namespaces
| name | type | comment |
|---|---|---|
id |
bigint |
PRIMARY KEY |
root_namespace_id |
bigint |
NOT NULL , FK REFERENCES namespaces(id) ON DELETE CASCADE
|
search |
bool |
NOT NULL , search enabled boolean flag |
number_of_replicas |
smallint |
NOT NULL, DEFAULT 1 (skip adding for now) |
created_at |
timestamp |
NOT NULL |
updated_at |
timestamp |
NOT NULL |
Add validation: root_namespace_id can only point to a root namespace
- Indices
btree UNIQUE (root_namespace_id)
Model: Search::Zoekt::Index
Table name: zoekt_indices
| name | type | comment |
|---|---|---|
id |
bigint |
PRIMARY KEY |
zoekt_enabled_namespace_id |
bigint |
FK REFERENCES zoekt_enabled_namespaces(id) ON DELETE SET NULL |
zoekt_node_id |
bigint |
NOT NULL , FK REFERENCES zoekt_nodes(id) ON DELETE CASCADE
|
state |
smallint |
NOT NULL , DEFAULT 0
|
created_at |
timestamp |
NOT NULL |
updated_at |
timestamp |
NOT NULL |
original_zoekt_enabled_namespace_id |
bigint |
NOT NULL |
Add uniqueness validation for zoekt_enabled_namespace_id and zoekt_node_id
- Indices
btree UNIQUE (zoekt_enabled_namespace_id, zoekt_node_id)btree (zoekt_node_id)btree (state)
- State Enum
- 0:
pending - 1:
initializing - 10:
ready - 20:
reallocating - 30:
orphaned - 50:
deleting
- 0:
Add a migration for existing records to set to :ready