Search in sources :

Example 76 with StreamInput

use of org.opensearch.common.io.stream.StreamInput in project OpenSearch by opensearch-project.

the class StreamsTests method testBytesStreamInput.

public void testBytesStreamInput() throws IOException {
    byte[] stuff = new byte[] { 0, 1, 2, 3 };
    BytesRef stuffRef = new BytesRef(stuff, 2, 2);
    BytesArray stuffArray = new BytesArray(stuffRef);
    StreamInput input = stuffArray.streamInput();
    assertEquals(2, input.read());
    assertEquals(3, input.read());
    assertEquals(-1, input.read());
    input.close();
}
Also used : BytesArray(org.opensearch.common.bytes.BytesArray) StreamInput(org.opensearch.common.io.stream.StreamInput) BytesRef(org.apache.lucene.util.BytesRef)

Example 77 with StreamInput

use of org.opensearch.common.io.stream.StreamInput in project OpenSearch by opensearch-project.

the class ShapeRelationTests method testInvalidReadFrom.

public void testInvalidReadFrom() throws Exception {
    try (BytesStreamOutput out = new BytesStreamOutput()) {
        out.writeVInt(randomIntBetween(3, Integer.MAX_VALUE));
        try (StreamInput in = out.bytes().streamInput()) {
            ShapeRelation.readFromStream(in);
            fail("Expected IOException");
        } catch (IOException e) {
            assertThat(e.getMessage(), containsString("Unknown ShapeRelation ordinal ["));
        }
    }
}
Also used : StreamInput(org.opensearch.common.io.stream.StreamInput) IOException(java.io.IOException) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Example 78 with StreamInput

use of org.opensearch.common.io.stream.StreamInput in project OpenSearch by opensearch-project.

the class RecoveryTargetTests method testConcurrentModificationIndexFileDetailsMap.

public void testConcurrentModificationIndexFileDetailsMap() throws InterruptedException {
    final Index index = new Index();
    final AtomicBoolean stop = new AtomicBoolean(false);
    Streamer<Index> readWriteIndex = new Streamer<Index>(stop, index) {

        @Override
        Index createObj(StreamInput in) throws IOException {
            return new Index(in);
        }
    };
    Thread modifyThread = new Thread() {

        @Override
        public void run() {
            for (int i = 0; i < 1000; i++) {
                index.addFileDetail(randomAlphaOfLength(10), 100, true);
            }
            stop.set(true);
        }
    };
    readWriteIndex.start();
    modifyThread.start();
    modifyThread.join();
    readWriteIndex.join();
    assertThat(readWriteIndex.error.get(), equalTo(null));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) StreamInput(org.opensearch.common.io.stream.StreamInput) Index(org.opensearch.indices.recovery.RecoveryState.Index) VerifyIndex(org.opensearch.indices.recovery.RecoveryState.VerifyIndex)

Example 79 with StreamInput

use of org.opensearch.common.io.stream.StreamInput in project OpenSearch by opensearch-project.

the class Store method failIfCorrupted.

private static void failIfCorrupted(Directory directory) throws IOException {
    final String[] files = directory.listAll();
    List<CorruptIndexException> ex = new ArrayList<>();
    for (String file : files) {
        if (file.startsWith(CORRUPTED_MARKER_NAME_PREFIX)) {
            try (ChecksumIndexInput input = directory.openChecksumInput(file, IOContext.READONCE)) {
                CodecUtil.checkHeader(input, CODEC, CORRUPTED_MARKER_CODEC_VERSION, CORRUPTED_MARKER_CODEC_VERSION);
                final int size = input.readVInt();
                final byte[] buffer = new byte[size];
                input.readBytes(buffer, 0, buffer.length);
                StreamInput in = StreamInput.wrap(buffer);
                Exception t = in.readException();
                if (t instanceof CorruptIndexException) {
                    ex.add((CorruptIndexException) t);
                } else {
                    ex.add(new CorruptIndexException(t.getMessage(), "preexisting_corruption", t));
                }
                CodecUtil.checkFooter(input);
            }
        }
    }
    if (ex.isEmpty() == false) {
        ExceptionsHelper.rethrowAndSuppress(ex);
    }
}
Also used : ChecksumIndexInput(org.apache.lucene.store.ChecksumIndexInput) ArrayList(java.util.ArrayList) StreamInput(org.opensearch.common.io.stream.StreamInput) BytesStreamInput(org.opensearch.common.io.stream.BytesStreamInput) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) IndexNotFoundException(org.apache.lucene.index.IndexNotFoundException) NoSuchFileException(java.nio.file.NoSuchFileException) IndexFormatTooNewException(org.apache.lucene.index.IndexFormatTooNewException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) UncheckedIOException(java.io.UncheckedIOException) ShardLockObtainFailedException(org.opensearch.env.ShardLockObtainFailedException) IOException(java.io.IOException) IndexFormatTooOldException(org.apache.lucene.index.IndexFormatTooOldException)

Example 80 with StreamInput

use of org.opensearch.common.io.stream.StreamInput in project OpenSearch by opensearch-project.

the class WriteableIngestDocumentTests method testSerialization.

public void testSerialization() throws IOException {
    Map<String, Object> sourceAndMetadata = RandomDocumentPicks.randomSource(random());
    int numFields = randomIntBetween(1, IngestDocument.Metadata.values().length);
    for (int i = 0; i < numFields; i++) {
        sourceAndMetadata.put(randomFrom(IngestDocument.Metadata.values()).getFieldName(), randomAlphaOfLengthBetween(5, 10));
    }
    Map<String, Object> ingestMetadata = new HashMap<>();
    numFields = randomIntBetween(1, 5);
    for (int i = 0; i < numFields; i++) {
        ingestMetadata.put(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 10));
    }
    WriteableIngestDocument writeableIngestDocument = new WriteableIngestDocument(new IngestDocument(sourceAndMetadata, ingestMetadata));
    BytesStreamOutput out = new BytesStreamOutput();
    writeableIngestDocument.writeTo(out);
    StreamInput streamInput = out.bytes().streamInput();
    WriteableIngestDocument otherWriteableIngestDocument = new WriteableIngestDocument(streamInput);
    assertIngestDocument(otherWriteableIngestDocument.getIngestDocument(), writeableIngestDocument.getIngestDocument());
}
Also used : HashMap(java.util.HashMap) StreamInput(org.opensearch.common.io.stream.StreamInput) IngestDocumentMatcher.assertIngestDocument(org.opensearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.opensearch.ingest.IngestDocument) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Aggregations

StreamInput (org.opensearch.common.io.stream.StreamInput)206 BytesStreamOutput (org.opensearch.common.io.stream.BytesStreamOutput)139 IOException (java.io.IOException)42 NamedWriteableAwareStreamInput (org.opensearch.common.io.stream.NamedWriteableAwareStreamInput)40 Version (org.opensearch.Version)27 OpenSearchException (org.opensearch.OpenSearchException)21 InputStreamStreamInput (org.opensearch.common.io.stream.InputStreamStreamInput)19 BytesReference (org.opensearch.common.bytes.BytesReference)18 Matchers.containsString (org.hamcrest.Matchers.containsString)17 Matchers.hasToString (org.hamcrest.Matchers.hasToString)17 NamedWriteableRegistry (org.opensearch.common.io.stream.NamedWriteableRegistry)17 UncheckedIOException (java.io.UncheckedIOException)15 CountDownLatch (java.util.concurrent.CountDownLatch)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)13 ArrayList (java.util.ArrayList)12 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 UnknownHostException (java.net.UnknownHostException)11 ExecutionException (java.util.concurrent.ExecutionException)11