Monday, July 3, 2017

Today we continue to review the NoSQL design. These involve Key Value store, run on a large number of commodity machines with data partitioned and replicated between machines. We discussed the infrastructure which consists of physical and virtual nodes. Data is partitioned based on consistent hashing. Cachepoints are arranged along the circle depicting the key range and cache objects corresponding to the range. With multiple copies of data, we need consistency models. These include strict consistency, read your write consistency, session consistency, monotonic read consistency and eventual consistency. The choice of consistency model determines how the client request is dispatched to a replica and how the replicas propagate and apply the updates. The topology that helps with this can be classified as a single master model, a multi-master or no-master model or a Quorum based 2-PC model that provides strict consistency. The single master model is one that has each data partition managed by a single master and multiple slaves. All update requests go to the master where the update is applied and propagated to the slaves asynchronously. This scales out the reads to the slaves unless the most recent updates are required in which case they go to the master. The master is still a virtual node. When a physical node crashes, some of the masters may be lost in which case the most uptodate slave may be chosen as master. This model works very well for high read/write ratio which is the case for most data replication.Replication happens by way of state or operations transfer which is similar to the well known log shipping and mirroring options. State gets overwritten on the destinations where as operations are reexecuted on the destination.
Multimaster model comes useful for high write scenarios or when a key range has hot spots. Multi-master model allows the updates to be spread more evenly because it happens at any replica which is why it is sometimes called a no-master. With updates happening to any replicas, state synchronization becomes important. One such method is Quorum based 2-PC which uses the traditional 2PC protocol to bring all replicas to the same state at every update. There is a prepare phase when all the replicas are flushed to disk and the commit phase when each replica writes a log entry to commit the udpate.
Courtesy:nosql patterns from horicky
#codingexercise 

1. Check divisibility by 8 for any permutation of a number 

    static string CheckDivisibilityBy8(string number) 

    { 

        var divs = new List<int>(Enumerable.Range(1, 999)); 

        divs = divs.Where(x => x % 8 == 0).ToList(); 

        divs.ForEach(x => Console.Write("{0},", x)); 

        var d = new Dictionary<char, int>(); 

        for (char c = '0'; c < '9'; c++d.Add(c, 0); 
        for (int i = 0; i < number.Lengthi++) 
        { 
            if (d.ContainsKey(number[i])) 
                d[number[i]]++; 
            else  
                d.Add(number[i], 1); 
        } 
       var reqLen = number.Length < 3 ? number.Length : 3; 
        for (int i = 0; i < divs.Counti++) 
        { 
            if (divs[i].ToString().Length >= reqLen && divs[i].ToString().ToCharArray().All(x => d.ContainsKey(x) && d[x] >= divs[i].ToString().Count(t => t == x))) 
                return "YES"; 
        } 
        return "NO"; 
    } 
2. If Similarity is defined as the length of the prefix matching two strings, find the sum of the similarities found from a string when it is matched with all its suffixes.
    static int GetSimilarity(string A, string B) 
    { 
        int count = 0; 
        for (int i = 0; i < Math.Min(A.LengthB.Length); i++) 
            if (A[i] != B[i]) 
            { 
                // Console.WriteLine(A.Substring(0, i)); 
                return count; 
            } 
            else 
            { 
                count++; 
            } 
        // if (count != 0) Console.WriteLine(A); 
        return count; 
    } 
    static int GetSimilarities(string A) 
    { 
        // sum up similarity for each suffix of A beginning with A itself 
        int sum = 0; 
        for (int k = 0; k < A.Length; k++) 
        { 
            var suffix = A.Substring(k, A.Length-k); 
            var count = GetSimilarity(A, suffix); 
            if (count != 0) 
            { 
                Console.WriteLine("suffix={0},count={1}", suffix, count); 
                sum += count; 
            } 
            else  
            { 
                Console.WriteLine("suffix={0}", suffix); 
            } 
        } 
        return sum; 
    }

No comments:

Post a Comment