3

我编写了一些 PostgreSQL 数据库客户端代码来更新中央数据库,其中包含来自多个客户端的 IP 地址和主机名表。有两张表:一张保存 IP 地址和主机名之间的映射,一张保存尚未解析为主机名的 IP 地址队列。

这是 IP 地址到主机名的映射表:

CREATE TABLE g_hostmap(
    appliance_id     INTEGER,
    ip               INET,
    fqdn             TEXT,
    resolve_time     TIMESTAMP, 
    expire_time      TIMESTAMP,
    UNIQUE(appliance_id, ip))

这是工作队列表:

CREATE TABLE g_hostmap_work(
    ip               INET,
    input_table      TEXT)

数据库客户端每个都从单个工作队列表中拉取请求。每个请求都包含一个私有 IPv4 地址,为其请求主机名。

工作流程如下:每个客户端定期向中央数据库工作队列查询需要主机名的 IP 地址列表,对这些地址执行反向 DNS 查找,然后使用 ( IP 地址、主机名)对,一次一对。我希望通过尝试同时解析相同的 IP 地址来最大限度地减少多个客户端重复工作的可能性。

我将每批更新限制为 10 行或工作队列大小的 10%(以行为单位),以较大者为准。客户的时间有点独立。如何在更新过程中进一步减少 DNS 名称服务器和主机名表的争用?我的客户担心会有很多重复的工作。

这是对工作队列中项目计数的初始查询:

SELECT COUNT(*)
       FROM g_hostmap_work queued
       LEFT JOIN g_hostmap cached
            ON queued.ip = cached.ip
            AND now() < cached.expire_time

这是返回工作队列中项目子集的查询:

SELECT queued.ip, queued.input_table, cached.expire_time
       FROM g_hostmap_work queued
       LEFT JOIN g_hostmap cached
            ON queued.ip = cached.ip
            AND now() < cached.expire_time
       LIMIT 10

以下是使用新 IP 地址/主机名映射更新数据库的单个 INSERT 语句示例:

INSERT INTO g_hostmap_20131230 VALUES
(NULL, '192.168.54.133', 'powwow.site', now(), now() + 900 * INTERVAL '1 SECOND')
4

1 回答 1

1

我会提出一个听起来很奇怪的建议。在源表中添加一个 auto-inc big int,并创建一组 10 个带模除法的索引。这是一个简单的测试用例示例:

create table queue (id bigserial, input text);
create index q0 on queue (id) where id%10=0;
create index q1 on queue (id) where id%10=1;
create index q2 on queue (id) where id%10=2;
create index q3 on queue (id) where id%10=3;
create index q4 on queue (id) where id%10=4;
create index q5 on queue (id) where id%10=5;
create index q6 on queue (id) where id%10=6;
create index q7 on queue (id) where id%10=7;
create index q8 on queue (id) where id%10=8;
create index q9 on queue (id) where id%10=9;
insert into queue select generate_series(1,50000),'this';

我们在这里所做的是创建一组索引,索引表的 1/10。接下来,我们将选择其中一个范围的一部分进行处理:

begin;
select * from queue where id%10=0 limit 100 for update;
id  | input 
------+-------
10 | this
20 | this
30 | this
-- do work here --
commit;

现在有趣的部分。如果您有超过 10 名工人使用此设置,您只需循环他们的数字,当上述更新选择运行时,超过 10 名工人将等待。但任何其他数字(1 到 9)仍然有效。

begin;
select * from queue where id%10=1 limit 100 for update;
 id  | input 
-----+-------
   1 | this
  11 | this
  21 | this
  31 | this
-- do work here
commit;

这样所有的工作都被分成了 10 个桶。想要更多的桶?更改 % 后的数字并增加要匹配的索引数。

于 2013-12-31T01:28:46.213 回答