-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation of Expire command
- Loading branch information
1 parent
321940c
commit f13e5ed
Showing
7 changed files
with
233 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright 2019 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 ( | ||
"github.com/buraksezer/olric/config" | ||
"github.com/buraksezer/olric/internal/protocol" | ||
"github.com/buraksezer/olric/internal/storage" | ||
"time" | ||
) | ||
|
||
func (db *Olric) localExpire(hkey uint64, dm *dmap, w *writeop) error { | ||
ttl := getTTL(w.timeout) | ||
val := &storage.VData{ | ||
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) 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) | ||
} | ||
return nil | ||
} | ||
|
||
func (db *Olric) expire(w *writeop) error { | ||
member, hkey := db.locateKey(w.dmap, w.key) | ||
if hostCmp(member, db.this) { | ||
// We are on the partition owner. | ||
return db.callExpireOnCluster(hkey, w) | ||
} | ||
// Redirect to the partition owner | ||
req := &protocol.Message{ | ||
DMap: w.dmap, | ||
Key: w.key, | ||
} | ||
_, err := db.requestTo(member.String(), protocol.OpExpire, req) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (db *Olric) exExpireOperation(req *protocol.Message) *protocol.Message { | ||
w := &writeop{} | ||
w.fromReq(req) | ||
return db.prepareResponse(req, db.expire(w)) | ||
} | ||
|
||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright 2019 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 ( | ||
"context" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestDMap_SetExpireStandalone(t *testing.T) { | ||
db, err := newDB(testStandaloneConfig()) | ||
if err != nil { | ||
t.Fatalf("Expected nil. Got: %v", err) | ||
} | ||
defer func() { | ||
err = db.Shutdown(context.Background()) | ||
if err != nil { | ||
db.log.V(2).Printf("[ERROR] Failed to shutdown Olric: %v", err) | ||
} | ||
}() | ||
|
||
key := "mykey" | ||
// Create a new DMap object and put a K/V pair. | ||
dm, err := db.NewDMap("foobar") | ||
if err != nil { | ||
t.Fatalf("Expected nil. Got: %v", err) | ||
} | ||
err = dm.Put(key, "value") | ||
if err != nil { | ||
t.Fatalf("Expected nil. Got: %v", err) | ||
} | ||
|
||
// Get the value and check it. | ||
_, err = dm.Get(key) | ||
if err != nil { | ||
t.Fatalf("Expected nil. Got: %v", err) | ||
} | ||
|
||
err = dm.Expire(key, time.Millisecond) | ||
if err != nil { | ||
t.Fatalf("Expected nil. Got: %v", err) | ||
} | ||
|
||
<-time.After(5 * time.Millisecond) | ||
// Update currentUnixNano to evict the key now. | ||
atomic.StoreInt64(¤tUnixNano, time.Now().UnixNano()) | ||
|
||
// Get the value and check it. | ||
_, err = dm.Get(key) | ||
if err != ErrKeyNotFound { | ||
t.Fatalf("Expected ErrKeyNotFound. Got: %v", err) | ||
} | ||
} | ||
|
||
|
||
func TestDMap_SetExpireKeyNotFound(t *testing.T) { | ||
db, err := newDB(testStandaloneConfig()) | ||
if err != nil { | ||
t.Fatalf("Expected nil. Got: %v", err) | ||
} | ||
defer func() { | ||
err = db.Shutdown(context.Background()) | ||
if err != nil { | ||
db.log.V(2).Printf("[ERROR] Failed to shutdown Olric: %v", err) | ||
} | ||
}() | ||
|
||
key := "mykey" | ||
// Create a new DMap object and put a K/V pair. | ||
dm, err := db.NewDMap("foobar") | ||
if err != nil { | ||
t.Fatalf("Expected nil. Got: %v", err) | ||
} | ||
|
||
err = dm.Expire(key, time.Millisecond) | ||
if err != ErrKeyNotFound { | ||
t.Fatalf("Expected ErrKeyNotFound. Got: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,6 +73,8 @@ const ( | |
OpPipeline | ||
OpPing | ||
OpStats | ||
OpExpire | ||
OpExpireReplica | ||
) | ||
|
||
// StatusCode ... | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters