aboutsummaryrefslogtreecommitdiff
path: root/src/zenserver/sessions/sessions.cpp
blob: 1212ba5d8f15c02d544ad779d44d16c134c2228b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright Epic Games, Inc. All Rights Reserved.

#include "sessions.h"

#include <zencore/basicfile.h>
#include <zencore/fmtutils.h>
#include <zencore/logging.h>

namespace zen {
using namespace std::literals;

class SessionLog : public TRefCounted<SessionLog>
{
public:
	SessionLog(std::filesystem::path LogFilePath) { m_LogFile.Open(LogFilePath, BasicFile::Mode::kWrite); }

private:
	BasicFile m_LogFile;
};

class SessionLogStore
{
public:
	SessionLogStore(std::filesystem::path StoragePath) : m_StoragePath(std::move(StoragePath)) {}

	~SessionLogStore() = default;

	Ref<SessionLog> GetLogForSession(const Oid& SessionId)
	{
		// For now, just return a new log for each session. We can implement actual log storage and retrieval later.
		return Ref(new SessionLog(m_StoragePath / (SessionId.ToString() + ".log")));
	}

	Ref<SessionLog> CreateLogForSession(const Oid& SessionId)
	{
		// For now, just return a new log for each session. We can implement actual log storage and retrieval later.
		return Ref(new SessionLog(m_StoragePath / (SessionId.ToString() + ".log")));
	}

private:
	std::filesystem::path m_StoragePath;
};

SessionsService::Session::Session(const SessionInfo& Info) : m_Info(Info)
{
}
SessionsService::Session::~Session() = default;

void
SessionsService::Session::AppendLog(LogEntry Entry)
{
	RwLock::ExclusiveLockScope Lock(m_LogLock);
	m_LogEntries.push_back(std::move(Entry));
	++m_TotalAppended;
	while (m_LogEntries.size() > MaxLogEntries)
	{
		m_LogEntries.pop_front();
	}
}

std::vector<SessionsService::LogEntry>
SessionsService::Session::GetLogEntries(uint32_t Limit, uint32_t Offset) const
{
	RwLock::SharedLockScope Lock(m_LogLock);

	const uint32_t Total = uint32_t(m_LogEntries.size());
	if (Offset >= Total)
	{
		return {};
	}

	const uint32_t Available = Total - Offset;
	const uint32_t Count	 = (Limit > 0) ? std::min(Limit, Available) : Available;

	std::vector<LogEntry> Result;
	Result.reserve(Count);
	for (uint32_t i = Offset; i < Offset + Count; i++)
	{
		Result.push_back(m_LogEntries[i]);
	}
	return Result;
}

uint64_t
SessionsService::Session::GetLogCount() const
{
	RwLock::SharedLockScope Lock(m_LogLock);
	return m_LogEntries.size();
}

SessionsService::Session::CursorResult
SessionsService::Session::GetLogEntriesAfter(uint64_t AfterCursor) const
{
	RwLock::SharedLockScope Lock(m_LogLock);

	const uint64_t DequeSize = m_LogEntries.size();

	// Cursor 0 means "give me everything currently in the deque".
	// Otherwise, compute how many new entries were appended since the cursor.
	uint64_t NewCount = (AfterCursor == 0) ? DequeSize : (m_TotalAppended > AfterCursor ? m_TotalAppended - AfterCursor : 0);

	// Clamp to what's actually available in the deque (entries may have been evicted).
	NewCount = std::min(NewCount, DequeSize);

	std::vector<LogEntry> Result;
	Result.reserve(NewCount);

	const uint64_t StartIndex = DequeSize - NewCount;
	for (uint64_t i = StartIndex; i < DequeSize; i++)
	{
		Result.push_back(m_LogEntries[i]);
	}

	return CursorResult{
		.Entries = std::move(Result),
		.Cursor	 = m_TotalAppended,
		.Count	 = DequeSize,
	};
}

//////////////////////////////////////////////////////////////////////////

SessionsService::SessionsService() : m_Log(logging::Get("sessions"))
{
}

SessionsService::~SessionsService() = default;

bool
SessionsService::RegisterSession(const Oid& SessionId, std::string AppName, std::string Mode, const Oid& JobId, CbObjectView Metadata)
{
	// Log outside the lock scope — InProcSessionLogSink calls back into
	// GetSession() which acquires m_Lock shared, so logging while holding
	// m_Lock exclusively would deadlock.
	{
		RwLock::ExclusiveLockScope Lock(m_Lock);

		if (m_Sessions.contains(SessionId))
		{
			return false;
		}

		const DateTime Now = DateTime::Now();
		m_Sessions.emplace(SessionId,
						   Ref(new Session(SessionInfo{.Id		  = SessionId,
													   .AppName	  = AppName,
													   .Mode	  = Mode,
													   .JobId	  = JobId,
													   .Metadata  = CbObject::Clone(Metadata),
													   .CreatedAt = Now,
													   .UpdatedAt = Now})));
	}

	ZEN_INFO("Session {} registered (AppName: {}, Mode: {}, JobId: {})", SessionId, AppName, Mode, JobId);
	return true;
}

bool
SessionsService::UpdateSession(const Oid& SessionId, CbObjectView Metadata)
{
	RwLock::ExclusiveLockScope Lock(m_Lock);

	auto It = m_Sessions.find(SessionId);
	if (It == m_Sessions.end())
	{
		return false;
	}

	It.value()->UpdateMetadata(Metadata);

	const SessionInfo& Info = It.value()->Info();
	ZEN_DEBUG("Session {} updated (AppName: {}, JobId: {})", SessionId, Info.AppName, Info.JobId);
	return true;
}

Ref<SessionsService::Session>
SessionsService::GetSession(const Oid& SessionId) const
{
	RwLock::SharedLockScope Lock(m_Lock);

	auto It = m_Sessions.find(SessionId);
	if (It == m_Sessions.end())
	{
		return {};
	}

	return It->second;
}

std::vector<Ref<SessionsService::Session>>
SessionsService::GetSessions() const
{
	RwLock::SharedLockScope Lock(m_Lock);

	std::vector<Ref<Session>> Result;
	Result.reserve(m_Sessions.size());
	for (const auto& [Id, SessionRef] : m_Sessions)
	{
		Result.push_back(SessionRef);
	}
	return Result;
}

bool
SessionsService::RemoveSession(const Oid& SessionId)
{
	std::string RemovedAppName;
	Oid			RemovedJobId;

	{
		RwLock::ExclusiveLockScope Lock(m_Lock);

		auto It = m_Sessions.find(SessionId);
		if (It == m_Sessions.end())
		{
			return false;
		}

		RemovedAppName = It.value()->Info().AppName;
		RemovedJobId   = It.value()->Info().JobId;

		Ref<Session> Ended = It.value();
		Ended->SetEndedAt(DateTime::Now());
		m_EndedSessions.push_back(std::move(Ended));

		m_Sessions.erase(It);
	}

	ZEN_INFO("Session {} removed (AppName: {}, JobId: {})", SessionId, RemovedAppName, RemovedJobId);
	return true;
}

std::vector<Ref<SessionsService::Session>>
SessionsService::GetEndedSessions() const
{
	RwLock::SharedLockScope Lock(m_Lock);
	return m_EndedSessions;
}

uint64_t
SessionsService::GetSessionCount() const
{
	RwLock::SharedLockScope Lock(m_Lock);
	return m_Sessions.size();
}

}  // namespace zen