Exemplo n.º 1
0
        private static async Task <MalTrainingData> LoadTrainingDataOnInitAsync(IMalTrainingDataLoader trainingDataLoader, CancellationToken cancellationToken)
        {
            Logging.Log.Info("Loading training data.");
            Stopwatch       timer = Stopwatch.StartNew();
            MalTrainingData trainingData;

            try
            {
                trainingData = await trainingDataLoader.LoadMalTrainingDataAsync(cancellationToken).ConfigureAwait(false);
            }
            catch (OperationCanceledException)
            {
                Logging.Log.Info("Canceled loading training data.");
                throw;
            }
            GC.Collect();
            timer.Stop();

            Logging.Log.InfoFormat("Training data loaded. {0} users, {1} animes, {2} entries. Took {3}.",
                                   trainingData.Users.Count, trainingData.Animes.Count,
                                   trainingData.Users.Keys.Sum(userId => trainingData.Users[userId].Entries.Count),
                                   timer.Elapsed);
            Logging.Log.InfoFormat("Memory use: {0} bytes", GC.GetTotalMemory(forceFullCollection: false));

            return(trainingData);
        }
Exemplo n.º 2
0
        // Loads training data and prerequisites from the database in parallel and does not return until they are loaded.
        private static (MalTrainingData trainingData, IDictionary <int, IList <int> > prereqs) LoadInitialData(IMalTrainingDataLoaderFactory trainingDataLoaderFactory, CancellationToken serviceStopToken)
        {
            using (IMalTrainingDataLoader initialTrainingDataLoader = trainingDataLoaderFactory.GetTrainingDataLoader())
                using (CancellationTokenSource trainingDataOtherFaultOrCancellation = new CancellationTokenSource())
                    using (CancellationTokenSource trainingDataCancel = CancellationTokenSource.CreateLinkedTokenSource(serviceStopToken, trainingDataOtherFaultOrCancellation.Token))
                        using (CancellationTokenSource prereqsOtherFaultOrCancellation = new CancellationTokenSource())
                            using (CancellationTokenSource prereqsCancel = CancellationTokenSource.CreateLinkedTokenSource(serviceStopToken, prereqsOtherFaultOrCancellation.Token))
                            {
                                CancellableAsyncFunc <MalTrainingData> trainingDataAsyncFunc = new CancellableAsyncFunc <MalTrainingData>(
                                    () => LoadTrainingDataOnInitAsync(initialTrainingDataLoader, trainingDataCancel.Token), trainingDataOtherFaultOrCancellation);

                                CancellableTask <MalTrainingData> trainingDataTask = trainingDataAsyncFunc.StartTaskEnsureExceptionsWrapped();

                                CancellableAsyncFunc <IDictionary <int, IList <int> > > prereqsAsyncFunc = new CancellableAsyncFunc <IDictionary <int, IList <int> > >(
                                    () => LoadPrereqsOnInit(initialTrainingDataLoader, prereqsCancel.Token), prereqsOtherFaultOrCancellation);

                                CancellableTask <IDictionary <int, IList <int> > > prereqsTask = prereqsAsyncFunc.StartTaskEnsureExceptionsWrapped();

                                AsyncUtils.WhenAllCancelOnFirstExceptionDontWaitForCancellations(trainingDataTask, prereqsTask).ConfigureAwait(false).GetAwaiter().GetResult();

                                return(trainingDataTask.Task.Result, prereqsTask.Task.Result);
                            }
        }
Exemplo n.º 3
0
        private static async Task <IDictionary <int, IList <int> > > LoadPrereqsOnInit(IMalTrainingDataLoader trainingDataLoader, CancellationToken cancellationToken)
        {
            Logging.Log.Info("Loading prerequisites.");
            Stopwatch timer = Stopwatch.StartNew();
            IDictionary <int, IList <int> > prereqs;

            try
            {
                prereqs = await trainingDataLoader.LoadPrerequisitesAsync(cancellationToken).ConfigureAwait(false);
            }
            catch (OperationCanceledException)
            {
                Logging.Log.Info("Canceled loading prerequisites.");
                throw;
            }
            timer.Stop();

            int numPrereqs = prereqs.Values.Sum(prereqList => prereqList.Count);

            Logging.Log.InfoFormat("Prerequisites loaded. {0} prerequisites for {1} animes. Took {2}.",
                                   numPrereqs, prereqs.Count, timer.Elapsed);
            Logging.Log.InfoFormat("Memory use {0} bytes", GC.GetTotalMemory(forceFullCollection: false));

            return(prereqs);
        }
