Skip to content

Thread Safety

opuneet edited this page Feb 24, 2014 · 1 revision

Initializing Astyanax

This should be done only once before all threads start processing. The init code creates a keyspace object that is thread safe and hence the same instances can be given to all processing threads, once it is inited safely.

Keyspace and ColumnFamily objects

As mentioned above, keyspace is thread safe and hence can be shared among multiple threads. Similarly ColumnFamily is also thread safe.

Reads and Writes

The impls for ColumnFamilyQuery and MutationBatch maintain intermittent state for supporting features like mixed write batches, merging batches, pagination, host pinning, timers, tracers etc and hence SHOULD NOT be used by multiple threads and are NOT thread safe.

How to use Astyanax with multiple threads.

  1. Initialize the AstyanaxContext object once safely and get the Keyspace object before any threads start processing.
  2. Then handoff the Keyspace and ColumnFamily objects to the worker threads.
  3. Each worker thread must create it's own read and write object by calling keyspace.prepareQuery() and keyspace.prepareMutationBatch() separately. These method calls do not need to be synchronized, but the objects that they return MUST NOT be shared by the worker threads. Hence each worker thread should have it's own read and write object.
Clone this wiki locally