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());
}
}
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>>()));
}
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();
}
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);
}
}
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));
}
}
Aggregations