subscription.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package interpreter
  11. import (
  12. "encoding/json"
  13. "fmt"
  14. "strings"
  15. "sync"
  16. "devt.de/krotik/common/cryptutil"
  17. "devt.de/krotik/common/errorutil"
  18. "devt.de/krotik/eliasdb/graph"
  19. "devt.de/krotik/eliasdb/graph/data"
  20. )
  21. /*
  22. SystemRuleGraphQLSubscriptionsName is the name of the graph manager rule which
  23. deals with subscriptions.
  24. */
  25. const SystemRuleGraphQLSubscriptionsName = "system.graphqlsubscriptions"
  26. /*
  27. SubscriptionCallbackHandler receives source stream events for a subscription.
  28. */
  29. type SubscriptionCallbackHandler interface {
  30. /*
  31. Publish is called for every event in the source stream of a subscription.
  32. This function should map the source stream event to a response stream event.
  33. */
  34. Publish(map[string]interface{}, error)
  35. /*
  36. IsFinished should return true if this handler should no longer
  37. receive events.
  38. */
  39. IsFinished() bool
  40. }
  41. var ruleMap = make(map[string]*SystemRuleGraphQLSubscriptions)
  42. /*
  43. InitSubscription ensures that the current graph manager has a rule for
  44. subscriptions to monitor data changes and forwards events to the subscription
  45. callback handler.
  46. */
  47. func (rtp *GraphQLRuntimeProvider) InitSubscription(rt *documentRuntime) {
  48. var rule *SystemRuleGraphQLSubscriptions
  49. if rt.rtp.subscriptionHandler != nil {
  50. // We already got a handler no need to create another
  51. return
  52. }
  53. // Lookup or create rule
  54. for _, r := range rtp.gm.GraphRules() {
  55. if strings.HasPrefix(r, SystemRuleGraphQLSubscriptionsName) {
  56. id := strings.Split(r, "-")[1]
  57. rule = ruleMap[id]
  58. errorutil.AssertTrue(rule != nil, "Previously created rule not found")
  59. }
  60. }
  61. if rule == nil {
  62. rule = &SystemRuleGraphQLSubscriptions{
  63. fmt.Sprintf("%x", cryptutil.GenerateUUID()),
  64. make(map[string]*subscriptionHandler),
  65. &sync.RWMutex{},
  66. }
  67. rtp.gm.SetGraphRule(rule)
  68. ruleMap[rule.ID] = rule
  69. }
  70. rtp.subscriptionHandler = &subscriptionHandler{
  71. fmt.Sprintf("%x", cryptutil.GenerateUUID()),
  72. rtp.part,
  73. make(map[string]string),
  74. &sync.RWMutex{},
  75. rt,
  76. "",
  77. rtp.callbackHandler,
  78. rule,
  79. }
  80. rule.AddHandler(rtp.subscriptionHandler)
  81. }
  82. /*
  83. subscriptionHandler coordinates a subscription.
  84. */
  85. type subscriptionHandler struct {
  86. id string // Unique ID which identifies the handler
  87. part string // Partition this handler is monitoring
  88. monitoredKinds map[string]string // All kinds which are monitored (for updates)
  89. monitoredKindsLock *sync.RWMutex // Lock for monitored kinds
  90. rt *documentRuntime // GraphQL document which can be executed
  91. lastResponse string // Last response which was given to the callback handler
  92. callbackHandler SubscriptionCallbackHandler // Handler which consumes updates
  93. rule *SystemRuleGraphQLSubscriptions // Rule which is providing events
  94. }
  95. /*
  96. HandleEvent handles an event from a rule and forwards it to the callbackHandler
  97. if appropriate.
  98. */
  99. func (h *subscriptionHandler) HandleEvent(event int, part string, node data.Node) {
  100. defer func() {
  101. // Check if the subscription is still needed - this call can be used
  102. // for done() call on a WaitGroup.
  103. if h.callbackHandler.IsFinished() {
  104. // Unsubscribe this handler - we are done
  105. h.rule.RemoveHandler(h)
  106. }
  107. }()
  108. // Only care if we are in the right partition
  109. if part == h.part {
  110. if event == graph.EventNodeUpdated {
  111. // If a node is updated only proceed if its kind is monitored
  112. if _, ok := h.monitoredKinds[node.Kind()]; !ok {
  113. return
  114. }
  115. }
  116. // Rerun the query
  117. resData, err := h.rt.Eval()
  118. // Stringify the result and see if it is different from the last response
  119. resBytes, _ := json.MarshalIndent(resData, "", " ")
  120. resString := string(resBytes)
  121. if h.lastResponse != resString || err != nil {
  122. // Finally send the new result
  123. h.callbackHandler.Publish(resData, err)
  124. h.lastResponse = resString
  125. }
  126. }
  127. }
  128. /*
  129. EnsureMonitoredKind ensure that the given kind is monitored for updates.
  130. */
  131. func (h *subscriptionHandler) EnsureMonitoredKind(kind string) {
  132. h.monitoredKindsLock.RLock()
  133. if _, ok := h.monitoredKinds[kind]; !ok {
  134. h.monitoredKindsLock.RUnlock()
  135. h.monitoredKindsLock.Lock()
  136. defer h.monitoredKindsLock.Unlock()
  137. h.monitoredKinds[kind] = ""
  138. } else {
  139. h.monitoredKindsLock.RUnlock()
  140. }
  141. }
  142. /*
  143. FetchNode intercepts a FetchNode call to the graph.Manager in order to subscribe
  144. to node updates if necessary.
  145. */
  146. func (rtp *GraphQLRuntimeProvider) FetchNode(part string, key string, kind string) (data.Node, error) {
  147. return rtp.FetchNodePart(part, key, kind, nil)
  148. }
  149. /*
  150. FetchNodePart intercepts a FetchNodePart call to the graph.Manager in order to subscribe
  151. to node updates if necessary.
  152. */
  153. func (rtp *GraphQLRuntimeProvider) FetchNodePart(part string, key string, kind string, attrs []string) (data.Node, error) {
  154. if rtp.subscriptionHandler != nil {
  155. go rtp.subscriptionHandler.EnsureMonitoredKind(kind)
  156. }
  157. return rtp.gm.FetchNodePart(part, key, kind, attrs)
  158. }
  159. /*
  160. SystemRuleGraphQLSubscriptions is a system rule to propagate state changes in the
  161. datastore to all relevant GraphQL subscriptions.
  162. */
  163. type SystemRuleGraphQLSubscriptions struct {
  164. ID string // Unique ID which identifies the rule
  165. handlers map[string]*subscriptionHandler
  166. handlersLock *sync.RWMutex
  167. }
  168. /*
  169. Name returns the name of the rule.
  170. */
  171. func (r *SystemRuleGraphQLSubscriptions) Name() string {
  172. return fmt.Sprintf("%s-%s", SystemRuleGraphQLSubscriptionsName, r.ID)
  173. }
  174. /*
  175. Handles returns a list of events which are handled by this rule.
  176. */
  177. func (r *SystemRuleGraphQLSubscriptions) Handles() []int {
  178. return []int{
  179. graph.EventNodeCreated,
  180. graph.EventNodeUpdated,
  181. graph.EventNodeDeleted,
  182. }
  183. }
  184. /*
  185. Handle handles an event.
  186. */
  187. func (r *SystemRuleGraphQLSubscriptions) Handle(gm *graph.Manager, trans graph.Trans, event int, ed ...interface{}) error {
  188. part := ed[0].(string)
  189. node := ed[1].(data.Node)
  190. r.handlersLock.RLock()
  191. defer r.handlersLock.RUnlock()
  192. for _, handler := range r.handlers {
  193. // Event is handled in a separate go routine
  194. go handler.HandleEvent(event, part, node)
  195. }
  196. return nil
  197. }
  198. /*
  199. AddHandler adds a new handler for rule events.
  200. */
  201. func (r *SystemRuleGraphQLSubscriptions) AddHandler(handler *subscriptionHandler) {
  202. r.handlersLock.Lock()
  203. defer r.handlersLock.Unlock()
  204. r.handlers[handler.id] = handler
  205. }
  206. /*
  207. RemoveHandler removes a handler from receiving further rule events.
  208. */
  209. func (r *SystemRuleGraphQLSubscriptions) RemoveHandler(handler *subscriptionHandler) {
  210. r.handlersLock.Lock()
  211. defer r.handlersLock.Unlock()
  212. delete(r.handlers, handler.id)
  213. }