HeartbeatThread.java revision a395dd575518d9e5280fc5d5d5ef47c61b174647
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk/*
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * CDDL HEADER START
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * The contents of this file are subject to the terms of the
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * Common Development and Distribution License, Version 1.0 only
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * (the "License"). You may not use this file except in compliance
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * with the License.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * You can obtain a copy of the license at
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * trunk/opends/resource/legal-notices/OpenDS.LICENSE
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * See the License for the specific language governing permissions
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * and limitations under the License.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * When distributing Covered Code, include this CDDL HEADER in each
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * file and include the License file at
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk * add the following below this CDDL HEADER, with the fields enclosed
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk * by brackets "[]" replaced with your own identifying information:
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk * Portions Copyright [yyyy] [name of copyright owner]
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk * CDDL HEADER END
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk * Copyright 2008 Sun Microsystems, Inc.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkpackage org.opends.server.replication.protocol;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkimport org.opends.server.api.DirectoryThread;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkimport static org.opends.server.loggers.debug.DebugLogger.*;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkimport org.opends.server.loggers.debug.DebugTracer;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkimport java.io.IOException;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk/**
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * This thread publishes a heartbeat message on a given protocol session at
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * regular intervals when there are no other replication messages being
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * published.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkpublic class HeartbeatThread extends DirectoryThread
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk{
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk /**
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * The tracer object for the debug logger.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk private static final DebugTracer TRACER = getTracer();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk /**
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * For test purposes only to simulate loss of heartbeats.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk static private boolean heartbeatsDisabled = false;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk /**
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * The session on which heartbeats are to be sent.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk private ProtocolSession session;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk /**
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * The time in milliseconds between heartbeats.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk private long heartbeatInterval;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk /**
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * Set this to stop the thread.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk private Boolean shutdown = false;
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk /**
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * Create a heartbeat thread.
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk * @param threadName The name of the heartbeat thread.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * @param session The session on which heartbeats are to be sent.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * @param heartbeatInterval The desired interval between heartbeats in
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * milliseconds.
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk public HeartbeatThread(String threadName, ProtocolSession session,
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk long heartbeatInterval)
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk {
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk super(threadName);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk this.session = session;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk this.heartbeatInterval = heartbeatInterval;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk /**
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * {@inheritDoc}
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk @Override
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk public void run()
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk try
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if (debugEnabled())
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk TRACER.debugInfo("Heartbeat thread is starting, interval is %d",
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk heartbeatInterval);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk HeartbeatMessage heartbeatMessage = new HeartbeatMessage();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk while (!shutdown)
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk long now = System.currentTimeMillis();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if (debugEnabled())
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk TRACER.debugVerbose("Heartbeat thread awoke at %d, last message " +
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk "was sent at %d", now, session.getLastPublishTime());
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if (now > session.getLastPublishTime() + heartbeatInterval)
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if (!heartbeatsDisabled)
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if (debugEnabled())
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk TRACER.debugVerbose("Heartbeat sent at %d", now);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk session.publish(heartbeatMessage);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk try
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk long sleepTime = session.getLastPublishTime() +
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk heartbeatInterval - now;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if (sleepTime <= 0)
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk sleepTime = heartbeatInterval;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if (debugEnabled())
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk TRACER.debugVerbose("Heartbeat thread sleeping for %d", sleepTime);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk synchronized (shutdown)
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if (!shutdown)
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk shutdown.wait(sleepTime);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk catch (InterruptedException e)
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk {
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk // Keep looping.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk catch (IOException e)
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk {
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk if (debugEnabled())
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk TRACER.debugInfo("Heartbeat thread could not send a heartbeat.");
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // This will be caught in another thread.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk finally
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if (debugEnabled())
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk TRACER.debugInfo("Heartbeat thread is exiting.");
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk /**
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * Call this method to stop the thread.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * This method is blocking until the thread has stopped.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk public void shutdown()
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk synchronized (shutdown)
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk shutdown.notifyAll();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk shutdown = true;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if (debugEnabled())
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk TRACER.debugInfo("Going to notify Heartbeat thread.");
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if (debugEnabled())
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk {
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk TRACER.debugInfo("Returning from Heartbeat shutdown.");
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk /**
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * For testing purposes only to simulate loss of heartbeats.
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk * @param heartbeatsDisabled Set true to prevent heartbeats from being sent.
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk public static void setHeartbeatsDisabled(boolean heartbeatsDisabled)
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk HeartbeatThread.heartbeatsDisabled = heartbeatsDisabled;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk}
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk