Touch DB Android monitor end of replication

374 views
Skip to first unread message

madprocessor

unread,
Jul 18, 2012, 3:45:23 PM7/18/12
to mobile-c...@googlegroups.com
HI there,

How can I  monitor the end of a replication? 
Or even better the progress?

My code looks like this but onSuccess is called before it is ready:

protected void startCouch(){

    TDURLStreamHandlerFactory.registerSelfIgnoreError();

    // start TouchDB

        tdserver = null;

        java.lang.String filesDir = getFilesDir().getAbsolutePath();

        try {

        tdserver = new TDServer(filesDir);

        } catch (IOException e) {

          Log.e(TAG, "Error starting TDServer", e);

        }


        // start TouchDB-Ektorp adapter

        org.ektorp.http.HttpClient httpClient =  new TouchDBHttpClient(tdserver);

        

        dbInstance = new StdCouchDbInstance( httpClient);


        

        // create a local database

        couchDbConnector = dbInstance.createConnector("grocery", true);


        // pull this database to the test replication server

        pullCommand = new ReplicationCommand.Builder()

            .source("http://couchbase.iriscouch.com/grocery-sync")

            .target("grocery")

            .continuous(false)

            .build();


        SBSyncEktorpAsyncTask pullReplication = new SBSyncEktorpAsyncTask() {

        ReplicationStatus repstatus;

@Override

protected void doInBackground() {

repstatus = dbInstance.replicate(pullCommand);

}

@Override

protected void onSuccess() {

Log.i(TAG,"db synced");

Log.i(TAG,"status: " + repstatus.isOk());

}

};


pullReplication.execute();

Log.i(TAG,"started sync");

    }




Thanks
Joerg

Marty Schoch

unread,
Jul 18, 2012, 4:01:06 PM7/18/12
to mobile-c...@googlegroups.com
On Wed, Jul 18, 2012 at 12:45 PM, madprocessor
<joerg.b...@foresee.biz> wrote:
> HI there,
>
> How can I monitor the end of a replication?
> Or even better the progress?

Currently there is no way using the public API. See this thread if
you want a way to monitor it using the internal API:

https://groups.google.com/d/msg/mobile-couchbase/cZN5l4eKEqQ/snFv3hjAiasJ

marty

Martin Häcker

unread,
Jul 19, 2012, 12:42:08 PM7/19/12
to mobile-c...@googlegroups.com
Hi there,

Am 18.07.2012 um 22:01 schrieb Marty Schoch:
>> How can I monitor the end of a replication?
>> Or even better the progress?

I'm currently doing this - feedback welcomed:

void replicateRemoteToLocalDatabase(String urlOfRemoteDatabase, String nameOfLocalDatabase, boolean waitUntilDone) {
try {
Log.i(TAG, "Starting Replication of db: " + nameOfLocalDatabase + " from remote: " + urlOfRemoteDatabase);
long startTime = System.currentTimeMillis();

TDDatabase database = getServer().getDatabaseNamed(nameOfLocalDatabase);
database.open();
TDReplicator replicator = database.getReplicator(new URL(urlOfRemoteDatabase), false, false);
replicator.start();

if (waitUntilDone) {
while(replicator.isRunning()) {
Log.i(TAG, "Waiting for replicator to finish");
Thread.sleep(1000);
}
}

long duration = System.currentTimeMillis() - startTime;
if (waitUntilDone) {
Log.i(TAG, "replication took " + (duration) + " milliseconds or " + (duration / 1000) + " seconds");
}

} catch(Exception e) {
// Really want to catch any exception as it is non critical to the working of the
// device if this fails for any reason (be it an out of memory exception)
Log.e(TAG, "Unable to replicate", e);
}
}

Marty Schoch

unread,
Jul 19, 2012, 1:07:06 PM7/19/12
to mobile-c...@googlegroups.com
I think this approach is reasonable until we get a proper solution in
place. One thing I notice is your comments about catching Exception.
This won't actually catch OutOfMemoryError because it is an Error not
an Exception. Generally catching these is a bad idea anyway.

marty

Martin Häcker

unread,
Jul 20, 2012, 3:31:42 AM7/20/12
to mobile-c...@googlegroups.com
Hi there,

Am 19.07.2012 um 19:07 schrieb Marty Schoch:
> I think this approach is reasonable until we get a proper solution in
> place. One thing I notice is your comments about catching Exception.
> This won't actually catch OutOfMemoryError because it is an Error not
> an Exception. Generally catching these is a bad idea anyway.

Sorry, after several years of not using it, my java is a bit rusty. What would you catch there if you just want to make sure that this code has as little consequences to the rest of the app as possible?

As a side note, this is running in a background thread of course, which is why it's ok to actively wait.

Best Regards,
Martin

Marty Schoch

unread,
Jul 20, 2012, 12:59:21 PM7/20/12
to mobile-c...@googlegroups.com
On Fri, Jul 20, 2012 at 12:31 AM, Martin Häcker
<mhae...@schwarz-online.org> wrote:
> Hi there,
>
> Am 19.07.2012 um 19:07 schrieb Marty Schoch:
>> I think this approach is reasonable until we get a proper solution in
>> place. One thing I notice is your comments about catching Exception.
>> This won't actually catch OutOfMemoryError because it is an Error not
>> an Exception. Generally catching these is a bad idea anyway.
>
> Sorry, after several years of not using it, my java is a bit rusty. What would you catch there if you just want to make sure that this code has as little consequences to the rest of the app as possible?

