try {
cycleType = waitForNextCycle();
} catch (final InterruptedException e) {
return;
}
ViewCycleExecutionOptions executionOptions = null;
try {
if (!getExecutionOptions().getExecutionSequence().isEmpty()) {
executionOptions = getExecutionOptions().getExecutionSequence().poll(getExecutionOptions().getDefaultExecutionOptions());
s_logger.debug("Next cycle execution options: {}", executionOptions);
}
if (executionOptions == null) {
s_logger.info("No more view cycle execution options");
jobCompleted();
return;
}
} catch (final Exception e) {
s_logger.error("Error obtaining next view cycle execution options from sequence for " + getWorkerContext(), e);
return;
}
if (executionOptions.getMarketDataSpecifications().isEmpty()) {
s_logger.error("No market data specifications for cycle");
cycleExecutionFailed(executionOptions, new OpenGammaRuntimeException("No market data specifications for cycle"));
return;
}
SnapshotManager snapshotManager;
try {
snapshotManager = _marketDataManager.createSnapshotManagerForCycle(getViewDefinition().getMarketDataUser(), executionOptions.getMarketDataSpecifications());
_executionCacheKey = ViewExecutionCacheKey.of(getViewDefinition(), _marketDataManager.getAvailabilityProvider());
} catch (final Exception e) {
s_logger.error("Error with market data provider", e);
cycleExecutionFailed(executionOptions, new OpenGammaRuntimeException("Error with market data provider", e));
return;
}
Instant compilationValuationTime;
try {
if (executionOptions.getValuationTime() != null) {
compilationValuationTime = executionOptions.getValuationTime();
} else {
// Neither the cycle-specific options nor the defaults have overridden the valuation time so use the time
// associated with the market data snapshot. To avoid initialising the snapshot perhaps before the required
// inputs are known or even subscribed to, only ask for an indication at the moment.
compilationValuationTime = snapshotManager.getSnapshotTimeIndication();
if (compilationValuationTime == null) {
throw new OpenGammaRuntimeException("Market data snapshot " + snapshotManager + " produced a null indication of snapshot time");
}
}
} catch (final Exception e) {
s_logger.error("Error obtaining compilation valuation time", e);
cycleExecutionFailed(executionOptions, new OpenGammaRuntimeException("Error obtaining compilation valuation time", e));
return;
}
final VersionCorrection versionCorrection = getResolverVersionCorrection(executionOptions);
VersionCorrectionUtils.lock(versionCorrection);
try {
final CompiledViewDefinitionWithGraphs compiledViewDefinition;
try {
// Don't query the cache so that the process gets a "compiled" message even if a cached compilation is used
final CompiledViewDefinitionWithGraphs previous = _latestCompiledViewDefinition;
if (_ignoreCompilationValidity && (previous != null) && CompiledViewDefinitionWithGraphsImpl.isValidFor(previous, compilationValuationTime)) {
compiledViewDefinition = previous;
} else {
compiledViewDefinition = getCompiledViewDefinition(compilationValuationTime, versionCorrection);
if (compiledViewDefinition == null) {
s_logger.warn("Job terminated during view compilation");
return;
}
if ((previous == null) || !previous.getCompilationIdentifier().equals(compiledViewDefinition.getCompilationIdentifier())) {
if (_targetResolverChanges != null) {
// We'll try to register for changes that will wake us up for a cycle if market data is not ticking
if (previous != null) {
final Set<UniqueId> subscribedIds = new HashSet<>(previous.getResolvedIdentifiers().values());
for (UniqueId uid : compiledViewDefinition.getResolvedIdentifiers().values()) {
if (!subscribedIds.contains(uid)) {
_targetResolverChanges.watch(uid.getObjectId());
}
}
} else {
for (UniqueId uid : compiledViewDefinition.getResolvedIdentifiers().values()) {
_targetResolverChanges.watch(uid.getObjectId());
}
}
}
viewDefinitionCompiled(compiledViewDefinition);
}
}
} catch (final Exception e) {
final String message = MessageFormat.format("Error obtaining compiled view definition {0} for time {1} at version-correction {2}", getViewDefinition().getUniqueId(),
compilationValuationTime, versionCorrection);
s_logger.error(message);
cycleExecutionFailed(executionOptions, new OpenGammaRuntimeException(message, e));
return;
}
// [PLAT-1174] This is necessary to support global injections by ValueRequirement. The use of a process-context level variable will be bad
// if there are multiple worker threads that initialise snapshots concurrently.
getProcessContext().getLiveDataOverrideInjector().setComputationTargetResolver(
getProcessContext().getFunctionCompilationService().getFunctionCompilationContext().getRawComputationTargetResolver().atVersionCorrection(versionCorrection));
try {
snapshotManager.addMarketDataRequirements(compiledViewDefinition.getMarketDataRequirements());
if (getExecutionOptions().getFlags().contains(ViewExecutionFlags.AWAIT_MARKET_DATA)) {
snapshotManager.initialiseSnapshotWithSubscriptionResults();
} else {
snapshotManager.initialiseSnapshot();
}
if (executionOptions.getValuationTime() == null) {
executionOptions = executionOptions.copy().setValuationTime(snapshotManager.getSnapshotTime()).create();
}
} catch (final Exception e) {
s_logger.error("Error initializing snapshot {}", snapshotManager);
cycleExecutionFailed(executionOptions, new OpenGammaRuntimeException("Error initializing snapshot " + snapshotManager, e));
}
if (_executeCycles) {
EngineResourceReference<SingleComputationCycle> cycleReference;
try {
cycleReference = createCycle(executionOptions, compiledViewDefinition, versionCorrection);
} catch (final Exception e) {
s_logger.error("Error creating next view cycle for " + getWorkerContext(), e);
return;
}
try {
try {
final SingleComputationCycle singleComputationCycle = cycleReference.get();
final Map<String, Collection<ComputationTargetSpecification>> configToComputationTargets = new HashMap<>();
final Map<String, Map<ValueSpecification, Set<ValueRequirement>>> configToTerminalOutputs = new HashMap<>();
final MarketDataSnapshot marketDataSnapshot = snapshotManager.getSnapshot();
for (DependencyGraphExplorer graphExp : compiledViewDefinition.getDependencyGraphExplorers()) {
final DependencyGraph graph = graphExp.getWholeGraph();
configToComputationTargets.put(graph.getCalculationConfigurationName(), graph.getAllComputationTargets());
configToTerminalOutputs.put(graph.getCalculationConfigurationName(), graph.getTerminalOutputs());
}
if (isTerminated()) {
return;
}
cycleStarted(new DefaultViewCycleMetadata(
cycleReference.get().getUniqueId(),
marketDataSnapshot.getUniqueId(),
compiledViewDefinition.getViewDefinition().getUniqueId(),
versionCorrection,
executionOptions.getValuationTime(),
singleComputationCycle.getAllCalculationConfigurationNames(),
configToComputationTargets,
configToTerminalOutputs));
if (isTerminated()) {
return;