forked from tsuna/gohbase
-
Notifications
You must be signed in to change notification settings - Fork 1
/
caches.go
275 lines (238 loc) · 7.67 KB
/
caches.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
// Copyright (C) 2016 The GoHBase Authors. All rights reserved.
// This file is part of GoHBase.
// Use of this source code is governed by the Apache License 2.0
// that can be found in the COPYING file.
package gohbase
import (
"bytes"
"io"
"sync"
"github.com/cznic/b"
"github.com/reborn-go/gohbase/hrpc"
log "github.com/sirupsen/logrus"
)
// clientRegionCache is client -> region cache. Used to quickly
// look up all the regioninfos that map to a specific client
type clientRegionCache struct {
m sync.RWMutex
regions map[hrpc.RegionClient]map[hrpc.RegionInfo]struct{}
}
// put caches client and associates a region with it. Returns a client that is in cache.
// TODO: obvious place for optimization (use map with address as key to lookup exisiting clients)
func (rcc *clientRegionCache) put(c hrpc.RegionClient, r hrpc.RegionInfo) hrpc.RegionClient {
rcc.m.Lock()
for existingClient, regions := range rcc.regions {
// check if client already exists, checking by host and port
// because concurrent callers might try to put the same client
if c.Addr() == existingClient.Addr() {
// check client already knows about the region, checking
// by pointer is enough because we make sure that there are
// no regions with the same name around
if _, ok := regions[r]; !ok {
regions[r] = struct{}{}
}
rcc.m.Unlock()
log.WithFields(log.Fields{
"existingClient": existingClient,
"client": c,
}).Debug("region client is already in client's cache")
return existingClient
}
}
// no such client yet
rcc.regions[c] = map[hrpc.RegionInfo]struct{}{r: struct{}{}}
rcc.m.Unlock()
log.WithField("client", c).Info("added new region client")
return c
}
func (rcc *clientRegionCache) del(r hrpc.RegionInfo) {
rcc.m.Lock()
c := r.Client()
if c != nil {
r.SetClient(nil)
regions := rcc.regions[c]
delete(regions, r)
}
rcc.m.Unlock()
}
func (rcc *clientRegionCache) closeAll() {
rcc.m.Lock()
for client, regions := range rcc.regions {
for region := range regions {
region.MarkUnavailable()
region.SetClient(nil)
}
client.Close()
}
rcc.m.Unlock()
}
func (rcc *clientRegionCache) clientDown(c hrpc.RegionClient) map[hrpc.RegionInfo]struct{} {
rcc.m.Lock()
downregions, ok := rcc.regions[c]
delete(rcc.regions, c)
rcc.m.Unlock()
if ok {
log.WithField("client", c).Info("removed region client")
}
return downregions
}
// TODO: obvious place for optimization (use map with address as key to lookup exisiting clients)
func (rcc *clientRegionCache) checkForClient(addr string) hrpc.RegionClient {
rcc.m.RLock()
for client := range rcc.regions {
if client.Addr() == addr {
rcc.m.RUnlock()
return client
}
}
rcc.m.RUnlock()
return nil
}
// key -> region cache.
type keyRegionCache struct {
m sync.RWMutex
// Maps a []byte of a region start key to a hrpc.RegionInfo
regions *b.Tree
}
func (krc *keyRegionCache) get(key []byte) ([]byte, hrpc.RegionInfo) {
krc.m.RLock()
enum, ok := krc.regions.Seek(key)
if ok {
krc.m.RUnlock()
log.Fatalf("WTF: got exact match for region search key %q", key)
return nil, nil
}
k, v, err := enum.Prev()
enum.Close()
krc.m.RUnlock()
if err == io.EOF {
// we are the beginning of the tree
return nil, nil
}
return k.([]byte), v.(hrpc.RegionInfo)
}
func isRegionOverlap(regA, regB hrpc.RegionInfo) bool {
// if region's stop key is empty, it's assumed to be the greatest key
return bytes.Equal(regA.Namespace(), regB.Namespace()) &&
bytes.Equal(regA.Table(), regB.Table()) &&
(len(regB.StopKey()) == 0 || bytes.Compare(regA.StartKey(), regB.StopKey()) < 0) &&
(len(regA.StopKey()) == 0 || bytes.Compare(regA.StopKey(), regB.StartKey()) > 0)
}
func (krc *keyRegionCache) getOverlaps(reg hrpc.RegionInfo) []hrpc.RegionInfo {
var overlaps []hrpc.RegionInfo
var v interface{}
var err error
// deal with empty tree in the beginning so that we don't have to check
// EOF errors for enum later
if krc.regions.Len() == 0 {
return overlaps
}
// check if key created from new region falls into any cached regions
key := createRegionSearchKey(fullyQualifiedTable(reg), reg.StartKey())
enum, ok := krc.regions.Seek(key)
if ok {
log.Fatalf("WTF: found a region with exact name as the search key %q", key)
}
// case 1: landed before the first region in cache
// enum.Prev() returns io.EOF
// enum.Next() returns io.EOF
// SeekFirst() + enum.Next() returns the first region, which has larger start key
// case 2: landed before the second region in cache
// enum.Prev() returns the first region X and moves pointer to -infinity
// enum.Next() returns io.EOF
// SeekFirst() + enum.Next() returns first region X, which has smaller start key
// case 3: landed anywhere after the second region
// enum.Prev() returns the region X before it landed, moves pointer to the region X - 1
// enum.Next() returns X - 1 and move pointer to X, which has smaller start key
enum.Prev()
_, _, err = enum.Next()
if err == io.EOF {
// we are in the beginning of tree, get new enum starting
// from first region
enum.Close()
enum, err = krc.regions.SeekFirst()
if err != nil {
log.Fatalf(
"error seeking first region when getting overlaps for region %v: %v", reg, err)
}
}
_, v, err = enum.Next()
if isRegionOverlap(v.(hrpc.RegionInfo), reg) {
overlaps = append(overlaps, v.(hrpc.RegionInfo))
}
_, v, err = enum.Next()
// now append all regions that overlap until the end of the tree
// or until they don't overlap
for err != io.EOF && isRegionOverlap(v.(hrpc.RegionInfo), reg) {
overlaps = append(overlaps, v.(hrpc.RegionInfo))
_, v, err = enum.Next()
}
enum.Close()
return overlaps
}
// put looks up if there's already region with this name in regions cache
// and if there's, returns it in overlaps and doesn't modify the cache.
// Otherwise, it puts the region and removes all overlaps in case all of
// them are older. Returns a slice of overlapping regions and whether
// passed region was put in the cache.
func (krc *keyRegionCache) put(reg hrpc.RegionInfo) (overlaps []hrpc.RegionInfo, replaced bool) {
krc.m.Lock()
krc.regions.Put(reg.Name(), func(v interface{}, exists bool) (interface{}, bool) {
if exists {
// region is already in cache,
// note: regions with the same name have the same age
overlaps = []hrpc.RegionInfo{v.(hrpc.RegionInfo)}
return nil, false
}
// find all entries that are overlapping with the range of the new region.
overlaps = krc.getOverlaps(reg)
for _, o := range overlaps {
if o.ID() > reg.ID() {
// overlapping region is younger,
// don't replace any regions
// TODO: figure out if there can a case where we might
// have both older and younger overlapping regions, for
// now we only replace if all overlaps are older
return nil, false
}
}
// all overlaps are older, put the new region
replaced = true
return reg, true
})
if !replaced {
krc.m.Unlock()
log.WithFields(log.Fields{
"region": reg,
"overlaps": overlaps,
"replaced": replaced,
}).Debug("region is already in cache")
return
}
// delete overlapping regions
// TODO: in case overlaps are always either younger or older,
// we can just greedily remove them in Put function
for _, o := range overlaps {
krc.regions.Delete(o.Name())
// let region establishers know that they can give up
o.MarkDead()
}
krc.m.Unlock()
log.WithFields(log.Fields{
"region": reg,
"overlaps": overlaps,
"replaced": replaced,
}).Info("added new region")
return
}
func (krc *keyRegionCache) del(reg hrpc.RegionInfo) bool {
krc.m.Lock()
success := krc.regions.Delete(reg.Name())
krc.m.Unlock()
// let region establishers know that they can give up
reg.MarkDead()
log.WithFields(log.Fields{
"region": reg,
}).Debug("removed region")
return success
}