Search in sources :

Example 6 with ReplicationState

use of org.openstreetmap.osmosis.replication.common.ReplicationState in project osmosis by openstreetmap.

the class ReplicationLagReader method getLag.

/**
 * Calculate the replication lag and print it to stdout
 */
private void getLag() {
    ReplicationDownloaderConfiguration configuration;
    ReplicationState serverState;
    ReplicationState localState;
    PropertiesPersister localStatePersistor;
    // Instantiate utility objects.
    configuration = new ReplicationDownloaderConfiguration(new File(workingDirectory, CONFIG_FILE));
    // Obtain the server state.
    LOG.fine("Reading current server state.");
    serverState = serverStateReader.getServerState(configuration.getBaseUrl());
    // Build the local state persister which is used for both loading and storing local state.
    localStatePersistor = new PropertiesPersister(new File(workingDirectory, LOCAL_STATE_FILE));
    // If local state isn't available we need to fail because no lag can be calculated.
    if (!localStatePersistor.exists()) {
        throw new OsmosisRuntimeException("Can't read local state.");
    }
    // fetch the local state from the file
    localState = new ReplicationState(localStatePersistor.loadMap());
    // extract the time of the local and the remote state files
    long local = localState.getTimestamp().getTime();
    long server = serverState.getTimestamp().getTime();
    // we assume the server being before the local state while calculating the difference
    long lag = (server - local) / 1000;
    // check if a human readable version is requested
    if (this.humanReadable) {
        if (lag > 86400) {
            // more than a day
            Object[] args = { new Long(lag / 86400), new Long((lag % 86400) / 3600) };
            System.out.println(new MessageFormat("{0} day(s) and {1} hour(s)").format(args));
        } else if (lag > 3600) {
            // morte than an hour
            Object[] args = { new Long(lag / 3600), new Long((lag % 3600) / 60) };
            System.out.println(new MessageFormat("{0} hour(s) and {1} minute(s)").format(args));
        } else if (lag > 60) {
            // more than a minute
            Object[] args = { new Long(lag / 60), new Long(lag % 60) };
            System.out.println(new MessageFormat("{0} minute(s) and {1} second(s)").format(args));
        } else {
            Object[] args = { new Long(lag) };
            // just some seconds
            System.out.println(new MessageFormat("{0} second(s)").format(args));
        }
    } else {
        // print out the raw number of seconds
        System.out.println(lag);
    }
}
Also used : MessageFormat(java.text.MessageFormat) ReplicationDownloaderConfiguration(org.openstreetmap.osmosis.replication.v0_6.impl.ReplicationDownloaderConfiguration) ReplicationState(org.openstreetmap.osmosis.replication.common.ReplicationState) File(java.io.File) OsmosisRuntimeException(org.openstreetmap.osmosis.core.OsmosisRuntimeException) PropertiesPersister(org.openstreetmap.osmosis.core.util.PropertiesPersister)

Example 7 with ReplicationState

use of org.openstreetmap.osmosis.replication.common.ReplicationState in project osmosis by openstreetmap.

the class ReplicationSequenceServerTest method testStartupShutdown.

/**
 * Very basic test that launches the server, runs several replication
 * iterations and then shuts down without connecting any clients.
 *
 * @throws InterruptedException
 *             if processing is interrupted.
 */
@Test
public void testStartupShutdown() throws InterruptedException {
    ReplicationSequenceServer server;
    server = new ReplicationSequenceServer(0);
    server.setChangeSink(new MockReplicationDestination());
    try {
        for (int i = 0; i < 10; i++) {
            ReplicationState state = new ReplicationState();
            Map<String, Object> metaData = new HashMap<String, Object>();
            metaData.put(ReplicationState.META_DATA_KEY, state);
            server.initialize(metaData);
            Thread.sleep(10);
            server.complete();
        }
    } finally {
        server.close();
    }
}
Also used : HashMap(java.util.HashMap) ReplicationState(org.openstreetmap.osmosis.replication.common.ReplicationState) AbstractDataTest(org.openstreetmap.osmosis.testutil.AbstractDataTest) Test(org.junit.Test)

Example 8 with ReplicationState

use of org.openstreetmap.osmosis.replication.common.ReplicationState in project osmosis by openstreetmap.

the class ReplicationDataServerHandler method getReplicationState.

private ReplicationState getReplicationState(long sequenceNumber) {
    PropertiesPersister persister = new PropertiesPersister(getStateFile(sequenceNumber));
    ReplicationState state = new ReplicationState();
    state.load(persister.loadMap());
    return state;
}
Also used : ReplicationState(org.openstreetmap.osmosis.replication.common.ReplicationState) PropertiesPersister(org.openstreetmap.osmosis.core.util.PropertiesPersister)

Example 9 with ReplicationState

use of org.openstreetmap.osmosis.replication.common.ReplicationState in project osmosis by openstreetmap.

the class ReplicationDataClientHandler method loadState.

private ReplicationState loadState(File stateFile) {
    PropertiesPersister persister = new PropertiesPersister(stateFile);
    ReplicationState state = new ReplicationState();
    state.load(persister.loadMap());
    return state;
}
Also used : ReplicationState(org.openstreetmap.osmosis.replication.common.ReplicationState) PropertiesPersister(org.openstreetmap.osmosis.core.util.PropertiesPersister)

Example 10 with ReplicationState

use of org.openstreetmap.osmosis.replication.common.ReplicationState in project osmosis by openstreetmap.

the class ReplicationDataClientHandler method invokeSinkInit.

private void invokeSinkInit() {
    replicationState = new ReplicationState();
    Map<String, Object> metaData = new HashMap<String, Object>(1);
    metaData.put(ReplicationState.META_DATA_KEY, replicationState);
    changeSink.initialize(metaData);
    sinkInitInvoked = true;
}
Also used : HashMap(java.util.HashMap) ReplicationState(org.openstreetmap.osmosis.replication.common.ReplicationState)

Aggregations

ReplicationState (org.openstreetmap.osmosis.replication.common.ReplicationState)10 File (java.io.File)4 PropertiesPersister (org.openstreetmap.osmosis.core.util.PropertiesPersister)4 Date (java.util.Date)3 HashMap (java.util.HashMap)3 OsmosisRuntimeException (org.openstreetmap.osmosis.core.OsmosisRuntimeException)2 ReplicationDownloaderConfiguration (org.openstreetmap.osmosis.replication.v0_6.impl.ReplicationDownloaderConfiguration)2 URL (java.net.URL)1 MessageFormat (java.text.MessageFormat)1 Test (org.junit.Test)1 ChangeContainer (org.openstreetmap.osmosis.core.container.v0_6.ChangeContainer)1 NodeContainer (org.openstreetmap.osmosis.core.container.v0_6.NodeContainer)1 CommonEntityData (org.openstreetmap.osmosis.core.domain.v0_6.CommonEntityData)1 Node (org.openstreetmap.osmosis.core.domain.v0_6.Node)1 OsmUser (org.openstreetmap.osmosis.core.domain.v0_6.OsmUser)1 ChangeAction (org.openstreetmap.osmosis.core.task.common.ChangeAction)1 AbstractDataTest (org.openstreetmap.osmosis.testutil.AbstractDataTest)1