Changed max-memory to uint64. Added TODO commends on how to handle memory checking for key eviction.

This commit is contained in:
Kelvin Clement Mwinuka
2024-03-05 22:45:05 +08:00
parent 28f97656c4
commit 5c347cb7de
6 changed files with 11 additions and 6 deletions

View File

@@ -4,7 +4,7 @@ build-server:
build:
env CC=x86_64-linux-musl-gcc GOOS=linux GOARCH=amd64 DEST=bin/linux/x86_64 make build-server
make run:
run:
make build && docker-compose up --build
test:

View File

@@ -30,7 +30,7 @@ services:
- RESTORE_SNAPSHOT=false
- RESTORE_AOF=true
- AOF_SYNC_STRATEGY=everysec
- MAX_MEMORY=100kb
- MAX_MEMORY=2000kb
- EVICTION_POLICY=allkeys-lfu
# List of server cert/key pairs
- CERT_KEY_PAIR_1=/etc/ssl/certs/echovault/server/server1.crt,/etc/ssl/certs/echovault/server/server1.key

View File

@@ -86,6 +86,8 @@ func (server *Server) GetValue(key string) interface{} {
// This count triggers a snapshot when the threshold is reached.
// The key must be locked prior to calling this function.
func (server *Server) SetValue(_ context.Context, key string, value interface{}) {
// TODO: If max-memory is exceeded and eviction policy is noeviction, do not store the new value
server.store[key] = value
server.updateKeyInCache(key)
@@ -157,4 +159,5 @@ func (server *Server) updateKeyInCache(key string) {
server.lruCache.Update(key)
}
}
// TODO: Check if memory usage is above max-memory. If it is, pop items from the cache until we get under the limit.
}

View File

@@ -127,6 +127,8 @@ func NewServer(opts Opts) *Server {
server.lfuCache = eviction.NewCacheLFU()
server.lruCache = eviction.NewCacheLRU()
// TODO: Start goroutine that continuously reads the mem stats before triggering purge once max-memory is reached
return server
}

View File

@@ -38,7 +38,7 @@ type Config struct {
RestoreSnapshot bool `json:"RestoreSnapshot" yaml:"RestoreSnapshot"`
RestoreAOF bool `json:"RestoreAOF" yaml:"RestoreAOF"`
AOFSyncStrategy string `json:"AOFSyncStrategy" yaml:"AOFSyncStrategy"`
MaxMemory int `json:"MaxMemory" yaml:"MaxMemory"`
MaxMemory uint64 `json:"MaxMemory" yaml:"MaxMemory"`
EvictionPolicy string `json:"EvictionPolicy" yaml:"EvictionPolicy"`
}
@@ -78,7 +78,7 @@ The options are 'always' for syncing on each command, 'everysec' to sync every s
return nil
})
maxMemory := -1
var maxMemory uint64 = 0
flag.Func("max-memory", `Upper memory limit before triggering eviction.
Supported units (kb, mb, gb, tb, pb). There is no limit by default.`, func(memory string) error {
b, err := ParseMemory(memory)

View File

@@ -134,7 +134,7 @@ func AbsInt(n int) int {
}
// ParseMemory returns an integer representing the bytes in the memory string
func ParseMemory(memory string) (int, error) {
func ParseMemory(memory string) (uint64, error) {
// Parse memory strings such as "100mb", "16gb"
memString := memory[0 : len(memory)-2]
bytesInt, err := strconv.ParseInt(memString, 10, 64)
@@ -158,5 +158,5 @@ func ParseMemory(memory string) (int, error) {
return 0, fmt.Errorf("memory unit %s not supported, use (kb, mb, gb, tb, pb) ", memUnit)
}
return int(bytesInt), nil
return uint64(bytesInt), nil
}