org.apache.sling.discovery.impl.common.heartbeat

## Class HeartbeatHandler

- java.lang.Object
- - org.apache.sling.discovery.base.commons.BaseViewChecker
  - - org.apache.sling.discovery.impl.common.heartbeat.HeartbeatHandler

All Implemented Interfaces:
  Runnable, org.apache.sling.discovery.base.commons.ViewChecker

    @Service(value=HeartbeatHandler.class)
     @Reference(referenceInterface=org.osgi.service.http.HttpService.class,
               cardinality=OPTIONAL_MULTIPLE,
               policy=DYNAMIC)
    public class HeartbeatHandler
    extends org.apache.sling.discovery.base.commons.BaseViewChecker

The heartbeat handler is responsible and capable of issuing both local and remote heartbeats and registers a periodic job with the scheduler for doing so.

Local heartbeats are stored in the repository. Remote heartbeats are POSTs to remote TopologyConnectorServlets.

### Field Summary

Fields
`protected org.apache.sling.discovery.base.connectors.announcement.AnnouncementRegistry` `announcementRegistry`
`protected org.apache.sling.discovery.base.connectors.ping.ConnectorRegistry` `connectorRegistry`
`protected String` `failedEstablishedViewId`
`protected org.apache.sling.discovery.base.commons.PeriodicBackgroundJob` `periodicCheckJob`
`protected org.apache.sling.api.resource.ResourceResolverFactory` `resourceResolverFactory`
`protected org.apache.sling.commons.scheduler.Scheduler` `scheduler`
`protected org.apache.sling.settings.SlingSettingsService` `slingSettingsService`

### Fields inherited from class org.apache.sling.discovery.base.commons.BaseViewChecker

`activated, context, endpoints, lock, logger, NAME, periodicPingJob, PROPERTY_ID_ENDPOINTS, PROPERTY_ID_RUNTIME, PROPERTY_ID_SLING_HOME_PATH, runtimeId, slingId`

### Constructor Summary

Constructors
Constructor and Description
`HeartbeatHandler()`

### Method Summary

All Methods Static Methods Instance Methods Concrete Methods
`protected void` `deactivate()`
`protected void` `doActivate()`
`protected void` `doCheckView()` Check whether the established view matches the reality, ie matches the heartbeats
`protected org.apache.sling.discovery.base.connectors.announcement.AnnouncementRegistry` `getAnnouncementRegistry()`
`protected org.apache.sling.discovery.base.connectors.BaseConfig` `getConnectorConfig()`
`protected org.apache.sling.discovery.base.connectors.ping.ConnectorRegistry` `getConnectorRegistry()`
`protected org.apache.sling.api.resource.ResourceResolverFactory` `getResourceResolverFactory()`
`protected org.apache.sling.commons.scheduler.Scheduler` `getScheduler()`
`protected org.apache.sling.settings.SlingSettingsService` `getSlingSettingsService()`
`void` `initialize(DiscoveryServiceImpl discoveryService, String initialVotingId)` The initialize method is called by the DiscoveryServiceImpl.activate as we require the discoveryService (and the discoveryService has a reference on us - but we cant have circular references in osgi).
`void` `invalidateCurrentEstablishedView()` Mark the current establishedView as invalid - requiring it to be replaced with a new one, be it by another instance or this one, via a new vote
`protected void` `issueClusterLocalHeartbeat()` Issue a cluster local heartbeat (into the repository)
`protected void` `issueHeartbeat()` Issue a heartbeat.
`boolean` `resetLeaderElectionId()` Hook that will cause a reset of the leaderElectionId on next invocation of issueClusterLocalHeartbeat.
`void` `startNewVoting()` Management function to trigger the otherwise algorithm-dependent start of a new voting.

`static HeartbeatHandler`

`testConstructor(org.apache.sling.settings.SlingSettingsService slingSettingsService, org.apache.sling.api.resource.ResourceResolverFactory factory, org.apache.sling.discovery.base.connectors.announcement.AnnouncementRegistry announcementRegistry, org.apache.sling.discovery.base.connectors.ping.ConnectorRegistry connectorRegistry, Config config, org.apache.sling.commons.scheduler.Scheduler scheduler, VotingHandler votingHandler)`
for testing only

`protected void` `updateProperties()`

### Methods inherited from class org.apache.sling.discovery.base.commons.BaseViewChecker

`activate, bindHttpService, checkView, getEndpointsAsString, heartbeatAndCheckView, issueConnectorPings, run, triggerAsyncConnectorPing, unbindHttpService`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### slingSettingsService

    @Reference
    protected org.apache.sling.settings.SlingSettingsService slingSettingsService

#### resourceResolverFactory

    @Reference
    protected org.apache.sling.api.resource.ResourceResolverFactory resourceResolverFactory

#### connectorRegistry

    @Reference
    protected org.apache.sling.discovery.base.connectors.ping.ConnectorRegistry connectorRegistry

#### announcementRegistry

    @Reference
    protected org.apache.sling.discovery.base.connectors.announcement.AnnouncementRegistry announcementRegistry

