-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExampleKvServer.hs
427 lines (365 loc) · 15.6 KB
/
ExampleKvServer.hs
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC "-Wno-orphans" #-}
module Main where
import Control.Monad.IO.Class (liftIO)
import Data.Monoid (Sum(..))
import Text.Printf (printf)
import Text.Read (readEither)
import qualified Control.Exception as Exc
import qualified Control.Concurrent.Async as Async
import qualified Control.Concurrent.STM as STM
import qualified Control.Monad as Monad
import qualified Data.Aeson as Aeson
import qualified Data.Map as Map
import qualified Network.HTTP.Client as HTTP
import qualified Network.Wai.Handler.Warp as Warp
import qualified Network.Wai.Middleware.Gzip as Gzip
import qualified System.Environment as Env
import qualified System.IO as IO
import qualified Network.Wai.Metrics as Metrics
import qualified System.Remote.Monitoring as EKG
import qualified System.Metrics as EKG
import qualified System.Metrics.Counter as Counter
import qualified System.Metrics.Distribution as Distribution
import Servant
import Servant.API.Generic ((:-), Generic, ToServantApi)
import qualified Servant.Client as Servant
import qualified Servant.Client.Generic as Servant
import qualified CBCAST
import qualified CBCAST.Core
-- $setup
-- >>> :{
-- testCoalesce pred monoids = do
-- q <- STM.newTQueueIO
-- print =<< (STM.atomically $ do
-- mapM_ (STM.writeTQueue q) monoids
-- coalesceWhile pred q)
-- print =<< (STM.atomically $ STM.flushTQueue q)
-- :}
-- * KV Application
type Key = String
type Val = Aeson.Value
data KvCommand
= KvPut Key Val
| KvDel Key
deriving (Generic, Show, Eq)
-- | State of the KV Store
type KvState = Map.Map Key Val
-- | Interpret a command to update KV Store state
kvApply :: KvCommand -> KvState -> KvState
kvApply (KvPut key val) = Map.insert key val
kvApply (KvDel key) = Map.delete key
kvQuery :: Key -> KvState -> Maybe Val
kvQuery key = Map.lookup key
-- | Client routes exposed by the KV Store
data KvRoutes mode = KvRoutes
{ kvPut :: mode :- "kv" :> Capture "key" Key :> ReqBody '[JSON] Val :> PutNoContent
, kvDel :: mode :- "kv" :> Capture "key" Key :> DeleteNoContent
, kvGet :: mode :- "kv" :> Capture "key" Key :> Get '[JSON] Val
}
deriving (Generic)
-- | Apply a message to application state.
applyMessage :: STM.TVar KvState -> Broadcast -> STM.STM ()
applyMessage kvState m =
STM.modifyTVar' kvState . kvApply $ CBCAST.mRaw m
-- * CBCAST Node & Peers
-- | State of the CBCAST node
type NodeState = CBCAST.Process KvCommand
-- | Type of messages exchanged by CBCAST nodes
type Broadcast = CBCAST.Message KvCommand
-- | Routes exposed by the CBCAST node
data PeerRoutes mode = PeerRoutes
-- We coalesce CBCASTs by sending a list of them in each request.
{ cbcast :: mode :- "cbcast" :> ReqBody '[JSON] [Broadcast] :> PostNoContent
}
deriving (Generic)
peerRoutes :: PeerRoutes (Servant.AsClientT Servant.ClientM)
peerRoutes = Servant.genericClient
-- | Type of coalesced broadcasts stored in outbound buffers for other CBCAST
-- nodes. The count is the number of failed requests to the recipient.
type Coalesced = (Sum Int, [Broadcast])
-- | Addresses of other CBCAST nodes
type Peers = [Servant.BaseUrl]
-- | Outbound buffers for other CBCAST nodes
type PeerQueues = [STM.TQueue Coalesced]
-- | Put a message on all peer queues.
feedPeerQueues :: PeerQueues -> Broadcast -> STM.STM ()
feedPeerQueues peerQueues m =
Monad.forM_ peerQueues $ \q ->
STM.writeTQueue q (0, [m])
-- * Serialization Instances
instance Aeson.ToJSON KvCommand
instance Aeson.FromJSON KvCommand
-- | Serialization round-tripper!
--
-- >>> let Right p = CBCAST.newProcess 3 0
-- >>> let (m, p') = CBCAST.broadcast (KvPut "some-key" Aeson.Null) p
-- >>> let m' = Aeson.decode $ Aeson.encode m :: Maybe Broadcast
-- >>> Just m == m'
-- True
--
-- >>> putStrLn . tail . init . filter (/= '\\') . show $ Aeson.encode m'
-- {"mRaw":{"contents":["some-key",null],"tag":"KvPut"},"mSender":0,"mVC":[1,0,0]}
--
instance (Generic r, Aeson.ToJSON r) => Aeson.ToJSON (CBCAST.Message r)
instance (Generic r, Aeson.FromJSON r) => Aeson.FromJSON (CBCAST.Message r)
-- * Backend
-- ** Request handlers
-- | Monitoring counters
data Stats = Stats
{ deliverCount :: Counter.Counter
, receiveCount :: Counter.Counter
, receivedIsDeliverable :: Distribution.Distribution
, broadcastCount :: Counter.Counter
, unicastAttemptSuccessful :: Distribution.Distribution
, unicastSuccessSize :: Distribution.Distribution
}
-- | Handle requests to send or receive messages.
handlers :: Stats -> PeerQueues -> STM.TVar NodeState -> STM.TVar KvState -> Application
handlers stats peerQueues nodeState kvState = serve
(Proxy :: Proxy (ToServantApi KvRoutes :<|> ToServantApi PeerRoutes))
(kvHandlers :<|> nodeHandlers)
where
-- https://github.com/haskell-servant/servant/issues/1394
kvHandlers :: Server (ToServantApi KvRoutes)
kvHandlers
= (\key val -> liftIO $ do
STM.atomically $ do
m <- STM.stateTVar nodeState . CBCAST.broadcast $ KvPut key val
applyMessage kvState m -- apply message locally
feedPeerQueues peerQueues m -- nonblocking send message to network
Counter.inc $ broadcastCount stats
return NoContent)
:<|> (\key -> liftIO $ do
STM.atomically $ do
m <- STM.stateTVar nodeState . CBCAST.broadcast $ KvDel key
applyMessage kvState m -- apply message locally
feedPeerQueues peerQueues m -- nonblocking send message to network
Counter.inc $ broadcastCount stats
return NoContent)
:<|> (\key -> do
kv <- liftIO $ STM.readTVarIO kvState
maybe (throwError err404) return $ kvQuery key kv)
nodeHandlers :: Server (ToServantApi PeerRoutes)
nodeHandlers
= (\messages -> liftIO $ do
mapM_ receiveOne messages
return NoContent)
receiveOne m = do
status <- STM.atomically $ do
-- observe deliverability; receive message into delay queue
deliverable <- STM.readTVar nodeState >>= \p ->
return $ CBCAST.Core.deliverable m (CBCAST.Core.pVC p)
receiveErr <- STM.stateTVar nodeState $ \p ->
either ((,p) . Just) (Nothing,) $ CBCAST.receive m p
return $ maybe (Right deliverable) Left receiveErr
case status of
Left e -> printf "receive error: %s\n" e
Right deliverable -> do
-- Count received messages and compute p(deliverable|received)
Counter.inc (receiveCount stats)
Distribution.add (receivedIsDeliverable stats) (if deliverable then 1 else 0)
-- ** Deliver messages
-- | Block until there is a deliverable message and apply it. Best used with
-- 'Monad.forever'.
readMailOnce :: STM.TVar NodeState -> STM.TVar KvState -> STM.STM ()
readMailOnce nodeState kvState = do
message <- STM.stateTVar nodeState CBCAST.deliver
maybe STM.retry (applyMessage kvState) message
-- ** Broadcast messages
-- | Drain broadcast messages and send them to peers, forever. Does not return.
sendMailThread :: Stats -> Peers -> PeerQueues -> IO ()
sendMailThread stats peers peerQueues =
Exc.assert (length peers == length peerQueues) $ do
printf "sendMailThread will deliver to peers: %s\n" (unwords $ Servant.showBaseUrl <$> peers)
connections <- HTTP.newManager HTTP.defaultManagerSettings
let clientEnvs = Servant.mkClientEnv connections <$> peers
-- Start tasks and wait forever.
-- Note: Graceful shutdown is not implemented.
(_, result)
<- Async.waitAnyCatchCancel
=<< mapM (Async.async . Monad.void . Monad.forever)
(sendToPeer stats <$> zip clientEnvs peerQueues)
printf "sendMailThread exited: %s\n" $ show result
-- | Send a message from a queue to a peer. On failure, backoff and put the
-- message back on the queue and note failure. Return.
sendToPeer :: Stats -> (Servant.ClientEnv, STM.TQueue Coalesced) -> IO ()
sendToPeer stats (env, queue) = do
(Sum failures, message) <- STM.atomically $ do
coalesced <- coalesceWhile (\(_, xs) -> length xs < 10) queue
STM.check $ mempty /= coalesced
return coalesced
result <- Servant.runClientM (cbcast peerRoutes message) env
case result of
Right NoContent -> do
Distribution.add (unicastAttemptSuccessful stats) 1
Distribution.add (unicastSuccessSize stats) (fromIntegral $ length message)
Left err -> do
Distribution.add (unicastAttemptSuccessful stats) 0
printf "sendToPeer error (%d, %s): %s \n" failures (Servant.showBaseUrl $ Servant.baseUrl env) (showClientError err)
backoff <- STM.registerDelay $ round (2^(min 5 failures) * 1e6 :: Double)
STM.atomically $ do
STM.check =<< STM.readTVar backoff
STM.unGetTQueue queue (Sum $ failures + 1, message)
-- | Coalesce monoids from a queue while a predicate is satisfied.
--
-- Stop when the predicate is no longer satisfied by the coalesced monoid.
--
-- >>> testCoalesce (< 5) $ fmap Sum [1,3,5,2,4]
-- Sum {getSum = 9}
-- [Sum {getSum = 2},Sum {getSum = 4}]
--
-- Always make a little progress.
--
-- >>> testCoalesce (< 5) $ fmap Sum [99,3,5,2,4]
-- Sum {getSum = 99}
-- [Sum {getSum = 3},Sum {getSum = 5},Sum {getSum = 2},Sum {getSum = 4}]
--
-- Coalesce in FIFO-order.
--
-- >>> testCoalesce (\x -> length x < 10) $ words "too many ducks and not enough pond"
-- "toomanyducks"
-- ["and","not","enough","pond"]
--
-- Stop when the queue is exausted.
--
-- >>> testCoalesce (const True) $ fmap Sum [1,3,5,2,4]
-- Sum {getSum = 15}
-- []
--
-- Return @mempty@ when the queue is empty.
--
-- >>> testCoalesce (const True) $ fmap Sum []
-- Sum {getSum = 0}
-- []
--
coalesceWhile :: Monoid a => (a -> Bool) -> STM.TQueue a -> STM.STM a
coalesceWhile more queue = f mempty
where
f x | more x = maybe (return x) (f . (x <>)) =<< STM.tryReadTQueue queue
| otherwise = return x
-- * Demo
-- | Try starting the server and hitting it with these commands:
--
-- $ curl 127.0.0.1:8080/kv/foo
-- $ curl -X DELETE 127.0.0.1:8080/kv/foo
-- $ curl -X PUT -H "Content-type: application/json" 127.0.0.1:8080/kv/foo -d '{"x":1,"y":20}'
--
main :: IO ()
main = Env.getArgs >>= \argv -> case argv of
[] -> do
prog <- Env.getProgName
putStrLn "USAGE:"
putStrLn $ "\t" ++ prog ++ " URL -- KV Client"
putStrLn "\t\tGenerate a sequence of random KV store requests and send them to the specified URL."
putStrLn ""
putStrLn $ "\t" ++ prog ++ " NUM URL [URL ...] -- KV Server & CBCAST Node"
putStrLn $ "\t\t" ++ unwords
[ "Start a KV server."
, "At least one URL must be provided."
, "The URLs are regarded as peers in the CBCAST cluster."
, "The NUM is an offset into the list of peers to indicate which corresponds to this host."
, "All peers must be provided with the same list of peers (order matters), and distinct NUMs."
]
[url] -> do
server <- Servant.parseBaseUrl url
printf "Starting KV client sticky to server: %s\n" $ Servant.showBaseUrl server
putStrLn "not implemented"
(num:urls) -> do
-- Note: Ensure buffering is set so that stdout goes to syslog.
IO.hSetBuffering IO.stdout IO.LineBuffering
-- !!! Set up monitoring
metricsStore <- mkMetricsStore
stats <- processMetrics metricsStore
metricsMiddleware <- waiMetricsMiddleware metricsStore
-- !!! Set up cluster
peers <- mapM Servant.parseBaseUrl urls
pid <- either fail return $ parsePID num peers
let port = Servant.baseUrlPort $ peers !! pid
printf "Starting KV server PID-%d on port %d with peer list: %s\n" pid port (unwords $ Servant.showBaseUrl <$> peers)
nodeState <- either fail STM.newTVarIO $ CBCAST.newProcess (length peers) pid
kvState <- STM.newTVarIO $ Map.empty
-- Note: One fewer peer queue because we don't send to ourselves.
peerQueues <- sequence $ replicate (length peers - 1) STM.newTQueueIO
-- Start tasks and wait forever.
-- Note: Graceful shutdown is not implemented.
(_, result)
<- Async.waitAnyCatchCancel
=<< mapM Async.async
[ Monad.forever $ do
STM.atomically $ readMailOnce nodeState kvState
Counter.inc (deliverCount stats)
-- Note: sendMailThread does not receive the url of the current process.
, sendMailThread stats (removeIndex pid peers) peerQueues
-- Note: Server listens on the port specified in the peer list.
, Warp.run port
. metricsMiddleware
. Gzip.gzip Gzip.def
$ handlers stats peerQueues nodeState kvState
]
printf "main thread exited: %s\n" $ show result
return ()
where
mkMetricsStore = do
let metricsHost = "localhost"
metricsPort = 9890
printf "Starting metrics server on %s and %d\n" (show metricsHost) metricsPort
server <- EKG.forkServer metricsHost metricsPort
return $ EKG.serverMetricStore server
waiMetricsMiddleware store = do
metrics <- Metrics.registerWaiMetrics store
return $ Metrics.metrics metrics
processMetrics store = Stats
<$> EKG.createCounter "cbcast.deliverCount" store
<*> EKG.createCounter "cbcast.receiveCount" store
<*> EKG.createDistribution "cbcast.receivedIsDeliverable" store
<*> EKG.createCounter "cbcast.broadcastCount" store
<*> EKG.createDistribution "cbcast.unicastAttemptSuccessful" store
<*> EKG.createDistribution "cbcast.unicastSuccessSize" store
-- * Helpers
-- | Parse a CBCAST PID with useful error messages.
--
-- >>> parsePID "2" [undefined, undefined, undefined]
-- Right 2
--
-- >>> parsePID "4" [undefined, undefined, undefined]
-- Left "invalid integer range 4, should be (0,3]"
-- >>> parsePID "foo" [undefined, undefined, undefined]
-- Left "invalid integer literal 'foo'"
parsePID :: String -> [a] -> Either String CBCAST.PID
parsePID s xs = case readEither s of
Left _ -> Left $ printf "invalid integer literal '%s'" s
Right n
| 0 <= n && n < length xs -> Right n
| otherwise -> Left $ printf "invalid integer range %d, should be (0,%d]" n (length xs)
-- | Remove the specified index from the list.
--
-- >>> removeIndex 0 "abc"
-- "bc"
-- >>> removeIndex 2 "abc"
-- "ab"
--
-- >>> removeIndex (-1) "abc"
-- "abc"
-- >>> removeIndex 10 "abc"
-- "abc"
--
removeIndex :: Int -> [a] -> [a]
removeIndex n xs
| 0 <= n && n < length xs = let (before, after) = splitAt n xs in before ++ tail after
| otherwise = xs
-- | Render client errors for better log messages.
showClientError :: Servant.ClientError -> String
showClientError err = case err of
Servant.FailureResponse _RequestF response -> summarize "FailureResponse" response
Servant.DecodeFailure _Text response -> summarize "DecodeFailure" response
Servant.UnsupportedContentType mediaType response -> summarize (printf "UnsupportedContentType: %s" $ show mediaType) response
Servant.InvalidContentTypeHeader response -> summarize "InvalidContentTypeHeader" response
Servant.ConnectionError someException -> printf "ConnectionError: %s" $ show someException
where
summarize :: String -> Servant.Response -> String
summarize cat resp = printf "%s: %s" cat (show $ Servant.responseStatusCode resp)