/// <summary> /// Initializes a new instance of the <see cref="PackageLoadingErrorArgs"/> class. /// </summary> /// <param name="context">The context.</param> /// <param name="errorString"></param> public PackageLoadingErrorArgs(LoadPackageContext context, string errorString) { ErrorString = errorString; Package = context.Package; Worker = context.Worker; ApplicationObjectSpace = context.ObjectSpace; CurrentNodeId = context.CurrentNodeId; }
/// <summary> /// Verify package the is valid. /// </summary> /// <param name="context">The context.</param> /// <param name="senderNode">The sender node.</param> /// <returns></returns> private bool packageIsValid(LoadPackageContext context, ReplicationNode senderNode) { var packageIsValid = true; var errors = new List <string>(); if (!Owner.AnonymousPackagesAllowed && senderNode == null) { packageIsValid = false; var errorText = string.Format(Localizer.SenderNodeIsNotFound, context.Package.SenderNodeId, context.Package); context.Worker.ReportError(errorText); errors.Add(errorText); } var expectedPackageId = 1; if (senderNode != null) { switch (context.Package.PackageType) { case PackageType.Protocol: expectedPackageId = senderNode.LastLoadedPackageNumber + 1; break; case PackageType.Snapshot: expectedPackageId = senderNode.LastLoadedSnapshotNumber + 1; break; } } if (context.Package.PackageId != expectedPackageId) { packageIsValid = false; var errorText = string.Format(Localizer.InvalidPackageId, context.Package, context.Package.PackageId, expectedPackageId); context.Worker.ReportError(errorText); errors.Add(errorText); } if (context.Package.PackageData == null || context.Package.PackageData.Length == 0) { packageIsValid = false; var errorText = string.Format(Localizer.PackageDataIsEmpty, context.Package); context.Worker.ReportError(errorText); errors.Add(errorText); } if (!packageIsValid) { var errorString = string.Join("\n", errors.ToArray()); context.Package.CreateLogRecord(PackageEventType.Rejected, errorString); } return(packageIsValid); }
public SnapshotLoadContext(LoadPackageContext context, IEnumerable <SnapshotOidMap> sourceMaps) : base(context.Package, context.Worker, context.ObjectSpace, context.CurrentNodeId) { Maps = new Dictionary <object, SnapshotOidMap>(); RestoredObjects = new List <object>(); UpdatedMapping = new List <object>(); var sourceList = sourceMaps.ToList(); // fixReplicaDictionary(context, sourceList); sourceList.ForEach(x => Maps.Add(x.Target.Target, x)); }
/// <summary> /// Loads the snapshot package. /// </summary> /// <param name="context">The context.</param> /// <returns></returns> public string LoadSnapshotPackage(LoadPackageContext context) { var result = string.Empty; try { using (var snapshotObjectSpace = new ObjectSpace(context.Package.UnitOfWork)) { // create root context var sourceMaps = snapshotObjectSpace.GetObjects <SnapshotOidMap>(); var rootContext = new SnapshotLoadContext(context, sourceMaps); context.Worker.ReportProgress(string.Format(Localizer.ObjectsFoundInSnapshot, sourceMaps.Count)); // restore all of object from snapshot foreach (var sourceMap in sourceMaps) { loadSnapshotObject(rootContext, sourceMap); } // fix OidMap references var newMaps = from c in context.ObjectSpace.ModifiedObjects.Cast <object>() where c is OidMap && ((OidMap)c).NewObject != null select c as OidMap; context.ObjectSpace.CommitChanges(); newMaps.ToList().ForEach(x => x.FixReference()); snapshotPostLoad(context.ObjectSpace); restoreLastLoadedNum(context, snapshotObjectSpace); context.ObjectSpace.CommitChanges(); snapshotObjectSpace.Rollback(); } } catch (Exception exception) { result = string.Format(Localizer.SnapshotLoadingIsFailed, exception.Message); context.Worker.ReportError(result); } if (string.IsNullOrEmpty(result)) { if (context.Worker.CancellationPending) { result = Localizer.LoadingAborted; } context.Worker.ReportProgress(Color.Blue, Localizer.SnapshotLoadingIs, (context.Worker.CancellationPending ? Localizer.Aborted : Localizer.Finished)); } return(result); }
/// <summary> /// Restore the last loaded number from marker. /// </summary> /// <param name="context">The context.</param> /// <param name="snapshotObjectSpace">The snapshot object space.</param> private static void restoreLastLoadedNum(LoadPackageContext context, IObjectSpace snapshotObjectSpace) { var marker = PackageMarker.GetInstance(snapshotObjectSpace); if (marker == null) { return; } var sender = ReplicationNode.FindNode(context.ObjectSpace, marker.SenderNodeId); if (sender != null) { sender.LastLoadedPackageNumber = marker.LastSavedPackageNumber; } }
/// <summary> /// Initializes a new instance of the <see cref="InvalidPackageArgs"/> class. /// </summary> /// <param name="context">The context.</param> public InvalidPackageArgs(LoadPackageContext context) { ApplicationObjectSpace = context.ObjectSpace; Package = context.Package; }
/// <summary> /// Loads the package. /// </summary> /// <param name="context">The load package params.</param> public bool LoadPackage(LoadPackageContext context) { // prevent loading self packages if (context.Package.SenderNodeId == context.CurrentNodeId) { return(false); } // don't load packages builded before last loaded snapshot var lastLoadedSnapshotDateTime = (from c in context.ObjectSpace.GetObjects <ReplicationNode>() select c.InitDateTime).Max(); if (context.Package.PackageDateTime < lastLoadedSnapshotDateTime) { return(true); } var senderNode = ReplicationNode.FindNode(context.ObjectSpace, context.Package.SenderNodeId); context.Worker.ReportProgress(string.Format(Localizer.LoadingPackage, context.Package)); var loadResult = false; if (packageIsValid(context, senderNode)) { Owner.DoBeforeLoadPackage(new LoadPackageEventArgs(context.Package)); context.Package.CreateLogRecord(PackageEventType.Loading, ""); var errorString = ""; switch (context.Package.PackageType) { case PackageType.Protocol: errorString = Owner.ProtocolReplicationService.LoadProtocolPackage(context); break; case PackageType.Snapshot: errorString = Owner.SnapshotService.LoadSnapshotPackage(context); break; } loadResult = string.IsNullOrEmpty(errorString); // rollback changes on error or abort if (!loadResult) { Owner.DoPackageLoadingError(new PackageLoadingErrorArgs(context, errorString)); context.ObjectSpace.Rollback(); // add and save log record context.Package.CreateLogRecord(PackageEventType.Failed, errorString); context.ObjectSpace.CommitChanges(); context.Worker.ReportError(Localizer.PackageLoadingIsFailed, context.Package); } else { // update last loaded package id for sender node senderNode = senderNode ?? ReplicationNode.FindNode(context.ObjectSpace, context.Package.SenderNodeId); if (senderNode != null) { switch (context.Package.PackageType) { case PackageType.Protocol: senderNode.LastLoadedPackageNumber = context.Package.PackageId; break; case PackageType.Snapshot: senderNode.LastLoadedSnapshotNumber = context.Package.PackageId; senderNode.InitDateTime = context.Package.PackageDateTime; break; } } context.Package.CreateLogRecord(PackageEventType.Loaded, ""); // save changes context.ObjectSpace.CommitChanges(); Owner.DoAfterLoadPackage(new LoadPackageEventArgs(context.Package)); context.Worker.ReportProgress(Color.Blue, Localizer.PackageLoadingCompleted, context.Package); } } else { Owner.DoInvalidPackage(new InvalidPackageArgs(context)); context.Worker.ReportError(Localizer.PackageRejected, context.Package); } return(loadResult); }
public SnapLoadContext(LoadPackageContext context, IEnumerable<SnapshotOidMap> sourceMaps) : base(context.Package, context.Worker, context.ObjectSpace, context.CurrentNodeId) { Maps = new Dictionary<object, SnapshotOidMap>(); RestoredObjects = new List<object>(); UpdatedMapping = new List<object>(); var sourceList = sourceMaps.ToList(); // fixReplicaDictionary(context, sourceList); sourceList.ForEach(x => Maps.Add(x.Target.Target, x)); }
/// <summary> /// Loads the package. /// </summary> /// <param name="context">The context.</param> /// <returns></returns> public string LoadProtocolPackage(LoadPackageContext context) { var result = string.Empty; try { using (var uow = context.Package.UnitOfWork) { var topSessions = new XPCollection<PackageSession>(uow, CriteriaOperator.Parse("IsNull(Parent)")); var senderNode = ReplicationNode.FindNode(context.ObjectSpace, context.Package.SenderNodeId); topSessions.OrderBy(x => x.CommitedOn.Add(x.UtcOffset)) /* skip sessions commited before last loaded snapshot */ .Where(x => senderNode == null || x.CommitedOn > senderNode.InitDateTime) .TakeWhile(z => !context.Worker.CancellationPending).ToList() .ForEach(y => loadTopSession(new LoadPackageSessionContext(context, y))); context.ObjectSpace.CommitChanges(); } } catch (Exception exception) { result = string.Format(Localizer.PackageLoadError, exception.Message); context.Worker.ReportError( result); } if (string.IsNullOrEmpty(result)) { if (context.Worker.CancellationPending) result = Localizer.LoadingAborted; context.Worker.ReportProgress(Color.Blue, Localizer.LoadingIs, (context.Worker.CancellationPending ? Localizer.Aborted : Localizer.Finished)); } return result; }
public string LoadSnapshotPackage(LoadPackageContext context) { var result = string.Empty; XafDeltaModule.Instance.LoadService.BeginLoad(); try { using (var snapshotObjectSpace = new ObjectSpace(context.Package.UnitOfWork)) { // create root context var sourceMaps = snapshotObjectSpace.GetObjects<SnapshotOidMap>().OrderBy(x => x.OrdNo).ToList(); var rootContext = new SnapLoadContext(context, sourceMaps); context.Worker.ReportProgress(string.Format(Localizer.ObjectsFoundInSnapshot, sourceMaps.Count)); var i = 0; // restore all of object from snapshot foreach (var sourceMap in sourceMaps.TakeWhile(x => !context.Worker.CancellationPending)) { context.Worker.ReportPercent((double) i++/(double) sourceMaps.Count); context.Worker.ShowStatus(string.Format(Localizer.SnapshotObject, sourceMap.Target.Target)); loadSnapshotObject(rootContext, sourceMap); } if (!context.Worker.CancellationPending) { // fix OidMap references var newMaps = from c in context.ObjectSpace.ModifiedObjects.Cast<object>() where c is OidMap && ((OidMap) c).NewObject != null select c as OidMap; context.ObjectSpace.CommitChanges(); newMaps.ToList().ForEach(x => x.FixReference()); snapshotPostLoad(context.ObjectSpace); restoreLastLoadedNum(context, snapshotObjectSpace); context.ObjectSpace.CommitChanges(); snapshotObjectSpace.Rollback(); } } } catch (Exception exception) { result = string.Format(Localizer.SnapshotLoadingIsFailed, exception.Message); context.Worker.ReportError(result); } XafDeltaModule.Instance.LoadService.EndLoad(); if (string.IsNullOrEmpty(result)) { if (context.Worker.CancellationPending) result = Localizer.LoadingAborted; context.Worker.ReportProgress(Color.Blue, Localizer.SnapshotLoadingIs, (context.Worker.CancellationPending ? Localizer.Aborted : Localizer.Finished)); } return result; }
/// <summary> /// Verify package the is valid. /// </summary> /// <param name="context">The context.</param> /// <param name="senderNode">The sender node.</param> /// <returns></returns> private bool packageIsValid(LoadPackageContext context, ReplicationNode senderNode) { var packageIsValid = true; var errors = new List<string>(); if (!Owner.AnonymousPackagesAllowed && senderNode == null) { packageIsValid = false; var errorText = string.Format(Localizer.SenderNodeIsNotFound, context.Package.SenderNodeId, context.Package); context.Worker.ReportError( errorText); errors.Add(errorText); } var expectedPackageId = 1; if (senderNode != null) { switch (context.Package.PackageType) { case PackageType.Protocol: expectedPackageId = senderNode.LastLoadedPackageNumber + 1; break; case PackageType.Snapshot: expectedPackageId = senderNode.LastLoadedSnapshotNumber + 1; break; } } if (context.Package.PackageId != expectedPackageId) { packageIsValid = false; var errorText = string.Format(Localizer.InvalidPackageId, context.Package, context.Package.PackageId, expectedPackageId); context.Worker.ReportError( errorText); errors.Add(errorText); } if(context.Package.PackageData == null || context.Package.PackageData.Length == 0) { packageIsValid = false; var errorText = string.Format(Localizer.PackageDataIsEmpty, context.Package); context.Worker.ReportError( errorText); errors.Add(errorText); } if(!packageIsValid) { var errorString = string.Join("\n", errors.ToArray()); context.Package.CreateLogRecord(PackageEventType.Rejected, errorString); } return packageIsValid; }
/// <summary> /// Loads the package. /// </summary> /// <param name="context">The load package params.</param> public bool LoadPackage(LoadPackageContext context) { // prevent loading self packages if (context.Package.SenderNodeId == context.CurrentNodeId) return false; // don't load packages builded before last loaded snapshot var lastLoadedSnapshotDateTime = (from c in context.ObjectSpace.GetObjects<ReplicationNode>() select c.InitDateTime).Max(); if(context.Package.PackageDateTime < lastLoadedSnapshotDateTime) return true; var senderNode = ReplicationNode.FindNode(context.ObjectSpace, context.Package.SenderNodeId); context.Worker.ReportProgress(string.Format(Localizer.LoadingPackage, context.Package)); var loadResult = false; if (packageIsValid(context, senderNode)) { Owner.DoBeforeLoadPackage(new LoadPackageEventArgs(context.Package)); context.Package.CreateLogRecord(PackageEventType.Loading, ""); var errorString = ""; switch (context.Package.PackageType) { case PackageType.Protocol: errorString = Owner.ProtocolReplicationService.LoadProtocolPackage(context); break; case PackageType.Snapshot: errorString = Owner.SnapshotService.LoadSnapshotPackage(context); break; } loadResult = string.IsNullOrEmpty(errorString); // rollback changes on error or abort if (!loadResult) { Owner.DoPackageLoadingError(new PackageLoadingErrorArgs(context, errorString)); context.ObjectSpace.Rollback(); // add and save log record context.Package.CreateLogRecord(PackageEventType.Failed, errorString); context.ObjectSpace.CommitChanges(); context.Worker.ReportError(Localizer.PackageLoadingIsFailed, context.Package); } else { // update last loaded package id for sender node senderNode = senderNode ?? ReplicationNode.FindNode(context.ObjectSpace, context.Package.SenderNodeId); if (senderNode != null) { switch (context.Package.PackageType) { case PackageType.Protocol: senderNode.LastLoadedPackageNumber = context.Package.PackageId; break; case PackageType.Snapshot: senderNode.LastLoadedSnapshotNumber = context.Package.PackageId; senderNode.InitDateTime = context.Package.PackageDateTime; break; } } context.Package.CreateLogRecord(PackageEventType.Loaded, ""); // save changes context.ObjectSpace.CommitChanges(); Owner.DoAfterLoadPackage(new LoadPackageEventArgs(context.Package)); context.Worker.ReportProgress(Color.Blue, Localizer.PackageLoadingCompleted, context.Package); } } else { Owner.DoInvalidPackage(new InvalidPackageArgs(context)); context.Worker.ReportError( Localizer.PackageRejected, context.Package); } return loadResult; }
private void createProtocolRecords(IEnumerable<PackageRecord> allRecords, LoadPackageContext context) { if (Owner.CreateExternalProtocolRecords) { using (var objectSpace = XafDeltaModule.XafApp.CreateObjectSpace()) { foreach (var packageRecord in allRecords) { var protocolRecord = ProtocolRecord.CreateForPackageRecord(objectSpace, packageRecord, context.Package.SenderNodeId); protocolRecord.ProtocolSession = ExternalProtocolSession.GetSession(objectSpace, packageRecord.PackageSession); } objectSpace.CommitChanges(); } } }
/// <summary> /// Restore the last loaded number from marker. /// </summary> /// <param name="context">The context.</param> /// <param name="snapshotObjectSpace">The snapshot object space.</param> private static void restoreLastLoadedNum(LoadPackageContext context, IObjectSpace snapshotObjectSpace) { var marker = PackageMarker.GetInstance(snapshotObjectSpace); if (marker == null) return; var sender = ReplicationNode.FindNode(context.ObjectSpace, marker.SenderNodeId); if (sender != null) { sender.LastLoadedPackageNumber = marker.LastSavedPackageNumber; } }
public LoadPackageSessionContext(LoadPackageContext context, PackageSession packageSession) : base(context.Package, context.Worker, context.ObjectSpace, context.CurrentNodeId) { PackageSession = packageSession; }