#### scheduler

    @Reference
    protected org.apache.sling.commons.scheduler.Scheduler scheduler

#### failedEstablishedViewId

    protected String failedEstablishedViewId

#### periodicCheckJob

    protected org.apache.sling.discovery.base.commons.PeriodicBackgroundJob periodicCheckJob

### Constructor Detail

#### HeartbeatHandler

    public HeartbeatHandler()

### Method Detail

#### testConstructor

    public static HeartbeatHandler testConstructor(org.apache.sling.settings.SlingSettingsService slingSettingsService,
                                                   org.apache.sling.api.resource.ResourceResolverFactory factory,
                                                   org.apache.sling.discovery.base.connectors.announcement.AnnouncementRegistry announcementRegistry,
                                                   org.apache.sling.discovery.base.connectors.ping.ConnectorRegistry connectorRegistry,
                                                   Config config,
                                                   org.apache.sling.commons.scheduler.Scheduler scheduler,
                                                   VotingHandler votingHandler)

for testing only

#### getAnnouncementRegistry

    protected org.apache.sling.discovery.base.connectors.announcement.AnnouncementRegistry getAnnouncementRegistry()

Specified by:
  `getAnnouncementRegistry` in class `org.apache.sling.discovery.base.commons.BaseViewChecker`

#### getConnectorConfig

    protected org.apache.sling.discovery.base.connectors.BaseConfig getConnectorConfig()

Specified by:
  `getConnectorConfig` in class `org.apache.sling.discovery.base.commons.BaseViewChecker`

#### getConnectorRegistry

    protected org.apache.sling.discovery.base.connectors.ping.ConnectorRegistry getConnectorRegistry()

Specified by:
  `getConnectorRegistry` in class `org.apache.sling.discovery.base.commons.BaseViewChecker`

#### getResourceResolverFactory

    protected org.apache.sling.api.resource.ResourceResolverFactory getResourceResolverFactory()

Specified by:
  `getResourceResolverFactory` in class `org.apache.sling.discovery.base.commons.BaseViewChecker`

#### getScheduler

    protected org.apache.sling.commons.scheduler.Scheduler getScheduler()

Specified by:
  `getScheduler` in class `org.apache.sling.discovery.base.commons.BaseViewChecker`

#### getSlingSettingsService

    protected org.apache.sling.settings.SlingSettingsService getSlingSettingsService()

Specified by:
  `getSlingSettingsService` in class `org.apache.sling.discovery.base.commons.BaseViewChecker`

#### doActivate

    protected void doActivate()

Overrides:
  `doActivate` in class `org.apache.sling.discovery.base.commons.BaseViewChecker`

#### deactivate

    protected void deactivate()

Overrides:
  `deactivate` in class `org.apache.sling.discovery.base.commons.BaseViewChecker`

#### initialize

    public void initialize(DiscoveryServiceImpl discoveryService,
                           String initialVotingId)

The initialize method is called by the DiscoveryServiceImpl.activate as we require the discoveryService (and the discoveryService has a reference on us - but we cant have circular references in osgi).

The initialVotingId is used to avoid an unnecessary topologyChanged event when starting up an instance in a 1-node cluster: the instance will wait until the first voting has been finished to send the TOPOLOGY_INIT event - BUT even before that the API method getTopology() is open - so if anyone asks for the topology BEFORE the first voting in a 1-node cluster is done, it gets a particular clusterId - that one we aim to reuse for the first voting.

#### resetLeaderElectionId

    public boolean resetLeaderElectionId()

Hook that will cause a reset of the leaderElectionId on next invocation of issueClusterLocalHeartbeat.

Returns:
  true if the leaderElectionId was reset - false if that was not necessary as that happened earlier already and it has not propagated yet to the ./clusterInstances in the meantime

#### issueHeartbeat

    protected void issueHeartbeat()

Issue a heartbeat.

This action consists of first updating the local properties, then issuing a cluster-local heartbeat (within the repository) and then a remote heartbeat (to all the topology connectors which announce this part of the topology to others)

Overrides:
  `issueHeartbeat` in class `org.apache.sling.discovery.base.commons.BaseViewChecker`

#### updateProperties

    protected void updateProperties()

Specified by:
  `updateProperties` in class `org.apache.sling.discovery.base.commons.BaseViewChecker`

#### issueClusterLocalHeartbeat

    protected void issueClusterLocalHeartbeat()

Issue a cluster local heartbeat (into the repository)

#### doCheckView

    protected void doCheckView()

Check whether the established view matches the reality, ie matches the heartbeats

Overrides:
  `doCheckView` in class `org.apache.sling.discovery.base.commons.BaseViewChecker`

#### invalidateCurrentEstablishedView

    public void invalidateCurrentEstablishedView()

Mark the current establishedView as invalid - requiring it to be replaced with a new one, be it by another instance or this one, via a new vote

#### startNewVoting

    public void startNewVoting()

Management function to trigger the otherwise algorithm-dependent start of a new voting. This can make sense when explicitly trying to force a leader change (which is otherwise not allowed by the discovery API)