Skip to content

Instantly share code, notes, and snippets.

View samueleresca's full-sized avatar

Samuele Resca samueleresca

View GitHub Profile
func (qs QuorumSystem) loadOptimalStrategy(
optimize OptimizeType,
readQuorums []ExprSet,
writeQuorums []ExprSet,
readFraction DistributionValues,
loadLimit *float64,
networkLimit *float64,
latencyLimit *float64) (*Strategy, error) {
...
func (qs QuorumSystem) loadOptimalStrategy(
optimize OptimizeType,
readQuorums []ExprSet,
writeQuorums []ExprSet,
readFraction DistributionValues,
loadLimit *float64,
networkLimit *float64,
latencyLimit *float64) (*Strategy, error) {
...
problemDefinition := lpDefinition {
Vars: []float64 {1.0, 1.0, 1.0}, // dice A , dice B, dice C
Constraints: [][2]float64{
{1, 6}, // Index 0: dice A
{1, 6}, // Index 1: dice B
{1, 6}, // Index 2: dice C
},
Objectives: [][]float64{
// LB A B C UB
{1.0, 1.0, -1.0, 0.0, math.Inf(1)}, // 1 ≤ a - b ≤ ∞ | Dice A cannot be equal to dice B
// lpDefinition defines a linear programming expression with its own Vars, Constraints, Objectives.
type lpDefinition struct {
Vars []float64
Constraints [][2]float64
Objectives [][]float64
}
// NewQuorumSystemWithReads defines a new quorum system given a read Expr, the write Expr is derived using DualOperator.Dual operation.
func NewQuorumSystemWithReads(reads Expr) QuorumSystem {
qs, _ := NewQuorumSystem(reads, reads.Dual())
qs.nameToNode = nameToNode{}
for node := range qs.GetNodes() {
qs.nameToNode[node.Name] = node
}
// OptimizeType describes an optimization type
type OptimizeType string
const (
Load OptimizeType = "Load"
Network OptimizeType = "Network"
Latency OptimizeType = "Latency"
)
// StrategyOptions describes the quorum system strategy options.
// QuorumSystem describes a read-write quorum system.
type QuorumSystem struct {
// reads describes the read-quorum.
reads Expr
// writes describes the write-quorum.
writes Expr
// nameToNode keeps track the name of a node to a GetNodeByName.
nameToNode nameToNode
}
// ExprOperator that wraps the Add and Multiply methods needed to build a quorum from a set of Node.
type ExprOperator interface {
// Add method aggregate a Node to an Expr with a logical Or (a ∨ b)
// returns the resulting Or operation.
Add(expr Expr) Or
// Multiply method aggregate a Node to an Expr with a logical And (a ∧ b)
// returns the resulting And operation.
Multiply(expr Expr) And
}
def search(nodes: List[Node[T]],
read_fraction: Optional[Distribution] = None,
write_fraction: Optional[Distribution] = None,
optimize: str = LOAD,
resilience: int = 0,
load_limit: Optional[float] = None,
network_limit: Optional[float] = None,
latency_limit: Optional[datetime.timedelta] = None,
f: int = 0,
timeout: datetime.timedelta = datetime.timedelta(seconds=0)) \
"""
Original code available at: https://github.com/mwhittaker/quoracle/blob/d83a811a905a51fc13ebc00ddac036d559463380/quoracle/quorum_system.py#L276
"""
def _f_resilient_quorums(self,
f: int,
xs: List[T],
e: Expr) -> Iterator[Set[T]]:
"""
Consider a set X of elements in xs. We say X is f-resilient if, despite