4

I have a master-slave configuration with two slaves and three sentinels. If I try the commands with command-line everything is ok. But I have some problems configuring it for C#. I am using StackExchange.Redis library. But I don't understand what I should do in order to get/set the keys from the master. Should I manually find the master and then get or set the keys or does it do it autonomously? What is the best way to do it? Each time i want to get/set a key should I check who is the master? For now I did only this and I left my the other code as it was with only one master.

private static string ServiceName = "mymaster";
private static string IP { get { return "127.0.0.1"; } }
private static int Port = 26379;

public static readonly ConnectionMultiplexer Connection = GetConn();
public static readonly IServer Server = Connection.GetServer(IP, Port);

public static ConnectionMultiplexer GetConn()
{
    try
    {
        // create a connection
        var options = new ConfigurationOptions()
        {
            CommandMap = CommandMap.Sentinel,
            EndPoints = { { IP, Port } },
            AllowAdmin = true,
            TieBreaker = "",
            ServiceName = ServiceName,
            SyncTimeout = 5000,
            AbortOnConnectFail = true,
            Ssl = false
        };
        var connection = ConnectionMultiplexer.Connect(options, Console.Out);
        return connection;
    }
    catch (Exception ex)
    {
        ASLog.LogError(ex, 1);
        return null;
    }
}

where I have one of the three sentinels at port 26379. Sorry but I am a bit confused how to use this with C#.

The Real Bill
  • 14,884
  • 8
  • 37
  • 39
ayasha
  • 1,221
  • 5
  • 27
  • 46
  • Just four years and we can get it out of the box https://stackoverflow.com/a/64177201/1426685 – d_f Oct 02 '20 at 19:43

2 Answers2

0

You don't need to specify or check for the master node, the library will choose the master for write operations.

Anyway all the methods have an optional CommandFlags parameters where you can specify where to execute an operation (DemandMaster, DemandSlave, PreferMaster, PreferSlave).

For example:

var cnn = ConnectionMultiplexer.Connect("localhost:6379");
var db = cnn.GetDatabase();
db.StringSet("key", "value", null, When.Always, CommandFlags.DemandMaster);
var value = db.StringGet("key", CommandFlags.PreferSlave);
thepirat000
  • 12,362
  • 4
  • 46
  • 72
  • 1
    when i try to use `StringSet` it says `This operation has been disabled in the command-map and cannot be used: SETEX`.. – ayasha Jan 18 '16 at 08:15
  • Do you have any [`rename-command`](http://redis.io/topics/security#disabling-of-specific-commands) on redis.conf? – thepirat000 Jan 18 '16 at 16:20
  • 1
    Why is he doing all this then? http://stackoverflow.com/questions/25385075/redis-failover-with-stackexchange-sentinel-from-c-sharp?rq=1 – ayasha Jan 20 '16 at 15:16
  • @thepirat000 Does the CommandFlags parameter handle master failover? – kaarthick raman May 09 '17 at 06:41
0

I was able to resolve mine by changing the CommandMap from Command.Sentinel as follows:

    var commands = new Dictionary<string, string> {
                    { "info", null }, // disabled
                    { "select", "use" },
            };
    sentinelConfig.CommandMap = CommandMap.Create(commands);

The error should disappear.