Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
StackExchange.Redis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
StackExchange.Redis
Commits
8c62c8f7
Commit
8c62c8f7
authored
Apr 08, 2020
by
mgravell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add a new cluster kind for redis-cluster-proxy; in most ways it is similar to twemproxy
parent
f798868d
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
163 additions
and
25 deletions
+163
-25
CommandMap.cs
src/StackExchange.Redis/CommandMap.cs
+44
-1
ConfigurationOptions.cs
src/StackExchange.Redis/ConfigurationOptions.cs
+10
-1
ConnectionMultiplexer.cs
src/StackExchange.Redis/ConnectionMultiplexer.cs
+40
-13
Proxy.cs
src/StackExchange.Redis/Enums/Proxy.cs
+5
-1
ServerType.cs
src/StackExchange.Redis/Enums/ServerType.cs
+5
-1
ServerEndPoint.cs
src/StackExchange.Redis/ServerEndPoint.cs
+18
-8
RedisClusterProxyTests.cs
tests/StackExchange.Redis.Tests/RedisClusterProxyTests.cs
+41
-0
No files found.
src/StackExchange.Redis/CommandMap.cs
View file @
8c62c8f7
...
@@ -21,7 +21,7 @@ internal CommandMap(CommandBytes[] map)
...
@@ -21,7 +21,7 @@ internal CommandMap(CommandBytes[] map)
public
static
CommandMap
Default
{
get
;
}
=
CreateImpl
(
null
,
null
);
public
static
CommandMap
Default
{
get
;
}
=
CreateImpl
(
null
,
null
);
/// <summary>
/// <summary>
/// The commands available to <a href="
twemproxy">https://github.com/twitter/
twemproxy</a>
/// The commands available to <a href="
https://github.com/twitter/twemproxy/">
twemproxy</a>
/// </summary>
/// </summary>
/// <remarks>https://github.com/twitter/twemproxy/blob/master/notes/redis.md</remarks>
/// <remarks>https://github.com/twitter/twemproxy/blob/master/notes/redis.md</remarks>
public
static
CommandMap
Twemproxy
{
get
;
}
=
CreateImpl
(
null
,
exclusions
:
new
HashSet
<
RedisCommand
>
public
static
CommandMap
Twemproxy
{
get
;
}
=
CreateImpl
(
null
,
exclusions
:
new
HashSet
<
RedisCommand
>
...
@@ -53,6 +53,49 @@ internal CommandMap(CommandBytes[] map)
...
@@ -53,6 +53,49 @@ internal CommandMap(CommandBytes[] map)
RedisCommand
.
SHUTDOWN
,
RedisCommand
.
SLAVEOF
,
RedisCommand
.
SLOWLOG
,
RedisCommand
.
SYNC
,
RedisCommand
.
TIME
RedisCommand
.
SHUTDOWN
,
RedisCommand
.
SLAVEOF
,
RedisCommand
.
SLOWLOG
,
RedisCommand
.
SYNC
,
RedisCommand
.
TIME
});
});
/// <summary>
/// The commands available to <a href="https://github.com/RedisLabs/redis-cluster-proxy/">redis-cluster-proxy</a>
/// </summary>
/// <remarks>https://github.com/RedisLabs/redis-cluster-proxy/</remarks>
public
static
CommandMap
RedisClusterProxy
{
get
;
}
=
CreateImpl
(
null
,
exclusions
:
new
HashSet
<
RedisCommand
>
{
// via "proxy command unsupported"
// RedisCommand.ACL,
RedisCommand
.
ASKING
,
RedisCommand
.
CLIENT
,
RedisCommand
.
CLUSTER
,
RedisCommand
.
CONFIG
,
RedisCommand
.
DEBUG
,
// RedisCommand.HELLO,
RedisCommand
.
INFO
,
RedisCommand
.
LATENCY
,
RedisCommand
.
MEMORY
,
RedisCommand
.
MIGRATE
,
// RedisCommand.MODULE,
RedisCommand
.
MONITOR
,
// RedisCommand.PFDEBUG,
// RedisCommand.PFSELFTEST,
RedisCommand
.
PSUBSCRIBE
,
// RedisCommand.PSYNC,
RedisCommand
.
PUBLISH
,
RedisCommand
.
PUBSUB
,
RedisCommand
.
PUNSUBSCRIBE
,
RedisCommand
.
READONLY
,
RedisCommand
.
READWRITE
,
// RedisCommand.REPLCONF,
// RedisCommand.REPLICAOF,
// RedisCommand.ROLE,
RedisCommand
.
SCRIPT
,
RedisCommand
.
SHUTDOWN
,
RedisCommand
.
SLAVEOF
,
RedisCommand
.
SLOWLOG
,
RedisCommand
.
SUBSCRIBE
,
RedisCommand
.
SYNC
,
RedisCommand
.
TIME
,
RedisCommand
.
UNSUBSCRIBE
,
// RedisCommand.WAIT,
});
/// <summary>
/// <summary>
/// The commands available to <a href="ssdb">http://www.ideawu.com/ssdb/</a>
/// The commands available to <a href="ssdb">http://www.ideawu.com/ssdb/</a>
/// </summary>
/// </summary>
...
...
src/StackExchange.Redis/ConfigurationOptions.cs
View file @
8c62c8f7
...
@@ -251,6 +251,8 @@ public CommandMap CommandMap
...
@@ -251,6 +251,8 @@ public CommandMap CommandMap
{
{
case
Proxy
.
Twemproxy
:
case
Proxy
.
Twemproxy
:
return
CommandMap
.
Twemproxy
;
return
CommandMap
.
Twemproxy
;
case
Proxy
.
RedisClusterProxy
:
return
CommandMap
.
RedisClusterProxy
;
default
:
default
:
return
CommandMap
.
Default
;
return
CommandMap
.
Default
;
}
}
...
@@ -482,7 +484,14 @@ public ConfigurationOptions Clone()
...
@@ -482,7 +484,14 @@ public ConfigurationOptions Clone()
/// </summary>
/// </summary>
public
void
SetDefaultPorts
()
public
void
SetDefaultPorts
()
{
{
EndPoints
.
SetDefaultPorts
(
Ssl
?
6380
:
6379
);
if
(
Proxy
==
Proxy
.
RedisClusterProxy
&&
!
Ssl
)
{
EndPoints
.
SetDefaultPorts
(
7777
);
// default port for redis-cluster-proxy
}
else
{
EndPoints
.
SetDefaultPorts
(
Ssl
?
6380
:
6379
);
}
}
}
/// <summary>
/// <summary>
...
...
src/StackExchange.Redis/ConnectionMultiplexer.cs
View file @
8c62c8f7
...
@@ -1248,7 +1248,12 @@ internal long LastHeartbeatSecondsAgo
...
@@ -1248,7 +1248,12 @@ internal long LastHeartbeatSecondsAgo
/// <param name="asyncState">The async state object to pass to the created <see cref="RedisSubscriber"/>.</param>
/// <param name="asyncState">The async state object to pass to the created <see cref="RedisSubscriber"/>.</param>
public
ISubscriber
GetSubscriber
(
object
asyncState
=
null
)
public
ISubscriber
GetSubscriber
(
object
asyncState
=
null
)
{
{
if
(
RawConfig
.
Proxy
==
Proxy
.
Twemproxy
)
throw
new
NotSupportedException
(
"The pub/sub API is not available via twemproxy"
);
switch
(
RawConfig
.
Proxy
)
{
case
Proxy
.
Twemproxy
:
case
Proxy
.
RedisClusterProxy
:
throw
new
NotSupportedException
(
$"The pub/sub API is not available via
{
RawConfig
.
Proxy
}
"
);
}
return
new
RedisSubscriber
(
this
,
asyncState
);
return
new
RedisSubscriber
(
this
,
asyncState
);
}
}
...
@@ -1263,7 +1268,15 @@ public IDatabase GetDatabase(int db = -1, object asyncState = null)
...
@@ -1263,7 +1268,15 @@ public IDatabase GetDatabase(int db = -1, object asyncState = null)
db
=
RawConfig
.
DefaultDatabase
??
0
;
db
=
RawConfig
.
DefaultDatabase
??
0
;
if
(
db
<
0
)
throw
new
ArgumentOutOfRangeException
(
nameof
(
db
));
if
(
db
<
0
)
throw
new
ArgumentOutOfRangeException
(
nameof
(
db
));
if
(
db
!=
0
&&
RawConfig
.
Proxy
==
Proxy
.
Twemproxy
)
throw
new
NotSupportedException
(
"Twemproxy only supports database 0"
);
if
(
db
!=
0
)
{
switch
(
RawConfig
.
Proxy
)
{
case
Proxy
.
Twemproxy
:
case
Proxy
.
RedisClusterProxy
:
throw
new
NotSupportedException
(
$"
{
RawConfig
.
Proxy
}
only supports database 0"
);
}
}
// if there's no async-state, and the DB is suitable, we can hand out a re-used instance
// if there's no async-state, and the DB is suitable, we can hand out a re-used instance
return
(
asyncState
==
null
&&
db
<=
MaxCachedDatabaseInstance
)
return
(
asyncState
==
null
&&
db
<=
MaxCachedDatabaseInstance
)
...
@@ -1318,7 +1331,12 @@ public IDatabase GetDatabase(int db = -1, object asyncState = null)
...
@@ -1318,7 +1331,12 @@ public IDatabase GetDatabase(int db = -1, object asyncState = null)
public
IServer
GetServer
(
EndPoint
endpoint
,
object
asyncState
=
null
)
public
IServer
GetServer
(
EndPoint
endpoint
,
object
asyncState
=
null
)
{
{
if
(
endpoint
==
null
)
throw
new
ArgumentNullException
(
nameof
(
endpoint
));
if
(
endpoint
==
null
)
throw
new
ArgumentNullException
(
nameof
(
endpoint
));
if
(
RawConfig
.
Proxy
==
Proxy
.
Twemproxy
)
throw
new
NotSupportedException
(
"The server API is not available via twemproxy"
);
switch
(
RawConfig
.
Proxy
)
{
case
Proxy
.
RedisClusterProxy
:
case
Proxy
.
Twemproxy
:
throw
new
NotSupportedException
(
$"The server API is not available via
{
RawConfig
.
Proxy
}
"
);
}
var
server
=
(
ServerEndPoint
)
servers
[
endpoint
];
var
server
=
(
ServerEndPoint
)
servers
[
endpoint
];
if
(
server
==
null
)
throw
new
ArgumentException
(
"The specified endpoint is not defined"
,
nameof
(
endpoint
));
if
(
server
==
null
)
throw
new
ArgumentException
(
"The specified endpoint is not defined"
,
nameof
(
endpoint
));
return
new
RedisServer
(
this
,
server
,
asyncState
);
return
new
RedisServer
(
this
,
server
,
asyncState
);
...
@@ -1634,6 +1652,7 @@ internal async Task<bool> ReconfigureAsync(bool first, bool reconfigureAll, LogP
...
@@ -1634,6 +1652,7 @@ internal async Task<bool> ReconfigureAsync(bool first, bool reconfigureAll, LogP
{
{
case
ServerType
.
Twemproxy
:
case
ServerType
.
Twemproxy
:
case
ServerType
.
Standalone
:
case
ServerType
.
Standalone
:
case
ServerType
.
RedisClusterProxy
:
standaloneCount
++;
standaloneCount
++;
break
;
break
;
case
ServerType
.
Sentinel
:
case
ServerType
.
Sentinel
:
...
@@ -1657,6 +1676,7 @@ internal async Task<bool> ReconfigureAsync(bool first, bool reconfigureAll, LogP
...
@@ -1657,6 +1676,7 @@ internal async Task<bool> ReconfigureAsync(bool first, bool reconfigureAll, LogP
switch
(
server
.
ServerType
)
switch
(
server
.
ServerType
)
{
{
case
ServerType
.
Twemproxy
:
case
ServerType
.
Twemproxy
:
case
ServerType
.
RedisClusterProxy
:
case
ServerType
.
Sentinel
:
case
ServerType
.
Sentinel
:
case
ServerType
.
Standalone
:
case
ServerType
.
Standalone
:
case
ServerType
.
Cluster
:
case
ServerType
.
Cluster
:
...
@@ -1701,17 +1721,24 @@ internal async Task<bool> ReconfigureAsync(bool first, bool reconfigureAll, LogP
...
@@ -1701,17 +1721,24 @@ internal async Task<bool> ReconfigureAsync(bool first, bool reconfigureAll, LogP
if
(
clusterCount
==
0
)
if
(
clusterCount
==
0
)
{
{
// set the serverSelectionStrategy
// set the serverSelectionStrategy
if
(
RawConfig
.
Proxy
==
Proxy
.
Twemproxy
)
switch
(
RawConfig
.
Proxy
)
{
ServerSelectionStrategy
.
ServerType
=
ServerType
.
Twemproxy
;
}
else
if
(
standaloneCount
==
0
&&
sentinelCount
>
0
)
{
ServerSelectionStrategy
.
ServerType
=
ServerType
.
Sentinel
;
}
else
{
{
ServerSelectionStrategy
.
ServerType
=
ServerType
.
Standalone
;
case
Proxy
.
Twemproxy
:
ServerSelectionStrategy
.
ServerType
=
ServerType
.
Twemproxy
;
break
;
case
Proxy
.
RedisClusterProxy
:
ServerSelectionStrategy
.
ServerType
=
ServerType
.
RedisClusterProxy
;
break
;
default
:
if
(
standaloneCount
==
0
&&
sentinelCount
>
0
)
{
ServerSelectionStrategy
.
ServerType
=
ServerType
.
Sentinel
;
}
else
{
ServerSelectionStrategy
.
ServerType
=
ServerType
.
Standalone
;
}
break
;
}
}
var
preferred
=
await
NominatePreferredMaster
(
log
,
servers
,
useTieBreakers
,
tieBreakers
,
masters
).
ObserveErrors
().
ForAwait
();
var
preferred
=
await
NominatePreferredMaster
(
log
,
servers
,
useTieBreakers
,
tieBreakers
,
masters
).
ObserveErrors
().
ForAwait
();
foreach
(
var
master
in
masters
)
foreach
(
var
master
in
masters
)
...
...
src/StackExchange.Redis/Enums/Proxy.cs
View file @
8c62c8f7
...
@@ -12,6 +12,10 @@ public enum Proxy
...
@@ -12,6 +12,10 @@ public enum Proxy
/// <summary>
/// <summary>
/// Communication via <a href="https://github.com/twitter/twemproxy">twemproxy</a>
/// Communication via <a href="https://github.com/twitter/twemproxy">twemproxy</a>
/// </summary>
/// </summary>
Twemproxy
Twemproxy
,
/// <summary>
/// Communication via <a href="https://github.com/RedisLabs/redis-cluster-proxy">redis-cluster-proxy</a>
/// </summary>
RedisClusterProxy
,
}
}
}
}
src/StackExchange.Redis/Enums/ServerType.cs
View file @
8c62c8f7
...
@@ -20,6 +20,10 @@ public enum ServerType
...
@@ -20,6 +20,10 @@ public enum ServerType
/// <summary>
/// <summary>
/// Distributed redis installation via <a href="https://github.com/twitter/twemproxy">twemproxy</a>
/// Distributed redis installation via <a href="https://github.com/twitter/twemproxy">twemproxy</a>
/// </summary>
/// </summary>
Twemproxy
Twemproxy
,
/// <summary>
/// Distributed redis installation via <a href="https://github.com/RedisLabs/redis-cluster-proxy">redis-cluster-proxy</a>
/// </summary>
RedisClusterProxy
,
}
}
}
}
src/StackExchange.Redis/ServerEndPoint.cs
View file @
8c62c8f7
...
@@ -57,11 +57,19 @@ public ServerEndPoint(ConnectionMultiplexer multiplexer, EndPoint endpoint)
...
@@ -57,11 +57,19 @@ public ServerEndPoint(ConnectionMultiplexer multiplexer, EndPoint endpoint)
writeEverySeconds
=
config
.
KeepAlive
>
0
?
config
.
KeepAlive
:
60
;
writeEverySeconds
=
config
.
KeepAlive
>
0
?
config
.
KeepAlive
:
60
;
serverType
=
ServerType
.
Standalone
;
serverType
=
ServerType
.
Standalone
;
// overrides for twemproxy
// overrides for twemproxy
etc
if
(
multiplexer
.
RawConfig
.
Proxy
==
Proxy
.
Twemp
roxy
)
switch
(
multiplexer
.
RawConfig
.
P
roxy
)
{
{
databases
=
1
;
case
Proxy
.
Twemproxy
:
serverType
=
ServerType
.
Twemproxy
;
databases
=
1
;
serverType
=
ServerType
.
Twemproxy
;
break
;
case
Proxy
.
RedisClusterProxy
:
databases
=
1
;
serverType
=
ServerType
.
RedisClusterProxy
;
if
(
version
<
RedisFeatures
.
v3_0_0
)
// cluster is at least 3.0
version
=
RedisFeatures
.
v3_0_0
;
break
;
}
}
}
}
...
@@ -254,11 +262,13 @@ internal void AddScript(string script, byte[] hash)
...
@@ -254,11 +262,13 @@ internal void AddScript(string script, byte[] hash)
internal
void
AutoConfigure
(
PhysicalConnection
connection
)
internal
void
AutoConfigure
(
PhysicalConnection
connection
)
{
{
if
(
serverType
==
ServerType
.
Twemproxy
)
switch
(
serverType
)
{
{
// don't try to detect configuration; all the config commands are disabled, and
case
ServerType
.
Twemproxy
:
// the fallback master/slave detection won't help
case
ServerType
.
RedisClusterProxy
:
return
;
// don't try to detect configuration; all the config commands are disabled, and
// the fallback master/slave detection won't help
return
;
}
}
var
commandMap
=
Multiplexer
.
CommandMap
;
var
commandMap
=
Multiplexer
.
CommandMap
;
...
...
tests/StackExchange.Redis.Tests/RedisClusterProxyTests.cs
0 → 100644
View file @
8c62c8f7
using
System
;
using
Xunit
;
using
Xunit.Abstractions
;
namespace
StackExchange.Redis.Tests
{
public
class
RedisClusterProxyTests
:
TestBase
{
public
RedisClusterProxyTests
(
ITestOutputHelper
output
)
:
base
(
output
)
{
}
protected
override
string
GetConfiguration
()
=>
"127.0.0.1,proxy=RedisClusterProxy,version=5.0"
;
[
Fact
(
Skip
=
"No CI build for this yet"
)]
public
void
CanConnectToClusterProxy
()
{
using
(
var
conn
=
Create
())
{
var
expected
=
Guid
.
NewGuid
().
ToString
();
var
db
=
conn
.
GetDatabase
();
RedisKey
key
=
Me
();
db
.
StringSet
(
key
,
expected
);
var
actual
=
(
string
)
db
.
StringGet
(
key
);
Assert
.
Equal
(
expected
,
actual
);
// check it knows that we're dealing with a cluster
var
ex
=
Assert
.
Throws
<
NotSupportedException
>(()
=>
conn
.
GetServer
(
"abc"
));
Assert
.
Equal
(
"The server API is not available via RedisClusterProxy"
,
ex
.
Message
);
ex
=
Assert
.
Throws
<
NotSupportedException
>(()
=>
conn
.
GetSubscriber
(
"abc"
));
Assert
.
Equal
(
"The pub/sub API is not available via RedisClusterProxy"
,
ex
.
Message
);
// test a script
const
string
LUA_SCRIPT
=
"return redis.call('info')"
;
var
name
=
(
string
)
db
.
ScriptEvaluate
(
LUA_SCRIPT
);
Log
(
$"client:
{
name
}
"
);
// run it twice to check we didn't rely on script hashing (SCRIPT is disabled)
_
=
db
.
ScriptEvaluate
(
LUA_SCRIPT
);
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment