Skip to content

Commit 0024f3d

Browse files
committed
Exchanged redis client library, fixed DNS issue when connecting (will be solved in .NET core 2.0), increasing processors instances, improvement in docs and script variable naming
1 parent 4c6cd26 commit 0024f3d

File tree

12 files changed

+77
-40
lines changed

12 files changed

+77
-40
lines changed

Create-Infrastructure.ps1

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Param(
1010
$ResourceGroupLocation = "North Europe",
1111

1212
[switch]
13-
$SkipCluster
13+
$SkipClusterInCloud
1414
)
1515

1616
# stop the script on first error
@@ -46,7 +46,7 @@ $registryTemplateParameters["EnvironmentTag"] = $EnvironmentTag
4646

4747
DeployTemplate -ResourceGroupName $resourceGroupName -TemplateFileFullPath $registryTemplateFile -TemplateParameters $registryTemplateParameters
4848

49-
if (-not $SkipCluster) {
49+
if (-not $SkipClusterInCloud) {
5050
$automationKeyVaultName = "ca-automation-$EnvironmentTag"
5151
$automationKeyVault = Get-AzureRmKeyVault -VaultName $automationKeyVaultName -ErrorAction SilentlyContinue
5252

Deploy-Application.ps1

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ $registryUrl = ("cadevcache" + $EnvironmentTag + "registry.azurecr.io")
1919

2020
$keyVaultName = "ca-devcache-$EnvironmentTag"
2121

22+
$storageKeyName = ("cadevcache" + $EnvironmentTag + "storage")
23+
2224
$recieveConnectionString = (Get-AzureKeyVaultSecret -VaultName $keyVaultName -SecretName eventHubReceiveConnectionString).SecretValueText
2325
$sendConnectionString = (Get-AzureKeyVaultSecret -VaultName $keyVaultName -SecretName eventHubSendConnectionString).SecretValueText
24-
$storageAccountKey = (Get-AzureKeyVaultSecret -VaultName $keyVaultName -SecretName cadevcachegreenstorage).SecretValueText
26+
$storageAccountKey = (Get-AzureKeyVaultSecret -VaultName $keyVaultName -SecretName $storageKeyName).SecretValueText
2527
$storageAccountName = ("cadevcache" + $EnvironmentTag + "storage")
2628
$eventHubReaderPath = "ca-devcache-$EnvironmentTag-hub"
2729

Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
11
using System;
22
using System.Collections.Generic;
33
using Microsoft.AspNetCore.Mvc;
4-
using ServiceStack.Redis;
4+
using StackExchange.Redis;
55

66
namespace DeviceCache.Frontend.Controllers
77
{
88
[Route("api/[controller]")]
99
public class DataController : Controller
1010
{
11-
private readonly RedisManagerPool _manager;
11+
private readonly ConnectionMultiplexer _redis;
1212

13-
public DataController(RedisManagerPool manager)
13+
public DataController(ConnectionMultiplexer redis)
1414
{
15-
_manager = manager;
15+
_redis = redis;
1616
}
1717

1818
[HttpGet]
1919
public IEnumerable<string> Get()
2020
{
2121
var random = new Random();
22-
var keys = new List<string>();
22+
var keys = new List<RedisKey>();
2323

2424
for (int i = 0; i < 40; i++)
2525
{
2626
var randomKey = random.Next(0, 100000);
2727
keys.Add(randomKey.ToString());
2828
}
2929

30-
using (var client = _manager.GetClient())
31-
{
32-
var values = client.GetValues(keys);
33-
return values;
34-
}
30+
IDatabase db = _redis.GetDatabase();
31+
32+
var values = db.StringGet(keys.ToArray());
33+
return values.ToStringArray();
3534
}
3635
}
3736
}

DeviceCache.Frontend/DeviceCache.Frontend.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
1111
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
1212
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
13-
<PackageReference Include="ServiceStack.Redis.Core" Version="1.0.43" />
13+
<PackageReference Include="StackExchange.Redis" Version="1.2.6" />
1414
</ItemGroup>
1515
<ItemGroup>
1616
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0" />

DeviceCache.Frontend/Startup.cs

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
using Microsoft.AspNetCore.Builder;
1+
using System.Linq;
2+
using System.Net;
3+
using Microsoft.AspNetCore.Builder;
24
using Microsoft.AspNetCore.Hosting;
35
using Microsoft.Extensions.Configuration;
46
using Microsoft.Extensions.DependencyInjection;
57
using Microsoft.Extensions.Logging;
6-
using ServiceStack.Redis;
8+
using StackExchange.Redis;
79

810
namespace DeviceCache.Frontend
911
{
@@ -27,8 +29,17 @@ public void ConfigureServices(IServiceCollection services)
2729
// Add framework services.
2830
services.AddMvc();
2931

30-
var manager = new RedisManagerPool("devicecache-cache:6379");
31-
services.AddSingleton(manager);
32+
ConfigurationOptions config = ConfigurationOptions.Parse("devicecache-cache:6379");
33+
34+
DnsEndPoint addressEndpoint = config.EndPoints.First() as DnsEndPoint;
35+
int port = addressEndpoint.Port;
36+
37+
IPHostEntry ip = Dns.GetHostEntryAsync(addressEndpoint.Host).Result;
38+
config.EndPoints.Remove(addressEndpoint);
39+
config.EndPoints.Add(ip.AddressList.First(), port);
40+
41+
var redis = ConnectionMultiplexer.Connect(config);
42+
services.AddSingleton(redis);
3243
}
3344

3445
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

DeviceCache.Infrastructure/EventHub.json

+7-6
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"sku": {
3535
"name": "Standard",
3636
"tier": "Standard",
37-
"capacity": 20
37+
"capacity": 10
3838
},
3939
"resources": [
4040
{
@@ -55,7 +55,7 @@
5555
"name": "devcacheConsumers",
5656
"type": "ConsumerGroups",
5757
"dependsOn": [
58-
"[variables('eventHubName')]"
58+
"[concat('Microsoft.EventHub/namespaces/', variables('namespaceName'),'/EventHubs/',variables('eventHubName'))]"
5959
]
6060
},
6161
{
@@ -69,7 +69,7 @@
6969
},
7070
"resources": [],
7171
"dependsOn": [
72-
"[variables('eventHubName')]"
72+
"[concat('Microsoft.EventHub/namespaces/', variables('namespaceName'),'/EventHubs/',variables('eventHubName'))]"
7373
]
7474
},
7575
{
@@ -83,7 +83,8 @@
8383
},
8484
"resources": [],
8585
"dependsOn": [
86-
"[variables('eventHubName')]"
86+
"[concat('Microsoft.EventHub/namespaces/', variables('namespaceName'),'/EventHubs/',variables('eventHubName'))]",
87+
"[concat('Microsoft.EventHub/namespaces/', variables('namespaceName'),'/EventHubs/',variables('eventHubName'), '/authorizationRules/', variables('receiveRuleName'))]"
8788
]
8889
}
8990
]
@@ -123,7 +124,7 @@
123124
"value": "[listkeys(variables('sendAuthRuleResourceId'), variables('eventHubApiVersion')).primaryConnectionString]"
124125
},
125126
"dependsOn": [
126-
"[concat('Microsoft.EventHub/namespaces/', variables('namespaceName'))]"
127+
"[concat('Microsoft.EventHub/namespaces/', variables('namespaceName'),'/EventHubs/',variables('eventHubName'), '/authorizationRules/', variables('sendRuleName'))]"
127128
]
128129
},
129130
{
@@ -135,7 +136,7 @@
135136
"value": "[listkeys(variables('receiveAuthRuleResourceId'), variables('eventHubApiVersion')).primaryConnectionString]"
136137
},
137138
"dependsOn": [
138-
"[concat('Microsoft.EventHub/namespaces/', variables('namespaceName'))]"
139+
"[concat('Microsoft.EventHub/namespaces/', variables('namespaceName'),'/EventHubs/',variables('eventHubName'), '/authorizationRules/', variables('receiveRuleName'))]"
139140
]
140141
}
141142
],

