stat

package
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 19, 2026 License: MIT Imports: 18 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

显式启动诊断服务

公开包 github.com/lfhy/compactfs/fs/stat 提供 Unix socket 上的 HTTP 统计、日志、同步和 pprof 服务。它借用 Source 的 StatsSnapshot、Logs 和 Sync,不负责创建或关闭文件引擎。

CLI 的本地 mount 先打开存储和诊断,再调用 FUSE Mount;二者就绪后才发布挂载成功。诊断启动失败不进入挂载,FUSE 失败由 Export.Close 清理诊断。blk 在开始 NBD serve 前启动诊断;stat/log/sync 仍通过相同 socket 访问。远程 Export 保留原行为,不创建本地诊断 socket。库调用 core.New、fuse.Mount 或 afero.New 都不会隐式打开诊断 listener,SDK 按需调用 stat.Listen(path, source)。

Source 是一个借用接口:

type Source interface {
    StatsSnapshot() core.Stats
    Logs(after uint64, limit int, errorsOnly bool, pathFilter string) core.LogSnapshot
    Sync(context.Context) error
}

本地 *core.FileSystem 可直接作为 Source。导入 github.com/lfhy/compactfs/fs/stat 后,在已有引擎上显式启动:

server, err := stat.Listen(root, storage) // root 为绝对卷路径
if err != nil {
    return err // storage 仍由原调用方负责关闭
}
// 使用应用视图或导出;退出前先停止新的文件操作并关闭其句柄。
diagnosticsErr := server.Close()
storageErr := storage.Close()
return errors.Join(diagnosticsErr, storageErr)

调用方拥有返回的 Server,在关闭被借用的引擎前调用 Server.Close():先停止监听并取消请求,再等待 HTTP handler 退出。关闭可并发、重复调用;2秒 HTTP 关闭宽限后会强制关闭连接,并继续等待所有 handler,Source.Sync 必须响应 context 取消。应用也应先停止数据访问和导出句柄,避免关闭时仍有新的引擎使用者。诊断服务不会代为关闭 core、缓存或 codec;同步失败和关闭错误由调用方处理。

stat.SocketPath(path) 保留原 socket 定位规则:清理绝对路径并取摘要,默认放在 ~/.compactfs/run,路径过长时回退到临时目录。socket 权限为 0600,原 stat/log/sync 参数与输出字段保持。服务只覆盖统计响应的挂载位置及服务开始/运行时间,不修改被借用引擎的统计状态。

端点 用途
/stats 累积统计快照,不等待后台队列排空
/logs 有界日志 ring 的游标查询与过滤
/sync 按既有语义封口并等待后台提交,传播失败或取消
/goroutines goroutine 诊断
/debug/pprof/profile 进程 CPU profile
/debug/pprof/heap 进程内存 profile,默认查看仍在使用的空间
/debug/pprof/allocs 同一内存 profile,默认查看累计分配空间

例如,通过 stat.SocketPath(mountpoint) 得到当前挂载的 socket 路径后:

diagnostic_socket=/path/to/compactfs.sock
curl --fail --unix-socket "$diagnostic_socket" 'http://localhost/debug/pprof/heap?gc=1' -o before.heap.pb.gz
# 在同一 daemon 中执行待诊断操作,再取得 after 快照。
curl --fail --unix-socket "$diagnostic_socket" 'http://localhost/debug/pprof/heap?gc=1' -o after.heap.pb.gz
go tool pprof -sample_index=alloc_space -base before.heap.pb.gz ./compactfs after.heap.pb.gz
curl --fail --unix-socket "$diagnostic_socket" 'http://localhost/debug/pprof/allocs' -o cumulative.allocs.pb.gz

默认输出为 gzip protobuf;debug=1 返回含运行时内存统计的文本。两种内存 profile 都含 alloc_space/alloc_objects(进程启动以来累计分配的空间/对象)及 inuse_space/inuse_objects(profile 中尚未释放的空间/对象)。诊断一段操作的累计分配时,在同一进程前后取快照,用对应实际 ELF 做差;单份累计 profile 包含启动、准备和此前工作的分配,不能直接当作该段操作的成本。

本服务不修改 runtime.MemProfileRate 或 GC 配置;结果按运行时采样率估计,不是每次分配的精确记录。内存 profile 的发布可能滞后 GC;仅 heap 的 gc=1 会在读取前额外执行一次 GC,allocs 的同名参数不触发 GC。这会扰动进程及缓存,应放在 API 计时外并保留取样边界;差分还可能包含后台工作和 profile 服务自身的分配。读取 profile 不调用存储 Sync,不是 fsync 确认或吞吐测量。服务只显式注册表中端点,不开放整个默认 pprof mux。

core 保留统计、原子计数和日志 ring;HTTP、socket 与 pprof 归本包,依赖单向为 stat → core。文件语义接口 core.Access 不再要求 StartDiagnostics 或 FUSE 请求计数;FUSE 在构造时发现可选计数观察器,远程实现不必增加空方法。

调用边界见目录与 API 迁移,开发约定见本包指南。

Documentation

Overview

Package stat serves a borrowed storage engine's local control and diagnostics endpoints. The owner must close this service before the engine.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SocketPath

func SocketPath(mountpoint string) (string, error)

SocketPath preserves the per-mount socket address used by stat, log and sync. A hash keeps distinct mountpoints independent and bounds Unix path lengths.

Types

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server owns its listener, connections and request goroutines, but not Source. Close joins requests before returning so the caller can then close Source.

func Listen

func Listen(mountpoint string, source Source) (*Server, error)

Listen starts the existing local HTTP protocol on a private Unix socket. Existing active sockets and non-socket files are never replaced. Source is only borrowed; failure and Close never close or otherwise own its resources.

func (*Server) Close

func (s *Server) Close() error

Close stops admission, cancels request contexts and joins Serve and every admitted handler. A graceful-shutdown timeout forces connections closed but never permits the borrowed engine to be released while a handler still runs. The same result is returned to all concurrent or subsequent callers.

type Source

type Source interface {
	StatsSnapshot() core.Stats
	Logs(uint64, int, bool, string) core.LogSnapshot
	Sync(context.Context) error
}

Source is borrowed until Close returns. Sync must respond to cancellation; its owner must not close the engine while a diagnostics request is using it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL