Пример #1
0
        public async Task NodeObserver_ObserveAsync_Successful_Observer_IsHealthy_WarningsOrErrorsDetected()
        {
            if (!this.isSFRuntimePresentOnTestMachine)
            {
                return;
            }

            var startDateTime = DateTime.Now;

            ObserverManager.FabricServiceContext = this.context;
            ObserverManager.FabricClientInstance = new FabricClient(FabricClientRole.User);
            ObserverManager.TelemetryEnabled     = false;
            ObserverManager.EtwEnabled           = false;

            var obs = new NodeObserver
            {
                IsTestRun = true,
                MemWarningUsageThresholdMB = 1, // This will generate Warning for sure...
            };

            await obs.ObserveAsync(this.token).ConfigureAwait(true);

            // observer ran to completion with no errors...
            Assert.IsTrue(obs.LastRunDateTime > startDateTime);

            Assert.IsTrue(obs.HasActiveFabricErrorOrWarning);

            // observer did not have any internal errors during run...
            Assert.IsFalse(obs.IsUnhealthy);

            obs.Dispose();
            ObserverManager.FabricClientInstance.Dispose();
        }
Пример #2
0
        public async Task NodeObserver_Integer_Greater_Than_100_CPU_Warn_Threshold_No_Fail()
        {
            if (!this.isSFRuntimePresentOnTestMachine)
            {
                return;
            }

            var startDateTime = DateTime.Now;

            ObserverManager.FabricServiceContext = this.context;
            ObserverManager.FabricClientInstance = new FabricClient(FabricClientRole.User);
            ObserverManager.TelemetryEnabled     = false;
            ObserverManager.EtwEnabled           = false;

            var obs = new NodeObserver
            {
                IsTestRun = true,
                CpuWarningUsageThresholdPct = 10000,
            };

            await obs.ObserveAsync(this.token).ConfigureAwait(true);

            // observer ran to completion with no errors...
            Assert.IsTrue(obs.LastRunDateTime > startDateTime);

            // observer detected no error conditions...
            Assert.IsFalse(obs.HasActiveFabricErrorOrWarning);

            // observer did not have any internal errors during run...
            Assert.IsFalse(obs.IsUnhealthy);

            obs.Dispose();
            ObserverManager.FabricClientInstance.Dispose();
        }
Пример #3
0
        public async Task NodeObserver_Negative_Integer_CPU_Mem_Ports_Firewalls_Values_No_Exceptions_Intialize()
        {
            if (!this.isSFRuntimePresentOnTestMachine)
            {
                return;
            }

            var startDateTime = DateTime.Now;

            ObserverManager.FabricServiceContext = this.context;
            ObserverManager.FabricClientInstance = new FabricClient(FabricClientRole.User);
            ObserverManager.TelemetryEnabled     = false;
            ObserverManager.EtwEnabled           = false;

            var obs = new NodeObserver
            {
                IsTestRun = true,
                CpuWarningUsageThresholdPct  = -1000,
                MemWarningUsageThresholdMB   = -2500,
                EphemeralPortsErrorThreshold = -42,
            };

            await obs.ObserveAsync(this.token).ConfigureAwait(true);

            // Bad values don't crash Initialize...
            Assert.IsFalse(obs.IsUnhealthy);

            // It ran (crashing in Initialize would not set LastRunDate, which is MinValue until set...)
            Assert.IsTrue(obs.LastRunDateTime > startDateTime);

            obs.Dispose();
            ObserverManager.FabricClientInstance.Dispose();
        }
Пример #4
0
 protected virtual Task <bool> FireCancellableNodeObserverEventAsync(ISnCancellableEvent snEvent,
                                                                     NodeObserver nodeObserver, CancellationToken cancel = default)
 {
     //TODO:<?event: Ensure that this method does not throw any exception (but trace and log).
     //snEvent.NodeObserverAction(nodeObserver);
     //TODO:event: Not implemented yet
     return(Task.FromResult(false));
 }
