Search in sources :

Example 1 with StateStoreSpec

use of org.apache.bookkeeper.statelib.api.StateStoreSpec 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)

Example 2 with StateStoreSpec

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

the class TestRocksdbKVAsyncStoreWithCheckpoints method testInitMissingStreamName.

@Test(expected = NullPointerException.class)
public void testInitMissingStreamName() throws Exception {
    this.streamName = "test-init-missing-stream-name";
    StateStoreSpec spec = StateStoreSpec.builder().name(streamName).keyCoder(ByteArrayCoder.of()).valCoder(ByteArrayCoder.of()).localStateStoreDir(localDir).build();
    result(store.init(spec));
}
Also used : StateStoreSpec(org.apache.bookkeeper.statelib.api.StateStoreSpec) Test(org.junit.Test)

Example 3 with StateStoreSpec

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

the class TestMVCCAsyncBytesStoreImpl method testPutGetDeleteRanges.

@Test
public void testPutGetDeleteRanges() throws Exception {
    this.streamName = "test-put-kvs";
    StateStoreSpec spec = initSpec(streamName);
    result(store.init(spec));
    int numPairs = 100;
    List<PutResult<byte[], byte[]>> kvs = writeKVs(numPairs, true);
    assertEquals(numPairs, kvs.size());
    for (PutResult<byte[], byte[]> kv : kvs) {
        assertEquals(Code.OK, kv.code());
        assertNull(kv.prevKv());
        kv.close();
    }
    verifyRange(20, 70, 2, 2, 0);
    List<KeyValue<byte[], byte[]>> prevKvs = result(store.deleteRange(getKey(20), getKey(70)));
    assertNotNull(prevKvs);
    verifyRecords(prevKvs, 20, 70, 2, 2, 0);
    prevKvs.forEach(KeyValue::close);
    prevKvs = result(store.range(getKey(20), getKey(70)));
    assertTrue(prevKvs.isEmpty());
}
Also used : KeyValue(org.apache.bookkeeper.api.kv.result.KeyValue) StateStoreSpec(org.apache.bookkeeper.statelib.api.StateStoreSpec) PutResult(org.apache.bookkeeper.api.kv.result.PutResult) Test(org.junit.Test)

Example 4 with StateStoreSpec

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

the class TestMVCCAsyncBytesStoreImpl method testBasicOps.

