ractor-wrapper 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,42 +1,78 @@
1
1
  # Ractor::Wrapper
2
2
 
3
- Ractor::Wrapper is an experimental class that wraps a non-shareable object in
3
+ `Ractor::Wrapper` is an experimental class that wraps a non-shareable object in
4
4
  an actor, allowing multiple Ractors to access it concurrently.
5
5
 
6
- **WARNING:** This is a highly experimental library, and currently _not_
7
- recommended for production use. (As of Ruby 4.0.0, the same can be said of
8
- Ractors in general.)
6
+ **WARNING:** This is an experimental library, and currently _not_ recommended
7
+ for production use. (As of Ruby 4.0, the same can still be said of Ractors in
8
+ general.)
9
9
 
10
10
  ## Quick start
11
11
 
12
12
  Install ractor-wrapper as a gem, or include it in your bundle.
13
13
 
14
- gem install ractor-wrapper
14
+ ```sh
15
+ gem install ractor-wrapper
16
+ ```
15
17
 
16
18
  Require it in your code:
17
19
 
18
- require "ractor/wrapper"
20
+ ```ruby
21
+ require "ractor/wrapper"
22
+ ```
19
23
 
20
24
  You can then create wrappers for objects. See the example below.
21
25
 
22
- Ractor::Wrapper requires Ruby 4.0.0 or later.
26
+ `Ractor::Wrapper` requires Ruby 4.0.0 or later.
23
27
 
24
- ## What is Ractor::Wrapper?
28
+ ## What is `Ractor::Wrapper`?
25
29
 
26
- For the most part, unless an object is _sharable_, which generally means
30
+ For the most part, unless an object is _shareable_, which generally means
27
31
  deeply immutable along with a few other restrictions, it cannot be accessed
28
- directly from another Ractor. This makes it difficult for multiple Ractors
29
- to share a resource that is stateful. Such a resource must typically itself
30
- be implemented as a Ractor and accessed via message passing.
31
-
32
- Ractor::Wrapper makes it possible for an ordinary non-shareable object to
33
- be accessed from multiple Ractors. It does this by "wrapping" the object
34
- with an actor that listens for messages and invokes the object's methods in
35
- a controlled single-Ractor environment. It then provides a stub object that
36
- reproduces the interface of the original object, but responds to method
37
- calls by sending messages to the wrapper. Ractor::Wrapper can be used to
38
- implement simple actors by writing "plain" Ruby objects, or to adapt
39
- existing non-shareable objects to a multi-Ractor world.
32
+ directly from a Ractor other than the one in which it was constructed. This
33
+ makes it difficult for multiple Ractors to share a resource that is stateful,
34
+ such as a database connection.
35
+
36
+ +----Main-Ractor----+ +-Another-Ractor-+
37
+ | | | |
38
+ | client1 | | |
39
+ | | | | |
40
+ | | ok | | |
41
+ | v | | |
42
+ | my_db_conn <------X------ client2 |
43
+ | | fails | |
44
+ +-------------------+ +----------------+
45
+
46
+ `Ractor::Wrapper` makes it possible for an ordinary non-shareable object to
47
+ be accessed from multiple Ractors. It does this by "wrapping" the object with
48
+ a shareable proxy.
49
+
50
+ +--Main-Ractor--+ +-Wrapper-Ractor-+ +-Another-Ractor-+
51
+ | | | | | |
52
+ | client1 | | | | client2 |
53
+ | | | | | | | |
54
+ | v | | | | v |
55
+ | +----------------------------------------------+ |
56
+ | | SHAREABLE WRAPPER | |
57
+ | +----------------------------------------------+ |
58
+ | | | | | | |
59
+ | | | v | | |
60
+ | | | my_db_conn | | |
61
+ +---------------+ +----------------+ +----------------+
62
+
63
+ The wrapper provides a shareable stub object that reproduces the method
64
+ interface of the original object, so, with a few caveats, the wrapper is almost
65
+ fully transparent. Behind the scenes, the wrapper "runs" the wrapped object in
66
+ a controlled single-Ractor environment, and uses port messaging to communicate
67
+ method calls, arguments, and return values between Ractors.
68
+
69
+ `Ractor::Wrapper` can be used to adapt non-shareable objects to a multi-Ractor
70
+ world. It can also be used to implement a simple actor by writing a "plain"
71
+ Ruby object and wrapping it with a Ractor.
72
+
73
+ ## Examples
74
+
75
+ Below are some illustrative examples showing how to use `Ractor::Wrapper`.
40
76
 
41
77
  ### Net::HTTP example
42
78
 
@@ -44,6 +80,8 @@ The following example shows how to share a single Net::HTTP session object
44
80
  among multiple Ractors.
45
81
 
46
82
  ```ruby
83
+ # Net::HTTP example
84
+
47
85
  require "ractor/wrapper"
48
86
  require "net/http"
49
87
 
@@ -101,6 +139,8 @@ The following example shows how to share a SQLite3 database among multiple
101
139
  Ractors.
102
140
 
103
141
  ```ruby
142
+ # SQLite3 example
143
+
104
144
  require "ractor/wrapper"
105
145
  require "sqlite3"
106
146
 
@@ -110,12 +150,13 @@ db = SQLite3::Database.new($my_database_path)
110
150
 
111
151
  # Create a wrapper around the database. A SQLite3::Database object
112
152
  # cannot be moved between Ractors, so we configure the wrapper to run
113
- # in the current Ractor. You can also configure it to run multiple
114
- # worker threads because the database object itself is thread-safe.
153
+ # in the current Ractor instead of an internal Ractor. We can also
154
+ # configure it to run multiple worker threads because the database
155
+ # object itself is thread-safe.
115
156
  wrapper = Ractor::Wrapper.new(db, use_current_ractor: true, threads: 2)
116
157
 
117
158
  # At this point, the database object can still be accessed directly
118
- # because it hasn't been moved to a different Ractor.
159
+ # from the current Ractor because it hasn't been moved.
119
160
  rows = db.execute("select * from numbers")
120
161
 
121
162
  # You can also access the database via the stub object provided by the
@@ -123,8 +164,7 @@ rows = db.execute("select * from numbers")
123
164
  rows = wrapper.stub.execute("select * from numbers")
124
165
 
125
166
  # Here, we start two Ractors, and pass the stub to each one. The
126
- # wrapper's two worker threads will handle the requests in the order
127
- # received.
167
+ # wrapper's worker threads will handle the requests concurrently.
128
168
  r1 = Ractor.new(wrapper.stub) do |stub|
129
169
  5.times do
130
170
  stub.execute("select * from numbers")
@@ -150,39 +190,286 @@ wrapper.join
150
190
  # When running a wrapper with :use_current_ractor, you do not need to
151
191
  # recover the object, because it was never moved. The recover_object
152
192
  # method is not available.
