use of com.linkedin.databus.core.TrailFilePositionSetter in project databus by linkedin.
the class GoldenGateEventProducer method locateScnInTrailFile.
/**
* Given an xml directory and prefix, the method identifies the file which has the scn (_scn from event producer class)
* and returns an inputstream reader pointing to the scn location. If the scn is not found:
* 1. If scn less than what is present in the trail file directory (minimum) - throws a fatal exception.
* 2. If exact scn is not found, but it's greater than the minimum scn in the trail file directory, it returns the closest scn greater than _scn (from the event producer class).
* This methods reads and modifies the _scn from the event producer class.
* @param xmlDir The directory where the trail files are located
* @param xmlPrefix The prefix of the xml trail files, eg. x4
* @return
* @throws IOException
* @throws DatabusException
*/
private ConcurrentAppendableCompositeFileInputStream locateScnInTrailFile(String xmlDir, String xmlPrefix) throws Exception {
ConcurrentAppendableCompositeFileInputStream compositeInputStream = null;
TrailFilePositionSetter.FilePositionResult filePositionResult = null;
TrailFilePositionSetter trailFilePositionSetter = null;
while (compositeInputStream == null) {
_log.info("Requesting trail file position setter for scn: " + _scn.get());
trailFilePositionSetter = new TrailFilePositionSetter(xmlDir, xmlPrefix, getName());
filePositionResult = trailFilePositionSetter.locateFilePosition(_scn.get(), new GGXMLTrailTransactionFinder());
_log.info("File position at : " + filePositionResult);
switch(filePositionResult.getStatus()) {
case ERROR:
_log.fatal("Unable to locate the scn in the trail file.");
throw new DatabusException("Unable to find the given scn " + _scn.get() + " in the trail files");
case NO_TXNS_FOUND:
// If the latest scn is not found in the trail files, then use the earliest scn.
if (_scn.get() == TrailFilePositionSetter.USE_LATEST_SCN) {
_log.info("Switching from USE_LATEST_SCN to USE_EARLIEST_SCN because no trail files were not found");
_scn.set(TrailFilePositionSetter.USE_EARLIEST_SCN);
}
// TODO sleep get configuration for sleep time
long noTxnsFoundSleepTime = 500;
_log.info("NO_TXNS_FOUND, sleeping for " + noTxnsFoundSleepTime + " ms before retrying");
Thread.sleep(noTxnsFoundSleepTime);
break;
case EXACT_SCN_NOT_FOUND:
{
_log.info("Exact SCN was not found, the closest scn found was: " + filePositionResult.getTxnPos().getMinScn());
compositeInputStream = new ConcurrentAppendableCompositeFileInputStream(xmlDir, filePositionResult.getTxnPos().getFile(), filePositionResult.getTxnPos().getFileOffset(), new TrailFilePositionSetter.FileFilter(new File(xmlDir), xmlPrefix), false);
long foundScn = filePositionResult.getTxnPos().getMaxScn();
/**
* If exact scn is not found, the trail file position setter returns the next immediate available scn, i.e., the contract guarantees
* a scn always greater than the given scn (foundscn > _scn). We use the _scn (requested scn to be found) as the prevScn to start the event buffer.
* And the scn found as the current scn(first event in the relay).
*/
if (foundScn <= _scn.get())
throw new DatabusException("EXACT_SCN_NOT_FOUND, but foundScn is <= _scn ");
_startPrevScn.set(_scn.get());
_log.info("Changing current scn from " + _scn.get() + " to " + foundScn);
_log.info("Planning to use prevScn " + _startPrevScn);
_scn.set(foundScn);
break;
}
case FOUND:
{
_log.info("Exact SCN was found" + filePositionResult.getTxnPos().getMaxScn());
compositeInputStream = new ConcurrentAppendableCompositeFileInputStream(xmlDir, filePositionResult.getTxnPos().getFile(), filePositionResult.getTxnPos().getFileOffset(), new TrailFilePositionSetter.FileFilter(new File(xmlDir), xmlPrefix), false);
/**
* The trail file position setter returns FOUND in two cases:
* 1. MaxScn was given as input.
* 2. Earliest or Latest scn was given as input.
* For both the cases, we set the prevScn to the foundScn-1 and the foundScn as the currentScn.
*/
long foundScn = filePositionResult.getTxnPos().getMaxScn();
// Assert that if maxScn was requested, the trail file position setter has returned the exact scn (It has returned FOUND).
if (_scn.get() >= 0 && _scn.get() != foundScn) {
throw new DatabusException("The exact scn was not found, but the trail file position setter has returned FOUND!");
}
_startPrevScn.set(foundScn - 1);
_scn.set(foundScn);
break;
}
default:
throw new DatabusException("Unhandled file position result in switch case, terminating producer.");
}
}
if (filePositionResult == null) {
_log.info(trailFilePositionSetter);
throw new DatabusException("file position Result returned by TrailFilePositionSetter is null!");
}
if (_scn.get() <= 0) {
_log.info("The scn is <=0, using scn from file position setter:" + filePositionResult);
_scn.set(filePositionResult.getTxnPos().getMaxScn());
}
return compositeInputStream;
}
Aggregations