forked from Terry-Mao/gopush-cluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql.go
168 lines (154 loc) · 4.62 KB
/
mysql.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
// Copyright © 2014 Terry Mao, LiuDing All rights reserved.
// This file is part of gopush-cluster.
// gopush-cluster is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// gopush-cluster is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with gopush-cluster. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"database/sql"
"encoding/json"
"errors"
"github.com/Terry-Mao/gopush-cluster/hash"
"github.com/Terry-Mao/gopush-cluster/rpc"
_ "github.com/go-sql-driver/mysql"
"github.com/golang/glog"
"time"
)
const (
defaultMYSQLNode = "node1"
saveSQL = "INSERT INTO message(sub,gid,mid,expire,msg,ctime,mtime) VALUES(?,?,?,?,?,?,?)"
getSQL = "SELECT mid, gid, expire, msg FROM message WHERE sub=? AND mid>?"
delExpireSQL = "DELETE FROM message WHERE expire<=?"
delKeySQL = "DELETE FROM message WHERE sub=?"
)
var (
ErrNoMySQLConn = errors.New("can't get a mysql db")
)
// MySQL Storage struct
type MySQLStorage struct {
// n
pool map[string]*sql.DB
ketama *hash.Ketama
}
// NewMySQLStorage initialize mysql pool and consistency hash ring.
func NewMySQLStorage() *MySQLStorage {
dbPool := make(map[string]*sql.DB)
for n, source := range Conf.MySQLSource {
db, err := sql.Open("mysql", source)
if err != nil {
glog.Errorf("sql.Open(\"mysql\", %s) failed (%v)", source, err)
panic(err)
}
dbPool[n] = db
}
s := &MySQLStorage{pool: dbPool, ketama: hash.NewKetama(len(dbPool), 255)}
go s.clean()
return s
}
// Save implements the Storage Save method.
func (s *MySQLStorage) Save(key string, msg json.RawMessage, mid int64, gid uint, expire uint) error {
db := s.getConn(key)
if db == nil {
return ErrNoMySQLConn
}
now := time.Now()
_, err := db.Exec(saveSQL, key, gid, mid, now.Unix()+int64(expire), []byte(msg), now, now)
if err != nil {
glog.Errorf("db.Exec(%s,%s,%d,%d,%d,%s,now,now) failed (%v)", saveSQL, key, 0, mid, expire, string(msg), err)
return err
}
return nil
}
// Get implements the Storage Get method.
func (s *MySQLStorage) Get(key string, mid int64) ([]*rpc.Message, error) {
db := s.getConn(key)
if db == nil {
return nil, ErrNoMySQLConn
}
now := time.Now().Unix()
rows, err := db.Query(getSQL, key, mid)
if err != nil {
glog.Errorf("db.Query(%s,%s,%d,now) failed (%v)", getSQL, key, mid, err)
return nil, err
}
expire := int64(0)
cmid := int64(0)
cgid := uint(0)
msg := json.RawMessage([]byte{})
msgs := []*rpc.Message{}
for rows.Next() {
if err := rows.Scan(&cmid, &cgid, &expire, &msg); err != nil {
glog.Errorf("rows.Scan() failed (%v)", err)
return nil, err
}
if now > expire {
glog.Warningf("user_key: \"%s\" mid: %d expired", key, cmid)
continue
}
msgs = append(msgs, &rpc.Message{MsgId: cmid, GroupId: cgid, Msg: msg})
}
return msgs, nil
}
// Del implements the Storage DelKey method.
func (s *MySQLStorage) Del(key string) error {
db := s.getConn(key)
if db == nil {
return ErrNoMySQLConn
}
res, err := db.Exec(delKeySQL, key)
if err != nil {
glog.Errorf("db.Exec(\"%s\", \"%s\") error(%v)", delKeySQL, key, err)
return err
}
rows, err := res.RowsAffected()
if err != nil {
glog.Errorf("res.RowsAffected() error(%v)", err)
return err
}
glog.Infof("user_key: \"%s\" clean message num: %d", rows)
return nil
}
// clean delete expired messages peroridly.
func (s *MySQLStorage) clean() {
for {
glog.Info("clean mysql expired message start")
now := time.Now().Unix()
affect := int64(0)
for _, db := range s.pool {
res, err := db.Exec(delExpireSQL, now)
if err != nil {
glog.Errorf("db.Exec(\"%s\", now) failed (%v)", delExpireSQL, err)
continue
}
aff, err := res.RowsAffected()
if err != nil {
glog.Errorf("res.RowsAffected() error(%v)", err)
continue
}
affect += aff
}
glog.Infof("clean mysql expired message finish, num: %d", affect)
time.Sleep(Conf.MySQLClean)
}
}
// getConn get the connection of matching with key using ketama hash
func (s *MySQLStorage) getConn(key string) *sql.DB {
node := defaultMYSQLNode
if len(s.pool) > 1 {
node = s.ketama.Node(key)
}
p, ok := s.pool[node]
if !ok {
glog.Warningf("no exists key:\"%s\" in mysql pool", key)
return nil
}
glog.V(1).Infof("key:\"%s\", hit node:\"%s\"", key, node)
return p
}