-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
203 lines (152 loc) · 5.78 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// user publishes a post (POST request to post DB) - So this is one endpoint that we need to implemenent
// Now we also have to send a on_publish event to kafka queue
// from this queue hashtag counter worker will be listening to this event and will compute the counts of all hashtags in the hashtag db
// for the purpose of this demo, , we will use mongodb for storing both posts and hashtags
package main
import (
"context"
"encoding/json"
"flag"
"log"
"net/http"
"time"
"github.com/segmentio/kafka-go"
"github.com/thedatamonk/hashtag-service/main/workers"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// specify schema of documents in posts and hashtag db
type Post struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
UserName string `bson:"username"`
Content string `bson:"content"`
CreatedAt time.Time `bson:"created_at"`
}
type Hashtag struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
Tag string `bson:"tag"`
Count int `bson:"count"`
}
var postDB *mongo.Database
var hashtagDB *mongo.Database
// Approach 1: Naive Increment Counter
// In this approach, we will increment the counter of each hashtag in the database
func publishPostHandler(w http.ResponseWriter, r *http.Request) {
start := time.Now()
var post Post
err := json.NewDecoder(r.Body).Decode(&post)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
post.ID = primitive.NewObjectID()
post.CreatedAt = time.Now()
// create a collection called posts inside postDB
collection := postDB.Collection("posts")
// insert the newly created post in the post colection
_, err = collection.InsertOne(context.TODO(), post)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// send publish post event to kafka in a separate go-routine
// to reduce the perceived time taken to publish the post
go publishPostEvent(post)
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(post)
elapsed := time.Since(start)
log.Printf("publishPostHandler took %s", elapsed)
}
// Approach 2: we will locally keep a copy of the counters of each tag
// and only after a certain time will write it to mongodb
// a local copy can be kept in a hashmap
// Approach 3: in this approach, we will create a deep copy of the hashmap
// and then use this deep copy to write into the db
// after creating a copy we will clear the original map and then start consuming messages again
// Approach 4: we maintain two maps in this approach - active and passive
// the messages are currently being written to the active map
// when it's full, we will swap the maps and start writing to the passive map
// ma, mp = mp, ma
// So now mp(ma) is being used to update the DB
// while ma(mp) is being used to collect the messages
// this way we can avoid locking the map for a longer time
// Approach 5: in this approach, we will have two brokers
// one broker (partitioned by user_id) will be responsible for receiving posts and extracting hashtags per post
// for each hashtag extracted from the post, send another event to the other broker (parttitioned by hashtag)
// the consumer of this broker will accumulate the number of posts per hashtag and then write it in bulk to mongodb
func initDB() {
// specify client options
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
// connect to mongodb instance with the given client options
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
log.Fatal(err)
}
// if everything is fine, once ping the connection
err = client.Ping(context.TODO(), nil)
if err != nil {
log.Fatal(err)
}
postDB = client.Database("postdb")
hashtagDB = client.Database("hashtagdb")
log.Println("Connected to MongoDB!")
log.Println("postDB:", postDB.Name())
log.Println("hashtagDB:", hashtagDB.Name())
}
var kafkaWriter *kafka.Writer
func initKafka() {
// create a new kafka writer object to send kafka events to the specified topic
kafkaWriter = &kafka.Writer{
Addr: kafka.TCP("localhost:9092"),
Topic: "on_post_publish",
Balancer: &kafka.Hash{},
}
}
func publishPostEvent(post Post) {
start := time.Now()
msg := kafka.Message{
Key: []byte(post.UserName),
Value: []byte(post.Content),
}
err := kafkaWriter.WriteMessages(context.TODO(), msg)
if err != nil {
log.Println("Error publishing post event to Kafka:", err)
} else {
log.Println("Successfully published post event to Kafka")
}
elapsed := time.Since(start)
log.Printf("publishPostEvent took %s", elapsed)
}
var approach_type string
func init() {
flag.StringVar(&approach_type, "approach-type", "naive", "Type of approach to apply for incrementing hashtag counter")
}
func main() {
// parse command line arguments
flag.Parse()
// initialise MongoDB and Kafka
initDB()
initKafka()
// depending upon the approach type, start the corresponding worker
if approach_type == "naive" {
log.Printf("Approach Type: %s", approach_type)
go workers.StartNaiveIncrementWorker(hashtagDB)
} else if approach_type == "naive_local_batching" {
log.Printf("Approach Type: %s", approach_type)
go workers.StartLocalCopyBatchingWorker(hashtagDB)
} else if approach_type == "optimised_deep_copy" {
log.Printf("Approach Type: %s", approach_type)
go workers.StartDeepCopyWorker(hashtagDB)
} else if approach_type == "optimised_two_copies" {
log.Printf("Approach Type: %s", approach_type)
go workers.StartTwoCopiesWorker(hashtagDB)
} else if approach_type == "optimised_two_brokers" {
log.Printf("Approach Type: %s", approach_type)
go workers.StartLocalCopyBatchingWorker(hashtagDB)
} else {
log.Fatalf("Unknown approach type: %s", approach_type)
}
http.HandleFunc("/posts/publish", publishPostHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}