Skip to content

Commit

Permalink
readme file wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Amey Tendulkar committed Nov 3, 2023
1 parent 05b6c7a commit 225b34d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,27 @@ This is a POC (proof of concept) project aimed to learn and implement system des

## Setting up

We are using docker to setup and run the project, so it should not take time to setup and make this project running on your machine.
### setup log: create directory 'logs' before running

1. docker-compose up
2. run migrations on primary db:docker-compose exec webserver python manage.py migrate --database=primary
3. populate data into database: docker-compose exec webserver python manage.py populatedb
4. generate backups of primary database and restore into replicas (still learning how to setup auto sync from master to replicas)
5. generate backup sql file: docker-compose exec db pg_dump -U postgres -d globalmart -f /backup.sql
6. copy into ur machine: docker cp db-master-globalmart:/backup.sql "your machine path"
7. copy this file into replicas: docker cp "machine path" db-replica1-globalmart:/backup.sql
8. restore sql file into replicas:docker-compose exec db-replica1 psql -U postgres -d globalmart -a -f /backup.sql
9. you can use pgadmin tool to login and view your databases. username and password is in docker-compose file. it works on port 5051.

## Running

Hit the following GET Api: http://localhost:8000/api/products-by-category?category=4&page=1
<br>
you can change the paramters.

<p>Observe the results in the terminal or in the log file, you can check which db the query went: replica1 or replica2.</p>

<p>I have created a small test file which runs 'N' nos of api requests, so we can see the queries going to replicas under load.
file: dbroutertest.py, run this file outside the container</p>

## Results
6 changes: 3 additions & 3 deletions api/management/commands/populateDb.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def handle(self, *args, **options):
Category.objects.bulk_create(bulk_add_categories)

category_objects = {}
categories_objects = Category.objects.all()
categories_objects = Category.objects.using('primary').all()
for category in categories_objects:
category_objects[category.name] = category

Expand All @@ -44,7 +44,7 @@ def handle(self, *args, **options):

SubCategory.objects.bulk_create(bulk_add_sub_categories)
subcategory_objects = {}
subcategories_objects = SubCategory.objects.all()
subcategories_objects = SubCategory.objects.using('primary').all()
for subcategory in subcategories_objects:
subcategory_objects[subcategory.name] = subcategory

Expand Down Expand Up @@ -79,5 +79,5 @@ def generate_product():
bulk_add_products.append(product_obj)

Product.objects.bulk_create(bulk_add_products)
self.stdout.write(self.style.SUCCESS('Successfully executed your custom script'))
self.stdout.write(self.style.SUCCESS('Successfully populated database.'))

2 changes: 1 addition & 1 deletion dbroutertest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import requests

def callapi():
url = "http://localhost:8000/api/products-by-category?category=21&page=2"
url = "http://localhost:8000/api/products-by-category?category=7&page=1"

payload = {}
headers = {}
Expand Down

0 comments on commit 225b34d

Please sign in to comment.