private void VerifySerialization(DaemonMsgCreate msg) { var daemonMsgSerializer = ser.FindSerializerFor(msg); AssertDaemonMsgCreate(msg, ser.Deserialize(daemonMsgSerializer.ToBinary(msg), daemonMsgSerializer.Identifier, typeof(DaemonMsgCreate)).AsInstanceOf <DaemonMsgCreate>()); }
/// <summary> /// Asynchronously loads a snapshot. /// This call is protected with a circuit-breaker /// </summary> /// <param name="persistenceId">PersistenceId</param> /// <param name="criteria">Selection criteria</param> /// <returns> /// TBD /// </returns> protected override Task <SelectedSnapshot> LoadAsync(string persistenceId, SnapshotSelectionCriteria criteria) { persistenceId = IdNormalizer.Normalize(persistenceId); var query = GetSnapshotQuery(persistenceId, criteria); var result = query .Where(a => a.DocumentType == "snap") .OrderByDescending(a => a.SequenceNr) .ToList()//CosmosDB doesn't allow constructor invocation .Select(a => { if (a.SerializerId.HasValue && a.Snapshot != null) { if (a.SerializerId.Value != 1) { a.Snapshot = serialization.Deserialize(Convert.FromBase64String((string)a.Snapshot), a.SerializerId.Value, a.Manifest); } else { if (a.Snapshot is JObject) { a.Snapshot = JsonConvert.DeserializeObject(((JObject)a.Snapshot).ToString(Newtonsoft.Json.Formatting.None), Type.GetType(a.Manifest), new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All } ); } } } return(new SelectedSnapshot(new SnapshotMetadata(a.PersistenceId, a.SequenceNr, a.Timestamp.ToDateTime()), a.Snapshot)); }) .FirstOrDefault(); return(Task.FromResult(result)); }
private void VerifySerialization(DaemonMsgCreate msg) { var daemonMsgSerializer = _ser.FindSerializerFor(msg); var binary = daemonMsgSerializer.ToBinary(msg); var actual = (DaemonMsgCreate)_ser.Deserialize(binary, daemonMsgSerializer.Identifier, typeof(DaemonMsgCreate)); AssertDaemonMsgCreate(msg, actual); }
public override Task ReplayMessagesAsync(IActorContext context, string persistenceId, long fromSequenceNr, long toSequenceNr, long max, Action <IPersistentRepresentation> recoveryCallback) { persistenceId = IdNormalizer.Normalize(persistenceId); // Limit allows only integer var limitValue = max >= int.MaxValue ? int.MaxValue : (int)max; // Do not replay messages if limit equal zero if (limitValue == 0) { return(Task.FromResult(false)); } IQueryable <JournalEntry> query = GetJournalEntryQuery(persistenceId) .Where(a => a.PersistenceId == persistenceId && a.SequenceNr >= fromSequenceNr && a.SequenceNr <= toSequenceNr && a.DocumentType == "jrnl") .OrderBy(a => a.SequenceNr) .Take(limitValue); var documents = query.ToList(); documents.ForEach(doc => { if (doc.SerializerId.HasValue) { if (doc.SerializerId.Value != 1) { doc.Payload = serialization.Deserialize(Convert.FromBase64String((string)doc.Payload), doc.SerializerId.Value, doc.Manifest); } else { if (doc.Payload is JObject) { doc.Payload = JsonConvert.DeserializeObject(((JObject)doc.Payload).ToString(Newtonsoft.Json.Formatting.None), Type.GetType(doc.Manifest)); } } } recoveryCallback(new Persistent(doc.Payload, doc.SequenceNr, doc.PersistenceId, doc.Manifest, doc.IsDeleted, context.Sender)); }); return(Task.FromResult(0)); }
private Persistent ToPersistenceRepresentation(JournalEntry entry, IActorRef sender) { if (_settings.LegacySerialization) { var manifest = string.IsNullOrEmpty(entry.Manifest) ? entry.Payload.GetType().TypeQualifiedName() : entry.Manifest; return(new Persistent( entry.Payload, entry.SequenceNr, entry.PersistenceId, manifest, entry.IsDeleted, sender, timestamp: ToTicks(entry.Ordering))); // MongoDb timestamps are stored as Unix Epoch } var legacy = entry.SerializerId.HasValue || !string.IsNullOrEmpty(entry.Manifest); if (!legacy) { var ser = _serialization.FindSerializerForType(typeof(Persistent)); var output = ser.FromBinary <Persistent>((byte[])entry.Payload); // backwards compatibility for https://github.com/akkadotnet/akka.net/pull/4680 // it the timestamp is not defined in the binary payload if (output.Timestamp == 0L) { output = (Persistent)output.WithTimestamp(ToTicks(entry.Ordering)); } return(output); } int? serializerId = null; Type type = null; // legacy serialization if (!entry.SerializerId.HasValue && !string.IsNullOrEmpty(entry.Manifest)) { type = Type.GetType(entry.Manifest, true); } else { serializerId = entry.SerializerId; } if (entry.Payload is byte[] bytes) { object deserialized = null; if (serializerId.HasValue) { deserialized = _serialization.Deserialize(bytes, serializerId.Value, entry.Manifest); } else { var deserializer = _serialization.FindSerializerForType(type); deserialized = deserializer.FromBinary(bytes, type); } if (deserialized is Persistent p) { return((Persistent)p.WithTimestamp(ToTicks(entry.Ordering))); } return(new Persistent(deserialized, entry.SequenceNr, entry.PersistenceId, entry.Manifest, entry.IsDeleted, sender, timestamp: ToTicks(entry.Ordering))); } else // backwards compat for object serialization - Payload was already deserialized by BSON { return(new Persistent(entry.Payload, entry.SequenceNr, entry.PersistenceId, entry.Manifest, entry.IsDeleted, sender, timestamp: ToTicks(entry.Ordering))); } }
private SelectedSnapshot ToSelectedSnapshot(SnapshotEntry entry) { if (_settings.LegacySerialization) { return(new SelectedSnapshot( new SnapshotMetadata( entry.PersistenceId, entry.SequenceNr, new DateTime(entry.Timestamp)), entry.Snapshot)); } var legacy = entry.SerializerId.HasValue || !string.IsNullOrEmpty(entry.Manifest); if (!legacy) { var ser = _serialization.FindSerializerForType(typeof(Serialization.Snapshot)); var snapshot = ser.FromBinary <Serialization.Snapshot>((byte[])entry.Snapshot); return(new SelectedSnapshot(new SnapshotMetadata(entry.PersistenceId, entry.SequenceNr), snapshot.Data)); } int? serializerId = null; Type type = null; // legacy serialization if (!entry.SerializerId.HasValue && !string.IsNullOrEmpty(entry.Manifest)) { type = Type.GetType(entry.Manifest, true); } else { serializerId = entry.SerializerId; } if (entry.Snapshot is byte[] bytes) { object deserialized; if (serializerId.HasValue) { deserialized = _serialization.Deserialize(bytes, serializerId.Value, entry.Manifest); } else { var deserializer = _serialization.FindSerializerForType(type); deserialized = deserializer.FromBinary(bytes, type); } if (deserialized is Serialization.Snapshot snap) { return(new SelectedSnapshot( new SnapshotMetadata(entry.PersistenceId, entry.SequenceNr, new DateTime(entry.Timestamp)), snap.Data)); } return(new SelectedSnapshot( new SnapshotMetadata(entry.PersistenceId, entry.SequenceNr, new DateTime(entry.Timestamp)), deserialized)); } // backwards compat - loaded an old snapshot using BSON serialization. No need to deserialize via Akka.NET return(new SelectedSnapshot( new SnapshotMetadata(entry.PersistenceId, entry.SequenceNr, new DateTime(entry.Timestamp)), entry.Snapshot)); }