DeviceCache.Processor/DeviceCache.Processor.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<ItemGroup>
1111
<PackageReference Include="Microsoft.Azure.EventHubs.Processor" Version="1.0.1" />
1212
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.2" />
13-
<PackageReference Include="ServiceStack.Redis.Core" Version="1.0.43" />
13+
<PackageReference Include="StackExchange.Redis" Version="1.2.6" />
1414
</ItemGroup>
1515

1616
<ItemGroup>

DeviceCache.Processor/Program.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ private static async Task MainAsync(string[] args)
4848

4949
// Registers the Event Processor Host and starts receiving messages
5050
var eventProcessorOptions = EventProcessorOptions.DefaultOptions;
51-
eventProcessorOptions.MaxBatchSize = 2000;
51+
eventProcessorOptions.MaxBatchSize = 1000;
52+
eventProcessorOptions.PrefetchCount = 1000;
5253
await eventProcessorHost.RegisterEventProcessorAsync<SimpleEventProcessor>(eventProcessorOptions);
5354
await Task.Delay(Timeout.Infinite);
5455
}

DeviceCache.Processor/SimpleEventProcessor.cs

+19-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Net;
35
using System.Text;
46
using System.Threading.Tasks;
57
using Microsoft.Azure.EventHubs;
68
using Microsoft.Azure.EventHubs.Processor;
79
using Newtonsoft.Json;
8-
using ServiceStack.Redis;
10+
using StackExchange.Redis;
911

