@Test public void removeLeaderUpdatesTheDatabase() { leaderElector.removeLeaderFor(new ResourceId("test-resource-one")); assertThat(testableLeaderDatabase.databaseHitCount).isEqualTo(1); }
@Override public boolean removeLeaderFor(ResourceId resourceId) { boolean removed = false; try { removed = leaderDatabase.removeLeadershipFor(resourceId); if (removed) { resourceLeaders.put(resourceId, LeaderId.NO_LEADER); registry.counter( leaderRemovalsCounterId .withTag("resource", resourceId.getId()) .withTag("result", "success") ) .increment(); } else { registry.counter( leaderRemovalsCounterId .withTag("resource", resourceId.getId()) .withTag("result", "failure") .withTag("error", "not_leader") ) .increment(); } } catch (Exception e) { final boolean removeLocalLeaderOnError = removeLocalLeaderStatusOnError(); if (removeLocalLeaderOnError) { resourceLeaders.put(resourceId, LeaderId.UNKNOWN); } final String exceptionName = e.getCause() != null ? e.getCause().getClass().getSimpleName() : e.getClass().getSimpleName(); registry.counter( leaderRemovalsCounterId .withTag("resource", resourceId.getId()) .withTag("result", "failure") .withTag("error", exceptionName) ) .increment(); logger.error("Exception during leader removal", e); } return removed; }
StandardLeaderElector implements LeaderElector, LeaderStatus { @Override public boolean removeLeaderFor(ResourceId resourceId) { boolean removed = false; try { removed = leaderDatabase.removeLeadershipFor(resourceId); if (removed) { resourceLeaders.put(resourceId, LeaderId.NO_LEADER); registry.counter( leaderRemovalsCounterId .withTag("resource", resourceId.getId()) .withTag("result", "success") ) .increment(); } else { registry.counter( leaderRemovalsCounterId .withTag("resource", resourceId.getId()) .withTag("result", "failure") .withTag("error", "not_leader") ) .increment(); } } catch (Exception e) { final boolean removeLocalLeaderOnError = removeLocalLeaderStatusOnError(); if (removeLocalLeaderOnError) { resourceLeaders.put(resourceId, LeaderId.UNKNOWN); } final String exceptionName = e.getCause() != null ? e.getCause().getClass().getSimpleName() : e.getClass().getSimpleName(); registry.counter( leaderRemovalsCounterId .withTag("resource", resourceId.getId()) .withTag("result", "failure") .withTag("error", exceptionName) ) .increment(); logger.error("Exception during leader removal", e); } return removed; } }
StandardLeaderElector implements LeaderElector, LeaderStatus { @Override public boolean removeLeaderFor(ResourceId resourceId) { boolean removed = false; try { removed = leaderDatabase.removeLeadershipFor(resourceId); if (removed) { resourceLeaders.put(resourceId, LeaderId.NO_LEADER); registry.counter( leaderRemovalsCounterId .withTag("resource", resourceId.getId()) .withTag("result", "success") ) .increment(); } else { registry.counter( leaderRemovalsCounterId .withTag("resource", resourceId.getId()) .withTag("result", "failure") .withTag("error", "not_leader") ) .increment(); } } catch (Exception e) { final boolean removeLocalLeaderOnError = removeLocalLeaderStatusOnError(); if (removeLocalLeaderOnError) { resourceLeaders.put(resourceId, LeaderId.UNKNOWN); } final String exceptionName = e.getCause() != null ? e.getCause().getClass().getSimpleName() : e.getClass().getSimpleName(); registry.counter( leaderRemovalsCounterId .withTag("resource", resourceId.getId()) .withTag("result", "failure") .withTag("error", exceptionName) ) .increment(); logger.error("Exception during leader removal", e); } return removed; } @Inject  StandardLeaderElector(LeaderDatabase leaderDatabase, Config config, Registry registry);  StandardLeaderElector(
      LeaderId leaderId,
      LeaderDatabase leaderDatabase,
      Config config,
      Registry registry,
      Map<ResourceId, LeaderId> resourceLeaders,
      Id leaderRemovalsCounterId,
      Id resourceLeaderGaugeId,
      Id resourceWithNoLeaderGaugeId
  ); }
