本篇将讲解registration_db.go文件。

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package nsqlookupd

import (
    "fmt"
    "sync"
    "time"
)

//定义了类型RegistrationDB,按字面意思:注册数据库,可理解为保存注册信息的数据库。
type RegistrationDB struct {
    //嵌入了RWMutex,所以在后面在方法中才可以调用Lock和Unlock方法
    sync.RWMutex
    //一个MAP,以Registration为键,Producers为值
    //看下面的代码中Producers的定义,它是Producer的slice,从中可看出Registration和Producer是一对多的关系
    registrationMap map[Registration]Producers
}

//定义类型Registration,
type Registration struct {
    Category string
    Key      string
    SubKey   string
}

//定义类型Registrations为Registration的slice
type Registrations []Registration

//定义类型PeerInfo
type PeerInfo struct {
    id               string
    RemoteAddress    string `json:"remote_address"`
    Hostname         string `json:"hostname"`
    BroadcastAddress string `json:"broadcast_address"`
    TcpPort          int    `json:"tcp_port"`
    HttpPort         int    `json:"http_port"`
    Version          string `json:"version"`
    lastUpdate       time.Time
}

//定义类型Producer
type Producer struct {
    //PeerInfo的指针
    peerInfo *PeerInfo
    //是否要被移除(tombstoned)
    tombstoned bool
    //移除时间
    tombstonedAt time.Time
}

//定义类型Producers为Producer的slice
type Producers []*Producer

//Producer的String方法
func (p *Producer) String() string {
    return fmt.Sprintf("%s [%d, %d]", p.peerInfo.BroadcastAddress, p.peerInfo.TcpPort, p.peerInfo.HttpPort)
}

//本方法将Producer标记为墓碑状态
func (p *Producer) Tombstone() {
    p.tombstoned = true
    p.tombstonedAt = time.Now()
}

//被标记为墓碑状态,同时距标记时间小于lifetime值。
//比如:在0分0秒时调用了上一个函数Tombstone(),在0分1秒时调用函数IsTombstoned(5),返回结果为true
func (p *Producer) IsTombstoned(lifetime time.Duration) bool {
    return p.tombstoned && time.Now().Sub(p.tombstonedAt) < lifetime
}

//新建RegistrationDB类型的变量
func NewRegistrationDB() *RegistrationDB {
    return &RegistrationDB{
        //make一个map
        registrationMap: make(map[Registration]Producers),
    }
}

//添加一个registration的key,只是把map的key设置了,value为一个空的Producers slice
func (r *RegistrationDB) AddRegistration(k Registration) {
    r.Lock()
    defer r.Unlock()
    _, ok := r.registrationMap[k]
    if !ok {
        r.registrationMap[k] = make(Producers, 0)
    }
}

//将一个Producer添加到指定的Registration里
func (r *RegistrationDB) AddProducer(k Registration, p *Producer) bool {
    r.Lock()
    defer r.Unlock()
    //producers是一个slice
    producers := r.registrationMap[k]

    //遍历producers,看这个要添加的Producer是否已经存在了
    found := false
    for _, producer := range producers {
        //通过producer.peerInfo.id来判断是否为同一个Producer
        if producer.peerInfo.id == p.peerInfo.id {
            found = true
        }
    }

    //只有要添加的Producer不存在时,才添加到Registration里
    if found == false {
        r.registrationMap[k] = append(producers, p)
    }
    return !found
}

// 根据producer.peerInfo.id从registration里删除一个Producer
func (r *RegistrationDB) RemoveProducer(k Registration, id string) (bool, int) {
    r.Lock()
    defer r.Unlock()

    //map中不存在key为k的Registration记录,所以也就无需删除了。
    producers, ok := r.registrationMap[k]
    if !ok {
        return false, 0
    }
    removed := false
    //创建空的Producers slice,所有不需要删除的Producer都移动这个slice里,并在移除完成后重新赋值给Registration
    //注意学习这种从slice中移除一个元素的方式
    cleaned := make(Producers, 0)
    for _, producer := range producers {
        //id不相同,不是要删除的producer,移到cleaned slice里
        if producer.peerInfo.id != id {
            cleaned = append(cleaned, producer)
        } else {
            removed = true
        }
    }
    // Note: this leaves keys in the DB even if they have empty lists
    //重新赋值,这确保了即使cleaned是一个空slice,键值k仍然会在RegistrationDB中存在
    r.registrationMap[k] = cleaned

    //返回两个值,是否删除,新slice的长度
    return removed, len(cleaned)
}

