-
Notifications
You must be signed in to change notification settings - Fork 543
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixing module registration logic + Implementing no-op & JSON module benchmarking #917
Open
TalZaccai
wants to merge
13
commits into
main
Choose a base branch
from
talzacc/bdn_module
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+593
−41
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2a0e05b
wip
TalZaccai 0f2f8a1
wip
TalZaccai 4c3a7d6
wip
TalZaccai 3e98846
wip
TalZaccai 59e204f
wip
TalZaccai 55ede47
wip
TalZaccai 33542d9
format
TalZaccai 6bb1d89
Added comments
TalZaccai 403b98d
small fixes
TalZaccai 165b27c
Moved NoOpModule under playground/
TalZaccai dc732a9
Fixing NoOpModule csproj
TalZaccai ace176a
Added programmatic load test
TalZaccai 6afddea
Merge branch 'main' into talzacc/bdn_module
TalZaccai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
using System.Reflection; | ||
using System.Text; | ||
using BenchmarkDotNet.Attributes; | ||
using Embedded.server; | ||
|
||
namespace BDN.benchmark.Operations | ||
{ | ||
/// <summary> | ||
/// Benchmark for ModuleOperations | ||
/// </summary> | ||
[MemoryDiagnoser] | ||
public class ModuleOperations : OperationsBase | ||
{ | ||
static ReadOnlySpan<byte> NOOPCMDREAD => "*2\r\n$22\r\nNoOpModule.NOOPCMDREAD\r\n$2\r\nk1\r\n"u8; | ||
Request noOpCmdRead; | ||
|
||
static ReadOnlySpan<byte> NOOPCMDRMW => "*2\r\n$21\r\nNoOpModule.NOOPCMDRMW\r\n$2\r\nk1\r\n"u8; | ||
Request noOpCmdRmw; | ||
|
||
static ReadOnlySpan<byte> NOOPOBJRMW => "*2\r\n$21\r\nNoOpModule.NOOPOBJRMW\r\n$2\r\nk2\r\n"u8; | ||
Request noOpObjRmw; | ||
|
||
static ReadOnlySpan<byte> NOOPOBJREAD => "*2\r\n$22\r\nNoOpModule.NOOPOBJREAD\r\n$2\r\nk2\r\n"u8; | ||
Request noOpObjRead; | ||
|
||
static ReadOnlySpan<byte> NOOPPROC => "*1\r\n$19\r\nNoOpModule.NOOPPROC\r\n"u8; | ||
Request noOpProc; | ||
|
||
static ReadOnlySpan<byte> NOOPTXN => "*1\r\n$18\r\nNoOpModule.NOOPTXN\r\n"u8; | ||
Request noOpTxn; | ||
|
||
static ReadOnlySpan<byte> JSONGETCMD => "*3\r\n$8\r\nJSON.GET\r\n$2\r\nk3\r\n$1\r\n$\r\n"u8; | ||
Request jsonGetCmd; | ||
|
||
static ReadOnlySpan<byte> JSONSETCMD => "*4\r\n$8\r\nJSON.SET\r\n$2\r\nk3\r\n$4\r\n$.f2\r\n$1\r\n2\r\n"u8; | ||
Request jsonSetCmd; | ||
|
||
private void RegisterModules() | ||
{ | ||
server.Register.NewModule(new NoOpModule.NoOpModule(), [], out _); | ||
server.Register.NewModule(new GarnetJSON.Module(), [], out _); | ||
} | ||
|
||
public override void GlobalSetup() | ||
{ | ||
base.GlobalSetup(); | ||
RegisterModules(); | ||
|
||
SetupOperation(ref noOpCmdRead, NOOPCMDREAD); | ||
SetupOperation(ref noOpCmdRmw, NOOPCMDRMW); | ||
SetupOperation(ref noOpObjRead, NOOPOBJREAD); | ||
SetupOperation(ref noOpObjRmw, NOOPOBJRMW); | ||
SetupOperation(ref noOpProc, NOOPPROC); | ||
SetupOperation(ref noOpTxn, NOOPTXN); | ||
|
||
SetupOperation(ref jsonGetCmd, JSONGETCMD); | ||
SetupOperation(ref jsonSetCmd, JSONSETCMD); | ||
|
||
SlowConsumeMessage("*3\r\n$3\r\nSET\r\n$2\r\nk1\r\n$1\r\nc\r\n"u8); | ||
SlowConsumeMessage(NOOPOBJRMW); | ||
SlowConsumeMessage("*4\r\n$8\r\nJSON.SET\r\n$2\r\nk3\r\n$1\r\n$\r\n$14\r\n{\"f1\":{\"a\":1}}\r\n"u8); | ||
} | ||
|
||
[Benchmark] | ||
public void ModuleNoOpRawStringReadCommand() | ||
{ | ||
Send(noOpCmdRead); | ||
} | ||
|
||
[Benchmark] | ||
public void ModuleNoOpRawStringRmwCommand() | ||
{ | ||
Send(noOpCmdRmw); | ||
} | ||
|
||
[Benchmark] | ||
public void ModuleNoOpObjRmwCommand() | ||
{ | ||
Send(noOpObjRmw); | ||
} | ||
|
||
[Benchmark] | ||
public void ModuleNoOpObjReadCommand() | ||
{ | ||
Send(noOpObjRead); | ||
} | ||
|
||
[Benchmark] | ||
public void ModuleNoOpProc() | ||
{ | ||
Send(noOpProc); | ||
} | ||
|
||
[Benchmark] | ||
public void ModuleNoOpTxn() | ||
{ | ||
Send(noOpTxn); | ||
} | ||
|
||
[Benchmark] | ||
public void ModuleJsonGetCommand() | ||
{ | ||
Send(jsonGetCmd); | ||
} | ||
|
||
[Benchmark] | ||
public void ModuleJsonSetCommand() | ||
{ | ||
Send(jsonSetCmd); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
using System; | ||
using System.Collections.Concurrent; | ||
using System.Diagnostics; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Garnet.server | ||
{ | ||
|
@@ -24,6 +25,8 @@ public class CustomCommandManager | |
private ConcurrentExpandableMap<CustomTransaction> transactionProcMap; | ||
private ConcurrentExpandableMap<CustomProcedureWrapper> customProcedureMap; | ||
|
||
private readonly ConcurrentDictionary<string, ModuleLoadContext> modules; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need strings and a ConcDict here? seems high overhead? |
||
|
||
internal static readonly int MinMapSize = 8; | ||
internal static readonly byte CustomTypeIdStartOffset = (byte)CustomObjectTypeMinId; | ||
|
||
|
@@ -46,6 +49,8 @@ public CustomCommandManager() | |
|
||
transactionProcMap = new ConcurrentExpandableMap<CustomTransaction>(MinMapSize, 0, byte.MaxValue); | ||
customProcedureMap = new ConcurrentExpandableMap<CustomProcedureWrapper>(MinMapSize, 0, byte.MaxValue); | ||
|
||
modules = new(); | ||
} | ||
|
||
internal int Register(string name, CommandType type, CustomRawStringFunctions customFunctions, | ||
|
@@ -137,6 +142,45 @@ internal int Register(string name, Func<CustomProcedure> customProcedure, RespCo | |
return cmdId; | ||
} | ||
|
||
/// <summary> | ||
/// Register module | ||
/// </summary> | ||
/// <param name="module">Module to register</param> | ||
/// <param name="moduleArgs">Module arguments</param> | ||
/// <param name="logger">Logger</param> | ||
/// <param name="errorMessage">Error message</param> | ||
/// <returns>True if module registered successfully</returns> | ||
public bool RegisterModule(ModuleBase module, string[] moduleArgs, ILogger logger, | ||
out ReadOnlySpan<byte> errorMessage) | ||
{ | ||
errorMessage = default; | ||
|
||
var moduleLoadContext = new ModuleLoadContext(this, logger); | ||
try | ||
{ | ||
module.OnLoad(moduleLoadContext, moduleArgs); | ||
} | ||
catch (Exception ex) | ||
{ | ||
logger?.LogError(ex, "Error loading module"); | ||
errorMessage = CmdStrings.RESP_ERR_MODULE_ONLOAD; | ||
return false; | ||
} | ||
|
||
if (!moduleLoadContext.Initialized) | ||
{ | ||
errorMessage = CmdStrings.RESP_ERR_MODULE_ONLOAD; | ||
logger?.LogError("Module {0} failed to initialize", moduleLoadContext.Name); | ||
return false; | ||
} | ||
|
||
logger?.LogInformation("Module {0} version {1} loaded", moduleLoadContext.Name, moduleLoadContext.Version); | ||
return true; | ||
} | ||
|
||
internal bool TryAddModule(ModuleLoadContext moduleLoadContext) | ||
=> modules.TryAdd(moduleLoadContext.Name, moduleLoadContext); | ||
|
||
internal bool TryGetCustomProcedure(int id, out CustomProcedureWrapper value) | ||
=> customProcedureMap.TryGetValue(id, out value); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
overall LGTM, if you could step through each of these/profile them and see if the unexpected allocations can be accounted/removed as part of this PR, that would be great. thanks.