Skip to content

Commit 52f9cb2

Browse files
authored
Merge pull request #480 from topcoder-platform/feature/improve-local-setup
Improve local setup
2 parents e24ea3a + 4b5cafa commit 52f9cb2

File tree

12 files changed

+288
-154
lines changed

12 files changed

+288
-154
lines changed

README.md

Lines changed: 169 additions & 122 deletions
Large diffs are not rendered by default.

config/test.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"rabbitmqUrl": "amqp://localhost:5672",
1818
"connectProjectsUrl": "https://local.topcoder-dev.com/projects/",
1919
"dbConfig": {
20-
"masterUrl": "postgres://coder:mysecretpassword@localhost:5433/projectsdb_test",
20+
"masterUrl": "postgres://coder:mysecretpassword@localhost:5432/projectsdb_test",
2121
"maxPoolSize": 50,
2222
"minPoolSize": 4,
2323
"idleTimeout": 1000
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Topcoder Project Service Architecture
2+
3+
- [Overview](#overview)
4+
- [Elasticsearch indexing](#elasticsearch-indexing)
5+
- [Read data](#read-data)
6+
- [Write data](#write-data)
7+
- [Kafka messages structure](#kafka-messages-structure)
8+
9+
## Overview
10+
11+
Topcoder Project Service is a microservice to manage CRUD operations for all things related to Projects. To communicate with other microservices like `tc-notifications`, `project-processor-es`, `legacy-project-processor` and event with itself **Project Service** produces Kafka messages and these service listen to the Kafka messages do some stuff. **Project Service** don't send Kafka messages directly, but uses a special service to send Kafka messages called `tc-bus-api`. So no matter what service we want to update, first we have to setup Kafka with Zookeeper and `tc-bus-api`.
12+
13+
![diagram](./images/diagram.svg)
14+
15+
*This diagram shows just some part of relations and services that are most important, it doesn't show all of them.*
16+
17+
## Elasticsearch indexing
18+
19+
It's important to keep in mind how the indexing and reading data from Elasticsearch works.
20+
21+
### Read data
22+
23+
As per global Topcoder API V5 standards, all endpoints in **Project Service** should get data from the Elasticsearch index first. If no data is found, endpoints should try to get data from Database.
24+
25+
### Write data
26+
27+
When some data is updated by **Project Service** it's directly changed in the Database. But **Project Service** doesn't change data in Elasticsearch directly. Instead of that, when some data is changed **Project Service** sends event to the Kafka (using `tc-bus-api`), and `project-processor-es` listens to the Kafka event and index updated data in Elasticsearch for **Project Service**.
28+
As a consequences, data in Elasticsearch is not updated immediately.
29+
30+
## Kafka messages structure
31+
32+
Project Service should send messages to 3 Kafka topics:
33+
- `project.action.create` - when some objects is created
34+
- `project.action.update` - when some objects is updated
35+
- `project.action.delete` - when some objects is deleted
36+
37+
The `payload` of any of this messages should contain the next required properties:
38+
```js
39+
payload: {
40+
// the name of the resource which has been create, updated or deleted,
41+
// see constant `RESOURCES` in `src/constants.js` for possible values
42+
"resource": "...",
43+
44+
// object which has been created, updated or deleted
45+
"data": {...},
46+
47+
// should be present only in `project.action.update` topic, and contain the objects before update
48+
"previousData": {...},
49+
}
50+
```
51+
52+
Example:
53+
```js
54+
topic: "project.action.update",
55+
payload: {
56+
"resource": "project.template",
57+
"data": {
58+
"id": 1234,
59+
"name": "Name of project template UPDATED",
60+
"key": "app",
61+
"createdAt": 1,
62+
"createdBy": 1,
63+
"updatedAt": 2,
64+
"updatedBy": 2,
65+
},
66+
"previousData": {
67+
"id": 1234,
68+
"name": "Name of project template",
69+
"key": "app",
70+
"createdAt": 1,
71+
"createdBy": 1,
72+
"updatedAt": 1,
73+
"updatedBy": 1,
74+
},
75+
}
76+
```

docs/guides/architercture/images/diagram.svg

Lines changed: 1 addition & 0 deletions
Loading

local/docker-compose.yml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,13 @@ services:
55
ports:
66
- "3001:3001"
77
db:
8-
image: "postgres:9.5"
8+
build: "postgres-db"
99
ports:
1010
- "5432:5432"
1111
environment:
1212
- POSTGRES_PASSWORD=mysecretpassword
1313
- POSTGRES_USER=coder
14-
- POSTGRES_DB=projectsdb
15-
db_test:
16-
image: "postgres:9.5"
17-
ports:
18-
- "5433:5432"
19-
environment:
20-
- POSTGRES_PASSWORD=mysecretpassword
21-
- POSTGRES_USER=coder
22-
- POSTGRES_DB=projectsdb_test
14+
- POSTGRES_MULTIPLE_DATABASES=projectsdb,projectsdb_test
2315
esearch:
2416
image: "elasticsearch:2.3"
2517
ports:

local/full/docker-compose.base.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ services:
1313
- TC_API_V5_BASE_URL=http://host.docker.internal:8001/v5
1414
- TC_API_V4_BASE_URL=https://api.topcoder-dev.com/v4
1515
- TC_API_V3_BASE_URL=https://api.topcoder-dev.com/v3
16-
- KAFKA_URL=kafka:9092
16+
- KAFKA_URL=kafka:9093
1717
- AUTH_SECRET=secret
18-
- DATABASE_URL=postgresql://coder:mysecretpassword@notifications_db:5432/tc_notifications
18+
- DATABASE_URL=postgresql://coder:mysecretpassword@db:5432/tc_notifications
1919
- JWKS_URI=test
2020
- LOG_LEVEL=debug
2121
- ENV=development

local/full/docker-compose.yml

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,8 @@ services:
88
extends:
99
file: ../docker-compose.yml
1010
service: db
11-
notifications_db:
12-
image: "postgres:9.5"
13-
expose:
14-
- "5432"
1511
environment:
16-
- POSTGRES_PASSWORD=mysecretpassword
17-
- POSTGRES_USER=coder
18-
- POSTGRES_DB=tc_notifications
19-
db_test:
20-
extends:
21-
file: ../docker-compose.yml
22-
service: db_test
12+
- POSTGRES_MULTIPLE_DATABASES=projectsdb,projectsdb_test,tc_notifications
2313
esearch:
2414
extends:
2515
file: ../docker-compose.yml
@@ -29,18 +19,24 @@ services:
2919
file: ../docker-compose.yml
3020
service: queue
3121
zookeeper:
32-
image: confluent/zookeeper
22+
image: wurstmeister/zookeeper
3323
ports:
3424
- "2181:2181"
3525
environment:
3626
zk_id: "1"
3727
kafka:
38-
image: confluent/kafka
28+
image: wurstmeister/kafka
3929
container_name: tc-projects-kafka
4030
depends_on:
4131
- zookeeper
4232
ports:
4333
- "9092:9092"
34+
environment:
35+
KAFKA_ADVERTISED_LISTENERS: INSIDE://kafka:9093,OUTSIDE://localhost:9092
36+
KAFKA_LISTENERS: INSIDE://kafka:9093,OUTSIDE://0.0.0.0:9092
37+
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INSIDE:PLAINTEXT,OUTSIDE:PLAINTEXT
38+
KAFKA_INTER_BROKER_LISTENER_NAME: INSIDE
39+
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
4440
kafka-client:
4541
build: ./kafka-client
4642
depends_on:
@@ -63,7 +59,7 @@ services:
6359
- kafka-client
6460
environment:
6561
- PORT=3000
66-
- KAFKA_URL=kafka:9092
62+
- KAFKA_URL=kafka:9093
6763
- JWT_TOKEN_SECRET=secret
6864
- VALID_ISSUERS="[\"https://topcoder-newauth.auth0.com/\",\"https://api.topcoder-dev.com\"]"
6965
project-processor-es:
@@ -82,7 +78,7 @@ services:
8278
- kafka-client
8379
environment:
8480
- PORT=5000
85-
- KAFKA_URL=kafka:9092
81+
- KAFKA_URL=kafka:9093
8682
- ES_HOST=esearch:9200
8783
tc-notifications-reset-db:
8884
extends:
@@ -95,8 +91,7 @@ services:
9591
ports:
9692
- "4002:4002"
9793
depends_on:
98-
- notifications_db
99-
- esearch
94+
- db
10095
environment:
10196
- PORT=4002
10297
tc-notifications-processor:

local/full/kafka-client/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
From confluent/kafka
1+
From wurstmeister/kafka
22
WORKDIR /app/
33
COPY topics.txt .
44
COPY create-topics.sh .
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
22

33
while read topic; do
4-
/usr/bin/kafka-topics --create --zookeeper zookeeper:2181 --partitions 1 --replication-factor 1 --topic $topic
4+
/opt/kafka/bin/kafka-topics.sh --create --zookeeper zookeeper:2181 --partitions 1 --replication-factor 1 --topic $topic
55
done < topics.txt

local/kafka/kafka-client/create-topics.sh

100644100755
File mode changed.

local/postgres-db/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FROM postgres:9.5
2+
COPY create-multiple-postgresql-databases.sh /docker-entrypoint-initdb.d/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
3+
set -e
4+
set -u
5+
6+
function create_user_and_database() {
7+
local database=$1
8+
echo " Creating database '$database'"
9+
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
10+
CREATE DATABASE $database;
11+
GRANT ALL PRIVILEGES ON DATABASE $database TO $POSTGRES_USER;
12+
EOSQL
13+
}
14+
15+
if [ -n "$POSTGRES_MULTIPLE_DATABASES" ]; then
16+
echo "Multiple database creation requested: $POSTGRES_MULTIPLE_DATABASES"
17+
for db in $(echo $POSTGRES_MULTIPLE_DATABASES | tr ',' ' '); do
18+
create_user_and_database $db
19+
done
20+
echo "Multiple databases created"
21+
fi

0 commit comments

Comments
 (0)