Commit d6681264 authored by Nick Craver's avatar Nick Craver

Cleanup: RedisServer

parent a6ef7169
...@@ -9,15 +9,13 @@ ...@@ -9,15 +9,13 @@
namespace StackExchange.Redis namespace StackExchange.Redis
{ {
internal sealed partial class RedisServer : RedisBase, IServer internal sealed partial class RedisServer : RedisBase, IServer
{ {
private readonly ServerEndPoint server; private readonly ServerEndPoint server;
internal RedisServer(ConnectionMultiplexer multiplexer, ServerEndPoint server, object asyncState) : base(multiplexer, asyncState) internal RedisServer(ConnectionMultiplexer multiplexer, ServerEndPoint server, object asyncState) : base(multiplexer, asyncState)
{ {
if (server == null) throw new ArgumentNullException(nameof(server)); this.server = server ?? throw new ArgumentNullException(nameof(server));
this.server = server;
} }
public ClusterConfiguration ClusterConfiguration => server.ClusterConfiguration; public ClusterConfiguration ClusterConfiguration => server.ClusterConfiguration;
...@@ -32,8 +30,8 @@ internal RedisServer(ConnectionMultiplexer multiplexer, ServerEndPoint server, o ...@@ -32,8 +30,8 @@ internal RedisServer(ConnectionMultiplexer multiplexer, ServerEndPoint server, o
public bool AllowSlaveWrites public bool AllowSlaveWrites
{ {
get { return server.AllowSlaveWrites; } get => server.AllowSlaveWrites;
set { server.AllowSlaveWrites = value; } set => server.AllowSlaveWrites = value;
} }
public ServerType ServerType => server.ServerType; public ServerType ServerType => server.ServerType;
...@@ -63,9 +61,10 @@ public Task<long> ClientKillAsync(long? id = null, ClientType? clientType = null ...@@ -63,9 +61,10 @@ public Task<long> ClientKillAsync(long? id = null, ClientType? clientType = null
var msg = GetClientKillMessage(endpoint, id, clientType, skipMe, flags); var msg = GetClientKillMessage(endpoint, id, clientType, skipMe, flags);
return ExecuteAsync(msg, ResultProcessor.Int64); return ExecuteAsync(msg, ResultProcessor.Int64);
} }
Message GetClientKillMessage(EndPoint endpoint, long? id, ClientType? clientType, bool skipMe, CommandFlags flags)
private Message GetClientKillMessage(EndPoint endpoint, long? id, ClientType? clientType, bool skipMe, CommandFlags flags)
{ {
List<RedisValue> parts = new List<RedisValue>(9) var parts = new List<RedisValue>(9)
{ {
RedisLiterals.KILL RedisLiterals.KILL
}; };
...@@ -194,6 +193,7 @@ public Task ConfigSetAsync(RedisValue setting, RedisValue value, CommandFlags fl ...@@ -194,6 +193,7 @@ public Task ConfigSetAsync(RedisValue setting, RedisValue value, CommandFlags fl
ExecuteSync(Message.Create(-1, flags | CommandFlags.FireAndForget, RedisCommand.CONFIG, RedisLiterals.GET, setting), ResultProcessor.AutoConfigure); ExecuteSync(Message.Create(-1, flags | CommandFlags.FireAndForget, RedisCommand.CONFIG, RedisLiterals.GET, setting), ResultProcessor.AutoConfigure);
return task; return task;
} }
public long DatabaseSize(int database = 0, CommandFlags flags = CommandFlags.None) public long DatabaseSize(int database = 0, CommandFlags flags = CommandFlags.None)
{ {
var msg = Message.Create(database, flags, RedisCommand.DBSIZE); var msg = Message.Create(database, flags, RedisCommand.DBSIZE);
...@@ -211,6 +211,7 @@ public RedisValue Echo(RedisValue message, CommandFlags flags) ...@@ -211,6 +211,7 @@ public RedisValue Echo(RedisValue message, CommandFlags flags)
var msg = Message.Create(-1, flags, RedisCommand.ECHO, message); var msg = Message.Create(-1, flags, RedisCommand.ECHO, message);
return ExecuteSync(msg, ResultProcessor.RedisValue); return ExecuteSync(msg, ResultProcessor.RedisValue);
} }
public Task<RedisValue> EchoAsync(RedisValue message, CommandFlags flags) public Task<RedisValue> EchoAsync(RedisValue message, CommandFlags flags)
{ {
var msg = Message.Create(-1, flags, RedisCommand.ECHO, message); var msg = Message.Create(-1, flags, RedisCommand.ECHO, message);
...@@ -222,6 +223,7 @@ public void FlushAllDatabases(CommandFlags flags = CommandFlags.None) ...@@ -222,6 +223,7 @@ public void FlushAllDatabases(CommandFlags flags = CommandFlags.None)
var msg = Message.Create(-1, flags, RedisCommand.FLUSHALL); var msg = Message.Create(-1, flags, RedisCommand.FLUSHALL);
ExecuteSync(msg, ResultProcessor.DemandOK); ExecuteSync(msg, ResultProcessor.DemandOK);
} }
public Task FlushAllDatabasesAsync(CommandFlags flags = CommandFlags.None) public Task FlushAllDatabasesAsync(CommandFlags flags = CommandFlags.None)
{ {
var msg = Message.Create(-1, flags, RedisCommand.FLUSHALL); var msg = Message.Create(-1, flags, RedisCommand.FLUSHALL);
...@@ -240,10 +242,7 @@ public Task FlushDatabaseAsync(int database = 0, CommandFlags flags = CommandFla ...@@ -240,10 +242,7 @@ public Task FlushDatabaseAsync(int database = 0, CommandFlags flags = CommandFla
return ExecuteAsync(msg, ResultProcessor.DemandOK); return ExecuteAsync(msg, ResultProcessor.DemandOK);
} }
public ServerCounters GetCounters() public ServerCounters GetCounters() => server.GetCounters();
{
return server.GetCounters();
}
public IGrouping<string, KeyValuePair<string, string>>[] Info(RedisValue section = default(RedisValue), CommandFlags flags = CommandFlags.None) public IGrouping<string, KeyValuePair<string, string>>[] Info(RedisValue section = default(RedisValue), CommandFlags flags = CommandFlags.None)
{ {
...@@ -319,6 +318,7 @@ public void MakeMaster(ReplicationChangeOptions options, TextWriter log = null) ...@@ -319,6 +318,7 @@ public void MakeMaster(ReplicationChangeOptions options, TextWriter log = null)
{ {
multiplexer.MakeMaster(server, options, log); multiplexer.MakeMaster(server, options, log);
} }
public void Save(SaveType type, CommandFlags flags = CommandFlags.None) public void Save(SaveType type, CommandFlags flags = CommandFlags.None)
{ {
var msg = GetSaveMessage(type, flags); var msg = GetSaveMessage(type, flags);
...@@ -527,9 +527,7 @@ internal static Message CreateSlaveOfMessage(EndPoint endpoint, CommandFlags fla ...@@ -527,9 +527,7 @@ internal static Message CreateSlaveOfMessage(EndPoint endpoint, CommandFlags fla
} }
else else
{ {
string hostRaw; if (Format.TryGetHostPort(endpoint, out string hostRaw, out int portRaw))
int portRaw;
if (Format.TryGetHostPort(endpoint, out hostRaw, out portRaw))
{ {
host = hostRaw; host = hostRaw;
port = portRaw; port = portRaw;
...@@ -588,11 +586,10 @@ public void SlaveOf(EndPoint master, CommandFlags flags = CommandFlags.None) ...@@ -588,11 +586,10 @@ public void SlaveOf(EndPoint master, CommandFlags flags = CommandFlags.None)
var configuration = this.multiplexer.RawConfig; var configuration = this.multiplexer.RawConfig;
// attempt to cease having an opinion on the master; will resume that when replication completes // attempt to cease having an opinion on the master; will resume that when replication completes
// (note that this may fail; we aren't depending on it) // (note that this may fail; we aren't depending on it)
if (!string.IsNullOrWhiteSpace(configuration.TieBreaker) if (!string.IsNullOrWhiteSpace(configuration.TieBreaker)
&& this.multiplexer.CommandMap.IsAvailable(RedisCommand.DEL)) && multiplexer.CommandMap.IsAvailable(RedisCommand.DEL))
{ {
var del = Message.Create(0, CommandFlags.FireAndForget | CommandFlags.NoRedirect, RedisCommand.DEL, (RedisKey)configuration.TieBreaker); var del = Message.Create(0, CommandFlags.FireAndForget | CommandFlags.NoRedirect, RedisCommand.DEL, (RedisKey)configuration.TieBreaker);
del.SetInternalCall(); del.SetInternalCall();
...@@ -601,8 +598,8 @@ public void SlaveOf(EndPoint master, CommandFlags flags = CommandFlags.None) ...@@ -601,8 +598,8 @@ public void SlaveOf(EndPoint master, CommandFlags flags = CommandFlags.None)
ExecuteSync(slaveofMsg, ResultProcessor.DemandOK); ExecuteSync(slaveofMsg, ResultProcessor.DemandOK);
// attempt to broadcast a reconfigure message to anybody listening to this server // attempt to broadcast a reconfigure message to anybody listening to this server
var channel = this.multiplexer.ConfigurationChangedChannel; var channel = multiplexer.ConfigurationChangedChannel;
if (channel != null && this.multiplexer.CommandMap.IsAvailable(RedisCommand.PUBLISH)) if (channel != null && multiplexer.CommandMap.IsAvailable(RedisCommand.PUBLISH))
{ {
var pub = Message.Create(-1, CommandFlags.FireAndForget | CommandFlags.NoRedirect, RedisCommand.PUBLISH, (RedisValue)channel, RedisLiterals.Wildcard); var pub = Message.Create(-1, CommandFlags.FireAndForget | CommandFlags.NoRedirect, RedisCommand.PUBLISH, (RedisValue)channel, RedisLiterals.Wildcard);
pub.SetInternalCall(); pub.SetInternalCall();
...@@ -636,7 +633,7 @@ private void FixFlags(Message message, ServerEndPoint server) ...@@ -636,7 +633,7 @@ private void FixFlags(Message message, ServerEndPoint server)
} }
} }
Message GetSaveMessage(SaveType type, CommandFlags flags = CommandFlags.None) private Message GetSaveMessage(SaveType type, CommandFlags flags = CommandFlags.None)
{ {
switch(type) switch(type)
{ {
...@@ -649,7 +646,7 @@ Message GetSaveMessage(SaveType type, CommandFlags flags = CommandFlags.None) ...@@ -649,7 +646,7 @@ Message GetSaveMessage(SaveType type, CommandFlags flags = CommandFlags.None)
} }
} }
ResultProcessor<bool> GetSaveResultProcessor(SaveType type) private ResultProcessor<bool> GetSaveResultProcessor(SaveType type)
{ {
switch (type) switch (type)
{ {
...@@ -662,15 +659,15 @@ ResultProcessor<bool> GetSaveResultProcessor(SaveType type) ...@@ -662,15 +659,15 @@ ResultProcessor<bool> GetSaveResultProcessor(SaveType type)
} }
} }
static class ScriptHash private static class ScriptHash
{ {
static readonly byte[] hex = { private static readonly byte[] hex = {
(byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7', (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7',
(byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f' }; (byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f' };
public static RedisValue Encode(byte[] value) public static RedisValue Encode(byte[] value)
{ {
if (value == null) return default(RedisValue); if (value == null) return default(RedisValue);
byte[] result = new byte[value.Length * 2]; var result = new byte[value.Length * 2];
int offset = 0; int offset = 0;
for (int i = 0; i < value.Length; i++) for (int i = 0; i < value.Length; i++)
{ {
...@@ -680,6 +677,7 @@ public static RedisValue Encode(byte[] value) ...@@ -680,6 +677,7 @@ public static RedisValue Encode(byte[] value)
} }
return result; return result;
} }
public static RedisValue Hash(string value) public static RedisValue Hash(string value)
{ {
if (value == null) return default(RedisValue); if (value == null) return default(RedisValue);
...@@ -691,7 +689,7 @@ public static RedisValue Hash(string value) ...@@ -691,7 +689,7 @@ public static RedisValue Hash(string value)
} }
} }
sealed class KeysScanEnumerable : CursorEnumerable<RedisKey> private sealed class KeysScanEnumerable : CursorEnumerable<RedisKey>
{ {
private readonly RedisValue pattern; private readonly RedisValue pattern;
...@@ -726,6 +724,7 @@ protected override Message CreateMessage(long cursor) ...@@ -726,6 +724,7 @@ protected override Message CreateMessage(long cursor)
} }
} }
} }
protected override ResultProcessor<ScanResult> Processor => processor; protected override ResultProcessor<ScanResult> Processor => processor;
public static readonly ResultProcessor<ScanResult> processor = new KeysResultProcessor(); public static readonly ResultProcessor<ScanResult> processor = new KeysResultProcessor();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment