This repository has been archived by the owner on May 17, 2022. It is now read-only.
/
main.go
179 lines (149 loc) · 3.64 KB
/
main.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
package main
import (
"log"
"os"
"sync"
"time"
jsoniter "github.com/json-iterator/go"
"github.com/bootjp/turbo-ubiquitous-store/kvs"
"github.com/gomodule/redigo/redis"
)
const (
NodePrimary = iota
NodeSecondary
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
type Node struct {
Conn redis.Conn
Type int
}
type QueueNodes struct {
ActiveNode *Node
queuePrimary *Node
queueSecondary *Node
isRunning bool
mutex sync.Mutex
log *log.Logger
}
type MasterNode = Node
type DistinctInsert interface {
Load(node *MasterNode)
Running() bool
GetActiveNode() *Node
Distinct(*[]kvs.UpdateQueue) *[]kvs.UpdateQueue
}
func (n *QueueNodes) DetachNode(node *Node) {
n.mutex.Lock()
defer n.mutex.Unlock()
switch node.Type {
case NodePrimary:
n.ActiveNode = n.queueSecondary
case NodeSecondary:
n.ActiveNode = n.queuePrimary
}
_, err := n.ActiveNode.Conn.Do("PING")
if err != nil {
log.Fatal("no available node, please recheck queue node health check")
}
}
func (n *QueueNodes) Running() bool {
n.mutex.Lock()
defer n.mutex.Unlock()
return n.isRunning
}
func (n *QueueNodes) GetActiveNode() *Node {
n.mutex.Lock()
defer n.mutex.Unlock()
return n.ActiveNode
}
func NewDistinctExecutor() *QueueNodes {
pconn, err := redis.Dial("tcp", os.Getenv("PRIMARY_REDIS_HOST"))
if err != nil {
log.Println(err)
log.Fatal("failed to connect primary storage.")
}
sconn, err := redis.Dial("tcp", os.Getenv("SECONDARY_REDIS_HOST"))
if err != nil {
log.Println(err)
log.Fatal("failed to connect secondary queue .")
}
return &QueueNodes{
ActiveNode: &Node{Conn: pconn, Type: NodePrimary},
queuePrimary: &Node{Conn: pconn, Type: NodePrimary},
queueSecondary: &Node{Conn: sconn, Type: NodeSecondary},
isRunning: false,
log: log.New(os.Stdout, "distinct_executor: ", log.Ltime),
}
}
const updateQueueKey = "tus_queue"
const windowSize = 1 * time.Minute
func (n *QueueNodes) Distinct(buffer *[]kvs.UpdateQueue) map[string]kvs.UpdateQueue {
m := map[string]kvs.UpdateQueue{}
for _, queue := range *buffer {
if _, ok := m[queue.Key]; !ok {
m[queue.Key] = queue
} else {
// データが新しければ新しいものに書き換える
if m[queue.Key].UpdateAt < queue.UpdateAt {
m[queue.Key] = queue
}
}
}
return m
}
func (n *QueueNodes) Load(m *MasterNode) {
if n.Running() {
return
}
n.mutex.Lock()
n.isRunning = true
n.mutex.Unlock()
var buffer []kvs.UpdateQueue
for range time.Tick(windowSize) {
n.log.Println("tick start")
qcount, err := redis.Int(n.ActiveNode.Conn.Do("LLEN", updateQueueKey))
if err != nil {
n.log.Println(err)
n.DetachNode(n.ActiveNode)
}
if qcount == 0 {
n.log.Println("distinct executor waiting queue")
continue
}
for qcount > 0 {
memory := &kvs.UpdateQueue{}
byte, err := redis.Bytes(n.ActiveNode.Conn.Do("RPOP", updateQueueKey))
if err := json.Unmarshal(byte, memory); err != nil {
n.log.Println(err)
}
buffer = append(buffer, *memory)
qcount, err = redis.Int(n.ActiveNode.Conn.Do("LLEN", updateQueueKey))
if err != nil {
n.log.Println(err)
n.DetachNode(n.ActiveNode)
}
}
if len(buffer) == 0 {
continue
}
data := n.Distinct(&buffer)
for k, v := range data {
json, err := json.MarshalToString(v)
if err != nil {
n.log.Println(err)
}
_, err = m.Conn.Do("SET", k, json)
if err != nil {
n.log.Println(err, v)
}
}
}
}
func main() {
mconn, err := redis.Dial("tcp", os.Getenv("MASTER_REDIS_HOST"))
if err != nil {
log.Println(err)
log.Fatal("failed to connect master storage.")
}
NewDistinctExecutor().Load(&Node{Conn: mconn, Type: NodePrimary})
}