mobile/android/base/sync/synchronizer/ConcurrentRecordConsumer.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/sync/synchronizer/ConcurrentRecordConsumer.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,122 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.sync.synchronizer;
     1.9 +
    1.10 +import org.mozilla.gecko.background.common.log.Logger;
    1.11 +import org.mozilla.gecko.sync.repositories.domain.Record;
    1.12 +
    1.13 +/**
    1.14 + * Consume records from a queue inside a RecordsChannel, as fast as we can.
    1.15 + * TODO: rewrite this in terms of an ExecutorService and a CompletionService.
    1.16 + * See Bug 713483.
    1.17 + *
    1.18 + * @author rnewman
    1.19 + *
    1.20 + */
    1.21 +class ConcurrentRecordConsumer extends RecordConsumer {
    1.22 +  private static final String LOG_TAG = "CRecordConsumer";
    1.23 +
    1.24 +  /**
    1.25 +   * When this is true and all records have been processed, the consumer
    1.26 +   * will notify its delegate.
    1.27 +   */
    1.28 +  protected boolean allRecordsQueued = false;
    1.29 +  private long counter = 0;
    1.30 +
    1.31 +  public ConcurrentRecordConsumer(RecordsConsumerDelegate delegate) {
    1.32 +    this.delegate = delegate;
    1.33 +  }
    1.34 +
    1.35 +  private Object monitor = new Object();
    1.36 +  @Override
    1.37 +  public void doNotify() {
    1.38 +    synchronized (monitor) {
    1.39 +      monitor.notify();
    1.40 +    }
    1.41 +  }
    1.42 +
    1.43 +  @Override
    1.44 +  public void queueFilled() {
    1.45 +    Logger.debug(LOG_TAG, "Queue filled.");
    1.46 +    synchronized (monitor) {
    1.47 +      this.allRecordsQueued = true;
    1.48 +      monitor.notify();
    1.49 +    }
    1.50 +  }
    1.51 +
    1.52 +  @Override
    1.53 +  public void halt() {
    1.54 +    synchronized (monitor) {
    1.55 +      this.stopImmediately = true;
    1.56 +      monitor.notify();
    1.57 +    }
    1.58 +  }
    1.59 +
    1.60 +  private Object countMonitor = new Object();
    1.61 +  @Override
    1.62 +  public void stored() {
    1.63 +    Logger.trace(LOG_TAG, "Record stored. Notifying.");
    1.64 +    synchronized (countMonitor) {
    1.65 +      counter++;
    1.66 +    }
    1.67 +  }
    1.68 +
    1.69 +  private void consumerIsDone() {
    1.70 +    Logger.debug(LOG_TAG, "Consumer is done. Processed " + counter + ((counter == 1) ? " record." : " records."));
    1.71 +    delegate.consumerIsDone(!allRecordsQueued);
    1.72 +  }
    1.73 +
    1.74 +  @Override
    1.75 +  public void run() {
    1.76 +    Record record;
    1.77 +
    1.78 +    while (true) {
    1.79 +      // The queue is concurrent-safe.
    1.80 +      while ((record = delegate.getQueue().poll()) != null) {
    1.81 +        synchronized (monitor) {
    1.82 +          Logger.trace(LOG_TAG, "run() took monitor.");
    1.83 +          if (stopImmediately) {
    1.84 +            Logger.debug(LOG_TAG, "Stopping immediately. Clearing queue.");
    1.85 +            delegate.getQueue().clear();
    1.86 +            Logger.debug(LOG_TAG, "Notifying consumer.");
    1.87 +            consumerIsDone();
    1.88 +            return;
    1.89 +          }
    1.90 +          Logger.debug(LOG_TAG, "run() dropped monitor.");
    1.91 +        }
    1.92 +
    1.93 +        Logger.trace(LOG_TAG, "Storing record with guid " + record.guid + ".");
    1.94 +        try {
    1.95 +          delegate.store(record);
    1.96 +        } catch (Exception e) {
    1.97 +          // TODO: Bug 709371: track records that failed to apply.
    1.98 +          Logger.error(LOG_TAG, "Caught error in store.", e);
    1.99 +        }
   1.100 +        Logger.trace(LOG_TAG, "Done with record.");
   1.101 +      }
   1.102 +      synchronized (monitor) {
   1.103 +        Logger.trace(LOG_TAG, "run() took monitor.");
   1.104 +
   1.105 +        if (allRecordsQueued) {
   1.106 +          Logger.debug(LOG_TAG, "Done with records and no more to come. Notifying consumerIsDone.");
   1.107 +          consumerIsDone();
   1.108 +          return;
   1.109 +        }
   1.110 +        if (stopImmediately) {
   1.111 +          Logger.debug(LOG_TAG, "Done with records and told to stop immediately. Notifying consumerIsDone.");
   1.112 +          consumerIsDone();
   1.113 +          return;
   1.114 +        }
   1.115 +        try {
   1.116 +          Logger.debug(LOG_TAG, "Not told to stop but no records. Waiting.");
   1.117 +          monitor.wait(10000);
   1.118 +        } catch (InterruptedException e) {
   1.119 +          // TODO
   1.120 +        }
   1.121 +        Logger.trace(LOG_TAG, "run() dropped monitor.");
   1.122 +      }
   1.123 +    }
   1.124 +  }
   1.125 +}

mercurial