Skip to content

Commit

Permalink
Merge pull request #391 from rudderlabs/release/1.21.3
Browse files Browse the repository at this point in the history
chore(release): pulling release/1.21.3 into master
  • Loading branch information
1abhishekpandey authored Feb 8, 2024
2 parents 14acedd + c41ffb4 commit ddac91d
Show file tree
Hide file tree
Showing 51 changed files with 663 additions and 214 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [1.21.3](https://github.com/rudderlabs/rudder-sdk-android/compare/v1.21.2...v1.21.3) (2024-02-07)


### Bug Fixes

* fixed serialization of strings twice ([#390](https://github.com/rudderlabs/rudder-sdk-android/issues/390)) ([4bc1d7d](https://github.com/rudderlabs/rudder-sdk-android/commit/4bc1d7d84f72d661cee70af3a672259d6d5f80a8))
* handling the serialization of special floating point values while serializing any object ([#382](https://github.com/rudderlabs/rudder-sdk-android/issues/382)) ([55521b6](https://github.com/rudderlabs/rudder-sdk-android/commit/55521b675de289c3c1ba5f80d40d338a61c9aac5))
* race condition fix using semaphore ([#388](https://github.com/rudderlabs/rudder-sdk-android/issues/388)) ([a792ce2](https://github.com/rudderlabs/rudder-sdk-android/commit/a792ce26514b31d82317eb59f16a97979ddfc13c))

### [1.21.2](https://github.com/rudderlabs/rudder-sdk-android/compare/v1.21.1...v1.21.2) (2024-01-25)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabaseCorruptException;
import android.icu.text.Collator;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
Expand All @@ -17,6 +16,7 @@
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;

import com.rudderstack.android.ruddermetricsreporterandroid.RudderReporter;
import com.rudderstack.android.sdk.core.persistence.DefaultPersistenceProviderFactory;
import com.rudderstack.android.sdk.core.persistence.Persistence;
import com.rudderstack.android.sdk.core.persistence.PersistenceProvider;
Expand All @@ -33,6 +33,7 @@
import java.util.Queue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/*
* Helper class for SQLite operations
Expand Down Expand Up @@ -62,7 +63,7 @@ class DBPersistentManager/* extends SQLiteOpenHelper*/ {
private static final int STATUS_DEVICE_MODE_DONE = 0b01;
private static final int STATUS_ALL_DONE = 0b11;
private static final int STATUS_NEW = 0b00;
// This column purpose is to identify if an event is dumped to device mode destinations without transformations or not.
// This column purpose is to identify if an event is sent to device mode destinations without transformations or not.
private static final String DM_PROCESSED_COL = "dm_processed";
// status value for DM_PROCESSED column
private static final int DM_PROCESSED_PENDING = 0;
Expand Down Expand Up @@ -244,6 +245,7 @@ void flushEvents() {
try {

if (persistence.isAccessible()) {
waitTillMigrationsAreDone();
String deleteSQL = String.format(Locale.US, "DELETE FROM %s", EVENTS_TABLE_NAME);
RudderLogger.logDebug(String.format(Locale.US, "DBPersistentManager: flushEvents: deleteSQL: %s", deleteSQL));
synchronized (DB_LOCK) {
Expand All @@ -266,6 +268,7 @@ void clearEventsFromDB(List<Integer> messageIds) {
try {
// get writable database
if (persistence.isAccessible()) {
waitTillMigrationsAreDone();
RudderLogger.logInfo(String.format(Locale.US, "DBPersistentManager: clearEventsFromDB: Clearing %d messages from DB", messageIds.size()));
// format CSV string from messageIds list
StringBuilder builder = new StringBuilder();
Expand Down Expand Up @@ -316,6 +319,7 @@ void getEventsFromDB(Map<Integer, Integer> messageIdStatusMap,//(id (row_id), st
}
Cursor cursor;
synchronized (DB_LOCK) {
waitTillMigrationsAreDone();
cursor = persistence.rawQuery(selectSQL, null);
}
if (!cursor.moveToFirst()) {
Expand Down Expand Up @@ -505,7 +509,10 @@ private boolean checkIfColumnExists(String newColumn) {
return false;
}

private final Semaphore migrationSemaphore = new Semaphore(1);

void checkForMigrations() {
acquireSemaphore();
Runnable runnable = () -> {
try {
boolean isNewColumnAdded = false;
Expand All @@ -525,12 +532,30 @@ void checkForMigrations() {
} catch (SQLiteDatabaseCorruptException | ConcurrentModificationException |
NullPointerException ex) {
RudderLogger.logError(DBPERSISTENT_MANAGER_CHECK_FOR_MIGRATIONS_TAG + ex.getLocalizedMessage());
} finally {
migrationSemaphore.release();
}
};
// Need to perform db operations on a separate thread to support strict mode.
executor.execute(runnable);
}

private void acquireSemaphore() {
try {
migrationSemaphore.acquire();
} catch (InterruptedException e) {
ReportManager.reportError(e);
Thread.currentThread().interrupt();
}
}
private void waitTillMigrationsAreDone() {
if(migrationSemaphore.availablePermits() == 1 ){
return;
}
acquireSemaphore();
migrationSemaphore.release();
}

private void performMigration(String columnName) {
try {
if (persistence.isAccessible()) {
Expand Down Expand Up @@ -596,6 +621,7 @@ void markDeviceModeTransformationAndDMProcessedDone(List<Integer> rowIds) {
" WHERE " + MESSAGE_ID_COL + " IN "
+ rowIdsCSVString + ";";
synchronized (DB_LOCK) {
waitTillMigrationsAreDone();
persistence.execSQL(sql);
}
}
Expand All @@ -605,6 +631,7 @@ public void markDeviceModeProcessedDone(Integer rowId) {
DBPersistentManager.DM_PROCESSED_COL + " = " + DBPersistentManager.DM_PROCESSED_DONE +
" WHERE " + MESSAGE_ID_COL + " = " + rowId + ";";
synchronized (DB_LOCK) {
waitTillMigrationsAreDone();
persistence.execSQL(sql);
}
}
Expand All @@ -616,6 +643,7 @@ private void updateEventStatus(String rowIdsCSVString, int status) {
") WHERE " + MESSAGE_ID_COL + " IN "
+ rowIdsCSVString + ";";
synchronized (DB_LOCK) {
waitTillMigrationsAreDone();
persistence.execSQL(sql);
}
}
Expand All @@ -627,6 +655,7 @@ void runGcForEvents() {

private void deleteDoneEvents() {
synchronized (DB_LOCK) {
waitTillMigrationsAreDone();
persistence.delete(EVENTS_TABLE_NAME,
DBPersistentManager.STATUS_COL + " = " + DBPersistentManager.STATUS_ALL_DONE,
null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public EventInsertionCallback(RudderMessage message, RudderDeviceModeManager dev

@Override
public void onInsertion(Integer rowId) {
deviceModeManager.makeFactoryDump(message, rowId, false);
deviceModeManager.processMessage(message, rowId, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -326,31 +326,35 @@ private void saveFlushConfig() {


/*
* generic method for dumping all the events
* generic method for processing all the events
* */
void processMessage(@NonNull RudderMessage message) {
if (!isSDKEnabled) {
incrementDiscardedCounter(1, Collections.singletonMap(LABEL_TYPE, ReportManager.LABEL_TYPE_SDK_DISABLED));
return;
}
RudderLogger.logDebug(String.format(Locale.US, "EventRepository: dump: eventName: %s", message.getEventName()));
RudderLogger.logDebug(String.format(Locale.US, "EventRepository: processMessage: eventName: %s", message.getEventName()));

applyRudderOptionsToMessageIntegrations(message);
RudderMessage updatedMessage = updateMessageWithConsentedDestinations(message);
userSessionManager.applySessionTracking(updatedMessage);

String eventJson = getEventJsonString(updatedMessage);
RudderLogger.logVerbose(String.format(Locale.US, "EventRepository: dump: message: %s", eventJson));
if (eventJson == null) {
RudderLogger.logError("EventRepository: processMessage: eventJson is null after serialization");
return;
}
if (isMessageJsonExceedingMaxSize(eventJson)) {
incrementDiscardedCounter(1, Collections.singletonMap(LABEL_TYPE, ReportManager.LABEL_TYPE_MSG_SIZE_INVALID));
RudderLogger.logError(String.format(Locale.US, "EventRepository: dump: Event size exceeds the maximum permitted event size(%d)", Utils.MAX_EVENT_SIZE));
RudderLogger.logError(String.format(Locale.US, "EventRepository: processMessage: Event size exceeds the maximum permitted event size(%d)", Utils.MAX_EVENT_SIZE));
return;
}
RudderLogger.logVerbose(String.format(Locale.US, "EventRepository: processMessage: message: %s", eventJson));
dbManager.saveEvent(eventJson, new EventInsertionCallback(message, deviceModeManager));
}

String getEventJsonString(RudderMessage updatedMessage) {
return RudderGson.getInstance().toJson(updatedMessage);
String getEventJsonString(RudderMessage message) {
return RudderGson.serialize(message);
}

private boolean isMessageJsonExceedingMaxSize(String eventJson) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public void track(@NonNull RudderMessageBuilder builder) {
*/
public void track(@NonNull RudderMessage message) {
message.setType(MessageType.TRACK);
dumpMessage(message);
processMessage(message);

}

Expand Down Expand Up @@ -293,7 +293,7 @@ public void screen(@NonNull RudderMessageBuilder builder) {
*/
public void screen(@NonNull RudderMessage message) {
message.setType(MessageType.SCREEN);
dumpMessage(message);
processMessage(message);

}

Expand Down Expand Up @@ -364,7 +364,7 @@ public void screen(@NonNull String screenName, @Nullable RudderProperty property
*/
public void identify(@NonNull RudderMessage message) {
message.setType(MessageType.IDENTIFY);
dumpMessage(message);
processMessage(message);

}

Expand Down Expand Up @@ -455,7 +455,7 @@ public void alias(@NonNull RudderMessageBuilder builder) {
*/
void alias(@NonNull RudderMessage message) {
message.setType(MessageType.ALIAS);
dumpMessage(message);
processMessage(message);

}

Expand Down Expand Up @@ -531,11 +531,11 @@ public void group(@NonNull RudderMessageBuilder builder) {
@Deprecated
public void group(@NonNull RudderMessage message) {
message.setType(MessageType.GROUP);
dumpMessage(message);
processMessage(message);
}


private void dumpMessage(@NonNull RudderMessage message) {
private void processMessage(@NonNull RudderMessage message) {
if (getOptOutStatus()) {
incrementDiscardedCounter(1, Collections.singletonMap(ReportManager.LABEL_TYPE,
ReportManager.LABEL_TYPE_OPT_OUT));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ public void run() {
private void deleteEventsWithoutAnonymousId(ArrayList<String> messages, ArrayList<Integer> messageIds) {
List<Integer> eventsToDelete = new ArrayList<>();
for (int i = 0; i < messages.size(); i++) {
Map<String, Object> message = RudderGson.getInstance().fromJson(messages.get(i), Map.class);
if (!message.containsKey("anonymousId") || message.get("anonymousId") == null) {
Map<String, Object> message = RudderGson.deserialize(messages.get(i), Map.class);
if (message != null && (!message.containsKey("anonymousId") || message.get("anonymousId") == null)) {
eventsToDelete.add(messageIds.get(i));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public class RudderContext {
RudderLogger.logDebug(String.format(Locale.US, "Traits from persistence storage%s", traitsJson));
if (traitsJson == null) {
RudderTraits traits = new RudderTraits(anonymousId);
this.traits = Utils.convertToMap(RudderGson.getInstance().toJson(traits));
this.traits = Utils.convertToMap(traits);
this.persistTraits();
RudderLogger.logDebug("New traits has been saved");
} else {
Expand Down Expand Up @@ -119,7 +119,7 @@ void resetTraits() {
RudderTraits traits = new RudderTraits();
// convert the whole traits to map and take care of the extras
synchronized (this) {
this.traits = Utils.convertToMap(RudderGson.getInstance().toJson(traits));
this.traits = Utils.convertToMap(traits);
}
}

Expand All @@ -130,7 +130,7 @@ void updateTraits(RudderTraits traits) {
}

// convert the whole traits to map and take care of the extras
Map<String, Object> traitsMap = Utils.convertToMap(RudderGson.getInstance().toJson(traits));
Map<String, Object> traitsMap = Utils.convertToMap(traits);

String existingId = (String) this.traits.get("id");
String newId = (String) traitsMap.get("id");
Expand Down Expand Up @@ -161,7 +161,7 @@ void persistTraits() {
if (RudderClient.getApplication() != null) {
RudderPreferenceManager preferenceManger = RudderPreferenceManager.getInstance(RudderClient.getApplication());
synchronized (this) {
preferenceManger.saveTraits(RudderGson.getInstance().toJson(this.traits));
preferenceManger.saveTraits(RudderGson.serialize(this.traits));
}
}
} catch (NullPointerException ex) {
Expand Down Expand Up @@ -343,7 +343,7 @@ void persistExternalIds() {
try {
if (RudderClient.getApplication() != null) {
RudderPreferenceManager preferenceManger = RudderPreferenceManager.getInstance(RudderClient.getApplication());
preferenceManger.saveExternalIds(RudderGson.getInstance().toJson(this.externalIds));
preferenceManger.saveExternalIds(RudderGson.serialize(this.externalIds));
}
} catch (NullPointerException ex) {
ReportManager.reportError(ex);
Expand Down
Loading

0 comments on commit ddac91d

Please sign in to comment.