2.5. Link Aggregation and High Availability with Bonding

本文介绍Linux环境下通过链路聚合提高带宽及实现高可用性的方法。使用IEEE 802.3ad标准,即常见的trunking或port trunking技术,将多个物理连接聚合为单一逻辑链路。此外,还介绍了如何配置冗余连接以增强系统的高可用性和故障转移能力。

2.5. Link Aggregation and High Availability with Bonding

 

Networking vendors have long offered a functionality for aggregating bandwidth across multiple physical links to a switch. This allows a machine (frequently a server) to treat multiple physical connections to switch units as a single logical link. The standard moniker for this technology is IEEE 802.3ad, although it is known by the common names of trunking, port trunking and link aggregation. The conventional use of bonding under linux is an implementation of this link aggregation.

A separate use of the same driver allows the kernel to present a single logical interface for two physical links to two separate switches. Only one link is used at any given time. By using media independent interface signal failure to detect when a switch or link becomes unusable, the kernel can, transparently to userspace and application layer services, fail to the backup physical connection. Though not common, the failure of switches, network interfaces, and cables can cause outages. As a component of high availability planning, these bonding techniques can help reduce the number of single points of failure.

For more information on bonding, see the Documentation/networking/bonding.txt from the linux source code tree.

 

2.5.1. Link Aggregation

Bonding for link aggregation must be supported by both endpoints. Two linux machines connected via crossover cables can take advantage of link aggregation. A single machine connected with two physical cables to a switch which supports port trunking can use link aggregation to the switch. Any conventional switch will become ineffably confused by a hardware address appearing on multiple ports simultaneously.

Example 2.12. Link aggregation bonding

[root@real-server root]# modprobe  bonding
[root@real-server root]# ip addr add 192.168.100.33/24 brd + dev bond0
[root@real-server root]# ip link set dev bond0 up
[root@real-server root]# ifenslave  bond0 eth2 eth3
master has no hw address assigned; getting one from slave!
The interface eth2 is up, shutting it down it to enslave it.
The interface eth3 is up, shutting it down it to enslave it.
[root@real-server root]# ifenslave  bond0 eth2 eth3
[root@real-server root]# cat /proc/net/bond0/info
Bonding Mode: load balancing (round-robin)
MII Status: up
MII Polling Interval (ms): 0
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: eth2
MII Status: up
Link Failure Count: 0

Slave Interface: eth3
MII Status: up
Link Failure Count: 0
        

FIXME; Need an experiment here....maybe a tcpdump to show how the management frames appear on the wire.

This Beowulf software page describes in a bit more detail the rationale and a practical application of linux channel bonding (for link aggregation).

 

 

2.5.2. High Availability

Bonding support under linux is part of a high availability solution. For an entry point into the complexity of high availability in conjunction with linux, see the linux-ha.org site. To guard against layer two (switch) and layer one (cable) failure, a machine can be configured with multiple physical connections to separate switch devices while presenting a single logical interface to userspace.

The name of the interface can be specified by the user. It is commonly bond0 or something similar. As a logical interface, it can be used in routing tables and by tcpdump.

