Skip to content

Commit 524deeb

Browse files
Add more logging and probe for Lua support (#1136)
* log .NET version, OS, and processor architecture at startup * don't include environment details in art; probe Lua support at app start if enabled * log if exception raised while trying to load a script; since error parameter was alwasy ignored, remove it * formatting --------- Co-authored-by: Tal Zaccai <[email protected]>
1 parent b74267e commit 524deeb

File tree

7 files changed

+52
-11
lines changed

7 files changed

+52
-11
lines changed

benchmark/BDN.benchmark/Lua/LuaScriptCacheOperations.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ public void IterationSetup()
8181
sessionScriptCache.Clear();
8282

8383
// Make outer hit available for every iteration
84-
if (!sessionScriptCache.TryLoad(session, "return 1"u8, new(outerHitDigest), out _, out _, out _, out var error))
84+
if (!sessionScriptCache.TryLoad(session, "return 1"u8, new(outerHitDigest), out _, out _, out _))
8585
{
86-
throw new InvalidOperationException($"Should have been able to load: {error}");
86+
throw new InvalidOperationException("Should have been able to load");
8787
}
8888
}
8989

@@ -146,7 +146,7 @@ private void LoadScript(Span<byte> digest)
146146
{
147147
if (storeWrapper.storeScriptCache.TryGetValue(digestKey, out var source))
148148
{
149-
if (!sessionScriptCache.TryLoad(session, source, digestKey, out runner, out _, out _, out var error))
149+
if (!sessionScriptCache.TryLoad(session, source, digestKey, out runner, out _, out _))
150150
{
151151
// TryLoad will have written an error out, it any
152152

libs/host/GarnetServer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Linq;
88
using System.Net.Sockets;
99
using System.Reflection;
10+
using System.Runtime.InteropServices;
1011
using System.Text;
1112
using System.Threading;
1213
using Garnet.cluster;
@@ -185,6 +186,7 @@ private void InitializeServer()
185186

186187
this.logger = this.loggerFactory?.CreateLogger("GarnetServer");
187188
logger?.LogInformation("Garnet {version} {bits} bit; {clusterMode} mode; Endpoint: {endpoint}", version, IntPtr.Size == 8 ? "64" : "32", opts.EnableCluster ? "cluster" : "standalone", opts.EndPoint);
189+
logger?.LogInformation("Environment .NET {netVersion}; {osPlatform}; {processArch}", Environment.Version, Environment.OSVersion.Platform, RuntimeInformation.ProcessArchitecture);
188190

189191
// Flush initialization logs from memory logger
190192
FlushMemoryLogger(this.initLogger, "ArgParser", this.loggerFactory);

libs/server/Lua/LuaCommands.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private unsafe bool TryEVALSHA()
4848
{
4949
if (storeWrapper.storeScriptCache.TryGetValue(scriptKey, out var source))
5050
{
51-
if (!sessionScriptCache.TryLoad(this, source, scriptKey, out runner, out _, out _, out _))
51+
if (!sessionScriptCache.TryLoad(this, source, scriptKey, out runner, out _, out _))
5252
{
5353
// TryLoad will have written an error out, it any
5454

@@ -121,7 +121,7 @@ private unsafe bool TryEVAL()
121121
sessionScriptCache.GetScriptDigest(script.ReadOnlySpan, digest);
122122

123123
var scriptKey = new ScriptHashKey(digest);
124-
if (!sessionScriptCache.TryLoad(this, script.ReadOnlySpan, scriptKey, out var runner, out _, out _, out var error))
124+
if (!sessionScriptCache.TryLoad(this, script.ReadOnlySpan, scriptKey, out var runner, out _, out _))
125125
{
126126
// TryLoad will have written any errors out
127127
return true;
@@ -251,7 +251,7 @@ private bool NetworkScriptLoad()
251251
Span<byte> digest = stackalloc byte[SessionScriptCache.SHA1Len];
252252
sessionScriptCache.GetScriptDigest(source.Span, digest);
253253

254-
if (sessionScriptCache.TryLoad(this, source.ReadOnlySpan, new(digest), out _, out var digestOnHeap, out var compiledSource, out var error))
254+
if (sessionScriptCache.TryLoad(this, source.ReadOnlySpan, new(digest), out _, out var digestOnHeap, out var compiledSource))
255255
{
256256
// TryLoad will write any errors out
257257

libs/server/Lua/LuaRunner.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2592,6 +2592,26 @@ static unsafe bool TryWriteTableToArray(LuaRunner runner, bool canSend, ref TRes
25922592
}
25932593
}
25942594

2595+
/// <summary>
2596+
/// Attempts a call into Lua libraries.
2597+
///
2598+
/// If this fails, it's basically impossible for any other Lua functionality to work.
2599+
/// </summary>
2600+
public static bool TryProbeSupport(out string errorMessage)
2601+
{
2602+
try
2603+
{
2604+
_ = NativeMethods.Version();
2605+
errorMessage = null;
2606+
return true;
2607+
}
2608+
catch (Exception e)
2609+
{
2610+
errorMessage = e.Message;
2611+
return false;
2612+
}
2613+
}
2614+
25952615
/// <summary>
25962616
/// Construct a bitmap we can quickly check for NoScript commands in.
25972617
/// </summary>

libs/server/Lua/NativeMethods.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,16 @@ internal static partial class NativeMethods
317317
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl), typeof(CallConvSuppressGCTransition)])]
318318
private static partial void lua_pushvalue(lua_State luaState, int stackIndex);
319319

320+
/// <summary>
321+
/// see: https://www.lua.org/manual/5.4/manual.html#lua_version
322+
///
323+
/// Just returns a constant.
324+
/// see: https://www.lua.org/source/5.4/lapi.c.html#lua_version
325+
/// </summary>
326+
[LibraryImport(LuaLibraryName)]
327+
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl), typeof(CallConvSuppressGCTransition)])]
328+
private static partial double lua_version(lua_State ignored);
329+
320330
// Helper methods for using the pinvokes defined above
321331