@Test
public void testBasicOps() throws Exception {
    this.streamName = "test-basic-ops";
    StateStoreSpec spec = initSpec(streamName);
    result(store.init(spec));
    // normal put
    {
        assertNull(result(store.get(getKey(0))));
        result(store.put(getKey(0), getValue(0)));
        assertArrayEquals(getValue(0), result(store.get(getKey(0))));
    }
    // putIfAbsent
    {
        // failure case
        assertArrayEquals(getValue(0), result(store.putIfAbsent(getKey(0), getValue(99))));
        assertArrayEquals(getValue(0), result(store.get(getKey(0))));
        // success case
        byte[] key1 = getKey(1);
        assertNull(result(store.putIfAbsent(key1, getValue(1))));
        assertArrayEquals(getValue(1), result(store.get(key1)));
    }
    // vPut
    {
        // key-not-found case
        int key = 2;
        int initialVal = 2;
        int casVal = 99;
        try {
            result(store.vPut(getKey(key), getValue(initialVal), 100L));
            fail("key2 doesn't exist yet");
        } catch (MVCCStoreException e) {
            assertEquals(Code.KEY_NOT_FOUND, e.getCode());
        }
        // vPut(k, v, -1L)
        try {
            result(store.vPut(getKey(key), getValue(initialVal), -1L));
            fail("key2 doesn't exist yet");
        } catch (MVCCStoreException e) {
            assertEquals(Code.KEY_NOT_FOUND, e.getCode());
        }
        // put(key2, v)
        assertNull(result(store.putIfAbsent(getKey(key), getValue(initialVal))));
        // vPut(key2, v, 0)
        assertEquals(1L, result(store.vPut(getKey(key), getValue(casVal), 0)).longValue());
        assertArrayEquals(getValue(casVal), result(store.get(getKey(key))));
    }
    // rPut
    {
        // key-not-found case
        int key = 3;
        int initialVal = 3;
        int casVal = 99;
        try {
            result(store.rPut(getKey(key), getValue(initialVal), 100L));
            fail("key2 doesn't exist yet");
        } catch (MVCCStoreException e) {
            assertEquals(Code.KEY_NOT_FOUND, e.getCode());
        }
        // vPut(k, v, -1L)
        try {
            result(store.rPut(getKey(key), getValue(initialVal), -1L));
            fail("key2 doesn't exist yet");
        } catch (MVCCStoreException e) {
            assertEquals(Code.KEY_NOT_FOUND, e.getCode());
        }
        // put(key2, v)
        assertNull(result(store.putIfAbsent(getKey(key), getValue(initialVal))));
        KeyValue<byte[], byte[]> kv = result(store.getKeyValue(getKey(key)));
        long revision = kv.modifiedRevision();
        assertArrayEquals(getValue(initialVal), kv.value());
        // vPut(key2, v, 0)
        assertEquals(revision + 1, result(store.rPut(getKey(key), getValue(casVal), revision)).longValue());
        assertArrayEquals(getValue(casVal), result(store.get(getKey(key))));
    }
    // delete(k)
    {
        // key not found
        assertNull(result(store.delete(getKey(99))));
        // key exists
        int key = 0;
        assertArrayEquals(getValue(key), result(store.get(getKey(key))));
        assertArrayEquals(getValue(key), result(store.delete(getKey(key))));
        assertNull(result(store.get(getKey(key))));
    }
    // delete(k, v)
    {
        // key not found
        assertNull(result(store.delete(getKey(99))));
        // key exists
        int key = 1;
        assertArrayEquals(getValue(key), result(store.get(getKey(key))));
        assertFalse(result(store.delete(getKey(key), getValue(99))));
        assertArrayEquals(getValue(key), result(store.get(getKey(key))));
        assertTrue(result(store.delete(getKey(key), getValue(key))));
        assertNull(result(store.get(getKey(key))));
    }
    // vDelete
    {
        int key = 2;
        @Cleanup KeyValue<byte[], byte[]> kv = result(store.getKeyValue(getKey(key)));
        long expectedVersion = kv.version();
        try {
            result(store.vDelete(getKey(key), expectedVersion + 1));
            fail("should fail to delete a key with wrong version");
        } catch (MVCCStoreException e) {
            assertEquals(Code.BAD_REVISION, e.getCode());
        }
        // vDelete(k, -1L)
        try {
            result(store.vDelete(getKey(key), -1L));
            fail("Should fail to delete a key with version(-1)");
        } catch (MVCCStoreException e) {
            assertEquals(Code.BAD_REVISION, e.getCode());
        }
        // vDelete(key2, version)
        @Cleanup KeyValue<byte[], byte[]> deletedKv = (result(store.vDelete(getKey(key), expectedVersion)));
        assertNotNull(deletedKv);
        assertEquals(kv.createRevision(), deletedKv.createRevision());
        assertEquals(kv.modifiedRevision(), deletedKv.modifiedRevision());
        assertEquals(kv.version(), deletedKv.version());
        assertArrayEquals(kv.value(), deletedKv.value());
        assertNull(result(store.get(getKey(key))));
    }
    // rPut
    {
        int key = 3;
        @Cleanup KeyValue<byte[], byte[]> kv = result(store.getKeyValue(getKey(key)));
        long expectedRevision = kv.modifiedRevision();
        try {
            result(store.rDelete(getKey(key), expectedRevision + 1));
            fail("should fail to delete a key with wrong revision");
        } catch (MVCCStoreException e) {
            assertEquals(Code.BAD_REVISION, e.getCode());
        }
        // rDelete(k, -1L)
        try {
            result(store.rDelete(getKey(key), -1L));
            fail("Should fail to delete a key with revision(-1)");
        } catch (MVCCStoreException e) {
            assertEquals(Code.BAD_REVISION, e.getCode());
        }
        // rDelete(key2, revision)
        @Cleanup KeyValue<byte[], byte[]> deletedKv = (result(store.rDelete(getKey(key), expectedRevision)));
        assertNotNull(deletedKv);
        assertEquals(kv.createRevision(), deletedKv.createRevision());
        assertEquals(kv.modifiedRevision(), deletedKv.modifiedRevision());
        assertEquals(kv.version(), deletedKv.version());
        assertArrayEquals(kv.value(), deletedKv.value());
        assertNull(result(store.get(getKey(key))));
    }
    // increment failure
    {
        int ki = 3;
        byte[] key = getKey(ki);
        result(store.put(key, getValue(ki)));
        try {
            result(store.increment(key, 100L));
            fail("Can't increment a non-number key");
        } catch (MVCCStoreException e) {
            assertEquals(Code.ILLEGAL_OP, e.getCode());
        }
    }
    // increment success
    {
        int ki = 4;
        byte[] key = getKey(ki);
        for (int i = 0; i < 5; i++) {
            result(store.increment(key, 100L));
            @Cleanup KeyValue<byte[], byte[]> kv = result(store.getKeyValue(key));
            assertEquals(100L * (i + 1), kv.numberValue());
        }
    }
}
Also used : KeyValue(org.apache.bookkeeper.api.kv.result.KeyValue) StateStoreSpec(org.apache.bookkeeper.statelib.api.StateStoreSpec) MVCCStoreException(org.apache.bookkeeper.statelib.api.exceptions.MVCCStoreException) Test(org.junit.Test)

Example 5 with StateStoreSpec

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

the class TestMVCCAsyncBytesStoreImpl method testInitMissingStreamName.

@Test(expected = NullPointerException.class)
public void testInitMissingStreamName() throws Exception {
    this.streamName = "test-init-missing-stream-name";
    StateStoreSpec spec = StateStoreSpec.builder().name(streamName).keyCoder(ByteArrayCoder.of()).valCoder(ByteArrayCoder.of()).localStateStoreDir(tempDir).build();
    result(store.init(spec));
}
Also used : StateStoreSpec(org.apache.bookkeeper.statelib.api.StateStoreSpec) Test(org.junit.Test)

Aggregations

StateStoreSpec (org.apache.bookkeeper.statelib.api.StateStoreSpec)10 Test (org.junit.Test)7 File (java.io.File)2 KeyValue (org.apache.bookkeeper.api.kv.result.KeyValue)2 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 Path (java.nio.file.Path)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