启动时嗅探

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

默认情况下,SupporsReseeding返回结果为true的连接池将在启动时进行嗅探。

下面通过使用NEST的测试集群框架创建一个虚拟Elasticsearch集群来演示这一点。

在这里,我们创建了一个使用嗅探连接池的10个节点的集群,除端口为9202的节点外,其它的节点嗅探均设置为失败:

var audit = new Auditor(() => VirtualClusterWith
    .Nodes(10)
    .Sniff(s => s.Fails(Always))
    .Sniff(s => s.OnPort(9202).Succeeds(Always))
    .Ping(c=>c.SucceedAlways())
    .ClientCalls(r => r.SucceedAlways())
    .SniffingConnectionPool()
    .AllDefaults()
);

当进行客户端调用时,从审计跟踪中我们看到,连接池首先尝试在启动时嗅探,在9200端口和9201端口的节点上均嗅探失败,然后在9202端口的节点上嗅探成功。然后在9200端口成功Ping并做出正常的响应,测试代码如下:

await audit.TraceCall(
     new ClientCall
     {
        { SniffOnStartup},
        { SniffFailure, 9200},
        { SniffFailure, 9201},
        { SniffSuccess, 9202},
        { PingSuccess , 9200},
        { HealthyResponse, 9200}
    }
 );

嗅探仅发生一次(Occurs once)

嗅探操作仅在启动时发生一次,也就是:

  • 只会尝试在一个节点上进行嗅探操作;
  • 如果这个节点嗅探失败,那么会尝试寻找下一个节点进行嗅探;
  • 如此循环,直到嗅探成功;
  • 启动时不会再发生嗅探。
var audit = new Auditor(() => VirtualClusterWith
    .Nodes(10)
    .Sniff(s => s.Fails(Always))
    .Sniff(s => s.OnPort(9202).Succeeds(Always))
    .ClientCalls(r => r.SucceedAlways())
    .Ping(c=>c.SucceedAlways())
    .SniffingConnectionPool()
    .AllDefaults()
);

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

使用集群状态(Uses cluster state)

启动时的嗅探将使用返回的集群状态。在接下来的一个示例中,从成功嗅探返回的集群状态将用于后续的客户端请求:

var audit = new Auditor(() => VirtualClusterWith
    .Nodes(10)
    .Sniff(s => s.Fails(Always))
    .Sniff(s => s.OnPort(9202).Succeeds(Always,
        VirtualClusterWith.Nodes(8, startFrom: 9204)
            .Ping(c=>c.SucceedAlways())
            .ClientCalls(c=>c.SucceedAlways())

        )) // 嗅探返回从9204开始的8个节点
    .ClientCalls(r => r.SucceedAlways())
    .Ping(c=>c.SucceedAlways())
    .SniffingConnectionPool()
    .AllDefaults()
);

await audit.TraceCall(new ClientCall {
    { SniffOnStartup},
    { SniffFailure, 9200},
    { SniffFailure, 9201},
    { SniffSuccess, 9202},
    { PingSuccess, 9204}, // 成功嗅探之后,ping现在发生在9204上
    { HealthyResponse, 9204}
});

优先使用主节点(Prefers master eligible nodes)

嗅探更倾向于在主节点上运行:

var audit = new Auditor(() => VirtualClusterWith
    .Nodes(new[] {
        new Node(new Uri("http://localhost:9200")) { MasterEligible = false },
        new Node(new Uri("http://localhost:9201")) { MasterEligible = false },
        new Node(new Uri("http://localhost:9202")) { MasterEligible = true },
    })
    .Sniff(s => s.Succeeds(Always))
    .Ping(s => s.Succeeds(Always))
    .ClientCalls(r => r.SucceedAlways())
    .SniffingConnectionPool()
    .AllDefaults()
);

await audit.TraceCall(new ClientCall {
    { SniffOnStartup},
    { SniffSuccess, 9202},
    { PingSuccess, 9200},
    { HealthyResponse, 9200}
});

然而,如果在符合主节点上嗅探失败时,它将嗅探操作转移到非主节点上:

var audit = new Auditor(() => VirtualClusterWith
    .Nodes(new[] {
        new Node(new Uri("http://localhost:9200")) { MasterEligible = true },
        new Node(new Uri("http://localhost:9201")) { MasterEligible = true },
        new Node(new Uri("http://localhost:9202")) { MasterEligible = false },
    })
    .Sniff(s => s.Fails(Always))
    .Sniff(s => s.OnPort(9202).Succeeds(Always))
    .Ping(c=>c.SucceedAlways())
    .ClientCalls(r => r.SucceedAlways())
    .SniffingConnectionPool()
    .AllDefaults()
);

await audit.TraceCall(new ClientCall {
    { SniffOnStartup},
    { SniffFailure, 9200},
    { SniffFailure, 9201},
    { SniffSuccess, 9202},
    { PingSuccess, 9200},
    { HealthyResponse, 9200}
});

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

发表评论

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