Exemplo n.º 1
0
        public void Should_never_forget_replicas()
        {
            topology = ServiceTopology.Build(new[] { replica1 }, null);
            provider.GetCluster().Should().BeEquivalentTo(new[] { replica1 }.Cast <object>());

            topology = ServiceTopology.Build(new List <Uri>(), null);
            provider.GetCluster().Should().BeEquivalentTo(new[] { replica1 }.Cast <object>());

            topology = ServiceTopology.Build(new[] { replica2 }, null);
            provider.GetCluster().Should().BeEquivalentTo(new[] { replica1, replica2 }.Cast <object>());

            topology = ServiceTopology.Build(new[] { replica1, replica2 }, null);
            provider.GetCluster().Should().BeEquivalentTo(new[] { replica1, replica2 }.Cast <object>());

            topology = ServiceTopology.Build(new List <Uri>(), null);
            provider.GetCluster().Should().BeEquivalentTo(new[] { replica1, replica2 }.Cast <object>());

            topology = ServiceTopology.Build(new[] { replica1, replica2, replica3 }, null);
            provider.GetCluster().Should().BeEquivalentTo(new[] { replica1, replica2, replica3 }.Cast <object>());

            topology = ServiceTopology.Build(new[] { replica3 }, null);
            provider.GetCluster().Should().BeEquivalentTo(new[] { replica1, replica2, replica3 }.Cast <object>());

            topology = ServiceTopology.Build(new List <Uri>(), null);
            provider.GetCluster().Should().BeEquivalentTo(new[] { replica1, replica2, replica3 }.Cast <object>());
        }
Exemplo n.º 2
0
        public void Should_return_new_replicas_from_new_topology()
        {
            topology = ServiceTopology.Build(new[] { replica1, replica2 }, null);
            provider.GetCluster().Should().BeEquivalentTo(new[] { replica1, replica2 }.Cast <object>());

            topology = ServiceTopology.Build(new[] { replica2 }, null);
            provider.GetCluster().Should().BeEquivalentTo(new[] { replica2 }.Cast <object>());
        }
Exemplo n.º 3
0
        public IEnumerable <Uri> Transform(IServiceTopology topology)
        {
            foreach (var topologyReplica in topology.Replicas)
            {
                detectedReplicas[topologyReplica] = topologyReplica;
            }

            return(detectedReplicas.Values);
        }
Exemplo n.º 4
0
        public void Should_filter_some_replicas_when_they_has_no_tags()
        {
            var applicationInfo = new ApplicationInfo(environment, application, null);

            topology = ServiceTopology.Build(replicas, applicationInfo.Properties.SetPersistentReplicaTags(replica1.ToString(), new TagCollection {
                "tag1"
            }));
            filter.Filter(replicas, context).Should().BeEquivalentTo(replica1);
        }
Exemplo n.º 5
0
        public void Should_correct_merge_fqdn_and_nofqdn_replicas_and_replace_by_the_last_one_it_saw()
        {
            var r1 = new Uri("http://razr02:80");
            var r2 = new Uri("http://razr02.domain.whatever:80");

            topology = ServiceTopology.Build(new[] { r1 }, null);
            provider.GetCluster().Should().BeEquivalentTo(new[] { r1 }.Cast <object>());

            topology = ServiceTopology.Build(new[] { r2 }, null);
            provider.GetCluster().Should().BeEquivalentTo(new[] { r2 }.Cast <object>());
        }
        public void Should_return_last_seen_non_empty_replicas_set_and_aply_new_blacklist_that_was_added_after_moment_of_caching()
        {
            var aliveReplicas = new[] { replica1, replica2 };

            topology = ServiceTopology.Build(aliveReplicas, null);
            provider.GetCluster().Should().BeEquivalentTo(aliveReplicas.Cast <object>());


            topology = ServiceTopology.Build(new List <Uri>(), CreateApplicationInfoPropertiesWithBlackList(new[] { replica1 }));
            provider.GetCluster().Should().BeEquivalentTo(new[] { replica2 }.Cast <object>());
        }
        public void Should_return_last_seen_non_empty_replicas_set()
        {
            var aliveReplicas = new[] { replica1, replica2 };

            topology = ServiceTopology.Build(aliveReplicas, null);
            provider.GetCluster().Should().BeEquivalentTo(aliveReplicas.Cast <object>());


            topology = ServiceTopology.Build(new List <Uri>(), null);
            provider.GetCluster().Should().BeEquivalentTo(aliveReplicas.Cast <object>());
        }
        public IEnumerable <Uri> Transform(IServiceTopology topology)
        {
            var replicas = topology.Replicas;

            if (replicas.Count == 0 && lastSeenReplicas?.Count > 0)
            {
                log.Warn("New observed topology is empty, but we have a cached one. Last seen cached topology will be used.");
                return(lastSeenReplicas);
            }

            return(lastSeenReplicas = replicas);
        }
