保持节点跟踪

796 发布于: 2021-03-25 读完约需 2 分钟

创建节点

Elasticsearch.Net或者NEST中,可以通过传入Uri参数来实例化节点,如下:

var node = new Node(new Uri("http://localhost:9200"));
node.Uri.Should().NotBeNull();
node.Uri.Port.Should().Be(9200);

默认情况下,实例化的节点既是master候选节点也是数据节点:

node.MasterEligible.Should().BeTrue();
node.HoldsData.Should().BeTrue();

构建节点路径

有时如果Elasticsearch的节点是一个代理路径,那么你可能需要传递带有路径的参数来创建节点,如下:

var node = new Node(new Uri("http://test.example/elasticsearch"));

node.Uri.Port.Should().Be(80);
node.Uri.AbsolutePath.Should().Be("/elasticsearch/");

Elasticsearch.NetNEST中,强制要求路径以正斜杠(/)结束,以便以后可以安全地组合完整的路径,比如:

var combinedPath = new Uri(node.Uri, "index/type/_search");
combinedPath.AbsolutePath.Should().Be("/elasticsearch/index/type/_search");

Node实例的CreatePath()方法的作用就是将路径组合在一起:

combinedPath = node.CreatePath("index/type/_search");
combinedPath.AbsolutePath.Should().Be("/elasticsearch/index/type/_search");

标记节点

var node = new Node(new Uri("http://localhost:9200"));
node.FailedAttempts.Should().Be(0);
node.IsAlive.Should().BeTrue();

每当一个节点被标记为挂掉(dead)状态时,尝试次数应该增加,并且日期时间参数应该被暴露出来,如下:

for(var i = 0; i<10;i++)
{
    var deadUntil = DateTime.Now.AddMinutes(1);
    node.MarkDead(deadUntil);
    node.FailedAttempts.Should().Be(i + 1);
    node.IsAlive.Should().BeFalse();
    node.DeadUntil.Should().Be(deadUntil);
}

然而,当标记一个节点为存活状态时,应该重置DeadUntil属性,并将FailedAttempts重置为0,如下:

node.MarkAlive();
node.FailedAttempts.Should().Be(0);
node.DeadUntil.Should().Be(default(DateTime));
node.IsAlive.Should().BeTrue();

节点相等性

如果节点具有相同的端点,则认为它们是相等的,而不管其他关联的元数据是什么,如:

var node = new Node(new Uri("http://localhost:9200")) { MasterEligible = false };
var nodeAsMaster = new Node(new Uri("http://localhost:9200")) { MasterEligible = true };

(node == nodeAsMaster).Should().BeTrue();
(node != nodeAsMaster).Should().BeFalse();

var uri = new Uri("http://localhost:9200");
(node == uri).Should().BeTrue();

var differentUri = new Uri("http://localhost:9201");
(node != differentUri).Should().BeTrue();

node.Should().Be(nodeAsMaster);

版权声明:本作品系原创,版权归码友网所有,如未经许可,禁止任何形式转载,违者必究。

发表评论

登录用户才能发表评论, 请 登 录 或者 注册