Skip to content

Commit

Permalink
Merge pull request #162 from kytos-ng/chore/db_index_script
Browse files Browse the repository at this point in the history
chore: added scripts/drop_compound_index.py
  • Loading branch information
viniarck authored Aug 22, 2023
2 parents bdfe9de + 76d071d commit f6e0ea9
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Changed
General Information
===================
- ``@rest`` endpoints are now run by ``starlette/uvicorn`` instead of ``flask/werkzeug``.
- ``scripts/drop_compound_index.py`` can be used to drop a compound index that has changed. If you tried to upgrade to ``2022.3.2`` before and it ended up creating ``'flow.priority_1'`` index, then you also want to delete it by running ``CMD=drop_index INDEX_NAME=flow.priority_1 python3 drop_compound_index.py``

Fixed
=====
Expand Down
4 changes: 2 additions & 2 deletions controllers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ def bootstrap_indexes(self) -> None:
index_tuples = [
("flows", [("flow_id", pymongo.ASCENDING)], {"unique": True}),
("flows", [("flow.cookie", pymongo.ASCENDING)], {}),
("flows", [("flow.priority", pymongo.ASCENDING)], {}),
("flows", [("flow.priority", pymongo.DESCENDING)], {}),
("flows", [("state", pymongo.ASCENDING)], {}),
(
"flows",
[
("switch", pymongo.ASCENDING),
("flow.cookie", pymongo.ASCENDING),
("flow.priority", pymongo.ASCENDING),
("state", pymongo.ASCENDING),
("flow.priority", pymongo.DESCENDING),
("inserted_at", pymongo.ASCENDING),
("updated_at", pymongo.ASCENDING),
],
Expand Down
8 changes: 8 additions & 0 deletions scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,11 @@ Run the script to upgrade all flows from `mef_eline`, `of_lldp` and `coloring` w
```
python3 pipeline_related.py
```

### Drop compound index

On version `2023.1`, this `flows` compound index `switch_1_flow.cookie_1_state_1_inserted_at_1_updated_at_1` has changed. If you're upgrading to `2023.1` froma previous version, you should run the `drop_compound_index.py` script to drop it:

```
CMD=drop_index python3 drop_compound_index.py
```
40 changes: 40 additions & 0 deletions scripts/drop_compound_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import os
import pymongo

from napps.kytos.flow_manager.controllers import FlowController


flow_controller = FlowController()


def drop_index(index_name=None, flow_controller=flow_controller) -> dict:
"""drop_index."""
index_name = index_name or os.environ.get(
"INDEX_NAME", "switch_1_flow.cookie_1_state_1_inserted_at_1_updated_at_1"
)
return flow_controller.db.flows.drop_index(index_name)


if __name__ == "__main__":
cmds = {
"drop_index": drop_index,
}
try:
cmd = os.environ["CMD"]
except KeyError:
print("Please set the 'CMD' env var.")
sys.exit(1)
try:
for command in cmd.split(","):
print(cmds[command]())
except KeyError as e:
print(
f"Unknown cmd: {str(e)}. 'CMD' env var has to be one of these {list(cmds.keys())}."
)
sys.exit(1)
except pymongo.errors.PyMongoError as e:
print(f"pymongo error: {str(e)}")
sys.exit(1)
4 changes: 2 additions & 2 deletions tests/unit/test_flow_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ def test_boostrap_indexes(self) -> None:
expected_indexes = [
("flows", [("flow_id", 1)]),
("flows", [("flow.cookie", 1)]),
("flows", [("flow.priority", 1)]),
("flows", [("flow.priority", -1)]),
("flows", [("state", 1)]),
(
"flows",
[
("switch", 1),
("flow.cookie", 1),
("flow.priority", 1),
("state", 1),
("flow.priority", -1),
("inserted_at", 1),
("updated_at", 1),
],
Expand Down

0 comments on commit f6e0ea9

Please sign in to comment.