Пример #5
0
 protected virtual Task FireNodeObserverEventAsync(INodeObserverEvent snEvent,
                                                   NodeObserver nodeObserver, CancellationToken cancel = default)
 {
     //TODO:<?event: Ensure that this method does not throw any exception (but trace and log).
     //snEvent.NodeObserverAction(nodeObserver);
     //TODO:event: Not implemented yet
     return(Task.CompletedTask);
 }
Пример #6
0
            public override void DoAction(bool onRemote, bool isFromMe)
            {
                if (onRemote && isFromMe)
                {
                    return;
                }
                var instance = (TreeCache <Q>)NodeObserver.GetInstanceByGenericBaseType(typeof(TreeCache <Q>));

                instance.InvalidatePrivate();
            }
Пример #7
0
        private static void LoadPrivate()
        {
            // this method must be called inside a lock block!
            var current = new NodeTypeManager();

            current.Load();

            Providers.Instance.NodeTypeManeger = current;

            NodeObserver.FireOnStart(Start);
            SnLog.WriteInformation("NodeTypeManager created: " + Providers.Instance.NodeTypeManeger);
        }
Пример #8
0
        private void LoadPrivate()
        {
            // this method must be called inside a lock block!
            var current = new ReadOnlyStorageSchema();

            current.Load();

            _nodeTypeManager = current;

            NodeObserver.FireOnStart();
            SnLog.WriteInformation("NodeTypeManager created: " + _nodeTypeManager);
        }
Пример #9
0
            public override STT.Task DoActionAsync(bool onRemote, bool isFromMe, CancellationToken cancellationToken)
            {
                if (onRemote && isFromMe)
                {
                    return(STT.Task.CompletedTask);
                }

                var instance = (TreeCache <Q>)NodeObserver.GetInstanceByGenericBaseType(typeof(TreeCache <Q>));

                instance.InvalidatePrivate();

                return(STT.Task.CompletedTask);
            }
Пример #10
0
        private static void LoadPrivate()
        {
            // this method must be called inside a lock block!
            var current = new NodeTypeManager();

            current.Load();
            current.StartEventSystem();

            _current = current;

            NodeObserver.FireOnStart(Start);
            SnLog.WriteInformation("NodeTypeManager created: " + _current);
        }
Пример #11
0
        /// <summary>
        /// Restarts the NodeTypeManager without sending any distributed action.
        /// Do not call this method explicitly, the system will call it if necessary (when the reset is triggered by an another instance).
        /// </summary>
        private void RestartPrivate()
        {
            SnLog.WriteInformation("NodeTypeManager.Restart executed.", EventId.RepositoryRuntime,
                                   properties: new Dictionary <string, object> {
                { "AppDomain", AppDomain.CurrentDomain.FriendlyName }
            });
            NodeObserver.FireOnReset();

            lock (_lock)
            {
                Providers.Instance.DataStore.Reset();
                LoadPrivate();
            }
        }
Пример #12
0
        public void Successful_NodeObserver_Run_Cancellation_Via_ObserverManager()
        {
            ObserverManager.FabricServiceContext = this.context;
            ObserverManager.TelemetryEnabled     = false;
            ObserverManager.EtwEnabled           = false;
            ObserverManager.FabricClientInstance = new FabricClient(FabricClientRole.User);

            var stopWatch = new Stopwatch();

            var obs = new NodeObserver
            {
                IsEnabled = true,
                NodeName  = "_Test_0",
                IsTestRun = true,
            };

            var obsMgr = new ObserverManager(obs)
            {
                ApplicationName = "fabric:/TestApp0",
            };

            var objReady = new ManualResetEventSlim(false);

            stopWatch.Start();
            var t = Task.Factory.StartNew(() =>
            {
                objReady.Set();
                obsMgr.StartObservers();
            });

            objReady?.Wait();

            while (!obsMgr.IsObserverRunning && stopWatch.Elapsed.TotalSeconds < 10)
            {
                // wait...
            }

            stopWatch.Stop();

            obsMgr.StopObservers();

            Thread.Sleep(5);
            Assert.IsFalse(obsMgr.IsObserverRunning);

            obs.Dispose();
            objReady?.Dispose();
        }
