@SuppressWarnings("unchecked") private <X extends StateHandle<?>> X serializeDeserialize(X handle) throws IOException, ClassNotFoundException { byte[] serialized = InstantiationUtil.serializeObject(handle); return (X) InstantiationUtil.deserializeObject( serialized, Thread.currentThread().getContextClassLoader()); }
private <T> T copyObject(T object) { try { return InstantiationUtil.deserializeObject( InstantiationUtil.serializeObject(object), getClass().getClassLoader()); } catch (IOException | ClassNotFoundException e) { throw new RuntimeException("Failed to copy object."); } }
/** Writes the {@code Context} to the given state checkpoint output. */ protected void writeToState(StateBackend.CheckpointStateOutputView out) throws IOException { keySerializer.serialize(key, out); windowSerializer.serialize(window, out); out.writeLong(watermarkTimer); out.writeLong(processingTimeTimer); byte[] serializedState = InstantiationUtil.serializeObject(state); out.writeInt(serializedState.length); out.write(serializedState, 0, serializedState.length); MultiplexingStreamRecordSerializer<IN> recordSerializer = new MultiplexingStreamRecordSerializer<>(inputSerializer); out.writeInt(windowBuffer.size()); for (StreamRecord<IN> element : windowBuffer.getElements()) { recordSerializer.serialize(element, out); } }
private void writeKVStateMetaData() throws IOException, InterruptedException { // write number of k/v states outputView.writeInt(stateBackend.kvStateInformation.size()); int kvStateId = 0; // iterate all column families, where each column family holds one k/v state, to write the // metadata for (Map.Entry<String, Tuple2<ColumnFamilyHandle, StateDescriptor<?, ?>>> column : stateBackend.kvStateInformation.entrySet()) { // be cooperative and check for interruption from time to time in the hot loop checkInterrupted(); // write StateDescriptor for this k/v state InstantiationUtil.serializeObject(outStream, column.getValue().f1); // retrieve iterator for this k/v states readOptions = new ReadOptions(); readOptions.setSnapshot(snapshot); RocksIterator iterator = stateBackend.db.newIterator(column.getValue().f0, readOptions); kvStateIterators.add(new Tuple2<>(iterator, kvStateId)); ++kvStateId; } }
/** * Writes to the output stream the error return code, and the given exception in serialized form. * * @param out Thr output stream to write to. * @param t The exception to send. * @throws IOException Thrown, if the output stream could not be written to. */ private static void writeErrorToStream(OutputStream out, Throwable t) throws IOException { byte[] bytes = InstantiationUtil.serializeObject(t); out.write(RETURN_ERROR); writeLength(bytes.length, out); out.write(bytes); }