153
- # db2 = wrapper.recover_object # <= raises Ractor::Error
193
+ # db2 = wrapper.recover_object # <= raises Ractor::Wrapper::Error
194
+ ```
195
+
196
+ ### Simple actor example
197
+
198
+ The following example demonstrates how to use `Ractor::Wrapper` to implement an
199
+ actor as a plain Ruby object. Focus on writing functionality as methods, and
200
+ let `Ractor::Wrapper` handle all the messaging logic.
201
+
202
+ ```ruby
203
+ # Simple actor example
204
+
205
+ require "ractor/wrapper"
206
+
207
+ class SimpleCalculator
208
+ class EmptyStackError < StandardError
209
+ end
210
+
211
+ def initialize
212
+ @stack = []
213
+ end
214
+
215
+ def push(number)
216
+ @stack.push(number)
217
+ nil
218
+ end
219
+
220
+ def pop
221
+ raise EmptyStackError if @stack.empty?
222
+ @stack.pop
223
+ end
224
+
225
+ def add
226
+ push(pop + pop)
227
+ nil
228
+ end
229
+ end
230
+
231
+ # Create an actor based on SimpleCalculator
232
+ calc_actor = Ractor::Wrapper.new(SimpleCalculator.new)
233
+
234
+ # You can now send messages by calling methods
235
+ calc_stub = calc_actor.stub
236
+ calc_stub.push(2)
237
+ calc_stub.push(3)
238
+ calc_stub.add
239
+ sum = calc_stub.pop
240
+
241
+ # Stop the actor by calling async_stop
242
+ calc_actor.async_stop
243
+ # Wait for the actor to shut down
244
+ calc_actor.join
245
+ ```
246
+
247
+ ## Configuring a wrapper
248
+
249
+ `Ractor::Wrapper` supports a fair amount of configuration, which may be needed
250
+ in order to ensure good behavior of the wrapped object. You can configure many
251
+ aspects of `Ractor::Wrapper` by passing keyword arguments to its constructor.
252
+ Alternatively, you can pass a block to the constructor; the constructor will
253
+ yield a configuration interface to your block, letting you configure the
254
+ wrapper's behavior in detail.
255
+
256
+ The various configuration options are described below.
257
+
258
+ ### Current Ractor mode
259
+
260
+ Normally a wrapper will spawn a new Ractor and move the wrapped object into
261
+ that Ractor. We call this default mode the "isolated Ractor" mode. Isolated
262
+ Ractor lets the object function as an actor that can be called uniformly from
263
+ any Ractor.
264
+
265
+ However, some objects cannot be moved to a different Ractor. This in particular
266
+ can include certain C-based I/O objects such as database connections.
267
+ Additionally, there are other objects that can live only in the main Ractor. If
268
+ the object to be wrapped cannot be moved to its own Ractor, configure it with
269
+ `use_current_ractor`, which will run the wrapper in a Thread in the calling
270
+ Ractor rather than trying to move it to its own Ractor. The SQLite3 example
271
+ above demonstrates wrapping an object that cannot be moved to its own Ractor.
272
+
273
+ ### Sequential vs concurrent execution
274
+
275
+ By default, wrappers run sequentially in a single Thread. The wrapper will
276
+ handle only a single method call at a time, and any other concurrent requests
277
+ are queued and blocked until their turn. This is the behavior of the classic
278
+ actor model, and in particular is appropriate for wrapped objects that are not
279
+ thread-safe.
280
+
281
+ You can, however, configure a wrapper with concurrent access. This will spin up
282
+ a configurable number of worker threads within the wrapper, to handle
283
+ potentially concurrent method calls. You should set this configuration only if
284
+ you are certain the wrapped object can handle concurrent access.
285
+
286
+ ### Data communication options
287
+
288
+ When you call a method on a wrapper, and you pass arguments and receive a
289
+ return value, or you pass a block that can receive arguments and return a
290
+ value, those objects are communicated to and from the wrapper via Ractor ports.
291
+ As such, if they are not shareable, they may be *copied* or *moved*. By
292
+ default, values are copied in order to minimize interference with surrounding
293
+ code, but a wrapper can be configured to move objects instead.
294
+
295
+ This configuration is done per-method, using the `configure_method` call in the
296
+ configuration block. You can, for particular method names, specify whether each
297
+ type of value: arguments, return values, block arguments, and block return
298
+ values, are copied or moved. For any given method, you must configure all
299
+ arguments to be handled the same way, but different methods can have different
300
+ configurations. You can also provide a default configuration that will apply to
301
+ all method names that are not explicitly configured.
302
+
303
+ Return values (and block return values) have a third configuration option:
304
+ *void*. This option disables communication of return values, sending `nil`
305
+ instead of what was actually returned from the method. This is intended for
306
+ methods that do not *semantically* need to return anything, but because of
307
+ their implementation they actually do return some internal object. You can use
308
+ the *void* option to prevent those methods from wasting resources copying a
309
+ return object unnecessarily, or worse, moving an object that shouldn't be moved.
310
+
311
+ ### Block execution environment
312
+
313
+ If a block is passed to a method, it is handled in one of two ways. By default,
314
+ if/when the method yields to the block, the wrapper will send a message *back*
315
+ to the caller, and the block will be executed in the caller's environment. In
316
+ most cases, this is what you want; your block may access information from its
317
+ lexical environment, and that environment would not be available to the wrapped
318
+ object. However, this extra communication can add overhead.
319
+
320
+ As an alternative, you can configure, per-method, blocks to be executed in the
321
+ context of the *wrapped object*. Effectively, the block itself is *moved* into
322
+ the wrapped object's Ractor/context, and called directly. This will work only
323
+ if the block does not access any information from its lexical context, or
324
+ anything that cannot be accessed from a different Ractor. A block must truly be
325
+ self-contained in order to use this option.
326
+
327
+ As with data communication options, configuring block execution environment is
328
+ done using the `configure_method` call in the configuration block. You can set
329
+ the environment either to `:caller` or `:wrapped`, and you can do so for an
330
+ individual method or provide a default to apply to all methods not explicitly
331
+ configured.
332
+
333
+ ## Additional features
334
+
335
+ ### Wrapper shutdown
336
+
337
+ If you are done with a wrapper, you should shut it down by calling `async_stop`.
338
+ This method will initiate a graceful shutdown of the wrapper, finishing any
339
+ pending method calls, and putting the wrapper in a state where it will refuse
340
+ new calls. Any additional method calls will cause a
341
+ `Ractor::Wrapper::StoppedError` to be raised.
342
+
343
+ `Ractor::Wrapper` also provides a `join` method that can be called to wait for
344
+ the wrapper to complete its shutdown.
345
+
346
+ ### Wrapped object access
347
+
348
+ The general intent is that once you've wrapped an object, all access should go
349
+ through the wrapper. In the default "isolated Ractor" mode, the wrapped object
350
+ is in fact *moved* to a different Ractor, so the Ractor system will prevent you
351
+ from accessing it directly. In "current Ractor" mode, the wrapped object is not
352
+ moved, so you technically could continue to access it directly from its
353
+ original Ractor. But beware: the wrapper runs a thread and will be making calls
354
+ to the object from that thread, which may cause you problems if the object is
355
+ not thread-safe.
356
+
357
+ In "isolated Ractor" mode, after you shut down the wrapper, you can recover the
358
+ original object by calling `recover_object`. Only one Ractor can call this
359
+ method; the object will be moved into the requesting Ractor, and any other
360
+ Ractor that subsequently requests the object will get an exception instead.
361
+
362
+ In "current Ractor" mode, the object will never have been moved to a different
363
+ Ractor, so any pre-existing references (in the original Ractor) will still be
364
+ valid. In this case, `recover_object` is not necessary and will raise an
365
+ exception if called.
366
+
367
+ ### Error handling
368
+
369
+ `Ractor::Wrapper` provides fairly robust handling of errors. If a method call
370
+ raises an exception, the exception will be passed back to the caller and raised
371
+ there. In the unlikely event that the wrapper itself crashes, it goes through a
372
+ very thorough clean-up process and makes every effort to shut down gracefully,
373
+ notifying any pending method calls that the wrapper has crashed by raising
374
+ `Ractor::Wrapper::CrashedError`.
375
+
376
+ ### Automatic stub conversion
377
+
378
+ One special case handled by the wrapper is methods that return `self`. This is
379
+ a common pattern in Ruby and is used to allow "chaining" interfaces. However,
380
+ you generally cannot return `self` from a wrapped object because, depending on
381
+ the communication configuration, you'll either get a *copy* of `self`, or
382
+ you'll *move* the object out of the wrapper, thus breaking the wrapper. Thus,
383
+ `Ractor::Wrapper` explicitly detects when methods return `self`, and instead
384
+ replaces it with the wrapper's stub object. The stub is shareable, and designed
385
+ to have the same usage as the original object, so this should work for most use
386
+ cases.
387
+
388
+ ## Known issues
389
+
390
+ Ractors are in general somewhat "bolted-on" to Ruby, and there are a lot of
391
+ caveats to their use. This also applies to `Ractor::Wrapper`, which itself is
392
+ essentially a workaround to the fact that Ruby has a lot of use cases that
393
+ simply don't play well in a Ractor world. Here we'll discuss some of the
394
+ caveats and known issues with `Ractor::Wrapper`.
395
+
396
+ ### Data communication issues
397
+
398
+ As of Ruby 4.0, most objects have been retrofitted to play reasonably with
399
+ Ractors. Some objects are shareable across Ractors, and most others can be
400
+ moved from one Ractor to another. However, there are a few objects that,
401
+ because of their semantics or details about their implementation, cannot be
402
+ moved and are confined to their creating Ractor (or in some cases, only the
403
+ main Ractor.) These may include objects such as threads, procs, backtraces, and
404
+ certain C-based objects.
405
+
406
+ One particular case of note is exception objects, which one might expect to be
407
+ shareable, but are not. Furthermore, they cannot be moved, and even copying an
408
+ exception has issues (in particular the backtrace of a copy gets cleared out).
409
+ See https://bugs.ruby-lang.org/issues/21818 for more info. When a method raises
410
+ an exception, `Ractor::Wrapper` communicates that exception via copying, which
411
+ means that currently backtraces will not be present.
412
+
413
+ ### Blocks
414
+
415
+ Ruby blocks pose particular challenges for `Ractor::Wrapper` because of their
416
+ semantics and some of their common usage patterns. We've already seen above
417
+ that `Ractor::Wrapper` can run them either in the caller's context or in the
418
+ wrapped object's context, which may limit what the block can do. Additionally,
419
+ the following restrictions apply to blocks:
420
+
421
+ Blocks configured to run in the caller's context can be run only while the
422
+ method is executing; i.e. they can only be "yielded" to. The wrapped object
423
+ cannot "save" the block as a proc to be run later, unless the block is
424
+ configured to run in the "wrapped object's" context. This is simply because we
425
+ have access to the caller only while the caller is making a method call. After
426
+ the call is done, we no longer have access to that context, and there's no
427
+ guarantee that the caller or its Ractor even exists anymore. In particular,
428
+ this means that the common Ruby idiom of using blocks to define callbacks (that
429
+ run in the context of the code defining the callback) can generally not be done
430
+ through a wrapper.
431
+
432
+ In Ruby, it is legal (although not considered very good practice) to do a
433
+ non-local `return` from inside a block. Assuming the block isn't being defined
434
+ via a lambda, this causes a return from the method *surrounding* the call that
435
+ includes the block. However, `Ractor::Wrapper` cannot reproduce this behavior.
436
+ Attempting to `return` within a block that was passed to `Ractor::Wrapper` will
437
+ result in an exception.
438
+
439
+ ### Block re-entrancy in a separate Thread/Fiber
440
+
441
+ One final known corner case has to do with block re-entrancy, i.e. calling a
442
+ method *from within a block passed to another call to the same object*. This
443
+ would mean that there are two "active" method calls to the object at once: one
444
+ made while another is "suspended" because it has yielded to the block.
445
+
446
+ ```ruby
447
+ stub.method_with_block do
448
+ stub.another_method
449
+ end
450
+ ```
451
+
452
+ In most cases, the wrapper handles this case properly, even in sequential mode
453
+ when it has no concurrency. Internally, it uses fibers to track each method
454
+ call, and it yields the fiber when yielding to a block. However, if the
455
+ "internal" method call itself is done in a separate Fiber or Thread, this would
456
+ break the internal fiber tracking. In such a case, the wrapper falls back to a
457
+ "blocking" model where it depends on concurrency to handle the simultaneous
458
+ method calls. If run in sequential mode, or if not enough worker threads are
459
+ available, this can deadlock.
460
+
461
+ ```ruby
462
+ # Can deadlock if the wrapper was created in sequential mode
463
+ stub.method_with_block do
464
+ t = Thread.new do
465
+ stub.another_method
466
+ end
467
+ t.join
468
+ end
154
469
  ```
155
470
 
156
- ### Features
157
-
158
- * Provides a Ractor-shareable method interface to a non-shareable object.
159
- * Supports arbitrary method arguments and return values.
160
- * Can be configured to run in its own isolated Ractor or in a Thread in
161
- the current Ractor.
162
- * Can be configured per method whether to copy or move arguments and
163
- return values.
164
- * Blocks can be run in the calling Ractor or in the object Ractor.
165
- * Raises exceptions thrown by the method.
166
- * Can serialize method calls for non-thread-safe objects, or run methods
167
- concurrently in multiple worker threads for thread-safe objects.
168
- * Can gracefully shut down the wrapper and retrieve the original object.
169
-
170
- ### Caveats
171
-
172
- * Certain types cannot be used as method arguments or return values
173
- because they cannot be moved between Ractors. As of Ruby 4.0.0, these
174
- include threads, backtraces, procs, and a few others.
175
- * As of Ruby 4.0.0, any exceptions raised are always copied (rather than
176
- moved) back to the calling Ractor, and the backtrace is cleared out.
177
- This is due to https://bugs.ruby-lang.org/issues/21818
178
- * Blocks can be run "in place" (i.e. in the wrapped object context) only
179
- if the block does not access any data outside the block. Otherwise, the
180
- block must be run in caller's context.
181
- * Blocks configured to run in the caller's context can only be run while
182
- a method is executing. They cannot be "saved" as a proc to be run
183
- later unless they are configured to run "in place". In particular,
184
- using blocks as a syntax to define callbacks can generally not be done
185
- through a wrapper.
471
+ To avoid this issue, do not call methods on the same wrapper from within a
472
+ separate Thread or Fiber spawned inside a block passed to that wrapper.
186
473
 
187
474
  ## Contributing
188
475
 
@@ -7,6 +7,6 @@ class Ractor
7
7
  #
8
8
  # @return [String]
9
9
  #
10
- VERSION = "0.3.0"
10
+ VERSION = "0.5.0"
11
11
  end
12
12
  end