Search in sources :

Example 1 with AsyncCloseable

use of com.twitter.distributedlog.io.AsyncCloseable in project distributedlog by twitter.

the class BKDistributedLogManager method asyncClose.

/**
     * Close the distributed log manager, freeing any resources it may hold.
     */
@Override
public Future<Void> asyncClose() {
    Promise<Void> closeFuture;
    BKLogReadHandler readHandlerToClose;
    synchronized (this) {
        if (null != closePromise) {
            return closePromise;
        }
        closeFuture = closePromise = new Promise<Void>();
        readHandlerToClose = readHandlerForListener;
    }
    // NOTE: the resources {scheduler, writerBKC, readerBKC} are mostly from namespace instance.
    //       so they are not blocking call except tests.
    AsyncCloseable resourcesCloseable = new AsyncCloseable() {

        @Override
        public Future<Void> asyncClose() {
            int schedTimeout = conf.getSchedulerShutdownTimeoutMs();
            // Clean up executor state.
            if (ownExecutor) {
                SchedulerUtils.shutdownScheduler(scheduler, schedTimeout, TimeUnit.MILLISECONDS);
                LOG.info("Stopped BKDL executor service for {}.", name);
                if (scheduler != readAheadScheduler) {
                    SchedulerUtils.shutdownScheduler(readAheadScheduler, schedTimeout, TimeUnit.MILLISECONDS);
                    LOG.info("Stopped BKDL ReadAhead Executor Service for {}.", name);
                }
                SchedulerUtils.shutdownScheduler(getLockStateExecutor(false), schedTimeout, TimeUnit.MILLISECONDS);
                LOG.info("Stopped BKDL Lock State Executor for {}.", name);
            }
            if (ownWriterBKC) {
                writerBKC.close();
            }
            if (ownReaderBKC) {
                readerBKC.close();
            }
            return Future.Void();
        }
    };
    Future<Void> closeResult = Utils.closeSequence(null, true, readHandlerToClose, pendingReaders, resourcesCloseable, new AsyncCloseable() {

        @Override
        public Future<Void> asyncClose() {
            return BKDistributedLogManager.super.asyncClose();
        }
    });
    closeResult.proxyTo(closeFuture);
    return closeFuture;
}
Also used : Promise(com.twitter.util.Promise) AsyncCloseable(com.twitter.distributedlog.io.AsyncCloseable) Future(com.twitter.util.Future)

Aggregations

AsyncCloseable (com.twitter.distributedlog.io.AsyncCloseable)1 Future (com.twitter.util.Future)1 Promise (com.twitter.util.Promise)1