StandardLeaderElector implements LeaderElector, LeaderStatus { @Override public boolean removeLeaderFor(ResourceId resourceId) { boolean removed = false; try { removed = leaderDatabase.removeLeadershipFor(resourceId); if (removed) { resourceLeaders.put(resourceId, LeaderId.NO_LEADER); registry.counter( leaderRemovalsCounterId .withTag("resource", resourceId.getId()) .withTag("result", "success") ) .increment(); } else { registry.counter( leaderRemovalsCounterId .withTag("resource", resourceId.getId()) .withTag("result", "failure") .withTag("error", "not_leader") ) .increment(); } } catch (Exception e) { final boolean removeLocalLeaderOnError = removeLocalLeaderStatusOnError(); if (removeLocalLeaderOnError) { resourceLeaders.put(resourceId, LeaderId.UNKNOWN); } final String exceptionName = e.getCause() != null ? e.getCause().getClass().getSimpleName() : e.getClass().getSimpleName(); registry.counter( leaderRemovalsCounterId .withTag("resource", resourceId.getId()) .withTag("result", "failure") .withTag("error", exceptionName) ) .increment(); logger.error("Exception during leader removal", e); } return removed; } @Inject  StandardLeaderElector(LeaderDatabase leaderDatabase, Config config, Registry registry);  StandardLeaderElector(
      LeaderId leaderId,
      LeaderDatabase leaderDatabase,
      Config config,
      Registry registry,
      Map<ResourceId, LeaderId> resourceLeaders,
      Id leaderRemovalsCounterId,
      Id resourceLeaderGaugeId,
      Id resourceWithNoLeaderGaugeId
  ); @Override boolean addResource(ResourceId resourceId); @Override LeaderId getLeaderFor(ResourceId resourceId); @Override Set<ResourceId> getResources(); @Override void initialize(); @Override boolean removeLeaderFor(ResourceId resourceId); @Override boolean removeResource(ResourceId resourceId); @Override void runElection(); @Override boolean hasLeadership(); @Override boolean hasLeadershipFor(ResourceId resourceId); }
StandardLeaderElector implements LeaderElector, LeaderStatus { @Override public boolean removeLeaderFor(ResourceId resourceId) { boolean removed = false; try { removed = leaderDatabase.removeLeadershipFor(resourceId); if (removed) { resourceLeaders.put(resourceId, LeaderId.NO_LEADER); registry.counter( leaderRemovalsCounterId .withTag("resource", resourceId.getId()) .withTag("result", "success") ) .increment(); } else { registry.counter( leaderRemovalsCounterId .withTag("resource", resourceId.getId()) .withTag("result", "failure") .withTag("error", "not_leader") ) .increment(); } } catch (Exception e) { final boolean removeLocalLeaderOnError = removeLocalLeaderStatusOnError(); if (removeLocalLeaderOnError) { resourceLeaders.put(resourceId, LeaderId.UNKNOWN); } final String exceptionName = e.getCause() != null ? e.getCause().getClass().getSimpleName() : e.getClass().getSimpleName(); registry.counter( leaderRemovalsCounterId .withTag("resource", resourceId.getId()) .withTag("result", "failure") .withTag("error", exceptionName) ) .increment(); logger.error("Exception during leader removal", e); } return removed; } @Inject  StandardLeaderElector(LeaderDatabase leaderDatabase, Config config, Registry registry);  StandardLeaderElector(
      LeaderId leaderId,
      LeaderDatabase leaderDatabase,
      Config config,
      Registry registry,
      Map<ResourceId, LeaderId> resourceLeaders,
      Id leaderRemovalsCounterId,
      Id resourceLeaderGaugeId,
      Id resourceWithNoLeaderGaugeId
  ); @Override boolean addResource(ResourceId resourceId); @Override LeaderId getLeaderFor(ResourceId resourceId); @Override Set<ResourceId> getResources(); @Override void initialize(); @Override boolean removeLeaderFor(ResourceId resourceId); @Override boolean removeResource(ResourceId resourceId); @Override void runElection(); @Override boolean hasLeadership(); @Override boolean hasLeadershipFor(ResourceId resourceId);  }