Search in sources :

Example 6 with ObjectOutputStream

use of java.io.ObjectOutputStream in project flink by apache.

the class KvStateRequestSerializer method serializeServerFailure.

/**
	 * Allocates a buffer and serializes the server failure into it.
	 *
	 * <p>The cause must not be or contain any user types as causes.
	 *
	 * @param alloc ByteBuf allocator for the buffer to serialize message into
	 * @param cause Failure cause
	 * @return Serialized server failure message
	 * @throws IOException Serialization failures are forwarded
	 */
public static ByteBuf serializeServerFailure(ByteBufAllocator alloc, Throwable cause) throws IOException {
    ByteBuf buf = alloc.ioBuffer();
    // Frame length is set at end
    buf.writeInt(0);
    writeHeader(buf, KvStateRequestType.SERVER_FAILURE);
    try (ByteBufOutputStream bbos = new ByteBufOutputStream(buf);
        ObjectOutputStream out = new ObjectOutputStream(bbos)) {
        out.writeObject(cause);
    }
    // Set frame length
    int frameLength = buf.readableBytes() - 4;
    buf.setInt(0, frameLength);
    return buf;
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) ByteBuf(io.netty.buffer.ByteBuf) ObjectOutputStream(java.io.ObjectOutputStream)

Example 7 with ObjectOutputStream

use of java.io.ObjectOutputStream in project flink by apache.

the class ZooKeeperLeaderElectionTest method testMultipleLeaders.

/**
	 * Tests that the current leader is notified when his leader connection information in ZooKeeper
	 * are overwritten. The leader must re-establish the correct leader connection information in
	 * ZooKeeper.
	 */
@Test
public void testMultipleLeaders() throws Exception {
    final String FAULTY_CONTENDER_URL = "faultyContender";
    final String leaderPath = "/leader";
    Configuration configuration = new Configuration();
    configuration.setString(HighAvailabilityOptions.HA_ZOOKEEPER_QUORUM, testingServer.getConnectString());
    configuration.setString(HighAvailabilityOptions.HA_MODE, "zookeeper");
    configuration.setString(ConfigConstants.HA_ZOOKEEPER_LEADER_PATH, leaderPath);
    ZooKeeperLeaderElectionService leaderElectionService = null;
    ZooKeeperLeaderRetrievalService leaderRetrievalService = null;
    ZooKeeperLeaderRetrievalService leaderRetrievalService2 = null;
    TestingListener listener = new TestingListener();
    TestingListener listener2 = new TestingListener();
    TestingContender contender;
    try {
        leaderElectionService = ZooKeeperUtils.createLeaderElectionService(configuration);
        leaderRetrievalService = ZooKeeperUtils.createLeaderRetrievalService(configuration);
        leaderRetrievalService2 = ZooKeeperUtils.createLeaderRetrievalService(configuration);
        contender = new TestingContender(TEST_URL, leaderElectionService);
        leaderElectionService.start(contender);
        leaderRetrievalService.start(listener);
        listener.waitForNewLeader(timeout.toMillis());
        assertEquals(listener.getLeaderSessionID(), contender.getLeaderSessionID());
        assertEquals(TEST_URL, listener.getAddress());
        CuratorFramework client = ZooKeeperUtils.startCuratorFramework(configuration);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeUTF(FAULTY_CONTENDER_URL);
        oos.writeObject(null);
        oos.close();
        // overwrite the current leader address, the leader should notice that and correct it
        boolean dataWritten = false;
        while (!dataWritten) {
            client.delete().forPath(leaderPath);
            try {
                client.create().forPath(leaderPath, baos.toByteArray());
                dataWritten = true;
            } catch (KeeperException.NodeExistsException e) {
            // this can happen if the leader election service was faster
            }
        }
        leaderRetrievalService2.start(listener2);
        listener2.waitForNewLeader(timeout.toMillis());
        if (FAULTY_CONTENDER_URL.equals(listener2.getAddress())) {
            listener2.waitForNewLeader(timeout.toMillis());
        }
        assertEquals(listener2.getLeaderSessionID(), contender.getLeaderSessionID());
        assertEquals(listener2.getAddress(), contender.getAddress());
    } finally {
        if (leaderElectionService != null) {
            leaderElectionService.stop();
        }
        if (leaderRetrievalService != null) {
            leaderRetrievalService.stop();
        }
        if (leaderRetrievalService2 != null) {
            leaderRetrievalService2.stop();
        }
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) Configuration(org.apache.flink.configuration.Configuration) ZooKeeperLeaderRetrievalService(org.apache.flink.runtime.leaderretrieval.ZooKeeperLeaderRetrievalService) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) KeeperException(org.apache.zookeeper.KeeperException) Test(org.junit.Test)

Example 8 with ObjectOutputStream

use of java.io.ObjectOutputStream in project flink by apache.

