dimanche 31 mars 2019

How to handle singleton design patern in ElasticSearch Nest 6.x?

My question is really weird for me because I have to use singleton pattern with Elasticsearch for high performance but I am really open for your advise. My question is how to use singleton with elasticsearch Nest library. I created a proxy library for that. But Whenever I call singleton instance, New index creating in previous index. For example:

var log = new Log("indx1-" + DateTime.Now.ToString("yyyy.MM"), "ind1");

created in elasticsearch : indx1-2019-03

var log = new Log("indx2-" + DateTime.Now.ToString("yyyy.MM"), "ind2");

I can not see my new index : indx2-2019-03

var log = new Log("indx3-" + DateTime.Now.ToString("yyyy.MM"), "ind3");

I can not see my new index : indx3-2019-03

I think my code is correct but I need a help for corect usage for elasticsearch. I desired that when I call "CURL" I have to see that: indx1-2019-03 indx2-2019-03 indx3-2019-03

I tried to solve this problem by calling constructor again when different indexname sending from clients compared to previos indexname:

 if( _instance.indexName != indexName)
    {
        _instance = new ESClient(url, indexName, instanceName);
    }

My Singleton elasticsearch client code :

    public interface ILog
{
    ILog Put(dynamic app);
}

public class Log : ILog
{
    private string url, indexName, instanceName;
    public Log(string indexName, string instanceName)
    {
        this.url = LogSettings.Url;
        this.indexName = indexName;
        this.instanceName = instanceName;
    }
    public ILog Put(dynamic app)
    {
        ESClient.GetInstance(url, indexName, instanceName).Run(app);
        return this;
    }
}

    public class ESClient
{
    private static ESClient _instance = null;

    private static Object _mutex = new Object();
    dynamic pool, settings, lowlevelClient;
    private string indexName = string.Empty, instanceName = string.Empty;
    private ESClient(string url, string indexName, string instanceName)
    {
        pool = new SingleNodeConnectionPool(new Uri(url));
        settings = new ConnectionConfiguration(pool);
        lowlevelClient = new ElasticLowLevelClient(settings);
        this.indexName = indexName;
        this.instanceName = instanceName;
    }

    public static ESClient GetInstance(string url, string indexName, string instanceName)
    {
        if (_instance == null)
        {
            lock (_mutex)
            {
                if (_instance == null)
                {
                    _instance = new ESClient(url, indexName, instanceName);
                }
            }
        }
        if( _instance.indexName != indexName)
        {
            _instance = new ESClient(url, indexName, instanceName);
        }
        return _instance;
    }
    public void Run(dynamic app)
    {

        try
        {
            var serializeddata = PostData.Serializable(app);
            var indexResponse = lowlevelClient.Index<StringResponse>(indexName, instanceName, Guid.NewGuid().ToString(), serializeddata);
            var responseStream = indexResponse.Body; 
            Console.WriteLine(responseStream.ToString());
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            Trace.Write(ex.InnerException == null ? ex.Message : ex.InnerException.ToString());
        }


    }
}

Aucun commentaire:

Enregistrer un commentaire