Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
* Added support for the SDK connection test. When the server asks for it, the SDK checks that it can reach every endpoint it depends on and reports the results.
* Added "setMaxKeyLength(int)" init config method to limit the length of event keys, view names, segmentation keys and custom user property keys. Longer keys are truncated. There is no limit by default, and the key length limit of the SDK behavior settings overrides it.
* Added "disableHealthCheck()" init config method to turn off the health check request.
* Added "changeServerUrl(String)" to "Countly.instance()" to change the server URL after init
* Added "changeAppKey(String)" to "Countly.instance()" to change the app key after init
* Added "flushQueues()", "replaceAllAppKeysInQueueWithCurrentAppKey()", "removeDifferentAppKeysFromQueue()" and "attemptToSendStoredRequests()" to "Countly.instance()" to manage the request queue
* The deprecated "event", "timedEvent", "view" and "addCrashReport" calls of the session interface, and "record" and "endAndRecord" of a legacy event, now log a warning that names the call to use instead.
* Failed requests are now logged at debug level, and wrong uses of the session, event, view and crash methods at warning level instead of error level.

Expand Down
4 changes: 2 additions & 2 deletions sdk-java/src/main/java/ly/count/sdk/java/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
/**
* URL of Countly server
*/
protected URL serverURL;
protected volatile URL serverURL;

/**
* Application key of Countly server
*/
protected String serverAppKey;
protected volatile String serverAppKey;

/**
* Set of Countly SDK features enabled
Expand Down Expand Up @@ -104,12 +104,12 @@
protected int networkReadTimeout = 30;

/**
* How long to wait between requests in milliseconds. Used to decrease CPU & I/O load on the device.

Check warning on line 107 in sdk-java/src/main/java/ly/count/sdk/java/Config.java

View workflow job for this annotation

GitHub Actions / Release checks (26.8.0)

invalid input: '&'
*/
protected int networkRequestCooldown = 1000;

