From e0ab6947558c50a16acd996156208f2f2bda055c Mon Sep 17 00:00:00 2001 From: xiaoweii Date: Thu, 22 Aug 2024 13:42:47 +0800 Subject: [PATCH 1/4] fix: db insert error for no space left --- .../solution/clickstream/client/EventRecorder.java | 14 ++++++++++++++ .../clickstream/client/db/ClickstreamDBUtil.java | 14 +++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/clickstream/src/main/java/software/aws/solution/clickstream/client/EventRecorder.java b/clickstream/src/main/java/software/aws/solution/clickstream/client/EventRecorder.java index a5bc1f1..feef170 100644 --- a/clickstream/src/main/java/software/aws/solution/clickstream/client/EventRecorder.java +++ b/clickstream/src/main/java/software/aws/solution/clickstream/client/EventRecorder.java @@ -21,6 +21,7 @@ import com.amazonaws.logging.Log; import com.amazonaws.logging.LogFactory; +import java.util.concurrent.Executors; import software.aws.solution.clickstream.client.db.ClickstreamDBUtil; import software.aws.solution.clickstream.client.db.EventTable; import software.aws.solution.clickstream.client.network.NetRequest; @@ -101,6 +102,7 @@ public Uri recordEvent(@NonNull final AnalyticsEvent event) { } } else { LOG.error(String.format("Error to save event with EventType: %s", event.getEventType())); + sendEventImmediately(event); } return uri; } @@ -207,5 +209,17 @@ String[] getBatchOfEvents(final Cursor cursor) { return new String[] {eventBuilder.toString(), lastEventId}; } + + public void sendEventImmediately(AnalyticsEvent event) { + Runnable task = () -> { + NetRequest.uploadEvents("[" + event.toJSONObject().toString() + "]", + clickstreamContext.getClickstreamConfiguration(), + bundleSequenceId); + bundleSequenceId += 1; + clickstreamContext.getSystem().getPreferences() + .putInt(KEY_BUNDLE_SEQUENCE_ID_PREF, bundleSequenceId); + }; + Executors.newSingleThreadExecutor().execute(task); + } } diff --git a/clickstream/src/main/java/software/aws/solution/clickstream/client/db/ClickstreamDBUtil.java b/clickstream/src/main/java/software/aws/solution/clickstream/client/db/ClickstreamDBUtil.java index 40114b4..9a22f75 100644 --- a/clickstream/src/main/java/software/aws/solution/clickstream/client/db/ClickstreamDBUtil.java +++ b/clickstream/src/main/java/software/aws/solution/clickstream/client/db/ClickstreamDBUtil.java @@ -18,9 +18,13 @@ import android.content.ContentValues; import android.content.Context; import android.database.Cursor; +import android.database.SQLException; import android.net.Uri; +import com.amazonaws.logging.Log; +import com.amazonaws.logging.LogFactory; import software.aws.solution.clickstream.client.AnalyticsEvent; +import software.aws.solution.clickstream.client.EventRecorder; /** * Clickstream Database Util. @@ -31,6 +35,8 @@ public class ClickstreamDBUtil { */ private ClickstreamDBBase clickstreamDBBase; + private static final Log LOG = LogFactory.getLog(EventRecorder.class); + /** * Constructs a ClickstreamDBUtil with the given Context. * @@ -58,7 +64,13 @@ public void closeDB() { * @return An Uri of the record inserted. */ public Uri saveEvent(final AnalyticsEvent event) { - return clickstreamDBBase.insert(clickstreamDBBase.getContentUri(), generateContentValuesFromEvent(event)); + Uri uri = null; + try { + uri = clickstreamDBBase.insert(clickstreamDBBase.getContentUri(), generateContentValuesFromEvent(event)); + } catch (SQLException e) { + LOG.info("SQLException: " + e.getMessage()); + } + return uri; } private ContentValues generateContentValuesFromEvent(final AnalyticsEvent event) { From b5c8edb927baccd2468edb6ec903a853c184fd75 Mon Sep 17 00:00:00 2001 From: xiaoweii Date: Thu, 22 Aug 2024 21:42:02 +0800 Subject: [PATCH 2/4] fix: format issue, add test case --- .../clickstream/client/EventRecorder.java | 6 +++++- .../client/db/ClickstreamDBUtil.java | 8 ++++---- .../clickstream/EventRecorderTest.java | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/clickstream/src/main/java/software/aws/solution/clickstream/client/EventRecorder.java b/clickstream/src/main/java/software/aws/solution/clickstream/client/EventRecorder.java index feef170..cef0362 100644 --- a/clickstream/src/main/java/software/aws/solution/clickstream/client/EventRecorder.java +++ b/clickstream/src/main/java/software/aws/solution/clickstream/client/EventRecorder.java @@ -21,7 +21,6 @@ import com.amazonaws.logging.Log; import com.amazonaws.logging.LogFactory; -import java.util.concurrent.Executors; import software.aws.solution.clickstream.client.db.ClickstreamDBUtil; import software.aws.solution.clickstream.client.db.EventTable; import software.aws.solution.clickstream.client.network.NetRequest; @@ -30,6 +29,7 @@ import java.util.Locale; import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; @@ -210,6 +210,10 @@ String[] getBatchOfEvents(final Cursor cursor) { return new String[] {eventBuilder.toString(), lastEventId}; } + /** + * Method for send event immediately when event saved fail. + * @param event AnalyticsEvent + */ public void sendEventImmediately(AnalyticsEvent event) { Runnable task = () -> { NetRequest.uploadEvents("[" + event.toJSONObject().toString() + "]", diff --git a/clickstream/src/main/java/software/aws/solution/clickstream/client/db/ClickstreamDBUtil.java b/clickstream/src/main/java/software/aws/solution/clickstream/client/db/ClickstreamDBUtil.java index 9a22f75..34ed6c2 100644 --- a/clickstream/src/main/java/software/aws/solution/clickstream/client/db/ClickstreamDBUtil.java +++ b/clickstream/src/main/java/software/aws/solution/clickstream/client/db/ClickstreamDBUtil.java @@ -30,13 +30,13 @@ * Clickstream Database Util. */ public class ClickstreamDBUtil { + private static final Log LOG = LogFactory.getLog(EventRecorder.class); + /** * ClickstreamDBBase is a basic helper for accessing the database. */ private ClickstreamDBBase clickstreamDBBase; - private static final Log LOG = LogFactory.getLog(EventRecorder.class); - /** * Constructs a ClickstreamDBUtil with the given Context. * @@ -67,8 +67,8 @@ public Uri saveEvent(final AnalyticsEvent event) { Uri uri = null; try { uri = clickstreamDBBase.insert(clickstreamDBBase.getContentUri(), generateContentValuesFromEvent(event)); - } catch (SQLException e) { - LOG.info("SQLException: " + e.getMessage()); + } catch (SQLException error) { + LOG.info("SQLException: " + error.getMessage()); } return uri; } diff --git a/clickstream/src/test/java/software/aws/solution/clickstream/EventRecorderTest.java b/clickstream/src/test/java/software/aws/solution/clickstream/EventRecorderTest.java index 93d067f..32d5618 100644 --- a/clickstream/src/test/java/software/aws/solution/clickstream/EventRecorderTest.java +++ b/clickstream/src/test/java/software/aws/solution/clickstream/EventRecorderTest.java @@ -69,6 +69,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; @RunWith(RobolectricTestRunner.class) @Config(manifest = Config.NONE) @@ -567,6 +568,23 @@ public void testRecordEventRequestUploadTimestamp() throws Exception { assertTrue(requestResult); } + /** + * test save event failed and send event immediately. + * + * @throws Exception exception. + */ + @Test + public void testSaveEventFailedAndWillSendEventImmediately() throws Exception { + dbUtil = mock(ClickstreamDBUtil.class); + ReflectUtil.modifyFiled(eventRecorder, "dbUtil", dbUtil); + when(dbUtil.saveEvent(event)).thenReturn(null); + eventRecorder.recordEvent(event); + verify(log).error("Error to save event with EventType: testEvent"); + Thread.sleep(1500); + int bundleSequenceId = (int) ReflectUtil.getFiled(eventRecorder, "bundleSequenceId"); + assertTrue(bundleSequenceId > 1); + } + /** * common method to set request path. * From d43e62831057e9c329bf6fef580ed6812e95ec61 Mon Sep 17 00:00:00 2001 From: xiaoweii Date: Thu, 22 Aug 2024 21:58:18 +0800 Subject: [PATCH 3/4] fix: add test for dbutil --- .../solution/clickstream/db/DBUtilTest.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/clickstream/src/test/java/software/aws/solution/clickstream/db/DBUtilTest.java b/clickstream/src/test/java/software/aws/solution/clickstream/db/DBUtilTest.java index 7735ae5..ff0a441 100644 --- a/clickstream/src/test/java/software/aws/solution/clickstream/db/DBUtilTest.java +++ b/clickstream/src/test/java/software/aws/solution/clickstream/db/DBUtilTest.java @@ -15,7 +15,9 @@ package software.aws.solution.clickstream.db; +import android.content.ContentValues; import android.database.Cursor; +import android.database.SQLException; import android.net.Uri; import androidx.test.core.app.ApplicationProvider; @@ -28,13 +30,19 @@ import org.robolectric.annotation.Config; import software.aws.solution.clickstream.AnalyticsEventTest; import software.aws.solution.clickstream.client.AnalyticsEvent; +import software.aws.solution.clickstream.client.db.ClickstreamDBBase; import software.aws.solution.clickstream.client.db.ClickstreamDBUtil; +import software.aws.solution.clickstream.util.ReflectUtil; import java.util.Objects; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; @RunWith(RobolectricTestRunner.class) @Config(manifest = Config.NONE, sdk = 26) @@ -63,6 +71,21 @@ public void testInsertSingleEvent() { assertNotEquals(idInserted, 0); } + /** + * test insert single event failed. + * + * @throws Exception exception. + */ + @Test + public void testInsertSingleEventFailed() throws Exception { + ClickstreamDBBase clickstreamDBBase = mock(ClickstreamDBBase.class); + ReflectUtil.modifyFiled(dbUtil, "clickstreamDBBase", clickstreamDBBase); + doThrow(new SQLException("Mocked SQLException")).when(clickstreamDBBase).insert(any(Uri.class), any( + ContentValues.class)); + Uri uri = dbUtil.saveEvent(analyticsEvent); + assertNull(uri); + } + /** * test query all. */ From caf41f90bee4ac07dcb9ec3db3c9484d19757294 Mon Sep 17 00:00:00 2001 From: xiaoweii Date: Fri, 23 Aug 2024 14:19:31 +0800 Subject: [PATCH 4/4] fix: change log level for SQLException --- .../aws/solution/clickstream/client/db/ClickstreamDBUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clickstream/src/main/java/software/aws/solution/clickstream/client/db/ClickstreamDBUtil.java b/clickstream/src/main/java/software/aws/solution/clickstream/client/db/ClickstreamDBUtil.java index 34ed6c2..7f64b7b 100644 --- a/clickstream/src/main/java/software/aws/solution/clickstream/client/db/ClickstreamDBUtil.java +++ b/clickstream/src/main/java/software/aws/solution/clickstream/client/db/ClickstreamDBUtil.java @@ -68,7 +68,7 @@ public Uri saveEvent(final AnalyticsEvent event) { try { uri = clickstreamDBBase.insert(clickstreamDBBase.getContentUri(), generateContentValuesFromEvent(event)); } catch (SQLException error) { - LOG.info("SQLException: " + error.getMessage()); + LOG.warn("SQLException: " + error.getMessage()); } return uri; }