Exemplo n.º 9
0
        public void Should_filter_all_blacklisted_replicas()
        {
            blacklist = new[] { replica2, replica1 };

            var applicationInfo = new ApplicationInfo(environment, application, null);

            topology = ServiceTopology.Build(
                new[] { replica1, replica2 },
                applicationInfo.Properties.SetBlacklist(blacklist));

            provider.GetCluster().Should().BeEmpty();
        }
Exemplo n.º 10
0
        public void SetUp()
        {
            environment = "environment";
            application = "application";
            replicas    = new List <Uri> {
                replica1, replica2
            };
            topology = ServiceTopology.Build(replicas, null);

            serviceLocator = Substitute.For <IServiceLocator>();
            serviceLocator.Locate(environment, application).Returns(_ => topology);

            filter = new ServiceDiscoveryReplicasFilter(serviceLocator, environment, application, log);

            context = new FakeContext(new RequestParameters().SetTagsFilter(collection => collection.ContainsKey("tag1")));
        }
Exemplo n.º 11
0
        public void Should_override_replicaMatchesFunc_from_constructor_when_defined_in_request()
        {
            var applicationInfo = new ApplicationInfo(environment, application, null);

            topology = ServiceTopology.Build(replicas, applicationInfo.Properties.SetPersistentReplicaTags(replica1.ToString(), new TagCollection {
                "tag1"
            }));
            var filter1  = new ServiceDiscoveryReplicasFilter(serviceLocator, environment, application, log, collection => collection.ContainsKey("tag1"));
            var context1 = new FakeContext(new RequestParameters());

            filter1.Filter(replicas, context1).Should().BeEquivalentTo(replica1);

            var context2 = new FakeContext(new RequestParameters().SetTagsFilter(collection => collection.ContainsKey("tag2")));

            filter1.Filter(replicas, context2).Should().BeEmpty();
        }
        private ReplicaWeights ExtractWeights([CanBeNull] IServiceTopology topology)
        {
            if (topology?.Properties == null)
            {
                return(null);
            }

            var weights = topology.Properties.GetReplicaWeights();

            if (weights != null)
            {
                LogWeights(weights);
            }

            return(weights);
        }
Exemplo n.º 13
0
        public void Should_filter_blacklisted_replicas_with_FQDN()
        {
            var r1 = new Uri("http://razr01:80");
            var r2 = new Uri("http://razr02:80");
            var r3 = new Uri("http://razr03:80");
            var r4 = new Uri("http://razr02.domain.whatever:80");

            blacklist = new[] { r4 };

            var applicationInfo = new ApplicationInfo(environment, application, null);

            topology = ServiceTopology.Build(
                new[] { r1, r2, r3, r4 },
                applicationInfo.Properties.SetBlacklist(blacklist));

            provider.GetCluster().Should().Equal(r1, r3);
        }
Exemplo n.º 14
0
        public void Should_filter_replicas_with_FQDN()
        {
            var replica1Fqdn = new Uri("http://replica1.domain.my:123/v1/");
            var replica2Fqdn = new Uri("http://replica2.domain.my:456/v2/");

            var applicationInfo = new ApplicationInfo(environment, application, null);

            topology = ServiceTopology.Build(
                new [] { replica1, replica2Fqdn, replica3 },
                applicationInfo.Properties
                .SetPersistentReplicaTags(replica1.ToString(), new TagCollection {
                "tag1"
            })
                .SetPersistentReplicaTags(replica2Fqdn.ToString(), new TagCollection {
                "tag1"
            }));
            filter.Filter(new List <Uri> {
                replica1Fqdn, replica2, replica3
            }, context).Should().BeEquivalentTo(replica1Fqdn, replica2);
        }
Exemplo n.º 15
0
        public void Should_filter_some_replicas_when_they_has_no_tags_and_filter_was_set_in_constructor()
        {
            var applicationInfo = new ApplicationInfo(environment, application, null);

            topology = ServiceTopology.Build(replicas, applicationInfo.Properties.SetPersistentReplicaTags(replica1.ToString(), new TagCollection {
                "tag1"
            }));
            var filter1    = new ServiceDiscoveryReplicasFilter(serviceLocator, environment, application, log, collection => collection.ContainsKey("tag1"));
            var newContext = new FakeContext(new RequestParameters());

            filter1.Filter(replicas, newContext).Should().BeEquivalentTo(replica1);

            var filter2 = new ServiceDiscoveryReplicasFilter(serviceLocator, environment, application, log, new ContainsTagFilter("tag1"));

            filter2.Filter(replicas, newContext).Should().BeEquivalentTo(replica1);

            var filter3 = new ServiceDiscoveryReplicasFilter(serviceLocator, environment, application, log, TagFilterExpressionHelpers.Parse("tag1"));

            filter3.Filter(replicas, newContext).Should().BeEquivalentTo(replica1);
        }
