Close flushes the WAL and closes the head. It also takes a snapshot of in-memory chunks if enabled.
()
| 1869 | // Close flushes the WAL and closes the head. |
| 1870 | // It also takes a snapshot of in-memory chunks if enabled. |
| 1871 | func (h *Head) Close() error { |
| 1872 | h.closedMtx.Lock() |
| 1873 | defer h.closedMtx.Unlock() |
| 1874 | h.closed = true |
| 1875 | |
| 1876 | // Stop the background series_state.json writer. |
| 1877 | if h.opts.EnableFastStartup && h.seriesStateQuit != nil { |
| 1878 | close(h.seriesStateQuit) |
| 1879 | h.seriesStateWg.Wait() |
| 1880 | h.seriesStateQuit = nil |
| 1881 | // Flush the final clean state. |
| 1882 | h.writeSeriesState(true) |
| 1883 | } |
| 1884 | |
| 1885 | // mmap all but last chunk in case we're performing snapshot since that only |
| 1886 | // takes samples from most recent head chunk. |
| 1887 | h.mmapHeadChunks() |
| 1888 | |
| 1889 | errs := h.chunkDiskMapper.Close() |
| 1890 | if h.wal != nil { |
| 1891 | errs = errors.Join(errs, h.wal.Close()) |
| 1892 | } |
| 1893 | if h.wbl != nil { |
| 1894 | errs = errors.Join(errs, h.wbl.Close()) |
| 1895 | } |
| 1896 | if errs == nil && h.opts.EnableMemorySnapshotOnShutdown { |
| 1897 | errs = errors.Join(errs, h.performChunkSnapshot()) |
| 1898 | } |
| 1899 | return errs |
| 1900 | } |
| 1901 | |
| 1902 | // String returns an human readable representation of the TSDB head. It's important to |
| 1903 | // keep this function in order to avoid the struct dump when the head is stringified in |