Пример #13
0
        public void NodeObserver_Constructor_Test()
        {
            ObserverManager.FabricServiceContext = this.context;
            ObserverManager.FabricClientInstance = new FabricClient(FabricClientRole.User);
            ObserverManager.TelemetryEnabled     = false;
            ObserverManager.EtwEnabled           = false;

            var obs = new NodeObserver();

            Assert.IsTrue(obs.ObserverLogger != null);
            Assert.IsTrue(obs.CsvFileLogger != null);
            Assert.IsTrue(obs.HealthReporter != null);
            Assert.IsTrue(obs.ObserverName == ObserverConstants.NodeObserverName);

            obs.Dispose();
            ObserverManager.FabricClientInstance.Dispose();
        }
Пример #14
0
        public async Task Should_accept_a_completed_node()
        {
            var tracker = new NodeTracker <SimpleValue>(1000, TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(60), () => DateTime.UtcNow);

            var observer = new NodeObserver();

            tracker.Connect(observer);

            var pendingValue     = new PendingValue <string, SimpleValue>("Hello", SimpleValueFactory.Healthy);
            var nodeValueFactory = new NodeValueFactory <SimpleValue>(pendingValue, 100);
            var node             = new FactoryNode <SimpleValue>(nodeValueFactory);

            tracker.Add(nodeValueFactory);

            var value = await node.Value;

            var observedNode = await observer.Value;

            Assert.That(observedNode, Is.InstanceOf <BucketNode <SimpleValue> >());
        }
Пример #15
0
 public new static TreeCache <T> GetInstance(Type type)
 {
     return((TreeCache <T>)NodeObserver.GetInstance(type));
 }
Пример #16
0
            protected override async T.Task <bool> FireCancellableNodeObserverEventAsync(ISnCancellableEvent snEvent, NodeObserver nodeObserver,
                                                                                         CancellationToken cancel = default)
            {
                using (var op = SnTrace.Test.StartOperation(
                           $"NodeObserverAction simulation: {nodeObserver.GetType().Name} {snEvent.GetType().Name}"))
                {
                    await T.Task.Delay(10, cancel).ConfigureAwait(false);

                    op.Successful = true;
                }

                var result = snEvent.CancellableEventArgs.Cancel;

                result |= await base.FireCancellableNodeObserverEventAsync(snEvent, nodeObserver, cancel);

                return(result);
            }
Пример #17
0
 private static void OnReset()
 {
     NodeObserver.FireOnReset(Reset);
 }