/**
* How long to wait between Device ID change & push token requests, in milliseconds.

Check warning on line 112 in sdk-java/src/main/java/ly/count/sdk/java/Config.java

View workflow job for this annotation

GitHub Actions / Release checks (26.8.0)

invalid input: '&'

Check warning on line 112 in sdk-java/src/main/java/ly/count/sdk/java/Config.java

View workflow job for this annotation

GitHub Actions / Release checks (26.8.0)

invalid input: '&'
* Required by Countly server, don't change unless you know what you're doing!
*/
protected int networkImportantRequestCooldown = 5000;
Expand All @@ -126,7 +126,7 @@
* This ensures pinning continues to work after certificate prolongation.
* Certificates ({@link #certificatePins}) on the other hand have specific expiry date.
* In case you chose this way of pinning, you MUST ensure that ALL installs of your app
* have both certificates (old & new) until expiry date.

Check warning on line 129 in sdk-java/src/main/java/ly/count/sdk/java/Config.java

View workflow job for this annotation

GitHub Actions / Release checks (26.8.0)

invalid input: '&'
*/
protected Set<String> publicKeyPins = null;

Expand All @@ -142,7 +142,7 @@
* This ensures pinning continues to work after certificate prolongation.
* Certificates on the other hand have specific expiry date.
* In case you chose this way of pinning, you MUST ensure that ALL installs of your app
* have both certificates (old & new) until expiry date.

Check warning on line 145 in sdk-java/src/main/java/ly/count/sdk/java/Config.java

View workflow job for this annotation

GitHub Actions / Release checks (26.8.0)

invalid input: '&'
*/
protected Set<String> certificatePins = null;

Expand All @@ -168,7 +168,7 @@
/**
* {@link CrashProcessor}-implementing class which is instantiated when application
* crashes or crash is reported programmatically using {@link Session#addCrashReport(Throwable, boolean, String, Map, String...)}.
* Crash processor helps you to add custom data in event of a crash: custom crash segments & crash logs.

Check warning on line 171 in sdk-java/src/main/java/ly/count/sdk/java/Config.java

View workflow job for this annotation

GitHub Actions / Release checks (26.8.0)

invalid input: '&'
*/
protected String crashProcessorClass = null;

Expand Down Expand Up @@ -718,7 +718,7 @@
}

/**
* Disable update requests completely. Only begin & end requests will be sent + some special

Check warning on line 721 in sdk-java/src/main/java/ly/count/sdk/java/Config.java

View workflow job for this annotation

GitHub Actions / Release checks (26.8.0)

invalid input: '&'
* cases if applicable like User Profile change or Push token updated.
*
* @return {@code this} instance for method chaining
Expand Down Expand Up @@ -816,7 +816,7 @@

/**
* How long to wait between requests in seconds.
* Used to decrease CPU & I/O load on the device in case of batch requests.

Check warning on line 819 in sdk-java/src/main/java/ly/count/sdk/java/Config.java

View workflow job for this annotation

GitHub Actions / Release checks (26.8.0)

invalid input: '&'
*
* @param milliseconds cooldown period in seconds
* @return {@code this} instance for method chaining
Expand All @@ -834,7 +834,7 @@

/**
* Set read timeout in seconds for HTTP requests SDK sends to Countly server. Defaults to 30.
* Used to decrease CPU & I/O load on the device in case of batch requests.

Check warning on line 837 in sdk-java/src/main/java/ly/count/sdk/java/Config.java

View workflow job for this annotation

GitHub Actions / Release checks (26.8.0)

invalid input: '&'
*
* @param milliseconds read timeout in milliseconds
* @return {@code this} instance for method chaining
Expand Down Expand Up @@ -862,7 +862,7 @@
* This ensures pinning continues to work after certificate prolongation.
* Certificates ({@link #certificatePins}) on the other hand have specific expiry date.
* In case you chose this way of pinning, you MUST ensure that ALL installs of your app
* have both certificates (old & new) until expiry date.

Check warning on line 865 in sdk-java/src/main/java/ly/count/sdk/java/Config.java

View workflow job for this annotation

GitHub Actions / Release checks (26.8.0)

invalid input: '&'
*
* NOTE: when {@link #serverURL} doesn't have {@code "https://"} public key pinning doesn't work
*
Expand Down
83 changes: 83 additions & 0 deletions sdk-java/src/main/java/ly/count/sdk/java/Countly.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.File;
import java.util.Arrays;
import java.util.Map;
import java.util.function.Consumer;
import ly.count.sdk.java.internal.Device;
import ly.count.sdk.java.internal.DeviceIdType;
import ly.count.sdk.java.internal.InternalConfig;
Expand Down Expand Up @@ -411,6 +412,88 @@ public static void onConsentRemoval(Config.Feature... features) {
}
}

/**
* Changes the server URL while the SDK runs. Every later request goes to the new URL, queued
* ones included; a request being sent finishes at the old one. The app key, salt and custom
* request headers stay. Then the SDK requests its behavior settings from the new server, and
* remote config when it downloads automatically. Kept in memory only: the next initialization
* uses the URL of the configuration again. An invalid URL is ignored with an error log.
*
* @param serverUrl an absolute http or https URL with a host, https only while certificate or
* public key pins are set; trailing "/" characters are removed
*/
public void changeServerUrl(String serverUrl) {
callIfInitialized("changeServerUrl, serverUrl:[" + serverUrl + "]", sdk -> sdk.changeServerUrl(serverUrl));
}

/**
* Changes the app key while the SDK runs, so the SDK reports to another app. First, under the
* old key, the running views are stopped, the queued events and pending user profile changes
* are stored as requests, and a running session is ended, then begun again under the new key.
* Queued requests keep their key: see {@link #replaceAllAppKeysInQueueWithCurrentAppKey()} and
* {@link #removeDifferentAppKeysFromQueue()}. Then the SDK requests the behavior settings of
* the new app, and its remote config when remote config downloads automatically; the remote
* config values of the old app are cleared, or marked as cached with value caching. Kept in
* memory only: the next initialization uses the key of the configuration again. An invalid key
* is ignored with an error log. The key in use is ignored, and so is any call in backend mode.
*
* @param appKey the new app key, neither empty nor "YOUR_APP_KEY"
*/
public void changeAppKey(String appKey) {
callIfInitialized("changeAppKey, appKey:[" + appKey + "]", sdk -> sdk.changeAppKey(appKey));
}

/**
* Clears the request queue and the event queue: all pending data is discarded. Started timed
* events, running views, pending user profile changes and the health check counters are kept,
* and a request that is being sent is not stopped. Ignored in backend mode.
*/
public void flushQueues() {
callIfInitialized("flushQueues", SDKCore::flushQueues);
}

/**
* Replaces the app key of every queued request with the current app key, so requests queued
* under another key are sent to the current app. Ignored in backend mode.
*/
public void replaceAllAppKeysInQueueWithCurrentAppKey() {
callIfInitialized("replaceAllAppKeysInQueueWithCurrentAppKey", SDKCore::replaceAllAppKeysInQueueWithCurrentAppKey);
}

/**
* Removes every queued request whose app key differs from the current app key, so it is never
* sent. Ignored in backend mode.
*/
public void removeDifferentAppKeysFromQueue() {
callIfInitialized("removeDifferentAppKeysFromQueue", SDKCore::removeDifferentAppKeysFromQueue);
}

/**
* Packs the queued events into a request and starts sending the request queue right away,
* without waiting for the next timer tick. During a backoff pause, or while the SDK behavior
* settings turn networking off, sending waits as usual.
*/
public void attemptToSendStoredRequests() {
callIfInitialized("attemptToSendStoredRequests", SDKCore::attemptToSendStoredRequests);
}

/**
* Logs a call at info level and runs it on the SDK, or only records log code w101 before init.
*
* @param logLine the call and its arguments, as logged
* @param call the call on the SDK
*/
private static void callIfInitialized(String logLine, Consumer<SDKCore> call) {
if (!isInitialized()) {
Log.recordLogCodeWhileNotInitialized("w101");
return;
}
if (cly.L != null) {
cly.L.i("[Countly] " + logLine);
}
call.accept(cly.sdk);
}

/**
* <code>Feedback</code> interface to use feedback widgets nps,rating and forms.
* And do manual reporting of feedbacks.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -238,6 +239,24 @@ public void setLogger(Log logger) {
this.configLog = logger;
}

/**
* Replaces the server URL every later request is sent to, in this configuration only.
*
* @param serverURL the new server URL
*/
void setServerURL(@Nonnull URL serverURL) {
this.serverURL = serverURL;
}

/**
* Replaces the app key every later request carries, in this configuration only.
*
* @param serverAppKey the new app key
*/
void setServerAppKey(@Nonnull String serverAppKey) {
this.serverAppKey = serverAppKey;
}

File getSdkStorageRootDirectory() {
return sdkStorageRootDirectory;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ private synchronized void addEventsAgainstDeviceIdToRequestQ(String deviceID) {
addRequestToRequestQ(request);
}

private synchronized void addEventsToRequestQ() {
synchronized void addEventsToRequestQ() {
L.d("[BackendMode] addEventsToRequestQ");

for (String s : eventQueues.keySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -903,9 +903,11 @@ void fetchConfigFromServer(@Nonnull InternalConfig config) {
Transport transport = config.sdk.networking.getTransport();
final long fetchStartNs = System.nanoTime();
final int requestedAfterDeviceIdChanges = deviceIdChangesWithoutMerge;
final String requestedAppKey = config.getServerAppKey();
final String requestedServerUrl = config.getServerURL().toString();

config.immediateRequestGenerator.createImmediateRequestMaker().doWork(requestData, "/o/sdk?", transport, false, true,
response -> onServerConfigResponse(config, response, fetchStartNs, requestedAfterDeviceIdChanges), L);
response -> onServerConfigResponse(config, response, fetchStartNs, requestedAfterDeviceIdChanges, requestedAppKey, requestedServerUrl), L);
} catch (Exception e) {
L.e("[ModuleConfiguration] fetchConfigFromServer, failed to request the configuration, [" + e + "]");
decideLogGatheringOffIfUndecided("the server config request failed");
Expand All @@ -920,8 +922,11 @@ void fetchConfigFromServer(@Nonnull InternalConfig config) {
* @param response the response, {@code null} when the request failed
* @param fetchStartNs {@link System#nanoTime()} just before the request was made
* @param requestedAfterDeviceIdChanges how many device ID changes without merge happened before the request was made
* @param requestedAppKey the app key the request was made with
* @param requestedServerUrl the server URL the request was made to
*/
private void onServerConfigResponse(@Nonnull InternalConfig config, @Nullable JSONObject response, long fetchStartNs, int requestedAfterDeviceIdChanges) {
private void onServerConfigResponse(@Nonnull InternalConfig config, @Nullable JSONObject response, long fetchStartNs, int requestedAfterDeviceIdChanges,
@Nullable String requestedAppKey, @Nonnull String requestedServerUrl) {
if (!running) {
L.d("[ModuleConfiguration] onServerConfigResponse, the SDK was stopped before the response arrived, ignoring it");
return;
Expand All @@ -933,6 +938,11 @@ private void onServerConfigResponse(@Nonnull InternalConfig config, @Nullable JS
return;
}

if (Utils.isNotEqual(requestedAppKey, config.getServerAppKey()) || !requestedServerUrl.equals(config.getServerURL().toString())) {
L.d("[ModuleConfiguration] onServerConfigResponse, the app key or the server URL changed after the request was made, ignoring its response");
return;
}

L.d("[ModuleConfiguration] onServerConfigResponse, retrieved configuration response: [" + response + "]");
long fetchLatencyMs = (System.nanoTime() - fetchStartNs) / 1_000_000L;
boolean connectionTestRequested = extractConnectionTestFlag(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ private synchronized void addEventsToRequestQ(@Nullable String deviceId, boolean
});
}

/**
* Drops the queued events, unsent; timed events are kept. Takes the lock of moving the queued
* events into a request, so a request they were already moved into is queued first.
*/
synchronized void clearEventQueue() {
eventQueue.clear();
}

protected void recordEventInternal(String key, int count, Double sum, Double dur, Map<String, Object> segmentation, String eventIdOverride) {
ConfigurationProvider configProvider = internalConfig.getConfigurationProvider();
if (!configProvider.getTrackingEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ void updateRemoteConfigValues(@Nullable final String[] keysOnly, @Nullable final

Transport transport = internalConfig.sdk.networking.getTransport();
final boolean networkingIsEnabled = internalConfig.getNetworkingEnabled();
final String requestedAppKey = internalConfig.getServerAppKey();
final String requestedServerUrl = internalConfig.getServerURL().toString();

internalConfig.immediateRequestGenerator.createImmediateRequestMaker().doWork(requestData, "/o/sdk?", transport, false, networkingIsEnabled, checkResponse -> {
L.d("[ModuleRemoteConfig] updateRemoteConfigValues, Processing remote config received response, received response is null:[" + (checkResponse == null) + "]");
Expand All @@ -82,6 +84,12 @@ void updateRemoteConfigValues(@Nullable final String[] keysOnly, @Nullable final
return;
}

if (Utils.isNotEqual(requestedAppKey, internalConfig.getServerAppKey()) || !requestedServerUrl.equals(internalConfig.getServerURL().toString())) {
L.d("[ModuleRemoteConfig] updateRemoteConfigValues, the app key or the server URL changed after the request was made, ignoring its response");
notifyDownloadCallbacks(devProvidedCallback, RequestResult.Error, "The app key or the server URL changed before the response arrived", fullUpdate, null);
return;
}

String error = null;
Map<String, RCData> newRC = downloadedValuesIntoMap(checkResponse);

Expand Down Expand Up @@ -238,7 +246,10 @@ private void saveRCValues(@Nonnull final RemoteConfigValueStore rcvs) {
internalConfig.storageProvider.setRemoteConfigValues(rcvs.values);
}

private void cacheOrClearRCValuesIfNeeded() {
/**
* Clears the stored remote config values, or marks them as cached with value caching.
*/
void cacheOrClearRCValuesIfNeeded() {
L.v("[ModuleRemoteConfig] cacheOrClearRCValuesIfNeeded, cache-clearing values");

RemoteConfigValueStore rcvs = getRemoteConfigValueStoreInternal();
Expand All @@ -254,7 +265,12 @@ private void notifyDownloadCallbacks(final RCDownloadCallback devProvidedCallbac
}
}

private void rcAutomaticDownloadTrigger(final boolean cacheClearOldValues) {
/**
* Downloads every remote config value when automatic download triggers are enabled.
*
* @param cacheClearOldValues whether the stored values are first cleared, or marked as cached with value caching
*/
void rcAutomaticDownloadTrigger(final boolean cacheClearOldValues) {
if (cacheClearOldValues) {
cacheOrClearRCValuesIfNeeded();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Predicate;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -387,6 +389,58 @@ static int dropOldestRequestsOverQueueLimit(@Nonnull InternalConfig config) {
return droppedIds.size();
}

/**
* Sets the current app key on every queued request that carries another one, or none, as one
* storage task. The checksum is computed when a request is sent, so it matches the new key.
*
* @param config configuration of the running SDK
* @return how many requests were changed
*/
static int replaceAppKeysInQueue(@Nonnull InternalConfig config) {
final String appKey = config.getServerAppKey();
final AtomicInteger replaced = new AtomicInteger();
Storage.transform(config, Request.getStoragePrefix(), (id, data) -> {
Request request = new Request(id);
if (!request.restore(data, config.getLogger()) || appKey.equals(request.params.get("app_key"))) {
return null;
}
request.params.remove("app_key");
request.params.add("app_key", appKey);
replaced.incrementAndGet();
return request.store(config.getLogger());
});
return replaced.get();
}

/**
* Removes the queued requests a filter selects, unsent, in one storage task, so no other storage
* work interleaves. A request being sent finishes, then the network removes it by its storage
* ID, so no other request is lost. Settles the journey triggers the removed requests carried.
*
* @param config configuration of the running SDK
* @param filter selects a request to remove, {@code null} to remove every request without reading it
* @return how many requests were removed
*/
static int removeQueuedRequests(@Nonnull InternalConfig config, @Nullable Predicate<Request> filter) {
List<Long> removedIds = Storage.runAndWait(config, () -> {
List<Long> removed = new ArrayList<>();
for (Long id : config.sdk.sdkStorage.storableList(config, Request.getStoragePrefix(), 0)) {
Request request = new Request(id);
boolean selected = filter == null || (Boolean.TRUE.equals(config.sdk.sdkStorage.storableRead(config, request)) && filter.test(request));
if (selected && Boolean.TRUE.equals(config.sdk.sdkStorage.storableRemove(config, request))) {
removed.add(id);
}
}
return removed;
});
if (removedIds == null) {
return 0;
}

settleJourneyTriggersOf(config, removedIds);
return removedIds.size();
}

/**
* Tells the events module which requests were dropped unread, so a journey trigger one of them
* carried stops waiting for a response. Only a set of IDs is touched, which is safe on the
Expand Down
Loading
Loading