@Override
public void execte(TaskContext context) throws Exception{
// Get parameters from the context
String clusterName = context.getStringParameter("cluster");
Boolean ignoreSystem = context.getBooleanParameter("ignoreSystem", true);
CassandraClusterEntity entity = (CassandraClusterEntity)context.getParamater("entity");
LOG.info("Refreshing cluster " + clusterName);
// Read the current state from the DAO
// CassandraClusterEntity entity = clusterDao.read(clusterName);
Map<String, String> existingKeyspaces = entity.getKeyspaces();
if (existingKeyspaces == null) {
existingKeyspaces = Maps.newHashMap();
entity.setKeyspaces(existingKeyspaces);
}
Map<String, String> existingColumnFamilies = entity.getColumnFamilies();
if (existingColumnFamilies == null) {
existingColumnFamilies = Maps.newHashMap();
entity.setColumnFamilies(existingColumnFamilies);
}
Set<String> foundKeyspaces = Sets.newHashSet();
Set<String> foundColumnFamilies = Sets.newHashSet();
Cluster cluster = provider.acquireCluster(new ClusterKey(entity.getClusterName(), entity.getDiscoveryType()));
boolean changed = false;
// // Iterate found keyspaces
try {
for (KeyspaceDefinition keyspace : cluster.describeKeyspaces()) {
// Extract data from the KeyspaceDefinition
String ksName = keyspace.getName();
MapStringToObject keyspaceOptions = getKeyspaceOptions(keyspace);
if (existingKeyspaces.containsKey(ksName)) {
MapStringToObject previousOptions = JsonSerializer.fromString(existingKeyspaces.get(ksName), MapStringToObject.class);
MapDifference keyspaceDiff = Maps.difference(keyspaceOptions, previousOptions);
if (keyspaceDiff.areEqual()) {
LOG.info("Keyspace '{}' didn't change", new Object[]{ksName});
}
else {
changed = true;
LOG.info("CF Changed: " + keyspaceDiff.entriesDiffering());
}
}
else {
changed = true;
}
String strKeyspaceOptions = JsonSerializer.toString(keyspaceOptions);
// // Keep track of keyspace
foundKeyspaces.add(keyspace.getName());
existingKeyspaces.put(ksName, strKeyspaceOptions);
LOG.info("Found keyspace '{}|{}' : {}", new Object[]{entity.getClusterName(), ksName, keyspaceOptions});
// // Iterate found column families
for (ColumnFamilyDefinition cf : keyspace.getColumnFamilyList()) {
// Extract data from the ColumnFamilyDefinition
String cfName = String.format("%s|%s", keyspace.getName(), cf.getName());
MapStringToObject cfOptions = getColumnFamilyOptions(cf);
String strCfOptions = JsonSerializer.toString(cfOptions);
//
// // Check for changes
if (existingColumnFamilies.containsKey(cfName)) {
MapStringToObject previousOptions = JsonSerializer.fromString(existingColumnFamilies.get(cfName), MapStringToObject.class);
LOG.info("Old options: " + previousOptions);
MapDifference cfDiff = Maps.difference(cfOptions, previousOptions);
if (cfDiff.areEqual()) {
LOG.info("CF '{}' didn't change", new Object[]{cfName});
}
else {
changed = true;
LOG.info("CF Changed: " + cfDiff.entriesDiffering());
}
}
else {
changed = true;
}
//
// // Keep track of the cf
foundColumnFamilies.add(cfName);
existingColumnFamilies.put(cfName, strCfOptions);
LOG.info("Found column family '{}|{}|{}' : {}", new Object[]{entity.getClusterName(), keyspace.getName(), cf.getName(), strCfOptions});
}
}
}
catch (Exception e) {
LOG.info("Error refreshing cluster: " + entity.getClusterName(), e);
entity.setEnabled(false);
}
SetView<String> ksRemoved = Sets.difference(existingKeyspaces.keySet(), foundKeyspaces);
LOG.info("Keyspaces removed: " + ksRemoved);