public void updateData(UpdateData data, int lastPing) { if (failed) { Console.WriteLine(this.serverURL + " is not responding."); Thread.Sleep(TimeSpan.FromSeconds(2 * Constants.TAKEOVER_TIMEOUT)); return; } checkFreezed(); Monitor.Enter(this); receivedPing = true; if (lastPingId + 1 == lastPing) { lastPingId++; applyUpdate(data); while (pendingUpdates.Count != 0 && pendingUpdates.ContainsKey(lastPingId + 1)) { UpdateData oldData = pendingUpdates[lastPingId + 1]; pendingUpdates.Remove(lastPingId + 1); lastPingId++; applyUpdate(oldData); } } else { pendingUpdates.Add(lastPing, data); } Monitor.Exit(this); status(); }
private void applyUpdate(UpdateData data) { pingData = data; Console.WriteLine("Ping Seq: " +lastPingId); if (data.createdPadIntsMap.Count != 0) { foreach (KeyValuePair<int, PadIntData> entry in data.createdPadIntsMap) { if (!createdPadIntsMap.ContainsKey(entry.Key)) { ServerPadInt sp = new ServerPadInt(entry.Key); sp.log.setReadData(entry.Value.lRead); sp.log.setWriteData(entry.Value.lWrite); sp.log.setTentativeData(entry.Value.lTentative); sp.log.setTentativeValueData(entry.Value.lTentativeVal); sp.log.setTentativeReadData(entry.Value.lTentativeRead); sp.setValueData(entry.Value.value); createdPadIntsMap.Add(entry.Key, sp); } allPingData.createdPadInt(entry.Key, entry.Value); } } if (data.removedCreatedPadInts.Count != 0) { foreach (int key in data.removedCreatedPadInts) { if (createdPadIntsMap.ContainsKey(key)) { createdPadIntsMap.Remove(key); } allPingData.removeCreatedPadInt(key); } } if (data.commitedPadIntsMap.Count != 0) { foreach (KeyValuePair<int, PadIntData> entry in data.commitedPadIntsMap) { ServerPadInt sp; if (padIntsMap.ContainsKey(entry.Key)) { sp = padIntsMap[entry.Key]; } else { sp = new ServerPadInt(entry.Key); } sp.log.setReadData(entry.Value.lRead); sp.log.setWriteData(entry.Value.lWrite); sp.log.setTentativeData(entry.Value.lTentative); sp.log.setTentativeValueData(entry.Value.lTentativeVal); sp.setValueData(entry.Value.value); if (!padIntsMap.ContainsKey(entry.Key)) { padIntsMap.Add(entry.Key, sp); } allPingData.commitedPadIntsMap.Add(entry.Key, entry.Value); } int actualOccupation = padIntsMap.Count; int actualNumberTransactions = tIdPadIntMap.Count; Monitor.Exit(this); new Thread(delegate() { master.updateChargeServer(serverURL, actualOccupation, actualNumberTransactions); }).Start(); Monitor.Enter(this); } if (data.modifiedPadIntsMap.Count != 0) { foreach (KeyValuePair<int, PadIntData> entry in data.modifiedPadIntsMap) { padIntsMap[entry.Key].log.setReadData(entry.Value.lRead); padIntsMap[entry.Key].log.setWriteData(entry.Value.lWrite); padIntsMap[entry.Key].log.setTentativeData(entry.Value.lTentative); padIntsMap[entry.Key].log.setTentativeValueData(entry.Value.lTentativeVal); padIntsMap[entry.Key].setValueData(entry.Value.value); allPingData.addModifiedPadInt(entry.Key, entry.Value); } } if (data.tIdPadIntMap.Count != 0) { foreach (KeyValuePair<IdTransaction, ISet<int>> entry in data.tIdPadIntMap) { if (!tIdPadIntMap.ContainsKey(entry.Key)) { tIdPadIntMap.Add(entry.Key, new HashSet<int>()); } tIdPadIntMap[entry.Key] = entry.Value; foreach (int padInt in entry.Value) { allPingData.addNewTransaction(entry.Key, padInt); } } } if (data.removedTransactions.Count != 0) { foreach (IdTransaction t in data.removedTransactions) { if (tIdPadIntMap.ContainsKey(t)) { tIdPadIntMap.Remove(t); } allPingData.removeTransaction(t); } } }
//int actualNumberConections; public Server(string serverURL) { masterURL = "tcp://localhost:"+Constants.MASTER_PORT+"/"+Constants.MASTER_NAME; this.serverURL = serverURL; master = (iMaster)Activator.GetObject(typeof(iMaster), masterURL); padIntsMap = new Dictionary<int, ServerPadInt>(); tIdPadIntMap = new Dictionary<IdTransaction, ISet<int>>(); createdPadIntsMap = new Dictionary<int, ServerPadInt>(); freezed = false; freezedLock = new Object(); notFreezed = new AutoResetEvent(true); failed = false; primary = null; secundary = null; primaryURL = ""; secundaryURL = ""; pingData = new UpdateData(); allPingData = new UpdateData(); lastPingId = 0; pendingUpdates = new Dictionary<int,UpdateData>(); pingTimer = null; receivedPing = true; }