Skip to content

Commit 1336422

Browse files
committed
scanner.Scan() have limit 64k bytes string length, fix #1305
Signed-off-by: Slach <[email protected]>
1 parent 405299d commit 1336422

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

pkg/keeper/keeper.go

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8+
"io"
89
"os"
910
"path"
1011
"strconv"
@@ -202,33 +203,46 @@ func (k *Keeper) Restore(dumpFile, prefix string) error {
202203
if k.root != "" && !strings.HasPrefix(prefix, k.root) {
203204
prefix = path.Join(k.root, prefix)
204205
}
205-
scanner := bufio.NewScanner(f)
206-
for scanner.Scan() {
206+
reader := bufio.NewReader(f)
207+
for {
208+
line, readErr := reader.ReadString('\n')
209+
if readErr != nil && readErr != io.EOF {
210+
return errors.Wrapf(readErr, "can't read %s", dumpFile)
211+
}
212+
line = strings.TrimSuffix(line, "\n")
213+
if line == "" {
214+
if readErr == io.EOF {
215+
break
216+
}
217+
continue
218+
}
207219
node := DumpNode{}
208-
binaryData := scanner.Bytes()
209-
if err = json.Unmarshal(binaryData, &node); err != nil {
220+
binaryData := []byte(line)
221+
if binaryUnmarshalErr := json.Unmarshal(binaryData, &node); binaryUnmarshalErr != nil {
210222
//convert from old format
211223
nodeString := DumpNodeString{}
212224
if stringUnmarshalErr := json.Unmarshal(binaryData, &nodeString); stringUnmarshalErr != nil {
213-
return errors.WithStack(fmt.Errorf("k.Restore can't read data binaryErr=%v, stringErr=%v", err, stringUnmarshalErr))
225+
return errors.WithStack(fmt.Errorf("k.Restore can't read data binaryErr=%v, stringErr=%v", binaryUnmarshalErr, stringUnmarshalErr))
214226
}
215227
}
216228
node.Path = path.Join(prefix, node.Path)
217229
version := int32(0)
218-
_, stat, err := k.conn.Get(node.Path)
219-
if err != nil {
220-
_, err = k.conn.Create(node.Path, node.Value, 0, zk.WorldACL(zk.PermAll))
221-
if err != nil {
222-
return errors.Wrapf(err, "can't create znode %s, error", node.Path)
230+
_, stat, keeperErr := k.conn.Get(node.Path)
231+
if keeperErr != nil {
232+
_, keeperErr = k.conn.Create(node.Path, node.Value, 0, zk.WorldACL(zk.PermAll))
233+
if keeperErr != nil {
234+
return errors.Wrapf(keeperErr, "can't create znode %s, error", node.Path)
223235
}
224236
} else {
225237
version = stat.Version
226-
_, err = k.conn.Set(node.Path, node.Value, version)
238+
_, keeperErr = k.conn.Set(node.Path, node.Value, version)
239+
if keeperErr != nil {
240+
return errors.Wrapf(keeperErr, "can't set znode %s, error", node.Path)
241+
}
242+
}
243+
if readErr == io.EOF {
244+
break
227245
}
228-
}
229-
230-
if err = scanner.Err(); err != nil {
231-
return errors.WithStack(fmt.Errorf("can't scan %s, error: %s", dumpFile, err))
232246
}
233247
return nil
234248
}

0 commit comments

Comments
 (0)