1012
namespace DeviceCache.Processor
1113
{
1214
public class SimpleEventProcessor : IEventProcessor
1315
{
1416
// very ugly code but gets the job done in proof of concept case
15-
private static readonly RedisManagerPool Pool = new RedisManagerPool("devicecache-cache:6379");
17+
private static ConnectionMultiplexer _multiplexer;
1618

1719
public Task CloseAsync(PartitionContext context, CloseReason reason)
1820
{
@@ -22,7 +24,17 @@ public Task CloseAsync(PartitionContext context, CloseReason reason)
2224

2325
public Task OpenAsync(PartitionContext context)
2426
{
25-
Console.WriteLine($"SimpleEventProcessor initialized. Partition: '{context.PartitionId}'");
27+
ConfigurationOptions config = ConfigurationOptions.Parse("devicecache-cache:6379");
28+
29+
DnsEndPoint addressEndpoint = config.EndPoints.First() as DnsEndPoint;
30+
int port = addressEndpoint.Port;
31+
32+
IPHostEntry ip = Dns.GetHostEntryAsync(addressEndpoint.Host).Result;
33+
config.EndPoints.Remove(addressEndpoint);
34+
config.EndPoints.Add(ip.AddressList.First(), port);
35+
36+
_multiplexer = ConnectionMultiplexer.Connect(config);
37+
Console.WriteLine($"SimpleEventProcessor initialized. Redis connected. Partition: '{context.PartitionId}'");
2638
return Task.CompletedTask;
2739
}
2840

@@ -34,7 +46,7 @@ public Task ProcessErrorAsync(PartitionContext context, Exception error)
3446

3547
public Task ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> messages)
3648
{
37-
Dictionary<string, string> map = new Dictionary<string, string>();
49+
Dictionary<RedisKey, RedisValue> map = new Dictionary<RedisKey, RedisValue>();
3850

3951
foreach (var eventData in messages)
4052
{
@@ -43,11 +55,9 @@ public Task ProcessEventsAsync(PartitionContext context, IEnumerable<EventData>
4355
map[message.Key] = message.Data;
4456
}
4557

46-
using (var client = Pool.GetClient())
47-
{
48-
client.SetAll(map);
49-
}
50-
58+
var client = _multiplexer.GetDatabase();
59+
client.StringSet(map.ToArray());
60+
5161
Console.WriteLine($"Processed { map.Count } at: { DateTime.Now }, Partition: '{context.PartitionId}'");
5262

5363
return context.CheckpointAsync();
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
replicas: 3
1+
replicas: 10

docs/cloud-prerequisites.md

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Prerequisites for cloud cluster deployment
22

3-
In case you will be also deploying a remote cluster with ACS (Create-Infrastructure.ps1 script without the -SkipCluster flag), you will need to setup some prerequisites for such deployment:
3+
In case you will be also deploying a remote cluster with ACS (Create-Infrastructure.ps1 script without the -SkipClusterInCloud flag), you will need to setup some prerequisites for such deployment:
44

55
- Subscription admin service principal credentials (**you need to provide this**)
66
- Public SSH key (**you need to provide this**)
@@ -21,7 +21,20 @@ You could also use some of the tools like **Bitvise** which are capable of creat
2121

2222
## Running the Create-CloudClusterPrerequisites.ps1 script
2323

24-
First, execute this line to securely enter your credentials for the service principal:
24+
First, make sure you log in to your Azure Subscription with
25+
26+
```powershell
27+
Login-AzureRmAccount
28+
```
29+
30+
and, if necessary, switch to the correct subscription using
31+
32+
```powershell
33+
# to find out the subscirption id, run Get-AzureRmSubscription
34+
Select-AzureRmSubscription
35+
```
36+
37+
Execute this line to securely enter your credentials for the service principal:
2538

2639
```powershell
2740
$servicePrincipalCredentials = Get-Credential

docs/setup.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ Select-AzureRmSubscription
5656
You can execute the `Create-Infrastructure.ps1` script now. In case if you will be working with a local cluster (e.g. minikube), run the following:
5757

5858
```powershell
59-
.\Create-Infrastructure.ps1 -EnvironmentTag <<set_tag_here>> -SkipCluster
59+
.\Create-Infrastructure.ps1 -EnvironmentTag <<set_tag_here>> -SkipClusterInCloud
6060
```
6161

62-
If you are going to be using a cloud cluster (ACS), omit the -SkipCluster flag.
62+
If you are going to be using a cloud cluster (ACS), omit the -SkipClusterInCloud flag.
6363

6464
```powershell
6565
# make sure you execute the Create-CloudClusterPrerequisites.ps1 script first!

0 commit comments

Comments
 (0)