-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer.go
164 lines (138 loc) · 3.67 KB
/
consumer.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
package kcl
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"os"
)
type Consumer struct {
InputStream io.Reader
OutputStream io.Writer
Processor RecordProcessor
Checkpointer *Checkpointer
}
func NewConsumer(processor RecordProcessor) *Consumer {
consumer := &Consumer{
InputStream: os.Stdin,
OutputStream: os.Stdout,
Processor: processor,
Checkpointer: &Checkpointer{
OutputStream: os.Stdout,
},
}
consumer.Checkpointer.Consumer = consumer
return consumer
}
func (consumer *Consumer) Run() {
for {
// read next daemon request
msg := consumer.readAction()
if msg.Len() == 0 {
break
}
consumer.handleAction(msg)
}
}
func (consumer *Consumer) readAction() (buffer bytes.Buffer) {
bio := bufio.NewReader(consumer.InputStream)
for {
linePart, hasMoreInLine, err := bio.ReadLine()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read line from stdin "+err.Error())
os.Exit(1)
}
buffer.Write(linePart)
if hasMoreInLine == false {
break
}
}
return buffer
}
// handleAction reads a request from the KCL MultiLangDaemon and performs the appropriate action.
func (consumer *Consumer) handleAction(msg bytes.Buffer) {
var action Action
err := json.Unmarshal(msg.Bytes(), &action)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not understand line read from input: %s\n", msg.String())
os.Exit(1)
}
switch action.ActionType {
case "initialize":
err = consumer.handleInitializeAction(&msg)
case "processRecords":
err = consumer.handleProcessRecordsAction(&msg)
case "shutdown":
err = consumer.handleShutdownAction(&msg)
case "shutdownRequested":
err = consumer.handleShutdownRequestedAction()
case "leaseLost":
err = consumer.handleLeaseLost(&msg)
case "shardEnded":
err = consumer.handleShardEnded(&msg)
default:
err = fmt.Errorf("unsupported KCL action: %s", action.ActionType)
}
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
// respond with ack
status := StatusAction{
Action: Action{"status"},
ResponseFor: action.ActionType,
}
var serializedStatus []byte
serializedStatus, err = json.Marshal(status)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
fmt.Println(string(serializedStatus))
}
func (consumer *Consumer) handleInitializeAction(buffer *bytes.Buffer) error {
var action InitializeAction
err := json.Unmarshal(buffer.Bytes(), &action)
if err != nil {
return err
}
consumer.Processor.Initialize(action.ShardID, action.SequenceNumber, action.SubSequenceNumber)
return nil
}
func (consumer *Consumer) handleProcessRecordsAction(buffer *bytes.Buffer) error {
var action ProcessRecordsAction
err := json.Unmarshal(buffer.Bytes(), &action)
if err != nil {
return err
}
consumer.Processor.ProcessRecords(&action.Records, action.MillisBehindLatest, consumer.Checkpointer)
return nil
}
func (consumer *Consumer) handleShutdownAction(buffer *bytes.Buffer) error {
var action ShutdownAction
err := json.Unmarshal(buffer.Bytes(), &action)
if err != nil {
return err
}
return consumer.Processor.ShardEnded(consumer.Checkpointer)
}
func (consumer *Consumer) handleLeaseLost(buffer *bytes.Buffer) error {
var action LeastLostAction
err := json.Unmarshal(buffer.Bytes(), &action)
if err != nil {
return err
}
return consumer.Processor.LeaseLost()
}
func (consumer *Consumer) handleShardEnded(buffer *bytes.Buffer) error {
var action ShardEndedAction
err := json.Unmarshal(buffer.Bytes(), &action)
if err != nil {
return err
}
return consumer.Processor.ShardEnded(consumer.Checkpointer)
}
func (consumer *Consumer) handleShutdownRequestedAction() error {
return consumer.Processor.ShutdownRequested(consumer.Checkpointer)
}