Beispiel #1
0
        public void GetOrCreateCluster_should_return_a_cluster_with_the_correct_settings()
        {
            var credentials = new[] { MongoCredential.CreateMongoCRCredential("source", "username", "password") };
            var servers     = new[] { new MongoServerAddress("localhost"), new MongoServerAddress("127.0.0.1", 30000), new MongoServerAddress("[::1]", 27018) };

            var sslSettings = new SslSettings
            {
                CheckCertificateRevocation = true,
                EnabledSslProtocols        = SslProtocols.Tls
            };

            var clientSettings = new MongoClientSettings
            {
                ApplicationName    = "app1",
                ConnectionMode     = ConnectionMode.ReplicaSet,
                ConnectTimeout     = TimeSpan.FromSeconds(1),
                Credentials        = credentials,
                GuidRepresentation = GuidRepresentation.Standard,
                HeartbeatInterval  = TimeSpan.FromSeconds(7),
                HeartbeatTimeout   = TimeSpan.FromSeconds(8),
                IPv6 = true,
                MaxConnectionIdleTime = TimeSpan.FromSeconds(2),
                MaxConnectionLifeTime = TimeSpan.FromSeconds(3),
                MaxConnectionPoolSize = 10,
                MinConnectionPoolSize = 5,
                ReplicaSetName        = "rs",
                LocalThreshold        = TimeSpan.FromMilliseconds(20),
                Servers = servers,
                ServerSelectionTimeout = TimeSpan.FromSeconds(5),
                SocketTimeout          = TimeSpan.FromSeconds(4),
                SslSettings            = sslSettings,
                UseSsl = true,
                VerifySslCertificate = true,
                WaitQueueSize        = 20,
                WaitQueueTimeout     = TimeSpan.FromSeconds(6)
            };

            var subject = new ClusterRegistry();

            using (var cluster = subject.GetOrCreateCluster(clientSettings.ToClusterKey()))
            {
                var endPoints = new EndPoint[]
                {
                    new DnsEndPoint("localhost", 27017),
                    new IPEndPoint(IPAddress.Parse("127.0.0.1"), 30000),
                    new IPEndPoint(IPAddress.Parse("[::1]"), 27018)
                };
                cluster.Settings.ConnectionMode.Should().Be(ClusterConnectionMode.ReplicaSet);
                cluster.Settings.EndPoints.Equals(endPoints);
                cluster.Settings.ReplicaSetName.Should().Be("rs");
                cluster.Settings.ServerSelectionTimeout.Should().Be(clientSettings.ServerSelectionTimeout);
                cluster.Settings.PostServerSelector.Should().NotBeNull().And.Subject.Should().BeOfType <LatencyLimitingServerSelector>();
                cluster.Settings.MaxServerSelectionWaitQueueSize.Should().Be(20);

                cluster.Description.Servers.Select(s => s.EndPoint).Should().Contain(endPoints);

                // TODO: don't know how to test the rest of the settings because they are all private to the cluster
            }
        }
Beispiel #2
0
        public void UnregisterAndDisposeCluster_should_unregister_and_dispose_the_cluster()
        {
            var subject    = new ClusterRegistry();
            var settings   = new MongoClientSettings();
            var clusterKey = settings.ToClusterKey();
            var cluster    = subject.GetOrCreateCluster(clusterKey);

            subject.UnregisterAndDisposeCluster(cluster);

            subject._registry().Count.Should().Be(0);
            cluster._state().Should().Be(2);
        }
        public void GetOrCreateCluster_should_return_a_cluster_with_the_correct_settings()
        {
            var credentials = new[] { MongoCredential.CreateMongoCRCredential("source", "username", "password") };
            var servers = new[] { new MongoServerAddress("localhost") };

            var certificates = new[] { new X509Certificate() };
            var sslSettings = new SslSettings
            {
                CheckCertificateRevocation = true,
                ClientCertificates = certificates,
                EnabledSslProtocols = SslProtocols.Tls12
            };

            var clientSettings = new MongoClientSettings
            {
                ConnectionMode = ConnectionMode.ReplicaSet,
                ConnectTimeout = TimeSpan.FromSeconds(1),
                Credentials = credentials,
                GuidRepresentation = GuidRepresentation.Standard,
                IPv6 = true,
                MaxConnectionIdleTime = TimeSpan.FromSeconds(2),
                MaxConnectionLifeTime = TimeSpan.FromSeconds(3),
                MaxConnectionPoolSize = 10,
                MinConnectionPoolSize = 5,
                ReplicaSetName = "rs",
                Servers = servers,
                SocketTimeout = TimeSpan.FromSeconds(4),
                SslSettings = sslSettings,
                UseSsl = true,
                VerifySslCertificate = true,
                WaitQueueSize = 20,
                WaitQueueTimeout = TimeSpan.FromSeconds(5)
            };

            var subject = new ClusterRegistry();

            using (var cluster = subject.GetOrCreateCluster(clientSettings))
            {
                var address = clientSettings.Servers.Single();
                var endPoints = new[] { new DnsEndPoint(address.Host, address.Port) };
                cluster.Settings.ConnectionMode.Should().Be(ClusterConnectionMode.ReplicaSet);
                cluster.Settings.EndPoints.Equals(endPoints);
                cluster.Settings.ReplicaSetName.Should().Be("rs");

                var serverDescription = cluster.Description.Servers.Single(s => s.EndPoint.Equals(endPoints[0]));
                serverDescription.EndPoint.Should().Be(endPoints[0]);

                // TODO: don't know how to test the rest of the settings because they are all private to the cluster
            }
        }
Beispiel #4
0
 public static Dictionary <ClusterKey, ICluster> _registry(this ClusterRegistry clusterRegistry) => (Dictionary <ClusterKey, ICluster>)Reflector.GetFieldValue(clusterRegistry, nameof(_registry));
        public void GetOrCreateCluster_should_return_a_different_cluster_if_client_settings_are_not_equal()
        {
            var clientSettings1 = new MongoClientSettings();
            var clientSettings2 = new MongoClientSettings() { IPv6 = true };

            var subject = new ClusterRegistry();

            using (var cluster1 = subject.GetOrCreateCluster(clientSettings1))
            using (var cluster2 = subject.GetOrCreateCluster(clientSettings2))
            {
                cluster2.Should().NotBeSameAs(cluster1);
            }
        }
        public void GetOrCreateCluster_should_return_the_same_cluster_if_client_settings_are_equal()
        {
            var clientSettings1 = new MongoClientSettings();
            var clientSettings2 = new MongoClientSettings();

            var subject = new ClusterRegistry();

            using (var cluster1 = subject.GetOrCreateCluster(clientSettings1))
            using (var cluster2 = subject.GetOrCreateCluster(clientSettings2))
            {
                cluster2.Should().BeSameAs(cluster1);
            }
        }
Beispiel #7
0
        public static Dictionary <ClusterKey, ICluster> _registry(this ClusterRegistry clusterRegistry)
        {
            var fieldInfo = typeof(ClusterRegistry).GetField("_registry", BindingFlags.NonPublic | BindingFlags.Instance);

            return((Dictionary <ClusterKey, ICluster>)fieldInfo.GetValue(clusterRegistry));
        }
