forked from apache/cassandra-gocql-driver
-
Notifications
You must be signed in to change notification settings - Fork 5
/
refreshPartitionMap.go
220 lines (182 loc) · 4.72 KB
/
refreshPartitionMap.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
package gocql
import (
"encoding/hex"
"net"
"sort"
"strconv"
"sync"
)
type QualifiedTableName struct {
keyspacename string
tablename string
}
func (q *QualifiedTableName) NewQualifiedTableName(keyspacename string, tablename string) {
q.keyspacename = keyspacename
q.tablename = tablename
}
func getKeyspaceName(QualifiedTableName *QualifiedTableName) string {
return QualifiedTableName.keyspacename
}
func getTableName(QualifiedTableName *QualifiedTableName) string {
return QualifiedTableName.tablename
}
type PartitionMetadata struct {
startkey int64
endkey int64
hosts []*HostInfo
}
func (p *PartitionMetadata) NewPartitionMetadata(s_key int64, e_key int64, h []*HostInfo) {
p.startkey = s_key
p.endkey = e_key
p.hosts = h
}
func getStartKey(PartitionMetadata *PartitionMetadata) int64 {
return PartitionMetadata.startkey
}
func getEndKey(PartitionMetadata *PartitionMetadata) int64 {
return PartitionMetadata.endkey
}
func getHosts(PartitionMetadata PartitionMetadata) []*HostInfo {
return PartitionMetadata.hosts
}
type TableSplitMetadata struct {
partitionMap map[int64]PartitionMetadata
}
func (r *TableSplitMetadata) Floor(num int64) int64 {
keys := make([]int64, 0, len(r.partitionMap))
for k := range r.partitionMap {
keys = append(keys, k)
}
var u int
sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] })
var i int
for i = 0; i < len(keys); i++ {
if keys[i] > num {
u = i - 1
break
}
}
if i == len(keys) {
return keys[len(keys)-1]
} else {
return keys[u]
}
}
func (r *TableSplitMetadata) NewTableSplitMetadata() {
r.partitionMap = make(map[int64]PartitionMetadata)
}
func (r *TableSplitMetadata) getPartitionMetadata(key int64) PartitionMetadata {
var p PartitionMetadata
p = r.partitionMap[key]
return p
}
func (r *TableSplitMetadata) getHosts(key int64) []*HostInfo {
p := r.getPartitionMetadata(key)
return getHosts(p)
}
func (r *TableSplitMetadata) getPartitionMap() map[int64]PartitionMetadata {
return r.partitionMap
}
func mapkey(m map[*net.IP]string, value string) (key []*net.IP, ok bool) {
for k, v := range m {
if v == value {
key = append(key, k)
ok = true
}
}
return
}
type tablesplitsStruc struct {
mu sync.Mutex
tableSplits map[QualifiedTableName]TableSplitMetadata
}
var tablesplits tablesplitsStruc
func (t *tablesplitsStruc) Write(table map[QualifiedTableName]TableSplitMetadata) {
t.mu.Lock()
t.tableSplits = table
t.mu.Unlock()
}
func (t *tablesplitsStruc) Read() (table map[QualifiedTableName]TableSplitMetadata) {
t.mu.Lock()
table = t.tableSplits
t.mu.Unlock()
return
}
func getTableSplitMetadata(keyspacename string, tablename string) TableSplitMetadata {
var p QualifiedTableName
p.NewQualifiedTableName(keyspacename, tablename)
table := tablesplits.Read()
return table[p]
}
func (r *ringDescriber) getClusterPartitionInfo() error {
Iter := r.session.control.query("SELECT * FROM system.partitions;")
var tableSplits1 = make(map[QualifiedTableName]TableSplitMetadata)
if Iter == nil {
return errNoControl
}
var (
keyspace_name string
table_name string
start_key string
end_key string
id UUID
replica_addresses map[*net.IP]string
)
rows := Iter.Scanner()
for rows.Next() {
rows.Scan(&keyspace_name, &table_name, &start_key, &end_key, &id, &replica_addresses)
var tableId QualifiedTableName
tableId.NewQualifiedTableName(keyspace_name, table_name)
var tableSplitMetadata TableSplitMetadata
tableSplitMetadata.NewTableSplitMetadata()
tableSplitMetadata = tableSplits1[tableId]
if tableSplitMetadata.partitionMap == nil {
tableSplitMetadata.NewTableSplitMetadata()
tableSplits1[tableId] = tableSplitMetadata
}
var hosts []*HostInfo
ip, _ := mapkey(replica_addresses, "LEADER")
for i := 0; i < len(ip); i++ {
host, _ := r.getHostInfoFromIp(*ip[i])
if host != nil {
hosts = append(hosts, host)
}
}
ip, _ = mapkey(replica_addresses, "FOLLOWER")
for i := 0; i < len(ip); i++ {
host, _ := r.getHostInfoFromIp(*ip[i])
if host != nil {
hosts = append(hosts, host)
}
}
ip, _ = mapkey(replica_addresses, "READ_REPLICA")
for i := 0; i < len(ip); i++ {
host, _ := r.getHostInfoFromIp(*ip[i])
if host != nil {
hosts = append(hosts, host)
}
}
hstart := hex.EncodeToString([]byte(start_key))
hend := hex.EncodeToString([]byte(end_key))
var (
s int64
e int64
)
if start_key != "" {
s, _ = strconv.ParseInt(hstart, 16, 64)
} else {
s = 0
}
if end_key != "" {
e, _ = strconv.ParseInt(hend, 16, 64)
} else {
e = 0
}
p := tableSplitMetadata.getPartitionMap()
var h PartitionMetadata
h.NewPartitionMetadata(s, e, hosts)
p[s] = h
}
tablesplits.Write(tableSplits1)
return nil
}