Skip to content

Commit ba19a6d

Browse files
hi-im-arenasdwsda
andauthored
Add Apache Kafka consumer/producer (#14)
* Add consumer/producer/airports workload * fix(circleci): bump executor image * fix(Makefile): go get -> go install * Version 3 implementation * Remove unused Kafka workload Co-authored-by: Peter Szabo <[email protected]>
1 parent 8d9b133 commit ba19a6d

File tree

16 files changed

+512
-114
lines changed

16 files changed

+512
-114
lines changed

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ orbs:
66
jobs:
77
build:
88
docker:
9-
- image: circleci/golang:1.16
9+
- image: cimg/go:1.18
1010
environment:
1111
GOFLAGS: -mod=readonly
1212
steps:
@@ -33,7 +33,7 @@ jobs:
3333
name: Save Go module cache
3434
key: gomod-v1-{{ .Branch }}-{{ checksum "go.sum" }}
3535
paths:
36-
- /go/pkg/mod
36+
- /home/circleci/go/pkg/mod
3737

3838
- restore_cache:
3939
name: Restore license cache

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ check: test-all lint ## Run tests and linters
7878

7979
bin/go-junit-report:
8080
@mkdir -p bin
81-
GOBIN=${PWD}/bin/ go get -u github.com/jstemmer/go-junit-report
81+
GOBIN=${PWD}/bin/ go install github.com/jstemmer/go-junit-report
8282

8383
TEST_PKGS ?= ./...
8484
TEST_REPORT_NAME ?= results.xml

README.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,61 @@ Simply echos back a set string
2424

2525
Available options:
2626

27-
- EHCO_STR - string to echo back
27+
- ECHO_STR - string to echo back
28+
2829

2930
### Subsequent requests
3031

3132
Subsequent request URLs can be set using the `REQUESTS` environment variable. Multiple URLs can be set and must be separated by `space`. A `count` must also be set for each URL using the following syntax: `URL#count`.
3233

34+
### Apache Kafka
35+
36+
Allspark can be used as an Apache Kafka consumer or producer.
37+
A single instance can work as both at the same time.
38+
39+
#### KafkaServer
40+
The Kafka server is a consumer that triggers `REQUESTS` when a message is consumed from the topic specified with the below option.
41+
Available options:
42+
- `KAFKASERVER_BOOTSTRAP_SERVER`
43+
44+
The kafka bootstrap server where the cluster can be reached.
45+
Required.
46+
47+
e.g.
48+
```yaml
49+
name: KAFKASERVER_BOOTSTRAP_SERVER
50+
value: "kafka-all-broker.kafka.svc.cluster.local:29092"
51+
```
52+
- `KAFKASERVER_TOPIC`
53+
54+
Starts a kafka consumer for the topic passed in as value.
55+
56+
e.g.
57+
```yaml
58+
name: KAFKASERVER_CONSUMER
59+
value: "example-topic"
60+
```
61+
- `KAFKASERVER_CONSUMER_GROUP`
62+
63+
Sets the consumer group id for the kafka consumer.
64+
65+
If not set it gets defaulted to `allspark-consumer-group`.
66+
67+
e.g.
68+
```yaml
69+
name: KAFKASERVER_CONSUMER_GROUP
70+
value: "example-group"
71+
```
72+
73+
#### Requests
74+
You can use the `REQUESTS` variable to set additional consumers and producers
75+
76+
e.g.
77+
```yaml
78+
name: REQUESTS
79+
value: kafka-consume://kafka-all-broker.kafka:29092/example-topic?consumerGroup=allspark-consumer-group kafka-produce://kafka-all-broker.kafka:29092/example-topic?message=example-message#1
80+
```
81+
3382
### Example deployment
3483

3584
```yaml
@@ -127,3 +176,4 @@ spec:
127176
selector:
128177
app: analytics
129178
```
179+

cmd/allspark/configuration.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"strings"
2222

2323
"emperror.dev/errors"
24+
"github.com/banzaicloud/allspark/internal/kafka"
2425
"github.com/spf13/pflag"
2526
"github.com/spf13/viper"
2627

@@ -57,6 +58,9 @@ type Config struct {
5758

5859
// TCP server configuration
5960
TCPServer tcpserver.Config `mapstructure:"tcpServer"`
61+
62+
// Kafka server consumer configurations
63+
KafkaServer kafka.Consumer `mapstructure:"kafkaServer"`
6064
}
6165

6266
// Validate validates the configuration
@@ -91,6 +95,12 @@ func (c Config) Validate() (Config, error) {
9195
}
9296
c.TCPServer = tcpServerConfig
9397

98+
kafkaServerConfig, err := c.KafkaServer.Validate()
99+
if err != nil {
100+
return c, errors.WrapIf(err, "could not validate kafka server config")
101+
}
102+
c.KafkaServer = *kafkaServerConfig
103+
94104
return c, nil
95105
}
96106

