public override void sendTuple(ReplicaInterface replica, TupleWrapper tuple) { Console.WriteLine("\n Inside sendTuple"); Exception exception = null; var thread = new Thread(() => { try { replica.addTuple(tuple); } catch (Exception e) { exception = e; } }); thread.Start(); if (!thread.Join(TIMEOUT_VALUE)) { Console.WriteLine("####### didnt finish call in 5 seconds"); thread.Abort(); //throw new TimeoutException(); throw new CouldNotSendTupleException(); } if (exception != null) { throw exception; } }
public DTOtupleState getTupleState(TupleWrapper tuple) { if (isOnSeenTuples(tuple)) { return(new DTOtupleState(tuple.ID)); } if (getOnProcessingOnMe(tuple) != null) { return(new DTOtupleState(tuple, replicaAddress)); } DTOtupleState result = getOnProcessingOnOther(tuple); if (result != null) { return(result); } if (isOnDeciding(tuple) || isOnAllTuples(tuple)) { return(new DTOtupleState(tuple)); } else { return(new DTOtupleState()); } }
public DTOtupleState getOnProcessingOnOther(TupleWrapper t) { DTOtupleState output; Monitor.Enter(processingOnOther); foreach (KeyValuePair <string, Dictionary <string, OtherReplicaTuple> > entry in processingOnOther) { foreach (Dictionary <string, OtherReplicaTuple> entry2 in processingOnOther.Values) { foreach (KeyValuePair <string, OtherReplicaTuple> entry3 in entry2) { if (t.Equals(entry3.Value.getTuple())) { output = new DTOtupleState(entry3.Value, entry.Key); Monitor.Pulse(processingOnOther); Monitor.Exit(processingOnOther); return(output); } } } } Monitor.Pulse(processingOnOther); Monitor.Exit(processingOnOther); return(null); }
public void finishedSending(TupleWrapper tuple, string url) { while (frozen) { Thread.Sleep(1); } Monitor.Enter(seenTuples); seenTuples.Add(tuple.ID); Monitor.Pulse(seenTuples); Monitor.Exit(seenTuples); if (once) { Monitor.Enter(processingOnOther); Dictionary <string, OtherReplicaTuple> t = processingOnOther[url]; t.Remove(tuple.ID); Monitor.Pulse(processingOnOther); Monitor.Exit(processingOnOther); } else if (atLeastOnce) { Monitor.Enter(allTuples); allTuples.Remove(tuple); Monitor.Pulse(processingOnOther); Monitor.Exit(processingOnOther); } }
/// <summary>Change the owner of the tuples from oldOwner to this replica</summary> private void changeToMe(string oldOwner) //TODO make locks { Dictionary <string, OtherReplicaTuple> notProcessed = new Dictionary <string, OtherReplicaTuple>(); Dictionary <string, OtherReplicaTuple> oldtuples = new Dictionary <string, OtherReplicaTuple>(processingOnOther[oldOwner]); foreach (string tupleid in oldtuples.Keys) { if (oldtuples[tupleid].isProcessed()) //Needs a majority { Monitor.Enter(processingOnOther); TupleWrapper tuple = processingOnOther[oldOwner][tupleid].getTuple(); List <TupleWrapper> result = processingOnOther[oldOwner][tupleid].getResult(); processingOnOther[oldOwner].Remove(tupleid); Monitor.Pulse(processingOnOther); Monitor.Exit(processingOnOther); Monitor.Enter(seenTuples); seenTuples.Add(tupleid); Monitor.Pulse(seenTuples); Monitor.Exit(seenTuples); foreach (TupleWrapper tup in result) { int n = 1; // To be used for calculating the minimum required number of working replicas object o = new object(); //TODO need timeout foreach (string otherReplica in allReplicasURL) { if (!otherReplica.Equals(replicaAddress)) { Thread thread = new Thread(() => broadcastFinished(tup, otherReplica, ref n, ref o)); thread.Start(); } } int x = ((allReplicasURL.Count) / 2) + 1; while (true) { lock (o) { if (n == x) { break; } } Thread.Sleep(1); } } } else //needs all the others information { notProcessed.Add(tupleid, oldtuples[tupleid]); Monitor.Enter(processingOnOther); processingOnOther[oldOwner].Remove(tupleid); Monitor.Pulse(processingOnOther); Monitor.Exit(processingOnOther); } } Thread t = new Thread(() => handleNotProcessed(notProcessed, allReplicasURL, replicaAddress)); t.Start(); }
/***************** * AUX FUNCTIONS * ****************/ public void addToQueue(TupleWrapper t, Queue q) { Monitor.Enter(q.SyncRoot); q.Enqueue(t); Monitor.PulseAll(q.SyncRoot); Monitor.Exit(q.SyncRoot); }
public void sendToNext(TupleWrapper tuple) { if (nextOperator.Count != 0) { ReplicaInterface replica = null; string outputReplica = calculateNext(tuple.Tuple); try { replica = (ReplicaInterface)Activator.GetObject(typeof(ReplicaInterface), outputReplica); } catch (System.Net.Sockets.SocketException e) { Console.WriteLine("Error with host " + outputReplica); Console.WriteLine(e); } try { // TODO FIXME something not right with tuple on mylib.dll version of professors semantics.sendTuple(replica, tuple); } catch (System.Net.Sockets.SocketException) { // The replica is dead Console.WriteLine(" ##!! " + outputReplica + " was down, removed from nextOperator list. Resending !!##\n"); nextOperator.Remove(outputReplica); sendToNext(tuple); } catch (CouldNotSendTupleException) { // The replica is alive, but slow Console.WriteLine(" ##!! " + outputReplica + " did not respond in time. Resending !!##\n"); sendToNext(tuple); } catch (Exception) { } } //ELSE can write on file }
private void confirmElectionRelay(TupleWrapper t, ReplicaInterface r, string replicaAddr, ref int counter, ref object o) { r.confirmElection(t, replicaAddress); lock (o) { counter++; } }
//method used to get tuples from the buffer //USED BY: owner(replica) public TupleWrapper getTuple() { Console.WriteLine("getTuple()"); TupleWrapper t = takeFromQueue(tupleQueue); Console.WriteLine(" GOT tuple"); return(t); }
public static void Main(string[] args) { string filepath = args[0]; string routingLower = args[1].ToLower(); string semantics = args[2]; List <string> replicas = new List <string>(); for (int i = 3; i < args.Length; i++) { replicas.Add(args[i]); } string[] lines; Router router; char[] delimiters = { '(', ')' }; string[] splitted = routingLower.Split(delimiters, StringSplitOptions.RemoveEmptyEntries); if (splitted[0].Equals("primary")) { router = new PrimaryRouter(replicas, semantics); } else if (splitted[0].Equals("random")) { router = new RandomRouter(replicas, semantics); } else { router = new HashRouter(replicas, semantics, int.Parse(splitted[1])); } try { lines = System.IO.File.ReadAllLines(filepath); lines = lines.Where(line => (line.Length > 0 && line[0] != '%')).ToArray(); int counter = 0; foreach (string line in lines) { string[] tuple = getTupleFromLine(line); TupleWrapper t = new TupleWrapper("", filepath + ":" + string.Join(" - ", replicas) + ":" + counter++, tuple); router.sendToNext(t); } } catch (System.IO.FileNotFoundException ex) { Console.WriteLine(ex); } catch (Exception e) { Console.WriteLine(e); }finally { Console.WriteLine("That's it Folks! Shutting down.."); Console.ReadLine(); } }
public bool isOnSeenTuples(TupleWrapper t) { Monitor.Enter(seenTuples); if (seenTuples.Contains(t.ID)) { Monitor.Pulse(seenTuples); Monitor.Exit(seenTuples); return(true); } Monitor.Pulse(seenTuples); Monitor.Exit(seenTuples); return(false); }
public TupleWrapper getOnProcessingOnMe(TupleWrapper t) { Monitor.Enter(processingOnMe); if (processingOnMe.Contains(t)) { Monitor.Pulse(processingOnMe); Monitor.Exit(processingOnMe); return(t); } Monitor.Pulse(processingOnMe); Monitor.Exit(processingOnMe); return(null); }
public bool isOnAllTuples(TupleWrapper t) { Monitor.Enter(allTuples); if (allTuples.ContainsKey(t)) { Monitor.Pulse(allTuples); Monitor.Exit(allTuples); return(true); } Monitor.Pulse(allTuples); Monitor.Exit(allTuples); return(false); }
public TupleWrapper takeFromQueue(Queue q) { Monitor.Enter(q.SyncRoot); while (q.Count == 0) { Monitor.Wait(q.SyncRoot); } TupleWrapper result = (TupleWrapper)q.Dequeue(); Monitor.Pulse(q.SyncRoot); Monitor.Exit(q.SyncRoot); return(result); }
public bool isOnDeciding(TupleWrapper t) { Monitor.Enter(deciding); if (deciding.ContainsKey(t)) { Monitor.Pulse(deciding); Monitor.Exit(deciding); return(true); } Monitor.Pulse(deciding); Monitor.Exit(deciding); return(false); }
/*************************** * FAULT-TOLERANCE METHODS * **************************/ // To be used locally private void broadcastTuple(TupleWrapper t, string url, ref int counter, ref object o) { ReplicaInterface r; if ((r = getReplica(url)) != null) { try { r.arrivedTuple(t); lock (o) { counter++; } } catch (System.Net.Sockets.SocketException) { } } }
// To be used locally private void broadcastFinished(TupleWrapper t, string url, ref int counter, ref object o) { ReplicaInterface r; if ((r = getReplica(url)) != null) { try { r.finishedSending(t, replicaAddress); lock (o) { counter++; } } catch (System.Net.Sockets.SocketException) { } } }
public void confirmElection(TupleWrapper t, string url) { while (frozen) { Thread.Sleep(1); } // Remove from deciding pile Monitor.Enter(deciding); if (deciding.ContainsKey(t)) { deciding.Remove(t); } Monitor.Pulse(deciding); Monitor.Exit(deciding); // Remove from allTuples Monitor.Enter(allTuples); allTuples.Remove(t); Monitor.Pulse(allTuples); Monitor.Exit(allTuples); // Add to the respective processing pile if (url.Equals(replicaAddress)) { addToQueue(t, tupleQueue); Monitor.Enter(processingOnMe); processingOnMe.Add(t); Monitor.Pulse(processingOnMe); Monitor.Exit(processingOnMe); } else { Monitor.Enter(processingOnOther); if (!processingOnOther.ContainsKey(url)) { processingOnOther.Add(url, new Dictionary <string, OtherReplicaTuple>()); } processingOnOther[url].Add(t.ID, new OtherReplicaTuple(t)); Monitor.Pulse(processingOnOther); Monitor.Exit(processingOnOther); } }
public void tryElectionOfProcessingReplica(TupleWrapper t, string url) { while (frozen) { Thread.Sleep(1); } Monitor.Enter(deciding); if (deciding.ContainsKey(t)) { Monitor.Pulse(deciding); Monitor.Exit(deciding); throw new AlreadyVotedException(); } deciding.Add(t, new DecisionStructure(url)); Monitor.Pulse(deciding); Monitor.Exit(deciding); }
public void arrivedTuple(TupleWrapper t) { while (frozen) { Thread.Sleep(1); } if (once) { if (isOnDeciding(t)) { return; } if (getOnProcessingOnMe(t) != null) { return; } if (getOnProcessingOnOther(t) != null) { return; } } if (isOnSeenTuples(t)) { return; } Monitor.Enter(allTuples); if (!allTuples.ContainsKey(t)) { allTuples.Add(t, new DateTime()); } else { allTuples[t] = new DateTime(); } Monitor.Pulse(allTuples); Monitor.Exit(allTuples); }
private void tryElectionOfProcessingReplicaRelay(TupleWrapper t, ReplicaInterface r, string replicaAddress) { r.tryElectionOfProcessingReplica(t, replicaAddress); }
/******************** * AGREEMENT THREAD * *******************/ private void chooseProcessingReplica(TupleWrapper t) { int majority = (int)((allReplicasURL.Count) / 2) + 1; int counter = 0; foreach (string url in allReplicasURL) { ReplicaInterface r; if ((r = getReplica(url)) == null) { continue; } int exception = -1; Thread thread = new Thread(() => { try { tryElectionOfProcessingReplicaRelay(t, r, replicaAddress); } catch (AlreadyVotedException) { exception = 0; } catch (Exception) { } }); thread.Start(); if (!thread.Join(3000)) { thread.Abort(); if (exception == 0) { return; } } else { counter++; } if (counter == majority) { break; } } counter = 0; object o = new object(); foreach (string replica in allReplicasURL) { ReplicaInterface r; if ((r = getReplica(replica)) == null) { continue; } Thread thread = new Thread(() => { confirmElectionRelay(t, r, replicaAddress, ref counter, ref o); }); thread.Start(); } while (true) { lock (o) { if (counter >= majority) { break; } } Thread.Sleep(1); } }
public override void sendTuple(ReplicaInterface replica, TupleWrapper tuple) { RemoteAsyncDelegate RemoteDel = new RemoteAsyncDelegate(replica.addTuple); IAsyncResult RemAr = RemoteDel.BeginInvoke(tuple, null, replica); }
public abstract void sendTuple(ReplicaInterface replica, TupleWrapper tuple);
// To be used in the consumer thread public void Operate() { Console.WriteLine("6-Waiting for START command"); int counter = 0; while (!start) { Thread.Sleep(100); } if (failureDetectorThread != null) { failureDetectorThread.Start();//TODO check if this is used just in exacly once } while (true) { int majority = (int)((allReplicasURL.Count) / 2) + 1; //see if it is feezed while (frozen == true) { Thread.Sleep(100); } //wait the defined time between processing Thread.Sleep(waitingTime); //get tuple from the buffer TupleWrapper tuple = getTuple(); List <string[]> result = operation.Operate(tuple.Tuple); if (result != null) { List <TupleWrapper> convertedResult = new List <TupleWrapper>(); foreach (string[] el in result) { convertedResult.Add(new TupleWrapper(tuple.ID, "" + counter++ + ":" + replicaAddress, el)); } if (once) { int n = 1; // To be used for calculating the minimum required number of working replicas object o = new object(); foreach (string otherReplica in allReplicasURL) { if (!otherReplica.Equals(replicaAddress)) { Thread t = new Thread(() => broadcastResult(tuple, otherReplica, convertedResult, ref n, ref o)); t.Start(); } } while (true) { lock (o) { if (n >= majority) { break; } } Thread.Sleep(1); } } foreach (TupleWrapper outTuple in convertedResult) { Console.WriteLine("sending tuple"); router.sendToNext(outTuple); if (logLevel) { log.Log("tuple " + operationName + " " + replicaAddress + " <" + string.Join(" - ", outTuple.Tuple) + ">"); } } if (once) { Monitor.Enter(seenTuples); seenTuples.Add(tuple.ID); Monitor.Pulse(seenTuples); Monitor.Exit(seenTuples); Monitor.Enter(processingOnMe); processingOnMe.Remove(tuple); Monitor.Pulse(processingOnMe); Monitor.Exit(processingOnMe); int n = 1; // To be used for calculating the minimum required number of working replicas object o = new object(); foreach (string otherReplica in allReplicasURL) { if (!otherReplica.Equals(replicaAddress)) { Thread t = new Thread(() => broadcastFinished(tuple, otherReplica, ref n, ref o)); t.Start(); } } while (true) { lock (o) { if (n >= majority) { break; } } Thread.Sleep(1); } } // To remove the tuples from the other lists else if (once) { Monitor.Enter(seenTuples); seenTuples.Add(tuple.ID); Monitor.Pulse(seenTuples); Monitor.Exit(seenTuples); int n = 1; // To be used for calculating the minimum required number of working replicas object o = new object(); foreach (string otherReplica in allReplicasURL) { if (!otherReplica.Equals(replicaAddress)) { Thread t = new Thread(() => broadcastFinished(tuple, otherReplica, ref n, ref o)); t.Start(); } } while (true) { lock (o) { if (n >= majority) { break; } } Thread.Sleep(1); } } } } }
public override void sendTuple(ReplicaInterface replica, TupleWrapper tuple) { alos.sendTuple(replica, tuple); }
//method used to send tuples to the owner of this buffer //USED BY: other replicas, input file public void addTuple(TupleWrapper tuple) { while (frozen || !start) { Thread.Sleep(1); } Console.WriteLine("addTuple({0})", tuple.Tuple); if (once || atLeastOnce) { int majority = (int)((allReplicasURL.Count) / 2) + 1; if (once) { if (isOnDeciding(tuple)) { return; } if (getOnProcessingOnMe(tuple) != null) { return; } if (getOnProcessingOnOther(tuple) != null) { return; } } if (isOnSeenTuples(tuple)) { return; } // ALL TUPLES Monitor.Enter(allTuples); if (!allTuples.ContainsKey(tuple)) { allTuples.Add(tuple, new DateTime()); } else { allTuples[tuple] = new DateTime(); } Monitor.Pulse(allTuples); Monitor.Exit(allTuples); int counter = 1; // To be used for calculating the minimum required number of working replicas object o = new object(); foreach (string otherReplica in allReplicasURL) { if (!otherReplica.Equals(replicaAddress)) { Thread t = new Thread(() => broadcastTuple(tuple, otherReplica, ref counter, ref o)); t.Start(); } } while (true) { lock (o) { if (counter >= majority) { break; } } Thread.Sleep(1); } if (once) { Thread thread = new Thread(() => chooseProcessingReplica(tuple)); thread.Start(); return; } } addToQueue(tuple, tupleQueue); }