Skip to content

Instantly share code, notes, and snippets.

@mpenick
Created September 1, 2017 17:56
Show Gist options
  • Save mpenick/25a8e77bf1c61156b229dec29790c8d9 to your computer and use it in GitHub Desktop.
Save mpenick/25a8e77bf1c61156b229dec29790c8d9 to your computer and use it in GitHub Desktop.
Attempt to reproduce "All hosts in current policy attempted and were either unavailable or failed" error
#!/bin/bash
while true; do
NODE="node$(( ( RANDOM % 3 ) + 1 ))"
echo "Marking $NODE down..."
ccm $NODE stop
ccm $NODE start
sleep 1
done
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*/
#include <stdio.h>
#include <stdlib.h>
#include <cassandra.h>
#include <time.h>
#include <unistd.h>
int main(int argc, char* argv[]) {
/* Setup and connect to cluster */
CassFuture* connect_future = NULL;
CassCluster* cluster = cass_cluster_new();
CassSession* session = cass_session_new();
const char* hosts = "127.0.0.1,127.0.0.2,127.0.0.3";
if (argc > 1) {
hosts = argv[1];
}
/* Add contact points */
cass_cluster_set_contact_points(cluster, hosts);
cass_cluster_set_load_balance_dc_aware(cluster, "datacenter1", 0, cass_false);
/* Provide the cluster object as configuration to connect the session */
connect_future = cass_session_connect(session, cluster);
if (cass_future_error_code(connect_future) == CASS_OK) {
while (1) {
/* Build statement and execute query */
const char* query = "SELECT release_version FROM system.local";
CassStatement* statement = cass_statement_new(query, 0);
cass_statement_set_is_idempotent(statement, cass_true);
CassFuture* result_future = cass_session_execute(session, statement);
if (cass_future_error_code(result_future) == CASS_OK) {
/* Retrieve result set and get the first row */
const CassResult* result = cass_future_get_result(result_future);
const CassRow* row = cass_result_first_row(result);
if (row) {
const CassValue* value = cass_row_get_column_by_name(row, "release_version");
const char* release_version;
size_t release_version_length;
cass_value_get_string(value, &release_version, &release_version_length);
printf("%u release_version: '%.*s'\n", (unsigned int)time(0), (int)release_version_length,
release_version);
}
cass_result_free(result);
} else {
/* Handle error */
const char* message;
size_t message_length;
cass_future_error_message(result_future, &message, &message_length);
fprintf(stdout, "Unable to run query: '%.*s'\n", (int)message_length,
message);
exit(-1);
}
cass_statement_free(statement);
cass_future_free(result_future);
usleep(100 * 1000);
}
} else {
/* Handle error */
const char* message;
size_t message_length;
cass_future_error_message(connect_future, &message, &message_length);
fprintf(stdout, "Unable to connect: '%.*s'\n", (int)message_length,
message);
}
cass_future_free(connect_future);
cass_cluster_free(cluster);
cass_session_free(session);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment