Skip to content

Commit c6c9cae

Browse files
committed
fix lua cross slot, clean up REDIS_URI parameter addition
1 parent e337b88 commit c6c9cae

File tree

7 files changed

+80
-19
lines changed

7 files changed

+80
-19
lines changed

README.md

+15-5
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,12 @@ mvn clean verify
6969
```
7070
* run the consumer
7171
```bash
72+
export REDIS_URI=redis://localhost:12000
7273
./runconsumer.sh
7374
```
7475
* run the producer
7576
```bash
77+
# will produce to localhost 12000 and 12002. (a little bit odd maybe)
7678
./runproducer.sh
7779
```
7880
### Verify the results
@@ -102,17 +104,21 @@ hgetall weather_sensor:wind:message:MSG2:float
102104

103105
## run with LUA consumer
104106
(Alternatively, this can be run through intellij)
105-
* Compile the code
107+
* Compile the code. These will use the REDIS_URI environment variable from setEnvironment.sh
106108
```bash
107109
mvn clean verify
108110
```
109-
* run the consumer
111+
* run the consumer
112+
* These scripts use REDIS_URI environment variable for connectivity
113+
*NOTE:* this use of one hash key for all the keys is needed because LUA only can function on redis objects in the exact same hash slot. Using this type of technique can severely skew the data so is for demo purpose only.
110114
```bash
115+
export REDIS_URI=redis://localhost:12000
111116
./runconsumerLUA.sh
112117
```
113-
* run the producer
118+
* run the producer.
114119
```bash
115-
./runproducer.sh
120+
export REDIS_URI=redis://localhost:12000
121+
./runproducerSingle.sh
116122
```
117123
### Verify the results
118124
```bash
@@ -126,10 +132,12 @@ The individual redis sets and hashes are the same as above in the simple consume
126132
* The crdpurge.sh is very handy to efficiently do a "purgeall" of the database contents
127133
* Can run a consumer against each "region" and both will receive all the messages. The crdb logic will handle de-duplication of the redis objects.
128134
```bash
135+
export REDIS_URI=redis://localhost:12000
129136
./runconsumer.sh
130137
```
131138
```bash
132-
./runconsumer2.sh
139+
export REDIS_URI=redis://localhost:12002
140+
./runconsumer.sh
133141
```
134142
## run with open source single redis container
135143
* stop docker if running from above
@@ -145,9 +153,11 @@ docker-compose up
145153
```
146154
```bash
147155
# edit port number in script to 6379
156+
export REDIS_URI=redis://localhost:12000
148157
./runconsumerLUA.sh
149158
```
150159
```bash
160+
export REDIS_URI=redis://localhost:12000
151161
./runproducerSingle.sh
152162
```
153163
## Run using AWS Cloudformation

cfndeploy.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
CAP1=CAPABILITY_NAMED_IAM
22
CAP2=CAPABILITY_AUTO_EXPAND
33
CAP3=CAPABILITY_IAM
4-
aws cloudformation deploy --template-file ./packaged-template.out --stack-name ${PROJECT_NAME} --capabilities $CAP1 $CAP2 $CAP3 --parameter-overrides ProjectName=$PROJECT_NAME S3BucketName=${S3_BUCKET} RedisDatabasePort=${REDISPORT} ClusterName=${CLUSTERNAME} HostedZoneName=${HOSTEDZONENAME} HostedZoneId=${HOSTEDZONEID} ClusterUserName=${CLUSTERUSERNAME} ClusterPassword=${CLUSTERPASSWORD} ClusterURL=${CLUSTERURL} LocalIp=${LOCAL_IP} S3Bucket=${S3_BUCKET} KeyPair=${KEY_PAIR} EC2InstanceType=${EC2_INSTANCE_TYPE} NumberInstances=${NUMBER_INSTANCES}
4+
aws cloudformation deploy --template-file ./packaged-template.out --stack-name ${PROJECT_NAME} --capabilities $CAP1 $CAP2 $CAP3 --parameter-overrides ProjectName=$PROJECT_NAME S3BucketName=${S3_BUCKET} RedisDatabasePort=${REDISPORT} ClusterName=${CLUSTERNAME} HostedZoneName=${HOSTEDZONENAME} HostedZoneId=${HOSTEDZONEID} ClusterUserName=${CLUSTERUSERNAME} ClusterPassword=${CLUSTERPASSWORD} ClusterURL=${CLUSTERURL} LocalIp=${LOCAL_IP} S3Bucket=${S3_BUCKET} KeyPair=${KEY_PAIR} EC2InstanceType=${EC2_INSTANCE_TYPE} NumberInstances=${NUMBER_INSTANCES} VpcCIDR=${VPC_CIDR} UseRedisElasticIP=${CREATE_ELASTIC_IPS}

runconsumerLUA.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
mvn exec:java -Dexec.mainClass="com.jphaugla.redis.streams.RedisStreams101ConsumerLUA" -Dexec.args="6379"
1+
mvn exec:java -Dexec.mainClass="com.jphaugla.redis.streams.RedisStreams101ConsumerLUA"

setEnvironment.sh

+5
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,8 @@ export EC2_INSTANCE_TYPE=r4.2xlarge
3232
export KEY_PAIR=${PREFIX}_${REGION}
3333
# number of instances between 1 and 3
3434
export NUMBER_INSTANCES=2
35+
# VPC Cidr
36+
export VPC_CIDR=10.1.0.0/16
37+
# Create Elastic IPs for fixed IPS on redis nodes
38+
export CREATE_ELASTIC_IPS=false
39+
export REDIS_URI=redis://redis-${REDISPORT}.${REGION}.${PREFIX}.demo-rlec.redislabs.com:${REDISPORT}

src/main/java/com/jphaugla/redis/streams/RedisStreams101ConsumerLUA.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class RedisStreams101ConsumerLUA {
1818
public final static String STREAMS_KEY = "weather_sensor:wind";
1919
public final static String HASH_INSIDE = ":hash:";
2020
// to avoid cross slot error must use brackets so same hash slot
21-
public final static String MESSAGE_PREFIX = "{wind:message:";
21+
public final static String MESSAGE_PREFIX = "{weather_sensor:wind}:message";
2222
public final static String APPLICATION_NAME = "application_1";
2323

2424
public static void main(String[] args) throws JsonProcessingException {
@@ -62,7 +62,7 @@ public static void main(String[] args) throws JsonProcessingException {
6262
// syncCommands.xack(STREAMS_KEY, APPLICATION_NAME, message.getId());
6363
Map<String, String> body = message.getBody();
6464
String messageId = body.get("message_id");
65-
String messageKey = MESSAGE_PREFIX + messageId + "}";
65+
String messageKey = MESSAGE_PREFIX + messageId;
6666
System.out.println(messageKey);
6767
String hashKey = messageKey + HASH_INSIDE + message.getId();
6868
// System.out.println(hashKey);

templates/MasterWithRedis.yaml

+30-8
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,22 @@ Parameters:
146146
- i3.8xlarge
147147
- i3.16xlarge
148148
ConstraintDescription: Must be a valid EC2 instance type.
149+
VpcCIDR:
150+
Description: Enter the VPC CIDR range in the form x.x.x.x/16.
151+
Type: String
152+
MinLength: 9
153+
MaxLength: 18
154+
AllowedPattern: "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})"
155+
ConstraintDescription: Must be a valid CIDR range in the form x.x.x.x/16.
156+
Default: 10.20.0.0/16
157+
UseRedisElasticIP:
158+
Description: Flag to fixed IPs for the redis nodes
159+
Type: String
160+
Default: false
161+
AllowedValues:
162+
- true
163+
- false
164+
149165

150166
Conditions:
151167
Create-One: !Equals [ !Ref NumberInstances, 1 ]
@@ -154,6 +170,7 @@ Conditions:
154170
Do-Two: !Or
155171
- !Condition Create-Two
156172
- !Condition Create-Three
173+
Create-ElasticIps: !Equals [ !Ref UseRedisElasticIP, true ]
157174

158175
Resources:
159176
VPCStack:
@@ -164,15 +181,19 @@ Resources:
164181
Parameters:
165182
LocalIp:
166183
Ref: LocalIp
184+
VpcCIDR:
185+
Ref: VpcCIDR
167186
Metadata:
168187
'AWS::CloudFormation::Designer':
169188
id: 7aa57bac-380e-4523-a72b-ef898d0a0367
170189

171190
IPAddressVM1:
172191
Type: AWS::EC2::EIP
192+
Condition: Create-ElasticIps
173193

174194
IPAssocVM1:
175195
Type: AWS::EC2::EIPAssociation
196+
Condition: Create-ElasticIps
176197
Properties:
177198
InstanceId: !Ref RedisEC2VM1
178199
EIP: !Ref IPAddressVM1
@@ -181,7 +202,6 @@ Resources:
181202
Type: AWS::EC2::Instance
182203
DependsOn:
183204
- VPCStack
184-
- IPAddressVM1
185205
Properties:
186206
ImageId:
187207
Fn::FindInMap:
@@ -212,10 +232,12 @@ Resources:
212232

213233
IPAddressVM2:
214234
Condition: Do-Two
235+
Condition: Create-ElasticIps
215236
Type: AWS::EC2::EIP
216237

217238
IPAssocVM2:
218239
Condition: Do-Two
240+
Condition: Create-ElasticIps
219241
Type: AWS::EC2::EIPAssociation
220242
Properties:
221243
InstanceId: !Ref RedisEC2VM2
@@ -227,7 +249,6 @@ Resources:
227249
DependsOn:
228250
- VPCStack
229251
- RedisEC2VM1
230-
- IPAddressVM2
231252
Properties:
232253
ImageId:
233254
Fn::FindInMap:
@@ -260,10 +281,12 @@ Resources:
260281

261282
IPAddressVM3:
262283
Condition: Create-Three
284+
Condition: Create-ElasticIps
263285
Type: AWS::EC2::EIP
264286

265287
IPAssocVM3:
266288
Condition: Create-Three
289+
Condition: Create-ElasticIps
267290
Type: AWS::EC2::EIPAssociation
268291
Properties:
269292
InstanceId: !Ref RedisEC2VM3
@@ -275,7 +298,6 @@ Resources:
275298
DependsOn:
276299
- VPCStack
277300
- RedisEC2VM1
278-
- IPAddressVM3
279301
Properties:
280302
ImageId:
281303
Fn::FindInMap:
@@ -348,29 +370,29 @@ Resources:
348370
RedisARecord1:
349371
Type: AWS::Route53::RecordSet
350372
DependsOn:
351-
- IPAddressVM1
373+
- RedisEC2VM1
352374
Properties:
353375
HostedZoneId: !Ref HostedZoneId
354376
Comment: DNS name for my instance
355377
Name: !Join ['', ['node1.', !Ref ClusterName, ., !Ref HostedZoneName ] ]
356378
Type: A
357379
TTL: 900
358380
ResourceRecords:
359-
- !Ref IPAddressVM1
381+
- !GetAtt RedisEC2VM1.PublicIp
360382

361383
RedisARecord2:
362384
Condition: Do-Two
363385
Type: AWS::Route53::RecordSet
364386
DependsOn:
365-
- IPAddressVM2
387+
- RedisEC2VM2
366388
Properties:
367389
HostedZoneId: !Ref HostedZoneId
368390
Comment: DNS name for my instance
369391
Name: !Join ['', ['node2.', !Ref ClusterName, ., !Ref HostedZoneName ] ]
370392
Type: A
371393
TTL: 900
372394
ResourceRecords:
373-
- !Ref IPAddressVM2
395+
- !GetAtt RedisEC2VM1.PublicIp
374396

375397
RedisARecord3:
376398
Condition: Create-Three
@@ -384,7 +406,7 @@ Resources:
384406
Type: A
385407
TTL: 900
386408
ResourceRecords:
387-
- !Ref IPAddressVM3
409+
- !GetAtt RedisEC2VM1.PublicIp
388410

389411
RedisNSRecord:
390412
Type: AWS::Route53::RecordSet

templates/VPCStack.yaml

+26-2
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,32 @@ Parameters:
55
LocalIp:
66
Description: Local IP address to limit workgroup opening
77
Type: String
8+
VpcCIDR:
9+
Description: Enter the VPC CIDR range in the form x.x.x.x/16.
10+
Type: String
11+
MinLength: 9
12+
MaxLength: 18
13+
AllowedPattern: "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})"
14+
ConstraintDescription: Must be a valid CIDR range in the form x.x.x.x/16.
15+
Default: 10.20.0.0/16
816

917
Resources:
1018
VPC:
1119
Type: 'AWS::EC2::VPC'
1220
Properties:
13-
CidrBlock: '10.0.0.0/16'
21+
CidrBlock:
22+
Ref: VpcCIDR
1423
EnableDnsSupport: true
1524
EnableDnsHostnames: true
1625
InstanceTenancy: default
26+
Tags:
27+
- Key: Name
28+
Value:
29+
Fn::Join:
30+
- "-"
31+
- - Ref: AWS::StackName
32+
- Vpc
33+
1734

1835
InternetGateway:
1936
Type: 'AWS::EC2::InternetGateway'
@@ -28,9 +45,16 @@ Resources:
2845
Type: 'AWS::EC2::Subnet'
2946
Properties:
3047
AvailabilityZone: !Select [0, !GetAZs '']
31-
CidrBlock: '10.0.0.1/20'
48+
CidrBlock: !Select [ 0, !Cidr [ !GetAtt VPC.CidrBlock, 3, 8 ]]
3249
MapPublicIpOnLaunch: true
3350
VpcId: !Ref VPC
51+
Tags:
52+
- Key: Name
53+
Value:
54+
Fn::Join:
55+
- "-"
56+
- - Ref: AWS::StackName
57+
- PublicSubnet1
3458

3559
RouteTablePublicSubnet1:
3660
Type: 'AWS::EC2::RouteTable'

0 commit comments

Comments
 (0)