diff --git a/gc.go b/gc.go new file mode 100644 index 000000000..a260cc0b5 --- /dev/null +++ b/gc.go @@ -0,0 +1,38 @@ +// Copyright 2017 Pilosa Corp. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package pilosa + +// GCNotifier represents an interface for garbage collection notificationss. +type GCNotifier interface { + Close() + AfterGC() <-chan struct{} +} + +func init() { + NopGCNotifier = &nopGCNotifier{} +} + +// NopGCNotifier represents a GCNotifier that doesn't do anything. +var NopGCNotifier GCNotifier + +type nopGCNotifier struct{} + +// Close is a no-op implemenetation of GCNotifier Close method. +func (n *nopGCNotifier) Close() {} + +// AfterGC is a no-op implemenetation of GCNotifier AfterGC method. +func (c *nopGCNotifier) AfterGC() <-chan struct{} { + return nil +} diff --git a/gcnotify/gcnotify.go b/gcnotify/gcnotify.go new file mode 100644 index 000000000..76953a378 --- /dev/null +++ b/gcnotify/gcnotify.go @@ -0,0 +1,44 @@ +// Copyright 2017 Pilosa Corp. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package gcnotify + +import ( + "github.com/CAFxX/gcnotifier" + "github.com/pilosa/pilosa" +) + +// Ensure ActiveGCNotifier implements interface. +var _ pilosa.GCNotifier = &ActiveGCNotifier{} + +type ActiveGCNotifier struct { + gcn *gcnotifier.GCNotifier +} + +// NewActiveGCNotifier creates an active GCNotifier. +func NewActiveGCNotifier() *ActiveGCNotifier { + return &ActiveGCNotifier{ + gcn: gcnotifier.New(), + } +} + +// Close implements the GCNotifier interface. +func (n *ActiveGCNotifier) Close() { + n.gcn.Close() +} + +// AfterGC implements the GCNotifier interface. +func (n *ActiveGCNotifier) AfterGC() <-chan struct{} { + return n.gcn.AfterGC() +} diff --git a/server.go b/server.go index 3751e28ad..be63a39e3 100644 --- a/server.go +++ b/server.go @@ -31,7 +31,6 @@ import ( "sync" "time" - "github.com/CAFxX/gcnotifier" "github.com/gogo/protobuf/proto" "github.com/pilosa/pilosa/diagnostics" "github.com/pilosa/pilosa/internal" @@ -73,6 +72,8 @@ type Server struct { Cluster *Cluster diagnostics *diagnostics.Diagnostics + GCNotifier GCNotifier + // Background monitoring intervals. AntiEntropyInterval time.Duration MetricInterval time.Duration @@ -103,6 +104,8 @@ func NewServer() *Server { Network: "tcp", + GCNotifier: NopGCNotifier, + AntiEntropyInterval: DefaultAntiEntropyInterval, MetricInterval: 0, DiagnosticInterval: 0, @@ -652,8 +655,7 @@ func (s *Server) monitorRuntime() { ticker := time.NewTicker(s.MetricInterval) defer ticker.Stop() - gcn := gcnotifier.New() - defer gcn.Close() + defer s.GCNotifier.Close() s.Logger().Printf("runtime stats initializing (%s interval)", s.MetricInterval) @@ -662,7 +664,7 @@ func (s *Server) monitorRuntime() { select { case <-s.closing: return - case <-gcn.AfterGC(): + case <-s.GCNotifier.AfterGC(): // GC just ran. s.Holder.Stats.Count("garbage_collection", 1, 1.0) case <-ticker.C: diff --git a/server/server.go b/server/server.go index 9803f7be2..f0d3314fa 100644 --- a/server/server.go +++ b/server/server.go @@ -33,6 +33,7 @@ import ( "crypto/tls" "github.com/pilosa/pilosa" + "github.com/pilosa/pilosa/gcnotify" "github.com/pilosa/pilosa/gossip" "github.com/pilosa/pilosa/statsd" ) @@ -151,6 +152,7 @@ func (m *Command) SetupServer() error { if m.Config.Metric.Diagnostics { m.Server.DiagnosticInterval = time.Duration(DefaultDiagnosticsInterval) } + m.Server.GCNotifier = gcnotify.NewActiveGCNotifier() m.Server.Holder.Stats, err = NewStatsClient(m.Config.Metric.Service, m.Config.Metric.Host) if err != nil { return err