2016-03-05 02:08:16 +03:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2016-04-01 02:26:36 +03:00
|
|
|
"github.com/tidwall/resp"
|
2018-04-19 18:43:32 +03:00
|
|
|
"github.com/tidwall/tile38/pkg/core"
|
|
|
|
"github.com/tidwall/tile38/pkg/log"
|
|
|
|
"github.com/tidwall/tile38/pkg/server"
|
2016-03-05 02:08:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var errNoLongerFollowing = errors.New("no longer following")
|
|
|
|
|
|
|
|
const checksumsz = 512 * 1024
|
|
|
|
|
Lua scripting feature. (#224)
* Start on lua scripting
* Implement evalsha, script load, script exists, and script flush
* Type conversions from lua to resp/json.
Refactor to make luastate and luascripts persistent in the controller.
* Change controller.command and all underlying commands to return resp.Value.
Serialize only during the ouput.
* First stab at tile38 call from lua
* Change tile38 into tile38.call in Lua
* Property return errors from scripts
* Minor refactoring. No locking on script run
* Cleanup/refactoring
* Create a pool of 5 lua states, allow for more as needed. Refactor.
* Use safe map for scripts. Add a limit for max number of lua states. Refactor.
* Refactor
* Refactor script commands into atomic, read-only, and non-atomic classes.
Proper locking for all three classes.
Add tests for scripts
* More tests for scripts
* Properly escape newlines in lua-produced errors
* Better test for readonly failure
* Correctly convert ok/err messages between lua and resp.
Add pcall, sha1hex, error_reply, status_reply functions to tile38 namespace in lua.
* Add pcall test. Change writeErr to work with string argument
* Make sure eval/evalsha never attempt to write AOF
* Add eval-set and eval-get to benchmarks
* Fix eval benchmark tests, add more
* Improve benchmarks
* Optimizations and refactoring.
* Add lua memtest
* Typo
* Add dependency
* golint fixes
* gofmt fixes
* Add scripting commands to the core/commands.json
* Use ARGV for args inside lua
2017-10-05 18:20:40 +03:00
|
|
|
func (c *Controller) cmdFollow(msg *server.Message) (res resp.Value, err error) {
|
2016-04-01 02:26:36 +03:00
|
|
|
start := time.Now()
|
|
|
|
vs := msg.Values[1:]
|
|
|
|
var ok bool
|
2016-03-05 02:08:16 +03:00
|
|
|
var host, sport string
|
Lua scripting feature. (#224)
* Start on lua scripting
* Implement evalsha, script load, script exists, and script flush
* Type conversions from lua to resp/json.
Refactor to make luastate and luascripts persistent in the controller.
* Change controller.command and all underlying commands to return resp.Value.
Serialize only during the ouput.
* First stab at tile38 call from lua
* Change tile38 into tile38.call in Lua
* Property return errors from scripts
* Minor refactoring. No locking on script run
* Cleanup/refactoring
* Create a pool of 5 lua states, allow for more as needed. Refactor.
* Use safe map for scripts. Add a limit for max number of lua states. Refactor.
* Refactor
* Refactor script commands into atomic, read-only, and non-atomic classes.
Proper locking for all three classes.
Add tests for scripts
* More tests for scripts
* Properly escape newlines in lua-produced errors
* Better test for readonly failure
* Correctly convert ok/err messages between lua and resp.
Add pcall, sha1hex, error_reply, status_reply functions to tile38 namespace in lua.
* Add pcall test. Change writeErr to work with string argument
* Make sure eval/evalsha never attempt to write AOF
* Add eval-set and eval-get to benchmarks
* Fix eval benchmark tests, add more
* Improve benchmarks
* Optimizations and refactoring.
* Add lua memtest
* Typo
* Add dependency
* golint fixes
* gofmt fixes
* Add scripting commands to the core/commands.json
* Use ARGV for args inside lua
2017-10-05 18:20:40 +03:00
|
|
|
|
2016-04-01 02:26:36 +03:00
|
|
|
if vs, host, ok = tokenval(vs); !ok || host == "" {
|
Lua scripting feature. (#224)
* Start on lua scripting
* Implement evalsha, script load, script exists, and script flush
* Type conversions from lua to resp/json.
Refactor to make luastate and luascripts persistent in the controller.
* Change controller.command and all underlying commands to return resp.Value.
Serialize only during the ouput.
* First stab at tile38 call from lua
* Change tile38 into tile38.call in Lua
* Property return errors from scripts
* Minor refactoring. No locking on script run
* Cleanup/refactoring
* Create a pool of 5 lua states, allow for more as needed. Refactor.
* Use safe map for scripts. Add a limit for max number of lua states. Refactor.
* Refactor
* Refactor script commands into atomic, read-only, and non-atomic classes.
Proper locking for all three classes.
Add tests for scripts
* More tests for scripts
* Properly escape newlines in lua-produced errors
* Better test for readonly failure
* Correctly convert ok/err messages between lua and resp.
Add pcall, sha1hex, error_reply, status_reply functions to tile38 namespace in lua.
* Add pcall test. Change writeErr to work with string argument
* Make sure eval/evalsha never attempt to write AOF
* Add eval-set and eval-get to benchmarks
* Fix eval benchmark tests, add more
* Improve benchmarks
* Optimizations and refactoring.
* Add lua memtest
* Typo
* Add dependency
* golint fixes
* gofmt fixes
* Add scripting commands to the core/commands.json
* Use ARGV for args inside lua
2017-10-05 18:20:40 +03:00
|
|
|
return server.NOMessage, errInvalidNumberOfArguments
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2016-04-01 02:26:36 +03:00
|
|
|
if vs, sport, ok = tokenval(vs); !ok || sport == "" {
|
Lua scripting feature. (#224)
* Start on lua scripting
* Implement evalsha, script load, script exists, and script flush
* Type conversions from lua to resp/json.
Refactor to make luastate and luascripts persistent in the controller.
* Change controller.command and all underlying commands to return resp.Value.
Serialize only during the ouput.
* First stab at tile38 call from lua
* Change tile38 into tile38.call in Lua
* Property return errors from scripts
* Minor refactoring. No locking on script run
* Cleanup/refactoring
* Create a pool of 5 lua states, allow for more as needed. Refactor.
* Use safe map for scripts. Add a limit for max number of lua states. Refactor.
* Refactor
* Refactor script commands into atomic, read-only, and non-atomic classes.
Proper locking for all three classes.
Add tests for scripts
* More tests for scripts
* Properly escape newlines in lua-produced errors
* Better test for readonly failure
* Correctly convert ok/err messages between lua and resp.
Add pcall, sha1hex, error_reply, status_reply functions to tile38 namespace in lua.
* Add pcall test. Change writeErr to work with string argument
* Make sure eval/evalsha never attempt to write AOF
* Add eval-set and eval-get to benchmarks
* Fix eval benchmark tests, add more
* Improve benchmarks
* Optimizations and refactoring.
* Add lua memtest
* Typo
* Add dependency
* golint fixes
* gofmt fixes
* Add scripting commands to the core/commands.json
* Use ARGV for args inside lua
2017-10-05 18:20:40 +03:00
|
|
|
return server.NOMessage, errInvalidNumberOfArguments
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2016-04-01 02:26:36 +03:00
|
|
|
if len(vs) != 0 {
|
Lua scripting feature. (#224)
* Start on lua scripting
* Implement evalsha, script load, script exists, and script flush
* Type conversions from lua to resp/json.
Refactor to make luastate and luascripts persistent in the controller.
* Change controller.command and all underlying commands to return resp.Value.
Serialize only during the ouput.
* First stab at tile38 call from lua
* Change tile38 into tile38.call in Lua
* Property return errors from scripts
* Minor refactoring. No locking on script run
* Cleanup/refactoring
* Create a pool of 5 lua states, allow for more as needed. Refactor.
* Use safe map for scripts. Add a limit for max number of lua states. Refactor.
* Refactor
* Refactor script commands into atomic, read-only, and non-atomic classes.
Proper locking for all three classes.
Add tests for scripts
* More tests for scripts
* Properly escape newlines in lua-produced errors
* Better test for readonly failure
* Correctly convert ok/err messages between lua and resp.
Add pcall, sha1hex, error_reply, status_reply functions to tile38 namespace in lua.
* Add pcall test. Change writeErr to work with string argument
* Make sure eval/evalsha never attempt to write AOF
* Add eval-set and eval-get to benchmarks
* Fix eval benchmark tests, add more
* Improve benchmarks
* Optimizations and refactoring.
* Add lua memtest
* Typo
* Add dependency
* golint fixes
* gofmt fixes
* Add scripting commands to the core/commands.json
* Use ARGV for args inside lua
2017-10-05 18:20:40 +03:00
|
|
|
return server.NOMessage, errInvalidNumberOfArguments
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
host = strings.ToLower(host)
|
|
|
|
sport = strings.ToLower(sport)
|
|
|
|
var update bool
|
|
|
|
if host == "no" && sport == "one" {
|
2017-09-30 04:11:05 +03:00
|
|
|
update = c.config.followHost() != "" || c.config.followPort() != 0
|
|
|
|
c.config.setFollowHost("")
|
|
|
|
c.config.setFollowPort(0)
|
2016-03-05 02:08:16 +03:00
|
|
|
} else {
|
|
|
|
n, err := strconv.ParseUint(sport, 10, 64)
|
|
|
|
if err != nil {
|
Lua scripting feature. (#224)
* Start on lua scripting
* Implement evalsha, script load, script exists, and script flush
* Type conversions from lua to resp/json.
Refactor to make luastate and luascripts persistent in the controller.
* Change controller.command and all underlying commands to return resp.Value.
Serialize only during the ouput.
* First stab at tile38 call from lua
* Change tile38 into tile38.call in Lua
* Property return errors from scripts
* Minor refactoring. No locking on script run
* Cleanup/refactoring
* Create a pool of 5 lua states, allow for more as needed. Refactor.
* Use safe map for scripts. Add a limit for max number of lua states. Refactor.
* Refactor
* Refactor script commands into atomic, read-only, and non-atomic classes.
Proper locking for all three classes.
Add tests for scripts
* More tests for scripts
* Properly escape newlines in lua-produced errors
* Better test for readonly failure
* Correctly convert ok/err messages between lua and resp.
Add pcall, sha1hex, error_reply, status_reply functions to tile38 namespace in lua.
* Add pcall test. Change writeErr to work with string argument
* Make sure eval/evalsha never attempt to write AOF
* Add eval-set and eval-get to benchmarks
* Fix eval benchmark tests, add more
* Improve benchmarks
* Optimizations and refactoring.
* Add lua memtest
* Typo
* Add dependency
* golint fixes
* gofmt fixes
* Add scripting commands to the core/commands.json
* Use ARGV for args inside lua
2017-10-05 18:20:40 +03:00
|
|
|
return server.NOMessage, errInvalidArgument(sport)
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
port := int(n)
|
2017-09-30 04:11:05 +03:00
|
|
|
update = c.config.followHost() != host || c.config.followPort() != port
|
|
|
|
auth := c.config.leaderAuth()
|
2016-03-05 02:08:16 +03:00
|
|
|
if update {
|
|
|
|
c.mu.Unlock()
|
2016-04-01 02:26:36 +03:00
|
|
|
conn, err := DialTimeout(fmt.Sprintf("%s:%d", host, port), time.Second*2)
|
2016-03-05 02:08:16 +03:00
|
|
|
if err != nil {
|
|
|
|
c.mu.Lock()
|
Lua scripting feature. (#224)
* Start on lua scripting
* Implement evalsha, script load, script exists, and script flush
* Type conversions from lua to resp/json.
Refactor to make luastate and luascripts persistent in the controller.
* Change controller.command and all underlying commands to return resp.Value.
Serialize only during the ouput.
* First stab at tile38 call from lua
* Change tile38 into tile38.call in Lua
* Property return errors from scripts
* Minor refactoring. No locking on script run
* Cleanup/refactoring
* Create a pool of 5 lua states, allow for more as needed. Refactor.
* Use safe map for scripts. Add a limit for max number of lua states. Refactor.
* Refactor
* Refactor script commands into atomic, read-only, and non-atomic classes.
Proper locking for all three classes.
Add tests for scripts
* More tests for scripts
* Properly escape newlines in lua-produced errors
* Better test for readonly failure
* Correctly convert ok/err messages between lua and resp.
Add pcall, sha1hex, error_reply, status_reply functions to tile38 namespace in lua.
* Add pcall test. Change writeErr to work with string argument
* Make sure eval/evalsha never attempt to write AOF
* Add eval-set and eval-get to benchmarks
* Fix eval benchmark tests, add more
* Improve benchmarks
* Optimizations and refactoring.
* Add lua memtest
* Typo
* Add dependency
* golint fixes
* gofmt fixes
* Add scripting commands to the core/commands.json
* Use ARGV for args inside lua
2017-10-05 18:20:40 +03:00
|
|
|
return server.NOMessage, fmt.Errorf("cannot follow: %v", err)
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
2016-03-08 18:35:43 +03:00
|
|
|
if auth != "" {
|
|
|
|
if err := c.followDoLeaderAuth(conn, auth); err != nil {
|
Lua scripting feature. (#224)
* Start on lua scripting
* Implement evalsha, script load, script exists, and script flush
* Type conversions from lua to resp/json.
Refactor to make luastate and luascripts persistent in the controller.
* Change controller.command and all underlying commands to return resp.Value.
Serialize only during the ouput.
* First stab at tile38 call from lua
* Change tile38 into tile38.call in Lua
* Property return errors from scripts
* Minor refactoring. No locking on script run
* Cleanup/refactoring
* Create a pool of 5 lua states, allow for more as needed. Refactor.
* Use safe map for scripts. Add a limit for max number of lua states. Refactor.
* Refactor
* Refactor script commands into atomic, read-only, and non-atomic classes.
Proper locking for all three classes.
Add tests for scripts
* More tests for scripts
* Properly escape newlines in lua-produced errors
* Better test for readonly failure
* Correctly convert ok/err messages between lua and resp.
Add pcall, sha1hex, error_reply, status_reply functions to tile38 namespace in lua.
* Add pcall test. Change writeErr to work with string argument
* Make sure eval/evalsha never attempt to write AOF
* Add eval-set and eval-get to benchmarks
* Fix eval benchmark tests, add more
* Improve benchmarks
* Optimizations and refactoring.
* Add lua memtest
* Typo
* Add dependency
* golint fixes
* gofmt fixes
* Add scripting commands to the core/commands.json
* Use ARGV for args inside lua
2017-10-05 18:20:40 +03:00
|
|
|
return server.NOMessage, fmt.Errorf("cannot follow: %v", err)
|
2016-03-08 18:35:43 +03:00
|
|
|
}
|
|
|
|
}
|
2016-04-01 02:26:36 +03:00
|
|
|
m, err := doServer(conn)
|
2016-03-05 02:08:16 +03:00
|
|
|
if err != nil {
|
|
|
|
c.mu.Lock()
|
Lua scripting feature. (#224)
* Start on lua scripting
* Implement evalsha, script load, script exists, and script flush
* Type conversions from lua to resp/json.
Refactor to make luastate and luascripts persistent in the controller.
* Change controller.command and all underlying commands to return resp.Value.
Serialize only during the ouput.
* First stab at tile38 call from lua
* Change tile38 into tile38.call in Lua
* Property return errors from scripts
* Minor refactoring. No locking on script run
* Cleanup/refactoring
* Create a pool of 5 lua states, allow for more as needed. Refactor.
* Use safe map for scripts. Add a limit for max number of lua states. Refactor.
* Refactor
* Refactor script commands into atomic, read-only, and non-atomic classes.
Proper locking for all three classes.
Add tests for scripts
* More tests for scripts
* Properly escape newlines in lua-produced errors
* Better test for readonly failure
* Correctly convert ok/err messages between lua and resp.
Add pcall, sha1hex, error_reply, status_reply functions to tile38 namespace in lua.
* Add pcall test. Change writeErr to work with string argument
* Make sure eval/evalsha never attempt to write AOF
* Add eval-set and eval-get to benchmarks
* Fix eval benchmark tests, add more
* Improve benchmarks
* Optimizations and refactoring.
* Add lua memtest
* Typo
* Add dependency
* golint fixes
* gofmt fixes
* Add scripting commands to the core/commands.json
* Use ARGV for args inside lua
2017-10-05 18:20:40 +03:00
|
|
|
return server.NOMessage, fmt.Errorf("cannot follow: %v", err)
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2016-04-01 02:26:36 +03:00
|
|
|
if m["id"] == "" {
|
2016-03-05 02:08:16 +03:00
|
|
|
c.mu.Lock()
|
Lua scripting feature. (#224)
* Start on lua scripting
* Implement evalsha, script load, script exists, and script flush
* Type conversions from lua to resp/json.
Refactor to make luastate and luascripts persistent in the controller.
* Change controller.command and all underlying commands to return resp.Value.
Serialize only during the ouput.
* First stab at tile38 call from lua
* Change tile38 into tile38.call in Lua
* Property return errors from scripts
* Minor refactoring. No locking on script run
* Cleanup/refactoring
* Create a pool of 5 lua states, allow for more as needed. Refactor.
* Use safe map for scripts. Add a limit for max number of lua states. Refactor.
* Refactor
* Refactor script commands into atomic, read-only, and non-atomic classes.
Proper locking for all three classes.
Add tests for scripts
* More tests for scripts
* Properly escape newlines in lua-produced errors
* Better test for readonly failure
* Correctly convert ok/err messages between lua and resp.
Add pcall, sha1hex, error_reply, status_reply functions to tile38 namespace in lua.
* Add pcall test. Change writeErr to work with string argument
* Make sure eval/evalsha never attempt to write AOF
* Add eval-set and eval-get to benchmarks
* Fix eval benchmark tests, add more
* Improve benchmarks
* Optimizations and refactoring.
* Add lua memtest
* Typo
* Add dependency
* golint fixes
* gofmt fixes
* Add scripting commands to the core/commands.json
* Use ARGV for args inside lua
2017-10-05 18:20:40 +03:00
|
|
|
return server.NOMessage, fmt.Errorf("cannot follow: invalid id")
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2017-09-30 04:11:05 +03:00
|
|
|
if m["id"] == c.config.serverID() {
|
2016-03-05 02:08:16 +03:00
|
|
|
c.mu.Lock()
|
Lua scripting feature. (#224)
* Start on lua scripting
* Implement evalsha, script load, script exists, and script flush
* Type conversions from lua to resp/json.
Refactor to make luastate and luascripts persistent in the controller.
* Change controller.command and all underlying commands to return resp.Value.
Serialize only during the ouput.
* First stab at tile38 call from lua
* Change tile38 into tile38.call in Lua
* Property return errors from scripts
* Minor refactoring. No locking on script run
* Cleanup/refactoring
* Create a pool of 5 lua states, allow for more as needed. Refactor.
* Use safe map for scripts. Add a limit for max number of lua states. Refactor.
* Refactor
* Refactor script commands into atomic, read-only, and non-atomic classes.
Proper locking for all three classes.
Add tests for scripts
* More tests for scripts
* Properly escape newlines in lua-produced errors
* Better test for readonly failure
* Correctly convert ok/err messages between lua and resp.
Add pcall, sha1hex, error_reply, status_reply functions to tile38 namespace in lua.
* Add pcall test. Change writeErr to work with string argument
* Make sure eval/evalsha never attempt to write AOF
* Add eval-set and eval-get to benchmarks
* Fix eval benchmark tests, add more
* Improve benchmarks
* Optimizations and refactoring.
* Add lua memtest
* Typo
* Add dependency
* golint fixes
* gofmt fixes
* Add scripting commands to the core/commands.json
* Use ARGV for args inside lua
2017-10-05 18:20:40 +03:00
|
|
|
return server.NOMessage, fmt.Errorf("cannot follow self")
|
2016-04-01 02:26:36 +03:00
|
|
|
}
|
|
|
|
if m["following"] != "" {
|
|
|
|
c.mu.Lock()
|
Lua scripting feature. (#224)
* Start on lua scripting
* Implement evalsha, script load, script exists, and script flush
* Type conversions from lua to resp/json.
Refactor to make luastate and luascripts persistent in the controller.
* Change controller.command and all underlying commands to return resp.Value.
Serialize only during the ouput.
* First stab at tile38 call from lua
* Change tile38 into tile38.call in Lua
* Property return errors from scripts
* Minor refactoring. No locking on script run
* Cleanup/refactoring
* Create a pool of 5 lua states, allow for more as needed. Refactor.
* Use safe map for scripts. Add a limit for max number of lua states. Refactor.
* Refactor
* Refactor script commands into atomic, read-only, and non-atomic classes.
Proper locking for all three classes.
Add tests for scripts
* More tests for scripts
* Properly escape newlines in lua-produced errors
* Better test for readonly failure
* Correctly convert ok/err messages between lua and resp.
Add pcall, sha1hex, error_reply, status_reply functions to tile38 namespace in lua.
* Add pcall test. Change writeErr to work with string argument
* Make sure eval/evalsha never attempt to write AOF
* Add eval-set and eval-get to benchmarks
* Fix eval benchmark tests, add more
* Improve benchmarks
* Optimizations and refactoring.
* Add lua memtest
* Typo
* Add dependency
* golint fixes
* gofmt fixes
* Add scripting commands to the core/commands.json
* Use ARGV for args inside lua
2017-10-05 18:20:40 +03:00
|
|
|
return server.NOMessage, fmt.Errorf("cannot follow a follower")
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
c.mu.Lock()
|
|
|
|
}
|
2017-09-30 04:11:05 +03:00
|
|
|
c.config.setFollowHost(host)
|
|
|
|
c.config.setFollowPort(port)
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2017-09-30 04:11:05 +03:00
|
|
|
c.config.write(false)
|
2016-03-05 02:08:16 +03:00
|
|
|
if update {
|
2017-09-30 17:34:08 +03:00
|
|
|
c.followc.add(1)
|
2017-09-30 04:11:05 +03:00
|
|
|
if c.config.followHost() != "" {
|
2016-03-05 02:08:16 +03:00
|
|
|
log.Infof("following new host '%s' '%s'.", host, sport)
|
2017-09-30 17:34:08 +03:00
|
|
|
go c.follow(c.config.followHost(), c.config.followPort(), c.followc.get())
|
2016-03-05 02:08:16 +03:00
|
|
|
} else {
|
|
|
|
log.Infof("following no one")
|
|
|
|
}
|
|
|
|
}
|
2016-04-01 02:26:36 +03:00
|
|
|
return server.OKMessage(msg, start), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func doServer(conn *Conn) (map[string]string, error) {
|
|
|
|
v, err := conn.Do("server")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if v.Error() != nil {
|
|
|
|
return nil, v.Error()
|
|
|
|
}
|
|
|
|
arr := v.Array()
|
|
|
|
m := make(map[string]string)
|
|
|
|
for i := 0; i < len(arr)/2; i++ {
|
|
|
|
m[arr[i*2+0].String()] = arr[i*2+1].String()
|
|
|
|
}
|
|
|
|
return m, err
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2017-09-30 17:34:08 +03:00
|
|
|
func (c *Controller) followHandleCommand(values []resp.Value, followc int, w io.Writer) (int, error) {
|
2016-03-05 02:08:16 +03:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2017-09-30 17:34:08 +03:00
|
|
|
if c.followc.get() != followc {
|
2016-04-01 02:26:36 +03:00
|
|
|
return c.aofsz, errNoLongerFollowing
|
|
|
|
}
|
|
|
|
msg := &server.Message{
|
|
|
|
Command: strings.ToLower(values[0].String()),
|
|
|
|
Values: values,
|
|
|
|
}
|
2017-01-30 21:35:42 +03:00
|
|
|
_, d, err := c.command(msg, nil, nil)
|
2016-04-01 02:26:36 +03:00
|
|
|
if err != nil {
|
|
|
|
if commandErrIsFatal(err) {
|
|
|
|
return c.aofsz, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := c.writeAOF(resp.ArrayValue(values), &d); err != nil {
|
|
|
|
return c.aofsz, err
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
return c.aofsz, nil
|
|
|
|
}
|
|
|
|
|
2016-04-01 02:26:36 +03:00
|
|
|
func (c *Controller) followDoLeaderAuth(conn *Conn, auth string) error {
|
|
|
|
v, err := conn.Do("auth", auth)
|
2016-03-08 18:35:43 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-01 02:26:36 +03:00
|
|
|
if v.Error() != nil {
|
|
|
|
return v.Error()
|
2016-03-08 18:35:43 +03:00
|
|
|
}
|
2016-04-01 02:26:36 +03:00
|
|
|
if v.String() != "OK" {
|
|
|
|
return errors.New("cannot follow: auth no ok")
|
2016-03-08 18:35:43 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-30 17:34:08 +03:00
|
|
|
func (c *Controller) followStep(host string, port int, followc int) error {
|
|
|
|
if c.followc.get() != followc {
|
2016-03-05 02:08:16 +03:00
|
|
|
return errNoLongerFollowing
|
|
|
|
}
|
2017-09-30 17:34:08 +03:00
|
|
|
c.mu.Lock()
|
2016-03-05 02:08:16 +03:00
|
|
|
c.fcup = false
|
2017-09-30 04:11:05 +03:00
|
|
|
auth := c.config.leaderAuth()
|
2016-03-05 02:08:16 +03:00
|
|
|
c.mu.Unlock()
|
|
|
|
addr := fmt.Sprintf("%s:%d", host, port)
|
2016-04-01 02:26:36 +03:00
|
|
|
|
2016-03-05 02:08:16 +03:00
|
|
|
// check if we are following self
|
2016-04-01 02:26:36 +03:00
|
|
|
conn, err := DialTimeout(addr, time.Second*2)
|
2016-03-05 02:08:16 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot follow: %v", err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
2016-03-08 18:35:43 +03:00
|
|
|
if auth != "" {
|
|
|
|
if err := c.followDoLeaderAuth(conn, auth); err != nil {
|
|
|
|
return fmt.Errorf("cannot follow: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2016-04-01 02:26:36 +03:00
|
|
|
m, err := doServer(conn)
|
2016-03-05 02:08:16 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot follow: %v", err)
|
|
|
|
}
|
2016-04-01 02:26:36 +03:00
|
|
|
|
|
|
|
if m["id"] == "" {
|
|
|
|
return fmt.Errorf("cannot follow: invalid id")
|
|
|
|
}
|
2017-09-30 04:11:05 +03:00
|
|
|
if m["id"] == c.config.serverID() {
|
2016-03-05 02:08:16 +03:00
|
|
|
return fmt.Errorf("cannot follow self")
|
|
|
|
}
|
2016-04-01 02:26:36 +03:00
|
|
|
if m["following"] != "" {
|
2016-03-05 02:08:16 +03:00
|
|
|
return fmt.Errorf("cannot follow a follower")
|
|
|
|
}
|
2016-04-01 02:26:36 +03:00
|
|
|
|
2016-03-05 02:08:16 +03:00
|
|
|
// verify checksum
|
|
|
|
pos, err := c.followCheckSome(addr, followc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-01 02:26:36 +03:00
|
|
|
v, err := conn.Do("aof", pos)
|
2016-03-05 02:08:16 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-01 02:26:36 +03:00
|
|
|
if v.Error() != nil {
|
|
|
|
return v.Error()
|
|
|
|
}
|
|
|
|
if v.String() != "OK" {
|
2016-03-05 02:08:16 +03:00
|
|
|
return errors.New("invalid response to aof live request")
|
|
|
|
}
|
2016-03-06 17:55:00 +03:00
|
|
|
if core.ShowDebugMessages {
|
2016-03-05 02:08:16 +03:00
|
|
|
log.Debug("follow:", addr, ":read aof")
|
|
|
|
}
|
2016-04-01 02:26:36 +03:00
|
|
|
|
|
|
|
aofSize, err := strconv.ParseInt(m["aof_size"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
caughtUp := pos >= aofSize
|
2016-03-05 02:08:16 +03:00
|
|
|
if caughtUp {
|
|
|
|
c.mu.Lock()
|
|
|
|
c.fcup = true
|
2017-08-04 15:28:35 +03:00
|
|
|
c.fcuponce = true
|
2016-03-05 02:08:16 +03:00
|
|
|
c.mu.Unlock()
|
|
|
|
log.Info("caught up")
|
|
|
|
}
|
|
|
|
nullw := ioutil.Discard
|
|
|
|
for {
|
2016-04-01 02:26:36 +03:00
|
|
|
v, telnet, _, err := conn.rd.ReadMultiBulk()
|
2016-03-05 02:08:16 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-01 02:26:36 +03:00
|
|
|
vals := v.Array()
|
|
|
|
if telnet || v.Type() != resp.Array {
|
|
|
|
return errors.New("invalid multibulk")
|
|
|
|
}
|
2016-04-01 03:58:02 +03:00
|
|
|
|
2016-04-01 02:26:36 +03:00
|
|
|
aofsz, err := c.followHandleCommand(vals, followc, nullw)
|
2016-03-05 02:08:16 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !caughtUp {
|
2016-04-01 02:26:36 +03:00
|
|
|
if aofsz >= int(aofSize) {
|
2016-03-05 02:08:16 +03:00
|
|
|
caughtUp = true
|
|
|
|
c.mu.Lock()
|
|
|
|
c.fcup = true
|
2017-08-04 15:28:35 +03:00
|
|
|
c.fcuponce = true
|
2016-03-05 02:08:16 +03:00
|
|
|
c.mu.Unlock()
|
|
|
|
log.Info("caught up")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-30 17:34:08 +03:00
|
|
|
func (c *Controller) follow(host string, port int, followc int) {
|
2016-03-05 02:08:16 +03:00
|
|
|
for {
|
|
|
|
err := c.followStep(host, port, followc)
|
|
|
|
if err == errNoLongerFollowing {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil && err != io.EOF {
|
2016-03-08 18:35:43 +03:00
|
|
|
log.Error("follow: " + err.Error())
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
}
|