All Projects → kavirajk → clock

kavirajk / clock

Licence: MIT license
Logical clocks implementation in Rust

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to clock

pat-helland-and-me
Materials related to my talk "Pat Helland and Me"
Stars: ✭ 14 (-62.16%)
Mutual labels:  distributed-systems
timestampy
🕒 Bunch of utilities useful when working with UNIX timestamps
Stars: ✭ 21 (-43.24%)
Mutual labels:  clock
ray tutorial
An introductory tutorial about leveraging Ray core features for distributed patterns.
Stars: ✭ 67 (+81.08%)
Mutual labels:  distributed-systems
matrixone
Hyperconverged cloud-edge native database
Stars: ✭ 1,057 (+2756.76%)
Mutual labels:  distributed-systems
analogclock
🕰 A customizable analog clock built using React
Stars: ✭ 16 (-56.76%)
Mutual labels:  clock
jQuery-Clock-Plugin
Turns a given dom element into a jQuery Clock that can take an initial timestamp instead of client system time, supports internationalization and PHP Style Format Characters, and is relatively independent from system clock
Stars: ✭ 70 (+89.19%)
Mutual labels:  clock
huffleraft
Replicated key-value store driven by the raft consensus protocol 🚵
Stars: ✭ 32 (-13.51%)
Mutual labels:  distributed-systems
awesome-list-of-awesomes
A curated list of all the Awesome --Topic Name-- lists I've found till date relevant to Data lifecycle, ML and DL.
Stars: ✭ 259 (+600%)
Mutual labels:  distributed-systems
pylm
A framework to build components for high performance distributed applications.
Stars: ✭ 14 (-62.16%)
Mutual labels:  distributed-systems
simple-analog-clock
Simple clock view for displaying uh...time?
Stars: ✭ 24 (-35.14%)
Mutual labels:  clock
Positional
An elegant and colorful location information app for Android with Compass, Clock, Level, Sun, Moon, Trail Marker and many other features.
Stars: ✭ 72 (+94.59%)
Mutual labels:  clock
zimfarm
Farm operated by bots to grow and harvest new zim files
Stars: ✭ 58 (+56.76%)
Mutual labels:  distributed-systems
pico-solar-system
No description or website provided.
Stars: ✭ 186 (+402.7%)
Mutual labels:  clock
MIT6.824-2021
4 labs + 2 challenges + 4 docs
Stars: ✭ 594 (+1505.41%)
Mutual labels:  distributed-systems
pingPongBallClock
Raspberry Pi code for the Ping Pong Ball Clock Project
Stars: ✭ 23 (-37.84%)
Mutual labels:  clock
Pubbie
A high performance pubsub client/server implementation for .NET Core
Stars: ✭ 122 (+229.73%)
Mutual labels:  distributed-systems
teracache
Scalable, fault-tolerant, highly-available cache
Stars: ✭ 15 (-59.46%)
Mutual labels:  distributed-systems
protoactor-go
Proto Actor - Ultra fast distributed actors for Go, C# and Java/Kotlin
Stars: ✭ 4,138 (+11083.78%)
Mutual labels:  distributed-systems
vue-flip-down
vue 翻页倒计时组件 妙啊
Stars: ✭ 90 (+143.24%)
Mutual labels:  clock
nact
nact ⇒ node.js + actors ⇒ your services have never been so µ
Stars: ✭ 1,003 (+2610.81%)
Mutual labels:  distributed-systems

Logical clocks

Package version Package docs License

clocks implements some of the modern logical clocks (vector clocks and dotted version vector).

A logical clock is a mechanism for capturing chronological and causal relationships(cause and effect, Event A caused event B, also called as happened-before relation) in a distributed system.

Given any two events across multiple nodes in the distributed system, logical clocks help in answering queries like "Does event A happened-before B" or "Is event B concurrent to event A"

Implementation of dotted version vector is based on the paper Scalable and Accurate Causality Tracking for Eventually Consistent Stores

Vector clocks vs Version Vectors

Although they both have same data structure representation, they solve different problems.

Vector clocks are used to partial order between any two events in the distributed systems, where as Version Vectors are used to partial order only events that changes datum(say you want to keep multiple versions of same key that are updated concurrently).

For more details about the differences, there good article here

Usage (A simple Key Value Store, simulating multiple clients)

use logical_clock::{VersionVector, Dot};
use std::collections::HashMap;

type Key = String;

#[derive(Clone, Debug)]
struct Value{
    val:i64,
    dot:Dot
}

struct KVStore {
    store:HashMap<Key, Vec<Value>>,
    vv:VersionVector,
	
}

impl KVStore {
    fn new() -> KVStore {
	KVStore{
	    store: HashMap::new(),
	    vv: VersionVector::new(),
	}
    }

    fn get(&self, key: &str) -> (Option<Vec<Value>>, VersionVector) {
	match self.store.get(key) {
	    None => (None, self.vv.clone()),
	    Some(v) => (Some(v.clone()), self.vv.clone())
	}
    }

    fn set(mut self, client_id:&str, context: &VersionVector, key: &str, val: i64) -> Self{
	// if incoming request context descends from local clock, just overwrite.
	if context.descends(&self.vv) {
	    self.vv = self.vv.inc(client_id);
	    let dot = self.vv.get_dot(client_id);
	    let new_obj = Value{val: val, dot: dot};
	    
	    // overwrite all the siblings
	    self.store.insert(key.to_string(), vec![new_obj]);
	    return self
	}

	let mut frontier = self.vv.merge(&context);
	frontier = frontier.inc(client_id);
	let dot = frontier.get_dot(client_id);
	let new_obj = Value{val: val, dot: dot};
	self.vv = frontier;
	return self.merge_siblings(key, new_obj)
    }

    fn merge_siblings(mut self, key: &str, new_val: Value) -> Self{
	// replace values that dominated by given value's dot
	let (old, _) = self.get(key);

	match old {
	    None => {
		self.store.insert(key.to_string(), vec![new_val]);
		return self
	    },
	    Some(values) => {
		let mut updated = Vec::new();
		for v in values {
		    if new_val.dot.descends(&v.dot) {
			continue;
		    }
		    updated.push(v);
		}
		updated.push(new_val);
		self.store.insert(key.to_string(), updated);
		return self
	    }
	}
    }
}

fn main() {
    let mut kv = KVStore::new();

    // always get before put - Semantics followed in any High Available Key value store

    // Client A and Client B
    let (_, ctx_a) = kv.get("x");
    let (_, ctx_b) = kv.get("x");

    
    kv = kv.set("A", &ctx_a, "x", 10); // A try to write x=10 with empty context
    kv = kv.set("B", &ctx_b, "x", 15); // B try to write x=12 with same empty context

    // both are concurrent from the client views, so both values should be kept
    assert_eq!(2, kv.store["x"].len());

    // Client C comes in.
    let (_, ctx_c) = kv.get("x");
    // now client C knows all the causal contex, so it replaces the key with all causal past.
    kv = kv.set("C", &ctx_c, "x", 20);
    assert_eq!(1, kv.store["x"].len());
    
    // now B set with old empty context.
    kv = kv.set("B", &ctx_b, "x", 30); // I know contex is empty just set it as 30.

    // From client views latest B write is concurrent to C latest write. so both should be kept.
    assert_eq!(2, kv.store["x"].len());
    
    for (k, v) in kv.store {
	println!("key: {}, values: {:?}", k, v)
	    // val: {}, dot: {:?}", k, v, v.dot);
    }
    println!("vv: {:?}", kv.vv);

}

Logical clocks in Real time

  1. Go race detector uses vector clocks to detect data race between go routines. Basic idea is every go routine have its own vector clock and when a shared memory is accessed by multiple goroutines, their vector clocks are compared to find if they are concurrent! https://www.slideshare.net/InfoQ/looking-inside-a-race-detector

  2. Riak Key Value store uses Dotted Version Vector to track concurrent versions of same key in multiple replica. https://riak.com/posts/technical/vector-clocks-revisited-part-2-dotted-version-vectors/index.html?p=9929.html

References

  1. https://haslab.uminho.pt/tome/files/dvvset-dais.pdf
  2. https://github.com/ricardobcl/Dotted-Version-Vectors
  3. https://lamport.azurewebsites.net/pubs/time-clocks.pdf

Licence

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].