Linux 中的 Cgroup 机制

1. 检查 cgroup 的版本

1
2
3
4
5
# check if cgroup is supported 
cat /proc/filesystems | grep cgroup

# check cgroup version 
cat /proc/mounts | grep cgroup

2. cgroup v2 操作

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# create new dir
cd /sys/fs/cgroup
mkdir test

# creat loop.sh for testing cpu quota
vim loop.sh
while :
do
        :
done

# lunch loop.sh, generate pid -> 2584068
nohup sh loop.sh &

# echo pid to cgroup.procs
echo 2584068 > test/cgroup.procs

# set cpu, at lease 0.1
echo 1000 10000 > test/cpu.max

# check 
top 

# recovery all
kill 2584068
rmdir test

参考:

Mysql 间隙锁

1. 准备数据

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# create new schema
create schema test;
use test;

# create table test
create table user
(
    id   int primary key,
    age  int
);

alter table user
    add index age_idx (age);

# insert some test data
insert into user
values (3, 10),
       (5, 20),
       (8, 30);
       

2. 间隙锁测试

1. 使用主键索引,指定行存在

1
2
3
4
5
6
7
8
9
# session 1, the row is exist for id = 3 , so it doesn't lock.
begin;

select * from user where id = 3 for update;

# session 2, execute successful.
begin;

insert into user value (1, 20);

2. 使用主键索引,指定行不存在

1
2
3
4
5
6
7
8
9
# session 1, the row isn't exist for id = 2, so it locks range (,3]
begin;

select * from user where id = 2 for update;

# session 2, execute block.
begin;

insert into user value (1, 20);

3. 使用主键索引,范围查找

1
2
3
4
5
6
7
8
9
# session 1, it locks range [1,5] 
begin;

select * from user where id >= 1 and id <= 5 for update;

# session 2, execute block
begin;

insert into user value (2, 20);

4. 使用二级索引,指定行存在

1
2
3
4
5
6
7
8
9
# session 1, it locks range [3,8]
begin;

select * from user where age = 20 for update;

# session 2, execute block.
begin;

insert into user value (4, 20);

5. 使用二级索引,指定行不存在

1
2
3
4
5
6
7
8
9
# session 1, it locks range [3,5]
begin;

select * from user where age = 15 for update;

# session 2, execute block.
begin;

insert into user value (4, 20);

6. 使用二级索引,范围查询

1
2
3
4
5
6
7
8
9
# session 1, it locks range [3,8]
begin;

select * from user where age >= 12 and age <= 28 for update;

# session 2, execute block.
begin;

insert into user value (4, 20);

7. 结论

使用主键索引,行存在时,才只会锁定这一行。 其他情况都是使用范围锁定

Docker 单主机网络

这篇文章主要简述 docker 中的 bridge 网络驱动是如何工作的。

1. 测试一,veth1 (ns1) — veth2 (ns2)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# create ns1, ns2
ip netns add ns1
ip netns add ns2

# create veth1, veth2
ip link add veth1 type veth peer name veth2

# set veth1 for ns1, set veth2 for ns2
ip link set dev veth1 netns ns1
ip link set dev veth2 netns ns2

# set veth1 ip, set veth2 ip
ip netns exec ns1 ip addr add 172.16.0.1/24 dev veth1
ip netns exec ns2 ip addr add 172.16.0.2/24 dev veth2

# set veth1 up, set veth2 up
ip netns exec ns1 ip link set dev veth1 up
ip netns exec ns2 ip link set dev veth2 up

# show ip address
ip netns exec ns1 ip addr
ip netns exec ns2 ip addr

# test for ping 
ip netns exec ns1 ping 172.16.0.2
ip netns exec ns2 ping 172.16.0.1

# recovery all setting 
ip netns delete ns1
ip netns delete ns2

2. 测试二,veth0 (bridge0) — veth1 (ns1)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# create ns1
ip netns add ns1

# create veth0, veth1
ip link add veth0 type veth peer name veth1

# create bridge0
ip link add bridge0 type bridge

# set veth1 for ns1
ip link set dev veth1 netns ns1

# set veth0 for bridge0
ip link set dev veth0 master bridge0

# set veth0 ip address
ip addr add 172.16.0.1/24 dev veth0

# set bridge0 ip address
ip addr add 172.16.0.0/24 dev bridge0

# set veth1 ip address
ip netns exec ns1 ip addr add 172.16.0.2/24 dev veth1

# set veth0, veth1, bridge0 up
ip link set dev veth0 up
ip link set dev bridge0 up
ip netns exec ns1 ip link set dev veth1 up

# delete veth0 route
ip route del 172.16.0.0/24 dev veth0

# show ip address
ip addr
ip netns exec ns1 ip addr

# test for ping 
ping 172.16.0.2
ip netns exec ns1 ping 172.16.0.1

# recovery all setting 
ip netns del ns1
ip link del bridge0

搭建 Istio 源码调试环境

1. 前置条件

  • 安装 docker,必须配置 docker 代理,否则 build 失败。 参考
  • 下载 istio 源码。
  • 安装 godlv 工具。参考

2. 设置环境变量

1
2
3
4
5
6
7
8
# docker 地址
export HUB="docker.io/youwillsee"

# istio 的源码目录
export ISTIO=/root/code/istio

# docker 的 tag
export TAG=1.17-debug

3. build istio

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 构建 debug 的版本,会输出在 out 目录下
make DEBUG=1 build

# 构建 debug 的版本,推到本地的 docker 中
make DEBUG=1 docker

# 推送到远端的 docker 中
make docker.push

# 清理
make clean

参考