/** * Handle the heartbeat of a provisioner, updating the last heartbeat time of the provisioner and updating the number * of live workers running on the provisioner for each tenant it is responsible for. * * @param provisionerId Id of the provisioner that sent the heartbeat * @param heartbeat The heartbeat containing live worker information * @throws IOException if there was an exception persisting the data * @throws MissingEntityException if there is no provisioner for the given id */ public void handleHeartbeat(String provisionerId, ProvisionerHeartbeat heartbeat) throws IOException, MissingEntityException { // no lock required here. Simply getting a provisioner and writing worker usage. Would only expect one provisioner // to be calling this at a time, and even if it is calling it concurrently for some reason, only the usage can // change and for that its ok for one of them to win. Provisioner provisioner = provisionerStore.getProvisioner(provisionerId); if (provisioner == null) { throw new MissingEntityException("Provisioner " + provisionerId + " not found."); } if (!provisioner.getUsage().equals(heartbeat.getUsage())) { provisioner.setUsage(heartbeat.getUsage()); provisionerStore.writeProvisioner(provisioner); } provisionerStore.setHeartbeat(provisionerId, System.currentTimeMillis()); }
@Test public void testGetIdleProvisioners() throws IOException { ProvisionerStore store = getProvisionerStore(); store.writeProvisioner(provisioner1); store.writeProvisioner(provisioner2); store.setHeartbeat(provisioner1.getId(), 100L); store.setHeartbeat(provisioner2.getId(), 1000L); Assert.assertTrue(store.getTimedOutProvisioners(99L).isEmpty()); Assert.assertEquals(ImmutableSet.of(provisioner1), ImmutableSet.copyOf(store.getTimedOutProvisioners(101L))); Assert.assertEquals(ImmutableSet.of(provisioner1, provisioner2), ImmutableSet.copyOf(store.getTimedOutProvisioners(1001L))); }