Skip to content
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

chore: added scripts/drop_compound_index.py #162

Merged
merged 5 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

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),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There will be future work to further augment and/or optimize index #161, but for now I decided to only cover the minimum necessary to minimize changes and risk to impact on other queries. I've changed priority to be descending since that's how it's being sorted and queried when finding flows.

("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)