GeezerVison is showing github.com — experimental query retry configuration · restmad/objectbox-java@58baa9c
File treeExpand file treeCollapse file tree Open diff view settings objectbox-java/src/main/java/io/objectbox tests/objectbox-java-test/src/main/java/io/objectbox
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: objectbox-java/src/main/java/io/objectbox/BoxStore.java +25-2Lines changed: 25 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -169,6 +169,10 @@ public static String getVersion() { | 169 | 169 |
| 170 | 170 | private int objectBrowserPort; | 171 | 171 |
| | 172 | + private final int defaultQueryAttempts; | | 173 | + | | 174 | + private final TxCallback defaultFailedReadTxAttemptCallback; | | 175 | + | 172 | 176 | BoxStore(BoxStoreBuilder builder) { | 173 | 177 | NativeLibraryLoader.ensureLoaded(); | 174 | 178 |
| @@ -226,6 +230,9 @@ public static String getVersion() { | 226 | 230 | } | 227 | 231 |
| 228 | 232 | objectClassPublisher = new ObjectClassPublisher(this); | | 233 | + | | 234 | + defaultFailedReadTxAttemptCallback = builder.defaultFailedReadTxAttemptCallback; | | 235 | + defaultQueryAttempts = builder.defaultQueryAttempts < 1 ? 1 : builder.defaultQueryAttempts; | 229 | 236 | } | 230 | 237 |
| 231 | 238 | private static void verifyNotAlreadyOpen(String canonicalPath) { | @@ -608,16 +615,22 @@ public <T> T callInReadTxWithRetry(Callable<T> callable, int attempts, int initi | 608 | 615 | return callInReadTx(callable); | 609 | 616 | } catch (DbException e) { | 610 | 617 | lastException = e; | | 618 | + | | 619 | + String diagnose = diagnose(); | | 620 | + String message = attempt + ". of " + attempts + " attempts of calling a read TX failed:"; | 611 | 621 | if (logAndHeal) { | 612 | | - System.err.println(attempt + ". of " + attempts + " attempts of calling a read TX failed:"); | | 622 | + System.err.println(message); | 613 | 623 | e.printStackTrace(); | 614 | | - System.err.println(diagnose()); | | 624 | + System.err.println(diagnose); | 615 | 625 | System.err.flush(); | 616 | 626 |
| 617 | 627 | System.gc(); | 618 | 628 | System.runFinalization(); | 619 | 629 | cleanStaleReadTransactions(); | 620 | 630 | } | | 631 | + if (defaultFailedReadTxAttemptCallback != null) { | | 632 | + defaultFailedReadTxAttemptCallback.txFinished(null, new DbException(message + " \n" + diagnose, e)); | | 633 | + } | 621 | 634 | try { | 622 | 635 | Thread.sleep(backoffInMs); | 623 | 636 | } catch (InterruptedException ie) { | @@ -854,6 +867,16 @@ public boolean isDebugRelations() { | 854 | 867 | return debugRelations; | 855 | 868 | } | 856 | 869 |
| | 870 | + @Internal | | 871 | + public int internalDefaultQueryAttempts() { | | 872 | + return defaultQueryAttempts; | | 873 | + } | | 874 | + | | 875 | + @Internal | | 876 | + public TxCallback internalDefaultFailedReadTxAttemptCallback() { | | 877 | + return defaultFailedReadTxAttemptCallback; | | 878 | + } | | 879 | + | 857 | 880 | void setDebugFlags(int debugFlags) { | 858 | 881 | nativeSetDebugFlags(handle, debugFlags); | 859 | 882 | } | |
Collapse file Copy file name to clipboardExpand all lines: objectbox-java/src/main/java/io/objectbox/BoxStoreBuilder.java +17-1Lines changed: 17 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 24 | 24 | import javax.annotation.Nonnull; | 25 | 25 | import javax.annotation.Nullable; | 26 | 26 |
| | 27 | +import io.objectbox.annotation.apihint.Experimental; | 27 | 28 | import io.objectbox.annotation.apihint.Internal; | 28 | 29 | import io.objectbox.ideasonly.ModelUpdate; | 29 | 30 |
| @@ -74,6 +75,10 @@ public class BoxStoreBuilder { | 74 | 75 |
| 75 | 76 | int maxReaders; | 76 | 77 |
| | 78 | + int defaultQueryAttempts; | | 79 | + | | 80 | + TxCallback defaultFailedReadTxAttemptCallback; | | 81 | + | 77 | 82 | final List<EntityInfo> entityInfoList = new ArrayList<>(); | 78 | 83 |
| 79 | 84 | @Internal | @@ -235,7 +240,6 @@ BoxStoreBuilder modelUpdate(ModelUpdate modelUpdate) { | 235 | 240 | * <p> | 236 | 241 | * In general, a maximum size prevents the DB from growing indefinitely when something goes wrong | 237 | 242 | * (for example you insert data in an infinite loop). | 238 | | - * | 239 | 243 | */ | 240 | 244 | public BoxStoreBuilder maxSizeInKByte(long maxSizeInKByte) { | 241 | 245 | this.maxSizeInKByte = maxSizeInKByte; | @@ -265,6 +269,18 @@ public BoxStoreBuilder debugRelations() { | 265 | 269 | return this; | 266 | 270 | } | 267 | 271 |
| | 272 | + @Experimental | | 273 | + public BoxStoreBuilder defaultQueryAttempts(int defaultQueryAttempts) { | | 274 | + this.defaultQueryAttempts = defaultQueryAttempts; | | 275 | + return this; | | 276 | + } | | 277 | + | | 278 | + @Experimental | | 279 | + public BoxStoreBuilder defaultFailedReadTxAttemptCallback(TxCallback defaultFailedReadTxAttemptCallback) { | | 280 | + this.defaultFailedReadTxAttemptCallback = defaultFailedReadTxAttemptCallback; | | 281 | + return this; | | 282 | + } | | 283 | + | 268 | 284 | /** | 269 | 285 | * Builds a {@link BoxStore} using any given configuration. | 270 | 286 | */ | |
Collapse file Copy file name to clipboardExpand all lines: objectbox-java/src/main/java/io/objectbox/query/Query.java +14-10Lines changed: 14 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -65,12 +65,12 @@ public class Query<T> { | 65 | 65 | native void nativeSetParameter(long handle, int propertyId, String parameterAlias, long value); | 66 | 66 |
| 67 | 67 | native void nativeSetParameters(long handle, int propertyId, String parameterAlias, long value1, | 68 | | - long value2); | | 68 | + long value2); | 69 | 69 |
| 70 | 70 | native void nativeSetParameter(long handle, int propertyId, String parameterAlias, double value); | 71 | 71 |
| 72 | 72 | native void nativeSetParameters(long handle, int propertyId, String parameterAlias, double value1, | 73 | | - double value2); | | 73 | + double value2); | 74 | 74 |
| 75 | 75 | private final Box<T> box; | 76 | 76 | private final BoxStore store; | @@ -79,12 +79,16 @@ native void nativeSetParameters(long handle, int propertyId, String parameterAli | 79 | 79 | private final List<EagerRelation> eagerRelations; | 80 | 80 | private final QueryFilter<T> filter; | 81 | 81 | private final Comparator<T> comparator; | | 82 | + private final int queryAttempts; | | 83 | + private final int initialRetryBackOffInMs = 10; | | 84 | + | 82 | 85 | long handle; | 83 | 86 |
| 84 | 87 | Query(Box<T> box, long queryHandle, boolean hasOrder, List<EagerRelation> eagerRelations, QueryFilter<T> filter, | 85 | 88 | Comparator<T> comparator) { | 86 | 89 | this.box = box; | 87 | 90 | store = box.getStore(); | | 91 | + queryAttempts = store.internalDefaultQueryAttempts(); | 88 | 92 | handle = queryHandle; | 89 | 93 | this.hasOrder = hasOrder; | 90 | 94 | publisher = new QueryPublisher<>(this, box); | @@ -115,15 +119,15 @@ public synchronized void close() { | 115 | 119 | @Nullable | 116 | 120 | public T findFirst() { | 117 | 121 | ensureNoFilterNoComparator(); | 118 | | - return store.callInReadTx(new Callable<T>() { | | 122 | + return store.callInReadTxWithRetry(new Callable<T>() { | 119 | 123 | @Override | 120 | 124 | public T call() { | 121 | 125 | @SuppressWarnings("unchecked") | 122 | 126 | T entity = (T) nativeFindFirst(handle, InternalAccess.getActiveTxCursorHandle(box)); | 123 | 127 | resolveEagerRelation(entity); | 124 | 128 | return entity; | 125 | 129 | } | 126 | | - }); | | 130 | + }, queryAttempts, initialRetryBackOffInMs, true); | 127 | 131 | } | 128 | 132 |
| 129 | 133 | private void ensureNoFilterNoComparator() { | @@ -149,23 +153,23 @@ private void ensureNoComparator() { | 149 | 153 | @Nullable | 150 | 154 | public T findUnique() { | 151 | 155 | ensureNoFilterNoComparator(); | 152 | | - return store.callInReadTx(new Callable<T>() { | | 156 | + return store.callInReadTxWithRetry(new Callable<T>() { | 153 | 157 | @Override | 154 | 158 | public T call() { | 155 | 159 | @SuppressWarnings("unchecked") | 156 | 160 | T entity = (T) nativeFindUnique(handle, InternalAccess.getActiveTxCursorHandle(box)); | 157 | 161 | resolveEagerRelation(entity); | 158 | 162 | return entity; | 159 | 163 | } | 160 | | - }); | | 164 | + }, queryAttempts, initialRetryBackOffInMs, true); | 161 | 165 | } | 162 | 166 |
| 163 | 167 | /** | 164 | 168 | * Find all Objects matching the query. | 165 | 169 | */ | 166 | 170 | @Nonnull | 167 | 171 | public List<T> find() { | 168 | | - return store.callInReadTx(new Callable<List<T>>() { | | 172 | + return store.callInReadTxWithRetry(new Callable<List<T>>() { | 169 | 173 | @Override | 170 | 174 | public List<T> call() throws Exception { | 171 | 175 | long cursorHandle = InternalAccess.getActiveTxCursorHandle(box); | @@ -185,7 +189,7 @@ public List<T> call() throws Exception { | 185 | 189 | } | 186 | 190 | return entities; | 187 | 191 | } | 188 | | - }); | | 192 | + }, queryAttempts, initialRetryBackOffInMs, true); | 189 | 193 | } | 190 | 194 |
| 191 | 195 | /** | @@ -194,15 +198,15 @@ public List<T> call() throws Exception { | 194 | 198 | @Nonnull | 195 | 199 | public List<T> find(final long offset, final long limit) { | 196 | 200 | ensureNoFilterNoComparator(); | 197 | | - return store.callInReadTx(new Callable<List<T>>() { | | 201 | + return store.callInReadTxWithRetry(new Callable<List<T>>() { | 198 | 202 | @Override | 199 | 203 | public List<T> call() { | 200 | 204 | long cursorHandle = InternalAccess.getActiveTxCursorHandle(box); | 201 | 205 | List entities = nativeFind(handle, cursorHandle, offset, limit); | 202 | 206 | resolveEagerRelations(entities); | 203 | 207 | return entities; | 204 | 208 | } | 205 | | - }); | | 209 | + }, queryAttempts, initialRetryBackOffInMs, true); | 206 | 210 | } | 207 | 211 |
| 208 | 212 | /** | |
Collapse file Copy file name to clipboardExpand all lines: tests/objectbox-java-test/src/main/java/io/objectbox/AbstractObjectBoxTest.java +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -147,7 +147,7 @@ protected long time() { | 147 | 147 | return System.currentTimeMillis(); | 148 | 148 | } | 149 | 149 |
| 150 | | - byte[] createTestModel(boolean withIndex) { | | 150 | + protected byte[] createTestModel(boolean withIndex) { | 151 | 151 | ModelBuilder modelBuilder = new ModelBuilder(); | 152 | 152 | addTestEntity(modelBuilder, withIndex); | 153 | 153 | modelBuilder.lastEntityId(lastEntityId, lastEntityUid); | |
Collapse file Copy file name to clipboardExpand all lines: tests/objectbox-java-test/src/main/java/io/objectbox/BoxStoreTest.java +23Lines changed: 23 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 21 | 21 | import java.io.File; | 22 | 22 | import java.util.concurrent.Callable; | 23 | 23 |
| | 24 | +import javax.annotation.Nullable; | | 25 | + | 24 | 26 | import io.objectbox.exception.DbException; | 25 | 27 |
| 26 | 28 | import static org.junit.Assert.*; | @@ -159,6 +161,27 @@ public void testCallInReadTxWithRetry_fail() { | 159 | 161 | store.callInReadTxWithRetry(createTestCallable(countHolder), 4, 0, true); | 160 | 162 | } | 161 | 163 |
| | 164 | + @Test | | 165 | + public void testCallInReadTxWithRetry_callback() { | | 166 | + closeStoreForTest(); | | 167 | + final int[] countHolder = {0}; | | 168 | + final int[] countHolderCallback = {0}; | | 169 | + | | 170 | + BoxStoreBuilder builder = new BoxStoreBuilder(createTestModel(false)).directory(boxStoreDir) | | 171 | + .defaultFailedReadTxAttemptCallback(new TxCallback() { | | 172 | + @Override | | 173 | + public void txFinished(@Nullable Object result, @Nullable Throwable error) { | | 174 | + assertNotNull(error); | | 175 | + countHolderCallback[0]++; | | 176 | + } | | 177 | + }); | | 178 | + store = builder.build(); | | 179 | + String value = store.callInReadTxWithRetry(createTestCallable(countHolder), 5, 0, true); | | 180 | + assertEquals("42", value); | | 181 | + assertEquals(5, countHolder[0]); | | 182 | + assertEquals(4, countHolderCallback[0]); | | 183 | + } | | 184 | + | 162 | 185 | private Callable<String> createTestCallable(final int[] countHolder) { | 163 | 186 | return new Callable<String>() { | 164 | 187 | @Override | |
Collapse file Copy file name to clipboardExpand all lines: tests/objectbox-java-test/src/main/java/io/objectbox/query/QueryTest.java +25Lines changed: 25 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 23 | 23 | import java.util.Comparator; | 24 | 24 | import java.util.List; | 25 | 25 |
| | 26 | +import javax.annotation.Nullable; | | 27 | + | 26 | 28 | import io.objectbox.AbstractObjectBoxTest; | 27 | 29 | import io.objectbox.Box; | | 30 | +import io.objectbox.BoxStoreBuilder; | 28 | 31 | import io.objectbox.TestEntity; | 29 | 32 | import io.objectbox.TestEntity_; | | 33 | +import io.objectbox.TxCallback; | 30 | 34 | import io.objectbox.query.QueryBuilder.StringOrder; | 31 | 35 |
| 32 | 36 | import static io.objectbox.TestEntity_.*; | @@ -457,6 +461,27 @@ public int compare(TestEntity o1, TestEntity o2) { | 457 | 461 | assertEquals("apple", entities.get(4).getSimpleString()); | 458 | 462 | } | 459 | 463 |
| | 464 | + @Test | | 465 | + // TODO can we improve? More than just "still works"? | | 466 | + public void testQueryAttempts() { | | 467 | + store.close(); | | 468 | + BoxStoreBuilder builder = new BoxStoreBuilder(createTestModel(false)).directory(boxStoreDir) | | 469 | + .defaultQueryAttempts(5) | | 470 | + .defaultFailedReadTxAttemptCallback(new TxCallback() { | | 471 | + @Override | | 472 | + public void txFinished(@Nullable Object result, @Nullable Throwable error) { | | 473 | + error.printStackTrace(); | | 474 | + } | | 475 | + }); | | 476 | + builder.entity(new TestEntity_()); | | 477 | + | | 478 | + store = builder.build(); | | 479 | + putTestEntitiesScalars(); | | 480 | + | | 481 | + Query<TestEntity> query = store.boxFor(TestEntity.class).query().equal(simpleInt, 2007).build(); | | 482 | + assertEquals(2007, query.findFirst().getSimpleInt()); | | 483 | + } | | 484 | + | 460 | 485 | private QueryFilter<TestEntity> createTestFilter() { | 461 | 486 | return new QueryFilter<TestEntity>() { | 462 | 487 | @Override | |
|