NeatNerd
NeatNerd

Reputation: 2373

Reading Live nodes in Zookeeper Solr

I am writting a frontend to Solr in ASP.net MVC 4, I want to connect to Zookeeper and read alive nodes and connect to a random one. For this purpose I wrote this code:

public class zkResolver
{
    private static readonly TimeSpan timeout = TimeSpan.FromSeconds(400);
    private const String LIVE_NODES_ZKNODE = "/live_nodes";
    public String ErrorMsg { get; set; }
    public static String getActiveInstance(String instanceUrl)
    {
        ZooKeeper zk = new ZooKeeper(instanceUrl, timeout, null);
        List<String> activeNodes = zk.GetChildren(LIVE_NODES_ZKNODE, false);
        Random rnd = new Random();
        int i = rnd.Next(0, activeNodes.Count - 1); //lets shuffle baby
        return activeNodes[i];
    }
}

However when I run it it returns -4 (CONNECTIONLOSS), but when I start debugging, it returns the correct result, any idea, why???

Upvotes: 3

Views: 1343

Answers (1)

NeatNerd
NeatNerd

Reputation: 2373

Ok, I have solved the problem. The ZooKeeper has async connection in ctor, so you have to wait till it is connected with smth like this:

        while (zk.State == ZooKeeperNet.ZooKeeper.States.CONNECTING)
        {
            Thread.Sleep(TimeSpan.FromSeconds(1));
        }

Upvotes: 2

Related Questions