Skip to content

Commit 05cb522

Browse files
committed
register provider, metrics, registration and store api to di
1 parent 58becd1 commit 05cb522

File tree

5 files changed

+90
-30
lines changed

5 files changed

+90
-30
lines changed

libs/host/GarnetApplicationBuilder.cs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Reflection;
67
using Garnet.cluster;
8+
using Garnet.common;
79
using Garnet.server;
810
using Microsoft.Extensions.Configuration;
911
using Microsoft.Extensions.DependencyInjection;
@@ -101,16 +103,38 @@ internal GarnetApplicationBuilder(GarnetApplicationOptions options, GarnetServer
101103
return storeFactory.CreateObjectStore();
102104
});
103105

104-
/*
106+
hostApplicationBuilder.Services.AddSingleton<IGarnetServer, GarnetServerTcp>();
107+
108+
hostApplicationBuilder.Services.AddSingleton(sp =>
109+
{
110+
var options = sp.GetRequiredService<IOptions<GarnetServerOptions>>();
111+
var opts = options.Value;
112+
113+
return new SubscribeBroker<SpanByte, SpanByte, IKeySerializer<SpanByte>>(
114+
new SpanByteKeySerializer(), null, opts.PubSubPageSizeBytes(), opts.SubscriberRefreshFrequencyMs, true);
115+
});
116+
105117
hostApplicationBuilder.Services.AddSingleton<StoreWrapper>(sp =>
106118
{
107119
var storeWrapperFactory = sp.GetRequiredService<StoreWrapperFactory>();
108120

109-
return storeWrapperFactory.Create();
121+
var version = GetVersion();
122+
123+
return storeWrapperFactory.Create(version);
110124
});
111-
*/
112125

113-
hostApplicationBuilder.Services.AddTransient<IGarnetServer, GarnetServerTcp>();
126+
hostApplicationBuilder.Services.AddSingleton<GarnetProviderFactory>();
127+
128+
hostApplicationBuilder.Services.AddSingleton<GarnetProvider>(sp =>
129+
{
130+
var garnetProviderFactory = sp.GetRequiredService<GarnetProviderFactory>();
131+
132+
return garnetProviderFactory.Create();
133+
});
134+
135+
hostApplicationBuilder.Services.AddSingleton<MetricsApi>();
136+
hostApplicationBuilder.Services.AddSingleton<RegisterApi>();
137+
hostApplicationBuilder.Services.AddSingleton<StoreApi>();
114138

115139
hostApplicationBuilder.Services.AddTransient<GarnetServer>();
116140

@@ -171,4 +195,11 @@ public IMetricsBuilder Metrics
171195

172196
public IServiceCollection Services
173197
=> hostApplicationBuilder.Services;
198+
199+
200+
static string GetVersion()
201+
{
202+
var Version = Assembly.GetExecutingAssembly().GetName().Version;
203+
return $"{Version.Major}.{Version.Minor}.{Version.Build}";
204+
}
174205
}

libs/host/GarnetProviderFactory.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
using Garnet.common;
5+
using Garnet.server;
6+
using Microsoft.Extensions.Options;
7+
using Tsavorite.core;
8+
9+
namespace Garnet;
10+
11+
public class GarnetProviderFactory
12+
{
13+
readonly GarnetServerOptions options;
14+
readonly SubscribeBroker<SpanByte, SpanByte, IKeySerializer<SpanByte>> subscribeBroker;
15+
readonly StoreWrapper storeWrapper;
16+
17+
public GarnetProviderFactory(
18+
IOptions<GarnetServerOptions> options,
19+
SubscribeBroker<SpanByte, SpanByte, IKeySerializer<SpanByte>> subscribeBroker,
20+
StoreWrapper storeWrapper)
21+
{
22+
this.options = options.Value;
23+
this.subscribeBroker = subscribeBroker;
24+
this.storeWrapper = storeWrapper;
25+
}
26+
27+
public GarnetProvider Create()
28+
{
29+
if (options.DisablePubSub)
30+
{
31+
return new GarnetProvider(storeWrapper, null);
32+
}
33+
34+
return new GarnetProvider(storeWrapper, subscribeBroker);
35+
}
36+
}

libs/host/GarnetServer.cs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -84,28 +84,35 @@ static string GetVersion()
8484
/// </summary>
8585
public StoreApi Store;
8686

