forked from buraksezer/olric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmap_expire.go
174 lines (158 loc) · 4.49 KB
/
dmap_expire.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
// Copyright 2018-2020 Burak Sezer
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package olric
import (
"fmt"
"time"
"github.com/buraksezer/olric/config"
"github.com/buraksezer/olric/internal/discovery"
"github.com/buraksezer/olric/internal/protocol"
"github.com/buraksezer/olric/internal/storage"
)
func (db *Olric) localExpire(hkey uint64, dm *dmap, w *writeop) error {
ttl := getTTL(w.timeout)
val := &storage.Entry{
Timestamp: w.timestamp,
TTL: ttl,
}
err := dm.storage.UpdateTTL(hkey, val)
if err != nil {
if err == storage.ErrKeyNotFound {
err = ErrKeyNotFound
}
return err
}
dm.updateAccessLog(hkey)
return nil
}
func (db *Olric) asyncExpireOnCluster(hkey uint64, dm *dmap, w *writeop) error {
req := w.toReq(protocol.OpExpireReplica)
// Fire and forget mode.
owners := db.getBackupPartitionOwners(hkey)
for _, owner := range owners {
db.wg.Add(1)
go func(host discovery.Member) {
defer db.wg.Done()
_, err := db.requestTo(host.String(), req)
if err != nil {
if db.log.V(3).Ok() {
db.log.V(3).Printf("[ERROR] Failed to set expire in async mode: %v", err)
}
}
}(owner)
}
return db.localPut(hkey, dm, w)
}
func (db *Olric) syncExpireOnCluster(hkey uint64, dm *dmap, w *writeop) error {
req := w.toReq(protocol.OpExpireReplica)
// Quorum based replication.
var successful int
owners := db.getBackupPartitionOwners(hkey)
for _, owner := range owners {
_, err := db.requestTo(owner.String(), req)
if err != nil {
if db.log.V(3).Ok() {
db.log.V(3).Printf("[ERROR] Failed to call expire command on %s for DMap: %s: %v",
owner, w.dmap, err)
}
continue
}
successful++
}
err := db.localExpire(hkey, dm, w)
if err != nil {
if db.log.V(3).Ok() {
db.log.V(3).Printf("[ERROR] Failed to call expire command on %s for DMap: %s: %v",
db.this, w.dmap, err)
}
} else {
successful++
}
if successful >= db.config.WriteQuorum {
return nil
}
return ErrWriteQuorum
}
func (db *Olric) callExpireOnCluster(hkey uint64, w *writeop) error {
// Get the dmap and acquire its lock
dm, err := db.getDMap(w.dmap, hkey)
if err != nil {
return err
}
dm.Lock()
defer dm.Unlock()
if db.config.ReplicaCount == config.MinimumReplicaCount {
// MinimumReplicaCount is 1. So it's enough to put the key locally. There is no
// other replica host.
return db.localExpire(hkey, dm, w)
}
if db.config.ReplicationMode == config.AsyncReplicationMode {
return db.asyncExpireOnCluster(hkey, dm, w)
} else if db.config.ReplicationMode == config.SyncReplicationMode {
return db.syncExpireOnCluster(hkey, dm, w)
}
return fmt.Errorf("invalid replication mode: %v", db.config.ReplicationMode)
}
func (db *Olric) expire(w *writeop) error {
member, hkey := db.findPartitionOwner(w.dmap, w.key)
if cmpMembersByName(member, db.this) {
// We are on the partition owner.
return db.callExpireOnCluster(hkey, w)
}
// Redirect to the partition owner
req := w.toReq(protocol.OpExpire)
_, err := db.requestTo(member.String(), req)
return err
}
func (db *Olric) expireReplicaOperation(w, r protocol.EncodeDecoder) {
req := r.(*protocol.DMapMessage)
hkey := db.getHKey(req.DMap(), req.Key())
dm, err := db.getBackupDMap(req.DMap(), hkey)
if err != nil {
db.errorResponse(w, err)
return
}
dm.Lock()
defer dm.Unlock()
wo := &writeop{}
wo.fromReq(req)
err = db.localExpire(hkey, dm, wo)
if err != nil {
db.errorResponse(w, err)
return
}
w.SetStatus(protocol.StatusOK)
}
func (db *Olric) exExpireOperation(w, r protocol.EncodeDecoder) {
req := r.(*protocol.DMapMessage)
wo := &writeop{}
wo.fromReq(req)
err := db.expire(wo)
if err != nil {
db.errorResponse(w, err)
return
}
w.SetStatus(protocol.StatusOK)
}
// Expire updates the expiry for the given key. It returns ErrKeyNotFound if the
// DB does not contains the key. It's thread-safe.
func (dm *DMap) Expire(key string, timeout time.Duration) error {
w := &writeop{
dmap: dm.name,
key: key,
timestamp: time.Now().UnixNano(),
timeout: timeout,
}
return dm.db.expire(w)
}