@Override public final InternalTimersSnapshot<K, N> readTimersSnapshot(DataInputView in) throws IOException { InternalTimersSnapshot<K, N> restoredTimersSnapshot = new InternalTimersSnapshot<>(); restoredTimersSnapshot.getKeySerializer(), restoredTimersSnapshot.getNamespaceSerializer()); restoredTimersSnapshot.setEventTimeTimers(restoredEventTimers); restoredTimersSnapshot.setProcessingTimeTimers(restoredProcessingTimers);
/** * Restore the timers (both processing and event time ones) for a given {@code keyGroupIdx}. * * @param restoredSnapshot the restored snapshot containing the key-group's timers, * and the serializers that were used to write them * @param keyGroupIdx the id of the key-group to be put in the snapshot. */ @SuppressWarnings("unchecked") public void restoreTimersForKeyGroup(InternalTimersSnapshot<?, ?> restoredSnapshot, int keyGroupIdx) { this.restoredTimersSnapshot = (InternalTimersSnapshot<K, N>) restoredSnapshot; if (areSnapshotSerializersIncompatible(restoredSnapshot)) { throw new IllegalArgumentException("Tried to restore timers " + "for the same service with different serializers."); } this.keyDeserializer = restoredTimersSnapshot.getKeySerializer(); this.namespaceDeserializer = restoredTimersSnapshot.getNamespaceSerializer(); checkArgument(localKeyGroupRange.contains(keyGroupIdx), "Key Group " + keyGroupIdx + " does not belong to the local range."); // restore the event time timers eventTimeTimersQueue.addAll(restoredTimersSnapshot.getEventTimeTimers()); // restore the processing time timers processingTimeTimersQueue.addAll(restoredTimersSnapshot.getProcessingTimeTimers()); }
@Override protected void writeKeyAndNamespaceSerializers(DataOutputView out) throws IOException { // write key / namespace serializers, and their configuration snapshots TypeSerializerSerializationUtil.writeSerializersAndConfigsWithResilience( out, Arrays.asList( Tuple2.of(timersSnapshot.getKeySerializer(), timersSnapshot.getKeySerializerConfigSnapshot()), Tuple2.of(timersSnapshot.getNamespaceSerializer(), timersSnapshot.getNamespaceSerializerConfigSnapshot()))); } }
@SuppressWarnings("unchecked") @Override protected void restoreKeyAndNamespaceSerializers( InternalTimersSnapshot<K, N> restoredTimersSnapshot, DataInputView in) throws IOException { List<Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>>> serializersAndConfigs = TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(in, userCodeClassLoader); restoredTimersSnapshot.setKeySerializer((TypeSerializer<K>) serializersAndConfigs.get(0).f0); restoredTimersSnapshot.setKeySerializerConfigSnapshot((TypeSerializerSnapshot<K>) serializersAndConfigs.get(0).f1); restoredTimersSnapshot.setNamespaceSerializer((TypeSerializer<N>) serializersAndConfigs.get(1).f0); restoredTimersSnapshot.setNamespaceSerializerConfigSnapshot((TypeSerializerSnapshot<N>) serializersAndConfigs.get(1).f1); } }
private boolean areSnapshotSerializersIncompatible(InternalTimersSnapshot<?, ?> restoredSnapshot) { return (this.keyDeserializer != null && !this.keyDeserializer.equals(restoredSnapshot.getKeySerializer())) || (this.namespaceDeserializer != null && !this.namespaceDeserializer.equals(restoredSnapshot.getNamespaceSerializer())); } }
@SuppressWarnings("unchecked") @Override protected void restoreKeyAndNamespaceSerializers( InternalTimersSnapshot<K, N> restoredTimersSnapshot, DataInputView in) throws IOException { DataInputViewStream dis = new DataInputViewStream(in); try { restoredTimersSnapshot.setKeySerializer(InstantiationUtil.deserializeObject(dis, userCodeClassLoader, true)); restoredTimersSnapshot.setNamespaceSerializer(InstantiationUtil.deserializeObject(dis, userCodeClassLoader, true)); } catch (ClassNotFoundException exception) { throw new IOException(exception); } } }
this.keyDeserializer, null, restoredTimersSnapshot.getKeySerializerConfigSnapshot(), keySerializer); this.namespaceDeserializer, null, restoredTimersSnapshot.getNamespaceSerializerConfigSnapshot(), namespaceSerializer);
/** * Snapshots the timers (both processing and event time ones) for a given {@code keyGroupIdx}. * * @param keyGroupIdx the id of the key-group to be put in the snapshot. * @return a snapshot containing the timers for the given key-group, and the serializers for them */ public InternalTimersSnapshot<K, N> snapshotTimersForKeyGroup(int keyGroupIdx) { return new InternalTimersSnapshot<>( keySerializer, keySerializer.snapshotConfiguration(), namespaceSerializer, namespaceSerializer.snapshotConfiguration(), eventTimeTimersQueue.getSubsetForKeyGroup(keyGroupIdx), processingTimeTimersQueue.getSubsetForKeyGroup(keyGroupIdx)); }
@SuppressWarnings("unchecked") @Override protected void restoreKeyAndNamespaceSerializers( InternalTimersSnapshot<K, N> restoredTimersSnapshot, DataInputView in) throws IOException { List<Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>>> serializersAndConfigs = TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(in, userCodeClassLoader); restoredTimersSnapshot.setKeySerializer((TypeSerializer<K>) serializersAndConfigs.get(0).f0); restoredTimersSnapshot.setKeySerializerConfigSnapshot((TypeSerializerSnapshot<K>) serializersAndConfigs.get(0).f1); restoredTimersSnapshot.setNamespaceSerializer((TypeSerializer<N>) serializersAndConfigs.get(1).f0); restoredTimersSnapshot.setNamespaceSerializerConfigSnapshot((TypeSerializerSnapshot<N>) serializersAndConfigs.get(1).f1); } }
@SuppressWarnings("unchecked") private <N> InternalTimerServiceImpl<K, N> registerOrGetTimerService( String serviceName, InternalTimersSnapshot<?, ?> restoredTimersSnapshot) { final TypeSerializer<K> keySerializer = (TypeSerializer<K>) restoredTimersSnapshot.getKeySerializer(); final TypeSerializer<N> namespaceSerializer = (TypeSerializer<N>) restoredTimersSnapshot.getNamespaceSerializer(); TimerSerializer<K, N> timerSerializer = new TimerSerializer<>(keySerializer, namespaceSerializer); return timerServicesManager.registerOrGetTimerService(serviceName, timerSerializer); } }
@SuppressWarnings("unchecked") @Override protected void restoreKeyAndNamespaceSerializers( InternalTimersSnapshot<K, N> restoredTimersSnapshot, DataInputView in) throws IOException { DataInputViewStream dis = new DataInputViewStream(in); try { restoredTimersSnapshot.setKeySerializer(InstantiationUtil.deserializeObject(dis, userCodeClassLoader, true)); restoredTimersSnapshot.setNamespaceSerializer(InstantiationUtil.deserializeObject(dis, userCodeClassLoader, true)); } catch (ClassNotFoundException exception) { throw new IOException(exception); } } }
this.keyDeserializer, null, restoredTimersSnapshot.getKeySerializerConfigSnapshot(), keySerializer); this.namespaceDeserializer, null, restoredTimersSnapshot.getNamespaceSerializerConfigSnapshot(), namespaceSerializer);
/** * Snapshots the timers (both processing and event time ones) for a given {@code keyGroupIdx}. * * @param keyGroupIdx the id of the key-group to be put in the snapshot. * @return a snapshot containing the timers for the given key-group, and the serializers for them */ public InternalTimersSnapshot<K, N> snapshotTimersForKeyGroup(int keyGroupIdx) { return new InternalTimersSnapshot<>( keySerializer, keySerializer.snapshotConfiguration(), namespaceSerializer, namespaceSerializer.snapshotConfiguration(), eventTimeTimersQueue.getSubsetForKeyGroup(keyGroupIdx), processingTimeTimersQueue.getSubsetForKeyGroup(keyGroupIdx)); }
@Override public final void writeTimersSnapshot(DataOutputView out) throws IOException { writeKeyAndNamespaceSerializers(out); LegacyTimerSerializer<K, N> timerSerializer = new LegacyTimerSerializer<>( timersSnapshot.getKeySerializer(), timersSnapshot.getNamespaceSerializer()); // write the event time timers Set<TimerHeapInternalTimer<K, N>> eventTimers = timersSnapshot.getEventTimeTimers(); if (eventTimers != null) { out.writeInt(eventTimers.size()); for (TimerHeapInternalTimer<K, N> eventTimer : eventTimers) { timerSerializer.serialize(eventTimer, out); } } else { out.writeInt(0); } // write the processing time timers Set<TimerHeapInternalTimer<K, N>> processingTimers = timersSnapshot.getProcessingTimeTimers(); if (processingTimers != null) { out.writeInt(processingTimers.size()); for (TimerHeapInternalTimer<K, N> processingTimer : processingTimers) { timerSerializer.serialize(processingTimer, out); } } else { out.writeInt(0); } } }
@Override public final InternalTimersSnapshot<K, N> readTimersSnapshot(DataInputView in) throws IOException { InternalTimersSnapshot<K, N> restoredTimersSnapshot = new InternalTimersSnapshot<>(); restoredTimersSnapshot.getKeySerializer(), restoredTimersSnapshot.getNamespaceSerializer()); restoredTimersSnapshot.setEventTimeTimers(restoredEventTimers); restoredTimersSnapshot.setProcessingTimeTimers(restoredProcessingTimers);
@Override protected void writeKeyAndNamespaceSerializers(DataOutputView out) throws IOException { // write key / namespace serializers, and their configuration snapshots TypeSerializerSerializationUtil.writeSerializersAndConfigsWithResilience( out, Arrays.asList( Tuple2.of(timersSnapshot.getKeySerializer(), timersSnapshot.getKeySerializerConfigSnapshot()), Tuple2.of(timersSnapshot.getNamespaceSerializer(), timersSnapshot.getNamespaceSerializerConfigSnapshot()))); } }
@SuppressWarnings("unchecked") @Override protected void restoreKeyAndNamespaceSerializers( InternalTimersSnapshot<K, N> restoredTimersSnapshot, DataInputView in) throws IOException { List<Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>>> serializersAndConfigs = TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(in, userCodeClassLoader); restoredTimersSnapshot.setKeySerializer((TypeSerializer<K>) serializersAndConfigs.get(0).f0); restoredTimersSnapshot.setKeySerializerConfigSnapshot((TypeSerializerSnapshot<K>) serializersAndConfigs.get(0).f1); restoredTimersSnapshot.setNamespaceSerializer((TypeSerializer<N>) serializersAndConfigs.get(1).f0); restoredTimersSnapshot.setNamespaceSerializerConfigSnapshot((TypeSerializerSnapshot<N>) serializersAndConfigs.get(1).f1); } }
@Override protected void writeKeyAndNamespaceSerializers(DataOutputView out) throws IOException { // the pre-versioned format only serializes the serializers, without their configuration snapshots try (ByteArrayOutputStreamWithPos stream = new ByteArrayOutputStreamWithPos()) { InstantiationUtil.serializeObject(stream, timersSnapshot.getKeySerializer()); InstantiationUtil.serializeObject(stream, timersSnapshot.getNamespaceSerializer()); out.write(stream.getBuf(), 0, stream.getPosition()); } } }
@SuppressWarnings("unchecked") @Override protected void restoreKeyAndNamespaceSerializers( InternalTimersSnapshot<K, N> restoredTimersSnapshot, DataInputView in) throws IOException { DataInputViewStream dis = new DataInputViewStream(in); try { restoredTimersSnapshot.setKeySerializer(InstantiationUtil.deserializeObject(dis, userCodeClassLoader, true)); restoredTimersSnapshot.setNamespaceSerializer(InstantiationUtil.deserializeObject(dis, userCodeClassLoader, true)); } catch (ClassNotFoundException exception) { throw new IOException(exception); } } }
this.keyDeserializer, null, restoredTimersSnapshot.getKeySerializerConfigSnapshot(), keySerializer); this.namespaceDeserializer, null, restoredTimersSnapshot.getNamespaceSerializerConfigSnapshot(), namespaceSerializer);