From f5373dc281453fe5262ff0527d4103686cd09b06 Mon Sep 17 00:00:00 2001 From: Gaston Thea Date: Fri, 29 Nov 2024 15:46:38 -0300 Subject: [PATCH] WIP integration --- .../android/client/SplitFactoryHelper.java | 18 +------- .../android/client/SplitFactoryImpl.java | 43 ++++++++++--------- .../storage/cipher/ApplyCipherTask.java | 3 +- .../android/client/SplitFactoryHelperTest.kt | 16 ------- 4 files changed, 25 insertions(+), 55 deletions(-) diff --git a/src/main/java/io/split/android/client/SplitFactoryHelper.java b/src/main/java/io/split/android/client/SplitFactoryHelper.java index 3757b633c..6889fcc79 100644 --- a/src/main/java/io/split/android/client/SplitFactoryHelper.java +++ b/src/main/java/io/split/android/client/SplitFactoryHelper.java @@ -27,7 +27,6 @@ import io.split.android.client.network.SplitHttpHeadersBuilder; import io.split.android.client.service.ServiceFactory; import io.split.android.client.service.SplitApiFacade; -import io.split.android.client.service.executor.SplitTaskExecutionListener; import io.split.android.client.service.executor.SplitTaskExecutor; import io.split.android.client.service.executor.SplitTaskFactory; import io.split.android.client.service.http.mysegments.MySegmentsFetcherFactory; @@ -70,7 +69,6 @@ import io.split.android.client.shared.ClientComponentsRegisterImpl; import io.split.android.client.shared.UserConsent; import io.split.android.client.storage.attributes.PersistentAttributesStorage; -import io.split.android.client.storage.cipher.EncryptionMigrationTask; import io.split.android.client.storage.cipher.SplitCipher; import io.split.android.client.storage.cipher.SplitCipherFactory; import io.split.android.client.storage.cipher.SplitEncryptionLevel; @@ -399,21 +397,9 @@ public ProcessStrategy getImpressionStrategy(SplitTaskExecutor splitTaskExecutor .getStrategy(config.impressionsMode()); } - SplitCipher migrateEncryption(String apiKey, - SplitRoomDatabase splitDatabase, - SplitTaskExecutor splitTaskExecutor, - final boolean encryptionEnabled, - SplitTaskExecutionListener executionListener) { - - SplitCipher toCipher = SplitCipherFactory.create(apiKey, encryptionEnabled ? SplitEncryptionLevel.AES_128_CBC : + @Nullable SplitCipher getCipher(String apiKey, boolean encryptionEnabled) { + return SplitCipherFactory.create(apiKey, encryptionEnabled ? SplitEncryptionLevel.AES_128_CBC : SplitEncryptionLevel.NONE); - splitTaskExecutor.submit(new EncryptionMigrationTask(apiKey, - splitDatabase, - encryptionEnabled, - toCipher), - executionListener); - - return toCipher; } @Nullable diff --git a/src/main/java/io/split/android/client/SplitFactoryImpl.java b/src/main/java/io/split/android/client/SplitFactoryImpl.java index b5c0252e3..fb4cfaabe 100644 --- a/src/main/java/io/split/android/client/SplitFactoryImpl.java +++ b/src/main/java/io/split/android/client/SplitFactoryImpl.java @@ -29,8 +29,6 @@ import io.split.android.client.network.HttpClientImpl; import io.split.android.client.service.SplitApiFacade; import io.split.android.client.service.executor.SplitSingleThreadTaskExecutor; -import io.split.android.client.service.executor.SplitTaskExecutionInfo; -import io.split.android.client.service.executor.SplitTaskExecutionListener; import io.split.android.client.service.executor.SplitTaskExecutor; import io.split.android.client.service.executor.SplitTaskExecutorImpl; import io.split.android.client.service.executor.SplitTaskFactory; @@ -38,6 +36,7 @@ import io.split.android.client.service.impressions.ImpressionManager; import io.split.android.client.service.impressions.StrategyImpressionManager; import io.split.android.client.service.sseclient.sseclient.StreamingComponents; +import io.split.android.client.service.synchronizer.RolloutCacheManagerImpl; import io.split.android.client.service.synchronizer.SyncManager; import io.split.android.client.service.synchronizer.Synchronizer; import io.split.android.client.service.synchronizer.SynchronizerImpl; @@ -49,6 +48,7 @@ import io.split.android.client.shared.SplitClientContainer; import io.split.android.client.shared.SplitClientContainerImpl; import io.split.android.client.shared.UserConsent; +import io.split.android.client.storage.cipher.EncryptionMigrationTask; import io.split.android.client.storage.cipher.SplitCipher; import io.split.android.client.storage.common.SplitStorageContainer; import io.split.android.client.storage.db.SplitRoomDatabase; @@ -84,9 +84,6 @@ public class SplitFactoryImpl implements SplitFactory { private final SplitClientContainer mClientContainer; private final UserConsentManager mUserConsentManager; - @SuppressWarnings("FieldCanBeLocal") // keeping the reference on purpose - private final SplitTaskExecutionListener mMigrationExecutionListener; - public SplitFactoryImpl(@NonNull String apiToken, @NonNull Key key, @NonNull SplitClientConfig config, @NonNull Context context) throws URISyntaxException { this(apiToken, key, config, context, @@ -163,20 +160,11 @@ private SplitFactoryImpl(@NonNull String apiToken, @NonNull Key key, @NonNull Sp defaultHttpClient.addStreamingHeaders(factoryHelper.buildStreamingHeaders(apiToken)); SplitTaskExecutor splitTaskExecutor = new SplitTaskExecutorImpl(); + splitTaskExecutor.pause(); EventsManagerCoordinator mEventsManagerCoordinator = new EventsManagerCoordinator(); - mMigrationExecutionListener = new SplitTaskExecutionListener() { - @Override - public void taskExecuted(@NonNull SplitTaskExecutionInfo taskInfo) { - mEventsManagerCoordinator.notifyInternalEvent(SplitInternalEvent.ENCRYPTION_MIGRATION_DONE); - } - }; - SplitCipher splitCipher = factoryHelper.migrateEncryption(mApiKey, - splitDatabase, - splitTaskExecutor, - config.encryptionEnabled(), - mMigrationExecutionListener); + SplitCipher splitCipher = factoryHelper.getCipher(apiToken, config.encryptionEnabled()); ScheduledThreadPoolExecutor impressionsObserverExecutor = new ScheduledThreadPoolExecutor(1, new ThreadPoolExecutor.CallerRunsPolicy()); @@ -200,6 +188,7 @@ public void taskExecuted(@NonNull SplitTaskExecutionInfo taskInfo) { cleanUpDabase(splitTaskExecutor, splitTaskFactory); WorkManagerWrapper workManagerWrapper = factoryHelper.buildWorkManagerWrapper(context, config, apiToken, databaseName, filters); SplitSingleThreadTaskExecutor splitSingleThreadTaskExecutor = new SplitSingleThreadTaskExecutor(); + splitSingleThreadTaskExecutor.pause(); ImpressionManager impressionManager = new StrategyImpressionManager(factoryHelper.getImpressionStrategy(splitTaskExecutor, splitTaskFactory, mStorageContainer, config)); final RetryBackoffCounterTimerFactory retryBackoffCounterTimerFactory = new RetryBackoffCounterTimerFactory(); @@ -254,8 +243,6 @@ public void taskExecuted(@NonNull SplitTaskExecutionInfo taskInfo) { mLifecycleManager = testLifecycleManager; } - mLifecycleManager.register(mSyncManager); - ExecutorService impressionsLoggingTaskExecutor = factoryHelper.getImpressionsLoggingTaskExecutor(); final ImpressionListener splitImpressionListener = new SyncImpressionListener(mSyncManager, impressionsLoggingTaskExecutor); @@ -335,14 +322,28 @@ public void run() { mStorageContainer.getSplitsStorage(), new SplitValidatorImpl(), mSplitParser); - mSyncManager.start(); + Runnable initializer = new Runnable() { + @Override + public void run() { + Logger.v("Running initialization thread"); + new RolloutCacheManagerImpl(config, splitTaskExecutor, mStorageContainer).execute(); + new EncryptionMigrationTask(apiToken, splitDatabase, config.encryptionEnabled(), splitCipher).execute(); + mEventsManagerCoordinator.notifyInternalEvent(SplitInternalEvent.ENCRYPTION_MIGRATION_DONE); + splitTaskExecutor.resume(); + splitSingleThreadTaskExecutor.resume(); + mSyncManager.start(); + mLifecycleManager.register(mSyncManager); + + Logger.i("Android SDK initialized!"); + } + }; + new Thread(initializer).start(); + if (config.shouldRecordTelemetry()) { int activeFactoriesCount = mFactoryMonitor.count(mApiKey); mStorageContainer.getTelemetryStorage().recordActiveFactories(activeFactoriesCount); mStorageContainer.getTelemetryStorage().recordRedundantFactories(activeFactoriesCount - 1); } - - Logger.i("Android SDK initialized!"); } private static String getFlagsSpec(@Nullable TestingConfig testingConfig) { diff --git a/src/main/java/io/split/android/client/storage/cipher/ApplyCipherTask.java b/src/main/java/io/split/android/client/storage/cipher/ApplyCipherTask.java index adb47d63a..e1e5928a1 100644 --- a/src/main/java/io/split/android/client/storage/cipher/ApplyCipherTask.java +++ b/src/main/java/io/split/android/client/storage/cipher/ApplyCipherTask.java @@ -49,6 +49,7 @@ public SplitTaskExecutionInfo execute() { mSplitDatabase.runInTransaction(new Runnable() { @Override public void run() { + updateAttributes(mSplitDatabase.attributesDao()); updateSplits(mSplitDatabase.splitDao()); updateSegments(mSplitDatabase.mySegmentDao()); updateLargeSegments(mSplitDatabase.myLargeSegmentDao()); @@ -56,7 +57,6 @@ public void run() { updateEvents(mSplitDatabase.eventDao()); updateImpressionsCount(mSplitDatabase.impressionsCountDao()); updateUniqueKeys(mSplitDatabase.uniqueKeysDao()); - updateAttributes(mSplitDatabase.attributesDao()); } }); @@ -87,7 +87,6 @@ private void updateAttributes(AttributesDao attributesDao) { private void updateUniqueKeys(UniqueKeysDao uniqueKeysDao) { List items = uniqueKeysDao.getAll(); - for (UniqueKeyEntity item : items) { String fromUserKey = mFromCipher.decrypt(item.getUserKey()); String fromFeatureList = mFromCipher.decrypt(item.getFeatureList()); diff --git a/src/test/java/io/split/android/client/SplitFactoryHelperTest.kt b/src/test/java/io/split/android/client/SplitFactoryHelperTest.kt index 7c123c8dd..acda1af2a 100644 --- a/src/test/java/io/split/android/client/SplitFactoryHelperTest.kt +++ b/src/test/java/io/split/android/client/SplitFactoryHelperTest.kt @@ -46,22 +46,6 @@ class SplitFactoryHelperTest { mocks.close() } - @Test - fun migrateEncryption() { - - helper.migrateEncryption( - "abcdedfghijklmnopqrstuvwxyz", - splitRoomDatabase, - splitTaskExecutor, - true, - taskListener, - ) - - verify(splitTaskExecutor).submit( - argThat { it is EncryptionMigrationTask }, - argThat { it?.equals(taskListener) == true }) - } - @Test fun generateDatabaseNameWithoutPrefixAndKeyLongerThan4() { val path = mock(File::class.java)