put GCNotify behind an inerface

This commit is contained in:
Travis Turner 2018-02-26 10:30:51 -06:00
parent 29dcfd06da
commit 10ebb6ab11
No known key found for this signature in database
GPG key ID: 7F08008DFD9314C9
4 changed files with 90 additions and 4 deletions

38
gc.go Normal file
View file

@ -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
}

44
gcnotify/gcnotify.go Normal file
View file

@ -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()
}

View file

@ -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:

View file

@ -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