Пример #18
0
        /// <summary>
        /// Executes all modifications.
        /// Current user must have SetPermissions permission on any modified entity.
        /// An Auditlog record with changed data will be writen.
        /// OnPermissionChanging and OnPermissionChanged events are fired on any active NodeObserver that is not in the exclusion list (see "disabledObservers" parameter).
        /// </summary>
        /// <param name="disabledObservers">NodeObserver exclusion list.</param>
        public void Apply(List <Type> disabledObservers)
        {
            foreach (var entityId in this._acls.Keys)
            {
                this.Context.AssertPermission(entityId, PermissionType.SetPermissions);
            }
            using (var audit = new AuditBlock(AuditEvent.PermissionChanged, "Trying to execute permission modifications",
                                              new Dictionary <string, object>
            {
                { "Entities", this._acls.Count },
                { "Breaks", this._breaks.Count },
                { "Unbreaks", this._unbreaks.Count }
            }))
            {
                using (var op = SnTrace.Security.StartOperation("AclEditor.Apply (acl count: {0})", _acls.Count))
                {
                    string msg = null;
                    if ((msg = Validate(this._acls)) != null)
                    {
                        // Log the error, but allow the operation to continue, because acl editor
                        // may contain many different operations that we do not want to lose.
                        SnLog.WriteWarning("Invalid ACL: " + msg, EventId.Security);
                    }

                    var relatedEntities = new List <int>();
                    // collect related aces
                    var originalAces = new List <string>();
                    // changed acls
                    foreach (var entityId in this._acls.Keys)
                    {
                        relatedEntities.Add(entityId);
                        originalAces.Add(AcesToString(entityId, this.Context.GetExplicitEntries(entityId)));
                    }
                    // breaks that are not in changed aces
                    foreach (var entityId in this._breaks)
                    {
                        if (!this._acls.ContainsKey(entityId))
                        {
                            relatedEntities.Add(entityId);
                            originalAces.Add(AcesToString(entityId, this.Context.GetExplicitEntries(entityId)));
                        }
                    }
                    // unbreaks that are not in changed aces
                    foreach (var entityId in this._unbreaks)
                    {
                        if (!this._acls.ContainsKey(entityId))
                        {
                            relatedEntities.Add(entityId);
                            originalAces.Add(AcesToString(entityId, this.Context.GetExplicitEntries(entityId)));
                        }
                    }

                    var relatedNodeHeads = relatedEntities.Select(NodeHead.Get).ToArray();
                    var changedData      = new[] { new ChangedData {
                                                       Name = "SetPermissions", Original = originalAces
                                                   } };

                    // fire "before" event
                    var args1 = new CancellablePermissionChangingEventArgs(relatedNodeHeads, changedData);
                    using (var op1 = SnTrace.Security.StartOperation("AclEditor.Apply / FireOnPermissionChanging"))
                    {
                        NodeObserver.FireOnPermissionChanging(null, null, args1, disabledObservers);
                        if (args1.Cancel)
                        {
                            throw new CancelNodeEventException(args1.CancelMessage, args1.EventType, null);
                        }
                        op1.Successful = true;
                    }

                    var customData = args1.CustomData;

                    // main operation
                    base.Apply();

                    // collect new values
                    changedData[0].Value = relatedEntities.Select(x => AcesToString(x, this.Context.GetExplicitEntries(x))).ToList();

                    // fire "after" event
                    var args2 = new PermissionChangedEventArgs(relatedNodeHeads, customData, changedData);
                    using (var op2 = SnTrace.Security.StartOperation("AclEditor.Apply / FireOnPermissionChanged"))
                    {
                        NodeObserver.FireOnPermissionChanged(null, null, args2, disabledObservers);
                        op2.Successful = true;
                    }

                    // iterate through all edited entities and log changes one by one
                    for (var i = 0; i < relatedEntities.Count; i++)
                    {
                        var entity = relatedNodeHeads[i];
                        SnLog.WriteAudit(AuditEvent.PermissionChanged, new Dictionary <string, object>
                        {
                            { "Id", entity != null ? entity.Id : 0 },
                            { "Path", entity != null ? entity.Path : string.Empty },
                            { "Type", changedData[0].Name },
                            { "OldAcl", (changedData[0].Original as List <string>)[i] }, // changed data lists are in the same order as relatedentities
                            { "NewAcl", (changedData[0].Value as List <string>)[i] }
                        });
                    }
                    op.Successful = true;
                }
                audit.Successful = true;
            }
        }
Пример #19
0
            protected override async T.Task FireNodeObserverEventAsync(INodeObserverEvent snEvent, NodeObserver nodeObserver,
                                                                       CancellationToken cancel = default)
            {
                using (var op = SnTrace.Test.StartOperation(
                           $"NodeObserverAction simulation: {nodeObserver.GetType().Name} {snEvent.GetType().Name}"))
                {
                    await T.Task.Delay(20, cancel).ConfigureAwait(false);

                    op.Successful = true;
                }

                await base.FireNodeObserverEventAsync(snEvent, nodeObserver, cancel);
            }