Search in sources :

Example 6 with InputStreamStreamInput

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

the class InboundHandlerTests method unreadableInboundHandshake.

private static InboundMessage unreadableInboundHandshake(Version remoteVersion, Header requestHeader) {
    return new InboundMessage(requestHeader, ReleasableBytesReference.wrap(BytesArray.EMPTY), () -> {
    }) {

        @Override
        public StreamInput openOrGetStreamInput() {
            final StreamInput streamInput = new InputStreamStreamInput(new InputStream() {

                @Override
                public int read() {
                    throw new OpenSearchException("unreadable handshake");
                }
            });
            streamInput.setVersion(remoteVersion);
            return streamInput;
        }
    };
}
Also used : InputStream(java.io.InputStream) InputStreamStreamInput(org.opensearch.common.io.stream.InputStreamStreamInput) StreamInput(org.opensearch.common.io.stream.StreamInput) OpenSearchException(org.opensearch.OpenSearchException) InputStreamStreamInput(org.opensearch.common.io.stream.InputStreamStreamInput)

Example 7 with InputStreamStreamInput

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

the class StoreTests method testMetadataSnapshotStreaming.

public void testMetadataSnapshotStreaming() throws Exception {
    Store.MetadataSnapshot outMetadataSnapshot = createMetadataSnapshot();
    org.opensearch.Version targetNodeVersion = randomVersion(random());
    ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
    OutputStreamStreamOutput out = new OutputStreamStreamOutput(outBuffer);
    out.setVersion(targetNodeVersion);
    outMetadataSnapshot.writeTo(out);
    ByteArrayInputStream inBuffer = new ByteArrayInputStream(outBuffer.toByteArray());
    InputStreamStreamInput in = new InputStreamStreamInput(inBuffer);
    in.setVersion(targetNodeVersion);
    Store.MetadataSnapshot inMetadataSnapshot = new Store.MetadataSnapshot(in);
    Map<String, StoreFileMetadata> origEntries = new HashMap<>();
    origEntries.putAll(outMetadataSnapshot.asMap());
    for (Map.Entry<String, StoreFileMetadata> entry : inMetadataSnapshot.asMap().entrySet()) {
        assertThat(entry.getValue().name(), equalTo(origEntries.remove(entry.getKey()).name()));
    }
    assertThat(origEntries.size(), equalTo(0));
    assertThat(inMetadataSnapshot.getCommitUserData(), equalTo(outMetadataSnapshot.getCommitUserData()));
}
Also used : HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) OutputStreamStreamOutput(org.opensearch.common.io.stream.OutputStreamStreamOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) HashMap(java.util.HashMap) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) InputStreamStreamInput(org.opensearch.common.io.stream.InputStreamStreamInput)

Example 8 with InputStreamStreamInput

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

the class StoreTests method testStreamStoreFilesMetadata.

public void testStreamStoreFilesMetadata() throws Exception {
    Store.MetadataSnapshot metadataSnapshot = createMetadataSnapshot();
    int numOfLeases = randomIntBetween(0, 10);
    List<RetentionLease> peerRecoveryRetentionLeases = new ArrayList<>();
    for (int i = 0; i < numOfLeases; i++) {
        peerRecoveryRetentionLeases.add(new RetentionLease(ReplicationTracker.getPeerRecoveryRetentionLeaseId(UUIDs.randomBase64UUID()), randomNonNegativeLong(), randomNonNegativeLong(), ReplicationTracker.PEER_RECOVERY_RETENTION_LEASE_SOURCE));
    }
    TransportNodesListShardStoreMetadata.StoreFilesMetadata outStoreFileMetadata = new TransportNodesListShardStoreMetadata.StoreFilesMetadata(new ShardId("test", "_na_", 0), metadataSnapshot, peerRecoveryRetentionLeases);
    ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
    OutputStreamStreamOutput out = new OutputStreamStreamOutput(outBuffer);
    org.opensearch.Version targetNodeVersion = randomVersion(random());
    out.setVersion(targetNodeVersion);
    outStoreFileMetadata.writeTo(out);
    ByteArrayInputStream inBuffer = new ByteArrayInputStream(outBuffer.toByteArray());
    InputStreamStreamInput in = new InputStreamStreamInput(inBuffer);
    in.setVersion(targetNodeVersion);
    TransportNodesListShardStoreMetadata.StoreFilesMetadata inStoreFileMetadata = new TransportNodesListShardStoreMetadata.StoreFilesMetadata(in);
    Iterator<StoreFileMetadata> outFiles = outStoreFileMetadata.iterator();
    for (StoreFileMetadata inFile : inStoreFileMetadata) {
        assertThat(inFile.name(), equalTo(outFiles.next().name()));
    }
    assertThat(outStoreFileMetadata.syncId(), equalTo(inStoreFileMetadata.syncId()));
    assertThat(outStoreFileMetadata.peerRecoveryRetentionLeases(), equalTo(peerRecoveryRetentionLeases));
}
Also used : ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ShardId(org.opensearch.index.shard.ShardId) OutputStreamStreamOutput(org.opensearch.common.io.stream.OutputStreamStreamOutput) RetentionLease(org.opensearch.index.seqno.RetentionLease) ByteArrayInputStream(java.io.ByteArrayInputStream) TransportNodesListShardStoreMetadata(org.opensearch.indices.store.TransportNodesListShardStoreMetadata) InputStreamStreamInput(org.opensearch.common.io.stream.InputStreamStreamInput)

