Skip to content

Commit 9239518

Browse files
committed
fix tests
1 parent 4b55f59 commit 9239518

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+531
-554
lines changed

hosting/Windows/Garnet.worker/Worker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class Worker : BackgroundService
1414
private bool _isDisposed = false;
1515
private readonly string[] args;
1616

17-
private GarnetServer server;
17+
private GarnetApplication server;
1818

1919
public Worker(string[] args)
2020
{

libs/host/GarnetApplication.cs

Lines changed: 32 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22
// Licensed under the MIT license.
33

44
using System;
5-
using System.Diagnostics;
6-
using System.Reflection;
75
using System.Threading;
86
using System.Threading.Tasks;
97
using Garnet.common;
108
using Garnet.server;
119
using Microsoft.Extensions.DependencyInjection;
1210
using Microsoft.Extensions.Hosting;
13-
using Microsoft.Extensions.Logging;
1411
using Microsoft.Extensions.Options;
1512

1613
namespace Garnet.host;
@@ -20,77 +17,58 @@ namespace Garnet.host;
2017
/// </summary>
2118
public class GarnetApplication : IHost
2219
{
20+
/// <summary>
21+
/// Metrics API
22+
/// </summary>
23+
public MetricsApi Metrics;
24+
25+
/// <summary>
26+
/// Command registration API
27+
/// </summary>
28+
public RegisterApi Register;
29+
30+
/// <summary>
31+
/// Store API
32+
/// </summary>
33+
public StoreApi Store;
34+
35+
internal GarnetProvider Provider;
36+
2337
private readonly IHost host;
2438

2539
public GarnetApplication(IHost host)
2640
{
2741
this.host = host;
42+
43+
Metrics = host.Services.GetRequiredService<MetricsApi>();
44+
Register = host.Services.GetService<RegisterApi>();
45+
Store = host.Services.GetService<StoreApi>();
46+
47+
Provider = host.Services.GetRequiredService<GarnetProvider>();
2848
}
2949

3050
public IServiceProvider Services => host.Services;
3151

3252
public Task StartAsync(CancellationToken cancellationToken = default)
33-
{
34-
var opts = host.Services.GetRequiredService<IOptions<GarnetServerOptions>>();
35-
var logger = host.Services.GetRequiredService<ILogger<GarnetServer>>();
36-
37-
Debug.Assert(opts != null);
38-
39-
var version = GetVersion();
40-
41-
if (!opts.Value.QuietMode)
42-
{
43-
var red = "\u001b[31m";
44-
var magenta = "\u001b[35m";
45-
var normal = "\u001b[0m";
46-
47-
Console.WriteLine($@"{red} _________
48-
/_||___||_\ {normal}Garnet {version} {(IntPtr.Size == 8 ? "64" : "32")} bit; {(opts.Value.EnableCluster ? "cluster" : "standalone")} mode{red}
49-
'. \ / .' {normal}Port: {opts.Value.Port}{red}
50-
'.\ /.' {magenta}https://aka.ms/GetGarnet{red}
51-
'.'
52-
{normal}");
53-
}
54-
55-
Trace.Listeners.Add(new ConsoleTraceListener());
56-
57-
// Set up an initial memory logger to log messages from configuration parser into memory.
58-
using var memLogProvider = new MemoryLoggerProvider();
59-
60-
var initLogger = (MemoryLogger)memLogProvider.CreateLogger("ArgParser");
61-
62-
logger?.LogInformation("Garnet {version} {bits} bit; {clusterMode} mode; Port: {port}", GetVersion(), IntPtr.Size == 8 ? "64" : "32", opts.Value.EnableCluster ? "cluster" : "standalone", opts.Value.Port);
63-
64-
// Flush initialization logs from memory logger
65-
initLogger.FlushLogger(logger);
66-
67-
var setMax = opts.Value.ThreadPoolMaxThreads <= 0 || ThreadPool.SetMaxThreads(opts.Value.ThreadPoolMaxThreads, opts.Value.ThreadPoolMaxThreads);
68-
69-
if (opts.Value.ThreadPoolMinThreads > 0 && !ThreadPool.SetMinThreads(opts.Value.ThreadPoolMinThreads, opts.Value.ThreadPoolMinThreads))
70-
throw new Exception($"Unable to call ThreadPool.SetMinThreads with {opts.Value.ThreadPoolMinThreads}");
71-
72-
// Retry to set max threads if it wasn't set in the previous step
73-
if (!setMax && !ThreadPool.SetMaxThreads(opts.Value.ThreadPoolMaxThreads, opts.Value.ThreadPoolMaxThreads))
74-
throw new Exception($"Unable to call ThreadPool.SetMaxThreads with {opts.Value.ThreadPoolMaxThreads}");
75-
76-
logger?.LogTrace("TLS is {tlsEnabled}", opts.Value.TlsOptions == null ? "disabled" : "enabled");
77-
78-
return host.StartAsync(cancellationToken);
79-
}
53+
=> host.StartAsync(cancellationToken);
8054

8155
public Task StopAsync(CancellationToken cancellationToken = default)
8256
=> host.StopAsync(cancellationToken);
83-
84-
public void Dispose() => host.Dispose();
57+
58+
public void Dispose()
59+
{
60+
host.Dispose();
61+
}
8562

8663
public void Run()
8764
{
8865
HostingAbstractionsHostExtensions.Run(this);
8966
}
9067

91-
public async Task RunAsync(CancellationToken cancellationToken = default)
68+
public Task RunAsync(CancellationToken cancellationToken = default)
9269
{
93-
await HostingAbstractionsHostExtensions.RunAsync(this, cancellationToken);
70+
HostingAbstractionsHostExtensions.RunAsync(this, cancellationToken);
71+
return Task.CompletedTask;
9472
}
9573

9674
public static GarnetApplicationBuilder CreateHostBuilder(string[] args)
@@ -125,10 +103,4 @@ public static GarnetApplicationBuilder CreateHostBuilder(string[] args, GarnetSe
125103
{
126104
return new (new GarnetApplicationOptions {Args = args}, options);
127105
}
128-
129-
private static string GetVersion()
130-
{
131-
var Version = Assembly.GetExecutingAssembly().GetName().Version;
132-
return $"{Version.Major}.{Version.Minor}.{Version.Build}";
133-
}
134106
}

libs/host/GarnetApplicationBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ internal GarnetApplicationBuilder(GarnetApplicationOptions options, GarnetServer
5757
hostApplicationBuilder.Services.AddSingleton(garnetServerOptionsWrapped);
5858

5959
hostApplicationBuilder.Services.AddSingleton<IGarnetServer, GarnetServerTcp>();
60+
hostApplicationBuilder.Services.AddHostedService<GarnetServer>();
61+
6062
hostApplicationBuilder.Services.AddSingleton<StoreFactory>();
6163
hostApplicationBuilder.Services.AddSingleton<CustomCommandManager>();
6264

@@ -73,7 +75,7 @@ internal GarnetApplicationBuilder(GarnetApplicationOptions options, GarnetServer
7375

7476
var store = storeFactory.CreateMainStore(out var checkpointDir);
7577
var objectStore = storeFactory.CreateObjectStore(checkpointDir, out var objectStoreSizeTracker);
76-
78+
7779
TsavoriteLog appendOnlyFile = null;
7880

7981
if (opts.Value.EnableAOF)
@@ -134,8 +136,6 @@ internal GarnetApplicationBuilder(GarnetApplicationOptions options, GarnetServer
134136
hostApplicationBuilder.Services.AddSingleton<MetricsApi>();
135137
hostApplicationBuilder.Services.AddSingleton<RegisterApi>();
136138
hostApplicationBuilder.Services.AddSingleton<StoreApi>();
137-
138-
hostApplicationBuilder.Services.AddHostedService<GarnetServer>();
139139
}
140140

141141
public GarnetApplication Build()

libs/host/GarnetServer.cs

Lines changed: 63 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,145 +1,102 @@
1-
// Copyright (c) Microsoft Corporation.
1+
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33

44
using System;
55
using System.Diagnostics;
66
using System.Reflection;
77
using System.Threading;
88
using System.Threading.Tasks;
9-
using Garnet.common;
109
using Garnet.networking;
1110
using Garnet.server;
1211
using Microsoft.Extensions.Hosting;
1312
using Microsoft.Extensions.Logging;
1413
using Microsoft.Extensions.Options;
15-
using Tsavorite.core;
1614

17-
namespace Garnet;
15+
namespace Garnet.host;
1816

19-
/// <summary>
20-
/// Implementation Garnet server
21-
/// </summary>
22-
public class GarnetServer : IHostedService, IDisposable
17+
public class GarnetServer : IHostedService
2318
{
24-
internal GarnetProvider Provider;
25-
26-
private readonly GarnetServerOptions opts;
27-
private IGarnetServer server;
28-
private SubscribeBroker<SpanByte, SpanByte, IKeySerializer<SpanByte>> subscribeBroker;
19+
private readonly IGarnetServer garnetServerTcp;
20+
private readonly GarnetProvider garnetProvider;
21+
private readonly StoreWrapper store;
22+
private readonly IOptions<GarnetServerOptions> opts;
2923
private readonly ILogger<GarnetServer> logger;
30-
31-
/// <summary>
32-
/// Store and associated information used by this Garnet server
33-
/// </summary>
34-
protected StoreWrapper storeWrapper;
35-
36-
/// <summary>
37-
/// Metrics API
38-
/// </summary>
39-
public MetricsApi Metrics;
40-
41-
/// <summary>
42-
/// Command registration API
43-
/// </summary>
44-
public RegisterApi Register;
45-
46-
/// <summary>
47-
/// Store API
48-
/// </summary>
49-
public StoreApi Store;
50-
24+
5125
public GarnetServer(
52-
IOptions<GarnetServerOptions> opts,
53-
ILogger<GarnetServer> logger,
54-
IGarnetServer garnetServerTcp,
55-
StoreWrapper storeWrapper)
26+
IGarnetServer garnetServerTcp,
27+
GarnetProvider garnetProvider,
28+
StoreWrapper store,
29+
IOptions<GarnetServerOptions> options,
30+
ILogger<GarnetServer> logger)
5631
{
32+
this.garnetServerTcp = garnetServerTcp;
33+
this.garnetProvider = garnetProvider;
34+
this.store = store;
35+
this.opts = options;
5736
this.logger = logger;
58-
this.server = garnetServerTcp;
59-
this.storeWrapper = storeWrapper;
6037

61-
Trace.Listeners.Add(new ConsoleTraceListener());
62-
63-
// Assign values to GarnetServerOptions
64-
this.opts = opts.Value;
65-
this.opts.AuthSettings = this.opts.AuthSettings;
38+
garnetServerTcp.Register(WireFormat.ASCII, garnetProvider);
6639

67-
this.InitializeServer();
6840
}
6941

70-
private void InitializeServer()
71-
{
72-
// Create session provider for Garnet
73-
Provider = new GarnetProvider(storeWrapper, subscribeBroker);
42+
public Task StartAsync(CancellationToken cancellationToken)
43+
{
44+
var version = GetVersion();
45+
46+
if (!opts.Value.QuietMode)
47+
{
48+
var red = "\u001b[31m";
49+
var magenta = "\u001b[35m";
50+
var normal = "\u001b[0m";
51+
52+
Console.WriteLine($@"{red} _________
53+
/_||___||_\ {normal}Garnet {version} {(IntPtr.Size == 8 ? "64" : "32")} bit; {(opts.Value.EnableCluster ? "cluster" : "standalone")} mode{red}
54+
'. \ / .' {normal}Port: {opts.Value.Port}{red}
55+
'.\ /.' {magenta}https://aka.ms/GetGarnet{red}
56+
'.'
57+
{normal}");
58+
}
59+
60+
Trace.Listeners.Add(new ConsoleTraceListener());
7461

75-
// Create user facing API endpoints
76-
Metrics = new MetricsApi(Provider);
77-
Register = new RegisterApi(Provider);
78-
Store = new StoreApi(storeWrapper);
62+
// Set up an initial memory logger to log messages from configuration parser into memory.
63+
using var memLogProvider = new MemoryLoggerProvider();
64+
65+
var initLogger = (MemoryLogger)memLogProvider.CreateLogger("ArgParser");
66+
67+
logger?.LogInformation("Garnet {version} {bits} bit; {clusterMode} mode; Port: {port}", GetVersion(), IntPtr.Size == 8 ? "64" : "32", opts.Value.EnableCluster ? "cluster" : "standalone", opts.Value.Port);
7968

80-
server.Register(WireFormat.ASCII, Provider);
81-
}
69+
// Flush initialization logs from memory logger
70+
initLogger.FlushLogger(logger);
8271

83-
/// <summary>
84-
/// Start server instance
85-
/// </summary>
86-
public void Start()
87-
{
88-
Provider.Recover();
89-
server.Start();
90-
Provider.Start();
91-
if (!opts.QuietMode)
92-
this.logger.LogInformation("* Ready to accept connections");
93-
}
72+
var setMax = opts.Value.ThreadPoolMaxThreads <= 0 || ThreadPool.SetMaxThreads(opts.Value.ThreadPoolMaxThreads, opts.Value.ThreadPoolMaxThreads);
9473

95-
/// <summary>
96-
/// Dispose store (including log and checkpoint directory)
97-
/// </summary>
98-
public void Dispose()
99-
{
100-
Dispose(false);
101-
}
74+
if (opts.Value.ThreadPoolMinThreads > 0 && !ThreadPool.SetMinThreads(opts.Value.ThreadPoolMinThreads, opts.Value.ThreadPoolMinThreads))
75+
throw new Exception($"Unable to call ThreadPool.SetMinThreads with {opts.Value.ThreadPoolMinThreads}");
10276

103-
/// <summary>
104-
/// Dispose, optionally deleting logs and checkpoints
105-
/// </summary>
106-
/// <param name="deleteDir">Whether to delete logs and checkpoints</param>
107-
public void Dispose(bool deleteDir = true)
108-
{
109-
InternalDispose();
110-
if (deleteDir)
111-
{
112-
if (opts.CheckpointDir != opts.LogDir && !string.IsNullOrEmpty(opts.CheckpointDir))
113-
{
114-
var ckptdir = opts.DeviceFactoryCreator();
115-
ckptdir.Initialize(opts.CheckpointDir);
116-
ckptdir.Delete(new FileDescriptor { directoryName = "" });
117-
}
118-
}
119-
}
77+
// Retry to set max threads if it wasn't set in the previous step
78+
if (!setMax && !ThreadPool.SetMaxThreads(opts.Value.ThreadPoolMaxThreads, opts.Value.ThreadPoolMaxThreads))
79+
throw new Exception($"Unable to call ThreadPool.SetMaxThreads with {opts.Value.ThreadPoolMaxThreads}");
12080

121-
private void InternalDispose()
122-
{
123-
// Provider.Dispose will get stuck
124-
//Provider?.Dispose();
81+
logger?.LogTrace("TLS is {tlsEnabled}", opts.Value.TlsOptions == null ? "disabled" : "enabled");
82+
83+
garnetProvider.Recover();
84+
garnetServerTcp.Start();
85+
garnetProvider.Start();
86+
87+
if (!opts.Value.QuietMode)
88+
logger?.LogInformation("* Ready to accept connections");
12589

126-
server.Dispose();
127-
subscribeBroker?.Dispose();
128-
opts.AuthSettings?.Dispose();
129-
}
130-
131-
public Task StartAsync(CancellationToken cancellationToken)
132-
{
133-
Start();
134-
13590
return Task.CompletedTask;
13691
}
13792

13893
public Task StopAsync(CancellationToken cancellationToken)
13994
{
140-
Dispose();
95+
garnetProvider?.Dispose();
96+
garnetServerTcp?.Dispose();
97+
//store?.Dispose();
14198

142-
return Task.CompletedTask;
99+
return Task.CompletedTask;
143100
}
144101

145102
private static string GetVersion()

libs/server/StoreWrapper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace Garnet.server
2626
/// <summary>
2727
/// Wrapper for store and store-specific information
2828
/// </summary>
29-
public sealed class StoreWrapper
29+
public sealed class StoreWrapper
3030
{
3131
internal readonly string version;
3232
internal readonly string redisProtocolVersion;

0 commit comments

Comments
 (0)