-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
373 lines (337 loc) · 9.66 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package main
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
"unicode/utf8"
"github.com/evankanderson/knative-async-poc/protocol"
"github.com/go-redis/redis/v8"
"github.com/bradleypeabody/gouuidv6"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/empty"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"google.golang.org/grpc"
"google.golang.org/grpc/peer"
)
func main() {
opts := &redis.UniversalOptions{
MasterName: os.Getenv("REDIS_MASTER_NAME"),
Addrs: []string{os.Getenv("BROKER")},
}
redis := redis.NewUniversalClient(opts)
rpcServer := grpc.NewServer()
tm := TaskManagerServer{srv: rpcServer, redis: redis, FinishedExpiration: 4 * time.Hour}
protocol.RegisterTaskManagerServer(rpcServer, &tm)
api := &ExternalAPI{redis: redis}
http.Handle("/v1/", api)
http.HandleFunc("/queueLength", func(w http.ResponseWriter, r *http.Request) {
works, err := redis.Keys(r.Context(), "pending_*").Result()
if err != nil {
w.WriteHeader(500)
fmt.Fprint(w, "Unable to fetch work: ", err)
return
}
fmt.Fprint(w, len(works))
})
h2s := &http2.Server{}
log.Fatal(http.ListenAndServe(":80", h2c.NewHandler(&tm, h2s)))
}
// ExternalAPI provides a meechanism
type ExternalAPI struct {
redis redis.Cmdable
}
// ServeHTTP implements http.Handler
func (api *ExternalAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("Got request for %q", r.URL)
ctx := r.Context()
switch r.Method {
case http.MethodGet:
id := r.URL.Path[len("/v1/"):]
log.Printf("Fetching %q", id)
val, err := api.redis.Get(ctx, "pending_"+id).Result()
if err == redis.Nil {
// See if it has been completed.
val, err = api.redis.Get(ctx, "done_"+id).Result()
}
if err != nil {
w.WriteHeader(404)
fmt.Fprintf(w, "No task %q found", id)
return
}
work := Work{}
if err := json.Unmarshal([]byte(val), &work); err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "Failed to unmarshal %q: %v", id, err)
return
}
retval := struct {
ID string `json:"id"`
Status string `json:"status"`
Result int `json:"result,omitempty"`
Headers map[string][]string `json:"headers,omitempty"`
Payload string `json:"payload,omitempty"`
}{
ID: work.ID,
Status: work.Status(),
Result: 0, // TODO: plumb this through
Headers: work.ResponseHeaders,
}
if utf8.Valid(work.ResponseBody) {
retval.Payload = string(work.ResponseBody)
} else {
retval.Payload = base64.StdEncoding.EncodeToString(work.ResponseBody)
}
out, err := json.Marshal(retval)
if err != nil {
w.WriteHeader(500)
fmt.Fprint(w, "Failed to marshal response: ", err)
return
}
w.Write(out)
case http.MethodPost:
data, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(500)
fmt.Fprint(w, "Unable to read body:", err)
return
}
now := time.Now()
id := gouuidv6.NewFromTime(now)
work := Work{
ID: id.String(),
Queue: "queuename", // TODO: pick up from URL
Headers: r.Header,
Body: data,
EnqueueTime: time.Now(),
}
workJSON, err := json.Marshal(work)
if err != nil {
w.WriteHeader(500)
fmt.Fprint(w, "Unable to marshal work", err)
return
}
ok, err := api.redis.SetNX(ctx, work.Key(), workJSON, 0).Result()
if err != nil || !ok {
log.Printf("Failed to publish %q (%t): %v", work.ID, ok, err)
w.WriteHeader(500)
fmt.Fprint(w, "Failed to publish task", err)
return
}
fmt.Fprintf(w, `{"id": "%s"}`, work.ID)
fmt.Fprint(w, "\n")
log.Printf("Created task %q", work.ID)
default:
w.WriteHeader(400)
fmt.Fprintf(w, "Unimplemented!")
}
}
// Work is the storage representation (converts to JSON) of a single unit of work.
type Work struct {
ID string
Queue string
Headers http.Header
Body []byte
// Timing info
EnqueueTime time.Time
LeaseUntil time.Time
CompletedAt time.Time
Retries []time.Time
// Response info
ResponseHeaders http.Header
ResponseBody []byte
FailureStatus string
}
// Status provides a human-readable status for Work.
func (w Work) Status() string {
if w.CompletedAt.IsZero() {
return "pending"
}
return "completed"
}
// Key returns the redis row key for the Work given its current state.
func (w Work) Key() string {
if w.CompletedAt.IsZero() {
return "pending_" + w.ID
}
return "done_" + w.ID
}
// TaskManagerServer implements protocol.TaskManagerServer
type TaskManagerServer struct {
srv *grpc.Server
redis redis.UniversalClient
FinishedExpiration time.Duration
}
// ServeHTTP implements http.Handler to mux between grpc and standard HTTP
func (tms *TaskManagerServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.ProtoMajor == 2 && strings.HasPrefix(r.Header.Get("Content-Type"), "application/grpc") {
tms.srv.ServeHTTP(w, r)
} else {
http.DefaultServeMux.ServeHTTP(w, r)
}
}
// Get implements protocol.TaskManagerServer
func (tms *TaskManagerServer) Get(ctx context.Context, req *protocol.GetRequest) (*protocol.Work, error) {
remoteAddr := "unknown"
if clientPeer, ok := peer.FromContext(ctx); ok {
remoteAddr = clientPeer.Addr.String()
}
works, err := tms.redis.Keys(ctx, "pending_*").Result()
if err != nil {
return nil, err
}
for _, key := range works {
workJSON, err := tms.redis.Get(ctx, key).Result()
if err != nil {
log.Printf("Error fetching %q, continuing: %v", key, err)
continue
}
work := Work{}
if err := json.Unmarshal([]byte(workJSON), &work); err != nil {
log.Printf("Error unmarshalling %q: %v", key, err)
}
if work.LeaseUntil.Before(time.Now()) {
leaseEnd, err := tms.update(ctx, work, func(work *Work) {
work.LeaseUntil = time.Now().Add(10 * time.Minute)
})
if err != nil {
log.Printf("Failed to claim %q: %v", work.ID, err)
continue
}
enqueueTime, err := ptypes.TimestampProto(work.EnqueueTime)
if err != nil {
log.Printf("Unable to encode time for %q (%s): %v", work.ID, work.EnqueueTime, err)
continue
}
leaseTime, err := ptypes.TimestampProto(leaseEnd)
if err != nil {
log.Printf("Unable to encode time for %q (%s): %v", work.ID, leaseEnd, err)
continue
}
retval := &protocol.Work{
Id: work.ID,
Payload: work.Body,
EnqueuedAt: enqueueTime,
LeasedUntil: leaseTime,
Headers: make(map[string]*protocol.HeaderValue, len(work.Headers)),
}
for k, v := range work.Headers {
retval.Headers[k] = &protocol.HeaderValue{Value: v}
}
log.Printf("Sending task %q to %q", work.ID, remoteAddr)
return retval, nil
}
}
// Return an empty work to signal no more tasks.
return &protocol.Work{}, nil
}
// Renew implements protocol.TaskManagerServer
func (tms *TaskManagerServer) Renew(ctx context.Context, req *protocol.RenewRequest) (*empty.Empty, error) {
work, err := tms.fetchKey(ctx, req.Id)
if err != nil {
return nil, err
}
_, err = tms.update(ctx, *work, func(work *Work) {
work.LeaseUntil = time.Now().Add(10 * time.Minute)
})
if err != nil {
return nil, err
}
return &empty.Empty{}, nil
}
// Finish implements protocol.TaskManagerServer
func (tms *TaskManagerServer) Finish(ctx context.Context, req *protocol.FinishRequest) (*empty.Empty, error) {
work, err := tms.fetchKey(ctx, req.Id)
oldKey := work.Key()
if err != nil {
return nil, err
}
work.CompletedAt = time.Now()
work.ResponseBody = req.Result
work.ResponseHeaders = make(http.Header, len(req.ResultHeaders))
for k, v := range req.ResultHeaders {
work.ResponseHeaders[k] = v.Value
}
if err := tms.finish(ctx, work, oldKey); err != nil {
return nil, err
}
log.Printf("Task %q finished!", req.Id)
return &empty.Empty{}, nil
}
// Nack implements protocol.TaskManagerServer
func (tms *TaskManagerServer) Nack(ctx context.Context, req *protocol.NackRequest) (*empty.Empty, error) {
work, err := tms.fetchKey(ctx, req.Id)
oldKey := work.Key()
if err != nil {
return nil, err
}
if len(work.Retries) > 2 {
work.CompletedAt = time.Now()
work.FailureStatus = req.Error
if err := tms.finish(ctx, work, oldKey); err != nil {
return nil, err
}
log.Printf("Failed task %q", req.Id)
return &empty.Empty{}, nil
}
// Enqueue another retry
_, err = tms.update(ctx, *work, func(work *Work) {
work.Retries = append(work.Retries, time.Now())
work.LeaseUntil = time.Time{}
})
if err != nil {
return nil, err
}
return &empty.Empty{}, nil
}
func (tms *TaskManagerServer) update(ctx context.Context, work Work, update func(work *Work)) (time.Time, error) {
oldJSON, err := json.Marshal(work)
if err != nil {
return time.Time{}, err
}
update(&work)
workJSON, err := json.Marshal(work)
if err != nil {
return time.Time{}, err
}
txn := func(tx *redis.Tx) error {
prev, err := tx.Get(ctx, work.Key()).Result()
if err != nil {
return err
}
if !bytes.Equal([]byte(prev), oldJSON) {
return fmt.Errorf("Newer data since %s:\n%s\n%s", work.LeaseUntil, oldJSON, prev)
}
return tx.Set(ctx, work.Key(), workJSON, 0).Err()
}
return work.LeaseUntil, tms.redis.Watch(ctx, txn, work.Key())
}
func (tms *TaskManagerServer) finish(ctx context.Context, work *Work, oldKey string) error {
workJSON, err := json.Marshal(work)
if err != nil {
return err
}
if err := tms.redis.Set(ctx, work.Key(), workJSON, tms.FinishedExpiration).Err(); err != nil {
return err
}
return tms.redis.Del(ctx, oldKey).Err()
}
func (tms *TaskManagerServer) fetchKey(ctx context.Context, id string) (*Work, error) {
workJSON, err := tms.redis.Get(ctx, "pending_"+id).Result()
if err != nil {
return nil, err
}
work := Work{}
if err := json.Unmarshal([]byte(workJSON), &work); err != nil {
return nil, err
}
return &work, nil
}