Example 9 with InputStreamStreamInput

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

the class TestTranslog method isTranslogHeaderVersionFlipped.

/**
 * An old translog header does not have a checksum. If we flip the header version of an empty translog from 3 to 2,
 * then we won't detect that corruption, and the translog will be considered clean as before.
 */
static boolean isTranslogHeaderVersionFlipped(Path corruptedFile, FileChannel channel) throws IOException {
    if (corruptedFile.toString().endsWith(TRANSLOG_FILE_SUFFIX) == false) {
        return false;
    }
    channel.position(0);
    final InputStreamStreamInput in = new InputStreamStreamInput(Channels.newInputStream(channel), channel.size());
    try {
        final int version = TranslogHeader.readHeaderVersion(corruptedFile, channel, in);
        return version == TranslogHeader.VERSION_CHECKPOINTS;
    } catch (IllegalStateException | TranslogCorruptedException | IOException e) {
        return false;
    }
}
Also used : IOException(java.io.IOException) InputStreamStreamInput(org.opensearch.common.io.stream.InputStreamStreamInput)

Example 10 with InputStreamStreamInput

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

the class TermVectorsUnitTests method testStreamRequest.

public void testStreamRequest() throws IOException {
    for (int i = 0; i < 10; i++) {
        TermVectorsRequest request = new TermVectorsRequest("index", "id");
        request.offsets(random().nextBoolean());
        request.fieldStatistics(random().nextBoolean());
        request.payloads(random().nextBoolean());
        request.positions(random().nextBoolean());
        request.termStatistics(random().nextBoolean());
        String pref = random().nextBoolean() ? "somePreference" : null;
        request.preference(pref);
        request.doc(new BytesArray("{}"), randomBoolean(), XContentType.JSON);
        // write
        ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
        OutputStreamStreamOutput out = new OutputStreamStreamOutput(outBuffer);
        request.writeTo(out);
        // read
        ByteArrayInputStream opensearchInBuffer = new ByteArrayInputStream(outBuffer.toByteArray());
        InputStreamStreamInput opensearchBuffer = new InputStreamStreamInput(opensearchInBuffer);
        TermVectorsRequest req2 = new TermVectorsRequest(opensearchBuffer);
        assertThat(request.offsets(), equalTo(req2.offsets()));
        assertThat(request.fieldStatistics(), equalTo(req2.fieldStatistics()));
        assertThat(request.payloads(), equalTo(req2.payloads()));
        assertThat(request.positions(), equalTo(req2.positions()));
        assertThat(request.termStatistics(), equalTo(req2.termStatistics()));
        assertThat(request.preference(), equalTo(pref));
        assertThat(request.routing(), equalTo(null));
        assertEquals(new BytesArray("{}"), request.doc());
        assertEquals(XContentType.JSON, request.xContentType());
    }
}
Also used : BytesArray(org.opensearch.common.bytes.BytesArray) OutputStreamStreamOutput(org.opensearch.common.io.stream.OutputStreamStreamOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InputStreamStreamInput(org.opensearch.common.io.stream.InputStreamStreamInput)

Aggregations

InputStreamStreamInput (org.opensearch.common.io.stream.InputStreamStreamInput)25 ByteArrayInputStream (java.io.ByteArrayInputStream)18 StreamInput (org.opensearch.common.io.stream.StreamInput)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 OutputStreamStreamOutput (org.opensearch.common.io.stream.OutputStreamStreamOutput)9 NamedWriteableAwareStreamInput (org.opensearch.common.io.stream.NamedWriteableAwareStreamInput)7 InputStream (java.io.InputStream)5 XContentParser (org.opensearch.common.xcontent.XContentParser)5 IOException (java.io.IOException)4 ByteArrayStreamOutput (org.codelibs.fesen.client.io.stream.ByteArrayStreamOutput)4 QueryBuilder (org.opensearch.index.query.QueryBuilder)4 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 BytesRef (org.apache.lucene.util.BytesRef)3 OpenSearchException (org.opensearch.OpenSearchException)3 BytesArray (org.opensearch.common.bytes.BytesArray)3 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)3 EOFException (java.io.EOFException)2 HashMap (java.util.HashMap)2 List (java.util.List)2