首页 / 教程列表 / Elasticsearch.NET/NEST中文文档(教程) / 在节点第一次被使用时Ping

在节点第一次被使用时Ping

897 发布于: 2021-03-25 读完约需 1 分钟

默认情况下,对于Static,SniffingSticky这几种连接池,Ping是启用状态的。这意味着,在节点在第一次被使用或恢复时,发出一个超时时间短的ping比尝试发出一个请求来将故障转移到健康节点要快得多,因为一个请求比ping笨重得多。

下面是一个带有两个节点的集群的例子,其中第二个节点ping会发生故障

var audit = new Auditor(() => VirtualClusterWith
    .Nodes(2)
    .Ping(p => p.Succeeds(Always))
    .Ping(p => p.OnPort(9201).FailAlways())
    .ClientCalls(c=>c.SucceedAlways())
    .StaticConnectionPool()
    .AllDefaults()
);

当发起调用时,第一次调用9200成功,第二次对9201进行Ping,因为9201是第一次被使用。Ping失败,所以随后会转到已经ping过的节点9200。

最后,可以断言连接池有一个被标记为dead的节点,即没有被Ping通的9201节点。

await audit.TraceCalls(

    new ClientCall {
        { PingSuccess, 9200},
        { HealthyResponse, 9200},
        { pool =>
        {
            pool.Nodes.Where(n=>!n.IsAlive).Should().HaveCount(0);
        } }
    },
    new ClientCall {
        { PingFailure, 9201},
        { HealthyResponse, 9200},
        { pool =>  pool.Nodes.Where(n=>!n.IsAlive).Should().HaveCount(1) }
    }
);

以下创建了具有4个节点的集群,其中第2个和第3个ping失败

var audit = new Auditor(() => VirtualClusterWith
    .Nodes(4)
    .Ping(p => p.SucceedAlways())
    .Ping(p => p.OnPort(9201).FailAlways())
    .Ping(p => p.OnPort(9202).FailAlways())
    .ClientCalls(c=>c.SucceedAlways())
    .StaticConnectionPool()
    .AllDefaults()
);

await audit.TraceCalls(
    new ClientCall {
        { PingSuccess, 9200}, // 第一个调用9200,并且调用成功
        { HealthyResponse, 9200},
        { pool =>
            pool.Nodes.Where(n=>!n.IsAlive).Should().HaveCount(0)
        }
    },
    new ClientCall {
        { PingFailure, 9201}, // 因为此节点是第一次使用,所以第二个调用会ping 9201,结果失败
        { PingFailure, 9202}, // 接着ping 9202,结果仍然失败
        { PingSuccess, 9203}, // 再接着ping 9203,结果成功(因为此节点还未被使用) 
        { HealthyResponse, 9203},
        { pool =>
            pool.Nodes.Where(n=>!n.IsAlive).Should().HaveCount(2) // 最后,断言连接池有2个节点被标记为了`dead`
        }
    }
);

所有节点在第一次使用时都能ping通,前提是它们是正常的

var audit = new Auditor(() => VirtualClusterWith
    .Nodes(4)
    .Ping(p => p.SucceedAlways()) 
    .ClientCalls(c=>c.SucceedAlways())
    .StaticConnectionPool()
    .AllDefaults()
);

await audit.TraceCalls(
    new ClientCall { { PingSuccess, 9200}, { HealthyResponse, 9200} }, 
    new ClientCall { { PingSuccess, 9201}, { HealthyResponse, 9201} },
    new ClientCall { { PingSuccess, 9202}, { HealthyResponse, 9202} },
    new ClientCall { { PingSuccess, 9203}, { HealthyResponse, 9203} },
    new ClientCall { { HealthyResponse, 9200} },
    new ClientCall { { HealthyResponse, 9201} },
    new ClientCall { { HealthyResponse, 9202} },
    new ClientCall { { HealthyResponse, 9203} },
    new ClientCall { { HealthyResponse, 9200} }
);

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

发表评论

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