Search in sources :

Example 1 with StreamNotReadyException

use of com.twitter.distributedlog.exceptions.StreamNotReadyException in project distributedlog by twitter.

the class StreamImpl method submit.

/**
 * Execute the StreamOp. If reacquire is needed, this may initiate reacquire and queue the op for
 * execution once complete.
 *
 * @param op
 *          stream operation to execute.
 */
@Override
public void submit(StreamOp op) {
    // Let stream acquire thread know a write has been attempted.
    writeSinceLastAcquire = true;
    try {
        limiter.apply(op);
    } catch (OverCapacityException ex) {
        op.fail(ex);
        return;
    }
    // Timeout stream op if requested.
    if (serviceTimeoutMs > 0) {
        scheduleTimeout(op);
    }
    boolean notifyAcquireThread = false;
    boolean completeOpNow = false;
    boolean success = true;
    if (StreamStatus.isUnavailable(status)) {
        // Stream is closed, fail the op immediately
        op.fail(new StreamUnavailableException("Stream " + name + " is closed."));
        return;
    }
    if (StreamStatus.INITIALIZED == status && writer != null) {
        completeOpNow = true;
        success = true;
    } else {
        synchronized (this) {
            if (StreamStatus.isUnavailable(status)) {
                // complete the write op as {@link #executeOp(op, success)} will handle closed case.
                completeOpNow = true;
                success = true;
            }
            if (StreamStatus.INITIALIZED == status) {
                completeOpNow = true;
                success = true;
            } else if (StreamStatus.BACKOFF == status && lastAcquireFailureWatch.elapsed(TimeUnit.MILLISECONDS) < nextAcquireWaitTimeMs) {
                completeOpNow = true;
                success = false;
            } else if (failFastOnStreamNotReady) {
                notifyAcquireThread = true;
                completeOpNow = false;
                success = false;
                op.fail(new StreamNotReadyException("Stream " + name + " is not ready; status = " + status));
            } else {
                // closing & initializing
                notifyAcquireThread = true;
                pendingOps.add(op);
                pendingOpsCounter.inc();
                if (1 == pendingOps.size()) {
                    if (op instanceof HeartbeatOp) {
                        ((HeartbeatOp) op).setWriteControlRecord(true);
                    }
                }
            }
        }
    }
    if (notifyAcquireThread && !suspended) {
        scheduleTryAcquireOnce(0L);
    }
    if (completeOpNow) {
        executeOp(op, success);
    }
}
Also used : StreamUnavailableException(com.twitter.distributedlog.exceptions.StreamUnavailableException) StreamNotReadyException(com.twitter.distributedlog.exceptions.StreamNotReadyException) OverCapacityException(com.twitter.distributedlog.exceptions.OverCapacityException)

Aggregations

OverCapacityException (com.twitter.distributedlog.exceptions.OverCapacityException)1 StreamNotReadyException (com.twitter.distributedlog.exceptions.StreamNotReadyException)1 StreamUnavailableException (com.twitter.distributedlog.exceptions.StreamUnavailableException)1