i glanced through ThriftCluster.java and didn't see any "schema sync"
code for waiting until the cluster is in sync with the latest schema
changes. below are three methods i use to wait for schema changes to
propagate throughout the cluster. being that cassandra is
distributed, you can't assume that the changes are immediate
throughout the cluster. start with "waitForSchemaSync" and pass it
the version ID returned by the add/remove keyspace or column family
command (the system_ methods in the Thrift API)
cheers
private void waitForSchemaSync(String newVer) {
if (null == newVer || newVer.isEmpty()) {
throw new IllegalArgumentException("version cannot be null
or empty");
}
long start = System.currentTimeMillis();
while (!isSchemaInSync(newVer) && (System.currentTimeMillis()
- start < MAX_WAIT_SCHEMA_SYNC)) {
try {
Thread.sleep(200);
}
catch (InterruptedException e) {
Thread.interrupted();
}
}
logger.info("Waited {}ms to sync schema",
System.currentTimeMillis() - start);
}
public boolean isSchemaInSync(String version) {
Map<String, List<String>> schemaMap = getSchemaVersionMap();
if (null == schemaMap) {
return false;
}
return null != schemaMap && schemaMap.containsKey(version) &&
1 == schemaMap.size();
}
protected Map<String, List<String>> getSchemaVersionMap() {
Map<String, List<String>> schemaMap;
CassandraClient client = cluster.borrowClient();
try {
Client thriftClient = client.getCassandra();
schemaMap = thriftClient.describe_schema_versions();
}
catch (Exception e) {
throw new RuntimeException(e);
}
finally {
cluster.releaseClient(client);
}
return schemaMap;