Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Added notificationRegistry to make sure that odpSettings updates #501

Merged
merged 20 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions core-api/src/main/java/com/optimizely/ab/Optimizely.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.optimizely.ab.event.*;
import com.optimizely.ab.event.internal.*;
import com.optimizely.ab.event.internal.payload.EventBatch;
import com.optimizely.ab.internal.NotificationRegistry;
import com.optimizely.ab.notification.*;
import com.optimizely.ab.odp.*;
import com.optimizely.ab.optimizelyconfig.OptimizelyConfig;
Expand Down Expand Up @@ -124,10 +125,15 @@ private Optimizely(@Nonnull EventHandler eventHandler,

if (odpManager != null) {
odpManager.getEventManager().start();
if (getProjectConfig() != null) {
if (projectConfigManager.getCachedConfig() != null) {
updateODPSettings();
}
addUpdateConfigNotificationHandler(configNotification -> { updateODPSettings(); });
if (projectConfigManager.getSDKKey() != null) {
NotificationRegistry.getInternalNotificationCenter(projectConfigManager.getSDKKey()).
addNotificationHandler(UpdateConfigNotification.class,
configNotification -> { updateODPSettings(); });
}

}
}

Expand All @@ -153,6 +159,8 @@ public void close() {
tryClose(eventProcessor);
tryClose(eventHandler);
tryClose(projectConfigManager);
notificationCenter.clearAllNotificationListeners();
NotificationRegistry.clearNotificationCenterRegistry(projectConfigManager.getSDKKey());
if (odpManager != null) {
tryClose(odpManager);
}
Expand Down Expand Up @@ -1477,8 +1485,8 @@ public void identifyUser(@Nonnull String userId) {
}

private void updateODPSettings() {
if (odpManager != null && getProjectConfig() != null) {
ProjectConfig projectConfig = getProjectConfig();
ProjectConfig projectConfig = projectConfigManager.getCachedConfig();
if (odpManager != null && projectConfig != null) {
odpManager.updateSettings(projectConfig.getHostForODP(), projectConfig.getPublicKeyForODP(), projectConfig.getAllSegments());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2019, Optimizely and contributors
* Copyright 2019, 2023, Optimizely and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -27,6 +27,21 @@ public ProjectConfig getConfig() {
return projectConfigReference.get();
}

/**
* Access to current cached project configuration.
*
* @return {@link ProjectConfig}
*/
@Override
public ProjectConfig getCachedConfig() {
return projectConfigReference.get();
}

@Override
public String getSDKKey() {
return null;
}

public void setConfig(ProjectConfig projectConfig) {
projectConfigReference.set(projectConfig);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2019-2020, Optimizely and contributors
* Copyright 2019-2020, 2023, Optimizely and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@
*/
package com.optimizely.ab.config;

import com.optimizely.ab.internal.NotificationRegistry;
import com.optimizely.ab.notification.NotificationCenter;
import com.optimizely.ab.notification.UpdateConfigNotification;
import com.optimizely.ab.optimizelyconfig.OptimizelyConfig;
Expand Down Expand Up @@ -56,6 +57,7 @@ public abstract class PollingProjectConfigManager implements ProjectConfigManage

private final CountDownLatch countDownLatch = new CountDownLatch(1);

private volatile String sdkKey;
private volatile boolean started;
private ScheduledFuture<?> scheduledFuture;

Expand Down Expand Up @@ -84,6 +86,16 @@ public PollingProjectConfigManager(long period, TimeUnit timeUnit, long blocking

protected abstract ProjectConfig poll();

/**
* Access to current cached project configuration, This is to make sure that config returns without any wait, even if it is null.
*
* @return {@link ProjectConfig}
*/
@Override
public ProjectConfig getCachedConfig() {
return currentProjectConfig.get();
}

/**
* Only allow the ProjectConfig to be set to a non-null value, if and only if the value has not already been set.
* @param projectConfig
Expand All @@ -109,6 +121,13 @@ void setConfig(ProjectConfig projectConfig) {
currentProjectConfig.set(projectConfig);
currentOptimizelyConfig.set(new OptimizelyConfigService(projectConfig).getConfig());
countDownLatch.countDown();

if (sdkKey == null) {
sdkKey = projectConfig.getSdkKey();
}
if (sdkKey != null) {
NotificationRegistry.getInternalNotificationCenter(sdkKey).send(SIGNAL);
}
notificationCenter.send(SIGNAL);
}

Expand Down Expand Up @@ -150,6 +169,11 @@ public OptimizelyConfig getOptimizelyConfig() {
return currentOptimizelyConfig.get();
}

@Override
public String getSDKKey() {
return this.sdkKey;
}

public synchronized void start() {
if (started) {
logger.warn("Manager already started.");
Expand Down Expand Up @@ -189,6 +213,10 @@ public synchronized void close() {
started = false;
}

protected void setSdkKey(String sdkKey) {
this.sdkKey = sdkKey;
}

mnoman09 marked this conversation as resolved.
Show resolved Hide resolved
public boolean isRunning() {
return started;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2019, Optimizely and contributors
* Copyright 2019, 2023, Optimizely and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,12 +16,33 @@
*/
package com.optimizely.ab.config;

import javax.annotation.Nullable;

public interface ProjectConfigManager {
/**
* Implementations of this method should block until a datafile is available.
*
* @return ProjectConfig
*/
ProjectConfig getConfig();

/**
* Implementations of this method should not block until a datafile is available, instead return current cached project configuration.
* return null if ProjectConfig is not ready at the moment.
*
mnoman09 marked this conversation as resolved.
Show resolved Hide resolved
* NOTE: To use ODP segments, implementation of this function is required to return current project configuration.
* @return ProjectConfig
*/
@Nullable
ProjectConfig getCachedConfig();

/**
* Implementations of this method should return SDK key. If there is no SDKKey then it should return null.
*
* NOTE: To update ODP segments configuration via polling, it is required to return sdkKey.
* @return String
*/
@Nullable
String getSDKKey();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get sdkKey via getConfig().getSDKKey() instead of adding the new inferface?
This will be convenient but we want to avoid any breaking changes with this new method here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see this comment. It will make sure that even when the config is not fetched, notificationCenter will contain the handler, which updates the ODPConfig.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mnoman09 Got it! Can we add a default implementation of this method (returning null), so no changes required for existing implementations. They'll all share the same internal notificationCenter for "null" sdkKey (until they fix it), which looks safe to me.
I believe the default interface method is supported in Java8 and also supported in android.

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
*
* Copyright 2023, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.optimizely.ab.internal;

import com.optimizely.ab.notification.NotificationCenter;

import javax.annotation.Nonnull;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class NotificationRegistry {
private final static Map<String, NotificationCenter> _notificationCenters = new ConcurrentHashMap<>();

private NotificationRegistry()
{
}

public static NotificationCenter getInternalNotificationCenter(@Nonnull String sdkKey)
{
NotificationCenter notificationCenter = null;
if (sdkKey != null) {
if (_notificationCenters.containsKey(sdkKey)) {
notificationCenter = _notificationCenters.get(sdkKey);
} else {
notificationCenter = new NotificationCenter();
_notificationCenters.put(sdkKey, notificationCenter);
}
}
return notificationCenter;
}

public static void clearNotificationCenterRegistry(@Nonnull String sdkKey) {
if (sdkKey != null) {
_notificationCenters.remove(sdkKey);
}
}

}
47 changes: 23 additions & 24 deletions core-api/src/test/java/com/optimizely/ab/OptimizelyTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2016-2022, Optimizely, Inc. and contributors *
* Copyright 2016-2023, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand Down Expand Up @@ -119,6 +119,23 @@ public static Collection<Object[]> data() throws IOException {
public OptimizelyRule optimizelyBuilder = new OptimizelyRule();
public EventHandlerRule eventHandler = new EventHandlerRule();

public ProjectConfigManager projectConfigManagerReturningNull = new ProjectConfigManager() {
@Override
public ProjectConfig getConfig() {
return null;
}

@Override
public ProjectConfig getCachedConfig() {
return null;
}

@Override
public String getSDKKey() {
return null;
}
};

@Rule
@SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
public RuleChain ruleChain = RuleChain.outerRule(thrown)
Expand Down Expand Up @@ -4505,13 +4522,13 @@ public void isValidReturnsTrueWhenClientIsValid() throws Exception {

@Test
public void testGetNotificationCenter() {
Optimizely optimizely = optimizelyBuilder.withConfigManager(() -> null).build();
Optimizely optimizely = optimizelyBuilder.withConfigManager(projectConfigManagerReturningNull).build();
assertEquals(optimizely.notificationCenter, optimizely.getNotificationCenter());
}

@Test
public void testAddTrackNotificationHandler() {
Optimizely optimizely = optimizelyBuilder.withConfigManager(() -> null).build();
Optimizely optimizely = optimizelyBuilder.withConfigManager(projectConfigManagerReturningNull).build();
NotificationManager<TrackNotification> manager = optimizely.getNotificationCenter()
.getNotificationManager(TrackNotification.class);

Expand All @@ -4521,7 +4538,7 @@ public void testAddTrackNotificationHandler() {

@Test
public void testAddDecisionNotificationHandler() {
Optimizely optimizely = optimizelyBuilder.withConfigManager(() -> null).build();
Optimizely optimizely = optimizelyBuilder.withConfigManager(projectConfigManagerReturningNull).build();
NotificationManager<DecisionNotification> manager = optimizely.getNotificationCenter()
.getNotificationManager(DecisionNotification.class);

Expand All @@ -4531,7 +4548,7 @@ public void testAddDecisionNotificationHandler() {

@Test
public void testAddUpdateConfigNotificationHandler() {
Optimizely optimizely = optimizelyBuilder.withConfigManager(() -> null).build();
Optimizely optimizely = optimizelyBuilder.withConfigManager(projectConfigManagerReturningNull).build();
NotificationManager<UpdateConfigNotification> manager = optimizely.getNotificationCenter()
.getNotificationManager(UpdateConfigNotification.class);

Expand All @@ -4541,7 +4558,7 @@ public void testAddUpdateConfigNotificationHandler() {

@Test
public void testAddLogEventNotificationHandler() {
Optimizely optimizely = optimizelyBuilder.withConfigManager(() -> null).build();
Optimizely optimizely = optimizelyBuilder.withConfigManager(projectConfigManagerReturningNull).build();
NotificationManager<LogEvent> manager = optimizely.getNotificationCenter()
.getNotificationManager(LogEvent.class);

Expand Down Expand Up @@ -4713,24 +4730,6 @@ public void initODPManagerWithProjectConfig() throws IOException {
verify(mockODPManager, times(1)).updateSettings(any(), any(), any());
}

@Test
public void updateODPManagerWhenConfigUpdates() throws IOException {
ODPEventManager mockODPEventManager = mock(ODPEventManager.class);
ODPManager mockODPManager = mock(ODPManager.class);
NotificationCenter mockNotificationCenter = mock(NotificationCenter.class);

Mockito.when(mockODPManager.getEventManager()).thenReturn(mockODPEventManager);
Optimizely.builder()
.withDatafile(validConfigJsonV4())
.withNotificationCenter(mockNotificationCenter)
.withODPManager(mockODPManager)
.build();

verify(mockODPManager, times(1)).updateSettings(any(), any(), any());

Mockito.verify(mockNotificationCenter, times(1)).addNotificationHandler(any(), any());
}

@Test
public void sendODPEvent() {
ProjectConfigManager mockProjectConfigManager = mock(ProjectConfigManager.class);
Expand Down
Loading