Search in sources :

Example 1 with Assertions

use of org.opensearch.Assertions in project OpenSearch by opensearch-project.

the class TranslogTests method testTranslogCloseInvariant.

// close method should never be called directly from Translog (the only exception is closeOnTragicEvent)
public void testTranslogCloseInvariant() throws IOException {
    assumeTrue("test only works with assertions enabled", Assertions.ENABLED);
    class MisbehavingTranslog extends Translog {

        MisbehavingTranslog(TranslogConfig config, String translogUUID, TranslogDeletionPolicy deletionPolicy, LongSupplier globalCheckpointSupplier, LongSupplier primaryTermSupplier) throws IOException {
            super(config, translogUUID, deletionPolicy, globalCheckpointSupplier, primaryTermSupplier, seqNo -> {
            });
        }

        void callCloseDirectly() throws IOException {
            close();
        }

        void callCloseUsingIOUtilsWithExceptionHandling() {
            IOUtils.closeWhileHandlingException(this);
        }

        void callCloseUsingIOUtils() throws IOException {
            IOUtils.close(this);
        }

        void callCloseOnTragicEvent() {
            Exception e = new Exception("test tragic exception");
            tragedy.setTragicException(e);
            closeOnTragicEvent(e);
        }
    }
    globalCheckpoint = new AtomicLong(SequenceNumbers.NO_OPS_PERFORMED);
    Path path = createTempDir();
    final TranslogConfig translogConfig = getTranslogConfig(path);
    final TranslogDeletionPolicy deletionPolicy = createTranslogDeletionPolicy(translogConfig.getIndexSettings());
    final String translogUUID = Translog.createEmptyTranslog(path, SequenceNumbers.NO_OPS_PERFORMED, shardId, primaryTerm.get());
    MisbehavingTranslog misbehavingTranslog = new MisbehavingTranslog(translogConfig, translogUUID, deletionPolicy, () -> globalCheckpoint.get(), primaryTerm::get);
    expectThrows(AssertionError.class, () -> misbehavingTranslog.callCloseDirectly());
    expectThrows(AssertionError.class, () -> misbehavingTranslog.callCloseUsingIOUtils());
    expectThrows(AssertionError.class, () -> misbehavingTranslog.callCloseUsingIOUtilsWithExceptionHandling());
    misbehavingTranslog.callCloseOnTragicEvent();
}
Also used : Matchers.hasToString(org.hamcrest.Matchers.hasToString) SequenceNumbers(org.opensearch.index.seqno.SequenceNumbers) Arrays(java.util.Arrays) LongSupplier(java.util.function.LongSupplier) Term(org.apache.lucene.index.Term) Matchers.not(org.hamcrest.Matchers.not) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) Version(org.opensearch.Version) FileSystemUtils(org.opensearch.common.io.FileSystemUtils) Document(org.opensearch.index.mapper.ParseContext.Document) Strings(org.opensearch.common.Strings) CodecUtil(org.apache.lucene.codecs.CodecUtil) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Path(java.nio.file.Path) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) FilterFileSystemProvider(org.apache.lucene.mockfile.FilterFileSystemProvider) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) LocalCheckpointTrackerTests(org.opensearch.index.seqno.LocalCheckpointTrackerTests) BlockingQueue(java.util.concurrent.BlockingQueue) StandardCharsets(java.nio.charset.StandardCharsets) Engine(org.opensearch.index.engine.Engine) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) VersionType(org.opensearch.index.VersionType) Stream(java.util.stream.Stream) Matchers.contains(org.hamcrest.Matchers.contains) Randomness(org.opensearch.common.Randomness) BytesArray(org.opensearch.common.bytes.BytesArray) LocalCheckpointTracker(org.opensearch.index.seqno.LocalCheckpointTracker) XContentType(org.opensearch.common.xcontent.XContentType) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) Matchers.containsString(org.hamcrest.Matchers.containsString) Matchers.endsWith(org.hamcrest.Matchers.endsWith) Matchers.in(org.hamcrest.Matchers.in) CopyOption(java.nio.file.CopyOption) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Mockito.mock(org.mockito.Mockito.mock) RandomPicks(com.carrotsearch.randomizedtesting.generators.RandomPicks) TranslogDeletionPolicies.createTranslogDeletionPolicy(org.opensearch.index.translog.TranslogDeletionPolicies.createTranslogDeletionPolicy) Origin(org.opensearch.index.engine.Engine.Operation.Origin) ArrayList(java.util.ArrayList) InvalidPathException(java.nio.file.InvalidPathException) VersionUtils(org.opensearch.test.VersionUtils) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) Before(org.junit.Before) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) LongStream(java.util.stream.LongStream) Files(java.nio.file.Files) MissingHistoryOperationsException(org.opensearch.index.engine.MissingHistoryOperationsException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) AtomicLong(java.util.concurrent.atomic.AtomicLong) Phaser(java.util.concurrent.Phaser) TextField(org.apache.lucene.document.TextField) ArrayDeque(java.util.ArrayDeque) SeqNoFieldMapper(org.opensearch.index.mapper.SeqNoFieldMapper) IdFieldMapper(org.opensearch.index.mapper.IdFieldMapper) CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) AbstractRunnable(org.opensearch.common.util.concurrent.AbstractRunnable) ToXContent(org.opensearch.common.xcontent.ToXContent) ByteSizeUnit(org.opensearch.common.unit.ByteSizeUnit) Random(java.util.Random) ByteBuffer(java.nio.ByteBuffer) NON_RECYCLING_INSTANCE(org.opensearch.common.util.BigArrays.NON_RECYCLING_INSTANCE) ConcurrentCollections(org.opensearch.common.util.concurrent.ConcurrentCollections) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions(org.opensearch.Assertions) After(org.junit.After) XContentFactory(org.opensearch.common.xcontent.XContentFactory) CyclicBarrier(java.util.concurrent.CyclicBarrier) IdentityHashMap(java.util.IdentityHashMap) Matchers.lessThanOrEqualTo(org.hamcrest.Matchers.lessThanOrEqualTo) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Collection(java.util.Collection) StandardOpenOption(java.nio.file.StandardOpenOption) EOFException(java.io.EOFException) Collectors(java.util.stream.Collectors) Tuple(org.opensearch.common.collect.Tuple) Location(org.opensearch.index.translog.Translog.Location) List(java.util.List) Matchers.equalTo(org.hamcrest.Matchers.equalTo) IndexSettings(org.opensearch.index.IndexSettings) LuceneTestCase(org.apache.lucene.util.LuceneTestCase) Queue(java.util.Queue) MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) Uid(org.opensearch.index.mapper.Uid) IndexSettingsModule(org.opensearch.test.IndexSettingsModule) IntStream(java.util.stream.IntStream) BytesReference(org.opensearch.common.bytes.BytesReference) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ByteSizeValue(org.opensearch.common.unit.ByteSizeValue) HashMap(java.util.HashMap) ReleasableLock(org.opensearch.common.util.concurrent.ReleasableLock) Deque(java.util.Deque) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) HashSet(java.util.HashSet) Charset(java.nio.charset.Charset) SnapshotMatchers.containsOperationsInAnyOrder(org.opensearch.index.translog.SnapshotMatchers.containsOperationsInAnyOrder) FilterFileChannel(org.apache.lucene.mockfile.FilterFileChannel) LineFileDocs(org.apache.lucene.util.LineFileDocs) UUIDs(org.opensearch.common.UUIDs) LinkedList(java.util.LinkedList) StreamInput(org.opensearch.common.io.stream.StreamInput) Matchers.empty(org.hamcrest.Matchers.empty) Iterator(java.util.Iterator) Matchers(org.hamcrest.Matchers) Mockito.when(org.mockito.Mockito.when) IndexFormatTooOldException(org.apache.lucene.index.IndexFormatTooOldException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) LongConsumer(java.util.function.LongConsumer) IOUtils(org.opensearch.core.internal.io.IOUtils) ShardId(org.opensearch.index.shard.ShardId) Field(org.apache.lucene.document.Field) Closeable(java.io.Closeable) Comparator(java.util.Comparator) FileChannel(java.nio.channels.FileChannel) Collections(java.util.Collections) Path(java.nio.file.Path) AtomicLong(java.util.concurrent.atomic.AtomicLong) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) LongSupplier(java.util.function.LongSupplier) TranslogDeletionPolicies.createTranslogDeletionPolicy(org.opensearch.index.translog.TranslogDeletionPolicies.createTranslogDeletionPolicy) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) InvalidPathException(java.nio.file.InvalidPathException) MissingHistoryOperationsException(org.opensearch.index.engine.MissingHistoryOperationsException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) EOFException(java.io.EOFException) IndexFormatTooOldException(org.apache.lucene.index.IndexFormatTooOldException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException)

Aggregations

RandomPicks (com.carrotsearch.randomizedtesting.generators.RandomPicks)1 Closeable (java.io.Closeable)1 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 FileChannel (java.nio.channels.FileChannel)1 Charset (java.nio.charset.Charset)1 StandardCharsets (java.nio.charset.StandardCharsets)1 CopyOption (java.nio.file.CopyOption)1 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)1 Files (java.nio.file.Files)1 InvalidPathException (java.nio.file.InvalidPathException)1 Path (java.nio.file.Path)1 StandardOpenOption (java.nio.file.StandardOpenOption)1 ArrayDeque (java.util.ArrayDeque)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1