Consistency Level

PHP Driver supports consistency levels when executing statements.

Background

Given
a running Cassandra cluster with “3” nodes
And
the following schema:
CREATE KEYSPACE simplex WITH replication = {
  'class': 'SimpleStrategy',
  'replication_factor': 3
};
USE simplex;
CREATE TABLE playlists (
  id uuid,
  title text,
  album text,
  artist text,
  song_id uuid,
  PRIMARY KEY (id, title, album, artist)
);

Consistency levels are specified via an ExecutionOptions object

Given
tracing is enabled
And
the following example:
<?php

$cluster     = Cassandra::cluster()
                 ->withContactPoints('127.0.0.1')
                 ->build();
                 $session     = $cluster->connect("simplex");

$insertQuery = "INSERT INTO playlists (id, song_id, artist, title, album) " .
               "VALUES (62c36092-82a1-3a00-93d1-46196ee77204, " .
                   new Cassandra\Uuid('756716f7-2e54-4715-9f00-91dcbea6cf50') . ", " .
                   "'Joséphine Baker', " .
                   "'La Petite Tonkinoise', " .
                   "'Bye Bye Blackbird')";

// ExecutionOptions is deprecated, but still legal. Disable error reporting for it.
error_reporting(E_ALL ^ E_DEPRECATED);

$options = new Cassandra\ExecutionOptions(array('consistency' => Cassandra::CONSISTENCY_ALL));
$session->execute($insertQuery, $options);

// Restore error-reporting to normal.
error_reporting(E_ALL);

// Below uses the system_traces.events table to verify consistency ALL is met
$result    = $session->execute("SELECT source from system_traces.events", $options);
$sources   = array();
foreach ($result as $row) {
    array_push($sources, (string) $row['source']);
}
$sources = array_unique($sources);
asort($sources);
foreach ($sources as $source) {
    echo $source . "\n";
}
When
it is executed
Then
its output should contain:
127.0.0.1
127.0.0.2
127.0.0.3

Consistency levels are specified via an array of execution options

Given
tracing is enabled
And
the following example:
<?php
$cluster     = Cassandra::cluster()
                 ->withContactPoints('127.0.0.1')
                 ->build();
                 $session     = $cluster->connect("simplex");

$insertQuery = "INSERT INTO playlists (id, song_id, artist, title, album) " .
               "VALUES (62c36092-82a1-3a00-93d1-46196ee77204, " .
                   new Cassandra\Uuid('756716f7-2e54-4715-9f00-91dcbea6cf50') . ", " .
                   "'Joséphine Baker', " .
                   "'La Petite Tonkinoise', " .
                   "'Bye Bye Blackbird')";
$options     = array('consistency' => Cassandra::CONSISTENCY_ALL);
$session->execute($insertQuery, $options);

// Below uses the system_traces.events table to verify consistency ALL is met
$result    = $session->execute("SELECT source from system_traces.events", $options);
$sources   = array();
foreach ($result as $row) {
    array_push($sources, (string) $row['source']);
}
$sources = array_unique($sources);
asort($sources);
foreach ($sources as $source) {
    echo $source . "\n";
}
When
it is executed
Then
its output should contain:
127.0.0.1
127.0.0.2
127.0.0.3