-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implementing INFO and allkeys-random key eviction
- Loading branch information
1 parent
9acc214
commit 38f156a
Showing
8 changed files
with
138 additions
and
6 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
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 |
---|---|---|
@@ -1,21 +1,40 @@ | ||
package core | ||
|
||
import "github.com/dicedb/dice/config" | ||
import ( | ||
"github.com/dicedb/dice/config" | ||
) | ||
|
||
// Evicts the first key it found while iterating the map | ||
// TODO: Make it efficient by doing thorough sampling | ||
func evictFirst() { | ||
for k := range store { | ||
delete(store, k) | ||
Del(k) | ||
return | ||
} | ||
} | ||
|
||
// Randomly removes keys to make space for the new data added. | ||
// The number of keys removed will be sufficient to free up least 10% space | ||
func evictAllkeysRandom() { | ||
evictCount := int64(config.EvictionRatio * float64(config.KeysLimit)) | ||
// Iteration of Golang dictionary can be considered as a random | ||
// because it depends on the hash of the inserted key | ||
for k := range store { | ||
Del(k) | ||
evictCount-- | ||
if evictCount <= 0 { | ||
break | ||
} | ||
} | ||
} | ||
|
||
// TODO: Make the eviction strategy configuration driven | ||
// TODO: Support multiple eviction strategies | ||
func evict() { | ||
switch config.EvictionStrategy { | ||
case "simple-first": | ||
evictFirst() | ||
case "allkeys-random": | ||
evictAllkeysRandom() | ||
} | ||
} |
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,7 @@ | ||
package core | ||
|
||
var KeyspaceStat [4]map[string]int | ||
|
||
func UpdateDBStat(num int, metric string, value int) { | ||
KeyspaceStat[num][metric] = value | ||
} |
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,56 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"math/rand" | ||
"net" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"github.com/dicedb/dice/core" | ||
) | ||
|
||
func getRandomKeyValue() (string, int64) { | ||
token := int64(rand.Uint64() % 5000000) | ||
return "k" + strconv.FormatInt(token, 10), token | ||
} | ||
|
||
func stormSet(wg *sync.WaitGroup) { | ||
defer wg.Done() | ||
conn, err := net.Dial("tcp", "localhost:7379") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for { | ||
time.Sleep(500 * time.Millisecond) | ||
k, v := getRandomKeyValue() | ||
var buf [512]byte | ||
cmd := fmt.Sprintf("SET %s %d", k, v) | ||
fmt.Println(cmd) | ||
_, err = conn.Write(core.Encode(strings.Split(cmd, " "), false)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
_, err = conn.Read(buf[:]) | ||
if err != nil { | ||
if err == io.EOF { | ||
return | ||
} | ||
panic(err) | ||
} | ||
} | ||
conn.Close() | ||
} | ||
|
||
func main() { | ||
var wg sync.WaitGroup | ||
for i := 0; i < 5; i++ { | ||
wg.Add(1) | ||
go stormSet(&wg) | ||
} | ||
wg.Wait() | ||
} |