Search in sources :

Example 1 with TimeSpan

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);
    }
}
Also used : ValidateAction(org.platformlayer.core.model.ValidateAction) ConfigureAction(org.platformlayer.core.model.ConfigureAction) DeleteAction(org.platformlayer.core.model.DeleteAction) BackupAction(org.platformlayer.core.model.BackupAction) Action(org.platformlayer.core.model.Action) OpsException(org.platformlayer.ops.OpsException) WebApplicationException(javax.ws.rs.WebApplicationException) BackupHelpers(org.platformlayer.ops.backups.BackupHelpers) ManagedItemRepository(org.platformlayer.xaas.repository.ManagedItemRepository) OpsContext(org.platformlayer.ops.OpsContext) TimeSpan(com.fathomdb.TimeSpan) BackupAction(org.platformlayer.core.model.BackupAction) ServiceType(org.platformlayer.ids.ServiceType) BackupContext(org.platformlayer.ops.backups.BackupContext) HasRetryInfo(org.platformlayer.exceptions.HasRetryInfo) List(java.util.List) BindingScope(org.platformlayer.ops.BindingScope) ConfigureAction(org.platformlayer.core.model.ConfigureAction) ItemBase(org.platformlayer.core.model.ItemBase) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) RepositoryException(org.platformlayer.RepositoryException) RepositoryException(org.platformlayer.RepositoryException) OpsException(org.platformlayer.ops.OpsException) WebApplicationException(javax.ws.rs.WebApplicationException) ServiceProvider(org.platformlayer.xaas.services.ServiceProvider) ProjectAuthorization(org.platformlayer.model.ProjectAuthorization) DeleteAction(org.platformlayer.core.model.DeleteAction) ValidateAction(org.platformlayer.core.model.ValidateAction) ManagedItemState(org.platformlayer.core.model.ManagedItemState)

Example 2 with TimeSpan

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);
    }
}
Also used : TimeSpan(com.fathomdb.TimeSpan) Date(java.util.Date)

Example 3 with TimeSpan

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);
    }
}
Also used : TimeSpan(com.fathomdb.TimeSpan) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) SshException(org.platformlayer.ops.ssh.SshException) RuntimeSshException(org.apache.sshd.common.RuntimeSshException) RuntimeSshException(org.apache.sshd.common.RuntimeSshException)

Example 4 with TimeSpan

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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) TimeSpan(com.fathomdb.TimeSpan) HttpServletResponse(javax.servlet.http.HttpServletResponse) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 5 with TimeSpan

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;
}
Also used : TimeSpan(com.fathomdb.TimeSpan) CuratorFramework(com.netflix.curator.framework.CuratorFramework) OpsException(org.platformlayer.ops.OpsException) RetryOneTime(com.netflix.curator.retry.RetryOneTime) Builder(com.netflix.curator.framework.CuratorFrameworkFactory.Builder) IOException(java.io.IOException) RetryPolicy(com.netflix.curator.RetryPolicy)

Aggregations

TimeSpan (com.fathomdb.TimeSpan)10 IOException (java.io.IOException)4 Date (java.util.Date)3 OpsException (org.platformlayer.ops.OpsException)3 SshException (org.platformlayer.ops.ssh.SshException)3 RuntimeSshException (org.apache.sshd.common.RuntimeSshException)2 RetryPolicy (com.netflix.curator.RetryPolicy)1 CuratorFramework (com.netflix.curator.framework.CuratorFramework)1 Builder (com.netflix.curator.framework.CuratorFrameworkFactory.Builder)1 RetryOneTime (com.netflix.curator.retry.RetryOneTime)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Socket (java.net.Socket)1 SimpleDateFormat (java.text.SimpleDateFormat)1 List (java.util.List)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 RepositoryException (org.platformlayer.RepositoryException)1 Action (org.platformlayer.core.model.Action)1 BackupAction (org.platformlayer.core.model.BackupAction)1