public void DeserializeProperties_should_ignore_null_and_empty_values() { var dict = new Dictionary <string, string> { { "a", "a-value" }, { "b", null }, { "c", "" }, { "d", " " } }; var replicaInfo = new ReplicaInfo( "default", "vostok", "doesntmatter", dict); var serialized = ReplicaNodeDataSerializer.Serialize(replicaInfo); var deserialized = ReplicaNodeDataSerializer.Deserialize(replicaInfo.Environment, replicaInfo.Application, replicaInfo.Replica, serialized); deserialized.Properties.Should() .BeEquivalentTo( new Dictionary <string, string> { { "a", "a-value" }, { "d", " " } }); }
protected void CreateReplicaNode(ReplicaInfo replicaInfo, bool persistent = true) { var data = ReplicaNodeDataSerializer.Serialize(replicaInfo); var path = PathHelper.BuildReplicaPath(replicaInfo.Environment, replicaInfo.Application, replicaInfo.Replica); CreateOrUpdate(path, data, persistent); }
public async Task <bool> TryCreatePermanentReplicaAsync(IReplicaInfo replica) { var replicaPath = pathHelper.BuildReplicaPath(replica.Environment, replica.Application, replica.Replica); var createRequest = new CreateRequest(replicaPath, CreateMode.Persistent) { Data = ReplicaNodeDataSerializer.Serialize(replica) }; return((await zooKeeperClient.CreateAsync(createRequest).ConfigureAwait(false)).IsSuccessful); }
public async Task <IReplicaInfo> GetReplicaAsync(string environment, string application, string replica) { var data = await zooKeeperClient.GetDataAsync(new GetDataRequest(pathHelper.BuildReplicaPath(environment, application, replica))).ConfigureAwait(false); if (data.Status == ZooKeeperStatus.NodeNotFound) { return(null); } data.EnsureSuccess(); var envData = ReplicaNodeDataSerializer.Deserialize(environment, application, replica, data.Data); return(envData); }
public void Should_use_default_replica_builder() { CreateEnvironmentNode("default"); using (var beacon = new ServiceBeacon(ZooKeeperClient)) { beacon.Start(); beacon.WaitForInitialRegistrationAsync().ShouldCompleteIn(DefaultTimeout); var builder = ReplicaInfoBuilder.Build(null, true); var path = new ServiceDiscoveryPathHelper(new ServiceBeaconSettings().ZooKeeperNodesPrefix, ZooKeeperPathEscaper.Instance) .BuildReplicaPath(builder.Environment, builder.Application, builder.Replica); var data = ZooKeeperClient.GetData(path).Data; var dict = ReplicaNodeDataSerializer.Deserialize(builder.Environment, builder.Application, builder.Replica, data).Properties; dict[ReplicaInfoKeys.Application].Should().Be(builder.Application); } }
internal ServiceBeacon( [NotNull] IZooKeeperClient zooKeeperClient, [NotNull] ReplicaInfo replicaInfo, [CanBeNull] ServiceBeaconSettings settings, [CanBeNull] ILog log) { this.zooKeeperClient = zooKeeperClient ?? throw new ArgumentNullException(nameof(settings)); this.replicaInfo = replicaInfo = replicaInfo ?? throw new ArgumentNullException(nameof(settings)); this.settings = settings ?? new ServiceBeaconSettings(); this.log = (log ?? LogProvider.Get()).ForContext <ServiceBeacon>(); var pathHelper = new ServiceDiscoveryPathHelper(this.settings.ZooKeeperNodesPrefix, this.settings.ZooKeeperNodesPathEscaper); environmentNodePath = pathHelper.BuildEnvironmentPath(replicaInfo.Environment); applicationNodePath = pathHelper.BuildApplicationPath(replicaInfo.Environment, replicaInfo.Application); replicaNodePath = pathHelper.BuildReplicaPath(replicaInfo.Environment, replicaInfo.Application, replicaInfo.Replica); replicaNodeData = ReplicaNodeDataSerializer.Serialize(replicaInfo); nodeWatcher = new AdHocNodeWatcher(OnNodeEvent); }
public void Start_should_create_node_with_replica_properties() { var replica = new ReplicaInfo("default", "vostok", "https://github.com/vostok"); replica.SetProperty("key", "value"); CreateEnvironmentNode(replica.Environment); using (var beacon = GetServiceBeacon(replica)) { beacon.Start(); beacon.WaitForInitialRegistrationAsync().ShouldCompleteIn(DefaultTimeout); var path = new ServiceDiscoveryPathHelper(new ServiceBeaconSettings().ZooKeeperNodesPrefix, ZooKeeperPathEscaper.Instance) .BuildReplicaPath(replica.Environment, replica.Application, replica.Replica); var data = ZooKeeperClient.GetData(path).Data; var dict = ReplicaNodeDataSerializer.Deserialize(replica.Environment, replica.Application, replica.Replica, data).Properties; dict["key"].Should().Be("value"); } }
public void DeserializeProperties_should_deserialize_serialized() { var dict = new Dictionary <string, string> { { "a", "a-value" }, { "asdf", "complex = value" }, { "a.b.c", "dsfds sdf sdf sdf sd ,. ,ds . . , ., ;; ; ;" }, { "with some spaces ", " " } }; var replicaInfo = new ReplicaInfo( "default", "vostok", "doesntmatter", dict); var serialized = ReplicaNodeDataSerializer.Serialize(replicaInfo); var deserialized = ReplicaNodeDataSerializer.Deserialize(replicaInfo.Environment, replicaInfo.Application, replicaInfo.Replica, serialized); deserialized.Should().BeEquivalentTo(replicaInfo); }
public void Should_use_replica_builder() { var url = "https://github.com/vostok"; CreateEnvironmentNode("default"); using (var beacon = new ServiceBeacon( ZooKeeperClient, setup => setup.SetUrl(new Uri(url)).SetApplication("test"))) { beacon.Start(); beacon.WaitForInitialRegistrationAsync().ShouldCompleteIn(DefaultTimeout); var path = new ServiceDiscoveryPathHelper(new ServiceBeaconSettings().ZooKeeperNodesPrefix, ZooKeeperPathEscaper.Instance) .BuildReplicaPath("default", "test", url); var data = ZooKeeperClient.GetData(path).Data; var dict = ReplicaNodeDataSerializer.Deserialize("default", "test", url, data).Properties; dict[ReplicaInfoKeys.Replica].Should().Be(url); dict[ReplicaInfoKeys.Application].Should().Be("test"); } }
public void SerializeProperties_should_ignore_null_and_empty_values() { var replicaInfo = new ReplicaInfo( "default", "vostok", "doesntmatter", new Dictionary <string, string> { { "a", null }, { "b", "value" }, { "c", "" } }); var serialized = ReplicaNodeDataSerializer.Serialize(replicaInfo); var str = Encoding.UTF8.GetString(serialized); var expected = new List <string> { "b = value" }; str.Should().Be(string.Join("\n", expected)); }
public void SerializeProperties_should_concat_dict_key_values() { var replicaInfo = new ReplicaInfo( "default", "vostok", "doesntmatter", new Dictionary <string, string> { { "a", "a-value" }, { "asdf", "complex = value" } }); var serialized = ReplicaNodeDataSerializer.Serialize( replicaInfo ); var str = Encoding.UTF8.GetString(serialized); var expected = new List <string> { "a = a-value", "asdf = complex = value" }; str.Should().Be(string.Join("\n", expected)); }
public void DeserializeProperties_should_replace_new_line_symbol() { var dict = new Dictionary <string, string> { { "a", "a-value\n2" }, { "b", "b\n\nb" } }; var replicaInfo = new ReplicaInfo( "default", "vostok", "doesntmatter", dict); var serialized = ReplicaNodeDataSerializer.Serialize(replicaInfo); var deserialized = ReplicaNodeDataSerializer.Deserialize(replicaInfo.Environment, replicaInfo.Application, replicaInfo.Replica, serialized); deserialized.Properties.Should() .BeEquivalentTo( new Dictionary <string, string> { { "a", "a-value 2" }, { "b", "b b" } }); }