Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8164d247 authored by JW Wang's avatar JW Wang
Browse files

Remove unused locks (2/n)

Now all state changes happen on the same thread. No locks are needed.

Bug: 159390709
Test: atest RollbackTest StagedRollbackTest
Test: atest RollbackUnitTest RollbackStoreTest AppDataRollbackHelperTest

Change-Id: Ib5e8027e4895a89f9441a5ef71b3270367a15554
parent 2ea5291c
Loading
Loading
Loading
Loading
+0 −8
Original line number Diff line number Diff line
@@ -22,7 +22,6 @@ import android.content.rollback.PackageRollbackInfo.RestoreInfo;
import android.os.storage.StorageManager;
import android.util.Slog;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.server.pm.ApexManager;
import com.android.server.pm.Installer;
@@ -59,8 +58,6 @@ public class AppDataRollbackHelper {
     * {@code userIds}. Updates said {@code packageRollbackInfo} with the inodes of the CE user data
     * snapshot folders.
     */
    @GuardedBy("rollback.mLock")
    // TODO(b/136241838): Move into Rollback and synchronize there.
    public void snapshotAppData(
            int rollbackId, PackageRollbackInfo packageRollbackInfo, int[] userIds) {
        for (int user : userIds) {
@@ -87,8 +84,6 @@ public class AppDataRollbackHelper {
     *         to {@code packageRollbackInfo} are restricted to the removal or addition of {@code
     *         userId} to the list of pending backups or restores.
     */
    @GuardedBy("rollback.mLock")
    // TODO(b/136241838): Move into Rollback and synchronize there.
    public boolean restoreAppData(int rollbackId, PackageRollbackInfo packageRollbackInfo,
            int userId, int appId, String seInfo) {
        int storageFlags = Installer.FLAG_STORAGE_DE;
@@ -191,8 +186,6 @@ public class AppDataRollbackHelper {
     * Deletes an app data snapshot with a given {@code rollbackId} for a specified package
     * {@code packageName} for a given {@code user}.
     */
    @GuardedBy("rollback.mLock")
    // TODO(b/136241838): Move into Rollback and synchronize there.
    public void destroyAppDataSnapshot(int rollbackId, PackageRollbackInfo packageRollbackInfo,
            int user) {
        try {
@@ -219,7 +212,6 @@ public class AppDataRollbackHelper {
     *
     * @return true if any backups or restores were found for the userId
     */
    @GuardedBy("rollback.mLock")
    boolean commitPendingBackupAndRestoreForUser(int userId, Rollback rollback) {
        boolean foundBackupOrRestore = false;
        for (PackageRollbackInfo info : rollback.info.getPackages()) {
+263 −333
Original line number Diff line number Diff line
@@ -45,7 +45,6 @@ import android.text.TextUtils;
import android.util.Slog;
import android.util.SparseIntArray;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.ArrayUtils;
import com.android.internal.util.IndentingPrintWriter;
@@ -137,20 +136,17 @@ class Rollback {
     * The timestamp is not applicable for all rollback states, but we make
     * sure to keep it non-null to avoid potential errors there.
     */
    @GuardedBy("mLock")
    private @NonNull Instant mTimestamp;

    /**
     * The current state of the rollback.
     * ENABLING, AVAILABLE, or COMMITTED.
     */
    @GuardedBy("mLock")
    private @RollbackState int mState;

    /**
     * The id of the post-reboot apk session for a staged install, if any.
     */
    @GuardedBy("mLock")
    private int mApkSessionId = -1;

    /**
@@ -158,14 +154,8 @@ class Rollback {
     * for this rollback because it has just been committed but the rollback
     * has not yet been fully applied.
     */
    @GuardedBy("mLock")
    private boolean mRestoreUserDataInProgress = false;

    /**
     * Lock object to guard all access to Rollback state.
     */
    private final Object mLock = new Object();

    /**
     * The user that performed the install with rollback enabled.
     */
@@ -192,7 +182,6 @@ class Rollback {
     * {@link PackageInstaller.SessionCallback#onFinished(int, boolean)}.
     * This rollback will be enabled only after all child sessions finished with success.
     */
    @GuardedBy("mLock")
    private int mNumPackageSessionsWithSuccess;

    /**
@@ -292,10 +281,8 @@ class Rollback {
    @WorkerThread
    Instant getTimestamp() {
        assertInWorkerThread();
        synchronized (mLock) {
        return mTimestamp;
    }
    }

    /**
     * Sets the time at which upgrade occurred.
@@ -303,11 +290,9 @@ class Rollback {
    @WorkerThread
    void setTimestamp(Instant timestamp) {
        assertInWorkerThread();
        synchronized (mLock) {
        mTimestamp = timestamp;
        RollbackStore.saveRollback(this);
    }
    }

    /**
     * Returns the session ID for the staged session if this rollback data represents a staged
@@ -350,10 +335,8 @@ class Rollback {
    @WorkerThread
    boolean isEnabling() {
        assertInWorkerThread();
        synchronized (mLock) {
        return mState == ROLLBACK_STATE_ENABLING;
    }
    }

    /**
     * Returns true if the rollback is in the AVAILABLE state.
@@ -361,10 +344,8 @@ class Rollback {
    @WorkerThread
    boolean isAvailable() {
        assertInWorkerThread();
        synchronized (mLock) {
        return mState == ROLLBACK_STATE_AVAILABLE;
    }
    }

    /**
     * Returns true if the rollback is in the COMMITTED state.
@@ -372,10 +353,8 @@ class Rollback {
    @WorkerThread
    boolean isCommitted() {
        assertInWorkerThread();
        synchronized (mLock) {
        return mState == ROLLBACK_STATE_COMMITTED;
    }
    }

    /**
     * Returns true if the rollback is in the DELETED state.
@@ -383,10 +362,8 @@ class Rollback {
    @WorkerThread
    boolean isDeleted() {
        assertInWorkerThread();
        synchronized (mLock) {
        return mState == ROLLBACK_STATE_DELETED;
    }
    }

    /**
     * Saves this rollback to persistent storage.
@@ -394,10 +371,8 @@ class Rollback {
    @WorkerThread
    void saveRollback() {
        assertInWorkerThread();
        synchronized (mLock) {
        RollbackStore.saveRollback(this);
    }
    }

    /**
     * Enables this rollback for the provided package.
@@ -426,10 +401,7 @@ class Rollback {
                new ArrayList<>() /* pendingBackups */, new ArrayList<>() /* pendingRestores */,
                isApex, false /* isApkInApex */, new ArrayList<>(), rollbackDataPolicy);

        synchronized (mLock) {
        info.getPackages().add(packageRollbackInfo);
        }

        return true;
    }

@@ -452,9 +424,7 @@ class Rollback {
                new VersionedPackage(packageName, installedVersion),
                new ArrayList<>() /* pendingBackups */, new ArrayList<>() /* pendingRestores */,
                false /* isApex */, true /* isApkInApex */, new ArrayList<>(), rollbackDataPolicy);
        synchronized (mLock) {
        info.getPackages().add(packageRollbackInfo);
        }
        return true;
    }

@@ -471,7 +441,6 @@ class Rollback {
    @WorkerThread
    void snapshotUserData(String packageName, int[] userIds, AppDataRollbackHelper dataHelper) {
        assertInWorkerThread();
        synchronized (mLock) {
        if (!isEnabling()) {
            return;
        }
@@ -488,7 +457,6 @@ class Rollback {
            }
        }
    }
    }

    /**
     * Commits the pending backups and restores for a given {@code userId}. If this rollback has a
@@ -498,12 +466,10 @@ class Rollback {
    @WorkerThread
    void commitPendingBackupAndRestoreForUser(int userId, AppDataRollbackHelper dataHelper) {
        assertInWorkerThread();
        synchronized (mLock) {
        if (dataHelper.commitPendingBackupAndRestoreForUser(userId, this)) {
            RollbackStore.saveRollback(this);
        }
    }
    }

    /**
     * Changes the state of the rollback to AVAILABLE. This also changes the timestamp to the
@@ -513,7 +479,6 @@ class Rollback {
    @WorkerThread
    void makeAvailable() {
        assertInWorkerThread();
        synchronized (mLock) {
        if (isDeleted()) {
            Slog.w(TAG, "Cannot make deleted rollback available.");
            return;
@@ -522,7 +487,6 @@ class Rollback {
        mTimestamp = Instant.now();
        RollbackStore.saveRollback(this);
    }
    }

    /**
     * Commits the rollback.
@@ -531,7 +495,6 @@ class Rollback {
    void commit(final Context context, List<VersionedPackage> causePackages,
            String callerPackageName, IntentSender statusReceiver) {
        assertInWorkerThread();
        synchronized (mLock) {
        if (!isAvailable()) {
            sendFailure(context, statusReceiver,
                    RollbackManager.STATUS_FAILURE_ROLLBACK_UNAVAILABLE,
@@ -640,11 +603,9 @@ class Rollback {
                        // Why would we expect commit not to fail again?
                        // TODO: Could this cause a rollback to be resurrected
                        // if it should otherwise have expired by now?
                            synchronized (mLock) {
                        mState = ROLLBACK_STATE_AVAILABLE;
                        mRestoreUserDataInProgress = false;
                        info.setCommittedSessionId(-1);
                            }
                        sendFailure(context, statusReceiver,
                                RollbackManager.STATUS_FAILURE_INSTALL,
                                "Rollback downgrade install failed: "
@@ -653,7 +614,6 @@ class Rollback {
                        return;
                    }

                        synchronized (mLock) {
                    if (!isStaged()) {
                        // All calls to restoreUserData should have
                        // completed by now for a non-staged install.
@@ -663,7 +623,6 @@ class Rollback {
                    info.getCausePackages().addAll(causePackages);
                    RollbackStore.deletePackageCodePaths(this);
                    RollbackStore.saveRollback(this);
                        }

                    // Send success.
                    try {
@@ -698,7 +657,6 @@ class Rollback {
                    "IOException: " + e.toString());
        }
    }
    }

    /**
     * Restores user data for the specified package if this rollback is currently marked as
@@ -711,7 +669,6 @@ class Rollback {
    boolean restoreUserDataForPackageIfInProgress(String packageName, int[] userIds, int appId,
            String seInfo, AppDataRollbackHelper dataHelper) {
        assertInWorkerThread();
        synchronized (mLock) {
        if (!isRestoreUserDataInProgress()) {
            return false;
        }
@@ -734,7 +691,6 @@ class Rollback {
        }
        return foundPackage;
    }
    }

    /**
     * Deletes app data snapshots associated with this rollback, and moves to the DELETED state.
@@ -742,7 +698,6 @@ class Rollback {
    @WorkerThread
    void delete(AppDataRollbackHelper dataHelper) {
        assertInWorkerThread();
        synchronized (mLock) {
        boolean containsApex = false;
        for (PackageRollbackInfo pkgInfo : info.getPackages()) {
            if (pkgInfo.isApex()) {
@@ -764,7 +719,6 @@ class Rollback {
        RollbackStore.deleteRollback(this);
        mState = ROLLBACK_STATE_DELETED;
    }
    }

    /**
     * Returns the id of the post-reboot apk session for a staged install, if any.
@@ -772,10 +726,8 @@ class Rollback {
    @WorkerThread
    int getApkSessionId() {
        assertInWorkerThread();
        synchronized (mLock) {
        return mApkSessionId;
    }
    }

    /**
     * Sets the id of the post-reboot apk session for a staged install.
@@ -783,11 +735,9 @@ class Rollback {
    @WorkerThread
    void setApkSessionId(int apkSessionId) {
        assertInWorkerThread();
        synchronized (mLock) {
        mApkSessionId = apkSessionId;
        RollbackStore.saveRollback(this);
    }
    }

    /**
     * Returns true if we are expecting the package manager to call restoreUserData for this
@@ -796,10 +746,8 @@ class Rollback {
    @WorkerThread
    boolean isRestoreUserDataInProgress() {
        assertInWorkerThread();
        synchronized (mLock) {
        return mRestoreUserDataInProgress;
    }
    }

    /**
     * Sets whether we are expecting the package manager to call restoreUserData for this
@@ -808,11 +756,9 @@ class Rollback {
    @WorkerThread
    void setRestoreUserDataInProgress(boolean restoreUserDataInProgress) {
        assertInWorkerThread();
        synchronized (mLock) {
        mRestoreUserDataInProgress = restoreUserDataInProgress;
        RollbackStore.saveRollback(this);
    }
    }

    /**
     * Returns true if this rollback includes the package with the provided {@code packageName}.
@@ -820,7 +766,6 @@ class Rollback {
    @WorkerThread
    boolean includesPackage(String packageName) {
        assertInWorkerThread();
        synchronized (mLock) {
        for (PackageRollbackInfo packageRollbackInfo : info.getPackages()) {
            if (packageRollbackInfo.getPackageName().equals(packageName)) {
                return true;
@@ -828,7 +773,6 @@ class Rollback {
        }
        return false;
    }
    }

    /**
     * Returns true if this rollback includes the package with the provided {@code packageName}
@@ -837,7 +781,6 @@ class Rollback {
    @WorkerThread
    boolean includesPackageWithDifferentVersion(String packageName, long versionCode) {
        assertInWorkerThread();
        synchronized (mLock) {
        for (PackageRollbackInfo pkgRollbackInfo : info.getPackages()) {
            if (pkgRollbackInfo.getPackageName().equals(packageName)
                    && pkgRollbackInfo.getVersionRolledBackFrom().getLongVersionCode()
@@ -847,7 +790,6 @@ class Rollback {
        }
        return false;
    }
    }

    /**
     * Returns a list containing the names of all the packages included in this rollback.
@@ -855,14 +797,12 @@ class Rollback {
    @WorkerThread
    List<String> getPackageNames() {
        assertInWorkerThread();
        synchronized (mLock) {
        List<String> result = new ArrayList<>();
        for (PackageRollbackInfo pkgRollbackInfo : info.getPackages()) {
            result.add(pkgRollbackInfo.getPackageName());
        }
        return result;
    }
    }

    /**
     * Returns a list containing the names of all the apex packages included in this rollback.
@@ -870,7 +810,6 @@ class Rollback {
    @WorkerThread
    List<String> getApexPackageNames() {
        assertInWorkerThread();
        synchronized (mLock) {
        List<String> result = new ArrayList<>();
        for (PackageRollbackInfo pkgRollbackInfo : info.getPackages()) {
            if (pkgRollbackInfo.isApex()) {
@@ -879,7 +818,6 @@ class Rollback {
        }
        return result;
    }
    }

    /**
     * Returns true if this rollback contains the provided {@code packageSessionId}.
@@ -902,10 +840,8 @@ class Rollback {
    @WorkerThread
    boolean notifySessionWithSuccess() {
        assertInWorkerThread();
        synchronized (mLock) {
        return ++mNumPackageSessionsWithSuccess == mPackageSessionIds.length;
    }
    }

    /**
     * Returns true if all packages in this rollback are enabled. We won't enable this rollback
@@ -915,7 +851,6 @@ class Rollback {
    @WorkerThread
    boolean allPackagesEnabled() {
        assertInWorkerThread();
        synchronized (mLock) {
        int packagesWithoutApkInApex = 0;
        for (PackageRollbackInfo rollbackInfo : info.getPackages()) {
            if (!rollbackInfo.isApkInApex()) {
@@ -924,7 +859,6 @@ class Rollback {
        }
        return packagesWithoutApkInApex == mPackageSessionIds.length;
    }
    }

    @AnyThread
    static String rollbackStateToString(@RollbackState int state) {
@@ -950,10 +884,8 @@ class Rollback {
    @WorkerThread
    String getStateAsString() {
        assertInWorkerThread();
        synchronized (mLock) {
        return rollbackStateToString(mState);
    }
    }

    /**
     * Returns true if there is an app installed that specifies a minExtensionVersion greater
@@ -1010,7 +942,6 @@ class Rollback {
    @WorkerThread
    void dump(IndentingPrintWriter ipw) {
        assertInWorkerThread();
        synchronized (mLock) {
        ipw.println(info.getRollbackId() + ":");
        ipw.increaseIndent();
        ipw.println("-state: " + getStateAsString());
@@ -1044,4 +975,3 @@ class Rollback {
        ipw.decreaseIndent();
    }
}
}
+0 −2
Original line number Diff line number Diff line
@@ -28,7 +28,6 @@ import android.os.UserHandle;
import android.util.Slog;
import android.util.SparseIntArray;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;

import libcore.io.IoUtils;
@@ -259,7 +258,6 @@ class RollbackStore {
    /**
     * Saves the given rollback to persistent storage.
     */
    @GuardedBy("rollback.mLock")
    static void saveRollback(Rollback rollback) {
        try {
            JSONObject dataJson = new JSONObject();