Search in sources :

Example 11 with RecordId

use of org.apache.jackrabbit.oak.segment.RecordId in project jackrabbit-oak by apache.

the class TarRevisions method bind.

/**
 * Bind this instance to a store.
 * @param store              store to bind to
 * @param idProvider         {@code SegmentIdProvider} of the {@code store}
 * @param writeInitialNode   provider for the initial node in case the journal is empty.
 * @throws IOException
 */
synchronized void bind(@Nonnull SegmentStore store, @Nonnull SegmentIdProvider idProvider, @Nonnull Supplier<RecordId> writeInitialNode) throws IOException {
    if (head.get() != null) {
        return;
    }
    RecordId persistedId = findPersistedRecordId(store, idProvider, journalFile);
    if (persistedId == null) {
        head.set(writeInitialNode.get());
    } else {
        persistedHead.set(persistedId);
        head.set(persistedId);
    }
}
Also used : RecordId(org.apache.jackrabbit.oak.segment.RecordId) FileStoreUtil.findPersistedRecordId(org.apache.jackrabbit.oak.segment.file.FileStoreUtil.findPersistedRecordId)

Example 12 with RecordId

use of org.apache.jackrabbit.oak.segment.RecordId in project jackrabbit-oak by apache.

the class TarRevisionsTest method concurrentSetHeadFromFunction.

@Test
public void concurrentSetHeadFromFunction() throws InterruptedException, ExecutionException, TimeoutException {
    ListeningExecutorService executor = listeningDecorator(newFixedThreadPool(2));
    try {
        ListenableFuture<Boolean> t1 = executor.submit(new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                return null != revisions.setHead(new Function<RecordId, RecordId>() {

                    @Nullable
                    @Override
                    public RecordId apply(RecordId headId) {
                        return addChild(reader.readNode(headId), "a").getRecordId();
                    }
                });
            }
        });
        ListenableFuture<Boolean> t2 = executor.submit(new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                return null != revisions.setHead(new Function<RecordId, RecordId>() {

                    @Nullable
                    @Override
                    public RecordId apply(RecordId headId) {
                        return addChild(reader.readNode(headId), "b").getRecordId();
                    }
                });
            }
        });
        assertTrue(t1.get(500, MILLISECONDS));
        assertTrue(t2.get(500, MILLISECONDS));
        SegmentNodeState root = reader.readNode(revisions.getHead());
        assertTrue(root.hasChildNode("a"));
        assertTrue(root.hasChildNode("b"));
    } finally {
        executor.shutdown();
    }
}
Also used : ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) RecordId(org.apache.jackrabbit.oak.segment.RecordId) SegmentNodeState(org.apache.jackrabbit.oak.segment.SegmentNodeState) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Nullable(javax.annotation.Nullable) Test(org.junit.Test)

Example 13 with RecordId

use of org.apache.jackrabbit.oak.segment.RecordId in project jackrabbit-oak by apache.

the class LockBasedSchedulerTest method testSimulatedRaceOnRevisions.

/**
 * OAK-7162
 *
 * This test guards against race conditions which may happen when the head
 * state in {@link Revisions} is changed from outside the scheduler. If a
 * race condition happens at that point, data from a single commit will be
 * lost.
 */
@Test
public void testSimulatedRaceOnRevisions() throws Exception {
    final MemoryStore ms = new MemoryStore();
    StatisticsProvider statsProvider = StatisticsProvider.NOOP;
    SegmentNodeStoreStats stats = new SegmentNodeStoreStats(statsProvider);
    final LockBasedScheduler scheduler = LockBasedScheduler.builder(ms.getRevisions(), ms.getReader(), stats).build();
    final RecordId initialHead = ms.getRevisions().getHead();
    ExecutorService executorService = newFixedThreadPool(10);
    final AtomicInteger count = new AtomicInteger();
    final Random rand = new Random();
    try {
        Callable<PropertyState> commitTask = new Callable<PropertyState>() {

            @Override
            public PropertyState call() throws Exception {
                String property = "prop" + count.incrementAndGet();
                Commit commit = createCommit(scheduler, property, "value");
                SegmentNodeState result = (SegmentNodeState) scheduler.schedule(commit);
                return result.getProperty(property);
            }
        };
        Callable<Void> parallelTask = new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                Thread.sleep(rand.nextInt(10));
                ms.getRevisions().setHead(ms.getRevisions().getHead(), initialHead);
                return null;
            }
        };
        List<Future<?>> results = newArrayList();
        for (int i = 0; i < 100; i++) {
            results.add(executorService.submit(commitTask));
            executorService.submit(parallelTask);
        }
        for (Future<?> result : results) {
            assertNotNull("PropertyState must not be null! The corresponding commit got lost because of a race condition.", result.get());
        }
    } finally {
        new ExecutorCloser(executorService).close();
    }
}
Also used : SegmentNodeStoreStats(org.apache.jackrabbit.oak.segment.SegmentNodeStoreStats) SegmentNodeState(org.apache.jackrabbit.oak.segment.SegmentNodeState) StatisticsProvider(org.apache.jackrabbit.oak.stats.StatisticsProvider) Callable(java.util.concurrent.Callable) PropertyState(org.apache.jackrabbit.oak.api.PropertyState) MemoryStore(org.apache.jackrabbit.oak.segment.memory.MemoryStore) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) RecordId(org.apache.jackrabbit.oak.segment.RecordId) ExecutorCloser(org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser) Test(org.junit.Test)

