-
Notifications
You must be signed in to change notification settings - Fork 801
/
Copy pathelasticsearch_masterinfo.go
175 lines (148 loc) · 3.6 KB
/
elasticsearch_masterinfo.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
package river
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"sync"
"github.com/juju/errors"
"github.com/siddontang/go-mysql-elasticsearch/elastic"
"github.com/siddontang/go-mysql/mysql"
"gopkg.in/birkirb/loggers.v1/log"
)
type elasticsearchMasterInfo struct {
sync.RWMutex
es *elastic.Client
Name string
Pos uint32
index string
docType string
id string
}
func loadElasticsearchMasterInfo(dataPath string) (masterInfo, error) {
var m elasticsearchMasterInfo
if !strings.HasPrefix(dataPath, "es:") {
return &m, errors.Errorf("error elasticsearch prefix: %s", dataPath)
}
esURL, err := url.Parse(dataPath[3:])
if err != nil {
return &m, err
}
cfg := new(elastic.ClientConfig)
cfg.Addr = esURL.Host
if esURL.User != nil {
cfg.User = esURL.User.Username()
cfg.Password, _ = esURL.User.Password()
}
cfg.Https = esURL.Scheme == "https"
m.es = elastic.NewClient(cfg)
paths := strings.Split(esURL.Path, "/")
m.index = paths[1]
m.docType = paths[2]
m.id = "1"
id := esURL.Query().Get("id")
if id != "" {
m.id = id
}
err = m.loadMasterInfo()
if err != nil {
return nil, err
}
return &m, nil
}
func (m *elasticsearchMasterInfo) Save(pos mysql.Position) error {
log.Infof("save position %s", pos)
m.Lock()
defer m.Unlock()
m.Name = pos.Name
m.Pos = pos.Pos
doc := map[string]interface{}{
"name": m.Name,
"pos": m.Pos,
}
err := m.es.Update(m.index, m.docType, m.id, doc)
if err != nil {
log.Errorf("ES MasterInfo save error: %s", err)
return err
}
return nil
}
func (m *elasticsearchMasterInfo) Position() mysql.Position {
m.RLock()
defer m.RUnlock()
return mysql.Position{
Name: m.Name,
Pos: m.Pos,
}
}
func (m *elasticsearchMasterInfo) Close() error {
pos := m.Position()
return m.Save(pos)
}
func (m *elasticsearchMasterInfo) loadMasterInfo() error {
mapping, err := m.es.GetMapping(m.index, m.docType)
if err != nil || mapping.Code == http.StatusNotFound {
err = m.createMapping()
}
info, err := m.es.Get(m.index, m.docType, m.id)
if err == nil && (info.Code == http.StatusOK || info.Code == http.StatusNotFound) {
item := info.ResponseItem
if item.Found {
source := item.Source
m.Name = source["name"].(string)
m.Pos = uint32(source["pos"].(float64))
}
return nil
}
return errors.Wrap(err, errors.New("loadMasterInfo error"))
}
func (m *elasticsearchMasterInfo) createMapping() error {
version, _ := m.getEsVersion()
nameType := "keyword"
if version < 5 {
nameType = "string"
}
mapping := map[string]interface{}{
"properties": map[string]interface{}{
"name": map[string]interface{}{
"type": nameType,
},
"pos": map[string]interface{}{
"type": "long",
},
},
}
return m.es.CreateMapping(m.index, m.docType, mapping)
}
func (m *elasticsearchMasterInfo) getEsVersion() (int64, error) {
reqURL := fmt.Sprintf("%s://%s/", m.es.Protocol, m.es.Addr)
resp, err := m.es.DoRequest("GET", reqURL, bytes.NewBuffer(nil))
if err != nil {
return 0, err
}
defer resp.Body.Close()
var esinfo EsinfoResponse
err = json.NewDecoder(resp.Body).Decode(&esinfo)
version := esinfo.Version.Number
if version == "" {
return 0, errors.Errorf("unknow version")
}
v := regexp.MustCompile("^\\d+").FindString(version)
mainVersion, err := strconv.ParseInt(v, 10, 32)
if err == nil {
return mainVersion, nil
}
return 0, errors.Errorf("unknow version")
}
type EsinfoResponse struct {
Name string `json:"name"`
ClusterName string `json:"cluster_name"`
Version EsVersionResponse `json:"version"`
}
type EsVersionResponse struct {
Number string
}