25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

336 lines
8.8 KiB

10 년 전
10 년 전
10 년 전
10 년 전
10 년 전
10 년 전
10 년 전
10 년 전
  1. # Goldrush #
  2. Goldrush is a small Erlang app that provides fast event stream processing
  3. # Features #
  4. * Event processing compiled to a query module
  5. - per module private event processing statistics
  6. - query module logic can be combined for any/all filters
  7. - query module logic can be reduced to efficiently match event processing
  8. * Complex event processing logic
  9. - match input events with greater than (gt) logic
  10. - match input events with less than (lt) logic
  11. - match input events with equal to (eq) logic
  12. - match input events with wildcard (wc) logic
  13. - match input events with notfound (nf) logic
  14. - match no input events (null blackhole) logic
  15. - match all input events (null passthrough) logic
  16. * Handle output events
  17. - Once a query has been composed the output action can be overriden
  18. with one or more erlang functions. The functions will be applied to each
  19. output event from the query.
  20. * Handle low latency retrieval of compile-time stored values.
  21. - Values stored are also provided to functions called on event output.
  22. - Handle job execution and timing which can also get values stored
  23. - create input events that include runtime on successful function executions.
  24. * Handle fastest lookups of stored values.
  25. - provide state storage option to compile, caching the values in query module.
  26. * Usage
  27. To use goldrush in your application, you need to define it as a rebar dep or
  28. include it in erlang's path.
  29. Before composing modules, you'll need to define a query. The query syntax
  30. matches any number of `{erlang, terms}' and is composed as follows:
  31. * Simple Logic
  32. - Simple logic is defined as any logic matching a single event filter
  33. Select all events where 'a' exists and is greater than 0.
  34. #+BEGIN_EXAMPLE
  35. glc:gt(a, 0).
  36. #+END_EXAMPLE
  37. Select all events where 'a' exists and is greater than or equal to 0.
  38. #+BEGIN_EXAMPLE
  39. glc:gte(a, 0).
  40. #+END_EXAMPLE
  41. Select all events where 'a' exists and is equal to 0.
  42. #+BEGIN_EXAMPLE
  43. glc:eq(a, 0).
  44. #+END_EXAMPLE
  45. Select all events where 'a' exists and is not equal to 0.
  46. #+BEGIN_EXAMPLE
  47. glc:neq(a, 0).
  48. #+END_EXAMPLE
  49. Select all events where 'a' exists and is less than 0.
  50. #+BEGIN_EXAMPLE
  51. glc:lt(a, 0).
  52. #+END_EXAMPLE
  53. Select all events where 'a' exists and is less than or equal to 0.
  54. #+BEGIN_EXAMPLE
  55. glc:lte(a, 0).
  56. #+END_EXAMPLE
  57. Select all events where 'a' exists.
  58. #+BEGIN_EXAMPLE
  59. glc:wc(a).
  60. #+END_EXAMPLE
  61. Select all events where 'a' does not exist.
  62. #+BEGIN_EXAMPLE
  63. glc:nf(a).
  64. #+END_EXAMPLE
  65. Select no input events. User as a black hole query.
  66. #+BEGIN_EXAMPLE
  67. glc:null(false).
  68. #+END_EXAMPLE
  69. Select all input events. Used as a passthrough query.
  70. #+BEGIN_EXAMPLE
  71. glc:null(true).
  72. #+END_EXAMPLE
  73. * Combined Logic
  74. - Combined logic is defined as logic matching multiple event filters
  75. Select all events where both 'a' AND 'b' exists and are greater than 0.
  76. #+BEGIN_EXAMPLE
  77. glc:all([glc:gt(a, 0), glc:gt(b, 0)]).
  78. #+END_EXAMPLE
  79. Select all events where 'a' OR 'b' exists and are greater than 0.
  80. #+BEGIN_EXAMPLE
  81. glc:any([glc:gt(a, 0), glc:gt(b, 0)]).
  82. #+END_EXAMPLE
  83. Select all events where 'a' AND 'b' exists where 'a' is greater than 1 and 'b' is less than 2.
  84. #+BEGIN_EXAMPLE
  85. glc:all([glc:gt(a, 1), glc:lt(b, 2)]).
  86. #+END_EXAMPLE
  87. Select all events where 'a' OR 'b' exists where 'a' is greater than 1 and 'b' is less than 2.
  88. #+BEGIN_EXAMPLE
  89. glc:any([glc:gt(a, 1), glc:lt(b, 2)]).
  90. #+END_EXAMPLE
  91. * Reduced Logic
  92. - Reduced logic is defined as logic which can be simplified to improve efficiency.
  93. Select all events where 'a' is equal to 1, 'b' is equal to 2 and 'c' is equal to 3 and collapse any duplicate logic.
  94. #+BEGIN_EXAMPLE
  95. glc_lib:reduce(
  96. glc:all([
  97. glc:any([glc:eq(a, 1), glc:eq(b, 2)]),
  98. glc:any([glc:eq(a, 1), glc:eq(c, 3)])])).
  99. #+END_EXAMPLE
  100. The previous example will produce and is equivalent to:
  101. #+BEGIN_EXAMPLE
  102. glc:all([glc:eq(a, 1), glc:eq(b, 2), glc:eq(c, 3)]).
  103. #+END_EXAMPLE
  104. * Composing Modules
  105. - All query modules must be compiled before use
  106. To compose a module you will take your Query defined above and compile it.
  107. #+BEGIN_EXAMPLE
  108. glc:compile(Module, Query).
  109. glc:compile(Module, Query, State).
  110. glc:compile(Module, Query, State, ResetStatistics).
  111. #+END_EXAMPLE
  112. - At this point you will be able to handle an event using a compiled query.
  113. Begin by constructing an event list.
  114. #+BEGIN_EXAMPLE
  115. Event = gre:make([{'a', 2}], [list]).
  116. #+END_EXAMPLE
  117. Now pass it to your query module to be handled.
  118. #+BEGIN_EXAMPLE
  119. glc:handle(Module, Event).
  120. #+END_EXAMPLE
  121. * Handling output events
  122. - You can override the output action with an erlang function.
  123. Write all input events as info reports to the error logger.
  124. #+BEGIN_EXAMPLE
  125. glc:with(glc:null(true), fun(E) ->
  126. error_logger:info_report(gre:pairs(E)) end).
  127. #+END_EXAMPLE
  128. Write all input events where `error_level' exists and is less than 5 as info reports to the error logger.
  129. #+BEGIN_EXAMPLE
  130. glc:with(glc:lt(error_level, 5), fun(E) ->
  131. error_logger:info_report(gre:pairs(E)) end).
  132. #+END_EXAMPLE
  133. Write all input events where `error_level' exists and is 3 or 5 as info reports to the error logger.
  134. #+BEGIN_EXAMPLE
  135. glc:any([
  136. glc:with(glc:lt(error_level, 3), fun(E) ->
  137. error_logger:info_report(gre:pairs(E)) end),
  138. glc:with(glc:lt(error_level, 5), fun(E) ->
  139. error_logger:info_report(gre:pairs(E)) end)]).
  140. #+END_EXAMPLE
  141. # Composing Modules with stored state #
  142. To compose a module with state data you will add a third argument (orddict).
  143. #+BEGIN_EXAMPLE
  144. glc:compile(Module, Query, [{stored, value}]).
  145. #+END_EXAMPLE
  146. # Accessing stored state data #
  147. Return the stored value in this query module.
  148. #+BEGIN_EXAMPLE
  149. {ok, value} = glc:get(stored).
  150. #+END_EXAMPLE
  151. Return all stored values in this query module.
  152. #+BEGIN_EXAMPLE
  153. [...] = Module:get().
  154. #+END_EXAMPLE
  155. * Composing Modules with stored data
  156. - You can create query modules with local state to compare to event data in `with' and `run'
  157. To compose a module with state data you will add a third argument (orddict).
  158. #+BEGIN_EXAMPLE
  159. glc:compile(Module, Query, [{stored, value}]).
  160. #+END_EXAMPLE
  161. * Accessing stored data in constant time
  162. - You can use query modules in a way similar to mochiglobal
  163. Return the stored value in this query module.
  164. #+BEGIN_EXAMPLE
  165. {ok, value} = glc:get(stored).
  166. #+END_EXAMPLE
  167. * Job processing with composed modules
  168. - You can use query modules to execute jobs, if the job errors or not, process an event.
  169. - `with' is similar to `run', the main difference is additional statistics and execution order
  170. - when a job completes in error, the event data will contain an additional {error, _} item
  171. To execute a job through the query module, inputting an event on success.
  172. #+BEGIN_EXAMPLE
  173. Event = gre:make([{'a', 2}], [list]).
  174. {ExecutionTime, Result}= glc:run(Module, fun(Event, State) ->
  175. %% do not end with {error, _} or throw an exception
  176. end, Event).
  177. #+END_EXAMPLE
  178. * Event Processing Statistics
  179. Return the number of input events for this query module.
  180. #+BEGIN_EXAMPLE
  181. glc:input(Module).
  182. #+END_EXAMPLE
  183. Return the number of output events for this query module.
  184. #+BEGIN_EXAMPLE
  185. glc:output(Module).
  186. #+END_EXAMPLE
  187. Return the number of filtered events for this query module.
  188. #+BEGIN_EXAMPLE
  189. glc:filter(Module).
  190. #+END_EXAMPLE
  191. * Job Processing Statistics
  192. Return the number of job runs for this query module.
  193. #+BEGIN_EXAMPLE
  194. glc:job_run(Module).
  195. #+END_EXAMPLE
  196. Return the number of job errors for this query module.
  197. #+BEGIN_EXAMPLE
  198. glc:job_error(Module).
  199. #+END_EXAMPLE
  200. Return the number of job inputs for this query module.
  201. #+BEGIN_EXAMPLE
  202. glc:job_input(Module).
  203. #+END_EXAMPLE
  204. Return the amount of time jobs took for this query module.
  205. #+BEGIN_EXAMPLE
  206. glc:job_time(Module).
  207. #+END_EXAMPLE
  208. * Some Tips & Tricks
  209. - This is really just a drop in the bucket.
  210. Return the average time jobs took for this query module.
  211. #+BEGIN_EXAMPLE
  212. glc:job_time(Module) / glc:job_input(Module) / 1000000.
  213. #+END_EXAMPLE
  214. Return the query combining the conditional logic of multiple modules
  215. #+BEGIN_EXAMPLE
  216. glc_lib:reduce(glc:all([Module1:info('query'), Module2:info('query')]).
  217. #+END_EXAMPLE
  218. Return all statistics from this query module.
  219. #+BEGIN_EXAMPLE
  220. glc:info(Module).
  221. #+END_EXAMPLE
  222. * Build
  223. #+BEGIN_EXAMPLE
  224. $ ./rebar compile
  225. #+END_EXAMPLE
  226. or
  227. #+BEGIN_EXAMPLE
  228. $ make
  229. #+END_EXAMPLE
  230. * CHANGELOG
  231. 0.1.9
  232. - Add support for running jobs
  233. 0.1.8
  234. - Add support for not equal
  235. 0.1.7
  236. - Support multiple functions specified using `with/2`
  237. - Add support for greater than or less than operators
  238. - Add state storage option for output events or lookup
  239. 0.1.7
  240. - Add job execution and timings
  241. - Add state storage option
  242. 0.1.7
  243. - Add job execution and timings
  244. - Add state storage option
  245. 0.1.6
  246. - Add notfound event matching
  247. 0.1.5
  248. - Rewrite to make highly crash resilient
  249. - per module supervision
  250. - statistics data recovery
  251. - Add wildcard event matching
  252. - Add reset counters