Example 14 with RecordId

use of org.apache.jackrabbit.oak.segment.RecordId in project jackrabbit-oak by apache.

the class CheckRepositoryTestBase method corruptPathFromCheckpoint.

protected void corruptPathFromCheckpoint() throws InvalidFileStoreVersionException, IOException {
    FileStore fileStore = FileStoreBuilder.fileStoreBuilder(temporaryFolder.getRoot()).withMaxFileSize(256).withSegmentCacheSize(64).build();
    SegmentNodeStore nodeStore = SegmentNodeStoreBuilders.builder(fileStore).build();
    SegmentNodeState cp1 = (SegmentNodeState) nodeStore.retrieve(checkpoints.iterator().next());
    RecordId bRecordId = ((SegmentNodeState) cp1.getChildNode("b")).getRecordId();
    fileStore.close();
    corruptRecord(bRecordId, "data00000a.tar");
}
Also used : FileStore(org.apache.jackrabbit.oak.segment.file.FileStore) RecordId(org.apache.jackrabbit.oak.segment.RecordId) SegmentNodeStore(org.apache.jackrabbit.oak.segment.SegmentNodeStore) SegmentNodeState(org.apache.jackrabbit.oak.segment.SegmentNodeState)

Example 15 with RecordId

use of org.apache.jackrabbit.oak.segment.RecordId in project jackrabbit-oak by apache.

the class DefaultStandbyReferenceReaderTest method shouldReturnEmptyReferences.

@Test
public void shouldReturnEmptyReferences() throws Exception {
    try (FileStore store = newFileStore()) {
        SegmentWriter writer = defaultSegmentWriterBuilder("test").build(store);
        RecordId id = writer.writeNode(EmptyNodeState.EMPTY_NODE);
        writer.flush();
        DefaultStandbyReferencesReader reader = new DefaultStandbyReferencesReader(store);
        Iterable<String> references = reader.readReferences(id.getSegmentId().asUUID().toString());
        assertFalse(references.iterator().hasNext());
    }
}
Also used : FileStore(org.apache.jackrabbit.oak.segment.file.FileStore) RecordId(org.apache.jackrabbit.oak.segment.RecordId) SegmentWriter(org.apache.jackrabbit.oak.segment.SegmentWriter) Test(org.junit.Test)

Aggregations

RecordId (org.apache.jackrabbit.oak.segment.RecordId)28 SegmentNodeState (org.apache.jackrabbit.oak.segment.SegmentNodeState)14 Test (org.junit.Test)9 FileStoreUtil.findPersistedRecordId (org.apache.jackrabbit.oak.segment.file.FileStoreUtil.findPersistedRecordId)7 FileStore (org.apache.jackrabbit.oak.segment.file.FileStore)4 SegmentNodeBuilder (org.apache.jackrabbit.oak.segment.SegmentNodeBuilder)3 SegmentWriter (org.apache.jackrabbit.oak.segment.SegmentWriter)3 NodeState (org.apache.jackrabbit.oak.spi.state.NodeState)3 IOException (java.io.IOException)2 UUID (java.util.UUID)2 PropertyState (org.apache.jackrabbit.oak.api.PropertyState)2 RecordId.fromString (org.apache.jackrabbit.oak.segment.RecordId.fromString)2 SegmentId (org.apache.jackrabbit.oak.segment.SegmentId)2 SegmentNodeStore (org.apache.jackrabbit.oak.segment.SegmentNodeStore)2 NodeBuilder (org.apache.jackrabbit.oak.spi.state.NodeBuilder)2 Function (com.google.common.base.Function)1 Stopwatch (com.google.common.base.Stopwatch)1 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)1 PrintWriter (java.io.PrintWriter)1 Random (java.util.Random)1