Exemplo n.º 4
0
        private async Task ReloadTrainingDataHighAvailabilityAsync(bool finalize, CancellationToken cancellationToken)
        {
            using (var trainingDataUpgradeableLock = await m_trainingDataLockAsync.EnterUpgradeableReadLockAsync(cancellationToken).ConfigureAwait(false))
            {
                Logging.Log.Info("Reloading training data and retraining rec sources. Rec sources will remain available.");
                Logging.Log.InfoFormat("Memory use: {0} bytes", GC.GetTotalMemory(forceFullCollection: false));

                Stopwatch totalTimer = Stopwatch.StartNew();

                // Load new training data
                MalTrainingData                 newData;
                IDictionary <int, string>       newUsernames;
                IDictionary <int, IList <int> > newPrereqs;
                using (IMalTrainingDataLoader malTrainingDataLoader = m_trainingDataLoaderFactory.GetTrainingDataLoader())
                    using (CancellationTokenSource faultCanceler = new CancellationTokenSource())
                        using (CancellationTokenSource faultOrUserCancel = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, faultCanceler.Token))
                        {
                            Stopwatch trainingDataTimer = Stopwatch.StartNew();

                            CancellableTask <MalTrainingData> trainingDataTask = new CancellableTask <MalTrainingData>(
                                malTrainingDataLoader.LoadMalTrainingDataAsync(faultOrUserCancel.Token), faultCanceler);

                            Task trainingDataTimerTask = trainingDataTask.Task.ContinueWith(task =>
                            {
                                trainingDataTimer.Stop();
                                Logging.Log.InfoFormat("Training data loaded. {0} users, {1} animes, {2} entries. Took {3}.",
                                                       task.Result.Users.Count, task.Result.Animes.Count,
                                                       task.Result.Users.Keys.Sum(userId => task.Result.Users[userId].Entries.Count),
                                                       trainingDataTimer.Elapsed);
                            },
                                                                                            cancellationToken, TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.NotOnCanceled
                                                                                            | TaskContinuationOptions.NotOnFaulted, TaskScheduler.Current);

                            Stopwatch prereqsTimer = Stopwatch.StartNew();

                            CancellableTask <IDictionary <int, IList <int> > > prereqsTask = new CancellableTask <IDictionary <int, IList <int> > >(
                                malTrainingDataLoader.LoadPrerequisitesAsync(faultOrUserCancel.Token), faultCanceler);

                            Task prereqsTimerTask = prereqsTask.Task.ContinueWith(task =>
                            {
                                prereqsTimer.Stop();
                                int numPrereqs = task.Result.Values.Sum(prereqList => prereqList.Count);
                                Logging.Log.InfoFormat("Prerequisites loaded. {0} prerequisites for {1} animes. Took {2}.",
                                                       numPrereqs, task.Result.Count, prereqsTimer.Elapsed);
                            },
                                                                                  cancellationToken, TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.NotOnCanceled
                                                                                  | TaskContinuationOptions.NotOnFaulted, TaskScheduler.Current);

                            await AsyncUtils.WhenAllCancelOnFirstExceptionDontWaitForCancellations(trainingDataTask, prereqsTask);

                            newData      = trainingDataTask.Task.Result;
                            newUsernames = GetUsernamesFromTrainingData(newData);

                            newPrereqs = prereqsTask.Task.Result;

                            await trainingDataTimerTask.ConfigureAwait(false);

                            await prereqsTimerTask.ConfigureAwait(false);
                        }

                GC.Collect();
                Logging.Log.InfoFormat("Memory use: {0} bytes", GC.GetTotalMemory(forceFullCollection: false));

                using (var recSourcesUpgradeableLock = await m_recSourcesLockAsync.EnterUpgradeableReadLockAsync(cancellationToken).ConfigureAwait(false))
                {
                    // clone the json rec sources without the training state and train each one with the new data.
                    Dictionary <string, ITrainableJsonRecSource>         newRecSources         = new Dictionary <string, ITrainableJsonRecSource>(StringComparer.OrdinalIgnoreCase);
                    Dictionary <string, Func <ITrainableJsonRecSource> > newRecSourceFactories = new Dictionary <string, Func <ITrainableJsonRecSource> >(m_recSourceFactories, StringComparer.OrdinalIgnoreCase);

                    if (m_recSourceFactories.Count == 0)
                    {
                        Logging.Log.Info("No rec sources to retrain.");
                    }
                    else
                    {
                        Logging.Log.Info("Retraining rec sources.");

                        object newRecSourcesLockAndMemFence = new object();

                        List <Task> recSourceTrainTasksList = new List <Task>();

                        // ToList() so we can unload a rec source as we iterate if it errors while training.
                        foreach (string recSourceNameLoopVar in m_recSourceFactories.Keys.ToList())
                        {
                            string recSourceName = recSourceNameLoopVar; // avoid capturing the loop var
                            ITrainableJsonRecSource recSource = newRecSourceFactories[recSourceName]();

                            Task recSourceTrainTask = Task.Run(() =>
                            {
                                Logging.Log.InfoFormat("Retraining rec source {0} ({1}).", recSourceName, recSource);
                                Stopwatch trainTimer = Stopwatch.StartNew();

                                try
                                {
                                    recSource.Train(newData, newUsernames, cancellationToken);
                                    trainTimer.Stop();
                                    Logging.Log.InfoFormat("Trained rec source {0} ({1}). Took {2}.", recSourceName, recSource, trainTimer.Elapsed);
                                    lock (newRecSourcesLockAndMemFence)
                                    {
                                        newRecSources[recSourceName] = recSource;
                                    }
                                }
                                catch (OperationCanceledException)
                                {
                                    Logging.Log.InfoFormat("Canceled while retraining rec source {0} ({1}).", recSourceName, recSource);
                                    throw;
                                }
                                catch (Exception ex)
                                {
                                    Logging.Log.ErrorFormat("Error retraining rec source {0} ({1}): {2} Unloading it.",
                                                            ex, recSourceName, recSource, ex.Message);

                                    lock (newRecSourcesLockAndMemFence)
                                    {
                                        newRecSourceFactories.Remove(recSourceName);
                                    }
                                }
                            }, cancellationToken);

                            recSourceTrainTasksList.Add(recSourceTrainTask);
                        }

                        // Wait for all to complete or cancellation. There should not be any exceptions other than OperationCanceledException.
                        await Task.WhenAll(recSourceTrainTasksList);

                        lock (newRecSourcesLockAndMemFence)
                        {
                            ; // just for the fence
                        }
                    }

                    // Swap in the newly trained rec sources.
                    using (var trainingDataWriteLock = await m_trainingDataLockAsync.UpgradeToWriteLock(cancellationToken).ConfigureAwait(false))
                        using (var recSourcesWriteLock = await m_recSourcesLockAsync.UpgradeToWriteLock(cancellationToken).ConfigureAwait(false))
                        {
                            m_recSources         = newRecSources;
                            m_recSourceFactories = newRecSourceFactories;

                            m_animes  = newData.Animes;
                            m_prereqs = newPrereqs;

                            if (finalize)
                            {
                                m_trainingData = null;
                                m_usernames    = null;
                                m_finalized    = true;
                                Logging.Log.Info("Finalized rec sources.");
                            }
                            else
                            {
                                m_trainingData = newData;
                                m_usernames    = newUsernames;
                                m_finalized    = false;
                            }
                        }
                }

                totalTimer.Stop();
                Logging.Log.InfoFormat("All rec sources retrained with the latest data. Total time: {0}", totalTimer.Elapsed);
            }

            GC.Collect();
            Logging.Log.InfoFormat("Memory use: {0} bytes", GC.GetTotalMemory(forceFullCollection: false));
        }
