You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CeDistributedInformationImpl.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.ce;
  21. import com.hazelcast.spi.exception.RetryableHazelcastException;
  22. import java.util.Map;
  23. import java.util.Set;
  24. import java.util.UUID;
  25. import java.util.concurrent.locks.Lock;
  26. import org.picocontainer.Startable;
  27. import org.sonar.api.utils.log.Logger;
  28. import org.sonar.api.utils.log.Loggers;
  29. import org.sonar.ce.taskprocessor.CeWorker;
  30. import org.sonar.ce.taskprocessor.CeWorkerFactory;
  31. import org.sonar.process.cluster.hz.HazelcastMember;
  32. import org.sonar.process.cluster.hz.HazelcastObjects;
  33. import static org.sonar.core.util.stream.MoreCollectors.toSet;
  34. import static org.sonar.process.cluster.hz.HazelcastObjects.WORKER_UUIDS;
  35. /**
  36. * Provide the set of worker's UUID in a clustered SonarQube instance
  37. */
  38. public class CeDistributedInformationImpl implements CeDistributedInformation, Startable {
  39. private static final Logger LOGGER = Loggers.get(CeDistributedInformationImpl.class);
  40. private final HazelcastMember hazelcastMember;
  41. private final CeWorkerFactory ceCeWorkerFactory;
  42. public CeDistributedInformationImpl(HazelcastMember hazelcastMember, CeWorkerFactory ceCeWorkerFactory) {
  43. this.hazelcastMember = hazelcastMember;
  44. this.ceCeWorkerFactory = ceCeWorkerFactory;
  45. }
  46. @Override
  47. public Set<String> getWorkerUUIDs() {
  48. Set<UUID> connectedWorkerUUIDs = hazelcastMember.getMemberUuids();
  49. return getClusteredWorkerUUIDs().entrySet().stream()
  50. .filter(e -> connectedWorkerUUIDs.contains(e.getKey()))
  51. .map(Map.Entry::getValue)
  52. .flatMap(Set::stream)
  53. .collect(toSet());
  54. }
  55. @Override
  56. public void broadcastWorkerUUIDs() {
  57. Set<CeWorker> workers = ceCeWorkerFactory.getWorkers();
  58. Set<String> workerUuids = workers.stream().map(CeWorker::getUUID).collect(toSet(workers.size()));
  59. getClusteredWorkerUUIDs().put(hazelcastMember.getUuid(), workerUuids);
  60. }
  61. @Override
  62. public Lock acquireCleanJobLock() {
  63. return hazelcastMember.getLock(HazelcastObjects.CE_CLEANING_JOB_LOCK);
  64. }
  65. @Override
  66. public void start() {
  67. // Nothing to do here
  68. }
  69. @Override
  70. public void stop() {
  71. try {
  72. // Removing the worker UUIDs
  73. getClusteredWorkerUUIDs().remove(hazelcastMember.getUuid());
  74. } catch (RetryableHazelcastException e) {
  75. LOGGER.debug("Unable to remove worker UUID from the list of active workers", e.getMessage());
  76. }
  77. }
  78. private Map<UUID, Set<String>> getClusteredWorkerUUIDs() {
  79. return hazelcastMember.getReplicatedMap(WORKER_UUIDS);
  80. }
  81. }