the class SharedBufferTest method testSharedBufferSerialization.

@Test
public void testSharedBufferSerialization() throws IOException, ClassNotFoundException {
    SharedBuffer<String, Event> sharedBuffer = new SharedBuffer<>(Event.createTypeSerializer());
    int numberEvents = 8;
    Event[] events = new Event[numberEvents];
    final long timestamp = 1L;
    for (int i = 0; i < numberEvents; i++) {
        events[i] = new Event(i + 1, "e" + (i + 1), i);
    }
    sharedBuffer.put("a1", events[0], timestamp, null, null, 0, DeweyNumber.fromString("1"));
    sharedBuffer.put("a[]", events[1], timestamp, "a1", events[0], timestamp, DeweyNumber.fromString("1.0"));
    sharedBuffer.put("a1", events[2], timestamp, null, null, 0, DeweyNumber.fromString("2"));
    sharedBuffer.put("a[]", events[2], timestamp, "a[]", events[1], timestamp, DeweyNumber.fromString("1.0"));
    sharedBuffer.put("a[]", events[3], timestamp, "a[]", events[2], timestamp, DeweyNumber.fromString("1.0"));
    sharedBuffer.put("a[]", events[3], timestamp, "a1", events[2], timestamp, DeweyNumber.fromString("2.0"));
    sharedBuffer.put("a[]", events[4], timestamp, "a[]", events[3], timestamp, DeweyNumber.fromString("1.0"));
    sharedBuffer.put("a[]", events[5], timestamp, "a[]", events[4], timestamp, DeweyNumber.fromString("1.1"));
    sharedBuffer.put("b", events[5], timestamp, "a[]", events[3], timestamp, DeweyNumber.fromString("2.0.0"));
    sharedBuffer.put("b", events[5], timestamp, "a[]", events[4], timestamp, DeweyNumber.fromString("1.0.0"));
    sharedBuffer.put("a[]", events[6], timestamp, "a[]", events[5], timestamp, DeweyNumber.fromString("1.1"));
    sharedBuffer.put("b", events[7], timestamp, "a[]", events[6], timestamp, DeweyNumber.fromString("1.1.0"));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(sharedBuffer);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ObjectInputStream ois = new ObjectInputStream(bais);
    SharedBuffer<String, Event> copy = (SharedBuffer<String, Event>) ois.readObject();
    assertEquals(sharedBuffer, copy);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Event(org.apache.flink.cep.Event) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 9 with ObjectOutputStream

use of java.io.ObjectOutputStream in project flink by apache.

the class CollectionInputFormatTest method testSerializationFailure.

@Test
public void testSerializationFailure() {
    try (ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(buffer)) {
        // a mock serializer that fails when writing
        CollectionInputFormat<ElementType> inFormat = new CollectionInputFormat<ElementType>(Collections.singleton(new ElementType()), new TestSerializer(false, true));
        try {
            out.writeObject(inFormat);
            fail("should throw an exception");
        } catch (TestException e) {
        // expected
        } catch (Exception e) {
            fail("Exception not properly forwarded");
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 10 with ObjectOutputStream

use of java.io.ObjectOutputStream in project flink by apache.

the class CollectionInputFormatTest method testDeserializationFailure.

@Test
public void testDeserializationFailure() {
    try (ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(buffer)) {
        // a mock serializer that fails when writing
        CollectionInputFormat<ElementType> inFormat = new CollectionInputFormat<ElementType>(Collections.singleton(new ElementType()), new TestSerializer(true, false));
        out.writeObject(inFormat);
        out.close();
        ByteArrayInputStream bais = new ByteArrayInputStream(buffer.toByteArray());
        ObjectInputStream in = new ObjectInputStream(bais);
        try {
            in.readObject();
            fail("should throw an exception");
        } catch (Exception e) {
            assertTrue(e.getCause() instanceof TestException);
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Aggregations

ObjectOutputStream (java.io.ObjectOutputStream)958 ByteArrayOutputStream (java.io.ByteArrayOutputStream)657 ObjectInputStream (java.io.ObjectInputStream)386 ByteArrayInputStream (java.io.ByteArrayInputStream)332 IOException (java.io.IOException)312 FileOutputStream (java.io.FileOutputStream)132 Test (org.junit.Test)130 File (java.io.File)75 BufferedOutputStream (java.io.BufferedOutputStream)46 ObjectOutput (java.io.ObjectOutput)35 OutputStream (java.io.OutputStream)35 HashMap (java.util.HashMap)35 FileInputStream (java.io.FileInputStream)24 ArrayList (java.util.ArrayList)24 InputStream (java.io.InputStream)22 FileNotFoundException (java.io.FileNotFoundException)20 Serializable (java.io.Serializable)15 Test (org.testng.annotations.Test)15 NotSerializableException (java.io.NotSerializableException)14 Map (java.util.Map)13