2015-10-17 01:10:16 +00:00
|
|
|
package kafka
|
2015-10-17 00:42:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
2015-10-18 00:45:25 +00:00
|
|
|
"sync"
|
2015-10-17 00:42:27 +00:00
|
|
|
|
|
|
|
"github.com/Shopify/sarama"
|
|
|
|
"github.com/localhots/satan"
|
|
|
|
)
|
|
|
|
|
2015-10-17 01:10:16 +00:00
|
|
|
// ConsumerState contains data that is required to create a Kafka consumer.
|
|
|
|
type ConsumerState struct {
|
2015-10-17 00:42:27 +00:00
|
|
|
Partition int32 `json:"partition"`
|
|
|
|
Offset int64 `json:"offset"`
|
|
|
|
}
|
|
|
|
|
2015-10-17 01:10:16 +00:00
|
|
|
// Stream is an implementation of satan.Stremer for Kafka messaging queue.
|
|
|
|
type Stream struct {
|
2015-10-17 00:42:27 +00:00
|
|
|
messages chan []byte
|
|
|
|
shutdown chan struct{}
|
2015-10-18 00:45:25 +00:00
|
|
|
wg sync.WaitGroup
|
2015-10-17 00:42:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
consumerStateFile = "tmp/consumers.json"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
kafkaClient sarama.Client
|
|
|
|
kafkaConsumer sarama.Consumer
|
2015-10-17 01:10:16 +00:00
|
|
|
consumers = map[string]map[string]ConsumerState{}
|
2015-10-17 00:42:27 +00:00
|
|
|
)
|
|
|
|
|
2015-10-17 01:10:16 +00:00
|
|
|
// Initialize sets up the kafka package.
|
|
|
|
func Initialize(brokers []string) {
|
2015-10-18 00:46:52 +00:00
|
|
|
log.Println("Initializing Kafka")
|
|
|
|
defer log.Println("Kafka is initialized")
|
|
|
|
|
2015-10-17 00:42:27 +00:00
|
|
|
conf := sarama.NewConfig()
|
|
|
|
conf.ClientID = "Satan Example"
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if kafkaClient, err = sarama.NewClient(brokers, conf); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if kafkaConsumer, err = sarama.NewConsumerFromClient(kafkaClient); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-10-17 01:10:16 +00:00
|
|
|
|
|
|
|
loadConsumerConfig()
|
2015-10-17 00:42:27 +00:00
|
|
|
}
|
|
|
|
|
2015-10-17 01:10:16 +00:00
|
|
|
// Shutdown shuts down the kafka package.
|
|
|
|
func Shutdown() {
|
2015-10-18 00:46:52 +00:00
|
|
|
log.Println("Shutting down Kafka")
|
|
|
|
defer log.Println("Kafka was shut down")
|
|
|
|
|
2015-10-17 00:42:27 +00:00
|
|
|
if err := kafkaConsumer.Close(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := kafkaClient.Close(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-17 01:11:29 +00:00
|
|
|
// Subscribe creates a satan.Streamer implementation for Kafka messaging queue.
|
|
|
|
func Subscribe(consumer, topic string) satan.Streamer {
|
2015-10-17 00:42:27 +00:00
|
|
|
c, ok := consumers[consumer]
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Errorf("Consumer %q has no config", consumer))
|
|
|
|
}
|
|
|
|
t, ok := c[topic]
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Errorf("Consumer %q has no config for topic %q", consumer, topic))
|
|
|
|
}
|
|
|
|
|
|
|
|
pc, err := kafkaConsumer.ConsumePartition(topic, t.Partition, t.Offset)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2015-10-17 01:10:16 +00:00
|
|
|
stream := &Stream{
|
2015-10-17 00:42:27 +00:00
|
|
|
messages: make(chan []byte),
|
|
|
|
shutdown: make(chan struct{}),
|
|
|
|
}
|
|
|
|
go func() {
|
2015-10-18 00:45:25 +00:00
|
|
|
stream.wg.Add(1)
|
|
|
|
defer stream.wg.Done()
|
|
|
|
defer pc.Close()
|
2015-10-17 00:42:27 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-pc.Messages():
|
2015-10-18 00:45:25 +00:00
|
|
|
select {
|
|
|
|
case stream.messages <- msg.Value:
|
|
|
|
t.Offset = msg.Offset
|
|
|
|
case <-stream.shutdown:
|
|
|
|
return
|
|
|
|
}
|
2015-10-17 00:42:27 +00:00
|
|
|
case err := <-pc.Errors():
|
|
|
|
log.Println("Kafka error:", err.Error())
|
|
|
|
case <-stream.shutdown:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return stream
|
|
|
|
}
|
|
|
|
|
2015-10-17 01:10:16 +00:00
|
|
|
// Messages returns a channel that stream messages.
|
|
|
|
func (s *Stream) Messages() <-chan []byte {
|
|
|
|
return s.messages
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close stops Kafka partition consumer.
|
|
|
|
func (s *Stream) Close() {
|
|
|
|
close(s.shutdown)
|
2015-10-18 00:45:25 +00:00
|
|
|
s.wg.Wait()
|
2015-10-17 01:10:16 +00:00
|
|
|
}
|
|
|
|
|
2015-10-17 00:42:27 +00:00
|
|
|
func loadConsumerConfig() {
|
|
|
|
if b, err := ioutil.ReadFile(consumerStateFile); err != nil {
|
|
|
|
fmt.Println(`Kafka consumers state file was not found at ` + consumerStateFile + `
|
|
|
|
Please create one in order to proceed with this example.
|
|
|
|
Config file contents should look like this:
|
|
|
|
{
|
|
|
|
"ConsumerName": {
|
|
|
|
"TopicName": {
|
|
|
|
"partition": 0,
|
|
|
|
"offset": 12345
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
os.Exit(1)
|
|
|
|
} else {
|
|
|
|
if err = json.Unmarshal(b, &consumers); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|