diff options
| author | Stefan Boberg <[email protected]> | 2026-03-23 14:19:57 +0100 |
|---|---|---|
| committer | GitHub Enterprise <[email protected]> | 2026-03-23 14:19:57 +0100 |
| commit | 2a445406e09328cb4cf320300f2678997d6775b7 (patch) | |
| tree | a92f02d94c92144cb6ae32160397298533e4c822 /src/zenserver/storage | |
| parent | add hub instance crash recovery (#885) (diff) | |
| download | zen-2a445406e09328cb4cf320300f2678997d6775b7.tar.xz zen-2a445406e09328cb4cf320300f2678997d6775b7.zip | |
Dashboard refresh (logs, storage, network, object store, docs) (#835)
## Summary
This PR adds a session management service, several new dashboard pages, and a number of infrastructure improvements.
### Sessions Service
- `SessionsServiceClient` in `zenutil` announces sessions to a remote zenserver with a 15s heartbeat (POST/PUT/DELETE lifecycle)
- Storage server registers itself with its own local sessions service on startup
- Session mode attribute coupled to server mode (Compute, Proxy, Hub, etc.)
- Ended sessions tracked with `ended_at` timestamp; status filtering (Active/Ended/All)
- `--sessions-url` config option for remote session announcement
- In-process log sink (`InProcSessionLogSink`) forwards server log output to the server's own session, visible in the dashboard
### Session Log Viewer
- POST/GET endpoints for session logs (`/sessions/{id}/log`) supporting raw text and structured JSON/CbObject with batch `entries` array
- In-memory log storage per session (capped at 10k entries) with cursor-based pagination for efficient incremental fetching
- Log panel in the sessions dashboard with incremental DOM updates, auto-scroll (Follow toggle), newest-first toggle, text filter, and log-level coloring
- Auto-selects the server's own session on page load
### TCP Log Streaming
- `LogStreamListener` and `TcpLogStreamSink` for log delivery over TCP
- Sequence numbers on each message with drop detection and synthetic "dropped" notice on gaps
- Gathered buffer writes to reduce syscall overhead when flushing batches
- Tests covering basic delivery, multi-line splitting, drop detection, and sequencing
### New Dashboard Pages
- **Sessions**: master-detail layout with selectable rows, metadata panel, live WebSocket updates, paging, abbreviated date formatting, and "this" pill for the local session
- **Object Store**: summary stats tiles and bucket table with click-to-expand inline object listing (`GET /obj/`)
- **Storage**: per-volume disk usage breakdown (`GET /admin/storage`), Garbage Collection status section (next-run countdown, last-run stats), and GC History table with paginated rows and expandable detail panels
- **Network**: overview tiles, per-service request table, proxy connections, and live WebSocket updates; distinct client IPs and session counts via HyperLogLog
### Documentation Page
- In-dashboard Docs page with sidebar navigation, markdown rendering (via `marked`), Mermaid diagram support (theme-aware), collapsible sections, text filtering with highlighting, and cross-document linking
- New user-facing docs: `overview.md` (with architecture and per-mode diagrams), `sessions.md`, `cache.md`, `projects.md`; updated `compute.md`
- Dev docs moved to `docs/dev/`
### Infrastructure & Bug Fixes
- **Deflate compression** for the embedded frontend zip (~3.4MB → ~950KB); zlib inflate support added to `ZipFs` with cached decompressed buffers
- **Local IP addresses**: `GetLocalIpAddresses()` (Windows via `GetAdaptersAddresses`, Linux/Mac via `getifaddrs`); surfaced in `/status/status`, `/health/info`, and the dashboard banner
- **Dashboard nav**: unified into `zen-nav` web component with `MutationObserver` for dynamically added links, CSS `::part()` to merge banner/nav border radii, and prefix-based active link detection
- Stats broadcast refactored from manual JSON string concatenation to `CbObjectWriter`; `CbObject`-to-JS conversion improved for `TimeSpan`, `DateTime`, and large integers
- Stats WebSocket boilerplate consolidated into `ZenPage.connect_stats_ws()`
Diffstat (limited to 'src/zenserver/storage')
| -rw-r--r-- | src/zenserver/storage/admin/admin.cpp | 133 | ||||
| -rw-r--r-- | src/zenserver/storage/objectstore/objectstore.cpp | 102 | ||||
| -rw-r--r-- | src/zenserver/storage/objectstore/objectstore.h | 1 | ||||
| -rw-r--r-- | src/zenserver/storage/storageconfig.cpp | 4 | ||||
| -rw-r--r-- | src/zenserver/storage/storageconfig.h | 3 | ||||
| -rw-r--r-- | src/zenserver/storage/zenstorageserver.cpp | 79 | ||||
| -rw-r--r-- | src/zenserver/storage/zenstorageserver.h | 10 |
7 files changed, 327 insertions, 5 deletions
diff --git a/src/zenserver/storage/admin/admin.cpp b/src/zenserver/storage/admin/admin.cpp index 6e78a6179..f1c2daea4 100644 --- a/src/zenserver/storage/admin/admin.cpp +++ b/src/zenserver/storage/admin/admin.cpp @@ -791,6 +791,139 @@ HttpAdminService::HttpAdminService(GcScheduler& Scheduler, }, HttpVerb::kPost); m_Router.RegisterRoute( + "storage", + [this](HttpRouterRequest& Req) { + CbObjectWriter Obj; + + // Collect known storage directories + struct StorageDir + { + std::string_view Name; + std::filesystem::path Path; + }; + + std::vector<StorageDir> Dirs; + const std::filesystem::path& DataDir = m_ServerOptions.DataDir; + Dirs.push_back({"cache"sv, DataDir / "cache"}); + Dirs.push_back({"cas"sv, DataDir / "cas"}); + Dirs.push_back({"projects"sv, DataDir / "projects"}); + Dirs.push_back({"builds"sv, DataDir / "builds"}); + Dirs.push_back({"builds_cas"sv, DataDir / "builds_cas"}); + Dirs.push_back({"obj"sv, DataDir / "obj"}); + + // Group directories by volume (identified by total capacity) + struct VolumeInfo + { + DiskSpace Space; + std::vector<const StorageDir*> Directories; + }; + + // Use canonical path to identify volumes. Directories on the same volume + // will report the same total capacity from DiskSpaceInfo. + std::vector<VolumeInfo> Volumes; + auto FindOrAddVolume = [&](DiskSpace Space) -> VolumeInfo& { + for (VolumeInfo& V : Volumes) + { + if (V.Space.Total == Space.Total && V.Space.Free == Space.Free) + { + return V; + } + } + Volumes.push_back({Space, {}}); + return Volumes.back(); + }; + + for (StorageDir& Dir : Dirs) + { + if (!IsDir(Dir.Path)) + { + continue; + } + DiskSpace Space; + if (DiskSpaceInfo(Dir.Path, Space)) + { + FindOrAddVolume(Space).Directories.push_back(&Dir); + } + } + + Obj.BeginArray("volumes"sv); + for (const VolumeInfo& Vol : Volumes) + { + Obj.BeginObject(); + Obj << "total"sv << Vol.Space.Total; + Obj << "free"sv << Vol.Space.Free; + Obj << "used"sv << (Vol.Space.Total - Vol.Space.Free); + + Obj.BeginArray("directories"sv); + for (const StorageDir* Dir : Vol.Directories) + { + Obj.BeginObject(); + Obj << "name"sv << Dir->Name; + Obj << "path"sv << Dir->Path.string(); + + DirStats Stats = GetStatsForDirectory(Dir->Path); + Obj << "bytes"sv << Stats.ByteCount; + Obj << "files"sv << Stats.FileCount; + Obj.EndObject(); + } + Obj.EndArray(); + Obj.EndObject(); + } + Obj.EndArray(); + + Req.ServerRequest().WriteResponse(HttpResponseCode::OK, Obj.Save()); + }, + HttpVerb::kGet); + + m_Router.RegisterRoute( + "gclog", + [this](HttpRouterRequest& Req) { + const GcSchedulerState State = m_GcScheduler.GetState(); + const std::filesystem::path Path = State.Config.RootDirectory / "gc.log"; + + CbObjectWriter Response; + Response.BeginArray("entries"sv); + + try + { + if (IsFile(Path)) + { + IoBuffer FileData = ReadFile(Path).Flatten(); + + // The log file contains concatenated named CBO object fields. + // Each field is a complete entry: [type+name header][object payload]. + // We wrap each one in a CbObject and add it to the response array. + const uint8_t* Ptr = static_cast<const uint8_t*>(FileData.GetData()); + const uint8_t* End = Ptr + FileData.GetSize(); + + while (Ptr < End) + { + CbFieldView Field(Ptr); + uint64_t FieldSize = Field.GetSize(); + if (FieldSize == 0 || Ptr + FieldSize > End) + { + break; + } + + // Wrap the named field as an object and add it + CbObjectView ObjView = Field.AsObjectView(); + CbObject Entry = CbObject::Clone(ObjView); + Response.AddObject(Entry); + Ptr += FieldSize; + } + } + } + catch (const std::exception& Ex) + { + ZEN_WARN("failed to read gc log '{}': {}", Path, Ex.what()); + } + + Response.EndArray(); + Req.ServerRequest().WriteResponse(HttpResponseCode::OK, Response.Save()); + }, + HttpVerb::kGet); + + m_Router.RegisterRoute( "flush", [this](HttpRouterRequest& Req) { HttpServerRequest& HttpReq = Req.ServerRequest(); diff --git a/src/zenserver/storage/objectstore/objectstore.cpp b/src/zenserver/storage/objectstore/objectstore.cpp index e347e2dfe..493326a32 100644 --- a/src/zenserver/storage/objectstore/objectstore.cpp +++ b/src/zenserver/storage/objectstore/objectstore.cpp @@ -280,6 +280,16 @@ HttpObjectStoreService::Inititalize() [](std::string_view Str) -> bool { return !Str.empty() && AsciiSet::HasOnly(Str, ValidBucketCharactersSet); }); m_Router.RegisterRoute( + "", + [this](zen::HttpRouterRequest& Request) { ListBuckets(Request); }, + HttpVerb::kGet); + + m_Router.RegisterRoute( + "bucket", + [this](zen::HttpRouterRequest& Request) { ListBuckets(Request); }, + HttpVerb::kGet); + + m_Router.RegisterRoute( "bucket", [this](zen::HttpRouterRequest& Request) { CreateBucket(Request); }, HttpVerb::kPost | HttpVerb::kPut); @@ -332,6 +342,98 @@ HttpObjectStoreService::GetBucketDirectory(std::string_view BucketName) } void +HttpObjectStoreService::ListBuckets(zen::HttpRouterRequest& Request) +{ + namespace fs = std::filesystem; + + const fs::path BucketsPath = m_Cfg.RootDirectory / "buckets"; + + CbObjectWriter Response; + Response.BeginArray("buckets"); + { + std::lock_guard _(BucketsMutex); + + // Configured buckets + for (const ObjectStoreConfig::BucketConfig& Bucket : m_Cfg.Buckets) + { + Response.BeginObject(); + Response << "name" << Bucket.Name; + + const fs::path Dir = Bucket.Directory.empty() ? (m_Cfg.RootDirectory / Bucket.Name) : Bucket.Directory; + if (IsDir(Dir)) + { + uint64_t TotalSize = 0; + uint64_t FileCount = 0; + for (const fs::directory_entry& Entry : + fs::recursive_directory_iterator(Dir, fs::directory_options::skip_permission_denied)) + { + if (Entry.is_regular_file()) + { + TotalSize += Entry.file_size(); + FileCount++; + } + } + Response << "size" << TotalSize; + Response << "object_count" << FileCount; + } + Response.EndObject(); + } + + // Dynamic buckets (on-disk directories not in config) + if (IsDir(BucketsPath)) + { + for (const fs::directory_entry& Entry : fs::directory_iterator(BucketsPath)) + { + if (!Entry.is_directory()) + { + continue; + } + const std::string Name = Entry.path().filename().string(); + + // Skip if already listed as a configured bucket + bool IsConfigured = false; + for (const ObjectStoreConfig::BucketConfig& Bucket : m_Cfg.Buckets) + { + if (Bucket.Name == Name) + { + IsConfigured = true; + break; + } + } + if (IsConfigured) + { + continue; + } + + Response.BeginObject(); + Response << "name" << Name; + + uint64_t TotalSize = 0; + uint64_t FileCount = 0; + for (const fs::directory_entry& FileEntry : + fs::recursive_directory_iterator(Entry.path(), fs::directory_options::skip_permission_denied)) + { + if (FileEntry.is_regular_file()) + { + TotalSize += FileEntry.file_size(); + FileCount++; + } + } + Response << "size" << TotalSize; + Response << "object_count" << FileCount; + + Response.EndObject(); + } + } + } + Response.EndArray(); + + Response << "total_bytes_served" << TotalBytesServed.load(); + + return Request.ServerRequest().WriteResponse(HttpResponseCode::OK, Response.Save()); +} + +void HttpObjectStoreService::CreateBucket(zen::HttpRouterRequest& Request) { namespace fs = std::filesystem; diff --git a/src/zenserver/storage/objectstore/objectstore.h b/src/zenserver/storage/objectstore/objectstore.h index 44e50e208..cc47b50c4 100644 --- a/src/zenserver/storage/objectstore/objectstore.h +++ b/src/zenserver/storage/objectstore/objectstore.h @@ -37,6 +37,7 @@ public: private: void Inititalize(); std::filesystem::path GetBucketDirectory(std::string_view BucketName); + void ListBuckets(zen::HttpRouterRequest& Request); void CreateBucket(zen::HttpRouterRequest& Request); void ListBucket(zen::HttpRouterRequest& Request, const std::string_view Path); void DeleteBucket(zen::HttpRouterRequest& Request); diff --git a/src/zenserver/storage/storageconfig.cpp b/src/zenserver/storage/storageconfig.cpp index e8ccb9097..0dbb45164 100644 --- a/src/zenserver/storage/storageconfig.cpp +++ b/src/zenserver/storage/storageconfig.cpp @@ -373,6 +373,7 @@ ZenStorageServerConfigurator::AddConfigOptions(LuaConfig::Options& LuaOptions) ////// server LuaOptions.AddOption("server.pluginsconfigfile"sv, ServerOptions.PluginsConfigFile, "plugins-config"sv); + LuaOptions.AddOption("sessions.url"sv, ServerOptions.SessionsTargetUrl, "sessions-url"sv); ////// objectstore LuaOptions.AddOption("server.objectstore.enabled"sv, ServerOptions.ObjectStoreEnabled, "objectstore-enabled"sv); @@ -620,6 +621,8 @@ ZenStorageServerCmdLineOptions::AddCliOptions(cxxopts::Options& options, ZenStor cxxopts::value(ServerOptions.ScrubOptions)->implicit_value("yes"), "(nocas,nogc,nodelete,yes,no)*"); + options.add_options()("sessions-url", "URL of remote zenserver to announce session to", cxxopts::value<std::string>(SessionsTargetUrl)); + AddSecurityOptions(options, ServerOptions); AddCacheOptions(options, ServerOptions); AddGcOptions(options, ServerOptions); @@ -1069,6 +1072,7 @@ ZenStorageServerCmdLineOptions::ApplyOptions(cxxopts::Options& options, ZenStora {.Name = OpenIdProviderName, .Url = OpenIdProviderUrl, .ClientId = OpenIdClientId}); } + ServerOptions.SessionsTargetUrl = SessionsTargetUrl; ServerOptions.ObjectStoreConfig = ParseBucketConfigs(BucketConfigs); ServerOptions.OidcTokenExecutable = MakeSafeAbsolutePath(OidcTokenExecutable); } diff --git a/src/zenserver/storage/storageconfig.h b/src/zenserver/storage/storageconfig.h index 128804d92..18af4f096 100644 --- a/src/zenserver/storage/storageconfig.h +++ b/src/zenserver/storage/storageconfig.h @@ -162,6 +162,7 @@ struct ZenStorageServerConfig : public ZenServerConfig bool RestrictContentTypes = false; std::filesystem::path OidcTokenExecutable; bool AllowExternalOidcTokenExe = true; + std::string SessionsTargetUrl; }; struct ZenStorageServerCmdLineOptions @@ -183,6 +184,8 @@ struct ZenStorageServerCmdLineOptions void AddSecurityOptions(cxxopts::Options& options, ZenStorageServerConfig& ServerOptions); + std::string SessionsTargetUrl; + std::string UpstreamCachePolicyOptions; void AddCacheOptions(cxxopts::Options& options, ZenStorageServerConfig& ServerOptions); diff --git a/src/zenserver/storage/zenstorageserver.cpp b/src/zenserver/storage/zenstorageserver.cpp index 68d722f60..de00eb1c2 100644 --- a/src/zenserver/storage/zenstorageserver.cpp +++ b/src/zenserver/storage/zenstorageserver.cpp @@ -34,9 +34,11 @@ #include <zentelemetry/otlptrace.h> #include <zenutil/logging.h> #include <zenutil/service.h> +#include <zenutil/sessionsclient.h> #include <zenutil/workerpools.h> #include <zenutil/zenserverprocess.h> -#include "../sessions/sessions.h" +#include "sessions/inprocsessionlogsink.h" +#include "sessions/sessions.h" #if ZEN_PLATFORM_WINDOWS # include <zencore/windows.h> @@ -251,7 +253,22 @@ ZenStorageServer::InitializeServices(const ZenStorageServerConfig& ServerOptions { m_SessionsService = std::make_unique<SessionsService>(); - m_HttpSessionsService = std::make_unique<HttpSessionsService>(m_StatusService, m_StatsService, *m_SessionsService); + m_HttpSessionsService = std::make_unique<HttpSessionsService>(m_StatusService, m_StatsService, *m_SessionsService, m_IoContext); + m_HttpSessionsService->SetSelfSessionId(GetSessionId()); + + m_InProcSessionLogSink = logging::SinkPtr(new InProcSessionLogSink(*m_SessionsService)); + m_InProcSessionLogSink->SetLevel(logging::Info); + GetDefaultBroadcastSink()->AddSink(m_InProcSessionLogSink); + } + + if (!ServerOptions.SessionsTargetUrl.empty()) + { + m_SessionsClient = std::make_unique<SessionsServiceClient>(SessionsServiceClient::Options{ + .TargetUrl = ServerOptions.SessionsTargetUrl, + .AppName = "zenserver", + .Mode = GetServerMode(), + .SessionId = GetSessionId(), + }); } if (ServerOptions.BuildStoreConfig.Enabled) @@ -821,6 +838,17 @@ ZenStorageServer::Run() OnReady(); + m_SessionsService->RegisterSession(GetSessionId(), "zenserver", GetServerMode(), Oid::Zero, {}); + + if (m_SessionsClient) + { + (void)m_SessionsClient->Announce(); + EnqueueSessionAnnounceTimer(); + + m_SessionLogSink = m_SessionsClient->CreateLogSink(); + GetDefaultBroadcastSink()->AddSink(m_SessionLogSink); + } + if (m_IsPowerCycle) { ZEN_INFO("Power cycle mode enabled -- shutting down"); @@ -841,19 +869,48 @@ ZenStorageServer::Cleanup() ZEN_INFO(ZEN_APP_NAME " cleaning up"); try { + m_SessionAnnounceTimer.cancel(); + + // Stop the IO context and join its thread first, before removing sinks. + // This ensures no async operations are trying to log through the + // broadcast sink while we modify its sink list. m_IoContext.stop(); if (m_IoRunner.joinable()) { m_IoRunner.join(); } - ShutdownServices(); - + // Close the HTTP server before removing sinks. HTTP worker threads + // continuously log messages and hold the BroadcastSink shared lock, + // which would starve the exclusive lock needed by RemoveSink(). if (m_Http) { m_Http->Close(); } + if (m_InProcSessionLogSink) + { + GetDefaultBroadcastSink()->RemoveSink(m_InProcSessionLogSink); + m_InProcSessionLogSink = {}; + } + if (m_SessionLogSink) + { + GetDefaultBroadcastSink()->RemoveSink(m_SessionLogSink); + m_SessionLogSink = {}; + } + if (m_SessionsClient) + { + (void)m_SessionsClient->Remove(); + m_SessionsClient.reset(); + } + + if (m_SessionsService) + { + m_SessionsService->RemoveSession(GetSessionId()); + } + + ShutdownServices(); + if (m_JobQueue) { m_JobQueue->Stop(); @@ -938,6 +995,20 @@ ZenStorageServer::CheckStateMarker() } void +ZenStorageServer::EnqueueSessionAnnounceTimer() +{ + m_SessionAnnounceTimer.expires_after(std::chrono::seconds(15)); + m_SessionAnnounceTimer.async_wait([this](const asio::error_code& Ec) { + if (!Ec && m_SessionsClient) + { + (void)m_SessionsClient->Announce(); + EnqueueSessionAnnounceTimer(); + } + }); + EnsureIoRunner(); +} + +void ZenStorageServer::Flush() { ZEN_TRACE_CPU("ZenStorageServer::Flush"); diff --git a/src/zenserver/storage/zenstorageserver.h b/src/zenserver/storage/zenstorageserver.h index d625f869c..fad22ad54 100644 --- a/src/zenserver/storage/zenstorageserver.h +++ b/src/zenserver/storage/zenstorageserver.h @@ -12,7 +12,6 @@ #include <zenstore/gc.h> #include <zenstore/projectstore.h> -#include "../sessions/httpsessions.h" #include "admin/admin.h" #include "buildstore/httpbuildstore.h" #include "cache/httpstructuredcache.h" @@ -20,11 +19,14 @@ #include "frontend/frontend.h" #include "objectstore/objectstore.h" #include "projectstore/httpprojectstore.h" +#include "sessions/httpsessions.h" #include "stats/statsreporter.h" #include "upstream/upstream.h" #include "vfs/vfsservice.h" #include "workspaces/httpworkspaces.h" +#include <zenutil/sessionsclient.h> + #if ZEN_WITH_COMPUTE_SERVICES # include <zencompute/httpcomputeservice.h> #endif @@ -94,6 +96,12 @@ private: std::unique_ptr<HttpAdminService> m_AdminService; std::unique_ptr<HttpApiService> m_ApiService; + std::unique_ptr<SessionsServiceClient> m_SessionsClient; + logging::SinkPtr m_SessionLogSink; + logging::SinkPtr m_InProcSessionLogSink; + asio::steady_timer m_SessionAnnounceTimer{m_IoContext}; + void EnqueueSessionAnnounceTimer(); + #if ZEN_WITH_COMPUTE_SERVICES std::unique_ptr<compute::HttpComputeService> m_HttpComputeService; #endif |