Skip to content

is_local = 0 in system.clusters when default_database is used #46277

@Feder1co5oave

Description

@Feder1co5oave

In table system.clusters, column is_local is always 0 when default_database is specified, even if the host resolves to a local address.

This is due to how this function is implemented:

inline bool isLocalImpl(const Cluster::Address & address, const Poco::Net::SocketAddress & resolved_address, UInt16 clickhouse_port)
{
/// If there is replica, for which:
/// - its port is the same that the server is listening;
/// - its host is resolved to set of addresses, one of which is the same as one of addresses of network interfaces of the server machine*;
/// then we must go to this shard without any inter-process communication.
///
/// * - this criteria is somewhat approximate.
///
/// Also, replica is considered non-local, if it has default database set
/// (only reason is to avoid query rewrite).
return address.default_database.empty() && isLocalAddress(resolved_address, clickhouse_port);
}

I was wondering if this is intended and the reason is still relevant today? "only reason is to avoid query rewrite".

How to reproduce
Create a cluster definition in <remote_servers> in config.xml with default_database. I use this to implement circular replication:

<remote_servers>
    <ch_cluster>
        <shard>
            <internal_replication>true</internal_replication>
            <replica>
                <host>ch-0.clickhouse</host>
                <port>9000</port>
                <default_database>stats_0</default_database>
            </replica>
            <replica>
                <host>ch-1.clickhouse</host>
                <port>9000</port>
                <default_database>stats_1</default_database>
            </replica>
        </shard>
        <shard>
            <internal_replication>true</internal_replication>
            <replica>
                <host>ch-1.clickhouse</host>
                <port>9000</port>
                <default_database>stats_0</default_database>
            </replica>
            <replica>
                <host>ch-2.clickhouse</host>
                <port>9000</port>
                <default_database>stats_1</default_database>
            </replica>
        </shard>
        <shard>
            <internal_replication>true</internal_replication>
            <replica>
                <host>ch-2.clickhouse</host>
                <port>9000</port>
                <default_database>stats_0</default_database>
            </replica>
            <replica>
                <host>ch-0.clickhouse</host>
                <port>9000</port>
                <default_database>stats_1</default_database>
            </replica>
        </shard>
    </ch_cluster>
</remote_servers>

Contents of system.clusters:

select host_address, is_local, default_database from system.clusters where cluster = 'ch_cluster'
┌─host_address─┬─is_local─┬─default_database─┐
│ 10.3.99.160  │        0 │ stats_0          │
│ 10.3.61.198  │        0 │ stats_1          │
│ 10.3.61.198  │        0 │ stats_0          │
│ 10.3.69.69   │        0 │ stats_1          │
│ 10.3.69.69   │        0 │ stats_0          │
│ 10.3.99.160  │        0 │ stats_1          │
└──────────────┴──────────┴──────────────────┘

I would expect that the entries for the host that I'm connected to have is_local = 1:

┌─host_address─┬─is_local─┬─default_database─┐
│ 10.3.99.160  │        1 │ stats_0          │
│ 10.3.61.198  │        0 │ stats_1          │
│ 10.3.61.198  │        0 │ stats_0          │
│ 10.3.69.69   │        0 │ stats_1          │
│ 10.3.69.69   │        0 │ stats_0          │
│ 10.3.99.160  │        1 │ stats_1          │
└──────────────┴──────────┴──────────────────┘

Metadata

Metadata

Assignees

No one assigned

    Labels

    unexpected behaviourResult is unexpected, but not entirely wrong at the same time.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions