-
Notifications
You must be signed in to change notification settings - Fork 56
/
s2.go
168 lines (144 loc) · 3.87 KB
/
s2.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
package shard
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/gojektech/weaver"
"github.com/gojektech/weaver/pkg/util"
geos2 "github.com/golang/geo/s2"
)
var (
defaultBackendS2id = "default"
)
func NewS2Strategy(data json.RawMessage) (weaver.Sharder, error) {
cfg := S2StrategyConfig{}
if err := json.Unmarshal(data, &cfg); err != nil {
return nil, err
}
if err := cfg.Validate(); err != nil {
return nil, err
}
s2Backends := make(map[string]*weaver.Backend, len(cfg.Backends))
for s2id, backend := range cfg.Backends {
var err error
if s2Backends[s2id], err = parseBackend(backend); err != nil {
return nil, err
}
}
if cfg.ShardKeyPosition == nil {
defaultPos := -1
cfg.ShardKeyPosition = &defaultPos
}
return &S2Strategy{
backends: s2Backends,
shardKeySeparator: cfg.ShardKeySeparator,
shardKeyPosition: *cfg.ShardKeyPosition,
}, nil
}
type S2Strategy struct {
backends map[string]*weaver.Backend
shardKeySeparator string
shardKeyPosition int
}
type S2StrategyConfig struct {
ShardKeySeparator string `json:"shard_key_separator"`
ShardKeyPosition *int `json:"shard_key_position,omitempty"`
Backends map[string]BackendDefinition `json:"backends"`
}
func (s2cfg S2StrategyConfig) Validate() error {
if s2cfg.ShardKeySeparator == "" {
return Error("missing required config: shard_key_separator")
}
if err := s2cfg.validateS2IDs(); err != nil {
return err
}
return nil
}
func (s2cfg S2StrategyConfig) validateS2IDs() error {
backendCount := len(s2cfg.Backends)
s2IDs := make([]uint64, backendCount)
for k := range s2cfg.Backends {
if k != defaultBackendS2id {
id, err := strconv.ParseUint(k, 10, 64)
if err != nil {
return fmt.Errorf("[error] Bad S2 ID found in backends: %s", k)
}
s2IDs = append(s2IDs, id)
}
}
if util.ContainsOverlappingS2IDs(s2IDs) {
return fmt.Errorf("[error] Overlapping S2 IDs found in backends: %v", s2cfg.Backends)
}
return nil
}
func s2idFromLatLng(latLng []string) (s2id geos2.CellID, err error) {
if len(latLng) != 2 {
err = Error("lat lng key is not valid")
return
}
lat, err := strconv.ParseFloat(latLng[0], 64)
if err != nil {
err = Error("fail to parse latitude")
return
}
lng, err := strconv.ParseFloat(latLng[1], 64)
if err != nil {
err = Error("fail to parse longitude")
return
}
s2LatLng := geos2.LatLngFromDegrees(lat, lng)
if !s2LatLng.IsValid() {
err = Error("fail to convert lat-long to geos2 objects")
return
}
s2id = geos2.CellIDFromLatLng(s2LatLng)
return
}
func s2idFromSmartID(smartIDComponents []string, pos int) (s2id geos2.CellID, err error) {
if len(smartIDComponents) <= pos {
err = Error("failed to get location from smart-id")
return
}
s2idStr := smartIDComponents[pos]
s2idUint, err := strconv.ParseUint(s2idStr, 10, 64)
if err != nil {
err = Error("failed to parse s2id")
return
}
s2id = geos2.CellID(s2idUint)
return
}
func (s2s *S2Strategy) s2ID(key string) (uint64, error) {
shardKeyComponents := strings.Split(key, s2s.shardKeySeparator)
var s2CellID geos2.CellID
var err error
switch s2s.shardKeyPosition {
case -1:
s2CellID, err = s2idFromLatLng(shardKeyComponents)
default:
s2CellID, err = s2idFromSmartID(shardKeyComponents, s2s.shardKeyPosition)
}
return uint64(s2CellID), err
}
func (s2s *S2Strategy) Shard(key string) (*weaver.Backend, error) {
s2id, err := s2s.s2ID(key)
if err != nil {
return nil, err
}
s2CellID := geos2.CellID(s2id)
for s2Str, backendConfig := range s2s.backends {
cellInt, err := strconv.ParseUint(s2Str, 10, 64)
if err != nil {
continue
}
shardCellID := geos2.CellID(cellInt)
if shardCellID.Contains(s2CellID) {
return backendConfig, nil
}
}
if _, ok := s2s.backends[defaultBackendS2id]; ok {
return s2s.backends[defaultBackendS2id], nil
}
return nil, Error("fail to find backend")
}