public Client(string workerURL) { //this.workerURL = workerURL; // Registers a one time channel to communicate with the puppet master //TcpChannel channel = new TcpChannel(); //ChannelServices.RegisterChannel(channel, true); service = new ClientService(this); registerService(); writeResultThread = new Thread(new ThreadStart(doWriteJob)); writeResultThread.Start(); /** * */ }
/// <summary> /// Does the map work and store the result at mapResult private field. /// </summary> private void doMapJob(ClientService clientService) { logger.Log("Got Mapper class: " + context.SubmitedMap.GetType()); string trackerURL = ""; if (context.workerControllerId > 10) trackerURL = "tcp://localhost:" + 300 + context.workerControllerId + "/W"; else if (context.workerControllerId > 100) trackerURL = "tcp://localhost:" + 30 + context.workerControllerId + "/W"; else if (context.workerControllerId < 10) trackerURL = "tcp://localhost:" + 3000 + context.workerControllerId + "/W"; WorkerService tracker = (WorkerService)Factory.Instance.GetService( typeof(WorkerService), trackerURL); if (tracker != null) { Factory.Instance.SafelyUseService(tracker, (service) => { service.UpdateSubmit(id, context.SplitsToFetch, fileObtained); }); } List<split> newSplitsToFetch = new List<split>(context.SplitsToFetch); mapResult = new List<IList<KeyValuePair<string, string>>>(); int j = 0; int fetchedSplits = 0; foreach (split job in context.SplitsToFetch) { fetchedSplits++; int printedLines = 0; mapResult.Add(new List<KeyValuePair<string, string>>()); Dictionary<int, IList<KeyValuePair<string, string>>> sendResult = new Dictionary<int, IList<KeyValuePair<string, string>>>(); int timeMilli = 0; int lastTimeMilli = System.DateTime.Now.Millisecond; for (int i = job.from; i <= job.to; i++) { int currentTimeMilli = System.DateTime.Now.Millisecond; timeMilli += currentTimeMilli - lastTimeMilli; IList<KeyValuePair<string, string>> tempResult = null; // If other worker calculated the result use it if (sendResult.ContainsKey(i)) { tempResult = sendResult[i]; } else { // No value calculated, do the hard work tempResult = context.SubmitedMap.Map(fileObtained[i - job.from]); } mapResult[j] = mapResult[j].Concat(tempResult).ToList(); sendResult.Add(i, mapResult[j]); if (timeMilli > SEND_RESULTS_TIMEOUT) { logger.Log("Sending calculated result to tracker"); if (tracker != null) { Factory.Instance.SafelyUseService(tracker, (service) => { service.UpdateOutputSplit(id, sendResult); }); } sendResult.Clear(); } lastTimeMilli = currentTimeMilli; // prints the map result splits (Debug only) if (fetchedSplits < 3) { if (printedLines < 3) { logger.Log("Result of Map[" + i + "] is: "); printedLines++; foreach (KeyValuePair<string, string> value in tempResult) { logger.Log("--- " + value); } } else if (printedLines == 3) { logger.Log(" ==> ... More " + (job.to - job.from - 3) + " results ..."); printedLines++; } } else if (fetchedSplits == 3) { logger.Log(" ==> ... More " + (context.SplitsToFetch.Count - 3) + " splits to do ..."); fetchedSplits++; } // --- } j++; List<split> splitToResult = new List<split>(); splitToResult.Add(job); clientService.WriteFinalResult(mapResult, splitToResult); this.nSplitsProcessed++; newSplitsToFetch.Remove(job); if (tracker != null) { Factory.Instance.SafelyUseService(tracker, (service) => { service.UpdateSubmit(id, newSplitsToFetch, null); }); } } }
/// <summary> /// This method is responsible for obtaining the /// part of the file the worker is responsible for processing. /// </summary> private void askClientForFile(ClientService clientService) { logger.Log("Asking client '" + context.ClientURL + "' for file fragments."); List<string> jobFileLines = new List<string>(); int fetchedSplits = 0; foreach (split job in context.SplitsToFetch) { fetchedSplits++; IList<string> fileFragment = clientService.GetSplit(job); // prints the returned splits (Debug only) int printedLines = 0; foreach (string line in fileFragment) { if (fetchedSplits < 3) { if (printedLines < 3) { logger.Log("Got: '" + line); printedLines++; } else if (printedLines == 3) { logger.Log(" ==> ... Fetching more " + (job.to - job.from - 3) + " lines ..."); printedLines++; } } else if (fetchedSplits == 3) { logger.Log(" ==> ... More " + (context.SplitsToFetch.Count - 3) + " fragments to do ..."); fetchedSplits++; } } // --- // adds lines to return value jobFileLines = jobFileLines.Concat(fileFragment).ToList(); } fileObtained = jobFileLines; }