use of com.fathomdb.TimeSpan in project platformlayer by platformlayer.
the class OperationWorker method doOperation.
Object doOperation() throws OpsException {
final Action action = activeJob.getAction();
final PlatformLayerKey targetItemKey = activeJob.getTargetItemKey();
RenameThread rename = new RenameThread(action.getClass().getSimpleName() + " " + targetItemKey);
try {
OpsContextBuilder opsContextBuilder = opsSystem.getInjector().getInstance(OpsContextBuilder.class);
final ProjectAuthorization project = activeJob.getProjectAuthorization();
final OpsContext opsContext = opsContextBuilder.buildOpsContext(activeJob);
final ServiceType serviceType = activeJob.getServiceType();
final ServiceProvider serviceProvider = opsSystem.getServiceProvider(serviceType);
try {
return OpsContext.runInContext(opsContext, new CheckedCallable<Object, Exception>() {
@Override
public Object call() throws Exception {
log.info("Starting job");
activeJob.setState(JobState.RUNNING);
ItemBase item;
ManagedItemRepository repository = opsSystem.getManagedItemRepository();
try {
boolean fetchTags = true;
item = repository.getManagedItem(targetItemKey, fetchTags, SecretProvider.from(project));
} catch (RepositoryException e) {
throw new OpsException("Error reading item from repository", e);
}
if (item == null) {
throw new WebApplicationException(404);
}
List<Object> scopeItems = Lists.newArrayList();
addActionScopeItems(action, item, scopeItems);
Object controller = serviceProvider.getController(item);
scopeItems.add(item);
scopeItems.add(action);
BindingScope scope = BindingScope.push(scopeItems);
opsContext.recurseOperation(scope, controller);
// TODO: Should we run a verify operation before -> ACTIVE??
// (we need to fix the states as well)
ManagedItemState newState = finishAction(action, scope);
if (newState != null) {
repository.changeState(targetItemKey, newState);
item.state = newState;
}
log.info("Job finished with SUCCESS");
activeJob.setState(JobState.SUCCESS);
return null;
}
private ManagedItemState finishAction(Action action, BindingScope scope) throws OpsException {
ManagedItemState newState = null;
if (action instanceof ConfigureAction) {
newState = ManagedItemState.ACTIVE;
}
if (action instanceof ValidateAction) {
// TODO: Change state to healthy??
}
if (action instanceof DeleteAction) {
newState = ManagedItemState.DELETED;
}
if (action instanceof BackupAction) {
BackupContext backupContext = scope.getInstance(BackupContext.class);
backupContext.writeDescriptor();
}
return newState;
}
private void addActionScopeItems(Action action, ItemBase item, List<Object> scopeItems) throws OpsException {
if (action instanceof BackupAction) {
// TODO: Don't hard-code this
BackupHelpers backupHelpers = opsSystem.getInjector().getInstance(BackupHelpers.class);
BackupContext backupContext = backupHelpers.createBackupContext(item);
scopeItems.add(backupContext);
}
}
});
} catch (Throwable e) {
log.warn("Error running operation", e);
log.warn("Job finished with FAILED");
// boolean isDone = false; // We will retry
activeJob.setState(JobState.FAILED);
TimeSpan retry = null;
HasRetryInfo retryInfo = ExceptionHelpers.findRetryInfo(e);
if (retryInfo != null) {
retry = retryInfo.getRetry();
}
if (retry == null) {
// TODO: Eventually give up??
retry = ApplicationMode.isDevelopment() ? TimeSpan.ONE_MINUTE : TimeSpan.FIVE_MINUTES;
}
// TODO: State transition??
// managedItem.setState(ManagedItemState.ACTIVE, true);
log.warn("Scheduling retry in " + retry);
activeJob.enqueueRetry(retry);
return null;
} finally {
try {
activeJob.recordJobEnd();
} catch (OpsException e) {
log.error("Error recording job in registry", e);
}
}
} finally {
IoUtils.safeClose(rename);
}
}
use of com.fathomdb.TimeSpan in project platformlayer by platformlayer.
the class SimpleJobScheduleCalculator method calculateNext.
protected static Date calculateNext(final Date base, final TimeSpan previousInterval, final TimeSpan currentInterval, final JobExecution previousExecution) {
long now = System.currentTimeMillis();
if (base == null) {
// Unbased
if (previousExecution == null) {
// We avoid randomly choosing a really small fraction, as that tends to start up tasks before the system
// is really ready
double fraction;
do {
synchronized (random) {
fraction = random.nextDouble();
}
} while (fraction < MIN_RANDOM_FRACTION);
TimeSpan delay = TimeSpan.fromMilliseconds((long) (fraction * previousInterval.getTotalMilliseconds()));
return new Date(now + delay.getTotalMilliseconds());
} else {
return currentInterval.addTo(previousExecution.getStartTimestamp());
}
} else {
// Based e.g. every morning at 2AM
long partial = (now - base.getTime()) % previousInterval.getTotalMilliseconds();
long previous = now - partial;
long next = previous + currentInterval.getTotalMilliseconds();
if (previousExecution != null) {
// skew tolerance)
if (previousExecution.getStartTimestamp().getTime() <= (previous - TIME_SKEW_ALLOWANCE.getTotalMilliseconds())) {
// We missed our previous execution, we should run asap
// Introduce a random delay to stop the thundering herd
long delay = (long) (random.nextDouble() * previousInterval.getTotalMilliseconds() / 10);
long scheduleAt = now + delay;
// 2.4 hours before = 1:36AM. That seems fairly sensible.
if (scheduleAt < (next - previousInterval.getTotalMilliseconds() / 10)) {
return new Date(scheduleAt);
}
}
}
return new Date(next);
}
}
use of com.fathomdb.TimeSpan in project platformlayer by platformlayer.
the class MinaSshConnection method sshReadFile0.
@Override
protected byte[] sshReadFile0(String remoteFile, boolean sudo) throws IOException, InterruptedException, SshException {
MinaSshConnectionWrapper sshConnection = ensureConnected();
MinaScpClient scp = new MinaScpClient(sshConnection);
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
TimeSpan timeout = TimeSpan.FIVE_MINUTES;
scp.get(remoteFile, baos, timeout, sudo);
return baos.toByteArray();
} catch (IOException ioException) {
throw new SshException("Cannot read file", ioException);
} catch (SshException sshException) {
String message = sshException.getMessage();
if (message != null && message.endsWith(": No such file or directory")) {
return null;
}
throw sshException;
} catch (RuntimeSshException e) {
throw new SshException("Error reading file", e);
}
}
use of com.fathomdb.TimeSpan in project platformlayer by platformlayer.
the class GwtCacheHeaderFilter method doFilter.
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if (response instanceof HttpServletResponse) {
String requestUri = ((HttpServletRequest) request).getRequestURI();
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
TimeSpan cacheFor = null;
if (requestUri.contains(".cache.")) {
// Cache forever
cacheFor = TimeSpan.ONE_DAY.multiplyBy(365);
} else if (requestUri.contains(".nocache.")) {
// Don't ever cache
cacheFor = null;
} else {
// Micro cache
cacheFor = TimeSpan.TEN_MINUTES;
}
if (cacheFor == null) {
httpServletResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
httpServletResponse.setHeader("Expires", "0");
} else {
long now = System.currentTimeMillis() / 1000L;
long timeoutAt = now + cacheFor.getTotalSeconds();
long deltaSeconds = timeoutAt - now;
httpServletResponse.setHeader("Cache-Control", "max-age=" + deltaSeconds);
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_PATTERN_RFC1123);
String expires = dateFormat.format(new Date(timeoutAt * 1000L));
httpServletResponse.setHeader("Expires", expires);
}
}
chain.doFilter(request, response);
}
use of com.fathomdb.TimeSpan in project platformlayer by platformlayer.
the class ZookeeperContext method buildZk.
public CuratorFramework buildZk(List<String> dnsNames) throws OpsException {
String connectionString = Joiner.on(",").join(dnsNames);
Builder builder = CuratorFrameworkFactory.builder();
builder.connectString(connectionString);
TimeSpan retryInterval = TimeSpan.FIVE_SECONDS;
RetryPolicy retryPolicy = new RetryOneTime((int) retryInterval.getTotalMilliseconds());
builder.retryPolicy(retryPolicy);
CuratorFramework curatorFramework;
try {
curatorFramework = builder.build();
} catch (IOException e) {
throw new OpsException("Error building zookeeper connection", e);
}
return curatorFramework;
// TimeSpan sessionTimeout = TimeSpan.TEN_SECONDS;
//
// ZooKeeper zk = new ZooKeeper(connectionString, (int) sessionTimeout.getTotalMilliseconds(), this);
// return zk;
}
Aggregations