From 56ed90689d05a36b0e6b86854dc3d6560e94f82b Mon Sep 17 00:00:00 2001 From: M66B Date: Tue, 22 Sep 2020 14:04:26 +0200 Subject: [PATCH] Revert "Simplify operation handling" This reverts commit b30a8d9b8a32a8449e3947c5ace5a609c6be37dc. --- app/src/main/java/eu/faircode/email/Core.java | 63 +++++++++++++++++-- .../eu/faircode/email/ServiceSynchronize.java | 5 +- 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/Core.java b/app/src/main/java/eu/faircode/email/Core.java index 110cc2de84..0de4667716 100644 --- a/app/src/main/java/eu/faircode/email/Core.java +++ b/app/src/main/java/eu/faircode/email/Core.java @@ -150,7 +150,7 @@ class Core { Context context, EntityAccount account, EntityFolder folder, List ops, Store istore, Folder ifolder, - State state) + State state, int priority, long sequence) throws JSONException { try { Log.i(folder.name + " start process"); @@ -160,7 +160,9 @@ class Core { int retry = 0; boolean group = true; Log.i(folder.name + " executing operations=" + ops.size()); - while (retry < LOCAL_RETRY_MAX && ops.size() > 0 && state.isRunning()) { + while (retry < LOCAL_RETRY_MAX && ops.size() > 0 && + state.isRunning() && + state.batchCanRun(folder.id, priority, sequence)) { TupleOperationEx op = ops.get(0); try { @@ -498,7 +500,9 @@ class Core { ops.remove(op); } else { retry++; - if (retry < LOCAL_RETRY_MAX && state.isRunning()) + if (retry < LOCAL_RETRY_MAX && + state.isRunning() && + state.batchCanRun(folder.id, priority, sequence)) try { Thread.sleep(LOCAL_RETRY_DELAY); } catch (InterruptedException ex1) { @@ -524,7 +528,9 @@ class Core { } } - if (ops.size() > 0) + if (ops.size() == 0) + state.batchCompleted(folder.id, priority, sequence); + else state.error(new OperationCanceledException("Processing")); } finally { Log.i(folder.name + " end process state=" + state + " pending=" + ops.size()); @@ -4058,6 +4064,9 @@ class Core { private boolean recoverable = true; private Long lastActivity = null; + private Map sequence = new HashMap<>(); + private Map batch = new HashMap<>(); + State(ConnectionHelper.NetworkState networkState) { this.networkState = networkState; } @@ -4137,6 +4146,17 @@ class Core { void reset() { recoverable = true; lastActivity = null; + resetBatches(); + } + + void resetBatches() { + synchronized (this) { + for (FolderPriority key : sequence.keySet()) { + batch.put(key, sequence.get(key)); + if (BuildConfig.DEBUG) + Log.i("=== Reset " + key.folder + ":" + key.priority + " batch=" + batch.get(key)); + } + } } private void yield() { @@ -4209,6 +4229,41 @@ class Core { return (last == null ? 0 : SystemClock.elapsedRealtime() - last); } + long getSequence(long folder, int priority) { + synchronized (this) { + FolderPriority key = new FolderPriority(folder, priority); + if (!sequence.containsKey(key)) { + sequence.put(key, 0L); + batch.put(key, 0L); + } + long result = sequence.get(key); + sequence.put(key, result + 1); + if (BuildConfig.DEBUG) + Log.i("=== Get " + folder + ":" + priority + " sequence=" + result); + return result; + } + } + + boolean batchCanRun(long folder, int priority, long current) { + synchronized (this) { + FolderPriority key = new FolderPriority(folder, priority); + boolean can = batch.get(key).equals(current); + if (BuildConfig.DEBUG) + Log.i("=== Can " + folder + ":" + priority + " can=" + can); + return can; + } + } + + void batchCompleted(long folder, int priority, long current) { + synchronized (this) { + FolderPriority key = new FolderPriority(folder, priority); + if (batch.get(key).equals(current)) + batch.put(key, batch.get(key) + 1); + if (BuildConfig.DEBUG) + Log.i("=== Completed " + folder + ":" + priority + " next=" + batch.get(key)); + } + } + @NonNull @Override public String toString() { diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index 1fac14d110..2e1296d64b 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -1296,6 +1296,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences " operations=" + partitions.get(key).size()); } + final long sequence = state.getSequence(folder.id, key.getPriority()); + handler.post(new Helper.PriorityRunnable(key.getPriority(), key.getOrder()) { @Override public void run() { @@ -1360,7 +1362,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences account, folder, partition, iservice.getStore(), ifolder, - state); + state, key.getPriority(), sequence); } catch (Throwable ex) { Log.e(folder.name, ex); @@ -1614,6 +1616,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences // Stop executing operations Log.i(account.name + " stop executing operations"); ht.quit(); + state.resetBatches(); // Close folders for (EntityFolder folder : mapFolders.keySet())