// 删除Registration和它对应的Producers
func (r *RegistrationDB) RemoveRegistration(k Registration) {
    r.Lock()
    defer r.Unlock()
    delete(r.registrationMap, k)
}

//查找Registrations,可以看到,传入参数的三个变量与Registration类型里的三个值是对应的
func (r *RegistrationDB) FindRegistrations(category string, key string, subkey string) Registrations {
    r.RLock()
    defer r.RUnlock()
    results := make(Registrations, 0)
    for k := range r.registrationMap {
        //找出registrationMap中所有category,key,subkey与入参相同的Registration
        //IsMatch方法在后面的代码中定义
        if !k.IsMatch(category, key, subkey) {
            continue
        }
        results = append(results, k)
    }
    return results
}

//根据category key subkey查找所有的Producer
func (r *RegistrationDB) FindProducers(category string, key string, subkey string) Producers {
    r.RLock()
    defer r.RUnlock()
    results := make(Producers, 0)
    //遍历map
    for k, producers := range r.registrationMap {
        if !k.IsMatch(category, key, subkey) {
            continue
        }
        //遍历每个registration下的producers
        for _, producer := range producers {
            found := false
            //判断producer是否已经存在了,如果存在的话,就不添加了
            for _, p := range results {
                if producer.peerInfo.id == p.peerInfo.id {
                    found = true
                }
            }
            if found == false {
                results = append(results, producer)
            }
        }
    }
    return results
}

//根据producer.peerInfo.id查找所属的registration key
func (r *RegistrationDB) LookupRegistrations(id string) Registrations {
    r.RLock()
    defer r.RUnlock()
    results := make(Registrations, 0)
    //遍历map
    for k, producers := range r.registrationMap {
        //遍历每个registration下的producers
        for _, p := range producers {
            if p.peerInfo.id == id {
                results = append(results, k)
                break
            }
        }
    }
    return results
}

//依据Registration类型里的三个变量,判断是否与Registration匹配
func (k Registration) IsMatch(category string, key string, subkey string) bool {
    if category != k.Category {
        return false
    }
    if key != "*" && k.Key != key {
        return false
    }
    if subkey != "*" && k.SubKey != subkey {
        return false
    }
    return true
}

//过滤获取所有与输入参数匹配的Registration
func (rr Registrations) Filter(category string, key string, subkey string) Registrations {
    output := make(Registrations, 0)
    for _, k := range rr {
        if k.IsMatch(category, key, subkey) {
            output = append(output, k)
        }
    }
    return output
}

//获取MAP中所有Registration的key
func (rr Registrations) Keys() []string {
    keys := make([]string, len(rr))
    for i, k := range rr {
        keys[i] = k.Key
    }
    return keys
}

//获取MAP中所有Registration的subkey
func (rr Registrations) SubKeys() []string {
    subkeys := make([]string, len(rr))
    for i, k := range rr {
        subkeys[i] = k.SubKey
    }
    return subkeys
}

//获取所有可用的Producer
func (pp Producers) FilterByActive(inactivityTimeout time.Duration, tombstoneLifetime time.Duration) Producers {
    now := time.Now()
    results := make(Producers, 0)
    for _, p := range pp {
        //满足以下两个判断条件的producer被忽略
        //1 超过了活跃时间,在inactivityTimeout时间内没有与nsqlookupd交互
        //2 被标记为墓碑状态,在tombstoneLifetime时间内标记的producer将被过滤掉
        if now.Sub(p.peerInfo.lastUpdate) > inactivityTimeout || p.IsTombstoned(tombstoneLifetime) {
            continue
        }
        results = append(results, p)
    }
    return results
}

//获取Producers中所有的PeerInfo
func (pp Producers) PeerInfo() []*PeerInfo {
    results := make([]*PeerInfo, 0)
    for _, p := range pp {
        results = append(results, p.peerInfo)
    }
    return results
}

读过上述代码,可总结出,registration_db.go文件用MAP以一对多的形式保存Producer,并提供一系列增、删、改、查的操作封装。同时使用RWMutex做并发控制。

