internal/counter: fix file rotation panics

Weekly, the memory-mapped counter file is replaced by a new one.
If this fails, using counters may panic.

This CL fixes the code to avoid the above panics by
setting the counter's file's mapping to nil while there is a danger
that an existing mapping is no longer valid.

The reader can understand the changes using the new TestRotateCounters
at line 99 to see what happens when remapping fails.
1. In the existing code, the current.Lookup at existing line 308 will fault
2. Commenting out that code delays the fault to the c.Inc() at line 106
   of rotate_test.go.
3. So the counters have dangling pointers. These have to be reset.
   This is done by f.current.Store(nil), and running a cleanup
   function at line 313 in the new code. The cleanup function resets
   all the counter pointers, and then cleans up the old mapping, if
   there is one. (The code to reset the counters cannot be run while
   f.mu is locked.)
4. But there is the same problem with counter pointers pointing to the
   old mapping even after the file points to the new mapping. The same
   solution works, resetting the counter pointer, and then closing
   the old mapping. (The old code freed the mapping too soon.)
In both cases (3 and 4), if a counter pointer is used, it will point
   to a valid mapping (nil is fine; the increment goes into the
   counter's state)

Summary: an old mapping cannot be removed until all the file's
counters point to a new mapping.

The CL also
  Fixes some internal documentation,
  Fixes a time-related test error that only showed up in Eastern
      time evening,
  Adds a test showing the life of a counter and testing mapping
      failure in file rotation.

Fixes: golang/go#64577

Change-Id: I3e4775fb527e94d8a32505c8d77a131659b0a82a
Reviewed-on: https://go-review.googlesource.com/c/telemetry/+/552276
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Peter Weinberger <pjw@google.com>
3 files changed
tree: 44d5585cfff081925d261432efc4e9e82717719a
  1. cmd/
  2. config/
  3. counter/
  4. godev/
  5. internal/
  6. upload/
  7. .dockerignore
  8. .eslintrc.json
  9. .gitignore
  10. .prettierrc.json
  11. .stylelintrc.json
  12. CONTRIBUTING.md
  13. doc.go
  14. go.mod
  15. go.sum
  16. LICENSE
  17. mode.go
  18. npm
  19. npx
  20. package-lock.json
  21. package.json
  22. PATENTS
  23. README.md
  24. tsconfig.json
  25. types.go
README.md

Go Telemetry

This repository holds the Go Telemetry server code and libraries.

Linting & Formatting

This repository uses eslint to format TS files, stylelint to format CSS files, and prettier to format TS, CSS, Markdown, and YAML files.

See the style guides:

It is encouraged that all TS and CSS code be run through formatters before submitting a change. However, it is not a strict requirement enforced by CI.

Installing npm Dependencies:

  1. Install docker
  2. Run ./npm install

Run ESLint, Stylelint, & Prettier

./npm run all

Report Issues / Send Patches

This repository uses Gerrit for code changes. To learn how to submit changes to this repository, see https://golang.org/doc/contribute.html.

The main issue tracker for the time repository is located at https://github.com/golang/go/issues. Prefix your issue with “x/telemetry:” in the subject line, so it is easy to find.