Search in sources :

Example 1 with Expression

use of org.dcache.util.expression.Expression in project dcache by dCache.

the class Job method schedule.

/**
 * Schedules jobs, depending on the current state and available resources.
 * <p>
 * Closely coupled to the <code>setState</code> method.
 *
 * @see setState
 */
@GuardedBy("_lock")
private void schedule() {
    if (_state == State.CANCELLING && _running.isEmpty()) {
        setState(State.CANCELLED);
    } else if (_state != State.INITIALIZING && _state != State.NEW && !_definition.isPermanent && _queued.isEmpty() && _running.isEmpty()) {
        setState(State.FINISHED);
    } else if (_state == State.STOPPING && _running.isEmpty()) {
        setState(State.FINISHED);
    } else if (_state == State.RUNNING && (!_definition.sourceList.isValid() || !_definition.poolList.isValid())) {
        setState(State.SLEEPING);
    } else if (_state == State.RUNNING) {
        Iterator<PnfsId> i = _queued.iterator();
        while ((_running.size() < _concurrency) && i.hasNext()) {
            Expression stopWhen = _definition.stopWhen;
            if (stopWhen != null && evaluateLifetimePredicate(stopWhen)) {
                stop();
                break;
            }
            Expression pauseWhen = _definition.pauseWhen;
            if (pauseWhen != null && evaluateLifetimePredicate(pauseWhen)) {
                pause();
                break;
            }
            PnfsId pnfsId = i.next();
            if (!_context.lock(pnfsId)) {
                addError(new Error(0, pnfsId, "File is locked"));
                continue;
            }
            try {
                i.remove();
                Repository repository = _context.getRepository();
                CacheEntry entry = repository.getEntry(pnfsId);
                Task task = new Task(_taskParameters, this, _context.getPoolName(), entry.getPnfsId(), getTargetState(entry), getTargetStickyRecords(entry), getPins(entry), entry.getFileAttributes(), entry.getLastAccessTime());
                _running.put(pnfsId, task);
                _statistics.addAttempt();
                task.run();
            } catch (FileNotInCacheException e) {
                _sizes.remove(pnfsId);
            } catch (CacheException e) {
                LOGGER.error("Migration job failed to read entry: {}", e.getMessage());
                setState(State.FAILED);
                break;
            } catch (InterruptedException e) {
                LOGGER.error("Migration job was interrupted: {}", e.getMessage());
                setState(State.FAILED);
                break;
            } finally {
                if (!_running.containsKey(pnfsId)) {
                    _context.unlock(pnfsId);
                }
            }
        }
        if (_running.isEmpty()) {
            if (!_definition.isPermanent && _queued.isEmpty()) {
                setState(State.FINISHED);
            } else {
                setState(State.SLEEPING);
            }
        }
    }
}
Also used : Repository(org.dcache.pool.repository.Repository) FireAndForgetTask(org.dcache.util.FireAndForgetTask) Expression(org.dcache.util.expression.Expression) CacheException(diskCacheV111.util.CacheException) FileNotInCacheException(diskCacheV111.util.FileNotInCacheException) PnfsId(diskCacheV111.util.PnfsId) CacheEntry(org.dcache.pool.repository.CacheEntry) FileNotInCacheException(diskCacheV111.util.FileNotInCacheException) GuardedBy(javax.annotation.concurrent.GuardedBy)

Example 2 with Expression

use of org.dcache.util.expression.Expression in project dcache by dCache.

the class PoolListFilterTest method createExpression.

private Expression createExpression(String s) {
    if (s == null) {
        return null;
    }
    ExpressionParser parser = Parboiled.createParser(ExpressionParser.class);
    ParsingResult<Expression> result = new BasicParseRunner(parser.Top()).run(s);
    try {
        result.resultValue.check(symbols);
    } catch (TypeMismatchException | UnknownIdentifierException e) {
        fail(e.toString());
    }
    return result.resultValue;
}
Also used : BasicParseRunner(org.parboiled.parserunners.BasicParseRunner) Expression(org.dcache.util.expression.Expression) UnknownIdentifierException(org.dcache.util.expression.UnknownIdentifierException) TypeMismatchException(org.dcache.util.expression.TypeMismatchException) ExpressionParser(org.dcache.util.expression.ExpressionParser)

Example 3 with Expression

use of org.dcache.util.expression.Expression in project dcache by dCache.

the class Job method setState.

/**
 * Sets the state of the job.
 * <p>
 * Closely coupled to the <code>schedule</code> method.
 *
 * @see schedule
 */
private void setState(State state) {
    _lock.lock();
    try {
        if (_state != state) {
            _state = state;
            switch(_state) {
                case RUNNING:
                    schedule();
                    break;
                case SLEEPING:
                    _context.getExecutor().schedule(new FireAndForgetTask(() -> {
                        _lock.lock();
                        try {
                            if (getState() == State.SLEEPING) {
                                setState(State.RUNNING);
                            }
                        } finally {
                            _lock.unlock();
                        }
                    }), 10, TimeUnit.SECONDS);
                    break;
                case PAUSED:
                    _context.getExecutor().schedule(new FireAndForgetTask(() -> {
                        _lock.lock();
                        try {
                            if (getState() == State.PAUSED) {
                                Expression stopWhen = _definition.stopWhen;
                                if (stopWhen != null && evaluateLifetimePredicate(stopWhen)) {
                                    stop();
                                }
                                Expression pauseWhen = _definition.pauseWhen;
                                if (!evaluateLifetimePredicate(pauseWhen)) {
                                    setState(State.RUNNING);
                                }
                            }
                        } finally {
                            _lock.unlock();
                        }
                    }), 10, TimeUnit.SECONDS);
                    break;
                case FINISHED:
                case CANCELLED:
                case FAILED:
                    _queued.clear();
                    _sizes.clear();
                    _context.getRepository().removeListener(this);
                    _refreshTask.cancel(false);
                    for (Map.Entry<PoolMigrationJobCancelMessage, DelayedReply> entry : _cancelRequests.entrySet()) {
                        entry.getValue().reply(entry.getKey());
                    }
                    _cancelRequests.clear();
                    break;
            }
        }
    } finally {
        _lock.unlock();
    }
}
Also used : Expression(org.dcache.util.expression.Expression) DelayedReply(dmg.cells.nucleus.DelayedReply) FireAndForgetTask(org.dcache.util.FireAndForgetTask) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Expression (org.dcache.util.expression.Expression)3 FireAndForgetTask (org.dcache.util.FireAndForgetTask)2 CacheException (diskCacheV111.util.CacheException)1 FileNotInCacheException (diskCacheV111.util.FileNotInCacheException)1 PnfsId (diskCacheV111.util.PnfsId)1 DelayedReply (dmg.cells.nucleus.DelayedReply)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 GuardedBy (javax.annotation.concurrent.GuardedBy)1 CacheEntry (org.dcache.pool.repository.CacheEntry)1 Repository (org.dcache.pool.repository.Repository)1 ExpressionParser (org.dcache.util.expression.ExpressionParser)1 TypeMismatchException (org.dcache.util.expression.TypeMismatchException)1 UnknownIdentifierException (org.dcache.util.expression.UnknownIdentifierException)1 BasicParseRunner (org.parboiled.parserunners.BasicParseRunner)1