2011-12-21 08:44:08 -08:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Android Sync Client.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* the Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2011
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Richard Newman <rnewman@mozilla.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
package org.mozilla.gecko.sync.synchronizer;
|
|
|
|
|
|
|
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
2012-01-14 09:20:31 -08:00
|
|
|
import java.util.concurrent.ExecutorService;
|
2011-12-21 08:44:08 -08:00
|
|
|
|
2012-02-15 22:05:52 -08:00
|
|
|
import org.mozilla.gecko.sync.Logger;
|
2011-12-21 08:44:08 -08:00
|
|
|
import org.mozilla.gecko.sync.ThreadPool;
|
2012-03-05 20:53:14 -08:00
|
|
|
import org.mozilla.gecko.sync.repositories.InvalidSessionTransitionException;
|
2012-01-14 09:20:31 -08:00
|
|
|
import org.mozilla.gecko.sync.repositories.NoStoreDelegateException;
|
2011-12-21 08:44:08 -08:00
|
|
|
import org.mozilla.gecko.sync.repositories.RepositorySession;
|
2012-01-14 09:20:31 -08:00
|
|
|
import org.mozilla.gecko.sync.repositories.delegates.DeferredRepositorySessionBeginDelegate;
|
|
|
|
import org.mozilla.gecko.sync.repositories.delegates.DeferredRepositorySessionStoreDelegate;
|
2011-12-21 08:44:08 -08:00
|
|
|
import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionBeginDelegate;
|
|
|
|
import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionFetchRecordsDelegate;
|
|
|
|
import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionStoreDelegate;
|
|
|
|
import org.mozilla.gecko.sync.repositories.domain.Record;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pulls records from `source`, applying them to `sink`.
|
|
|
|
* Notifies its delegate of errors and completion.
|
|
|
|
*
|
2012-01-14 09:20:31 -08:00
|
|
|
* All stores (initiated by a fetch) must have been completed before storeDone
|
|
|
|
* is invoked on the sink. This is to avoid the existing stored items being
|
|
|
|
* considered as the total set, with onStoreCompleted being called when they're
|
|
|
|
* done:
|
|
|
|
*
|
|
|
|
* store(A) store(B)
|
|
|
|
* store(C) storeDone()
|
|
|
|
* store(A) finishes. Store job begins.
|
|
|
|
* store(C) finishes. Store job begins.
|
|
|
|
* storeDone() finishes.
|
|
|
|
* Storing of A complete.
|
|
|
|
* Storing of C complete.
|
|
|
|
* We're done! Call onStoreCompleted.
|
|
|
|
* store(B) finishes... uh oh.
|
|
|
|
*
|
|
|
|
* In other words, storeDone must be gated on the synchronous invocation of every store.
|
|
|
|
*
|
|
|
|
* Similarly, we require that every store callback have returned before onStoreCompleted is invoked.
|
|
|
|
*
|
|
|
|
* This whole set of guarantees should be achievable thusly:
|
|
|
|
*
|
|
|
|
* * The fetch process must run in a single thread, and invoke store()
|
|
|
|
* synchronously. After processing every incoming record, storeDone is called,
|
|
|
|
* setting a flag.
|
|
|
|
* If the fetch cannot be implicitly queued, it must be explicitly queued.
|
|
|
|
* In this implementation, we assume that fetch callbacks are strictly ordered in this way.
|
|
|
|
*
|
|
|
|
* * The store process must be (implicitly or explicitly) queued. When the
|
|
|
|
* queue empties, the consumer checks the storeDone flag. If it's set, and the
|
|
|
|
* queue is exhausted, invoke onStoreCompleted.
|
|
|
|
*
|
|
|
|
* RecordsChannel exists to enforce this ordering of operations.
|
|
|
|
*
|
2011-12-21 08:44:08 -08:00
|
|
|
* @author rnewman
|
|
|
|
*
|
|
|
|
*/
|
2012-01-14 09:20:31 -08:00
|
|
|
class RecordsChannel implements
|
|
|
|
RepositorySessionFetchRecordsDelegate,
|
|
|
|
RepositorySessionStoreDelegate,
|
|
|
|
RecordsConsumerDelegate,
|
|
|
|
RepositorySessionBeginDelegate {
|
|
|
|
|
2011-12-21 08:44:08 -08:00
|
|
|
private static final String LOG_TAG = "RecordsChannel";
|
|
|
|
public RepositorySession source;
|
|
|
|
public RepositorySession sink;
|
|
|
|
private RecordsChannelDelegate delegate;
|
|
|
|
private long timestamp;
|
2012-02-15 22:05:53 -08:00
|
|
|
private long fetchEnd = -1;
|
2011-12-21 08:44:08 -08:00
|
|
|
|
|
|
|
public RecordsChannel(RepositorySession source, RepositorySession sink, RecordsChannelDelegate delegate) {
|
2012-01-14 09:20:31 -08:00
|
|
|
this.source = source;
|
|
|
|
this.sink = sink;
|
|
|
|
this.delegate = delegate;
|
2011-12-21 08:44:08 -08:00
|
|
|
this.timestamp = source.lastSyncTimestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We push fetched records into a queue.
|
|
|
|
* A separate thread is waiting for us to notify it of work to do.
|
2012-01-14 09:20:31 -08:00
|
|
|
* When we tell it to stop, it'll stop. We do that when the fetch
|
|
|
|
* is completed.
|
|
|
|
* When it stops, we tell the sink that there are no more records,
|
|
|
|
* and wait for the sink to tell us that storing is done.
|
|
|
|
* Then we notify our delegate of completion.
|
2011-12-21 08:44:08 -08:00
|
|
|
*/
|
|
|
|
private RecordConsumer consumer;
|
2012-01-14 09:20:31 -08:00
|
|
|
private boolean waitingForQueueDone = false;
|
|
|
|
private ConcurrentLinkedQueue<Record> toProcess = new ConcurrentLinkedQueue<Record>();
|
2011-12-21 08:44:08 -08:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public ConcurrentLinkedQueue<Record> getQueue() {
|
|
|
|
return toProcess;
|
|
|
|
}
|
|
|
|
|
2012-01-14 09:20:31 -08:00
|
|
|
protected boolean isReady() {
|
|
|
|
return source.isActive() && sink.isActive();
|
|
|
|
}
|
|
|
|
|
2011-12-21 08:44:08 -08:00
|
|
|
/**
|
|
|
|
* Attempt to abort an outstanding fetch. Finish both sessions.
|
|
|
|
*/
|
|
|
|
public void abort() {
|
|
|
|
if (source.isActive()) {
|
|
|
|
source.abort();
|
|
|
|
}
|
|
|
|
if (sink.isActive()) {
|
|
|
|
sink.abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start records flowing through the channel.
|
|
|
|
*/
|
|
|
|
public void flow() {
|
|
|
|
if (!isReady()) {
|
|
|
|
RepositorySession failed = source;
|
|
|
|
if (source.isActive()) {
|
|
|
|
failed = sink;
|
|
|
|
}
|
|
|
|
this.delegate.onFlowBeginFailed(this, new SessionNotBegunException(failed));
|
|
|
|
}
|
2012-01-14 09:20:31 -08:00
|
|
|
sink.setStoreDelegate(this);
|
2011-12-21 08:44:08 -08:00
|
|
|
// Start a consumer thread.
|
2012-01-14 09:20:31 -08:00
|
|
|
this.consumer = new ConcurrentRecordConsumer(this);
|
2011-12-21 08:44:08 -08:00
|
|
|
ThreadPool.run(this.consumer);
|
|
|
|
waitingForQueueDone = true;
|
|
|
|
source.fetchSince(timestamp, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Begin both sessions, invoking flow() when done.
|
2012-03-05 20:53:14 -08:00
|
|
|
* @throws InvalidSessionTransitionException
|
2011-12-21 08:44:08 -08:00
|
|
|
*/
|
2012-03-05 20:53:14 -08:00
|
|
|
public void beginAndFlow() throws InvalidSessionTransitionException {
|
2012-02-15 22:05:52 -08:00
|
|
|
Logger.info(LOG_TAG, "Beginning source.");
|
2011-12-21 08:44:08 -08:00
|
|
|
source.begin(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void store(Record record) {
|
2012-01-14 09:20:31 -08:00
|
|
|
try {
|
|
|
|
sink.store(record);
|
|
|
|
} catch (NoStoreDelegateException e) {
|
2012-02-15 22:05:52 -08:00
|
|
|
Logger.error(LOG_TAG, "Got NoStoreDelegateException in RecordsChannel.store(). This should not occur. Aborting.", e);
|
2012-01-14 09:20:31 -08:00
|
|
|
delegate.onFlowStoreFailed(this, e);
|
|
|
|
this.abort();
|
|
|
|
}
|
2011-12-21 08:44:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFetchFailed(Exception ex, Record record) {
|
2012-02-15 22:05:52 -08:00
|
|
|
Logger.warn(LOG_TAG, "onFetchFailed. Calling for immediate stop.", ex);
|
2012-01-14 09:20:31 -08:00
|
|
|
this.consumer.halt();
|
2011-12-21 08:44:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFetchedRecord(Record record) {
|
2012-01-14 09:20:31 -08:00
|
|
|
this.toProcess.add(record);
|
2011-12-21 08:44:08 -08:00
|
|
|
this.consumer.doNotify();
|
|
|
|
}
|
|
|
|
|
2012-01-14 09:20:31 -08:00
|
|
|
@Override
|
2012-02-15 22:05:53 -08:00
|
|
|
public void onFetchSucceeded(Record[] records, final long fetchEnd) {
|
2012-01-14 09:20:31 -08:00
|
|
|
for (Record record : records) {
|
|
|
|
this.toProcess.add(record);
|
|
|
|
}
|
|
|
|
this.consumer.doNotify();
|
2012-02-15 22:05:53 -08:00
|
|
|
this.onFetchCompleted(fetchEnd);
|
2012-01-14 09:20:31 -08:00
|
|
|
}
|
|
|
|
|
2011-12-21 08:44:08 -08:00
|
|
|
@Override
|
2012-02-15 22:05:53 -08:00
|
|
|
public void onFetchCompleted(final long fetchEnd) {
|
2012-02-15 22:05:52 -08:00
|
|
|
Logger.info(LOG_TAG, "onFetchCompleted. Stopping consumer once stores are done.");
|
2012-02-15 22:05:53 -08:00
|
|
|
Logger.info(LOG_TAG, "Fetch timestamp is " + fetchEnd);
|
|
|
|
this.fetchEnd = fetchEnd;
|
2012-01-14 09:20:31 -08:00
|
|
|
this.consumer.queueFilled();
|
2011-12-21 08:44:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-01-14 09:20:31 -08:00
|
|
|
public void onRecordStoreFailed(Exception ex) {
|
2011-12-21 08:44:08 -08:00
|
|
|
this.consumer.stored();
|
|
|
|
delegate.onFlowStoreFailed(this, ex);
|
|
|
|
// TODO: abort?
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-01-14 09:20:31 -08:00
|
|
|
public void onRecordStoreSucceeded(Record record) {
|
2011-12-21 08:44:08 -08:00
|
|
|
this.consumer.stored();
|
|
|
|
}
|
|
|
|
|
2012-01-14 09:20:31 -08:00
|
|
|
|
2011-12-21 08:44:08 -08:00
|
|
|
@Override
|
2012-01-14 09:20:31 -08:00
|
|
|
public void consumerIsDone(boolean allRecordsQueued) {
|
2012-02-15 22:05:52 -08:00
|
|
|
Logger.trace(LOG_TAG, "Consumer is done. Are we waiting for it? " + waitingForQueueDone);
|
2012-01-14 09:20:31 -08:00
|
|
|
if (waitingForQueueDone) {
|
|
|
|
waitingForQueueDone = false;
|
|
|
|
this.sink.storeDone(); // Now we'll be waiting for onStoreCompleted.
|
2011-12-21 08:44:08 -08:00
|
|
|
}
|
2012-01-14 09:20:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-02-15 22:05:53 -08:00
|
|
|
public void onStoreCompleted(long storeEnd) {
|
|
|
|
Logger.info(LOG_TAG, "onStoreCompleted. Notifying delegate of onFlowCompleted. " +
|
|
|
|
"Fetch end is " + fetchEnd + ", store end is " + storeEnd);
|
2012-01-14 09:20:31 -08:00
|
|
|
// TODO: synchronize on consumer callback?
|
2012-02-15 22:05:53 -08:00
|
|
|
delegate.onFlowCompleted(this, fetchEnd, storeEnd);
|
2011-12-21 08:44:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBeginFailed(Exception ex) {
|
|
|
|
delegate.onFlowBeginFailed(this, ex);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBeginSucceeded(RepositorySession session) {
|
|
|
|
if (session == source) {
|
2012-02-15 22:05:52 -08:00
|
|
|
Logger.info(LOG_TAG, "Source session began. Beginning sink session.");
|
2012-03-05 20:53:14 -08:00
|
|
|
try {
|
|
|
|
sink.begin(this);
|
|
|
|
} catch (InvalidSessionTransitionException e) {
|
|
|
|
onBeginFailed(e);
|
|
|
|
}
|
2011-12-21 08:44:08 -08:00
|
|
|
}
|
|
|
|
if (session == sink) {
|
2012-02-15 22:05:52 -08:00
|
|
|
Logger.info(LOG_TAG, "Sink session began. Beginning flow.");
|
2011-12-21 08:44:08 -08:00
|
|
|
this.flow();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: error!
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-01-14 09:20:31 -08:00
|
|
|
public RepositorySessionStoreDelegate deferredStoreDelegate(final ExecutorService executor) {
|
|
|
|
return new DeferredRepositorySessionStoreDelegate(this, executor);
|
2011-12-21 08:44:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-01-14 09:20:31 -08:00
|
|
|
public RepositorySessionBeginDelegate deferredBeginDelegate(final ExecutorService executor) {
|
|
|
|
return new DeferredRepositorySessionBeginDelegate(this, executor);
|
|
|
|
}
|
2011-12-21 08:44:08 -08:00
|
|
|
|
2012-01-14 09:20:31 -08:00
|
|
|
@Override
|
|
|
|
public RepositorySessionFetchRecordsDelegate deferredFetchDelegate(ExecutorService executor) {
|
|
|
|
// Lie outright. We know that all of our fetch methods are safe.
|
|
|
|
return this;
|
2011-12-21 08:44:08 -08:00
|
|
|
}
|
|
|
|
}
|