-
Notifications
You must be signed in to change notification settings - Fork 576
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
253 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,4 @@ logs | |
# jetbrains | ||
.idea | ||
*.iml | ||
|
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,158 @@ | ||
package aof | ||
|
||
import ( | ||
"github.com/hdt3213/godis/config" | ||
"github.com/hdt3213/godis/datastruct/dict" | ||
List "github.com/hdt3213/godis/datastruct/list" | ||
"github.com/hdt3213/godis/datastruct/set" | ||
SortedSet "github.com/hdt3213/godis/datastruct/sortedset" | ||
"github.com/hdt3213/godis/interface/database" | ||
"github.com/hdt3213/godis/lib/logger" | ||
rdb "github.com/hdt3213/rdb/encoder" | ||
"github.com/hdt3213/rdb/model" | ||
"io/ioutil" | ||
"os" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
func (handler *Handler) Rewrite2RDB() error { | ||
ctx, err := handler.startRewrite2RDB() | ||
if err != nil { | ||
return err | ||
} | ||
err = handler.rewrite2RDB(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
rdbFilename := config.Properties.RDBFilename | ||
if rdbFilename == "" { | ||
rdbFilename = "dump.rdb" | ||
} | ||
err = ctx.tmpFile.Close() | ||
if err != nil { | ||
return err | ||
} | ||
err = os.Rename(ctx.tmpFile.Name(), rdbFilename) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (handler *Handler) startRewrite2RDB() (*RewriteCtx, error) { | ||
handler.pausingAof.Lock() // pausing aof | ||
defer handler.pausingAof.Unlock() | ||
|
||
err := handler.aofFile.Sync() | ||
if err != nil { | ||
logger.Warn("fsync failed") | ||
return nil, err | ||
} | ||
|
||
// get current aof file size | ||
fileInfo, _ := os.Stat(handler.aofFilename) | ||
filesize := fileInfo.Size() | ||
// create tmp file | ||
file, err := ioutil.TempFile("", "*.aof") | ||
if err != nil { | ||
logger.Warn("tmp file create failed") | ||
return nil, err | ||
} | ||
return &RewriteCtx{ | ||
tmpFile: file, | ||
fileSize: filesize, | ||
}, nil | ||
} | ||
|
||
func (handler *Handler) rewrite2RDB(ctx *RewriteCtx) error { | ||
// load aof tmpFile | ||
tmpHandler := handler.newRewriteHandler() | ||
tmpHandler.LoadAof(int(ctx.fileSize)) | ||
encoder := rdb.NewEncoder(ctx.tmpFile).EnableCompress() | ||
err := encoder.WriteHeader() | ||
if err != nil { | ||
return err | ||
} | ||
auxMap := map[string]string{ | ||
"redis-ver": "6.0.0", | ||
"redis-bits": "64", | ||
"aof-preamble": "0", | ||
"ctime": strconv.FormatInt(time.Now().Unix(), 10), | ||
} | ||
for k, v := range auxMap { | ||
err := encoder.WriteAux(k, v) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
for i := 0; i < config.Properties.Databases; i++ { | ||
keyCount, ttlCount := tmpHandler.db.GetDBSize(i) | ||
if keyCount == 0 { | ||
continue | ||
} | ||
err = encoder.WriteDBHeader(uint(i), uint64(keyCount), uint64(ttlCount)) | ||
if err != nil { | ||
return err | ||
} | ||
// dump db | ||
var err2 error | ||
tmpHandler.db.ForEach(i, func(key string, entity *database.DataEntity, expiration *time.Time) bool { | ||
var opts []interface{} | ||
if expiration != nil { | ||
opts = append(opts, rdb.WithTTL(uint64(expiration.UnixNano()/1e6))) | ||
} | ||
switch obj := entity.Data.(type) { | ||
case []byte: | ||
err = encoder.WriteStringObject(key, obj, opts...) | ||
case *List.LinkedList: | ||
vals := make([][]byte, 0, obj.Len()) | ||
obj.ForEach(func(i int, v interface{}) bool { | ||
bytes, _ := v.([]byte) | ||
vals = append(vals, bytes) | ||
return true | ||
}) | ||
err = encoder.WriteListObject(key, vals, opts...) | ||
case *set.Set: | ||
vals := make([][]byte, 0, obj.Len()) | ||
obj.ForEach(func(m string) bool { | ||
vals = append(vals, []byte(m)) | ||
return true | ||
}) | ||
err = encoder.WriteSetObject(key, vals, opts...) | ||
case dict.Dict: | ||
hash := make(map[string][]byte) | ||
obj.ForEach(func(key string, val interface{}) bool { | ||
bytes, _ := val.([]byte) | ||
hash[key] = bytes | ||
return true | ||
}) | ||
err = encoder.WriteHashMapObject(key, hash, opts...) | ||
case *SortedSet.SortedSet: | ||
var entries []*model.ZSetEntry | ||
obj.ForEach(int64(0), obj.Len(), true, func(element *SortedSet.Element) bool { | ||
entries = append(entries, &model.ZSetEntry{ | ||
Member: element.Member, | ||
Score: element.Score, | ||
}) | ||
return true | ||
}) | ||
err = encoder.WriteZSetObject(key, entries, opts...) | ||
} | ||
if err != nil { | ||
err2 = err | ||
return false | ||
} | ||
return true | ||
}) | ||
if err2 != nil { | ||
return err2 | ||
} | ||
} | ||
err = encoder.WriteEnd() | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
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
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