You can catch Throwable, or Error or OutOfMemoryError specifically,
but none of these are recommended. The question you have to answer is
what you're going to do when you catch it, in this case the only thing
you can do is exit. And that should happen anyway.

The only real solution is to avoid OutOfMemoryErrors in the first place.

marty

aat

unread,
Aug 30, 2012, 1:51:35 PM8/30/12
to mobile-c...@googlegroups.com
Hi,

I use the method  as discussed in this thread to monitor replicator. However, there has been instances when the code goes in to an iteration. I am not sure why, but when I restart my app the replicator works fine. I need help to understand a way to tackle it.

08-30 13:39:09.256: V/TDDatabase(32324): Making request to http://admin:ad...@10.162.4.108:5984/student/_changes?feed=normal&heartbeat=300000
08-30 13:39:09.876: I/ItemListActivity(32324): Waiting for replicator to finish
08-30 13:39:10.876: I/ItemListActivity(32324): Waiting for replicator to finish
08-30 13:39:11.876: I/ItemListActivity(32324): Waiting for replicator to finish
....
08-30 13:42:18.096: I/ItemListActivity(32324): Waiting for replicator to finish
08-30 13:42:18.846: E/TDDatabase(32324): IOException in change tracker
08-30 13:42:18.846: E/TDDatabase(32324): org.apache.http.conn.HttpHostConnectException: Connection to http://10.162.4.108:5984 refused
08-30 13:42:18.846: E/TDDatabase(32324): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
08-30 13:42:18.846: E/TDDatabase(32324): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
08-30 13:42:18.846: E/TDDatabase(32324): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
08-30 13:42:18.846: E/TDDatabase(32324): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
08-30 13:42:18.846: E/TDDatabase(32324): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
08-30 13:42:18.846: E/TDDatabase(32324): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-30 13:42:18.846: E/TDDatabase(32324): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
08-30 13:42:18.846: E/TDDatabase(32324): at com.couchbase.touchdb.replicator.changetracker.TDChangeTracker.run(TDChangeTracker.java:191)
08-30 13:42:18.846: E/TDDatabase(32324): at java.lang.Thread.run(Thread.java:856)
08-30 13:42:18.846: E/TDDatabase(32324): Caused by: java.net.ConnectException: failed to connect to /10.162.4.108 (port 5984): connect failed: ETIMEDOUT (Connection timed out)
08-30 13:42:18.846: E/TDDatabase(32324): at libcore.io.IoBridge.connect(IoBridge.java:114)
08-30 13:42:18.846: E/TDDatabase(32324): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
08-30 13:42:18.846: E/TDDatabase(32324): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
08-30 13:42:18.846: E/TDDatabase(32324): at java.net.Socket.connect(Socket.java:842)
08-30 13:42:18.846: E/TDDatabase(32324): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
08-30 13:42:18.846: E/TDDatabase(32324): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
08-30 13:42:18.846: E/TDDatabase(32324): ... 8 more
08-30 13:42:18.846: E/TDDatabase(32324): Caused by: libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
08-30 13:42:18.846: E/TDDatabase(32324): at libcore.io.Posix.connect(Native Method)
08-30 13:42:18.846: E/TDDatabase(32324): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
08-30 13:42:18.846: E/TDDatabase(32324): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
08-30 13:42:18.846: E/TDDatabase(32324): at libcore.io.IoBridge.connect(IoBridge.java:112)
08-30 13:42:18.846: E/TDDatabase(32324): ... 13 more
08-30 13:42:18.866: V/TDDatabase(32324): Making request to http://admin:ad...@10.162.4.108:5984/student/_changes?feed=normal&heartbeat=300000
08-30 13:42:19.096: I/ItemListActivity(32324): Waiting for replicator to finish
08-30 13:42:20.096: I/ItemListActivity(32324): Waiting for replicator to finish



Thank you.
--AAT

Marty Schoch

unread,
Aug 30, 2012, 2:04:54 PM8/30/12
to mobile-c...@googlegroups.com
On Thu, Aug 30, 2012 at 1:51 PM, aat <ambuj...@gmail.com> wrote:
> Hi,
>
> I use the method as discussed in this thread to monitor replicator.
> However, there has been instances when the code goes in to an iteration. I
> am not sure why, but when I restart my app the replicator works fine. I need
> help to understand a way to tackle it.

I just took a look at the code and right now the change tracker is set
up to keep trying when it encounters an exception. That is why you're
seeing it loop and never exit.

Please log an issue for this on github, and I'll review how the iOS
version is currently handling this. My guess is we should do some
sort of exponential backoff and eventually fail.

marty

aat

unread,
Aug 30, 2012, 2:18:28 PM8/30/12
to mobile-c...@googlegroups.com
Here is the link for the issue:


Thanks
--AAT
Reply all
Reply to author
Forward
0 new messages