Exemplo n.º 5
0
        private MalTrainingData LoadTrainingDataOnInit(IMalTrainingDataLoader trainingDataLoader)
        {
            Logging.Log.Info("Loading training data.");
            Stopwatch timer = Stopwatch.StartNew();
            MalTrainingData trainingData = trainingDataLoader.LoadMalTrainingData();
            GC.Collect();
            timer.Stop();

            Logging.Log.InfoFormat("Training data loaded. {0} users, {1} animes, {2} entries. Took {3}.",
                trainingData.Users.Count, trainingData.Animes.Count,
                trainingData.Users.Keys.Sum(userId => trainingData.Users[userId].Entries.Count),
                timer.Elapsed);
            Logging.Log.InfoFormat("Memory use: {0} bytes", GC.GetTotalMemory(forceFullCollection: false));

            return trainingData;
        }
Exemplo n.º 6
0
        private IDictionary<int, IList<int>> LoadPrereqsOnInit(IMalTrainingDataLoader trainingDataLoader)
        {
            Logging.Log.Info("Loading prerequisites.");
            Stopwatch timer = Stopwatch.StartNew();
            IDictionary<int, IList<int>> prereqs = trainingDataLoader.LoadPrerequisites();
            timer.Stop();

            int numPrereqs = prereqs.Values.Sum(prereqList => prereqList.Count);
            Logging.Log.InfoFormat("Prerequisites loaded. {0} prerequisites for {1} animes. Took {2}.",
                numPrereqs, prereqs.Count, timer.Elapsed);
            Logging.Log.InfoFormat("Memory use {0} bytes", GC.GetTotalMemory(forceFullCollection: false));

            return prereqs;
        }