Search in sources :

Example 11 with ObjectOutputStream

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

the class MemoryStateBackendTest method testOversizedState.

@Test
public void testOversizedState() {
    try {
        MemoryStateBackend backend = new MemoryStateBackend(10);
        CheckpointStreamFactory streamFactory = backend.createStreamFactory(new JobID(), "test_op");
        HashMap<String, Integer> state = new HashMap<>();
        state.put("hey there", 2);
        state.put("the crazy brown fox stumbles over a sentence that does not contain every letter", 77);
        try {
            CheckpointStreamFactory.CheckpointStateOutputStream outStream = streamFactory.createCheckpointStateOutputStream(12, 459);
            ObjectOutputStream oos = new ObjectOutputStream(outStream);
            oos.writeObject(state);
            oos.flush();
            outStream.closeAndGetHandle();
            fail("this should cause an exception");
        } catch (IOException e) {
        // now darling, isn't that exactly what we wanted?
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : HashMap(java.util.HashMap) MemoryStateBackend(org.apache.flink.runtime.state.memory.MemoryStateBackend) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) JobID(org.apache.flink.api.common.JobID) IOException(java.io.IOException) Test(org.junit.Test)

Example 12 with ObjectOutputStream

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

the class MetricDumpSerializerTest method testJavaSerialization.

@Test
public void testJavaSerialization() throws IOException {
    MetricDumpSerialization.MetricDumpSerializer serializer = new MetricDumpSerialization.MetricDumpSerializer();
    final ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
    final ObjectOutputStream oos = new ObjectOutputStream(bos);
    oos.writeObject(serializer.serialize(new HashMap<Counter, Tuple2<QueryScopeInfo, String>>(), new HashMap<Gauge<?>, Tuple2<QueryScopeInfo, String>>(), new HashMap<Histogram, Tuple2<QueryScopeInfo, String>>(), new HashMap<Meter, Tuple2<QueryScopeInfo, String>>()));
}
Also used : HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) Test(org.junit.Test)

Example 13 with ObjectOutputStream

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

the class KeyedOneInputStreamOperatorTestHarness method snapshotLegacy.

/**
	 *
	 */
@Override
public StreamStateHandle snapshotLegacy(long checkpointId, long timestamp) throws Exception {
    // simply use an in-memory handle
    MemoryStateBackend backend = new MemoryStateBackend();
    CheckpointStreamFactory streamFactory = backend.createStreamFactory(new JobID(), "test_op");
    CheckpointStreamFactory.CheckpointStateOutputStream outStream = streamFactory.createCheckpointStateOutputStream(checkpointId, timestamp);
    if (operator instanceof StreamCheckpointedOperator) {
        ((StreamCheckpointedOperator) operator).snapshotState(outStream, checkpointId, timestamp);
    }
    if (keyedStateBackend != null) {
        RunnableFuture<KeyGroupsStateHandle> keyedSnapshotRunnable = keyedStateBackend.snapshot(checkpointId, timestamp, streamFactory, CheckpointOptions.forFullCheckpoint());
        if (!keyedSnapshotRunnable.isDone()) {
            Thread runner = new Thread(keyedSnapshotRunnable);
            runner.start();
        }
        outStream.write(1);
        ObjectOutputStream oos = new ObjectOutputStream(outStream);
        oos.writeObject(keyedSnapshotRunnable.get());
        oos.flush();
    } else {
        outStream.write(0);
    }
    return outStream.closeAndGetHandle();
}
Also used : CheckpointStreamFactory(org.apache.flink.runtime.state.CheckpointStreamFactory) MemoryStateBackend(org.apache.flink.runtime.state.memory.MemoryStateBackend) StreamCheckpointedOperator(org.apache.flink.streaming.api.operators.StreamCheckpointedOperator) ObjectOutputStream(java.io.ObjectOutputStream) JobID(org.apache.flink.api.common.JobID) KeyGroupsStateHandle(org.apache.flink.runtime.state.KeyGroupsStateHandle)

Example 14 with ObjectOutputStream

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

the class CommonTestUtils method createCopySerializable.

/**
	 * Creates a copy of an object via Java Serialization.
	 *
	 * @param original The original object.
	 * @return The copied object.
	 */
public static <T extends java.io.Serializable> T createCopySerializable(T original) throws IOException {
    if (original == null) {
        throw new IllegalArgumentException();
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(original);
    oos.close();
    baos.close();
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    try (ObjectInputStream ois = new ObjectInputStream(bais)) {
        @SuppressWarnings("unchecked") T copy = (T) ois.readObject();
        return copy;
    } catch (ClassNotFoundException e) {
        throw new IOException(e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 15 with ObjectOutputStream

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

the class ZooKeeperLeaderElectionService method writeLeaderInformation.

/**
	 * Writes the current leader's address as well the given leader session ID to ZooKeeper.
	 *
	 * @param leaderSessionID Leader session ID which is written to ZooKeeper
	 */
protected void writeLeaderInformation(UUID leaderSessionID) {
    // is thread-safe
    try {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Write leader information: Leader={}, session ID={}.", leaderContender.getAddress(), leaderSessionID);
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeUTF(leaderContender.getAddress());
        oos.writeObject(leaderSessionID);
        oos.close();
        boolean dataWritten = false;
        while (!dataWritten && leaderLatch.hasLeadership()) {
            Stat stat = client.checkExists().forPath(leaderPath);
            if (stat != null) {
                long owner = stat.getEphemeralOwner();
                long sessionID = client.getZookeeperClient().getZooKeeper().getSessionId();
                if (owner == sessionID) {
                    try {
                        client.setData().forPath(leaderPath, baos.toByteArray());
                        dataWritten = true;
                    } catch (KeeperException.NoNodeException noNode) {
                    // node was deleted in the meantime
                    }
                } else {
                    try {
                        client.delete().forPath(leaderPath);
                    } catch (KeeperException.NoNodeException noNode) {
                    // node was deleted in the meantime --> try again
                    }
                }
            } else {
                try {
                    client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(leaderPath, baos.toByteArray());
                    dataWritten = true;
                } catch (KeeperException.NodeExistsException nodeExists) {
                // node has been created in the meantime --> try again
                }
            }
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Successfully wrote leader information: Leader={}, session ID={}.", leaderContender.getAddress(), leaderSessionID);
        }
    } catch (Exception e) {
        leaderContender.handleError(new Exception("Could not write leader address and leader session ID to " + "ZooKeeper.", e));
    }
}
Also used : Stat(org.apache.zookeeper.data.Stat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) KeeperException(org.apache.zookeeper.KeeperException) KeeperException(org.apache.zookeeper.KeeperException)

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