public void fullReplication(ReplicatedInfo replicatedInfo) { this.replicatedInfo = replicatedInfo; if (!beating) { Console.WriteLine("I AM NOW SECONDARY URAYYY"); lastPrimaryHeartBeat = DateTime.Now; // start heartbeats heartbeatDelegate del = new heartbeatDelegate(heartbeat); del.BeginInvoke(null, null); beating = true; } }
public WorkerJobTracker(int id, string serviceURL, TcpChannel clientChannel) { this.id = id; this.serviceURL = serviceURL; this.JTURL = serviceURL; this.isJT = true; this.tcpChannel = clientChannel; replicatedInfo = new ReplicatedInfo(); replicatedInfo.workers = new Dictionary <int, string>(); jobTracker = (IWorkerJobTracker)Activator.GetObject( typeof(IWorkerJobTracker), serviceURL); addWorker(id, serviceURL); heartbeatDelegate del = new heartbeatDelegate(heartbeat); del.BeginInvoke(null, null); }
public void heartbeat() { JTsemaphore.WaitOne(); JTsemaphore.Release(); //Console.WriteLine("heartBeat"); if (isJT && replicatedInfo.workers.Count > 1 && secondary == null && !outOfJT) { int newSecondaryID = id + 1; if (newSecondaryID > replicatedInfo.workers.Count) { newSecondaryID = 1; } IWorkerJobTracker worker = (IWorkerJobTracker)Activator.GetObject(typeof(IWorkerJobTracker), replicatedInfo.workers[newSecondaryID]); worker.fullReplication(replicatedInfo); secondaryID = newSecondaryID; secondary = worker; Console.WriteLine("creating secondary"); } else if (isJT && secondary != null) { Console.WriteLine("sending heartbeat"); sendHeartbeatDelegator del = new sendHeartbeatDelegator(secondary.sendHeartbeat); IAsyncResult result = del.BeginInvoke(null, null); DateTime time = DateTime.Now; while (!result.IsCompleted) { if ((DateTime.Now - time).TotalMilliseconds > HEARTBEATTIMEOUT * 1000) { //replace secondary Console.WriteLine("RIP secondary Count: " + replicatedInfo.workers.Count); if (replicatedInfo.workers.Count < 3) { outOfJT = true; secondary = null; secondaryID = 0; break; } else { int newSecondaryID = id + 2; if (newSecondaryID > replicatedInfo.workers.Count) { newSecondaryID = 1; } IWorkerJobTracker worker = (IWorkerJobTracker)Activator.GetObject(typeof(IWorkerJobTracker), replicatedInfo.workers[newSecondaryID]); worker.fullReplication(replicatedInfo); secondaryID = newSecondaryID; secondary = worker; Console.WriteLine("created new secondary"); break; } } } if (result.IsCompleted) { bool stillprimary = del.EndInvoke(result); if (!stillprimary) { replicatedInfo = null; isJT = false; secondary = null; beating = false; Console.WriteLine("Lost primary :C "); return; } } } else if (!isJT && secondary == null) { Console.WriteLine("checking on primary " + (DateTime.Now - lastPrimaryHeartBeat).TotalMilliseconds); if ((DateTime.Now - lastPrimaryHeartBeat).TotalMilliseconds > HEARTBEATTIMEOUT * 1000) { areYouAliveDelegate del = new areYouAliveDelegate(jobTracker.areYouAlive); IAsyncResult result = del.BeginInvoke(id, null, null); DateTime time = DateTime.Now; while (!result.IsCompleted) { if ((DateTime.Now - time).TotalMilliseconds > HEARTBEATTIMEOUT / 2 * 1000) { // replace primary Console.WriteLine("RIP PRIMARY Count: " + replicatedInfo.workers.Count); if (replicatedInfo.workers.Count < 3) { outOfJT = true; isJT = true; foreach (string w in replicatedInfo.workers.Values) { ((IWorkerJobTracker)Activator.GetObject(typeof(IWorkerJobTracker), w)).newJobTracker(serviceURL); } Console.WriteLine("created no secondary"); break; } else { int newSecondaryID = id + 1; if (newSecondaryID > replicatedInfo.workers.Count) { newSecondaryID = 1; } IWorkerJobTracker worker = (IWorkerJobTracker)Activator.GetObject(typeof(IWorkerJobTracker), replicatedInfo.workers[newSecondaryID]); worker.fullReplication(replicatedInfo); secondaryID = newSecondaryID; secondary = worker; isJT = true; foreach (string w in replicatedInfo.workers.Values) { ((IWorkerJobTracker)Activator.GetObject(typeof(IWorkerJobTracker), w)).newJobTracker(serviceURL); } Console.WriteLine("created new secondary"); break; } } } if (result.IsCompleted) { bool stillSecondary = del.EndInvoke(result); if (!stillSecondary) { beating = false; replicatedInfo = null; return; } } } } Task.Delay(TimeSpan.FromSeconds(heartbeatDelay)).Wait(); heartbeat(); }