Exemplo n.º 16
0
        public void Should_apply_bl_only_after_restoring_all_replicas()
        {
            topology = ServiceTopology.Build(new[] { replica1 }, CreateApplicationInfoPropertiesWithBlackList(new[] { replica1 }));
            provider.GetCluster().Should().BeEmpty();

            topology = ServiceTopology.Build(new List <Uri>(), null);
            provider.GetCluster().Should().BeEquivalentTo(new[] { replica1 }.Cast <object>());

            topology = ServiceTopology.Build(new[] { replica2 }, CreateApplicationInfoPropertiesWithBlackList(new[] { replica1 }));
            provider.GetCluster().Should().BeEquivalentTo(new[] { replica2 }.Cast <object>());

            topology = ServiceTopology.Build(new[] { replica1, replica2 }, null);
            provider.GetCluster().Should().BeEquivalentTo(new[] { replica1, replica2 }.Cast <object>());

            topology = ServiceTopology.Build(new List <Uri>(), CreateApplicationInfoPropertiesWithBlackList(new[] { replica1, replica2 }));
            provider.GetCluster().Should().BeEmpty();

            topology = ServiceTopology.Build(new List <Uri>(), null);
            provider.GetCluster().Should().BeEquivalentTo(new[] { replica1, replica2 }.Cast <object>());
        }
Exemplo n.º 17
0
        private Uri[] ParseReplicas([CanBeNull] IServiceTopology topology)
        {
            if (topology == null)
            {
                LogTopologyNotFound();
                return(null);
            }

            var replicas = (settings.ServiceTopologyTransform?.Transform(topology) ?? topology.Replicas)
                           .Except(topology.Properties.GetBlacklist(), ReplicaComparer.Instance)
                           .ToArray();

            if (!ReplicaListComparer.Equals(resolvedReplicas, replicas))
            {
                LogResolvedReplicas(replicas);
                resolvedReplicas = replicas;
            }

            return(replicas);
        }
Exemplo n.º 18
0
        public void Should_filter_replicas_that_does_not_exists_in_given_replicas()
        {
            var applicationInfo = new ApplicationInfo(environment, application, null);

            topology = ServiceTopology.Build(
                new [] { replica1, replica2, replica3 },
                applicationInfo.Properties
                .SetPersistentReplicaTags(replica1.ToString(), new TagCollection {
                "tag1"
            })
                .SetPersistentReplicaTags(replica2.ToString(), new TagCollection {
                "tag1"
            })
                .SetPersistentReplicaTags(replica3.ToString(), new TagCollection {
                "tag1"
            }));

            filter.Filter(new List <Uri> {
                replica1, replica2
            }, context).Should().BeEquivalentTo(replica1, replica2);
        }
Exemplo n.º 19
0
        public void Should_ignore_duplicate_replica_name_in_service_tags()
        {
            var replicaNoFqdn = new Uri("http://replica1:123/v1/");
            var replicaFqdn   = new Uri("http://replica1.domain.my:123/v1/");

            var applicationInfo = new ApplicationInfo(environment, application, null);

            topology = ServiceTopology.Build(
                new [] { replicaFqdn },
                applicationInfo.Properties
                .SetPersistentReplicaTags(replicaNoFqdn.ToString(), new TagCollection {
                "tag2"
            })
                .SetPersistentReplicaTags(replicaFqdn.ToString(), new TagCollection {
                "tag1"
            }));

            filter.Filter(new List <Uri> {
                replicaFqdn
            }, context).Should().BeEquivalentTo(replicaFqdn);
            filter.Filter(new List <Uri> {
                replicaNoFqdn
            }, context).Should().BeEquivalentTo(replicaNoFqdn);
        }
Exemplo n.º 20
0
        private IServiceTopology LocateInner(string environmentName, string applicationName)
        {
            var currentEnvironmentName = environmentName;

            // Note(kungurtsev): not return null, if application was found in some skipped environment.
            IServiceTopology firstResolved = null;

            for (var depth = 0; depth < settings.MaximumEnvironmentsDepth; depth++)
            {
                var environment = environmentsStorage.Get(currentEnvironmentName);
                if (environment == null)
                {
                    return(firstResolved);
                }

                var topology = applicationsStorage.Get(currentEnvironmentName, applicationName).ServiceTopology;
                firstResolved = firstResolved ?? topology;

                var parentEnvironment = environment.ParentEnvironment;
                if (parentEnvironment == null)
                {
                    return(topology ?? firstResolved);
                }

                var goToParent = topology == null || topology.Replicas.Count == 0 && environment.SkipIfEmpty();
                if (!goToParent)
                {
                    return(topology);
                }

                currentEnvironmentName = parentEnvironment;
            }

            log.Warn("Cycled when resolving '{Application}' application in '{Environment}'.", applicationName, environmentName);
            return(firstResolved);
        }
Exemplo n.º 21
0
 public void Should_return_null_for_null_topology()
 {
     topology = null;
     provider.GetCluster().Should().BeNull();
 }
Exemplo n.º 22
0
 public void Should_return_given_replicas_for_null_topology()
 {
     topology = null;
     filter.Filter(replicas, context).Should().BeEquivalentTo(replicas);
 }
Exemplo n.º 23
0
        public void Should_return_empty_list_for_empty_replicas()
        {
            topology = ServiceTopology.Build(new List <Uri>(), null);

            provider.GetCluster().Should().BeEmpty();
        }