go语言nsq源码解读五 nsqlookupd源码registration_db.go的更多相关文章

  1. go语言 nsq源码解读三 nsqlookupd源码nsqlookupd.go

    从本节开始,将逐步阅读nsq各模块的代码. 读一份代码,我的思路一般是: 1.了解用法,知道了怎么使用,对理解代码有宏观上有很大帮助. 2.了解各大模块的功能特点,同时再想想,如果让自己来实现这些模块 ...

  2. go语言 nsq源码解读四 nsqlookupd源码options.go、context.go和wait_group_wrapper.go

    本节会解读nsqlookupd.go文件中涉及到的其中三个文件:options.go.context.go和wait_group_wrapper.go. options.go 123456789101 ...

  3. (转)go语言nsq源码解读二 nsqlookupd、nsqd与nsqadmin

    转自:http://www.baiyuxiong.com/?p=886 ---------------------------------------------------------------- ...

  4. REST、DRF(View源码解读、APIView源码解读)

    一 . REST            前言 1 . 编程 : 数据结构和算法的结合 .小程序如简单的计算器,我们输入初始数据,经过计算,得到最终的数据,这个过程中,初始数据和结果数据都是数据,而计算 ...

  5. Restful 1 -- REST、DRF(View源码解读、APIView源码解读)及框架实现

    一.REST 1.什么是编程? 数据结构和算法的结合 2.什么是REST? - url用来唯一定位资源,http请求方式来区分用户行为 首先回顾我们曾经做过的图书管理系统,我们是这样设计url的,如下 ...

  6. DRF(1) - REST、DRF(View源码解读、APIView源码解读)

    一.REST 1.什么是编程? 数据结构和算法的结合. 2.什么是REST? 首先回顾我们曾经做过的图书管理系统,我们是这样设计url的,如下: /books/ /get_all_books/ 访问所 ...

  7. go语言nsq源码解读二 nsqlookupd、nsqd与nsqadmin

    nsqlookupd: 官方文档解释见:http://bitly.github.io/nsq/components/nsqlookupd.html 用官方话来讲是:nsqlookupd管理拓扑信息,客 ...

  8. mybatis源码解读(五)——sql语句的执行流程

    还是以第一篇博客中给出的例子,根据代码实例来入手分析. static { InputStream inputStream = MybatisTest.class.getClassLoader().ge ...

  9. spring源码解读之 JdbcTemplate源码

    原文:https://blog.csdn.net/songjinbin/article/details/19857567 在Spring中,JdbcTemplate是经常被使用的类来帮助用户程序操作数 ...

随机推荐

  1. ValueObject的理解

    思考ValueObject应该更多从内存的角度思考,而非DB持久化的角度. 例如: public class A { public int Id { get; set; } public Addres ...

  2. 致IT之路的先驱者和旅人

    1,图灵和香农 故事的开始,要从计算机之父图灵和信息论的创始人香农开始说起.图灵最大的贡献是发明了图灵机,关于图灵机如果要让人明白究竟有什么用,从如何实现一个半导体电路图灵机这方面理解比较好.只要一个 ...

  3. Android Gradle使用总结

    转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/77678577 本文出自[赵彦军的博客] 其他 Groovy 使用完全解析 http ...

  4. linux下面调试C、C++

    (1)写好makefile文件(支持debug) objects = Main.o Satellite.o TimeSystem.o  SRPPara:$(objects) g++ -g -o SRP ...

  5. PHP采用Cookie实现 购物车

    先来看一下逻辑分析图:

  6. 从一个微型例子看“C/C++的内存分配机制”和“数组变量名与指针变量名”(转)

    C++的内存有五大分区:堆区.栈区.自由存储区.全局/静态存储区.常量存储区. 五个数据段:数据段.代码段.BSS段.堆.栈 内存分配方式有三种: 从静态存储区域分配.内存在程序编译的时候就已经分配好 ...

  7. Xamarin.Android 使用AsyncTask提示上传动态

    我们有时候会通过WebServices上传数据,如果信息量过大并没有提示,用户会觉得是死机,或是系统崩溃,这时候我们可以用到AsyncTask(异步任务)来提示上传信息,例如:正在上传数据... 这里 ...

  8. iOS xcode9 framework静态库的创建以及xib和图片的使用记录

    来到了新公司,要开发的第一个项目据说可能要封成framework,可是我从来没自己做过framework呀!顿时开始发愤图强,赶紧恶补了起来.但是还是遇到了一些乱七八糟的情况,所以写个随笔记下来. 1 ...

  9. 在AspNetCore 中 使用Redis实现分布式缓存

    AspNetCore 使用Redis实现分布式缓存 上一篇讲到了,Core的内置缓存:IMemoryCache,以及缓存的基础概念.本篇会进行一些概念上的补充. 本篇我们记录的内容是怎么在Core中使 ...

  10. 如何使用 toml 配置 SpaceVim

    配置 SpaceVim 主要包括以下几个内容: 设置 SpaceVim 选项 启动/禁用模块 添加自定义插件 添加自定义按键映射以及插件配置 设置SpaceVim选项 原先,在 init.vim 文件 ...