The bond interface, when created, has no link layer address. In the example below, an address is manually added to the interface. See Example 2.12, “Link aggregation bonding” for an example of the bonding driver reporting setting the link layer address when the first device is enslaved to the bond (doesn't that sound cruel!).

Example 2.13. High availability bonding

[root@real-server root]# modprobe bonding mode=1 miimon=100 downdelay=200 updelay=200
[root@real-server root]# ip link set dev bond0 addr 00:80:c8:e7:ab:5c
[root@real-server root]# ip addr add 192.168.100.33/24 brd + dev bond0
[root@real-server root]# ip link set dev bond0 up
[root@real-server root]# ifenslave  bond0 eth2 eth3
The interface eth2 is up, shutting it down it to enslave it.
The interface eth3 is up, shutting it down it to enslave it.
[root@real-server root]# ip link show eth2 ; ip link show eth3 ; ip link show bond0
4: eth2: <BROADCAST,MULTICAST,SLAVE,UP> mtu 1500 qdisc pfifo_fast master bond0 qlen 100
  link/ether 00:80:c8:e7:ab:5c brd ff:ff:ff:ff:ff:ff
5: eth3: <BROADCAST,MULTICAST,NOARP,SLAVE,DEBUG,AUTOMEDIA,PORTSEL,NOTRAILERS,UP> mtu 1500 qdisc pfifo_fast master bond0 qlen 100
  link/ether 00:80:c8:e7:ab:5c brd ff:ff:ff:ff:ff:ff
58: bond0: <BROADCAST,MULTICAST,MASTER,UP> mtu 1500 qdisc noqueue
  link/ether 00:80:c8:e7:ab:5c brd ff:ff:ff:ff:ff:ff
        

Immediately noticeable, there is a new flag in the ip link show output. The MASTER and SLAVE flags clearly report the nature of the relationship between the interfaces. Also, the Ethernet interfaces indicate the master interface via the keywords master bond0.

Note also, that all three of the interfaces share the same link layer address, 00:80:c8:e7:ab:5c.

FIXME; What doe DEBUG,AUTOMEDIA,PORTSEL,NOTRAILERS mean?

<think> 我们需要在原聚合查询基础上增加筛选条件:status等于零(注意:用户输入为"statuswe为零",根据上下文推断应为"status为零",可能是笔误) 原代码使用sample(1000)进行随机抽样,现在需要添加匹配条件(match)来过滤status=0的记录 步骤: 1. 在聚合管道中,首先使用$match阶段过滤出status=0的文档 2. 然后使用$sample阶段进行随机抽样 注意:由于先过滤后抽样,所以抽样的是满足条件(status=0)的文档中的1000条 修改后的聚合管道: Aggregation.newAggregation( Aggregation.match(Criteria.where("status").is(0)), // 添加匹配条件 Aggregation.sample(1000) // 然后随机取样1000条 ); 但请注意:如果满足条件的文档不足1000条,则返回所有文档(sample操作会返回可用文档数量) 代码实现: Query query = new Query(); query.limit(1000); // 这个query对象实际上在聚合中并没有使用,可以忽略或删除 因此,我们修改聚合部分的代码即可。 修改后的代码: org.springframework.data.mongodb.core.aggregation.Aggregation aggregation = org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation( org.springframework.data.mongodb.core.aggregation.Aggregation.match( // 添加match阶段 Criteria.where("status").is(0) ), org.springframework.data.mongodb.core.aggregation.Aggregation.sample(1000) ); return mongoTemplate.aggregate(aggregation, UserLogDoc.class, UserLogDoc.class) .getMappedResults(); 注意:需要导入Criteria类(org.springframework.data.mongodb.core.query.Criteria) 另外,原代码中的Query对象和limit(1000)并没有在聚合中使用,可以删除。 如果原代码中还有其他条件,需要整合到match阶段中。 由于用户只要求添加status为零的条件,所以按上述修改即可。 </think> 根据您的需求,需要对原有的随机抽样查询添加过滤条件 `status=0`。以下是修改后的代码实现: ```java Criteria criteria = Criteria.where("status").is(0); // 新增过滤条件 Aggregation aggregation = Aggregation.newAggregation( Aggregation.match(criteria), // 添加条件过滤阶段 Aggregation.sample(1000) // 随机抽样1000条 ); return mongoTemplate.aggregate(aggregation, UserLogDoc.class, UserLogDoc.class) .getMappedResults(); ``` ### 关键修改说明: 1. **新增过滤阶段**: - 使用 `Aggregation.match(Criteria.where("status").is(0))` 添加过滤条件 - 该阶段会筛选出所有 `status` 字段值为 `0` 的文档 2. **管道执行顺序**: ```mermaid graph LR A[原始集合] --> B[Match过滤 status=0] B --> C[Sample随机抽样1000条] C --> D[返回结果] ``` 3. **性能优化**: - 先过滤后抽样,大幅减少处理文档量 - 避免对无效文档进行抽样操作 4. **注意事项**: - 确保 `status` 字段在文档中存在且为数值类型 - 若过滤后文档不足1000条,将返回所有符合条件的文档 - 建议在 `status` 字段添加索引以提升过滤速度
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值