Esempio n. 1
0
        async Task <List <Siblings> > ReplicateGetAsync(string key)
        {
            IList <string> replicaNodes = _ring.PreferenceList(key, N);

            replicaNodes.Remove(_name);

            var replicaValues = new List <Siblings>();

            List <Task <Siblings> > pendingGets = replicaNodes.Select(r => _remote.GetReplicaAsync(r, key)).ToList();
            await Task.WhenAll(pendingGets);

            foreach (var pendingGet in pendingGets)
            {
                try
                {
                    Siblings siblings = await pendingGet;
                    replicaValues.Add(siblings);
                }
                catch (Exception e)
                {
                    Log.Error("Error replicating get -- ignored", e);
                }
            }

            return(replicaValues);
        }
Esempio n. 2
0
 public void Add(Siblings siblings)
 {
     foreach (VersionedObject sibling in siblings)
     {
         Add(sibling);
     }
 }
Esempio n. 3
0
        public async Task PutReplicaAsync(string node, string key, Siblings siblings)
        {
            var requestUri = GetReplicaRequestUri(node, key);

            using (var client = new HttpClient())
            {
                var req = new HttpRequestMessage(HttpMethod.Put, requestUri);
                req.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(JsonMediaType));

                // TODO: Serialize JSON to stream, not string
                //MemoryStream stream = new MemoryStream();
                //StreamReader streamWriter = new StreamReader(stream);
                //using (JsonWriter jsonWriter = new JsonTextWriter(streamWriter))
                //{
                //    JsonSerializer serializer = new JsonSerializer();
                //    serializer.Serialize(jsonWriter, value);
                //}
                //req.Content = new StreamContent();

                string json = JsonConvert.SerializeObject(siblings);
                req.Content = new StringContent(json, Encoding.UTF8, JsonMediaType);

                HttpResponseMessage response = await client.SendAsync(req);

                response.EnsureSuccessStatusCode();
            }
        }
Esempio n. 4
0
        public static Siblings Merge(this DvvKernel kernel, IList <Siblings> siblingsList)
        {
            Ensure.That(siblingsList, "siblingsList").IsNotNull();

            Siblings merged = siblingsList.Aggregate(kernel.Sync);

            return(merged);
        }
Esempio n. 5
0
        public VersionVector Join(Siblings s1)
        {
            Ensure.That(s1, "s1").IsNotNull();

            var ids = s1.Ids().Select(i => new CausalEvent(i, s1.MaxDot(i)));

            return(new VersionVector(ids));
        }
Esempio n. 6
0
        async Task ReplicatePutAsync(string key, Siblings siblings)
        {
            IList <string> replicaNodes = _ring.PreferenceList(key, N);

            replicaNodes.Remove(_name);

            List <Task> pendingPuts = replicaNodes.Select(r => _remote.PutReplicaAsync(r, key, siblings)).ToList();
            await Task.WhenAll(pendingPuts);
        }
Esempio n. 7
0
        /// <summary>
        /// Remove obsolete versions.
        /// </summary>
        public Siblings Discard(Siblings s, VersionVector context)
        {
            Ensure.That(s, "s").IsNotNull();
            Ensure.That(context, "context").IsNotNull();

            // discard all siblings that are obsolete because they are included
            // in the context.
            IEnumerable <VersionedObject> concurrent = s.Where(sibling => !sibling.Clock.HappensBefore(context));

            return(new Siblings(concurrent));
        }
Esempio n. 8
0
        internal Siblings GetReplicaAsync(string key)
        {
            Ensure.That(key, "key").IsNotNullOrWhiteSpace();

            Log.DebugFormat("get-replica k={0}", key);

            BigInteger hash     = _ring.Hash(key);
            Siblings   siblings = _data[hash];

            return(siblings);
        }
Esempio n. 9
0
        public Siblings Sync(Siblings s1, Siblings s2)
        {
            Ensure.That(s1, "s1").IsNotNull();
            Ensure.That(s2, "s2").IsNotNull();

            IEnumerable <VersionedObject> r1 = s1.Where(sibling1 => !s2.Any(sibling1.HappensBefore));
            IEnumerable <VersionedObject> r2 = s2.Where(sibling2 => !s1.Any(sibling2.HappensBefore));

            IEnumerable <VersionedObject> union = r1.Union(r2).ToList();

            return(new Siblings(union));
        }
Esempio n. 10
0
        internal void PutReplicaAsync(string key, Siblings coordinatorSiblings)
        {
            Ensure.That(key, "key").IsNotNullOrWhiteSpace();

            Log.DebugFormat("put-replica k={0}", key);

            BigInteger hash     = _ring.Hash(key);
            Siblings   siblings = _data.GetValueOrDefault(hash) ?? new Siblings();

            siblings = _kernel.Sync(siblings, coordinatorSiblings);

            _data[hash] = siblings;
        }
Esempio n. 11
0
        /// <summary>
        /// Generates a new clock.
        /// </summary>
        public DottedVersionVector Event(VersionVector context, Siblings s, string i)
        {
            Ensure.That(s, "s").IsNotNull();
            Ensure.That(context, "context").IsNotNull();
            Ensure.That(i, "i").IsNotNullOrEmpty();

            long maxDot           = s.MaxDot(i);
            long maxCausalHistory = context[i];

            long maxCounter = Math.Max(maxDot, maxCausalHistory);
            var  dot        = new CausalEvent(i, maxCounter + 1);

            return(new DottedVersionVector(dot, context));
        }
Esempio n. 12
0
        internal async Task PutAsync(string key, JToken value, VersionVector context = null)
        {
            Ensure.That(key, "key").IsNotNullOrWhiteSpace();

            context = context ?? new VersionVector();

            string coordinatingNode = _ring.Node(key);

            if (_ring.PreferenceList(key, N).Contains(_name))
            {
                Log.DebugFormat("put k={0}", key);

                BigInteger hash     = _ring.Hash(key);
                Siblings   siblings = _data.GetValueOrDefault(hash);
                if (siblings != null)
                {
                    // discard obsolete versions
                    siblings = _kernel.Discard(siblings, context);
                }
                else
                {
                    siblings = new Siblings();
                }

                DottedVersionVector dvv = _kernel.Event(context, siblings, _name);
                var versionedObject     = new VersionedObject(value, dvv);

                siblings.Add(versionedObject);

                _data[hash] = siblings;

                await ReplicatePutAsync(key, siblings);
            }
            else
            {
                Log.DebugFormat("forward to coordinating node {0}", coordinatingNode);
                await _remote.PutAsync(coordinatingNode, key, value, context);
            }
        }