Skip to content

Commit

Permalink
Replace reloaded varz field with config_load_time
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler Treat committed Jun 27, 2017
1 parent de2f407 commit dd3ad77
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -84,7 +84,7 @@ Hello World
On Unix systems, the NATS server responds to the following signals:

| Signal | Result |
| ------- | --------------------------------------|
| ------- | ------------------------------------- |
| SIGKILL | Kills the process immediately |
| SIGINT | Stops the server gracefully |
| SIGUSR1 | Reopens the log file for log rotation |
Expand Down
4 changes: 2 additions & 2 deletions server/monitor.go
Expand Up @@ -399,7 +399,7 @@ type Varz struct {
SlowConsumers int64 `json:"slow_consumers"`
Subscriptions uint32 `json:"subscriptions"`
HTTPReqStats map[string]uint64 `json:"http_req_stats"`
Reloaded uint64 `json:"reloaded"`
ConfigLoadTime time.Time `json:"config_load_time"`
}

func myUptime(d time.Duration) string {
Expand Down Expand Up @@ -479,7 +479,7 @@ func (s *Server) HandleVarz(w http.ResponseWriter, r *http.Request) {
v.OutBytes = atomic.LoadInt64(&s.outBytes)
v.SlowConsumers = atomic.LoadInt64(&s.slowConsumers)
v.Subscriptions = s.sl.Count()
v.Reloaded = s.reloaded
v.ConfigLoadTime = s.configTime
s.httpReqStats[VarzPath]++
// Need a copy here since s.httpReqStas can change while doing
// the marshaling down below.
Expand Down
3 changes: 2 additions & 1 deletion server/reload.go
Expand Up @@ -9,6 +9,7 @@ import (
"net/url"
"reflect"
"strings"
"time"
)

// FlagSnapshot captures the server options as specified by CLI flags at
Expand Down Expand Up @@ -259,7 +260,7 @@ func (s *Server) Reload() error {
err = s.reloadOptions(newOpts)
if err == nil {
s.mu.Lock()
s.reloaded++
s.configTime = time.Now()
s.mu.Unlock()
}
return err
Expand Down
32 changes: 12 additions & 20 deletions server/reload_test.go
Expand Up @@ -19,14 +19,12 @@ import (
// not start with a config file.
func TestConfigReloadNoConfigFile(t *testing.T) {
server := New(&Options{})
if reloaded := server.NumReloads(); reloaded != 0 {
t.Fatalf("Reloaded is incorrect.\nexpected: 0\ngot: %d", reloaded)
}
loaded := server.ConfigTime()
if server.Reload() == nil {
t.Fatal("Expected Reload to return an error")
}
if reloaded := server.NumReloads(); reloaded != 0 {
t.Fatalf("Reloaded is incorrect.\nexpected: 0\ngot: %d", reloaded)
if reloaded := server.ConfigTime(); reloaded != loaded {
t.Fatalf("ConfigTime is incorrect.\nexpected: %s\ngot: %s", loaded, reloaded)
}
}

Expand All @@ -36,9 +34,7 @@ func TestConfigReloadUnsupported(t *testing.T) {
server, opts, config := newServerWithSymlinkConfig(t, "tmp.conf", "./configs/reload/test.conf")
defer os.Remove(config)

if reloaded := server.NumReloads(); reloaded != 0 {
t.Fatalf("Reloaded is incorrect.\nexpected: 0\ngot: %d", reloaded)
}
loaded := server.ConfigTime()

golden := &Options{
ConfigFile: config,
Expand Down Expand Up @@ -86,8 +82,8 @@ func TestConfigReloadUnsupported(t *testing.T) {
golden, opts)
}

if reloaded := server.NumReloads(); reloaded != 0 {
t.Fatalf("Reloaded is incorrect.\nexpected: 0\ngot: %d", reloaded)
if reloaded := server.ConfigTime(); reloaded != loaded {
t.Fatalf("ConfigTime is incorrect.\nexpected: %s\ngot: %s", loaded, reloaded)
}
}

Expand All @@ -96,9 +92,7 @@ func TestConfigReloadInvalidConfig(t *testing.T) {
server, opts, config := newServerWithSymlinkConfig(t, "tmp.conf", "./configs/reload/test.conf")
defer os.Remove(config)

if reloaded := server.NumReloads(); reloaded != 0 {
t.Fatalf("Reloaded is incorrect.\nexpected: 0\ngot: %d", reloaded)
}
loaded := server.ConfigTime()

golden := &Options{
ConfigFile: config,
Expand Down Expand Up @@ -146,8 +140,8 @@ func TestConfigReloadInvalidConfig(t *testing.T) {
golden, opts)
}

if reloaded := server.NumReloads(); reloaded != 0 {
t.Fatalf("Reloaded is incorrect.\nexpected: 0\ngot: %d", reloaded)
if reloaded := server.ConfigTime(); reloaded != loaded {
t.Fatalf("ConfigTime is incorrect.\nexpected: %s\ngot: %s", loaded, reloaded)
}
}

Expand All @@ -156,9 +150,7 @@ func TestConfigReload(t *testing.T) {
server, opts, config := newServerWithSymlinkConfig(t, "tmp.conf", "./configs/reload/test.conf")
defer os.Remove(config)

if reloaded := server.NumReloads(); reloaded != 0 {
t.Fatalf("Reloaded is incorrect.\nexpected: 0\ngot: %d", reloaded)
}
loaded := server.ConfigTime()

golden := &Options{
ConfigFile: config,
Expand Down Expand Up @@ -232,8 +224,8 @@ func TestConfigReload(t *testing.T) {
t.Fatal("Expected NoAdvertise to be true")
}

if reloaded := server.NumReloads(); reloaded != 1 {
t.Fatalf("Reloaded is incorrect.\nexpected: 1\ngot: %d", reloaded)
if reloaded := server.ConfigTime(); !reloaded.After(loaded) {
t.Fatalf("ConfigTime is incorrect.\nexpected greater than: %s\ngot: %s", loaded, reloaded)
}
}

Expand Down
15 changes: 8 additions & 7 deletions server/server.go
Expand Up @@ -79,7 +79,7 @@ type Server struct {
grRunning bool
grWG sync.WaitGroup // to wait on various go routines
cproto int64 // number of clients supporting async INFO
reloaded uint64 // number of times server config has been reloaded
configTime time.Time // last time config was loaded
logging struct {
sync.RWMutex
logger Logger
Expand Down Expand Up @@ -119,13 +119,15 @@ func New(opts *Options) *Server {
clientConnectURLs: make(map[string]struct{}),
}

now := time.Now()
s := &Server{
configFile: opts.ConfigFile,
info: info,
sl: NewSublist(),
opts: opts,
done: make(chan bool, 1),
start: time.Now(),
start: now,
configTime: now,
}

s.mu.Lock()
Expand Down Expand Up @@ -919,12 +921,11 @@ func (s *Server) NumSubscriptions() uint32 {
return subs
}

// NumReloads returns the number of times the server config has been reloaded.
func (s *Server) NumReloads() uint64 {
// ConfigTime will report the last time the server configuration was loaded.
func (s *Server) ConfigTime() time.Time {
s.mu.Lock()
reloaded := s.reloaded
s.mu.Unlock()
return reloaded
defer s.mu.Unlock()
return s.configTime
}

// Addr will return the net.Addr object for the current listener.
Expand Down
8 changes: 3 additions & 5 deletions server/signal_test.go
Expand Up @@ -70,17 +70,15 @@ func TestSignalToReloadConfig(t *testing.T) {
s := RunServer(opts)
defer s.Shutdown()

if reloaded := s.NumReloads(); reloaded != 0 {
t.Fatalf("Reloaded is incorrect.\nexpected: 0\ngot: %d", reloaded)
}
loaded := s.ConfigTime()

// This should cause config to be reloaded.
syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
// Wait a bit for action to be performed
time.Sleep(500 * time.Millisecond)

if reloaded := s.NumReloads(); reloaded != 1 {
t.Fatalf("Reloaded is incorrect.\nexpected: 1\ngot: %d", reloaded)
if reloaded := s.ConfigTime(); !reloaded.After(loaded) {
t.Fatalf("ConfigTime is incorrect.\nexpected greater than: %s\ngot: %s", loaded, reloaded)
}
}

Expand Down

0 comments on commit dd3ad77

Please sign in to comment.