Search in sources :

Example 16 with StreamOutput

use of org.elasticsearch.common.io.stream.StreamOutput in project elasticsearch by elastic.

the class PublishClusterStateActionTests method testSerializationFailureDuringDiffPublishing.

public void testSerializationFailureDuringDiffPublishing() throws Exception {
    MockNode nodeA = createMockNode("nodeA", Settings.EMPTY, new ClusterStateListener() {

        @Override
        public void clusterChanged(ClusterChangedEvent event) {
            fail("Shouldn't send cluster state to myself");
        }
    }).setAsMaster();
    MockNode nodeB = createMockNode("nodeB");
    // Initial cluster state with both states - the second node still shouldn't get
    // diff even though it's present in the previous cluster state
    DiscoveryNodes discoveryNodes = DiscoveryNodes.builder(nodeA.nodes()).add(nodeB.discoveryNode).build();
    ClusterState previousClusterState = ClusterState.builder(CLUSTER_NAME).nodes(discoveryNodes).build();
    ClusterState clusterState = ClusterState.builder(previousClusterState).incrementVersion().build();
    publishStateAndWait(nodeA.action, clusterState, previousClusterState);
    assertSameStateFromFull(nodeB.clusterState, clusterState);
    // cluster state update - add block
    previousClusterState = clusterState;
    clusterState = ClusterState.builder(clusterState).blocks(ClusterBlocks.builder().addGlobalBlock(MetaData.CLUSTER_READ_ONLY_BLOCK)).incrementVersion().build();
    ClusterState unserializableClusterState = new ClusterState(clusterState.version(), clusterState.stateUUID(), clusterState) {

        @Override
        public Diff<ClusterState> diff(ClusterState previousState) {
            return new Diff<ClusterState>() {

                @Override
                public ClusterState apply(ClusterState part) {
                    fail("this diff shouldn't be applied");
                    return part;
                }

                @Override
                public void writeTo(StreamOutput out) throws IOException {
                    throw new IOException("Simulated failure of diff serialization");
                }
            };
        }
    };
    try {
        publishStateAndWait(nodeA.action, unserializableClusterState, previousClusterState);
        fail("cluster state published despite of diff errors");
    } catch (Discovery.FailedToCommitClusterStateException e) {
        assertThat(e.getCause(), notNullValue());
        assertThat(e.getCause().getMessage(), containsString("failed to serialize"));
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) Diff(org.elasticsearch.cluster.Diff) Discovery(org.elasticsearch.discovery.Discovery) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) IOException(java.io.IOException) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) ClusterStateListener(org.elasticsearch.cluster.ClusterStateListener)

Example 17 with StreamOutput

use of org.elasticsearch.common.io.stream.StreamOutput in project elasticsearch by elastic.

the class PublishClusterStateAction method serializeDiffClusterState.

public static BytesReference serializeDiffClusterState(Diff diff, Version nodeVersion) throws IOException {
    BytesStreamOutput bStream = new BytesStreamOutput();
    try (StreamOutput stream = CompressorFactory.COMPRESSOR.streamOutput(bStream)) {
        stream.setVersion(nodeVersion);
        stream.writeBoolean(false);
        diff.writeTo(stream);
    }
    return bStream.bytes();
}
Also used : StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 18 with StreamOutput

use of org.elasticsearch.common.io.stream.StreamOutput in project crate by crate.

the class NodeStatsContextTest method testStreamEmptyContext.

@Test
public void testStreamEmptyContext() throws Exception {
    NodeStatsContext ctx1 = new NodeStatsContext(false);
    ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
    StreamOutput out = new OutputStreamStreamOutput(outBuffer);
    ctx1.writeTo(out);
    ByteArrayInputStream inBuffer = new ByteArrayInputStream(outBuffer.toByteArray());
    InputStreamStreamInput in = new InputStreamStreamInput(inBuffer);
    NodeStatsContext ctx2 = new NodeStatsContext(false);
    ctx2.readFrom(in);
    assertNull(ctx2.id());
    assertNull(ctx2.name());
    assertNull(ctx2.hostname());
    assertNull(ctx2.restUrl());
    assertNull(ctx2.port());
    assertNull(ctx2.jvmStats());
    assertNull(ctx2.osInfo());
    assertNull(ctx2.processStats());
    assertNull(ctx2.osStats());
    assertNull(ctx2.extendedOsStats());
    assertNull(ctx2.networkStats());
    assertNull(ctx2.extendedProcessCpuStats());
    assertNull(ctx2.extendedFsStats());
    assertNull(ctx2.threadPools());
}
Also used : OutputStreamStreamOutput(org.elasticsearch.common.io.stream.OutputStreamStreamOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) OutputStreamStreamOutput(org.elasticsearch.common.io.stream.OutputStreamStreamOutput) InputStreamStreamInput(org.elasticsearch.common.io.stream.InputStreamStreamInput) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Aggregations

StreamOutput (org.elasticsearch.common.io.stream.StreamOutput)18 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)8 OutputStreamStreamOutput (org.elasticsearch.common.io.stream.OutputStreamStreamOutput)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 CrateUnitTest (io.crate.test.integration.CrateUnitTest)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStreamStreamInput (org.elasticsearch.common.io.stream.InputStreamStreamInput)6 Test (org.junit.Test)6 BytesReference (org.elasticsearch.common.bytes.BytesReference)5 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)4 IOException (java.io.IOException)3 StreamInput (org.elasticsearch.common.io.stream.StreamInput)3 ThreadPools (io.crate.monitor.ThreadPools)2 CompositeBytesReference (org.elasticsearch.common.bytes.CompositeBytesReference)2 ReleasableBytesStreamOutput (org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput)2 Releasable (org.elasticsearch.common.lease.Releasable)2 AbstractLifecycleRunnable (org.elasticsearch.common.util.concurrent.AbstractLifecycleRunnable)2 AbstractRunnable (org.elasticsearch.common.util.concurrent.AbstractRunnable)2 DataOutputStream (java.io.DataOutputStream)1 ByteBuffer (java.nio.ByteBuffer)1