forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stats: allow statistics maps to quietly fade away when the object the…
…y refer to is closed As per influxdata#5784, there is a risk that a long running influxd process will accumulate a lot of statistics that describe objects that have already been closed. This is a concern primarily because of the amount of IO and CPU involved with continuing to publish the last will and testament of an object long since departed. This series address the issue by pinching some code from Kapacitor - thanks @nathanielc - and using it as a top level name space for influx statistics objects. The maps are reference counted. One reference is allocated to the described object, another to the monitor. When the described object is closed, it yells DeleteStatistics! at the influxdb package which takes note of the request, but otherwise ignores it. When the monitor is doing its thing, it quietly peeks at the statistics map's reference count and if it detects that its number is up (well, down really), it yells DeleteStatistics! at the influxdb package who takes notice of it this time. This deferral of the grim reaper is required so that the monitor has time to tidy up the paper work and confirm that the undertaker's cheque has not bounced. Signed-off-by: Jon Seymour <jon@wildducktheories.com>
- Loading branch information
1 parent
a2f2443
commit 5233c12
Showing
3 changed files
with
118 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package influxdb_test | ||
|
||
import ( | ||
"expvar" | ||
"testing" | ||
|
||
"github.com/influxdata/influxdb" | ||
) | ||
|
||
func TestEmptyStatistics(t *testing.T) { | ||
found := make([]expvar.KeyValue, 0) | ||
influxdb.DoStatistics(func(kv expvar.KeyValue) { | ||
found = append(found, kv) | ||
}) | ||
|
||
if length := len(found); length != 0 { | ||
t.Fatalf("non empty initial state. got %d, expected: %d", length, 0) | ||
} | ||
} | ||
|
||
// Test that we can create one statistic and that it disappears after it is deleted twice. | ||
func TestOneStatistic(t *testing.T) { | ||
|
||
foo := influxdb.NewStatistics("foo", "bar", map[string]string{"tag": "T"}) | ||
|
||
found := make([]expvar.KeyValue, 0) | ||
influxdb.DoStatistics(func(kv expvar.KeyValue) { | ||
found = append(found, kv) | ||
}) | ||
|
||
if len(found) != 1 { | ||
t.Fatalf("enumeration error after do. length of slice: got %d, expected %d", len(found), 1) | ||
} | ||
if m, ok := found[0].Value.(*expvar.Map); !ok { | ||
t.Fatalf("value of found object got: %v, expected: a map", found[0].Value) | ||
} else { | ||
if fooActual := m.Get("values"); fooActual != foo { | ||
t.Fatalf("failed to obtain expected map. got: %v, expected: %v", fooActual, foo) | ||
} | ||
} | ||
|
||
influxdb.DeleteStatistics("foo") | ||
|
||
found = make([]expvar.KeyValue, 0) | ||
influxdb.DoStatistics(func(kv expvar.KeyValue) { | ||
found = append(found, kv) | ||
}) | ||
|
||
if length := len(found); length != 1 { | ||
t.Fatalf("failed to find expected number of objects. got: %d, expected: 1", length) | ||
} | ||
|
||
influxdb.DeleteStatistics("foo") | ||
|
||
found = make([]expvar.KeyValue, 0) | ||
influxdb.DoStatistics(func(kv expvar.KeyValue) { | ||
found = append(found, kv) | ||
}) | ||
|
||
if length := len(found); length != 0 { | ||
t.Fatalf("failed to find expected number of objects. got: %d, expected: 0", length) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters