-
Notifications
You must be signed in to change notification settings - Fork 13
/
kv.go
261 lines (205 loc) · 8.31 KB
/
kv.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
package httpapi
import (
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/redis/go-redis/v9"
"github.com/julienschmidt/httprouter"
"google.golang.org/protobuf/encoding/protojson"
"github.com/streamdal/streamdal/libs/protos/build/go/protos"
"github.com/streamdal/streamdal/apps/server/validate"
)
func (a *HTTPAPI) getUsageKVHandler(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json; charset=UTF-8")
usage, err := a.Options.KVService.GetUsage(r.Context())
if err != nil {
Write(rw, http.StatusInternalServerError, "unable to fetch kv usage: %v", err)
return
}
WriteJSON(rw, usage, http.StatusOK)
}
// Returns []*protos.KVObject as JSON
func (a *HTTPAPI) getAllKVHandler(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json; charset=UTF-8")
kvs, err := a.Options.KVService.GetAll(r.Context())
if err != nil {
Write(rw, http.StatusInternalServerError, "unable to fetch all kv objects: %v", err)
return
}
WriteJSON(rw, kvs, http.StatusOK)
}
// Returns *protos.KVObject as JSON
func (a *HTTPAPI) getKVHandler(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json; charset=UTF-8")
key := strings.TrimPrefix(r.URL.Path, "/api/v1/kv/")
// instead of doing this: params := httprouter.ParamsFromContext(r.Context())
// because tests might not have a router started
if key == "" {
// Get the key from the path
Write(rw, http.StatusInternalServerError, "bug? got a request for fetching a KV but key is empty")
return
}
object, err := a.Options.KVService.Get(r.Context(), key)
if err != nil {
statusCode := http.StatusInternalServerError
statusMsg := fmt.Sprintf("unable to fetch kv object '%s': %s", key, err)
if err == redis.Nil {
statusCode = http.StatusNotFound
statusMsg = fmt.Sprintf("kv object '%s' not found", key)
}
Write(rw, statusCode, statusMsg)
return
}
WriteJSON(rw, object, http.StatusOK)
}
type CreateKVRequest struct {
Overwrite bool `json:"overwrite"`
KVS []*protos.KVObject `json:"kvs"`
}
// Input []*protos.KVObject as JSON; possible query param "overwrite" bool will
// cause createKVHandler to not error if key already exists.
// Returns created *protos.KVObject as JSON
func (a *HTTPAPI) createKVHandler(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json; charset=UTF-8")
body, err := io.ReadAll(r.Body)
if err != nil {
Write(rw, http.StatusBadRequest, "unable to read request body: %v", err)
return
}
defer r.Body.Close()
// Unmarshal input into pb's
createHTTPRequest := &protos.KVCreateHTTPRequest{}
if err := protojson.Unmarshal(body, createHTTPRequest); err != nil {
Write(rw, http.StatusBadRequest, "unable to unmarshal request body: %v", err)
return
}
// Validate the input
if err := validate.KVCreateHTTPRequest(createHTTPRequest); err != nil {
Write(rw, http.StatusBadRequest, "invalid request body: %v", err)
return
}
// Attempt to create in KV service
//
// TODO: This should be updated to pass this data to the KV service via a chan;
// this should be good enough for now though.
if err := a.Options.KVService.Create(r.Context(), createHTTPRequest.Kvs, createHTTPRequest.Overwrite); err != nil {
Write(rw, http.StatusInternalServerError, "unable to complete create KV request: %v", err)
return
}
// Broadcast KV changes to other server nodes (reminder: we do this
// so that the nodes can inform their connected SDKs of the change and the
// SDKs can update their local KV state)
// TODO: create broadcast handlers for emitting commands on KV updates
if err := a.Options.BusService.BroadcastKVCreate(r.Context(), createHTTPRequest.Kvs, createHTTPRequest.Overwrite); err != nil {
Write(rw, http.StatusInternalServerError, "unable to broadcast kv command: %v", err)
return
}
WriteJSON(rw, createHTTPRequest.Kvs, http.StatusOK)
}
// Update one or more KV objects; returns (updated) []*protos.KVObject as JSON
func (a *HTTPAPI) updateKVHandler(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json; charset=UTF-8")
body, err := io.ReadAll(r.Body)
if err != nil {
Write(rw, http.StatusBadRequest, "unable to read request body: %v", err)
return
}
defer r.Body.Close()
// Unmarshal input into pb's
updateHTTPRequest := &protos.KVUpdateHTTPRequest{}
if err := protojson.Unmarshal(body, updateHTTPRequest); err != nil {
Write(rw, http.StatusBadRequest, "unable to unmarshal request body: %v", err)
return
}
// Validate the input
if err := validate.KVUpdateHTTPRequest(updateHTTPRequest); err != nil {
Write(rw, http.StatusBadRequest, "invalid request body: %v", err)
return
}
updateList := make([]*protos.KVObject, 0)
for _, kv := range updateHTTPRequest.Kvs {
// Exist check + we need existing to get created_at TS
existing, err := a.Options.KVService.Get(r.Context(), kv.Key)
if err != nil {
Write(rw, http.StatusInternalServerError, "unable to fetch existing kv object '%s': %v", kv.Key, err)
return
}
// Overwrite requested object's timestamps
kv.CreatedAtUnixTsNanoUtc = existing.CreatedAtUnixTsNanoUtc
kv.UpdatedAtUnixTsNanoUtc = time.Now().UTC().UnixNano()
// Attempt to update in KV service
updated, err := a.Options.KVService.Update(r.Context(), kv)
if err != nil {
Write(rw, http.StatusInternalServerError, "unable to complete update request: %v", err)
return
}
// Add to update list
updateList = append(updateList, updated)
}
// Broadcast KV changes to other server nodes (reminder: we do this
// so that the nodes can inform their connected SDKs of the change and the
// SDKs can update their local KV state)
if err := a.Options.BusService.BroadcastKVUpdate(r.Context(), updateList); err != nil {
Write(rw, http.StatusInternalServerError, "unable to broadcast update kv command: %v", err)
return
}
WriteJSON(rw, updateList, http.StatusOK)
}
// Returns *ResponseJSON; status code 200 - ok; 400 - bad input; 404 - not found; 500 - internal server error
func (a *HTTPAPI) deleteKVHandler(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json; charset=UTF-8")
// Get the key
params := httprouter.ParamsFromContext(r.Context())
key := params.ByName("key")
if key == "" {
Write(rw, http.StatusInternalServerError, "bug? matched delete route for key but key is empty")
return
}
// Does this kv exist?
if _, err := a.Options.KVService.Get(r.Context(), key); err != nil {
if strings.Contains(err.Error(), redis.Nil.Error()) {
Write(rw, http.StatusNotFound, "kv with key '%s' not found", key)
return
}
Write(rw, http.StatusInternalServerError, "unable to fetch existing kv object: %s", err)
return
}
// Key exists; attempt to delete in KV
if err := a.Options.KVService.Delete(r.Context(), key); err != nil {
Write(rw, http.StatusInternalServerError, "unable to complete delete request: %s", err)
return
}
// Broadcast KV changes to other server nodes (reminder: we do this
// so that the nodes can inform their connected SDKs of the change and the
// SDKs can update their local KV state)
if err := a.Options.BusService.BroadcastKVDelete(r.Context(), key); err != nil {
Write(rw, http.StatusInternalServerError, "unable to broadcast delete kv command: %s", err)
return
}
Write(rw, http.StatusOK, "kv deleted")
}
// Returns *ResponseJSON; status code 200 - ok; 500 - internal server error
// "Message" indicates how many KV objects were deleted.
//
// THIS IS AN EXTREMELY DANGEROUS ENDPOINT.
func (a *HTTPAPI) deleteAllKVHandler(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json; charset=UTF-8")
yes := r.URL.Query().Get("yes")
if yes != "please" {
Write(rw, http.StatusBadRequest, "please pass ?yes=please to confirm you want to delete all kv objects")
return
}
// Delete all kv objects
if err := a.Options.KVService.DeleteAll(r.Context()); err != nil {
Write(rw, http.StatusInternalServerError, "unable to complete delete all request: %s", err)
return
}
// Broadcast the delete all change
if err := a.Options.BusService.BroadcastKVDeleteAll(r.Context()); err != nil {
Write(rw, http.StatusInternalServerError, "unable to broadcast delete all kv command: %s", err)
return
}
Write(rw, http.StatusOK, "deletion request issued; please wait a few seconds for the change to propagate")
}