87-
readonly StoreWrapperFactory storeWrapperFactory;
88-
8987
/// <summary>
9088
/// Create Garnet Server instance using GarnetServerOptions instance; use Start to start the server.
9189
/// </summary>
9290
/// <param name="options">Server options</param>
9391
/// <param name="logger">Logger</param>
9492
/// <param name="loggerFactory">Logger factory</param>
9593
/// <param name="server">The IGarnetServer to use. If none is provided, will use a GarnetServerTcp.</param>
96-
/// <param name="storeWrapperFactory"></param>
94+
/// <param name="garnetProvider"></param>
95+
/// <param name="metricsApi"></param>
96+
/// <param name="registerApi"></param>
97+
/// <param name="storeApi"></param>
9798
public GarnetServer(
9899
IOptions<GarnetServerOptions> options,
99100
ILogger<GarnetServer> logger,
100101
ILoggerFactory loggerFactory,
101-
IGarnetServer server,
102-
StoreWrapperFactory storeWrapperFactory)
102+
IGarnetServer server,
103+
GarnetProvider garnetProvider,
104+
MetricsApi metricsApi,
105+
RegisterApi registerApi,
106+
StoreApi storeApi)
103107
{
104108
this.server = server;
105109
this.opts = options.Value;
106110
this.logger = logger;
107111
this.loggerFactory = loggerFactory;
108-
this.storeWrapperFactory = storeWrapperFactory;
112+
this.Provider = garnetProvider;
113+
this.Metrics = metricsApi;
114+
this.Register = registerApi;
115+
this.Store = storeApi;
109116

110117
this.cleanupDir = false;
111118
this.InitializeServerUpdated();
@@ -174,23 +181,8 @@ private void InitializeServerUpdated()
174181
if (!setMax && !ThreadPool.SetMaxThreads(maxThreads, maxCPThreads))
175182
throw new Exception($"Unable to call ThreadPool.SetMaxThreads with {maxThreads}, {maxCPThreads}");
176183

177-
if (!opts.DisablePubSub)
178-
subscribeBroker = new SubscribeBroker<SpanByte, SpanByte, IKeySerializer<SpanByte>>(
179-
new SpanByteKeySerializer(), null, opts.PubSubPageSizeBytes(), opts.SubscriberRefreshFrequencyMs,
180-
true);
181-
182184
logger?.LogTrace("TLS is {tlsEnabled}", opts.TlsOptions == null ? "disabled" : "enabled");
183185

184-
storeWrapper = storeWrapperFactory.Create(version, server);
185-
186-
// Create session provider for Garnet
187-
Provider = new GarnetProvider(storeWrapper, subscribeBroker);
188-
189-
// Create user facing API endpoints
190-
Metrics = new MetricsApi(Provider);
191-
Register = new RegisterApi(Provider);
192-
Store = new StoreApi(storeWrapper);
193-
194186
server.Register(WireFormat.ASCII, Provider);
195187
}
196188

libs/host/StoreWrapperFactory.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class StoreWrapperFactory
2929

3030
readonly ILoggerFactory loggerFactory;
3131
readonly ILogger<StoreWrapperFactory> logger;
32+
readonly IGarnetServer garnetServer;
3233
readonly StoreFactory storeFactory;
3334
readonly GarnetServerOptions options;
3435
readonly CustomCommandManager customCommandManager;
@@ -40,6 +41,7 @@ public class StoreWrapperFactory
4041
public StoreWrapperFactory(
4142
ILoggerFactory loggerFactory,
4243
ILogger<StoreWrapperFactory> logger,
44+
IGarnetServer garnetServer,
4345
StoreFactory storeFactory,
4446
IOptions<GarnetServerOptions> options,
4547
CustomCommandManager customCommandManager,
@@ -50,6 +52,7 @@ public StoreWrapperFactory(
5052
{
5153
this.loggerFactory = loggerFactory;
5254
this.logger = logger;
55+
this.garnetServer = garnetServer;
5356
this.storeFactory = storeFactory;
5457
this.options = options.Value;
5558
this.customCommandManager = customCommandManager;
@@ -59,9 +62,7 @@ public StoreWrapperFactory(
5962
this.appendOnlyFileWrapper = appendOnlyFileWrapper;
6063
}
6164

62-
public StoreWrapper Create(
63-
string version,
64-
IGarnetServer server)
65+
public StoreWrapper Create(string version)
6566
{
6667
var store = mainStoreWrapper.store;
6768
var objectStore = objectStoreWrapper.objectStore;
@@ -88,7 +89,7 @@ public StoreWrapper Create(
8889
return new StoreWrapper(
8990
version,
9091
redisProtocolVersion,
91-
server,
92+
garnetServer,
9293
store,
9394
objectStore,
9495
objectStoreSizeTracker,

libs/server/StoreWrapper.cs

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

0 commit comments

Comments
 (0)