Beispiel #8
0
        internal void PreMountCopy(Guid mdbGuid, bool isPublicMdb)
        {
            RegistryReplicator registryReplicator = null;
            RealRegistry       realRegistry       = null;
            ClusterRegistry    clusterRegistry    = null;
            AmConfig           config             = AmSystemManager.Instance.Config;

            if (config.IsPamOrSam)
            {
                string text  = isPublicMdb ? RegistryMonitor.publicString : RegistryMonitor.privateString;
                string text2 = string.Format(RegistryMonitor.localRegistryPathFormat, Environment.MachineName, mdbGuid, text);
                string root  = string.Format(RegistryMonitor.clusterRegistryPathFormat, mdbGuid, text);
                try
                {
                    if (this.replicators.ContainsKey(mdbGuid))
                    {
                        this.replicators[mdbGuid].DisableCopy();
                    }
                    SafeRegistryHandle handle;
                    DiagnosticsNativeMethods.ErrorCode errorCode = DiagnosticsNativeMethods.RegOpenKeyEx(SafeRegistryHandle.LocalMachine, text2, 0, 131097, out handle);
                    if (DiagnosticsNativeMethods.ErrorCode.FileNotFound == errorCode)
                    {
                        RegistryKey registryKey = Registry.LocalMachine.CreateSubKey(text2);
                        if (registryKey != null)
                        {
                            registryKey.Close();
                            errorCode = DiagnosticsNativeMethods.RegOpenKeyEx(SafeRegistryHandle.LocalMachine, text2, 0, 131097, out handle);
                        }
                    }
                    if (errorCode != DiagnosticsNativeMethods.ErrorCode.Success)
                    {
                        throw new AmRegistryException("RegOpenKeyEx", new Win32Exception((int)errorCode));
                    }
                    realRegistry       = new RealRegistry(text2, handle);
                    clusterRegistry    = new ClusterRegistry(root, config.DagConfig.Cluster.Handle);
                    registryReplicator = new RegistryReplicator(realRegistry, clusterRegistry, null);
                    realRegistry       = null;
                    clusterRegistry    = null;
                    if (!registryReplicator.IsInitialReplication)
                    {
                        registryReplicator.InverseCopy();
                    }
                }
                catch (ClusterException innerException)
                {
                    throw new AmDbMountNotAllowedDueToRegistryConfigurationException(innerException);
                }
                catch (AmRegistryException innerException2)
                {
                    throw new AmDbMountNotAllowedDueToRegistryConfigurationException(innerException2);
                }
                finally
                {
                    if (registryReplicator != null)
                    {
                        registryReplicator.Dispose();
                    }
                    if (realRegistry != null)
                    {
                        realRegistry.Dispose();
                    }
                    if (clusterRegistry != null)
                    {
                        clusterRegistry.Dispose();
                    }
                    if (this.replicators.ContainsKey(mdbGuid))
                    {
                        this.replicators[mdbGuid].EnableCopy();
                    }
                }
            }
        }
