forked from CodisLabs/codis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binlog.go
169 lines (150 loc) · 3.49 KB
/
binlog.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
// Copyright 2014 Wandoujia Inc. All Rights Reserved.
// Licensed under the MIT (MIT-LICENSE.txt) license.
package binlog
import (
"container/list"
"sync"
"github.com/wandoulabs/codis/extern/redis-binlog/pkg/store"
"github.com/wandoulabs/codis/extern/redis-port/pkg/libs/errors"
"github.com/wandoulabs/codis/extern/redis-port/pkg/libs/log"
)
var (
ErrClosed = errors.Static("binlog has been closed")
)
type Binlog struct {
mu sync.Mutex
db store.Database
splist list.List
itlist list.List
serial uint64
}
func New(db store.Database) *Binlog {
return &Binlog{db: db}
}
func (b *Binlog) acquire() error {
b.mu.Lock()
if b.db != nil {
return nil
}
b.mu.Unlock()
return errors.Trace(ErrClosed)
}
func (b *Binlog) release() {
b.mu.Unlock()
}
func (b *Binlog) commit(bt *store.Batch, fw *Forward) error {
if bt.Len() == 0 {
return nil
}
if err := b.db.Commit(bt); err != nil {
log.WarnErrorf(err, "binlog commit failed")
return err
}
for i := b.itlist.Len(); i != 0; i-- {
v := b.itlist.Remove(b.itlist.Front()).(*binlogIterator)
v.Close()
}
b.serial++
return nil
}
func (b *Binlog) getRowValue(key []byte) ([]byte, error) {
return b.db.Get(key)
}
func (b *Binlog) getIterator() (it *binlogIterator) {
if e := b.itlist.Front(); e != nil {
return b.itlist.Remove(e).(*binlogIterator)
}
return &binlogIterator{
Iterator: b.db.NewIterator(),
serial: b.serial,
}
}
func (b *Binlog) putIterator(it *binlogIterator) {
if it.serial == b.serial && it.Error() == nil {
b.itlist.PushFront(it)
} else {
it.Close()
}
}
func (b *Binlog) Close() {
if err := b.acquire(); err != nil {
return
}
defer b.release()
log.Infof("binlog is closing ...")
for i := b.splist.Len(); i != 0; i-- {
v := b.splist.Remove(b.splist.Front()).(*BinlogSnapshot)
v.Close()
}
for i := b.itlist.Len(); i != 0; i-- {
v := b.itlist.Remove(b.itlist.Front()).(*binlogIterator)
v.Close()
}
if b.db != nil {
b.db.Close()
b.db = nil
}
log.Infof("binlog is closed")
}
func (b *Binlog) NewSnapshot() (*BinlogSnapshot, error) {
if err := b.acquire(); err != nil {
return nil, err
}
defer b.release()
sp := &BinlogSnapshot{sp: b.db.NewSnapshot()}
b.splist.PushBack(sp)
log.Infof("binlog create new snapshot, address = %p", sp)
return sp, nil
}
func (b *Binlog) ReleaseSnapshot(sp *BinlogSnapshot) {
if err := b.acquire(); err != nil {
return
}
defer b.release()
log.Infof("binlog release snapshot, address = %p", sp)
for i := b.splist.Len(); i != 0; i-- {
v := b.splist.Remove(b.splist.Front()).(*BinlogSnapshot)
if v != sp {
b.splist.PushBack(v)
}
}
sp.Close()
}
func (b *Binlog) Reset() error {
if err := b.acquire(); err != nil {
return err
}
defer b.release()
log.Infof("binlog is reseting...")
for i := b.splist.Len(); i != 0; i-- {
v := b.splist.Remove(b.splist.Front()).(*BinlogSnapshot)
v.Close()
}
for i := b.itlist.Len(); i != 0; i-- {
v := b.itlist.Remove(b.itlist.Front()).(*binlogIterator)
v.Close()
}
if err := b.db.Clear(); err != nil {
b.db.Close()
b.db = nil
log.ErrorErrorf(err, "binlog reset failed")
return err
} else {
b.serial++
log.Infof("binlog is reset")
return nil
}
}
func (b *Binlog) compact(start, limit []byte) error {
if err := b.db.Compact(start, limit); err != nil {
log.ErrorErrorf(err, "binlog compact failed")
return err
} else {
return nil
}
}
func errArguments(format string, v ...interface{}) error {
err := errors.Errorf(format, v...)
log.DebugErrorf(err, "call binlog function with invalid arguments")
return err
}