Skip to content

Commit

Permalink
Merge branch 'master' into feature/getbit
Browse files Browse the repository at this point in the history
  • Loading branch information
YIDWang committed Jan 23, 2019
2 parents c98c48d + cc4c698 commit e3a406c
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
[![Go Report Card](https://goreportcard.com/badge/github.com/meitu/titan)](https://goreportcard.com/report/github.com/meitu/titan)
[![Coverage Status](https://coveralls.io/repos/github/meitu/titan/badge.svg?branch=master)](https://coveralls.io/github/meitu/titan?branch=master)
[![Coverage Status](https://img.shields.io/badge/version-v0.3.1-brightgreen.svg)](https://github.com/meitu/titan/releases)
[![Discourse status](https://img.shields.io/discourse/https/meta.discourse.org/status.svg)](https://titan-tech-group.slack.com)


A distributed implementation of Redis compatible layer based on [TiKV](https://github.com/tikv/tikv/)

Expand Down
45 changes: 45 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
version: '2.1'

services:
pd0:
image: pingcap/pd:latest
ports:
- "2379"
volumes:
- ./data:/data
- ./logs:/logs
command:
- --name=pd0
- --client-urls=http://0.0.0.0:2379
- --peer-urls=http://0.0.0.0:2380
- --advertise-client-urls=http://pd0:2379
- --advertise-peer-urls=http://pd0:2380
- --initial-cluster=pd0=http://pd0:2380
- --data-dir=/data/pd0
- --log-file=/logs/pd0.log
restart: on-failure

tikv0:
image: pingcap/tikv:latest
volumes:
- ./data:/data
- ./logs:/logs
command:
- --addr=0.0.0.0:20160
- --advertise-addr=tikv0:20160
- --data-dir=/data/tikv0
- --pd=pd0:2379
- --log-file=/logs/tikv0.log
depends_on:
- "pd0"
restart: on-failure

titan:
image: meitu/titan
ports:
- "7369:7369"
command:
- --pd-addrs=tikv://pd0:2379
depends_on:
- "tikv0"
restart: on-failure
25 changes: 25 additions & 0 deletions docs/ops/deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,27 @@ Edit conf/titan.toml and set the pd-addrs
pd-addrs="tikv://your-pd-addrs:port"
```

### Enable Multi-tenancy
First, set auth value in the 'server' section of conf/titan.toml

```
auth = "YOUR_SERVER_KEY"
```

Then use ./tools/token/token to generate a client token.

```
cd $GOPATH/src/github.com/meitu/titan/tools/token
go build main.go -o titan-gen-client-token
./titan-gen-client-token -key YOUR_SERVER_KEY -namespace bbs
```

Then you'll get the token for client auth, for example: bbs-1543999615-1-7a50221d92e69d63e1b443

### Run

```
cd $GOPATH/src/github.com/meitu/titan
./titan
```

Expand All @@ -36,3 +54,10 @@ pd-addrs="tikv://your-pd-addrs:port"
```
redis-cli -p 7369
```

If Multi-tenancy is enabled, use server-generated token to auth:

```
redis-cli -p 7369 -a bbs-1543999615-1-7a50221d92e69d63e1b443
```

15 changes: 9 additions & 6 deletions metrics/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var (
gm *Metrics
)

//Metrics prometheus statistics
// Metrics prometheus statistics
type Metrics struct {
//biz
ConnectionOnlineGaugeVec *prometheus.GaugeVec
Expand All @@ -61,7 +61,7 @@ type Metrics struct {
LogMetricsCounterVec *prometheus.CounterVec
}

//init create global object
// init create global object
func init() {
gm = &Metrics{}

Expand Down Expand Up @@ -165,16 +165,19 @@ func init() {
[]string{labelName},
)
prometheus.MustRegister(gm.LogMetricsCounterVec)

http.Handle("/titan/metrics", prometheus.Handler())
}

//GetMetrics return metrics object
// GetMetrics return metrics object
func GetMetrics() *Metrics {
return gm
}

//Measure logger level rate
// MetricsHandle register the metrics handle
func MetricsHandle() {
http.Handle("/metrics", prometheus.Handler())
}

// Measure logger level rate
func Measure(e zapcore.Entry) error {
label := e.LoggerName + "_" + e.Level.String()
gm.LogMetricsCounterVec.WithLabelValues(label).Inc()
Expand Down
3 changes: 2 additions & 1 deletion metrics/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func NewServer(config *conf.Status) *Server {
// Serve accepts incoming connections on the Listener l
func (s *Server) Serve(lis net.Listener) error {
zap.L().Info("status server start", zap.String("addr", s.addr))
MetricsHandle()
return s.statusServer.Serve(lis)
}

Expand Down Expand Up @@ -70,5 +71,5 @@ func (s *Server) ListenAndServe(addr string) error {
return err
}
zap.L().Info("status server start", zap.String("addr", s.addr))
return s.statusServer.Serve(lis)
return s.Serve(lis)
}
75 changes: 75 additions & 0 deletions proposals/hash-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Hashes 设计

## 背景
Hash是Redis最常用和重要的数据结构,其Meta信息维护了Hash对象的成员个数和更新时间,在并发写入(比如HSET)的场景下,可能造成大量事务冲突,严重影响Hash结构的并发性能。如果选择不维护上述两个Meta信息,则在执行HLEN时需要遍历所有相关数据,代价较大。本设计通过引入MetaSlot的概念,将Meta信息分拆到多个Slot中,从而减少并发更新Meta信息的事务冲突,在获取长度和减少并发冲突之间,做出最合适的折中和平衡。

## Meta信息
* Len 标识Hash中成员的个数
* MetaSlot 标识Meta信息Slot个数(注:这里没有直接叫Slot,是因为容易理解为哈希表的slot)

### MetaSlotKey
为了减少并发修改MetaKey造成的事务冲突,我们引入MetaSlotKey,将写Meta信息的请求,均衡分散到多个MetaSlotKey上。

MetaSlotKey 信息
* Len
* UpdatedAt

MetaSlotKey 格式
```
{namespace}:{dbid}:MS:{objectid}
```

新增tag:MS作为MetaSlotKey的标识,既可以避免遍历Meta信息时被MetaSlotKey干扰,也可以将Meta信息与MetaSlot信息尽可能放到同一个Region。

### 均衡算法
随机。每次写入,随机0~MetaSlot-1之间的数字,选择对应的MetaSlotKey,更新Meta信息。

## 命令处理

### HSET、HSETNX、HMSET

1. 获取Meta信息,判断Slot个数,如果为0,则更新Meta信息时,直接写入MetaKey
2. 如果Slot个数大于零,在更新Meta信息时,选择对应的MetaSlotKey写入。

### HLEN

1. 获取Meta信息,判断Slot个数,如果为0,则返回Meta信息中的长度。
2. 如果Slot个数大于零,发起一次Seek请求,获取所有MetaSlotKey,返回所有MetaSlotKey中所有长度之和

### HDEL

1. 先获取HASH长度,方法跟HLEN相同
2. 判断HDEL删除的是最后一个Key,则销毁整个HASH对象

### DEBUG OBJECT

1. 获取Meta信息,判断Slot个数,如果为0,返回Meta信息
2. 如果Slot个数大于0,发起一次Seek请求,获取所有MetaSlotKey,将MetaSlotKey中Len加和,作为返回的Len,选择最大的UpdatedAt,作为返回的UpdatedAt

### DEL

1. 获取Meta信息,判断对象类型,如果是HASH,则继续
2. 直接删除Meta信息,并将DataKey前缀交给GC
3. 判断Slot个数,如果大于0,则将MetaSlotKey前缀交给GC

## 设置MetaSlot

### 配置文件
默认为0,可通过配置文件修改,对整个集群生效。修改后,只对新的Key,或原有MetaSlot=0的Key生效。

### 扩展命令

新增扩展命令
```
hmslot key count
```

成功返回SimpleString OK,否则返回Error

### MetaSlot 调整策略

#### MetaSlot增大
修改Meta信息中的MetaSlot为新的值即可。

#### MetaSlot减小
先获取旧的MetaSlotKey,并得到Len和UpdatedAt信息,跟新的MetaSlot一起写入Meta信息。
1 change: 1 addition & 0 deletions proposals/metakey-datakey-affinity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Combine metakey and datakey in a single region as can as possible

0 comments on commit e3a406c

Please sign in to comment.