322332
/// <summary>
@@ -653,6 +663,12 @@ internal static void Rotate(lua_State luaState, int stackIndex, int n)
653663
internal static unsafe void SetHook(lua_State luaState, delegate* unmanaged[Cdecl]<nint, nint, void> hook, LuaHookMask mask, int count)
654664
=> lua_sethook(luaState, (nint)hook, (int)mask, count);
655665

666+
/// <summary>
667+
/// Get the Lua version.
668+
/// </summary>
669+
internal static double Version()
670+
=> lua_version(default);
671+
656672
/// <summary>
657673
/// Invoke the Lua GC.
658674
/// </summary>

libs/server/Lua/SessionScriptCache.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,9 @@ internal bool TryLoad(
132132
ScriptHashKey digest,
133133
out LuaRunner runner,
134134
out ScriptHashKey? digestOnHeap,
135-
out byte[] compiledSource,
136-
out string error
135+
out byte[] compiledSource
137136
)
138137
{
139-
error = null;
140-
141138
if (scriptCache.TryGetValue(digest, out runner))
142139
{
143140
digestOnHeap = null;
@@ -185,7 +182,8 @@ out string error
185182
}
186183
catch (Exception ex)
187184
{
188-
error = ex.Message;
185+
logger?.LogError(ex, "During Lua script loading, an unexpected exception");
186+
189187
digestOnHeap = null;
190188
compiledSource = null;
191189
return false;

libs/server/StoreWrapper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ public StoreWrapper(
175175
{
176176
this.storeScriptCache = [];
177177

178+
if (!LuaRunner.TryProbeSupport(out var errorMessage))
179+
{
180+
logger?.LogCritical("Lua probe failed, Lua scripts will fail {errorMessage}", errorMessage);
181+
}
182+
178183
if (serverOptions.LuaOptions.Timeout != Timeout.InfiniteTimeSpan)
179184
{
180185
this.luaTimeoutManager = new(serverOptions.LuaOptions.Timeout, loggerFactory?.CreateLogger<LuaTimeoutManager>());

0 commit comments

Comments
 (0)