*/
final class UidNameCB implements Callback<Deferred<Boolean>, String> {
@Override
public Deferred<Boolean> call(final String name) throws Exception {
UIDMeta new_meta = new UIDMeta(type, uid, name);
new_meta.setCreated(timestamp);
tsdb.indexUIDMeta(new_meta);
LOG.info("Replacing corrupt UID [" + UniqueId.uidToString(uid) +
"] of type [" + type + "]");
return new_meta.syncToStorage(tsdb, true);
}
}
@Override
public Deferred<Boolean> call(final UIDMeta meta) throws Exception {
// we only want to update the time if it was outside of an hour
// otherwise it's probably an accurate timestamp
if (meta.getCreated() > (timestamp + 3600) ||
meta.getCreated() == 0) {
LOG.info("Updating UID [" + UniqueId.uidToString(uid) +
"] of type [" + type + "]");
meta.setCreated(timestamp);
// if the UIDMeta object was missing any of these fields, we'll
// consider it corrupt and replace it with a new object
if (meta.getUID() == null || meta.getUID().isEmpty() ||
meta.getType() == null) {
return tsdb.getUidName(type, uid)
.addCallbackDeferring(new UidNameCB());
} else {
// the meta was good, just needed a timestamp update so sync to
// search and storage
tsdb.indexUIDMeta(meta);
LOG.info("Syncing valid UID [" + UniqueId.uidToString(uid) +
"] of type [" + type + "]");
return meta.syncToStorage(tsdb, false);
}
} else {
LOG.debug("UID [" + UniqueId.uidToString(uid) +
"] of type [" + type + "] is up to date in storage");
return Deferred.fromResult(true);
}
}
}
/**
* Called to handle a previously unprocessed TSMeta object. This callback
* will update the "created" timestamp, create a new TSMeta object if
* missing, and update search plugins.
*/
final class TSMetaCB implements Callback<Deferred<Boolean>, TSMeta> {
private final String tsuid_string;
private final byte[] tsuid;
private final long timestamp;
/**
* Default constructor
* @param tsuid ID of the timeseries
* @param timestamp The timestamp when the first data point was recorded
*/
public TSMetaCB(final byte[] tsuid, final long timestamp) {
this.tsuid = tsuid;
tsuid_string = UniqueId.uidToString(tsuid);
this.timestamp = timestamp;
}
@Override
public Deferred<Boolean> call(final TSMeta meta) throws Exception {
// if we couldn't find a TSMeta in storage, then we need to generate a
// new one
if (meta == null) {
/**
* Called after successfully creating a TSMeta counter and object,
* used to convert the deferred long to a boolean so it can be
* combined with other calls for waiting.
*/
final class CreatedCB implements Callback<Deferred<Boolean>, Long> {
@Override
public Deferred<Boolean> call(Long value) throws Exception {
LOG.info("Created counter and meta for timeseries [" +
tsuid_string + "]");
return Deferred.fromResult(true);
}
}
/**
* Called after checking to see if the counter exists and is used
* to determine if we should create a new counter AND meta or just a
* new meta
*/
final class CounterCB implements Callback<Deferred<Boolean>, Boolean> {
@Override
public Deferred<Boolean> call(final Boolean exists) throws Exception {
if (!exists) {
// note that the increment call will create the meta object
// and send it to the search plugin so we don't have to do that
// here or in the local callback
return TSMeta.incrementAndGetCounter(tsdb, tsuid)
.addCallbackDeferring(new CreatedCB());
} else {
TSMeta new_meta = new TSMeta(tsuid, timestamp);
tsdb.indexTSMeta(new_meta);
LOG.info("Counter exists but meta was null, creating meta data for timeseries [" +
tsuid_string + "]");
return new_meta.storeNew(tsdb);
}
}
}
// Take care of situations where the counter is created but the
// meta data is not. May happen if the TSD crashes or is killed
// improperly before the meta is flushed to storage.
return TSMeta.counterExistsInStorage(tsdb, tsuid)
.addCallbackDeferring(new CounterCB());
}
// verify the tsuid is good, it's possible for this to become
// corrupted
if (meta.getTSUID() == null ||
meta.getTSUID().isEmpty()) {
LOG.warn("Replacing corrupt meta data for timeseries [" +
tsuid_string + "]");
TSMeta new_meta = new TSMeta(tsuid, timestamp);
tsdb.indexTSMeta(new_meta);
return new_meta.storeNew(tsdb);
} else {
// we only want to update the time if it was outside of an
// hour otherwise it's probably an accurate timestamp
if (meta.getCreated() > (timestamp + 3600) ||
meta.getCreated() == 0) {