func (cmd *cmdSync) SendCmd(master string) (net.Conn, int64) { c, wait := openSyncConn(master, args.auth) for { select { case nsize := <-wait: if nsize == 0 { log.Info("+") } else { return c, nsize } case <-time.After(time.Second): log.Info("-") } } }
func (cmd *cmdDump) DumpRDBFile(reader *bufio.Reader, writer *bufio.Writer, nsize int64) { var nread atomic2.Int64 wait := make(chan struct{}) go func() { defer close(wait) p := make([]byte, WriterBufferSize) for nsize != nread.Get() { cnt := iocopy(reader, writer, p, int(nsize-nread.Get())) nread.Add(int64(cnt)) } flushWriter(writer) }() for done := false; !done; { select { case <-wait: done = true case <-time.After(time.Second): } n := nread.Get() p := 100 * n / nsize log.Infof("total = %d - %12d [%3d%%]\n", nsize, n, p) } log.Info("dump: rdb done") }
func (cmd *cmdRestore) RestoreRDBFile(reader *bufio.Reader, target string, nsize int64) { pipe := newRDBLoader(reader, args.parallel*32) wait := make(chan struct{}) go func() { defer close(wait) group := make(chan int, args.parallel) for i := 0; i < cap(group); i++ { go func() { defer func() { group <- 0 }() c := openRedisConn(target) defer c.Close() var lastdb uint32 = 0 for e := range pipe { if !acceptDB(e.DB) { continue } if e.DB != lastdb { lastdb = e.DB selectDB(c, lastdb) } restoreRdbEntry(c, e) cmd.nobjs.Incr() } }() } for i := 0; i < cap(group); i++ { <-group } }() for done := false; !done; { select { case <-wait: done = true case <-time.After(time.Second): } n, o := cmd.nread.Get(), cmd.nobjs.Get() if nsize != 0 { p := 100 * n / nsize log.Infof("total = %d - %12d [%3d%%] objs=%d\n", nsize, n, p, o) } else { log.Infof("total = %12d objs=%d\n", n, o) } } log.Info("restore: rdb done") }
func (cmd *cmdDecode) Main() { input, output := args.input, args.output if len(input) == 0 { input = "/dev/stdin" } if len(output) == 0 { output = "/dev/stdout" } log.Infof("decode from '%s' to '%s'\n", input, output) var readin io.ReadCloser var nsize int64 if input != "/dev/stdin" { readin, nsize = openReadFile(input) defer readin.Close() } else { readin, nsize = os.Stdin, 0 } var saveto io.WriteCloser if output != "/dev/stdout" { saveto = openWriteFile(output) defer saveto.Close() } else { saveto = os.Stdout } reader := bufio.NewReaderSize(ioutils.NewCountReader(readin, &cmd.nread), ReaderBufferSize) writer := bufio.NewWriterSize(ioutils.NewCountWriter(saveto, &cmd.nsave), WriterBufferSize) ipipe := newRDBLoader(reader, args.parallel*32) opipe := make(chan string, cap(ipipe)) go func() { defer close(opipe) group := make(chan int, args.parallel) for i := 0; i < cap(group); i++ { go func() { defer func() { group <- 0 }() cmd.decoderMain(ipipe, opipe) }() } for i := 0; i < cap(group); i++ { <-group } }() wait := make(chan struct{}) go func() { defer close(wait) for s := range opipe { if _, err := writer.WriteString(s); err != nil { log.PanicError(err, "write string failed") } flushWriter(writer) } }() for done := false; !done; { select { case <-wait: done = true case <-time.After(time.Second): } n, w, o := cmd.nread.Get(), cmd.nsave.Get(), cmd.nobjs.Get() if nsize != 0 { p := 100 * n / nsize log.Infof("total = %d - %12d [%3d%%] write=%-12d objs=%d\n", nsize, n, p, w, o) } else { log.Infof("total = %12d write=%-12d objs=%d\n", n, w, o) } } log.Info("done") }