cmd/allspark/main.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121

2222
"emperror.dev/emperror"
2323
"emperror.dev/errors"
24+
"github.com/banzaicloud/allspark/internal/kafka"
25+
"github.com/banzaicloud/allspark/internal/kafka/server"
2426
"github.com/spf13/pflag"
2527
"github.com/spf13/viper"
2628
yaml "gopkg.in/yaml.v2"
@@ -113,6 +115,7 @@ func main() {
113115
}
114116
wl = workload.NewPIWorkload(uint(count), logger)
115117
}
118+
116119
var wg sync.WaitGroup
117120

118121
// HTTP server
@@ -181,5 +184,28 @@ func main() {
181184
srv.Run()
182185
}()
183186

187+
// Kafka server
188+
wg.Add(1)
189+
go func() {
190+
defer wg.Done()
191+
consumer := kafka.NewConsumer(configuration.KafkaServer.BootstrapServer, configuration.KafkaServer.Topic, configuration.KafkaServer.ConsumerGroup, logger)
192+
srv := server.New(consumer, logger, errorHandler)
193+
if wl != nil {
194+
srv.SetWorkload(wl)
195+
}
196+
197+
kafkaRequests, err := request.CreateRequestsFromStringSlice(viper.GetStringSlice("kafkaRequests"), logger.WithField("server", "kafka"))
198+
if err != nil {
199+
panic(err)
200+
}
201+
if len(kafkaRequests) == 0 {
202+
kafkaRequests = requests
203+
}
204+
205+
srv.SetRequests(kafkaRequests)
206+
srv.SetSQLClient(sqlClient)
207+
srv.Run()
208+
}()
209+
184210
wg.Wait()
185211
}

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
github.com/google/uuid v1.3.0
1212
github.com/jackc/pgx/v4 v4.15.0
1313
github.com/pkg/errors v0.9.1
14+
github.com/segmentio/kafka-go v0.4.31
1415
github.com/sirupsen/logrus v1.8.1
1516
github.com/spf13/pflag v1.0.5
1617
github.com/spf13/viper v1.10.1
@@ -35,13 +36,16 @@ require (
3536
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
3637
github.com/jackc/pgtype v1.10.0 // indirect
3738
github.com/json-iterator/go v1.1.12 // indirect
39+
github.com/jstemmer/go-junit-report v1.0.0 // indirect
40+
github.com/klauspost/compress v1.14.2 // indirect
3841
github.com/leodido/go-urn v1.2.1 // indirect
3942
github.com/magiconair/properties v1.8.6 // indirect
4043
github.com/mattn/go-isatty v0.0.14 // indirect
4144
github.com/mitchellh/mapstructure v1.4.3 // indirect
4245
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
4346
github.com/modern-go/reflect2 v1.0.2 // indirect
4447
github.com/pelletier/go-toml v1.9.4 // indirect
48+
github.com/pierrec/lz4/v4 v4.1.14 // indirect
4549
github.com/pingcap/errors v0.11.5-0.20201126102027-b0a155152ca3 // indirect
4650
github.com/satori/go.uuid v1.2.0 // indirect
4751
github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726 // indirect

0 commit comments

Comments
 (0)