Esempio n. 1
0
 public ImageViewerPageViewModel(IllustrationViewerPageViewModel illustrationViewerPageViewModel, IllustrationViewModel illustrationViewModel)
 {
     IllustrationViewerPageViewModel = illustrationViewerPageViewModel;
     IllustrationViewModel           = illustrationViewModel;
     ImageLoadingCancellationHandle  = new CancellationHandle();
     _ = LoadImage();
 }
Esempio n. 2
0
        private void StartModeSwitching(ThreadStart switcher, CancellationHandle cancellationHandle)
        {
            lock (this)
            {
                if (_modeSwitcherFuture != null)
                {
                    // Cancel any delayed previous switching
                    this._cancellationHandle.cancel();
                    // Wait for it to actually stop what it was doing
                    try
                    {
                        _modeSwitcherFuture.get();
                    }
                    catch (UnableToCopyStoreFromOldMasterException e)
                    {
                        throw e;
                    }
                    catch (Exception e)
                    {
                        _msgLog.warn("Got exception from cancelled task", e);
                    }
                }

                this._cancellationHandle = cancellationHandle;
                _modeSwitcherFuture      = _modeSwitcherExecutor.submit(switcher);
            }
        }
Esempio n. 3
0
        private void SwitchToMaster()
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final CancellationHandle cancellationHandle = new CancellationHandle();
            CancellationHandle cancellationHandle = new CancellationHandle();

            StartModeSwitching(() =>
            {
                if (_currentTargetState != HighAvailabilityMemberState.ToMaster)
                {
                    return;
                }

                // We just got scheduled. Maybe we are already obsolete - test
                if (cancellationHandle.CancellationRequested())
                {
                    _msgLog.info("Switch to master cancelled on start.");
                    return;
                }

                _componentSwitcher.switchToMaster();

                if (cancellationHandle.CancellationRequested())
                {
                    _msgLog.info("Switch to master cancelled before ha communication started.");
                    return;
                }

                _haCommunicationLife.shutdown();
                _haCommunicationLife = new LifeSupport();

                try
                {
                    _masterHaURI = _switchToMaster.switchToMaster(_haCommunicationLife, _me);
                    _canAskForElections.set(true);
                }
                catch (Exception e)
                {
                    _msgLog.error("Failed to switch to master", e);

                    /*
                     * If the attempt to switch to master fails, then we must not try again. We'll trigger an election
                     * and if we are elected again, we'll try again. We differentiate between this case and the case where
                     * we simply receive another election result while switching hasn't completed yet by setting the
                     * currentTargetState as follows:
                     */
                    _currentTargetState = HighAvailabilityMemberState.Pending;
                    // Since this master switch failed, elect someone else
                    _election.demote(_instanceId);
                }
            }, cancellationHandle);
        }
Esempio n. 4
0
 public IllustrationViewModel(Illustration illustration)
 {
     Illustration = illustration;
     LoadingThumbnailCancellationHandle = new CancellationHandle();
 }
Esempio n. 5
0
 protected ObservableDownloadTask(DownloadHistoryEntry entry)
 {
     DatabaseEntry      = entry;
     CancellationHandle = new CancellationHandle();
     Completion         = new TaskCompletionSource();
 }
Esempio n. 6
0
        private void SwitchToSlave()
        {
            // Do this with a scheduler, so that if it fails, it can retry later with an exponential backoff with max
            // wait time.

            /*
             * This is purely defensive and should never trigger. There was a race where the switch to slave task would
             * start after this instance was elected master and the task would constantly try to change as slave
             * for itself, never cancelling. This now should not be possible, since we cancel the task and wait for it
             * to complete, all in a single thread executor. However, this is a check worth doing because if this
             * condition slips through via some other code path it can cause trouble.
             */
            if (GetServerId(_availableMasterId).Equals(_instanceId))
            {
                _msgLog.error("I (" + _me + ") tried to switch to slave for myself as master (" + _availableMasterId + ")");
                return;
            }
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.concurrent.atomic.AtomicLong wait = new java.util.concurrent.atomic.AtomicLong();
            AtomicLong wait = new AtomicLong();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final CancellationHandle cancellationHandle = new CancellationHandle();
            CancellationHandle cancellationHandle = new CancellationHandle();

            StartModeSwitching(() =>
            {
                if (_currentTargetState != HighAvailabilityMemberState.ToSlave)
                {
                    return;                       // Already switched - this can happen if a second master becomes available while waiting
                }

                if (cancellationHandle.CancellationRequested())
                {
                    _msgLog.info("Switch to slave cancelled on start.");
                    return;
                }

                _componentSwitcher.switchToSlave();

                try
                {
                    if (cancellationHandle.CancellationRequested())
                    {
                        _msgLog.info("Switch to slave cancelled before ha communication started.");
                        return;
                    }

                    _haCommunicationLife.shutdown();
                    _haCommunicationLife = new LifeSupport();

                    // it is important for availableMasterId to be re-read on every attempt so that
                    // slave switching would not result in an infinite loop with wrong/stale availableMasterId
                    URI resultingSlaveHaURI = _switchToSlave.switchToSlave(_haCommunicationLife, _me, _availableMasterId, cancellationHandle);
                    if (resultingSlaveHaURI == null)
                    {
                        /*
                         * null slave uri means the task was cancelled. The task then must simply terminate and
                         * have no side effects.
                         */
                        _msgLog.info("Switch to slave is effectively cancelled");
                    }
                    else
                    {
                        _slaveHaURI = resultingSlaveHaURI;
                        _canAskForElections.set(true);
                    }
                }
                catch (HighAvailabilityStoreFailureException e)
                {
                    _userLog.error("UNABLE TO START UP AS SLAVE: %s", e.Message);
                    _msgLog.error("Unable to start up as slave", e);

                    _clusterMemberAvailability.memberIsUnavailable(SLAVE);
                    ClusterClient clusterClient = HighAvailabilityModeSwitcher.this._clusterClient;
                    try
                    {
                        // TODO I doubt this actually works
                        clusterClient.leave();
                        clusterClient.stop();
                        _haCommunicationLife.shutdown();
                    }
                    catch (Exception t)
                    {
                        _msgLog.error("Unable to stop cluster client", t);
                    }

                    _modeSwitcherExecutor.schedule(this, 5, TimeUnit.SECONDS);
                }
                catch (MismatchingStoreIdException)
                {
                    // Try again immediately, the place that threw it have already treated the db
                    // as branched and so a new attempt will have this slave copy a new store from master.
                    run();
                }
                catch (Exception t)
                {
                    _msgLog.error("Error while trying to switch to slave", t);

                    // Try again later
                    wait.set(1 + wait.get() * 2);                         // Exponential backoff
                    wait.set(Math.Min(wait.get(), 5 * 60));               // Wait maximum 5 minutes

                    _modeSwitcherFuture = _modeSwitcherExecutor.schedule(this, wait.get(), TimeUnit.SECONDS);

                    _msgLog.info("Attempting to switch to slave in %ds", wait.get());
                }
            }, cancellationHandle);
        }