Search in sources :

Example 1 with StateStore

use of org.apache.bookkeeper.statelib.api.StateStore in project bookkeeper by apache.

the class AbstractStateStoreWithJournal method init.

@Override
public CompletableFuture<Void> init(StateStoreSpec spec) {
    try {
        validateStoreSpec(spec);
    } catch (IllegalArgumentException e) {
        log.error("Fail to init state store due to : ", e);
        return FutureUtils.exception(e);
    }
    this.spec = spec;
    this.name = spec.getName();
    if (null != spec.getWriteIOScheduler()) {
        writeIOScheduler = spec.getWriteIOScheduler();
        ownWriteScheduler = false;
    } else {
        ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("statestore-" + spec.getName() + "-write-io-scheduler-%d").build();
        writeIOScheduler = Executors.newSingleThreadScheduledExecutor(threadFactory);
        ownWriteScheduler = true;
    }
    if (null != spec.getReadIOScheduler()) {
        readIOScheduler = spec.getReadIOScheduler();
    } else if (ownWriteScheduler) {
        readIOScheduler = writeIOScheduler;
        ownReadScheduler = false;
    } else {
        ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("statestore-" + spec.getName() + "-read-io-scheduler-%d").build();
        readIOScheduler = Executors.newSingleThreadScheduledExecutor(threadFactory);
        ownReadScheduler = true;
    }
    if (null != spec.getCheckpointStore()) {
        this.checkpointInterval = spec.getCheckpointDuration();
    } else {
        this.checkpointInterval = null;
    }
    if (spec.isReadonly()) {
        return initializeLocalStore(spec).thenComposeAsync(ignored -> getLastDLSN(spec), writeIOScheduler).thenComposeAsync(endDLSN -> replayJournal(endDLSN), writeIOScheduler);
    } else {
        return initializeLocalStore(spec).thenComposeAsync(ignored -> initializeJournalWriter(spec), writeIOScheduler).thenComposeAsync(endDLSN -> {
            log.info("Successfully write a barrier record for mvcc store {} at {}", name, endDLSN);
            return replayJournal(endDLSN);
        }, writeIOScheduler);
    }
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) ScheduledFuture(java.util.concurrent.ScheduledFuture) Getter(lombok.Getter) StateStoreClosedException(org.apache.bookkeeper.statelib.api.exceptions.StateStoreClosedException) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) Callable(java.util.concurrent.Callable) CompletableFuture(java.util.concurrent.CompletableFuture) LogNotFoundException(org.apache.distributedlog.exceptions.LogNotFoundException) Supplier(java.util.function.Supplier) StateStoreRuntimeException(org.apache.bookkeeper.statelib.api.exceptions.StateStoreRuntimeException) StateStoreException(org.apache.bookkeeper.statelib.api.exceptions.StateStoreException) ByteBuf(io.netty.buffer.ByteBuf) LogRecordWithDLSN(org.apache.distributedlog.LogRecordWithDLSN) Duration(java.time.Duration) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) DLSN(org.apache.distributedlog.DLSN) Utils(org.apache.distributedlog.util.Utils) AsyncStateStore(org.apache.bookkeeper.statelib.api.AsyncStateStore) ThreadFactory(java.util.concurrent.ThreadFactory) LogRecord(org.apache.distributedlog.LogRecord) FutureEventListener(org.apache.bookkeeper.common.concurrent.FutureEventListener) StateStore(org.apache.bookkeeper.statelib.api.StateStore) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) IOException(java.io.IOException) FutureUtils(org.apache.bookkeeper.common.concurrent.FutureUtils) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) Namespace(org.apache.distributedlog.api.namespace.Namespace) Slf4j(lombok.extern.slf4j.Slf4j) LogEmptyException(org.apache.distributedlog.exceptions.LogEmptyException) StateStoreSpec(org.apache.bookkeeper.statelib.api.StateStoreSpec) AsyncLogWriter(org.apache.distributedlog.api.AsyncLogWriter) AsyncLogReader(org.apache.distributedlog.api.AsyncLogReader) ThreadFactory(java.util.concurrent.ThreadFactory) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder)

Aggregations

Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 ByteBuf (io.netty.buffer.ByteBuf)1 IOException (java.io.IOException)1 Duration (java.time.Duration)1 Callable (java.util.concurrent.Callable)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Executors (java.util.concurrent.Executors)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 ScheduledFuture (java.util.concurrent.ScheduledFuture)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 TimeUnit (java.util.concurrent.TimeUnit)1 Supplier (java.util.function.Supplier)1 Getter (lombok.Getter)1 Slf4j (lombok.extern.slf4j.Slf4j)1 FutureEventListener (org.apache.bookkeeper.common.concurrent.FutureEventListener)1 FutureUtils (org.apache.bookkeeper.common.concurrent.FutureUtils)1 AsyncStateStore (org.apache.bookkeeper.statelib.api.AsyncStateStore)1 StateStore (org.apache.bookkeeper.statelib.api.StateStore)1 StateStoreSpec (org.apache.bookkeeper.statelib.api.StateStoreSpec)1