Beispiel #9
0
        private void MonitorRegistry()
        {
            int      num      = 0;
            int      num2     = RegistryParameters.RegistryMonitorPollingIntervalInSec / 5;
            AmConfig amConfig = null;

            Dictionary <Guid, RegistryReplicator> .KeyCollection.Enumerator enumerator = this.replicators.Keys.GetEnumerator();
            try
            {
                bool flag = false;
                RegistryReplicator registryReplicator = null;
                RealRegistry       realRegistry       = null;
                ClusterRegistry    clusterRegistry    = null;
                List <Guid>        list  = new List <Guid>();
                List <WaitHandle>  list2 = new List <WaitHandle>();
                while (!this.stopEvent.WaitOne(5000, false))
                {
                    amConfig = AmSystemManager.Instance.Config;
                    while (!this.stopEvent.WaitOne(0, false) && amConfig.IsPamOrSam)
                    {
                        AmClusterHandle handle = amConfig.DagConfig.Cluster.Handle;
                        if (flag)
                        {
                            enumerator.Dispose();
                            enumerator = this.replicators.Keys.GetEnumerator();
                        }
                        int num3 = 0;
                        list2.Clear();
                        list.Clear();
                        while (num3 < 64 && enumerator.MoveNext())
                        {
                            if (this.replicators[enumerator.Current].IsValid)
                            {
                                list2.Add(this.replicators[enumerator.Current].KeyChanged);
                                list.Add(enumerator.Current);
                                num3++;
                            }
                        }
                        flag = (num3 < 64);
                        if (list2.Count <= 0)
                        {
                            goto IL_205;
                        }
                        int num4 = WaitHandle.WaitAny(list2.ToArray(), 5000, false);
                        if (num4 >= 0 && num4 < list2.Count)
                        {
                            Guid key = list[num4];
                            if (this.replicators[key].IsCopyEnabled)
                            {
                                lock (this.workQueue)
                                {
                                    this.workQueue.Enqueue(this.replicators[key]);
                                    this.replicators[key].KeyChanged.Reset();
                                }
                                try
                                {
                                    DiagnosticsNativeMethods.ErrorCode errorCode = DiagnosticsNativeMethods.RegNotifyChangeKeyValue((SafeRegistryHandle)this.replicators[key].Handle, true, DiagnosticsNativeMethods.RegistryNotifications.ChangeName | DiagnosticsNativeMethods.RegistryNotifications.ChangeAttributes | DiagnosticsNativeMethods.RegistryNotifications.LastSet, this.replicators[key].KeyChanged.SafeWaitHandle, true);
                                    if (errorCode != DiagnosticsNativeMethods.ErrorCode.Success)
                                    {
                                        throw new AmRegistryException("RegNotifyChangeKeyValue", new Win32Exception((int)errorCode));
                                    }
                                    goto IL_21B;
                                }
                                catch (AmRegistryException ex)
                                {
                                    AmTrace.Warning("Registering for registry key change notifications failed. Inner Exception: {0}", new object[]
                                    {
                                        ex
                                    });
                                    goto IL_21B;
                                }
                                goto IL_205;
                            }
                        }
IL_21B:
                        if (num == 0 || this.dbMountedEvent.WaitOne(0, false))
                        {
                            num2 = RegistryParameters.RegistryMonitorPollingIntervalInSec / 5;
                            foreach (KeyValuePair <Guid, RegistryReplicator> keyValuePair in this.replicators)
                            {
                                keyValuePair.Value.SetMarkedForRemoval();
                            }
                            IADDatabase[] databasesToMonitor = this.GetDatabasesToMonitor();
                            if (databasesToMonitor != null)
                            {
                                foreach (IADDatabase iaddatabase in databasesToMonitor)
                                {
                                    if (this.replicators.ContainsKey(iaddatabase.Guid))
                                    {
                                        this.replicators[iaddatabase.Guid].ResetMarkedForRemoval();
                                    }
                                    else
                                    {
                                        Exception ex2 = null;
                                        try
                                        {
                                            if (!flag)
                                            {
                                                enumerator.Dispose();
                                                flag = true;
                                            }
                                            string             text             = iaddatabase.IsPublicFolderDatabase ? RegistryMonitor.publicString : RegistryMonitor.privateString;
                                            string             text2            = string.Format(RegistryMonitor.localRegistryPathFormat, Environment.MachineName, iaddatabase.Guid, text);
                                            string             root             = string.Format(RegistryMonitor.clusterRegistryPathFormat, iaddatabase.Guid, text);
                                            ManualResetEvent   manualResetEvent = new ManualResetEvent(false);
                                            SafeRegistryHandle safeRegistryHandle;
                                            DiagnosticsNativeMethods.ErrorCode errorCode = DiagnosticsNativeMethods.RegOpenKeyEx(SafeRegistryHandle.LocalMachine, text2, 0, 131097, out safeRegistryHandle);
                                            if (DiagnosticsNativeMethods.ErrorCode.FileNotFound == errorCode)
                                            {
                                                RegistryKey registryKey = Registry.LocalMachine.CreateSubKey(text2);
                                                if (registryKey != null)
                                                {
                                                    registryKey.Close();
                                                    errorCode = DiagnosticsNativeMethods.RegOpenKeyEx(SafeRegistryHandle.LocalMachine, text2, 0, 131097, out safeRegistryHandle);
                                                }
                                            }
                                            if (errorCode != DiagnosticsNativeMethods.ErrorCode.Success)
                                            {
                                                throw new AmRegistryException("RegOpenKeyEx", new Win32Exception((int)errorCode));
                                            }
                                            realRegistry       = new RealRegistry(text2, safeRegistryHandle);
                                            clusterRegistry    = new ClusterRegistry(root, amConfig.DagConfig.Cluster.Handle);
                                            registryReplicator = new RegistryReplicator(realRegistry, clusterRegistry, manualResetEvent);
                                            realRegistry       = null;
                                            clusterRegistry    = null;
                                            if (registryReplicator.IsInitialReplication)
                                            {
                                                registryReplicator.Copy(handle);
                                                registryReplicator.SetInitialReplication();
                                            }
                                            else
                                            {
                                                registryReplicator.InverseCopy();
                                            }
                                            errorCode = DiagnosticsNativeMethods.RegNotifyChangeKeyValue(safeRegistryHandle, true, DiagnosticsNativeMethods.RegistryNotifications.ChangeName | DiagnosticsNativeMethods.RegistryNotifications.ChangeAttributes | DiagnosticsNativeMethods.RegistryNotifications.LastSet, manualResetEvent.SafeWaitHandle, true);
                                            if (errorCode != DiagnosticsNativeMethods.ErrorCode.Success)
                                            {
                                                throw new AmRegistryException("RegNotifyChangeKeyValue", new Win32Exception((int)errorCode));
                                            }
                                            this.replicators.Add(iaddatabase.Guid, registryReplicator);
                                            registryReplicator = null;
                                        }
                                        catch (AmRegistryException ex3)
                                        {
                                            ex2 = ex3;
                                            AmTrace.Warning("Failed to add database to monitor: {0}. Exception: {1}", new object[]
                                            {
                                                iaddatabase.Name,
                                                ex3
                                            });
                                        }
                                        catch (ClusterException ex4)
                                        {
                                            ex2 = ex4;
                                            AmTrace.Warning("Failed to add database to monitor: {0}. Exception: {1}", new object[]
                                            {
                                                iaddatabase.Name,
                                                ex4
                                            });
                                        }
                                        finally
                                        {
                                            if (ex2 != null)
                                            {
                                                ReplayEventLogConstants.Tuple_RegistryReplicatorException.LogEvent(null, new object[]
                                                {
                                                    ex2
                                                });
                                            }
                                            if (registryReplicator != null)
                                            {
                                                registryReplicator.Dispose();
                                                registryReplicator = null;
                                            }
                                            if (realRegistry != null)
                                            {
                                                realRegistry.Dispose();
                                                realRegistry = null;
                                            }
                                            if (clusterRegistry != null)
                                            {
                                                clusterRegistry.Dispose();
                                                clusterRegistry = null;
                                            }
                                        }
                                    }
                                }
                            }
                            list.Clear();
                            foreach (KeyValuePair <Guid, RegistryReplicator> keyValuePair2 in this.replicators)
                            {
                                if (keyValuePair2.Value.IsMarkedForRemoval)
                                {
                                    list.Add(keyValuePair2.Key);
                                }
                            }
                            lock (this.workQueue)
                            {
                                foreach (Guid key2 in list)
                                {
                                    registryReplicator = this.replicators[key2];
                                    registryReplicator.SetInvalid();
                                    if (!registryReplicator.IsCopying)
                                    {
                                        if (!flag)
                                        {
                                            flag = true;
                                        }
                                        this.replicators.Remove(key2);
                                        registryReplicator.Dispose();
                                    }
                                }
                            }
                            registryReplicator = null;
                        }
                        num      = (num + 1) % num2;
                        amConfig = AmSystemManager.Instance.Config;
                        continue;
IL_205:
                        if (!this.stopEvent.WaitOne(5000, false))
                        {
                            goto IL_21B;
                        }
                        break;
                    }
                }
                foreach (KeyValuePair <Guid, RegistryReplicator> keyValuePair3 in this.replicators)
                {
                    if (!keyValuePair3.Value.IsCopying)
                    {
                        keyValuePair3.Value.SetInvalid();
                    }
                }
            }
            finally
            {
                enumerator.Dispose();
            }
        }